2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Generate and receive file lists
24 * @todo Get rid of the string_area optimization. Efficiently
25 * allocating blocks is the responsibility of the system's malloc
26 * library, not of rsync.
32 extern struct stats stats;
36 extern int always_checksum;
38 extern int cvs_exclude;
42 extern int one_file_system;
43 extern int make_backups;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_perms;
47 extern int preserve_devices;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int preserve_times;
51 extern int relative_paths;
52 extern int copy_links;
53 extern int copy_unsafe_links;
54 extern int remote_version;
56 extern int sanitize_paths;
58 extern int read_batch;
59 extern int write_batch;
61 static char topsrcname[MAXPATHLEN];
63 static struct exclude_struct **local_exclude_list;
65 static struct file_struct null_file;
67 static void clean_flist(struct file_list *flist, int strip_root);
69 static struct string_area *string_area_new(int size)
71 struct string_area *a;
73 if (size <= 0) size = ARENA_SIZE;
74 a = malloc(sizeof(*a));
75 if (!a) out_of_memory("string_area_new");
76 a->current = a->base = malloc(size);
77 if (!a->current) out_of_memory("string_area_new buffer");
78 a->end = a->base + size;
84 static void string_area_free(struct string_area *a)
86 struct string_area *next;
88 for ( ; a ; a = next) {
94 static char *string_area_malloc(struct string_area **ap, int size)
97 struct string_area *a;
99 /* does the request fit into the current space? */
101 if (a->current + size >= a->end) {
102 /* no; get space, move new string_area to front of the list */
103 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
108 /* have space; do the "allocation." */
114 static char *string_area_strdup(struct string_area **ap, const char *src)
116 char* dest = string_area_malloc(ap, strlen(src) + 1);
117 return strcpy(dest, src);
120 static void list_file_entry(struct file_struct *f)
125 /* this can happen if duplicate names were removed */
128 permstring(perms, f->mode);
130 if (preserve_links && S_ISLNK(f->mode)) {
131 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
133 (double)f->length, timestring(f->modtime),
136 rprintf(FINFO,"%s %11.0f %s %s\n",
138 (double)f->length, timestring(f->modtime), f_name(f));
143 int readlink_stat(const char *Path, STRUCT_STAT *Buffer, char *Linkbuf)
147 return do_stat(Path, Buffer);
149 if (do_lstat(Path, Buffer) == -1) {
152 if (S_ISLNK(Buffer->st_mode)) {
154 if ((l = readlink((char *) Path, Linkbuf, MAXPATHLEN-1))== -1) {
158 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
159 unsafe_symlink(Linkbuf, topsrcname)) {
160 return do_stat(Path, Buffer);
165 return do_stat(Path, Buffer);
169 int link_stat(const char *Path, STRUCT_STAT *Buffer)
173 return do_stat(Path, Buffer);
175 return do_lstat(Path, Buffer);
178 return do_stat(Path, Buffer);
183 This function is used to check if a file should be included/excluded
184 from the list of files based on its name and type etc
186 static int check_exclude_file(int f,char *fname,STRUCT_STAT *st)
188 extern int delete_excluded;
190 /* f is set to -1 when calculating deletion file list */
191 if ((f == -1) && delete_excluded) {
194 if (check_exclude(fname,local_exclude_list,st)) {
200 /* used by the one_file_system code */
201 static dev_t filesystem_dev;
203 static void set_filesystem(char *fname)
206 if (link_stat(fname,&st) != 0) return;
207 filesystem_dev = st.st_dev;
211 static int to_wire_mode(mode_t mode)
213 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
214 return (mode & ~(_S_IFMT)) | 0120000;
219 static mode_t from_wire_mode(int mode)
221 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
222 return (mode & ~(_S_IFMT)) | _S_IFLNK;
228 static void send_directory(int f,struct file_list *flist,char *dir);
230 static char *flist_dir;
233 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
236 static time_t last_time;
237 static mode_t last_mode;
238 static dev_t last_rdev;
239 static uid_t last_uid;
240 static gid_t last_gid;
241 static char lastname[MAXPATHLEN];
252 fname = f_name(file);
256 if (file->mode == last_mode) flags |= SAME_MODE;
257 if (file->rdev == last_rdev) flags |= SAME_RDEV;
258 if (file->uid == last_uid) flags |= SAME_UID;
259 if (file->gid == last_gid) flags |= SAME_GID;
260 if (file->modtime == last_time) flags |= SAME_TIME;
262 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
263 l2 = strlen(fname) - l1;
265 if (l1 > 0) flags |= SAME_NAME;
266 if (l2 > 255) flags |= LONG_NAME;
268 /* we must make sure we don't send a zero flags byte or the other
269 end will terminate the flist transfer */
270 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
271 if (flags == 0) flags |= LONG_NAME;
274 if (flags & SAME_NAME)
276 if (flags & LONG_NAME)
280 write_buf(f,fname+l1,l2);
282 write_longint(f,file->length);
283 if (!(flags & SAME_TIME))
284 write_int(f,(int)file->modtime);
285 if (!(flags & SAME_MODE))
286 write_int(f,to_wire_mode(file->mode));
287 if (preserve_uid && !(flags & SAME_UID)) {
289 write_int(f,(int)file->uid);
291 if (preserve_gid && !(flags & SAME_GID)) {
293 write_int(f,(int)file->gid);
295 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
296 write_int(f,(int)file->rdev);
299 if (preserve_links && S_ISLNK(file->mode)) {
300 write_int(f,strlen(file->link));
301 write_buf(f,file->link,strlen(file->link));
305 #if SUPPORT_HARD_LINKS
306 if (preserve_hard_links && S_ISREG(file->mode)) {
307 if (remote_version < 26) {
308 /* 32-bit dev_t and ino_t */
309 write_int(f,(int)file->dev);
310 write_int(f,(int)file->inode);
312 /* 64-bit dev_t and ino_t */
313 write_longint(f, file->dev);
314 write_longint(f, file->inode);
319 if (always_checksum) {
320 if (remote_version < 21) {
321 write_buf(f,file->sum,2);
323 write_buf(f,file->sum,MD4_SUM_LENGTH);
327 last_mode = file->mode;
328 last_rdev = file->rdev;
329 last_uid = file->uid;
330 last_gid = file->gid;
331 last_time = file->modtime;
333 strlcpy(lastname,fname,MAXPATHLEN);
334 lastname[MAXPATHLEN-1] = 0;
339 static void receive_file_entry(struct file_struct **fptr,
340 unsigned flags,int f)
342 static time_t last_time;
343 static mode_t last_mode;
344 static dev_t last_rdev;
345 static uid_t last_uid;
346 static gid_t last_gid;
347 static char lastname[MAXPATHLEN];
348 char thisname[MAXPATHLEN];
351 struct file_struct *file;
353 if (flags & SAME_NAME)
356 if (flags & LONG_NAME)
361 file = (struct file_struct *)malloc(sizeof(*file));
362 if (!file) out_of_memory("receive_file_entry");
363 memset((char *)file, 0, sizeof(*file));
366 if (l2 >= MAXPATHLEN-l1) {
367 rprintf(FERROR,"overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
368 flags, l1, l2, lastname);
369 overflow("receive_file_entry");
372 strlcpy(thisname,lastname,l1+1);
373 read_sbuf(f,&thisname[l1],l2);
376 strlcpy(lastname,thisname,MAXPATHLEN);
377 lastname[MAXPATHLEN-1] = 0;
379 clean_fname(thisname);
381 if (sanitize_paths) {
382 sanitize_path(thisname, NULL);
385 if ((p = strrchr(thisname,'/'))) {
386 static char *lastdir;
388 if (lastdir && strcmp(thisname, lastdir)==0) {
389 file->dirname = lastdir;
391 file->dirname = strdup(thisname);
392 lastdir = file->dirname;
394 file->basename = strdup(p+1);
396 file->dirname = NULL;
397 file->basename = strdup(thisname);
400 if (!file->basename) out_of_memory("receive_file_entry 1");
404 file->length = read_longint(f);
405 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
406 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
408 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
410 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
411 if (preserve_devices && IS_DEVICE(file->mode))
412 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
414 if (preserve_links && S_ISLNK(file->mode)) {
416 file->link = (char *)malloc(l+1);
417 if (!file->link) out_of_memory("receive_file_entry 2");
418 read_sbuf(f,file->link,l);
419 if (sanitize_paths) {
420 sanitize_path(file->link, file->dirname);
424 #if SUPPORT_HARD_LINKS
425 if (preserve_hard_links && S_ISREG(file->mode)) {
426 if (remote_version < 26) {
427 file->dev = read_int(f);
428 file->inode = read_int(f);
430 file->dev = read_longint(f);
431 file->inode = read_longint(f);
436 if (always_checksum) {
437 file->sum = (char *)malloc(MD4_SUM_LENGTH);
438 if (!file->sum) out_of_memory("md4 sum");
439 if (remote_version < 21) {
440 read_buf(f,file->sum,2);
442 read_buf(f,file->sum,MD4_SUM_LENGTH);
446 last_mode = file->mode;
447 last_rdev = file->rdev;
448 last_uid = file->uid;
449 last_gid = file->gid;
450 last_time = file->modtime;
452 if (!preserve_perms) {
453 extern int orig_umask;
454 /* set an appropriate set of permissions based on original
455 permissions and umask. This emulates what GNU cp does */
456 file->mode &= ~orig_umask;
461 /* determine if a file in a different filesstem should be skipped
462 when one_file_system is set. We bascally only want to include
463 the mount points - but they can be hard to find! */
464 static int skip_filesystem(char *fname, STRUCT_STAT *st)
467 char *p = strrchr(fname, '/');
469 /* skip all but directories */
470 if (!S_ISDIR(st->st_mode)) return 1;
472 /* if its not a subdirectory then allow */
476 if (link_stat(fname, &st2)) {
482 return (st2.st_dev != filesystem_dev);
485 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
486 /* IRIX cc cares that the operands to the ternary have the same type. */
487 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
489 /* create a file_struct for a named file */
490 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
493 struct file_struct *file;
495 char sum[SUM_LENGTH];
497 char cleaned_name[MAXPATHLEN];
498 char linkbuf[MAXPATHLEN];
499 extern int module_id;
501 strlcpy(cleaned_name, fname, MAXPATHLEN);
502 cleaned_name[MAXPATHLEN-1] = 0;
503 clean_fname(cleaned_name);
504 if (sanitize_paths) {
505 sanitize_path(cleaned_name, NULL);
507 fname = cleaned_name;
509 memset(sum,0,SUM_LENGTH);
511 if (readlink_stat(fname,&st,linkbuf) != 0) {
512 int save_errno = errno;
513 if ((errno == ENOENT) && copy_links && !noexcludes) {
514 /* symlink pointing nowhere, see if excluded */
515 memset((char *)&st, 0, sizeof(st));
516 if (check_exclude_file(f,fname,&st)) {
517 /* file is excluded anyway, ignore silently */
522 rprintf(FERROR,"readlink %s: %s\n",
523 fname,strerror(save_errno));
527 /* we use noexcludes from backup.c */
528 if (noexcludes) goto skip_excludes;
530 if (S_ISDIR(st.st_mode) && !recurse) {
531 rprintf(FINFO,"skipping directory %s\n",fname);
535 if (one_file_system && st.st_dev != filesystem_dev) {
536 if (skip_filesystem(fname, &st))
540 if (check_exclude_file(f,fname,&st))
544 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
550 rprintf(FINFO,"make_file(%d,%s)\n",f,fname);
552 file = (struct file_struct *)malloc(sizeof(*file));
553 if (!file) out_of_memory("make_file");
554 memset((char *)file,0,sizeof(*file));
556 if ((p = strrchr(fname,'/'))) {
557 static char *lastdir;
559 if (lastdir && strcmp(fname, lastdir)==0) {
560 file->dirname = lastdir;
562 file->dirname = strdup(fname);
563 lastdir = file->dirname;
565 file->basename = STRDUP(ap, p+1);
568 file->dirname = NULL;
569 file->basename = STRDUP(ap, fname);
572 file->modtime = st.st_mtime;
573 file->length = st.st_size;
574 file->mode = st.st_mode;
575 file->uid = st.st_uid;
576 file->gid = st.st_gid;
577 file->dev = st.st_dev;
578 file->inode = st.st_ino;
580 file->rdev = st.st_rdev;
584 if (S_ISLNK(st.st_mode)) {
585 file->link = STRDUP(ap, linkbuf);
589 if (always_checksum) {
590 file->sum = (char *)MALLOC(ap, MD4_SUM_LENGTH);
591 if (!file->sum) out_of_memory("md4 sum");
592 /* drat. we have to provide a null checksum for non-regular
593 files in order to be compatible with earlier versions
595 if (S_ISREG(st.st_mode)) {
596 file_checksum(fname,file->sum,st.st_size);
598 memset(file->sum, 0, MD4_SUM_LENGTH);
603 static char *lastdir;
604 if (lastdir && strcmp(lastdir, flist_dir)==0) {
605 file->basedir = lastdir;
607 file->basedir = strdup(flist_dir);
608 lastdir = file->basedir;
611 file->basedir = NULL;
614 if (!S_ISDIR(st.st_mode))
615 stats.total_size += st.st_size;
622 void send_file_name(int f,struct file_list *flist,char *fname,
623 int recursive, unsigned base_flags)
625 struct file_struct *file;
627 file = make_file(f,fname, &flist->string_area, 0);
631 if (flist->count >= flist->malloced) {
632 if (flist->malloced < 1000)
633 flist->malloced += 1000;
635 flist->malloced *= 2;
636 flist->files = (struct file_struct **)realloc(flist->files,
637 sizeof(flist->files[0])*
640 out_of_memory("send_file_name");
643 if (write_batch) /* dw */
644 file->flags = FLAG_DELETE;
646 if (strcmp(file->basename,"")) {
647 flist->files[flist->count++] = file;
648 send_file_entry(file,f,base_flags);
651 if (S_ISDIR(file->mode) && recursive) {
652 struct exclude_struct **last_exclude_list = local_exclude_list;
653 send_directory(f,flist,f_name(file));
654 local_exclude_list = last_exclude_list;
661 static void send_directory(int f,struct file_list *flist,char *dir)
665 char fname[MAXPATHLEN];
672 rprintf(FERROR,"opendir(%s): %s\n",
673 dir,strerror(errno));
677 strlcpy(fname,dir,MAXPATHLEN);
679 if (fname[l-1] != '/') {
680 if (l == MAXPATHLEN-1) {
682 rprintf(FERROR,"skipping long-named directory %s\n",fname);
686 strlcat(fname,"/", MAXPATHLEN);
689 p = fname + strlen(fname);
691 local_exclude_list = NULL;
694 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
695 strcpy(p,".cvsignore");
696 local_exclude_list = make_exclude_list(fname,NULL,0,0);
699 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
703 for (di=readdir(d); di; di=readdir(d)) {
704 char *dname = d_name(di);
705 if (strcmp(dname,".")==0 ||
706 strcmp(dname,"..")==0)
708 strlcpy(p,dname,MAXPATHLEN-l);
709 send_file_name(f,flist,fname,recurse,0);
712 if (local_exclude_list) {
713 add_exclude_list("!", &local_exclude_list, 0);
720 struct file_list *send_file_list(int f,int argc,char *argv[])
724 char *p,*dir,*olddir;
725 char lastpath[MAXPATHLEN]="";
726 struct file_list *flist;
729 if (verbose && recurse && !am_server && f != -1) {
730 rprintf(FINFO,"building file list ... ");
732 rprintf(FINFO, "\n");
736 start_write = stats.total_written;
741 io_start_buffering(f);
744 for (i=0;i<argc;i++) {
745 char *fname = topsrcname;
747 strlcpy(fname,argv[i],MAXPATHLEN);
750 if (l != 1 && fname[l-1] == '/') {
751 if ((l == 2) && (fname[0] == '.')) {
752 /* Turn ./ into just . rather than ./.
753 This was put in to avoid a problem with
754 rsync -aR --delete from ./
755 The send_file_name() below of ./ was
756 mysteriously preventing deletes */
759 strlcat(fname,".",MAXPATHLEN);
763 if (link_stat(fname,&st) != 0) {
766 rprintf(FERROR,"link_stat %s : %s\n",fname,strerror(errno));
771 if (S_ISDIR(st.st_mode) && !recurse) {
772 rprintf(FINFO,"skipping directory %s\n",fname);
779 if (!relative_paths) {
780 p = strrchr(fname,'/');
789 } else if (f != -1 && (p=strrchr(fname,'/'))) {
790 /* this ensures we send the intermediate directories,
791 thus getting their permissions right */
793 if (strcmp(lastpath,fname)) {
794 strlcpy(lastpath, fname, sizeof(lastpath));
796 for (p=fname+1; (p=strchr(p,'/')); p++) {
797 int copy_links_saved = copy_links;
798 int recurse_saved = recurse;
800 copy_links = copy_unsafe_links;
801 /* set recurse to 1 to prevent make_file
802 from ignoring directory, but still
803 turn off the recursive parameter to
806 send_file_name(f, flist, fname, 0, 0);
807 copy_links = copy_links_saved;
808 recurse = recurse_saved;
820 olddir = push_dir(dir, 1);
824 rprintf(FERROR,"push_dir %s : %s\n",
825 dir,strerror(errno));
833 set_filesystem(fname);
835 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
837 if (olddir != NULL) {
839 if (pop_dir(olddir) != 0) {
840 rprintf(FERROR,"pop_dir %s : %s\n",
841 dir,strerror(errno));
842 exit_cleanup(RERR_FILESELECT);
847 topsrcname[0] = '\0';
850 send_file_entry(NULL,f,0);
853 if (verbose && recurse && !am_server && f != -1)
854 rprintf(FINFO,"done\n");
856 clean_flist(flist, 0);
858 /* now send the uid/gid list. This was introduced in protocol
860 if (f != -1 && remote_version >= 15) {
864 /* if protocol version is >= 17 then send the io_error flag */
865 if (f != -1 && remote_version >= 17) {
866 extern int module_id;
867 write_int(f, lp_ignore_errors(module_id)? 0 : io_error);
872 stats.flist_size = stats.total_written - start_write;
873 stats.num_files = flist->count;
874 if (write_batch) /* dw */
875 write_batch_flist_info(flist->count, flist->files);
879 rprintf(FINFO,"send_file_list done\n");
885 struct file_list *recv_file_list(int f)
887 struct file_list *flist;
890 extern int list_only;
892 if (verbose && recurse && !am_server) {
893 rprintf(FINFO,"receiving file list ... ");
897 start_read = stats.total_read;
899 flist = (struct file_list *)malloc(sizeof(flist[0]));
904 flist->malloced=1000;
905 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
911 for (flags=read_byte(f); flags; flags=read_byte(f)) {
912 int i = flist->count;
914 if (i >= flist->malloced) {
915 if (flist->malloced < 1000)
916 flist->malloced += 1000;
918 flist->malloced *= 2;
919 flist->files =(struct file_struct **)realloc(flist->files,
920 sizeof(flist->files[0])*
926 receive_file_entry(&flist->files[i],flags,f);
928 if (S_ISREG(flist->files[i]->mode))
929 stats.total_size += flist->files[i]->length;
934 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
939 rprintf(FINFO,"received %d names\n",flist->count);
941 clean_flist(flist, relative_paths);
943 if (verbose && recurse && !am_server) {
944 rprintf(FINFO,"done\n");
947 /* now recv the uid/gid list. This was introduced in protocol version 15 */
948 if (f != -1 && remote_version >= 15) {
949 recv_uid_list(f, flist);
952 /* if protocol version is >= 17 then recv the io_error flag */
953 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
954 extern int module_id;
955 extern int ignore_errors;
956 if (lp_ignore_errors(module_id) || ignore_errors) {
959 io_error |= read_int(f);
965 for (i=0;i<flist->count;i++) {
966 list_file_entry(flist->files[i]);
972 rprintf(FINFO,"recv_file_list done\n");
974 stats.flist_size = stats.total_read - start_read;
975 stats.num_files = flist->count;
980 out_of_memory("recv_file_list");
981 return NULL; /* not reached */
985 int file_compare(struct file_struct **f1,struct file_struct **f2)
987 if (!(*f1)->basename && !(*f2)->basename) return 0;
988 if (!(*f1)->basename) return -1;
989 if (!(*f2)->basename) return 1;
990 if ((*f1)->dirname == (*f2)->dirname)
991 return u_strcmp((*f1)->basename, (*f2)->basename);
992 return u_strcmp(f_name(*f1),f_name(*f2));
996 int flist_find(struct file_list *flist,struct file_struct *f)
998 int low=0,high=flist->count-1;
1000 if (flist->count <= 0) return -1;
1002 while (low != high) {
1003 int mid = (low+high)/2;
1004 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1005 if (ret == 0) return flist_up(flist, mid);
1013 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
1014 return flist_up(flist,low);
1022 void free_file(struct file_struct *file)
1025 if (file->basename) free(file->basename);
1026 if (file->link) free(file->link);
1027 if (file->sum) free(file->sum);
1033 * allocate a new file list
1035 struct file_list *flist_new()
1037 struct file_list *flist;
1039 flist = (struct file_list *)malloc(sizeof(flist[0]));
1040 if (!flist) out_of_memory("send_file_list");
1043 flist->malloced = 1000;
1044 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
1046 if (!flist->files) out_of_memory("send_file_list");
1048 flist->string_area = string_area_new(0);
1050 flist->string_area = NULL;
1055 * free up all elements in a flist
1057 void flist_free(struct file_list *flist)
1060 for (i=1;i<flist->count;i++) {
1061 if (!flist->string_area)
1062 free_file(flist->files[i]);
1063 free(flist->files[i]);
1065 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
1067 if (flist->string_area)
1068 string_area_free(flist->string_area);
1069 memset((char *)flist, 0, sizeof(*flist));
1075 * This routine ensures we don't have any duplicate names in our file list.
1076 * duplicate names can cause corruption because of the pipelining
1078 static void clean_flist(struct file_list *flist, int strip_root)
1082 if (!flist || flist->count == 0)
1085 qsort(flist->files,flist->count,
1086 sizeof(flist->files[0]),
1087 (int (*)())file_compare);
1089 for (i=1;i<flist->count;i++) {
1090 if (flist->files[i]->basename &&
1091 flist->files[i-1]->basename &&
1092 strcmp(f_name(flist->files[i]),
1093 f_name(flist->files[i-1])) == 0) {
1094 if (verbose > 1 && !am_server)
1095 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
1096 f_name(flist->files[i-1]),i-1);
1097 /* it's not great that the flist knows the semantics of the
1098 * file memory usage, but i'd rather not add a flag byte
1099 * to that struct. XXX can i use a bit in the flags field? */
1100 if (flist->string_area)
1101 flist->files[i][0] = null_file;
1103 free_file(flist->files[i]);
1107 /* FIXME: There is a bug here when filenames are repeated more
1108 * than once, because we don't handle freed files when doing
1109 * the comparison. */
1112 /* we need to strip off the root directory in the case
1113 of relative paths, but this must be done _after_
1114 the sorting phase */
1115 for (i=0;i<flist->count;i++) {
1116 if (flist->files[i]->dirname &&
1117 flist->files[i]->dirname[0] == '/') {
1118 memmove(&flist->files[i]->dirname[0],
1119 &flist->files[i]->dirname[1],
1120 strlen(flist->files[i]->dirname));
1123 if (flist->files[i]->dirname &&
1124 !flist->files[i]->dirname[0]) {
1125 flist->files[i]->dirname = NULL;
1131 if (verbose <= 3) return;
1133 for (i=0;i<flist->count;i++) {
1134 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%.0f\n",
1136 NS(flist->files[i]->dirname),
1137 NS(flist->files[i]->basename),
1138 (int) flist->files[i]->mode,
1139 (double)flist->files[i]->length);
1145 * return the full filename of a flist entry
1147 char *f_name(struct file_struct *f)
1149 static char names[10][MAXPATHLEN];
1153 if (!f || !f->basename) return NULL;
1158 strlcpy(p, f->dirname, MAXPATHLEN);
1159 strlcat(p, "/", MAXPATHLEN);
1160 strlcat(p, f->basename, MAXPATHLEN);
1162 strlcpy(p, f->basename, MAXPATHLEN);