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 */
24 extern struct stats stats;
26 extern int csum_length;
30 extern int always_checksum;
32 extern int cvs_exclude;
36 extern int one_file_system;
37 extern int make_backups;
38 extern int preserve_links;
39 extern int preserve_hard_links;
40 extern int preserve_perms;
41 extern int preserve_devices;
42 extern int preserve_uid;
43 extern int preserve_gid;
44 extern int preserve_times;
45 extern int relative_paths;
46 extern int copy_links;
47 extern int remote_version;
50 static struct exclude_struct **local_exclude_list;
52 static void clean_flist(struct file_list *flist, int strip_root);
55 static void list_file_entry(struct file_struct *f)
57 char perms[11] = "----------";
58 char *perm_map = "rwxrwxrwx";
62 /* this can happen if duplicate names were removed */
66 if (f->mode & (1<<i)) perms[9-i] = perm_map[8-i];
68 if (S_ISLNK(f->mode)) perms[0] = 'l';
69 if (S_ISDIR(f->mode)) perms[0] = 'd';
70 if (S_ISBLK(f->mode)) perms[0] = 'b';
71 if (S_ISCHR(f->mode)) perms[0] = 'c';
72 if (S_ISSOCK(f->mode)) perms[0] = 's';
73 if (S_ISFIFO(f->mode)) perms[0] = 'p';
75 if (preserve_links && S_ISLNK(f->mode)) {
76 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
78 (double)f->length, timestring(f->modtime),
81 rprintf(FINFO,"%s %11.0f %s %s\n",
83 (double)f->length, timestring(f->modtime), f_name(f));
88 int link_stat(const char *Path, STRUCT_STAT *Buffer)
92 return do_stat(Path, Buffer);
94 return do_lstat(Path, Buffer);
97 return do_stat(Path, Buffer);
102 This function is used to check if a file should be included/excluded
103 from the list of files based on its name and type etc
105 static int match_file_name(char *fname,STRUCT_STAT *st)
107 if (check_exclude(fname,local_exclude_list,st)) {
109 rprintf(FINFO,"excluding file %s\n",fname);
115 /* used by the one_file_system code */
116 static dev_t filesystem_dev;
118 static void set_filesystem(char *fname)
121 if (link_stat(fname,&st) != 0) return;
122 filesystem_dev = st.st_dev;
126 static int to_wire_mode(mode_t mode)
128 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
129 return (mode & ~(_S_IFMT)) | 0120000;
134 static mode_t from_wire_mode(int mode)
136 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
137 return (mode & ~(_S_IFMT)) | S_IFLNK;
143 static void send_directory(int f,struct file_list *flist,char *dir);
145 static char *flist_dir;
148 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
151 static time_t last_time;
152 static mode_t last_mode;
153 static dev_t last_rdev;
154 static uid_t last_uid;
155 static gid_t last_gid;
156 static char lastname[MAXPATHLEN];
167 fname = f_name(file);
171 if (file->mode == last_mode) flags |= SAME_MODE;
172 if (file->rdev == last_rdev) flags |= SAME_RDEV;
173 if (file->uid == last_uid) flags |= SAME_UID;
174 if (file->gid == last_gid) flags |= SAME_GID;
175 if (file->modtime == last_time) flags |= SAME_TIME;
177 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
178 l2 = strlen(fname) - l1;
180 if (l1 > 0) flags |= SAME_NAME;
181 if (l2 > 255) flags |= LONG_NAME;
183 /* we must make sure we don't send a zero flags byte or the other
184 end will terminate the flist transfer */
185 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
186 if (flags == 0) flags |= LONG_NAME;
189 if (flags & SAME_NAME)
191 if (flags & LONG_NAME)
195 write_buf(f,fname+l1,l2);
197 write_longint(f,file->length);
198 if (!(flags & SAME_TIME))
199 write_int(f,(int)file->modtime);
200 if (!(flags & SAME_MODE))
201 write_int(f,to_wire_mode(file->mode));
202 if (preserve_uid && !(flags & SAME_UID)) {
204 write_int(f,(int)file->uid);
206 if (preserve_gid && !(flags & SAME_GID)) {
208 write_int(f,(int)file->gid);
210 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
211 write_int(f,(int)file->rdev);
214 if (preserve_links && S_ISLNK(file->mode)) {
215 write_int(f,strlen(file->link));
216 write_buf(f,file->link,strlen(file->link));
220 #if SUPPORT_HARD_LINKS
221 if (preserve_hard_links && S_ISREG(file->mode)) {
222 write_int(f,(int)file->dev);
223 write_int(f,(int)file->inode);
227 if (always_checksum) {
228 write_buf(f,file->sum,csum_length);
231 last_mode = file->mode;
232 last_rdev = file->rdev;
233 last_uid = file->uid;
234 last_gid = file->gid;
235 last_time = file->modtime;
237 strlcpy(lastname,fname,MAXPATHLEN);
238 lastname[MAXPATHLEN-1] = 0;
243 static void receive_file_entry(struct file_struct **fptr,
244 unsigned flags,int f)
246 static time_t last_time;
247 static mode_t last_mode;
248 static dev_t last_rdev;
249 static uid_t last_uid;
250 static gid_t last_gid;
251 static char lastname[MAXPATHLEN];
252 char thisname[MAXPATHLEN];
255 struct file_struct *file;
257 if (flags & SAME_NAME)
260 if (flags & LONG_NAME)
265 file = (struct file_struct *)malloc(sizeof(*file));
266 if (!file) out_of_memory("receive_file_entry");
267 memset((char *)file, 0, sizeof(*file));
270 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
272 strlcpy(thisname,lastname,l1+1);
273 read_sbuf(f,&thisname[l1],l2);
276 strlcpy(lastname,thisname,MAXPATHLEN);
277 lastname[MAXPATHLEN-1] = 0;
279 clean_fname(thisname);
281 if ((p = strrchr(thisname,'/'))) {
282 static char *lastdir;
284 if (lastdir && strcmp(thisname, lastdir)==0) {
285 file->dirname = lastdir;
287 file->dirname = strdup(thisname);
288 lastdir = file->dirname;
290 file->basename = strdup(p+1);
292 file->dirname = NULL;
293 file->basename = strdup(thisname);
296 if (!file->basename) out_of_memory("receive_file_entry 1");
300 file->length = read_longint(f);
301 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
302 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
304 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
306 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
307 if (preserve_devices && IS_DEVICE(file->mode))
308 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
310 if (preserve_links && S_ISLNK(file->mode)) {
312 file->link = (char *)malloc(l+1);
313 if (!file->link) out_of_memory("receive_file_entry 2");
314 read_sbuf(f,file->link,l);
317 #if SUPPORT_HARD_LINKS
318 if (preserve_hard_links && S_ISREG(file->mode)) {
319 file->dev = read_int(f);
320 file->inode = read_int(f);
324 if (always_checksum) {
325 file->sum = (char *)malloc(MD4_SUM_LENGTH);
326 if (!file->sum) out_of_memory("md4 sum");
327 read_buf(f,file->sum,csum_length);
330 last_mode = file->mode;
331 last_rdev = file->rdev;
332 last_uid = file->uid;
333 last_gid = file->gid;
334 last_time = file->modtime;
336 if (!preserve_perms) {
337 extern int orig_umask;
338 /* set an appropriate set of permissions based on original
339 permissions and umask. This emulates what GNU cp does */
340 file->mode &= ~orig_umask;
345 /* determine if a file in a different filesstem should be skipped
346 when one_file_system is set. We bascally only want to include
347 the mount points - but they can be hard to find! */
348 static int skip_filesystem(char *fname, STRUCT_STAT *st)
351 char *p = strrchr(fname, '/');
353 /* skip all but directories */
354 if (!S_ISDIR(st->st_mode)) return 1;
356 /* if its not a subdirectory then allow */
360 if (link_stat(fname, &st2)) {
366 return (st2.st_dev != filesystem_dev);
369 static struct file_struct *make_file(char *fname)
371 struct file_struct *file;
373 char sum[SUM_LENGTH];
375 char cleaned_name[MAXPATHLEN];
377 strlcpy(cleaned_name, fname, MAXPATHLEN);
378 cleaned_name[MAXPATHLEN-1] = 0;
379 clean_fname(cleaned_name);
380 fname = cleaned_name;
382 memset(sum,0,SUM_LENGTH);
384 if (link_stat(fname,&st) != 0) {
386 rprintf(FERROR,"%s: %s\n",
387 fname,strerror(errno));
391 if (S_ISDIR(st.st_mode) && !recurse) {
392 rprintf(FINFO,"skipping directory %s\n",fname);
396 if (one_file_system && st.st_dev != filesystem_dev) {
397 if (skip_filesystem(fname, &st))
401 if (!match_file_name(fname,&st))
405 rprintf(FINFO,"make_file(%s)\n",fname);
407 file = (struct file_struct *)malloc(sizeof(*file));
408 if (!file) out_of_memory("make_file");
409 memset((char *)file,0,sizeof(*file));
411 if ((p = strrchr(fname,'/'))) {
412 static char *lastdir;
414 if (lastdir && strcmp(fname, lastdir)==0) {
415 file->dirname = lastdir;
417 file->dirname = strdup(fname);
418 lastdir = file->dirname;
420 file->basename = strdup(p+1);
423 file->dirname = NULL;
424 file->basename = strdup(fname);
427 file->modtime = st.st_mtime;
428 file->length = st.st_size;
429 file->mode = st.st_mode;
430 file->uid = st.st_uid;
431 file->gid = st.st_gid;
432 file->dev = st.st_dev;
433 file->inode = st.st_ino;
435 file->rdev = st.st_rdev;
439 if (S_ISLNK(st.st_mode)) {
441 char lnk[MAXPATHLEN];
442 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
444 rprintf(FERROR,"readlink %s : %s\n",
445 fname,strerror(errno));
449 file->link = strdup(lnk);
453 if (always_checksum) {
454 file->sum = (char *)malloc(MD4_SUM_LENGTH);
455 if (!file->sum) out_of_memory("md4 sum");
456 /* drat. we have to provide a null checksum for non-regular
457 files in order to be compatible with earlier versions
459 if (S_ISREG(st.st_mode)) {
460 file_checksum(fname,file->sum,st.st_size);
462 memset(file->sum, 0, MD4_SUM_LENGTH);
467 static char *lastdir;
468 if (lastdir && strcmp(lastdir, flist_dir)==0) {
469 file->basedir = lastdir;
471 file->basedir = strdup(flist_dir);
472 lastdir = file->basedir;
475 file->basedir = NULL;
478 if (!S_ISDIR(st.st_mode))
479 stats.total_size += st.st_size;
486 void send_file_name(int f,struct file_list *flist,char *fname,
487 int recursive, unsigned base_flags)
489 struct file_struct *file;
491 file = make_file(fname);
495 if (flist->count >= flist->malloced) {
496 if (flist->malloced < 1000)
497 flist->malloced += 1000;
499 flist->malloced *= 2;
500 flist->files = (struct file_struct **)realloc(flist->files,
501 sizeof(flist->files[0])*
504 out_of_memory("send_file_name");
507 if (strcmp(file->basename,"")) {
508 flist->files[flist->count++] = file;
509 send_file_entry(file,f,base_flags);
512 if (S_ISDIR(file->mode) && recursive) {
513 struct exclude_struct **last_exclude_list = local_exclude_list;
514 send_directory(f,flist,f_name(file));
515 local_exclude_list = last_exclude_list;
522 static void send_directory(int f,struct file_list *flist,char *dir)
526 char fname[MAXPATHLEN];
533 rprintf(FERROR,"opendir(%s): %s\n",
534 dir,strerror(errno));
538 strlcpy(fname,dir,MAXPATHLEN);
540 if (fname[l-1] != '/') {
541 if (l == MAXPATHLEN-1) {
543 rprintf(FERROR,"skipping long-named directory %s\n",fname);
547 strlcat(fname,"/", MAXPATHLEN);
550 p = fname + strlen(fname);
552 local_exclude_list = NULL;
555 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
556 strcpy(p,".cvsignore");
557 local_exclude_list = make_exclude_list(fname,NULL,0,0);
560 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
564 for (di=readdir(d); di; di=readdir(d)) {
565 char *dname = d_name(di);
566 if (strcmp(dname,".")==0 ||
567 strcmp(dname,"..")==0)
569 strlcpy(p,dname,MAXPATHLEN-l);
570 send_file_name(f,flist,fname,recurse,0);
573 if (local_exclude_list) {
574 add_exclude_list("!", &local_exclude_list, 0);
582 struct file_list *send_file_list(int f,int argc,char *argv[])
586 char *p,*dir,*olddir;
587 char lastpath[MAXPATHLEN]="";
588 struct file_list *flist;
591 if (verbose && recurse && !am_server && f != -1) {
592 rprintf(FINFO,"building file list ... ");
596 start_write = stats.total_written;
598 flist = (struct file_list *)malloc(sizeof(flist[0]));
599 if (!flist) out_of_memory("send_file_list");
602 flist->malloced = 1000;
603 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
605 if (!flist->files) out_of_memory("send_file_list");
608 io_start_buffering(f);
611 for (i=0;i<argc;i++) {
612 char fname2[MAXPATHLEN];
613 char *fname = fname2;
615 strlcpy(fname,argv[i],MAXPATHLEN);
618 if (l != 1 && fname[l-1] == '/') {
619 strlcat(fname,".",MAXPATHLEN);
622 if (link_stat(fname,&st) != 0) {
624 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
628 if (S_ISDIR(st.st_mode) && !recurse) {
629 rprintf(FINFO,"skipping directory %s\n",fname);
636 if (!relative_paths) {
637 p = strrchr(fname,'/');
646 } else if (f != -1 && (p=strrchr(fname,'/'))) {
647 /* this ensures we send the intermediate directories,
648 thus getting their permissions right */
650 if (strcmp(lastpath,fname)) {
651 strlcpy(lastpath, fname, sizeof(lastpath));
653 for (p=fname+1; (p=strchr(p,'/')); p++) {
654 int copy_links_saved = copy_links;
657 send_file_name(f, flist, fname, 0, 0);
658 copy_links = copy_links_saved;
670 olddir = push_dir(dir, 1);
674 rprintf(FERROR,"push_dir %s : %s\n",
675 dir,strerror(errno));
683 set_filesystem(fname);
685 if (!recurse || !send_included_file_names(f,flist))
686 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
688 if (olddir != NULL) {
690 if (pop_dir(olddir) != 0) {
691 rprintf(FERROR,"pop_dir %s : %s\n",
692 dir,strerror(errno));
693 exit_cleanup(RERR_FILESELECT);
699 send_file_entry(NULL,f,0);
702 if (verbose && recurse && !am_server && f != -1)
703 rprintf(FINFO,"done\n");
705 clean_flist(flist, 0);
707 /* now send the uid/gid list. This was introduced in protocol
709 if (f != -1 && remote_version >= 15) {
713 /* if protocol version is >= 17 then send the io_error flag */
714 if (f != -1 && remote_version >= 17) {
715 write_int(f, io_error);
720 stats.flist_size = stats.total_written - start_write;
721 stats.num_files = flist->count;
725 rprintf(FINFO,"send_file_list done\n");
731 struct file_list *recv_file_list(int f)
733 struct file_list *flist;
736 extern int list_only;
738 if (verbose && recurse && !am_server) {
739 rprintf(FINFO,"receiving file list ... ");
743 start_read = stats.total_read;
745 flist = (struct file_list *)malloc(sizeof(flist[0]));
750 flist->malloced=1000;
751 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
757 for (flags=read_byte(f); flags; flags=read_byte(f)) {
758 int i = flist->count;
760 if (i >= flist->malloced) {
761 if (flist->malloced < 1000)
762 flist->malloced += 1000;
764 flist->malloced *= 2;
765 flist->files =(struct file_struct **)realloc(flist->files,
766 sizeof(flist->files[0])*
772 receive_file_entry(&flist->files[i],flags,f);
774 if (S_ISREG(flist->files[i]->mode))
775 stats.total_size += flist->files[i]->length;
780 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
785 rprintf(FINFO,"received %d names\n",flist->count);
787 clean_flist(flist, relative_paths);
789 if (verbose && recurse && !am_server) {
790 rprintf(FINFO,"done\n");
793 /* now recv the uid/gid list. This was introduced in protocol version 15 */
794 if (f != -1 && remote_version >= 15) {
795 recv_uid_list(f, flist);
798 /* if protocol version is >= 17 then recv the io_error flag */
799 if (f != -1 && remote_version >= 17) {
800 io_error |= read_int(f);
805 for (i=0;i<flist->count;i++) {
806 list_file_entry(flist->files[i]);
812 rprintf(FINFO,"recv_file_list done\n");
814 stats.flist_size = stats.total_read - start_read;
815 stats.num_files = flist->count;
820 out_of_memory("recv_file_list");
821 return NULL; /* not reached */
825 int file_compare(struct file_struct **f1,struct file_struct **f2)
827 if (!(*f1)->basename && !(*f2)->basename) return 0;
828 if (!(*f1)->basename) return -1;
829 if (!(*f2)->basename) return 1;
830 if ((*f1)->dirname == (*f2)->dirname)
831 return u_strcmp((*f1)->basename, (*f2)->basename);
832 return u_strcmp(f_name(*f1),f_name(*f2));
836 int flist_find(struct file_list *flist,struct file_struct *f)
838 int low=0,high=flist->count-1;
840 if (flist->count <= 0) return -1;
842 while (low != high) {
843 int mid = (low+high)/2;
844 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
845 if (ret == 0) return flist_up(flist, mid);
853 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
854 return flist_up(flist,low);
862 static void free_file(struct file_struct *file)
865 if (file->basename) free(file->basename);
866 if (file->link) free(file->link);
867 if (file->sum) free(file->sum);
868 memset((char *)file, 0, sizeof(*file));
873 * free up all elements in a flist
875 void flist_free(struct file_list *flist)
878 for (i=1;i<flist->count;i++) {
879 free_file(flist->files[i]);
880 free(flist->files[i]);
882 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
884 memset((char *)flist, 0, sizeof(*flist));
890 * This routine ensures we don't have any duplicate names in our file list.
891 * duplicate names can cause corruption because of the pipelining
893 static void clean_flist(struct file_list *flist, int strip_root)
897 if (!flist || flist->count == 0)
900 qsort(flist->files,flist->count,
901 sizeof(flist->files[0]),
902 (int (*)())file_compare);
904 for (i=1;i<flist->count;i++) {
905 if (flist->files[i]->basename &&
906 flist->files[i-1]->basename &&
907 strcmp(f_name(flist->files[i]),
908 f_name(flist->files[i-1])) == 0) {
909 if (verbose > 1 && !am_server)
910 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
911 f_name(flist->files[i-1]),i-1);
912 free_file(flist->files[i]);
917 /* we need to strip off the root directory in the case
918 of relative paths, but this must be done _after_
920 for (i=0;i<flist->count;i++) {
921 if (flist->files[i]->dirname &&
922 flist->files[i]->dirname[0] == '/') {
923 memmove(&flist->files[i]->dirname[0],
924 &flist->files[i]->dirname[1],
925 strlen(flist->files[i]->dirname));
928 if (flist->files[i]->dirname &&
929 !flist->files[i]->dirname[0]) {
930 flist->files[i]->dirname = NULL;
936 if (verbose <= 3) return;
938 for (i=0;i<flist->count;i++) {
939 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
941 NS(flist->files[i]->dirname),
942 NS(flist->files[i]->basename),
943 flist->files[i]->mode,
944 (int)flist->files[i]->length);
950 * return the full filename of a flist entry
952 char *f_name(struct file_struct *f)
954 static char names[10][MAXPATHLEN];
958 if (!f || !f->basename) return NULL;
963 strlcpy(p, f->dirname, MAXPATHLEN);
964 strlcat(p, "/", MAXPATHLEN);
965 strlcat(p, f->basename, MAXPATHLEN);
967 strlcpy(p, f->basename, MAXPATHLEN);