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.
28 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
34 extern struct stats stats;
38 extern int always_checksum;
40 extern int cvs_exclude;
44 extern int one_file_system;
45 extern int make_backups;
46 extern int preserve_links;
47 extern int preserve_hard_links;
48 extern int preserve_perms;
49 extern int preserve_devices;
50 extern int preserve_uid;
51 extern int preserve_gid;
52 extern int preserve_times;
53 extern int relative_paths;
54 extern int copy_links;
55 extern int copy_unsafe_links;
56 extern int remote_version;
58 extern int sanitize_paths;
60 extern int read_batch;
61 extern int write_batch;
63 static char topsrcname[MAXPATHLEN];
65 static struct exclude_struct **local_exclude_list;
67 static struct file_struct null_file;
69 static void clean_flist(struct file_list *flist, int strip_root);
72 static int show_build_progress_p(void)
74 extern int do_progress;
76 return do_progress && verbose && recurse && !am_server;
80 * True if we're local, etc, and should emit progress emssages.
82 static void emit_build_progress(const struct file_list *flist)
84 rprintf(FINFO, " %d files...\r", flist->count);
88 static void finish_build_progress(const struct file_list *flist)
90 if (verbose && recurse && !am_server) {
91 /* This overwrites the progress line, if any. */
92 rprintf(FINFO, RSYNC_NAME ": %d files to consider.\n",
98 static struct string_area *string_area_new(int size)
100 struct string_area *a;
104 a = malloc(sizeof(*a));
106 out_of_memory("string_area_new");
107 a->current = a->base = malloc(size);
109 out_of_memory("string_area_new buffer");
110 a->end = a->base + size;
116 static void string_area_free(struct string_area *a)
118 struct string_area *next;
120 for (; a; a = next) {
126 static char *string_area_malloc(struct string_area **ap, int size)
129 struct string_area *a;
131 /* does the request fit into the current space? */
133 if (a->current + size >= a->end) {
134 /* no; get space, move new string_area to front of the list */
135 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
140 /* have space; do the "allocation." */
146 static char *string_area_strdup(struct string_area **ap, const char *src)
148 char *dest = string_area_malloc(ap, strlen(src) + 1);
149 return strcpy(dest, src);
152 static void list_file_entry(struct file_struct *f)
157 /* this can happen if duplicate names were removed */
160 permstring(perms, f->mode);
162 if (preserve_links && S_ISLNK(f->mode)) {
163 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
165 (double) f->length, timestring(f->modtime),
168 rprintf(FINFO, "%s %11.0f %s %s\n",
170 (double) f->length, timestring(f->modtime),
176 int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
180 return do_stat(Path, Buffer);
182 if (do_lstat(Path, Buffer) == -1) {
185 if (S_ISLNK(Buffer->st_mode)) {
188 readlink((char *) Path, Linkbuf,
189 MAXPATHLEN - 1)) == -1) {
193 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
194 unsafe_symlink(Linkbuf, topsrcname)) {
195 return do_stat(Path, Buffer);
200 return do_stat(Path, Buffer);
204 int link_stat(const char *Path, STRUCT_STAT * Buffer)
208 return do_stat(Path, Buffer);
210 return do_lstat(Path, Buffer);
213 return do_stat(Path, Buffer);
218 This function is used to check if a file should be included/excluded
219 from the list of files based on its name and type etc
221 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
223 extern int delete_excluded;
225 /* f is set to -1 when calculating deletion file list */
226 if ((f == -1) && delete_excluded) {
229 if (check_exclude(fname, local_exclude_list, st)) {
235 /* used by the one_file_system code */
236 static dev_t filesystem_dev;
238 static void set_filesystem(char *fname)
241 if (link_stat(fname, &st) != 0)
243 filesystem_dev = st.st_dev;
247 static int to_wire_mode(mode_t mode)
249 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
250 return (mode & ~(_S_IFMT)) | 0120000;
255 static mode_t from_wire_mode(int mode)
257 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
258 return (mode & ~(_S_IFMT)) | _S_IFLNK;
260 return (mode_t) mode;
264 static void send_directory(int f, struct file_list *flist, char *dir);
266 static char *flist_dir;
269 static void send_file_entry(struct file_struct *file, int f,
273 static time_t last_time;
274 static mode_t last_mode;
275 static dev_t last_rdev;
276 static uid_t last_uid;
277 static gid_t last_gid;
278 static char lastname[MAXPATHLEN];
290 fname = f_name(file);
294 if (file->mode == last_mode)
296 if (file->rdev == last_rdev)
298 if (file->uid == last_uid)
300 if (file->gid == last_gid)
302 if (file->modtime == last_time)
306 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
308 l2 = strlen(fname) - l1;
315 /* we must make sure we don't send a zero flags byte or the other
316 end will terminate the flist transfer */
317 if (flags == 0 && !S_ISDIR(file->mode))
318 flags |= FLAG_DELETE;
322 write_byte(f, flags);
323 if (flags & SAME_NAME)
325 if (flags & LONG_NAME)
329 write_buf(f, fname + l1, l2);
331 write_longint(f, file->length);
332 if (!(flags & SAME_TIME))
333 write_int(f, (int) file->modtime);
334 if (!(flags & SAME_MODE))
335 write_int(f, to_wire_mode(file->mode));
336 if (preserve_uid && !(flags & SAME_UID)) {
338 write_int(f, (int) file->uid);
340 if (preserve_gid && !(flags & SAME_GID)) {
342 write_int(f, (int) file->gid);
344 if (preserve_devices && IS_DEVICE(file->mode)
345 && !(flags & SAME_RDEV))
346 write_int(f, (int) file->rdev);
349 if (preserve_links && S_ISLNK(file->mode)) {
350 write_int(f, strlen(file->link));
351 write_buf(f, file->link, strlen(file->link));
355 #if SUPPORT_HARD_LINKS
356 if (preserve_hard_links && S_ISREG(file->mode)) {
357 if (remote_version < 26) {
358 /* 32-bit dev_t and ino_t */
359 write_int(f, (int) file->dev);
360 write_int(f, (int) file->inode);
362 /* 64-bit dev_t and ino_t */
363 write_longint(f, file->dev);
364 write_longint(f, file->inode);
369 if (always_checksum) {
370 if (remote_version < 21) {
371 write_buf(f, file->sum, 2);
373 write_buf(f, file->sum, MD4_SUM_LENGTH);
377 last_mode = file->mode;
378 last_rdev = file->rdev;
379 last_uid = file->uid;
380 last_gid = file->gid;
381 last_time = file->modtime;
383 strlcpy(lastname, fname, MAXPATHLEN);
384 lastname[MAXPATHLEN - 1] = 0;
389 static void receive_file_entry(struct file_struct **fptr,
390 unsigned flags, int f)
392 static time_t last_time;
393 static mode_t last_mode;
394 static dev_t last_rdev;
395 static uid_t last_uid;
396 static gid_t last_gid;
397 static char lastname[MAXPATHLEN];
398 char thisname[MAXPATHLEN];
399 unsigned int l1 = 0, l2 = 0;
401 struct file_struct *file;
403 if (flags & SAME_NAME)
406 if (flags & LONG_NAME)
411 file = (struct file_struct *) malloc(sizeof(*file));
413 out_of_memory("receive_file_entry");
414 memset((char *) file, 0, sizeof(*file));
417 if (l2 >= MAXPATHLEN - l1) {
419 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
420 flags, l1, l2, lastname);
421 overflow("receive_file_entry");
424 strlcpy(thisname, lastname, l1 + 1);
425 read_sbuf(f, &thisname[l1], l2);
426 thisname[l1 + l2] = 0;
428 strlcpy(lastname, thisname, MAXPATHLEN);
429 lastname[MAXPATHLEN - 1] = 0;
431 clean_fname(thisname);
433 if (sanitize_paths) {
434 sanitize_path(thisname, NULL);
437 if ((p = strrchr(thisname, '/'))) {
438 static char *lastdir;
440 if (lastdir && strcmp(thisname, lastdir) == 0) {
441 file->dirname = lastdir;
443 file->dirname = strdup(thisname);
444 lastdir = file->dirname;
446 file->basename = strdup(p + 1);
448 file->dirname = NULL;
449 file->basename = strdup(thisname);
453 out_of_memory("receive_file_entry 1");
457 file->length = read_longint(f);
459 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
461 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
464 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
467 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
468 if (preserve_devices && IS_DEVICE(file->mode))
470 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
472 if (preserve_links && S_ISLNK(file->mode)) {
475 rprintf(FERROR, "overflow: l=%d\n", l);
476 overflow("receive_file_entry");
478 file->link = (char *) malloc(l + 1);
480 out_of_memory("receive_file_entry 2");
481 read_sbuf(f, file->link, l);
482 if (sanitize_paths) {
483 sanitize_path(file->link, file->dirname);
486 #if SUPPORT_HARD_LINKS
487 if (preserve_hard_links && S_ISREG(file->mode)) {
488 if (remote_version < 26) {
489 file->dev = read_int(f);
490 file->inode = read_int(f);
492 file->dev = read_longint(f);
493 file->inode = read_longint(f);
498 if (always_checksum) {
499 file->sum = (char *) malloc(MD4_SUM_LENGTH);
501 out_of_memory("md4 sum");
502 if (remote_version < 21) {
503 read_buf(f, file->sum, 2);
505 read_buf(f, file->sum, MD4_SUM_LENGTH);
509 last_mode = file->mode;
510 last_rdev = file->rdev;
511 last_uid = file->uid;
512 last_gid = file->gid;
513 last_time = file->modtime;
515 if (!preserve_perms) {
516 extern int orig_umask;
517 /* set an appropriate set of permissions based on original
518 permissions and umask. This emulates what GNU cp does */
519 file->mode &= ~orig_umask;
524 /* determine if a file in a different filesstem should be skipped
525 when one_file_system is set. We bascally only want to include
526 the mount points - but they can be hard to find! */
527 static int skip_filesystem(char *fname, STRUCT_STAT * st)
530 char *p = strrchr(fname, '/');
532 /* skip all but directories */
533 if (!S_ISDIR(st->st_mode))
536 /* if its not a subdirectory then allow */
541 if (link_stat(fname, &st2)) {
547 return (st2.st_dev != filesystem_dev);
550 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
551 /* IRIX cc cares that the operands to the ternary have the same type. */
552 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
554 /* create a file_struct for a named file */
555 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
558 struct file_struct *file;
560 char sum[SUM_LENGTH];
562 char cleaned_name[MAXPATHLEN];
563 char linkbuf[MAXPATHLEN];
564 extern int module_id;
566 strlcpy(cleaned_name, fname, MAXPATHLEN);
567 cleaned_name[MAXPATHLEN - 1] = 0;
568 clean_fname(cleaned_name);
569 if (sanitize_paths) {
570 sanitize_path(cleaned_name, NULL);
572 fname = cleaned_name;
574 memset(sum, 0, SUM_LENGTH);
576 if (readlink_stat(fname, &st, linkbuf) != 0) {
577 int save_errno = errno;
578 if ((errno == ENOENT) && copy_links && !noexcludes) {
579 /* symlink pointing nowhere, see if excluded */
580 memset((char *) &st, 0, sizeof(st));
581 if (check_exclude_file(f, fname, &st)) {
582 /* file is excluded anyway, ignore silently */
587 rprintf(FERROR, "readlink %s: %s\n",
588 fname, strerror(save_errno));
592 /* we use noexcludes from backup.c */
596 if (S_ISDIR(st.st_mode) && !recurse) {
597 rprintf(FINFO, "skipping directory %s\n", fname);
601 if (one_file_system && st.st_dev != filesystem_dev) {
602 if (skip_filesystem(fname, &st))
606 if (check_exclude_file(f, fname, &st))
610 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
616 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
618 file = (struct file_struct *) malloc(sizeof(*file));
620 out_of_memory("make_file");
621 memset((char *) file, 0, sizeof(*file));
623 if ((p = strrchr(fname, '/'))) {
624 static char *lastdir;
626 if (lastdir && strcmp(fname, lastdir) == 0) {
627 file->dirname = lastdir;
629 file->dirname = strdup(fname);
630 lastdir = file->dirname;
632 file->basename = STRDUP(ap, p + 1);
635 file->dirname = NULL;
636 file->basename = STRDUP(ap, fname);
639 file->modtime = st.st_mtime;
640 file->length = st.st_size;
641 file->mode = st.st_mode;
642 file->uid = st.st_uid;
643 file->gid = st.st_gid;
644 file->dev = st.st_dev;
645 file->inode = st.st_ino;
647 file->rdev = st.st_rdev;
651 if (S_ISLNK(st.st_mode)) {
652 file->link = STRDUP(ap, linkbuf);
656 if (always_checksum) {
657 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
659 out_of_memory("md4 sum");
660 /* drat. we have to provide a null checksum for non-regular
661 files in order to be compatible with earlier versions
663 if (S_ISREG(st.st_mode)) {
664 file_checksum(fname, file->sum, st.st_size);
666 memset(file->sum, 0, MD4_SUM_LENGTH);
671 static char *lastdir;
672 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
673 file->basedir = lastdir;
675 file->basedir = strdup(flist_dir);
676 lastdir = file->basedir;
679 file->basedir = NULL;
682 if (!S_ISDIR(st.st_mode))
683 stats.total_size += st.st_size;
690 void send_file_name(int f, struct file_list *flist, char *fname,
691 int recursive, unsigned base_flags)
693 struct file_struct *file;
695 file = make_file(f, fname, &flist->string_area, 0);
700 if (show_build_progress_p() & !(flist->count % 100))
701 emit_build_progress(flist);
703 if (flist->count >= flist->malloced) {
704 if (flist->malloced < 1000)
705 flist->malloced += 1000;
707 flist->malloced *= 2;
709 (struct file_struct **) realloc(flist->files,
714 out_of_memory("send_file_name");
717 if (write_batch) /* dw */
718 file->flags = FLAG_DELETE;
720 if (strcmp(file->basename, "")) {
721 flist->files[flist->count++] = file;
722 send_file_entry(file, f, base_flags);
725 if (S_ISDIR(file->mode) && recursive) {
726 struct exclude_struct **last_exclude_list =
728 send_directory(f, flist, f_name(file));
729 local_exclude_list = last_exclude_list;
736 static void send_directory(int f, struct file_list *flist, char *dir)
740 char fname[MAXPATHLEN];
747 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
751 strlcpy(fname, dir, MAXPATHLEN);
753 if (fname[l - 1] != '/') {
754 if (l == MAXPATHLEN - 1) {
757 "skipping long-named directory %s\n",
762 strlcat(fname, "/", MAXPATHLEN);
765 p = fname + strlen(fname);
767 local_exclude_list = NULL;
770 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
771 strcpy(p, ".cvsignore");
773 make_exclude_list(fname, NULL, 0, 0);
777 "cannot cvs-exclude in long-named directory %s\n",
782 for (di = readdir(d); di; di = readdir(d)) {
783 char *dname = d_name(di);
784 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
786 strlcpy(p, dname, MAXPATHLEN - l);
787 send_file_name(f, flist, fname, recurse, 0);
790 if (local_exclude_list) {
791 add_exclude_list("!", &local_exclude_list, 0);
800 * I *think* f==-1 means that the list should just be built in memory
801 * and not transmitted. But who can tell? -- mbp
803 struct file_list *send_file_list(int f, int argc, char *argv[])
807 char *p, *dir, *olddir;
808 char lastpath[MAXPATHLEN] = "";
809 struct file_list *flist;
812 if (verbose && recurse && !am_server && f != -1) {
813 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
815 rprintf(FINFO, "\n");
819 start_write = stats.total_written;
824 io_start_buffering(f);
827 for (i = 0; i < argc; i++) {
828 char *fname = topsrcname;
830 strlcpy(fname, argv[i], MAXPATHLEN);
833 if (l != 1 && fname[l - 1] == '/') {
834 if ((l == 2) && (fname[0] == '.')) {
835 /* Turn ./ into just . rather than ./.
836 This was put in to avoid a problem with
837 rsync -aR --delete from ./
838 The send_file_name() below of ./ was
839 mysteriously preventing deletes */
842 strlcat(fname, ".", MAXPATHLEN);
846 if (link_stat(fname, &st) != 0) {
849 rprintf(FERROR, "link_stat %s : %s\n",
850 fname, strerror(errno));
855 if (S_ISDIR(st.st_mode) && !recurse) {
856 rprintf(FINFO, "skipping directory %s\n", fname);
863 if (!relative_paths) {
864 p = strrchr(fname, '/');
873 } else if (f != -1 && (p = strrchr(fname, '/'))) {
874 /* this ensures we send the intermediate directories,
875 thus getting their permissions right */
877 if (strcmp(lastpath, fname)) {
878 strlcpy(lastpath, fname, sizeof(lastpath));
880 for (p = fname + 1; (p = strchr(p, '/'));
882 int copy_links_saved = copy_links;
883 int recurse_saved = recurse;
885 copy_links = copy_unsafe_links;
886 /* set recurse to 1 to prevent make_file
887 from ignoring directory, but still
888 turn off the recursive parameter to
891 send_file_name(f, flist, fname, 0,
893 copy_links = copy_links_saved;
894 recurse = recurse_saved;
906 olddir = push_dir(dir, 1);
910 rprintf(FERROR, "push_dir %s : %s\n",
911 dir, strerror(errno));
919 set_filesystem(fname);
921 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
923 if (olddir != NULL) {
925 if (pop_dir(olddir) != 0) {
926 rprintf(FERROR, "pop_dir %s : %s\n",
927 dir, strerror(errno));
928 exit_cleanup(RERR_FILESELECT);
933 topsrcname[0] = '\0';
936 send_file_entry(NULL, f, 0);
939 finish_build_progress(flist);
941 clean_flist(flist, 0);
943 /* now send the uid/gid list. This was introduced in protocol
945 if (f != -1 && remote_version >= 15) {
949 /* if protocol version is >= 17 then send the io_error flag */
950 if (f != -1 && remote_version >= 17) {
951 extern int module_id;
952 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
957 stats.flist_size = stats.total_written - start_write;
958 stats.num_files = flist->count;
959 if (write_batch) /* dw */
960 write_batch_flist_info(flist->count, flist->files);
964 rprintf(FINFO, "send_file_list done\n");
970 struct file_list *recv_file_list(int f)
972 struct file_list *flist;
975 extern int list_only;
977 if (verbose && recurse && !am_server) {
978 rprintf(FINFO, "receiving file list ... ");
982 start_read = stats.total_read;
984 flist = (struct file_list *) malloc(sizeof(flist[0]));
989 flist->malloced = 1000;
991 (struct file_struct **) malloc(sizeof(flist->files[0]) *
997 for (flags = read_byte(f); flags; flags = read_byte(f)) {
998 int i = flist->count;
1000 if (i >= flist->malloced) {
1001 if (flist->malloced < 1000)
1002 flist->malloced += 1000;
1004 flist->malloced *= 2;
1006 (struct file_struct **) realloc(flist->files,
1016 receive_file_entry(&flist->files[i], flags, f);
1018 if (S_ISREG(flist->files[i]->mode))
1019 stats.total_size += flist->files[i]->length;
1024 rprintf(FINFO, "recv_file_name(%s)\n",
1025 f_name(flist->files[i]));
1030 rprintf(FINFO, "received %d names\n", flist->count);
1032 clean_flist(flist, relative_paths);
1034 if (verbose && recurse && !am_server) {
1035 rprintf(FINFO, "done\n");
1038 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1039 if (f != -1 && remote_version >= 15) {
1040 recv_uid_list(f, flist);
1043 /* if protocol version is >= 17 then recv the io_error flag */
1044 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1045 extern int module_id;
1046 extern int ignore_errors;
1047 if (lp_ignore_errors(module_id) || ignore_errors) {
1050 io_error |= read_int(f);
1056 for (i = 0; i < flist->count; i++) {
1057 list_file_entry(flist->files[i]);
1063 rprintf(FINFO, "recv_file_list done\n");
1065 stats.flist_size = stats.total_read - start_read;
1066 stats.num_files = flist->count;
1071 out_of_memory("recv_file_list");
1072 return NULL; /* not reached */
1077 * XXX: This is currently the hottest function while building the file
1078 * list, because building f_name()s every time is expensive.
1080 int file_compare(struct file_struct **f1, struct file_struct **f2)
1082 if (!(*f1)->basename && !(*f2)->basename)
1084 if (!(*f1)->basename)
1086 if (!(*f2)->basename)
1088 if ((*f1)->dirname == (*f2)->dirname)
1089 return u_strcmp((*f1)->basename, (*f2)->basename);
1090 return u_strcmp(f_name(*f1), f_name(*f2));
1094 int flist_find(struct file_list *flist, struct file_struct *f)
1096 int low = 0, high = flist->count - 1;
1098 if (flist->count <= 0)
1101 while (low != high) {
1102 int mid = (low + high) / 2;
1104 file_compare(&flist->files[flist_up(flist, mid)], &f);
1106 return flist_up(flist, mid);
1114 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1115 return flist_up(flist, low);
1123 void free_file(struct file_struct *file)
1128 free(file->basename);
1138 * allocate a new file list
1140 struct file_list *flist_new()
1142 struct file_list *flist;
1144 flist = (struct file_list *) malloc(sizeof(flist[0]));
1146 out_of_memory("send_file_list");
1149 flist->malloced = 1000;
1151 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1154 out_of_memory("send_file_list");
1156 flist->string_area = string_area_new(0);
1158 flist->string_area = NULL;
1164 * free up all elements in a flist
1166 void flist_free(struct file_list *flist)
1169 for (i = 1; i < flist->count; i++) {
1170 if (!flist->string_area)
1171 free_file(flist->files[i]);
1172 free(flist->files[i]);
1174 memset((char *) flist->files, 0,
1175 sizeof(flist->files[0]) * flist->count);
1177 if (flist->string_area)
1178 string_area_free(flist->string_area);
1179 memset((char *) flist, 0, sizeof(*flist));
1185 * This routine ensures we don't have any duplicate names in our file list.
1186 * duplicate names can cause corruption because of the pipelining
1188 static void clean_flist(struct file_list *flist, int strip_root)
1192 if (!flist || flist->count == 0)
1195 qsort(flist->files, flist->count,
1196 sizeof(flist->files[0]), (int (*)()) file_compare);
1198 for (i = 1; i < flist->count; i++) {
1199 if (flist->files[i]->basename &&
1200 flist->files[i - 1]->basename &&
1201 strcmp(f_name(flist->files[i]),
1202 f_name(flist->files[i - 1])) == 0) {
1203 if (verbose > 1 && !am_server)
1205 "removing duplicate name %s from file list %d\n",
1206 f_name(flist->files[i - 1]),
1208 /* it's not great that the flist knows the semantics of the
1209 * file memory usage, but i'd rather not add a flag byte
1210 * to that struct. XXX can i use a bit in the flags field? */
1211 if (flist->string_area)
1212 flist->files[i][0] = null_file;
1214 free_file(flist->files[i]);
1218 /* FIXME: There is a bug here when filenames are repeated more
1219 * than once, because we don't handle freed files when doing
1220 * the comparison. */
1223 /* we need to strip off the root directory in the case
1224 of relative paths, but this must be done _after_
1225 the sorting phase */
1226 for (i = 0; i < flist->count; i++) {
1227 if (flist->files[i]->dirname &&
1228 flist->files[i]->dirname[0] == '/') {
1229 memmove(&flist->files[i]->dirname[0],
1230 &flist->files[i]->dirname[1],
1231 strlen(flist->files[i]->dirname));
1234 if (flist->files[i]->dirname &&
1235 !flist->files[i]->dirname[0]) {
1236 flist->files[i]->dirname = NULL;
1245 for (i = 0; i < flist->count; i++) {
1246 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1248 NS(flist->files[i]->dirname),
1249 NS(flist->files[i]->basename),
1250 (int) flist->files[i]->mode,
1251 (double) flist->files[i]->length);
1257 * return the full filename of a flist entry
1259 * This function is too expensive at the moment, because it copies
1260 * strings when often we only want to compare them. In any case,
1261 * using strlcat is silly because it will walk the string repeatedly.
1263 char *f_name(struct file_struct *f)
1265 static char names[10][MAXPATHLEN];
1269 if (!f || !f->basename)
1277 off = strlcpy(p, f->dirname, MAXPATHLEN);
1278 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1279 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1281 strlcpy(p, f->basename, MAXPATHLEN);