2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* generate and receive file lists */
26 extern int always_checksum;
27 extern off_t total_size;
29 extern int cvs_exclude;
31 extern int one_file_system;
32 extern int make_backups;
33 extern int preserve_links;
34 extern int preserve_perms;
35 extern int preserve_devices;
36 extern int preserve_uid;
37 extern int preserve_gid;
38 extern int preserve_times;
40 static char **local_exclude_list = NULL;
42 static void clean_fname(char *name);
46 This function is used to check if a file should be included/excluded
47 from the list of files based on its name and type etc
49 static int match_file_name(char *fname,struct stat *st)
51 if (check_exclude(fname,local_exclude_list)) {
53 fprintf(stderr,"excluding file %s\n",fname);
59 /* used by the one_file_system code */
60 static dev_t filesystem_dev;
62 static void set_filesystem(char *fname)
65 if (lstat(fname,&st) != 0) return;
66 filesystem_dev = st.st_dev;
70 static void send_directory(int f,struct file_list *flist,char *dir);
72 static char *flist_dir = NULL;
75 #define SAME_MODE (1<<1)
76 #define SAME_DEV (1<<2)
77 #define SAME_UID (1<<3)
78 #define SAME_GID (1<<4)
79 #define SAME_DIR (1<<5)
81 static void send_file_entry(struct file_struct *file,int f)
84 static mode_t last_mode=0;
85 static dev_t last_dev=0;
86 static uid_t last_uid=0;
87 static gid_t last_gid=0;
88 static char lastdir[MAXPATHLEN]="";
100 if (file->mode == last_mode) flags |= SAME_MODE;
101 if (file->dev == last_dev) flags |= SAME_DEV;
102 if (file->uid == last_uid) flags |= SAME_UID;
103 if (file->gid == last_gid) flags |= SAME_GID;
105 if (strncmp(file->name,lastdir,strlen(lastdir)) == 0) {
107 p = file->name + strlen(lastdir);
113 if (flags & SAME_DIR)
114 write_byte(f,strlen(p));
116 write_int(f,strlen(p));
117 write_buf(f,p,strlen(p));
118 write_int(f,(int)file->modtime);
119 write_int(f,(int)file->length);
120 if (!(flags & SAME_MODE))
121 write_int(f,(int)file->mode);
122 if (preserve_uid && !(flags & SAME_UID))
123 write_int(f,(int)file->uid);
124 if (preserve_gid && !(flags & SAME_GID))
125 write_int(f,(int)file->gid);
126 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV))
127 write_int(f,(int)file->dev);
130 if (preserve_links && S_ISLNK(file->mode)) {
131 write_int(f,strlen(file->link));
132 write_buf(f,file->link,strlen(file->link));
136 if (always_checksum) {
137 write_buf(f,file->sum,SUM_LENGTH);
140 last_mode = file->mode;
141 last_dev = file->dev;
142 last_uid = file->uid;
143 last_gid = file->gid;
144 p = strrchr(file->name,'/');
146 int l = (int)(p - file->name) + 1;
147 strncpy(lastdir,file->name,l);
156 static void receive_file_entry(struct file_struct *file,
157 unsigned char flags,int f)
159 static mode_t last_mode=0;
160 static dev_t last_dev=0;
161 static uid_t last_uid=0;
162 static gid_t last_gid=0;
163 static char lastdir[MAXPATHLEN]="";
167 if (flags & SAME_DIR) {
169 l2 = strlen(lastdir);
175 file->name = (char *)malloc(l1+l2+1);
176 if (!file->name) out_of_memory("receive_file_entry");
178 strncpy(file->name,lastdir,l2);
179 read_buf(f,file->name+l2,l1);
180 file->name[l1+l2] = 0;
182 file->modtime = (time_t)read_int(f);
183 file->length = (off_t)read_int(f);
184 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
186 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
188 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
189 if (preserve_devices && IS_DEVICE(file->mode))
190 file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f);
193 if (preserve_links && S_ISLNK(file->mode)) {
195 file->link = (char *)malloc(l+1);
196 if (!file->link) out_of_memory("receive_file_entry");
197 read_buf(f,file->link,l);
203 read_buf(f,file->sum,SUM_LENGTH);
205 last_mode = file->mode;
206 last_dev = file->dev;
207 last_uid = file->uid;
208 last_gid = file->gid;
209 p = strrchr(file->name,'/');
211 int l = (int)(p - file->name) + 1;
212 strncpy(lastdir,file->name,l);
220 static struct file_struct *make_file(int recurse,char *fname)
222 static struct file_struct file;
224 char sum[SUM_LENGTH];
226 bzero(sum,SUM_LENGTH);
228 if (lstat(fname,&st) != 0) {
229 fprintf(stderr,"%s: %s\n",
230 fname,strerror(errno));
234 if (S_ISDIR(st.st_mode) && !recurse) {
235 fprintf(stderr,"skipping directory %s\n",fname);
239 if (one_file_system && st.st_dev != filesystem_dev)
242 if (!match_file_name(fname,&st))
246 fprintf(stderr,"make_file(%s)\n",fname);
248 file.name = strdup(fname);
249 file.modtime = st.st_mtime;
250 file.length = st.st_size;
251 file.mode = st.st_mode;
252 file.uid = st.st_uid;
253 file.gid = st.st_gid;
255 file.dev = st.st_rdev;
259 if (S_ISLNK(st.st_mode)) {
261 char lnk[MAXPATHLEN];
262 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
263 fprintf(stderr,"readlink %s : %s\n",fname,strerror(errno));
267 file.link = strdup(lnk);
271 if (always_checksum && S_ISREG(st.st_mode)) {
272 file_checksum(fname,file.sum,st.st_size);
276 file.dir = strdup(flist_dir);
280 if (!S_ISDIR(st.st_mode))
281 total_size += st.st_size;
288 static void send_file_name(int f,struct file_list *flist,
289 int recurse,char *fname)
291 struct file_struct *file;
293 file = make_file(recurse,fname);
297 if (flist->count >= flist->malloced) {
298 flist->malloced += 100;
299 flist->files = (struct file_struct *)realloc(flist->files,
300 sizeof(flist->files[0])*
303 out_of_memory("send_file_name");
306 if (strcmp(file->name,".") && strcmp(file->name,"/")) {
307 flist->files[flist->count++] = *file;
308 send_file_entry(file,f);
311 if (S_ISDIR(file->mode) && recurse) {
312 char **last_exclude_list = local_exclude_list;
313 send_directory(f,flist,file->name);
314 local_exclude_list = last_exclude_list;
321 static void send_directory(int f,struct file_list *flist,char *dir)
325 char fname[MAXPATHLEN];
331 fprintf(stderr,"%s: %s\n",
332 dir,strerror(errno));
338 if (fname[l-1] != '/')
340 p = fname + strlen(fname);
343 strcpy(p,".cvsignore");
344 local_exclude_list = make_exclude_list(fname,NULL,0);
347 for (di=readdir(d); di; di=readdir(d)) {
348 if (strcmp(di->d_name,".")==0 ||
349 strcmp(di->d_name,"..")==0)
351 strcpy(p,di->d_name);
352 send_file_name(f,flist,1,fname);
360 struct file_list *send_file_list(int f,int recurse,int argc,char *argv[])
365 char dbuf[MAXPATHLEN];
366 struct file_list *flist;
368 if (verbose && recurse) {
369 fprintf(am_server?stderr:stdout,"building file list ... ");
370 fflush(am_server?stderr:stdout);
373 flist = (struct file_list *)malloc(sizeof(flist[0]));
374 if (!flist) out_of_memory("send_file_list");
377 flist->malloced = 100;
378 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
380 if (!flist->files) out_of_memory("send_file_list");
382 for (i=0;i<argc;i++) {
383 char fname2[MAXPATHLEN];
384 char *fname = fname2;
386 strcpy(fname,argv[i]);
389 if (l != 1 && fname[l-1] == '/') {
393 if (lstat(fname,&st) != 0) {
394 fprintf(stderr,"%s : %s\n",fname,strerror(errno));
398 if (S_ISDIR(st.st_mode) && !recurse) {
399 fprintf(stderr,"skipping directory %s\n",fname);
404 p = strrchr(fname,'/');
414 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
415 fprintf(stderr,"getwd : %s\n",strerror(errno));
418 if (chdir(dir) != 0) {
419 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
424 set_filesystem(fname);
425 send_file_name(f,flist,recurse,fname);
427 if (chdir(dbuf) != 0) {
428 fprintf(stderr,"chdir %s : %s\n",dbuf,strerror(errno));
435 set_filesystem(fname);
436 send_file_name(f,flist,recurse,fname);
440 send_file_entry(NULL,f);
446 if (verbose && recurse)
447 fprintf(am_server?stderr:stdout,"done\n");
453 struct file_list *recv_file_list(int f)
455 struct file_list *flist;
459 fprintf(stderr,"recv_file_list starting\n");
461 flist = (struct file_list *)malloc(sizeof(flist[0]));
467 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
473 for (flags=read_byte(f); flags; flags=read_byte(f)) {
474 int i = flist->count;
476 if (i >= flist->malloced) {
477 flist->malloced += 100;
478 flist->files =(struct file_struct *)realloc(flist->files,
479 sizeof(flist->files[0])*
485 receive_file_entry(&flist->files[i],flags,f);
487 if (S_ISREG(flist->files[i].mode))
488 total_size += flist->files[i].length;
493 fprintf(stderr,"recv_file_name(%s)\n",flist->files[i].name);
498 fprintf(stderr,"received %d names\n",flist->count);
505 out_of_memory("recv_file_list");
506 return NULL; /* not reached */
510 static int flist_compare(struct file_struct *f1,struct file_struct *f2)
512 if (!f1->name && !f2->name) return 0;
513 if (!f1->name) return -1;
514 if (!f2->name) return 1;
515 return strcmp(f1->name,f2->name);
519 int flist_find(struct file_list *flist,struct file_struct *f)
521 int low=0,high=flist->count;
523 while (low != high) {
524 int mid = (low+high)/2;
525 int ret = flist_compare(&flist->files[mid],f);
526 if (ret == 0) return mid;
532 if (flist_compare(&flist->files[low],f) == 0)
538 static void clean_fname(char *name)
549 if ((p=strstr(name,"/./"))) {
557 if ((p=strstr(name,"//"))) {
565 if (strncmp(p=name,"./",2) == 0) {
574 if (l > 1 && p[l-1] == '/') {
583 * This routine ensures we don't have any duplicate names in our file list.
584 * duplicate names can cause corruption because of the pipelining
586 void clean_flist(struct file_list *flist)
590 if (!flist || flist->count == 0)
593 for (i=0;i<flist->count;i++) {
594 clean_fname(flist->files[i].name);
597 qsort(flist->files,flist->count,
598 sizeof(flist->files[0]),
599 (int (*)())flist_compare);
601 for (i=1;i<flist->count;i++) {
602 if (flist->files[i].name &&
603 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
604 if (verbose > 1 && !am_server)
605 fprintf(stderr,"removing duplicate name %s from file list\n",
606 flist->files[i].name);
607 free(flist->files[i-1].name);
608 flist->files[i-1].name = NULL;