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 void show_flist_stats(void)
104 static struct string_area *string_area_new(int size)
106 struct string_area *a;
110 a = malloc(sizeof(*a));
112 out_of_memory("string_area_new");
113 a->current = a->base = malloc(size);
115 out_of_memory("string_area_new buffer");
116 a->end = a->base + size;
122 static void string_area_free(struct string_area *a)
124 struct string_area *next;
126 for (; a; a = next) {
132 static char *string_area_malloc(struct string_area **ap, int size)
135 struct string_area *a;
137 /* does the request fit into the current space? */
139 if (a->current + size >= a->end) {
140 /* no; get space, move new string_area to front of the list */
141 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
146 /* have space; do the "allocation." */
152 static char *string_area_strdup(struct string_area **ap, const char *src)
154 char *dest = string_area_malloc(ap, strlen(src) + 1);
155 return strcpy(dest, src);
158 static void list_file_entry(struct file_struct *f)
163 /* this can happen if duplicate names were removed */
166 permstring(perms, f->mode);
168 if (preserve_links && S_ISLNK(f->mode)) {
169 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
171 (double) f->length, timestring(f->modtime),
174 rprintf(FINFO, "%s %11.0f %s %s\n",
176 (double) f->length, timestring(f->modtime),
182 int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
186 return do_stat(Path, Buffer);
188 if (do_lstat(Path, Buffer) == -1) {
191 if (S_ISLNK(Buffer->st_mode)) {
194 readlink((char *) Path, Linkbuf,
195 MAXPATHLEN - 1)) == -1) {
199 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
200 unsafe_symlink(Linkbuf, topsrcname)) {
201 return do_stat(Path, Buffer);
206 return do_stat(Path, Buffer);
210 int link_stat(const char *Path, STRUCT_STAT * Buffer)
214 return do_stat(Path, Buffer);
216 return do_lstat(Path, Buffer);
219 return do_stat(Path, Buffer);
224 This function is used to check if a file should be included/excluded
225 from the list of files based on its name and type etc
227 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
229 extern int delete_excluded;
231 /* f is set to -1 when calculating deletion file list */
232 if ((f == -1) && delete_excluded) {
235 if (check_exclude(fname, local_exclude_list, st)) {
241 /* used by the one_file_system code */
242 static dev_t filesystem_dev;
244 static void set_filesystem(char *fname)
247 if (link_stat(fname, &st) != 0)
249 filesystem_dev = st.st_dev;
253 static int to_wire_mode(mode_t mode)
255 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
256 return (mode & ~(_S_IFMT)) | 0120000;
261 static mode_t from_wire_mode(int mode)
263 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
264 return (mode & ~(_S_IFMT)) | _S_IFLNK;
266 return (mode_t) mode;
270 static void send_directory(int f, struct file_list *flist, char *dir);
272 static char *flist_dir;
276 * Make sure @p flist is big enough to hold at least @p flist->count
279 static void flist_expand(struct file_list *flist)
281 if (flist->count >= flist->malloced) {
282 if (flist->malloced < 1000)
283 flist->malloced += 1000;
285 flist->malloced *= 2;
286 flist->files = (struct file_struct **)
287 realloc(flist->files,
288 sizeof(flist->files[0]) * flist->malloced);
290 out_of_memory("flist_expand");
295 static void send_file_entry(struct file_struct *file, int f,
299 static time_t last_time;
300 static mode_t last_mode;
301 static dev_t last_rdev;
302 static uid_t last_uid;
303 static gid_t last_gid;
304 static char lastname[MAXPATHLEN];
316 fname = f_name(file);
320 if (file->mode == last_mode)
322 if (file->rdev == last_rdev)
324 if (file->uid == last_uid)
326 if (file->gid == last_gid)
328 if (file->modtime == last_time)
332 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
334 l2 = strlen(fname) - l1;
341 /* we must make sure we don't send a zero flags byte or the other
342 end will terminate the flist transfer */
343 if (flags == 0 && !S_ISDIR(file->mode))
344 flags |= FLAG_DELETE;
348 write_byte(f, flags);
349 if (flags & SAME_NAME)
351 if (flags & LONG_NAME)
355 write_buf(f, fname + l1, l2);
357 write_longint(f, file->length);
358 if (!(flags & SAME_TIME))
359 write_int(f, (int) file->modtime);
360 if (!(flags & SAME_MODE))
361 write_int(f, to_wire_mode(file->mode));
362 if (preserve_uid && !(flags & SAME_UID)) {
364 write_int(f, (int) file->uid);
366 if (preserve_gid && !(flags & SAME_GID)) {
368 write_int(f, (int) file->gid);
370 if (preserve_devices && IS_DEVICE(file->mode)
371 && !(flags & SAME_RDEV))
372 write_int(f, (int) file->rdev);
375 if (preserve_links && S_ISLNK(file->mode)) {
376 write_int(f, strlen(file->link));
377 write_buf(f, file->link, strlen(file->link));
381 #if SUPPORT_HARD_LINKS
382 if (preserve_hard_links && S_ISREG(file->mode)) {
383 if (remote_version < 26) {
384 /* 32-bit dev_t and ino_t */
385 write_int(f, (int) file->dev);
386 write_int(f, (int) file->inode);
388 /* 64-bit dev_t and ino_t */
389 write_longint(f, file->dev);
390 write_longint(f, file->inode);
395 if (always_checksum) {
396 if (remote_version < 21) {
397 write_buf(f, file->sum, 2);
399 write_buf(f, file->sum, MD4_SUM_LENGTH);
403 last_mode = file->mode;
404 last_rdev = file->rdev;
405 last_uid = file->uid;
406 last_gid = file->gid;
407 last_time = file->modtime;
409 strlcpy(lastname, fname, MAXPATHLEN);
410 lastname[MAXPATHLEN - 1] = 0;
415 static void receive_file_entry(struct file_struct **fptr,
416 unsigned flags, int f)
418 static time_t last_time;
419 static mode_t last_mode;
420 static dev_t last_rdev;
421 static uid_t last_uid;
422 static gid_t last_gid;
423 static char lastname[MAXPATHLEN];
424 char thisname[MAXPATHLEN];
425 unsigned int l1 = 0, l2 = 0;
427 struct file_struct *file;
429 if (flags & SAME_NAME)
432 if (flags & LONG_NAME)
437 file = (struct file_struct *) malloc(sizeof(*file));
439 out_of_memory("receive_file_entry");
440 memset((char *) file, 0, sizeof(*file));
443 if (l2 >= MAXPATHLEN - l1) {
445 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
446 flags, l1, l2, lastname);
447 overflow("receive_file_entry");
450 strlcpy(thisname, lastname, l1 + 1);
451 read_sbuf(f, &thisname[l1], l2);
452 thisname[l1 + l2] = 0;
454 strlcpy(lastname, thisname, MAXPATHLEN);
455 lastname[MAXPATHLEN - 1] = 0;
457 clean_fname(thisname);
459 if (sanitize_paths) {
460 sanitize_path(thisname, NULL);
463 if ((p = strrchr(thisname, '/'))) {
464 static char *lastdir;
466 if (lastdir && strcmp(thisname, lastdir) == 0) {
467 file->dirname = lastdir;
469 file->dirname = strdup(thisname);
470 lastdir = file->dirname;
472 file->basename = strdup(p + 1);
474 file->dirname = NULL;
475 file->basename = strdup(thisname);
479 out_of_memory("receive_file_entry 1");
483 file->length = read_longint(f);
485 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
487 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
490 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
493 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
494 if (preserve_devices && IS_DEVICE(file->mode))
496 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
498 if (preserve_links && S_ISLNK(file->mode)) {
501 rprintf(FERROR, "overflow: l=%d\n", l);
502 overflow("receive_file_entry");
504 file->link = (char *) malloc(l + 1);
506 out_of_memory("receive_file_entry 2");
507 read_sbuf(f, file->link, l);
508 if (sanitize_paths) {
509 sanitize_path(file->link, file->dirname);
512 #if SUPPORT_HARD_LINKS
513 if (preserve_hard_links && S_ISREG(file->mode)) {
514 if (remote_version < 26) {
515 file->dev = read_int(f);
516 file->inode = read_int(f);
518 file->dev = read_longint(f);
519 file->inode = read_longint(f);
524 if (always_checksum) {
525 file->sum = (char *) malloc(MD4_SUM_LENGTH);
527 out_of_memory("md4 sum");
528 if (remote_version < 21) {
529 read_buf(f, file->sum, 2);
531 read_buf(f, file->sum, MD4_SUM_LENGTH);
535 last_mode = file->mode;
536 last_rdev = file->rdev;
537 last_uid = file->uid;
538 last_gid = file->gid;
539 last_time = file->modtime;
541 if (!preserve_perms) {
542 extern int orig_umask;
543 /* set an appropriate set of permissions based on original
544 permissions and umask. This emulates what GNU cp does */
545 file->mode &= ~orig_umask;
550 /* determine if a file in a different filesstem should be skipped
551 when one_file_system is set. We bascally only want to include
552 the mount points - but they can be hard to find! */
553 static int skip_filesystem(char *fname, STRUCT_STAT * st)
556 char *p = strrchr(fname, '/');
558 /* skip all but directories */
559 if (!S_ISDIR(st->st_mode))
562 /* if its not a subdirectory then allow */
567 if (link_stat(fname, &st2)) {
573 return (st2.st_dev != filesystem_dev);
576 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
577 /* IRIX cc cares that the operands to the ternary have the same type. */
578 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
580 /* create a file_struct for a named file */
581 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
584 struct file_struct *file;
586 char sum[SUM_LENGTH];
588 char cleaned_name[MAXPATHLEN];
589 char linkbuf[MAXPATHLEN];
590 extern int module_id;
592 strlcpy(cleaned_name, fname, MAXPATHLEN);
593 cleaned_name[MAXPATHLEN - 1] = 0;
594 clean_fname(cleaned_name);
595 if (sanitize_paths) {
596 sanitize_path(cleaned_name, NULL);
598 fname = cleaned_name;
600 memset(sum, 0, SUM_LENGTH);
602 if (readlink_stat(fname, &st, linkbuf) != 0) {
603 int save_errno = errno;
604 if ((errno == ENOENT) && copy_links && !noexcludes) {
605 /* symlink pointing nowhere, see if excluded */
606 memset((char *) &st, 0, sizeof(st));
607 if (check_exclude_file(f, fname, &st)) {
608 /* file is excluded anyway, ignore silently */
613 rprintf(FERROR, "readlink %s: %s\n",
614 fname, strerror(save_errno));
618 /* we use noexcludes from backup.c */
622 if (S_ISDIR(st.st_mode) && !recurse) {
623 rprintf(FINFO, "skipping directory %s\n", fname);
627 if (one_file_system && st.st_dev != filesystem_dev) {
628 if (skip_filesystem(fname, &st))
632 if (check_exclude_file(f, fname, &st))
636 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
642 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
644 file = (struct file_struct *) malloc(sizeof(*file));
646 out_of_memory("make_file");
647 memset((char *) file, 0, sizeof(*file));
649 if ((p = strrchr(fname, '/'))) {
650 static char *lastdir;
652 if (lastdir && strcmp(fname, lastdir) == 0) {
653 file->dirname = lastdir;
655 file->dirname = strdup(fname);
656 lastdir = file->dirname;
658 file->basename = STRDUP(ap, p + 1);
661 file->dirname = NULL;
662 file->basename = STRDUP(ap, fname);
665 file->modtime = st.st_mtime;
666 file->length = st.st_size;
667 file->mode = st.st_mode;
668 file->uid = st.st_uid;
669 file->gid = st.st_gid;
670 file->dev = st.st_dev;
671 file->inode = st.st_ino;
673 file->rdev = st.st_rdev;
677 if (S_ISLNK(st.st_mode)) {
678 file->link = STRDUP(ap, linkbuf);
682 if (always_checksum) {
683 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
685 out_of_memory("md4 sum");
686 /* drat. we have to provide a null checksum for non-regular
687 files in order to be compatible with earlier versions
689 if (S_ISREG(st.st_mode)) {
690 file_checksum(fname, file->sum, st.st_size);
692 memset(file->sum, 0, MD4_SUM_LENGTH);
697 static char *lastdir;
698 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
699 file->basedir = lastdir;
701 file->basedir = strdup(flist_dir);
702 lastdir = file->basedir;
705 file->basedir = NULL;
708 if (!S_ISDIR(st.st_mode))
709 stats.total_size += st.st_size;
716 void send_file_name(int f, struct file_list *flist, char *fname,
717 int recursive, unsigned base_flags)
719 struct file_struct *file;
721 file = make_file(f, fname, &flist->string_area, 0);
726 if (show_build_progress_p() & !(flist->count % 100))
727 emit_build_progress(flist);
731 if (write_batch) /* dw */
732 file->flags = FLAG_DELETE;
734 if (strcmp(file->basename, "")) {
735 flist->files[flist->count++] = file;
736 send_file_entry(file, f, base_flags);
739 if (S_ISDIR(file->mode) && recursive) {
740 struct exclude_struct **last_exclude_list =
742 send_directory(f, flist, f_name(file));
743 local_exclude_list = last_exclude_list;
750 static void send_directory(int f, struct file_list *flist, char *dir)
754 char fname[MAXPATHLEN];
761 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
765 strlcpy(fname, dir, MAXPATHLEN);
767 if (fname[l - 1] != '/') {
768 if (l == MAXPATHLEN - 1) {
771 "skipping long-named directory %s\n",
776 strlcat(fname, "/", MAXPATHLEN);
779 p = fname + strlen(fname);
781 local_exclude_list = NULL;
784 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
785 strcpy(p, ".cvsignore");
787 make_exclude_list(fname, NULL, 0, 0);
791 "cannot cvs-exclude in long-named directory %s\n",
796 for (di = readdir(d); di; di = readdir(d)) {
797 char *dname = d_name(di);
798 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
800 strlcpy(p, dname, MAXPATHLEN - l);
801 send_file_name(f, flist, fname, recurse, 0);
804 if (local_exclude_list) {
805 add_exclude_list("!", &local_exclude_list, 0);
814 * I *think* f==-1 means that the list should just be built in memory
815 * and not transmitted. But who can tell? -- mbp
817 struct file_list *send_file_list(int f, int argc, char *argv[])
821 char *p, *dir, *olddir;
822 char lastpath[MAXPATHLEN] = "";
823 struct file_list *flist;
826 if (verbose && recurse && !am_server && f != -1) {
827 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
829 rprintf(FINFO, "\n");
833 start_write = stats.total_written;
838 io_start_buffering(f);
841 for (i = 0; i < argc; i++) {
842 char *fname = topsrcname;
844 strlcpy(fname, argv[i], MAXPATHLEN);
847 if (l != 1 && fname[l - 1] == '/') {
848 if ((l == 2) && (fname[0] == '.')) {
849 /* Turn ./ into just . rather than ./.
850 This was put in to avoid a problem with
851 rsync -aR --delete from ./
852 The send_file_name() below of ./ was
853 mysteriously preventing deletes */
856 strlcat(fname, ".", MAXPATHLEN);
860 if (link_stat(fname, &st) != 0) {
863 rprintf(FERROR, "link_stat %s : %s\n",
864 fname, strerror(errno));
869 if (S_ISDIR(st.st_mode) && !recurse) {
870 rprintf(FINFO, "skipping directory %s\n", fname);
877 if (!relative_paths) {
878 p = strrchr(fname, '/');
887 } else if (f != -1 && (p = strrchr(fname, '/'))) {
888 /* this ensures we send the intermediate directories,
889 thus getting their permissions right */
891 if (strcmp(lastpath, fname)) {
892 strlcpy(lastpath, fname, sizeof(lastpath));
894 for (p = fname + 1; (p = strchr(p, '/'));
896 int copy_links_saved = copy_links;
897 int recurse_saved = recurse;
899 copy_links = copy_unsafe_links;
900 /* set recurse to 1 to prevent make_file
901 from ignoring directory, but still
902 turn off the recursive parameter to
905 send_file_name(f, flist, fname, 0,
907 copy_links = copy_links_saved;
908 recurse = recurse_saved;
920 olddir = push_dir(dir, 1);
924 rprintf(FERROR, "push_dir %s : %s\n",
925 dir, strerror(errno));
933 set_filesystem(fname);
935 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
937 if (olddir != NULL) {
939 if (pop_dir(olddir) != 0) {
940 rprintf(FERROR, "pop_dir %s : %s\n",
941 dir, strerror(errno));
942 exit_cleanup(RERR_FILESELECT);
947 topsrcname[0] = '\0';
950 send_file_entry(NULL, f, 0);
953 finish_build_progress(flist);
955 clean_flist(flist, 0);
957 /* now send the uid/gid list. This was introduced in protocol
959 if (f != -1 && remote_version >= 15) {
963 /* if protocol version is >= 17 then send the io_error flag */
964 if (f != -1 && remote_version >= 17) {
965 extern int module_id;
966 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
971 stats.flist_size = stats.total_written - start_write;
972 stats.num_files = flist->count;
973 if (write_batch) /* dw */
974 write_batch_flist_info(flist->count, flist->files);
978 rprintf(FINFO, "send_file_list done\n");
984 struct file_list *recv_file_list(int f)
986 struct file_list *flist;
989 extern int list_only;
991 if (verbose && recurse && !am_server) {
992 rprintf(FINFO, "receiving file list ... ");
996 start_read = stats.total_read;
998 flist = (struct file_list *) malloc(sizeof(flist[0]));
1003 flist->malloced = 1000;
1005 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1011 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1012 flist_expand(flist);
1014 receive_file_entry(&flist->files[i], flags, f);
1016 if (S_ISREG(flist->files[i]->mode))
1017 stats.total_size += flist->files[i]->length;
1022 rprintf(FINFO, "recv_file_name(%s)\n",
1023 f_name(flist->files[i]));
1028 rprintf(FINFO, "received %d names\n", flist->count);
1030 clean_flist(flist, relative_paths);
1032 if (verbose && recurse && !am_server) {
1033 rprintf(FINFO, "done\n");
1036 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1037 if (f != -1 && remote_version >= 15) {
1038 recv_uid_list(f, flist);
1041 /* if protocol version is >= 17 then recv the io_error flag */
1042 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1043 extern int module_id;
1044 extern int ignore_errors;
1045 if (lp_ignore_errors(module_id) || ignore_errors) {
1048 io_error |= read_int(f);
1054 for (i = 0; i < flist->count; i++) {
1055 list_file_entry(flist->files[i]);
1061 rprintf(FINFO, "recv_file_list done\n");
1063 stats.flist_size = stats.total_read - start_read;
1064 stats.num_files = flist->count;
1069 out_of_memory("recv_file_list");
1070 return NULL; /* not reached */
1075 * XXX: This is currently the hottest function while building the file
1076 * list, because building f_name()s every time is expensive.
1078 int file_compare(struct file_struct **f1, struct file_struct **f2)
1080 if (!(*f1)->basename && !(*f2)->basename)
1082 if (!(*f1)->basename)
1084 if (!(*f2)->basename)
1086 if ((*f1)->dirname == (*f2)->dirname)
1087 return u_strcmp((*f1)->basename, (*f2)->basename);
1088 return u_strcmp(f_name(*f1), f_name(*f2));
1092 int flist_find(struct file_list *flist, struct file_struct *f)
1094 int low = 0, high = flist->count - 1;
1096 if (flist->count <= 0)
1099 while (low != high) {
1100 int mid = (low + high) / 2;
1102 file_compare(&flist->files[flist_up(flist, mid)], &f);
1104 return flist_up(flist, mid);
1112 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1113 return flist_up(flist, low);
1121 void free_file(struct file_struct *file)
1126 free(file->basename);
1136 * allocate a new file list
1138 struct file_list *flist_new()
1140 struct file_list *flist;
1142 flist = (struct file_list *) malloc(sizeof(flist[0]));
1144 out_of_memory("send_file_list");
1147 flist->malloced = 0;
1148 flist->files = NULL;
1151 flist->string_area = string_area_new(0);
1153 flist->string_area = NULL;
1159 * free up all elements in a flist
1161 void flist_free(struct file_list *flist)
1164 for (i = 1; i < flist->count; i++) {
1165 if (!flist->string_area)
1166 free_file(flist->files[i]);
1167 free(flist->files[i]);
1169 /* FIXME: I don't think we generally need to blank the flist
1170 * since it's about to be freed. This will just cause more
1171 * memory traffic. If you want a freed-memory debugger, you
1172 * know where to get it. */
1173 memset((char *) flist->files, 0,
1174 sizeof(flist->files[0]) * flist->count);
1176 if (flist->string_area)
1177 string_area_free(flist->string_area);
1178 memset((char *) flist, 0, sizeof(*flist));
1184 * This routine ensures we don't have any duplicate names in our file list.
1185 * duplicate names can cause corruption because of the pipelining
1187 static void clean_flist(struct file_list *flist, int strip_root)
1191 if (!flist || flist->count == 0)
1194 qsort(flist->files, flist->count,
1195 sizeof(flist->files[0]), (int (*)()) file_compare);
1197 for (i = 1; i < flist->count; i++) {
1198 if (flist->files[i]->basename &&
1199 flist->files[i - 1]->basename &&
1200 strcmp(f_name(flist->files[i]),
1201 f_name(flist->files[i - 1])) == 0) {
1202 if (verbose > 1 && !am_server)
1204 "removing duplicate name %s from file list %d\n",
1205 f_name(flist->files[i - 1]),
1207 /* it's not great that the flist knows the semantics of the
1208 * file memory usage, but i'd rather not add a flag byte
1209 * to that struct. XXX can i use a bit in the flags field? */
1210 if (flist->string_area)
1211 flist->files[i][0] = null_file;
1213 free_file(flist->files[i]);
1217 /* FIXME: There is a bug here when filenames are repeated more
1218 * than once, because we don't handle freed files when doing
1219 * the comparison. */
1222 /* we need to strip off the root directory in the case
1223 of relative paths, but this must be done _after_
1224 the sorting phase */
1225 for (i = 0; i < flist->count; i++) {
1226 if (flist->files[i]->dirname &&
1227 flist->files[i]->dirname[0] == '/') {
1228 memmove(&flist->files[i]->dirname[0],
1229 &flist->files[i]->dirname[1],
1230 strlen(flist->files[i]->dirname));
1233 if (flist->files[i]->dirname &&
1234 !flist->files[i]->dirname[0]) {
1235 flist->files[i]->dirname = NULL;
1244 for (i = 0; i < flist->count; i++) {
1245 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1247 NS(flist->files[i]->dirname),
1248 NS(flist->files[i]->basename),
1249 (int) flist->files[i]->mode,
1250 (double) flist->files[i]->length);
1256 * return the full filename of a flist entry
1258 * This function is too expensive at the moment, because it copies
1259 * strings when often we only want to compare them. In any case,
1260 * using strlcat is silly because it will walk the string repeatedly.
1262 char *f_name(struct file_struct *f)
1264 static char names[10][MAXPATHLEN];
1268 if (!f || !f->basename)
1276 off = strlcpy(p, f->dirname, MAXPATHLEN);
1277 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1278 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1280 strlcpy(p, f->basename, MAXPATHLEN);