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;
270 * Make sure @p flist is big enough to hold at least @p flist->count
273 static void flist_expand(struct file_list *flist)
275 if (flist->count >= flist->malloced) {
276 if (flist->malloced < 1000)
277 flist->malloced += 1000;
279 flist->malloced *= 2;
280 flist->files = (struct file_struct **)
281 realloc(flist->files,
282 sizeof(flist->files[0]) * flist->malloced);
284 out_of_memory("flist_expand");
289 static void send_file_entry(struct file_struct *file, int f,
293 static time_t last_time;
294 static mode_t last_mode;
295 static dev_t last_rdev;
296 static uid_t last_uid;
297 static gid_t last_gid;
298 static char lastname[MAXPATHLEN];
310 fname = f_name(file);
314 if (file->mode == last_mode)
316 if (file->rdev == last_rdev)
318 if (file->uid == last_uid)
320 if (file->gid == last_gid)
322 if (file->modtime == last_time)
326 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
328 l2 = strlen(fname) - l1;
335 /* we must make sure we don't send a zero flags byte or the other
336 end will terminate the flist transfer */
337 if (flags == 0 && !S_ISDIR(file->mode))
338 flags |= FLAG_DELETE;
342 write_byte(f, flags);
343 if (flags & SAME_NAME)
345 if (flags & LONG_NAME)
349 write_buf(f, fname + l1, l2);
351 write_longint(f, file->length);
352 if (!(flags & SAME_TIME))
353 write_int(f, (int) file->modtime);
354 if (!(flags & SAME_MODE))
355 write_int(f, to_wire_mode(file->mode));
356 if (preserve_uid && !(flags & SAME_UID)) {
358 write_int(f, (int) file->uid);
360 if (preserve_gid && !(flags & SAME_GID)) {
362 write_int(f, (int) file->gid);
364 if (preserve_devices && IS_DEVICE(file->mode)
365 && !(flags & SAME_RDEV))
366 write_int(f, (int) file->rdev);
369 if (preserve_links && S_ISLNK(file->mode)) {
370 write_int(f, strlen(file->link));
371 write_buf(f, file->link, strlen(file->link));
375 #if SUPPORT_HARD_LINKS
376 if (preserve_hard_links && S_ISREG(file->mode)) {
377 if (remote_version < 26) {
378 /* 32-bit dev_t and ino_t */
379 write_int(f, (int) file->dev);
380 write_int(f, (int) file->inode);
382 /* 64-bit dev_t and ino_t */
383 write_longint(f, file->dev);
384 write_longint(f, file->inode);
389 if (always_checksum) {
390 if (remote_version < 21) {
391 write_buf(f, file->sum, 2);
393 write_buf(f, file->sum, MD4_SUM_LENGTH);
397 last_mode = file->mode;
398 last_rdev = file->rdev;
399 last_uid = file->uid;
400 last_gid = file->gid;
401 last_time = file->modtime;
403 strlcpy(lastname, fname, MAXPATHLEN);
404 lastname[MAXPATHLEN - 1] = 0;
409 static void receive_file_entry(struct file_struct **fptr,
410 unsigned flags, int f)
412 static time_t last_time;
413 static mode_t last_mode;
414 static dev_t last_rdev;
415 static uid_t last_uid;
416 static gid_t last_gid;
417 static char lastname[MAXPATHLEN];
418 char thisname[MAXPATHLEN];
419 unsigned int l1 = 0, l2 = 0;
421 struct file_struct *file;
423 if (flags & SAME_NAME)
426 if (flags & LONG_NAME)
431 file = (struct file_struct *) malloc(sizeof(*file));
433 out_of_memory("receive_file_entry");
434 memset((char *) file, 0, sizeof(*file));
437 if (l2 >= MAXPATHLEN - l1) {
439 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
440 flags, l1, l2, lastname);
441 overflow("receive_file_entry");
444 strlcpy(thisname, lastname, l1 + 1);
445 read_sbuf(f, &thisname[l1], l2);
446 thisname[l1 + l2] = 0;
448 strlcpy(lastname, thisname, MAXPATHLEN);
449 lastname[MAXPATHLEN - 1] = 0;
451 clean_fname(thisname);
453 if (sanitize_paths) {
454 sanitize_path(thisname, NULL);
457 if ((p = strrchr(thisname, '/'))) {
458 static char *lastdir;
460 if (lastdir && strcmp(thisname, lastdir) == 0) {
461 file->dirname = lastdir;
463 file->dirname = strdup(thisname);
464 lastdir = file->dirname;
466 file->basename = strdup(p + 1);
468 file->dirname = NULL;
469 file->basename = strdup(thisname);
473 out_of_memory("receive_file_entry 1");
477 file->length = read_longint(f);
479 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
481 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
484 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
487 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
488 if (preserve_devices && IS_DEVICE(file->mode))
490 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
492 if (preserve_links && S_ISLNK(file->mode)) {
495 rprintf(FERROR, "overflow: l=%d\n", l);
496 overflow("receive_file_entry");
498 file->link = (char *) malloc(l + 1);
500 out_of_memory("receive_file_entry 2");
501 read_sbuf(f, file->link, l);
502 if (sanitize_paths) {
503 sanitize_path(file->link, file->dirname);
506 #if SUPPORT_HARD_LINKS
507 if (preserve_hard_links && S_ISREG(file->mode)) {
508 if (remote_version < 26) {
509 file->dev = read_int(f);
510 file->inode = read_int(f);
512 file->dev = read_longint(f);
513 file->inode = read_longint(f);
518 if (always_checksum) {
519 file->sum = (char *) malloc(MD4_SUM_LENGTH);
521 out_of_memory("md4 sum");
522 if (remote_version < 21) {
523 read_buf(f, file->sum, 2);
525 read_buf(f, file->sum, MD4_SUM_LENGTH);
529 last_mode = file->mode;
530 last_rdev = file->rdev;
531 last_uid = file->uid;
532 last_gid = file->gid;
533 last_time = file->modtime;
535 if (!preserve_perms) {
536 extern int orig_umask;
537 /* set an appropriate set of permissions based on original
538 permissions and umask. This emulates what GNU cp does */
539 file->mode &= ~orig_umask;
544 /* determine if a file in a different filesstem should be skipped
545 when one_file_system is set. We bascally only want to include
546 the mount points - but they can be hard to find! */
547 static int skip_filesystem(char *fname, STRUCT_STAT * st)
550 char *p = strrchr(fname, '/');
552 /* skip all but directories */
553 if (!S_ISDIR(st->st_mode))
556 /* if its not a subdirectory then allow */
561 if (link_stat(fname, &st2)) {
567 return (st2.st_dev != filesystem_dev);
570 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
571 /* IRIX cc cares that the operands to the ternary have the same type. */
572 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
574 /* create a file_struct for a named file */
575 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
578 struct file_struct *file;
580 char sum[SUM_LENGTH];
582 char cleaned_name[MAXPATHLEN];
583 char linkbuf[MAXPATHLEN];
584 extern int module_id;
586 strlcpy(cleaned_name, fname, MAXPATHLEN);
587 cleaned_name[MAXPATHLEN - 1] = 0;
588 clean_fname(cleaned_name);
589 if (sanitize_paths) {
590 sanitize_path(cleaned_name, NULL);
592 fname = cleaned_name;
594 memset(sum, 0, SUM_LENGTH);
596 if (readlink_stat(fname, &st, linkbuf) != 0) {
597 int save_errno = errno;
598 if ((errno == ENOENT) && copy_links && !noexcludes) {
599 /* symlink pointing nowhere, see if excluded */
600 memset((char *) &st, 0, sizeof(st));
601 if (check_exclude_file(f, fname, &st)) {
602 /* file is excluded anyway, ignore silently */
607 rprintf(FERROR, "readlink %s: %s\n",
608 fname, strerror(save_errno));
612 /* we use noexcludes from backup.c */
616 if (S_ISDIR(st.st_mode) && !recurse) {
617 rprintf(FINFO, "skipping directory %s\n", fname);
621 if (one_file_system && st.st_dev != filesystem_dev) {
622 if (skip_filesystem(fname, &st))
626 if (check_exclude_file(f, fname, &st))
630 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
636 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
638 file = (struct file_struct *) malloc(sizeof(*file));
640 out_of_memory("make_file");
641 memset((char *) file, 0, sizeof(*file));
643 if ((p = strrchr(fname, '/'))) {
644 static char *lastdir;
646 if (lastdir && strcmp(fname, lastdir) == 0) {
647 file->dirname = lastdir;
649 file->dirname = strdup(fname);
650 lastdir = file->dirname;
652 file->basename = STRDUP(ap, p + 1);
655 file->dirname = NULL;
656 file->basename = STRDUP(ap, fname);
659 file->modtime = st.st_mtime;
660 file->length = st.st_size;
661 file->mode = st.st_mode;
662 file->uid = st.st_uid;
663 file->gid = st.st_gid;
664 file->dev = st.st_dev;
665 file->inode = st.st_ino;
667 file->rdev = st.st_rdev;
671 if (S_ISLNK(st.st_mode)) {
672 file->link = STRDUP(ap, linkbuf);
676 if (always_checksum) {
677 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
679 out_of_memory("md4 sum");
680 /* drat. we have to provide a null checksum for non-regular
681 files in order to be compatible with earlier versions
683 if (S_ISREG(st.st_mode)) {
684 file_checksum(fname, file->sum, st.st_size);
686 memset(file->sum, 0, MD4_SUM_LENGTH);
691 static char *lastdir;
692 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
693 file->basedir = lastdir;
695 file->basedir = strdup(flist_dir);
696 lastdir = file->basedir;
699 file->basedir = NULL;
702 if (!S_ISDIR(st.st_mode))
703 stats.total_size += st.st_size;
710 void send_file_name(int f, struct file_list *flist, char *fname,
711 int recursive, unsigned base_flags)
713 struct file_struct *file;
715 file = make_file(f, fname, &flist->string_area, 0);
720 if (show_build_progress_p() & !(flist->count % 100))
721 emit_build_progress(flist);
725 if (write_batch) /* dw */
726 file->flags = FLAG_DELETE;
728 if (strcmp(file->basename, "")) {
729 flist->files[flist->count++] = file;
730 send_file_entry(file, f, base_flags);
733 if (S_ISDIR(file->mode) && recursive) {
734 struct exclude_struct **last_exclude_list =
736 send_directory(f, flist, f_name(file));
737 local_exclude_list = last_exclude_list;
744 static void send_directory(int f, struct file_list *flist, char *dir)
748 char fname[MAXPATHLEN];
755 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
759 strlcpy(fname, dir, MAXPATHLEN);
761 if (fname[l - 1] != '/') {
762 if (l == MAXPATHLEN - 1) {
765 "skipping long-named directory %s\n",
770 strlcat(fname, "/", MAXPATHLEN);
773 p = fname + strlen(fname);
775 local_exclude_list = NULL;
778 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
779 strcpy(p, ".cvsignore");
781 make_exclude_list(fname, NULL, 0, 0);
785 "cannot cvs-exclude in long-named directory %s\n",
790 for (di = readdir(d); di; di = readdir(d)) {
791 char *dname = d_name(di);
792 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
794 strlcpy(p, dname, MAXPATHLEN - l);
795 send_file_name(f, flist, fname, recurse, 0);
798 if (local_exclude_list) {
799 add_exclude_list("!", &local_exclude_list, 0);
808 * I *think* f==-1 means that the list should just be built in memory
809 * and not transmitted. But who can tell? -- mbp
811 struct file_list *send_file_list(int f, int argc, char *argv[])
815 char *p, *dir, *olddir;
816 char lastpath[MAXPATHLEN] = "";
817 struct file_list *flist;
820 if (verbose && recurse && !am_server && f != -1) {
821 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
823 rprintf(FINFO, "\n");
827 start_write = stats.total_written;
832 io_start_buffering(f);
835 for (i = 0; i < argc; i++) {
836 char *fname = topsrcname;
838 strlcpy(fname, argv[i], MAXPATHLEN);
841 if (l != 1 && fname[l - 1] == '/') {
842 if ((l == 2) && (fname[0] == '.')) {
843 /* Turn ./ into just . rather than ./.
844 This was put in to avoid a problem with
845 rsync -aR --delete from ./
846 The send_file_name() below of ./ was
847 mysteriously preventing deletes */
850 strlcat(fname, ".", MAXPATHLEN);
854 if (link_stat(fname, &st) != 0) {
857 rprintf(FERROR, "link_stat %s : %s\n",
858 fname, strerror(errno));
863 if (S_ISDIR(st.st_mode) && !recurse) {
864 rprintf(FINFO, "skipping directory %s\n", fname);
871 if (!relative_paths) {
872 p = strrchr(fname, '/');
881 } else if (f != -1 && (p = strrchr(fname, '/'))) {
882 /* this ensures we send the intermediate directories,
883 thus getting their permissions right */
885 if (strcmp(lastpath, fname)) {
886 strlcpy(lastpath, fname, sizeof(lastpath));
888 for (p = fname + 1; (p = strchr(p, '/'));
890 int copy_links_saved = copy_links;
891 int recurse_saved = recurse;
893 copy_links = copy_unsafe_links;
894 /* set recurse to 1 to prevent make_file
895 from ignoring directory, but still
896 turn off the recursive parameter to
899 send_file_name(f, flist, fname, 0,
901 copy_links = copy_links_saved;
902 recurse = recurse_saved;
914 olddir = push_dir(dir, 1);
918 rprintf(FERROR, "push_dir %s : %s\n",
919 dir, strerror(errno));
927 set_filesystem(fname);
929 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
931 if (olddir != NULL) {
933 if (pop_dir(olddir) != 0) {
934 rprintf(FERROR, "pop_dir %s : %s\n",
935 dir, strerror(errno));
936 exit_cleanup(RERR_FILESELECT);
941 topsrcname[0] = '\0';
944 send_file_entry(NULL, f, 0);
947 finish_build_progress(flist);
949 clean_flist(flist, 0);
951 /* now send the uid/gid list. This was introduced in protocol
953 if (f != -1 && remote_version >= 15) {
957 /* if protocol version is >= 17 then send the io_error flag */
958 if (f != -1 && remote_version >= 17) {
959 extern int module_id;
960 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
965 stats.flist_size = stats.total_written - start_write;
966 stats.num_files = flist->count;
967 if (write_batch) /* dw */
968 write_batch_flist_info(flist->count, flist->files);
972 rprintf(FINFO, "send_file_list done\n");
978 struct file_list *recv_file_list(int f)
980 struct file_list *flist;
983 extern int list_only;
985 if (verbose && recurse && !am_server) {
986 rprintf(FINFO, "receiving file list ... ");
990 start_read = stats.total_read;
992 flist = (struct file_list *) malloc(sizeof(flist[0]));
997 flist->malloced = 1000;
999 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1005 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1006 flist_expand(flist);
1008 receive_file_entry(&flist->files[i], flags, f);
1010 if (S_ISREG(flist->files[i]->mode))
1011 stats.total_size += flist->files[i]->length;
1016 rprintf(FINFO, "recv_file_name(%s)\n",
1017 f_name(flist->files[i]));
1022 rprintf(FINFO, "received %d names\n", flist->count);
1024 clean_flist(flist, relative_paths);
1026 if (verbose && recurse && !am_server) {
1027 rprintf(FINFO, "done\n");
1030 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1031 if (f != -1 && remote_version >= 15) {
1032 recv_uid_list(f, flist);
1035 /* if protocol version is >= 17 then recv the io_error flag */
1036 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1037 extern int module_id;
1038 extern int ignore_errors;
1039 if (lp_ignore_errors(module_id) || ignore_errors) {
1042 io_error |= read_int(f);
1048 for (i = 0; i < flist->count; i++) {
1049 list_file_entry(flist->files[i]);
1055 rprintf(FINFO, "recv_file_list done\n");
1057 stats.flist_size = stats.total_read - start_read;
1058 stats.num_files = flist->count;
1063 out_of_memory("recv_file_list");
1064 return NULL; /* not reached */
1069 * XXX: This is currently the hottest function while building the file
1070 * list, because building f_name()s every time is expensive.
1072 int file_compare(struct file_struct **f1, struct file_struct **f2)
1074 if (!(*f1)->basename && !(*f2)->basename)
1076 if (!(*f1)->basename)
1078 if (!(*f2)->basename)
1080 if ((*f1)->dirname == (*f2)->dirname)
1081 return u_strcmp((*f1)->basename, (*f2)->basename);
1082 return u_strcmp(f_name(*f1), f_name(*f2));
1086 int flist_find(struct file_list *flist, struct file_struct *f)
1088 int low = 0, high = flist->count - 1;
1090 if (flist->count <= 0)
1093 while (low != high) {
1094 int mid = (low + high) / 2;
1096 file_compare(&flist->files[flist_up(flist, mid)], &f);
1098 return flist_up(flist, mid);
1106 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1107 return flist_up(flist, low);
1115 void free_file(struct file_struct *file)
1120 free(file->basename);
1130 * allocate a new file list
1132 struct file_list *flist_new()
1134 struct file_list *flist;
1136 flist = (struct file_list *) malloc(sizeof(flist[0]));
1138 out_of_memory("send_file_list");
1141 flist->malloced = 0;
1142 flist->files = NULL;
1145 flist->string_area = string_area_new(0);
1147 flist->string_area = NULL;
1153 * free up all elements in a flist
1155 void flist_free(struct file_list *flist)
1158 for (i = 1; i < flist->count; i++) {
1159 if (!flist->string_area)
1160 free_file(flist->files[i]);
1161 free(flist->files[i]);
1163 /* FIXME: I don't think we generally need to blank the flist
1164 * since it's about to be freed. This will just cause more
1165 * memory traffic. If you want a freed-memory debugger, you
1166 * know where to get it. */
1167 memset((char *) flist->files, 0,
1168 sizeof(flist->files[0]) * flist->count);
1170 if (flist->string_area)
1171 string_area_free(flist->string_area);
1172 memset((char *) flist, 0, sizeof(*flist));
1178 * This routine ensures we don't have any duplicate names in our file list.
1179 * duplicate names can cause corruption because of the pipelining
1181 static void clean_flist(struct file_list *flist, int strip_root)
1185 if (!flist || flist->count == 0)
1188 qsort(flist->files, flist->count,
1189 sizeof(flist->files[0]), (int (*)()) file_compare);
1191 for (i = 1; i < flist->count; i++) {
1192 if (flist->files[i]->basename &&
1193 flist->files[i - 1]->basename &&
1194 strcmp(f_name(flist->files[i]),
1195 f_name(flist->files[i - 1])) == 0) {
1196 if (verbose > 1 && !am_server)
1198 "removing duplicate name %s from file list %d\n",
1199 f_name(flist->files[i - 1]),
1201 /* it's not great that the flist knows the semantics of the
1202 * file memory usage, but i'd rather not add a flag byte
1203 * to that struct. XXX can i use a bit in the flags field? */
1204 if (flist->string_area)
1205 flist->files[i][0] = null_file;
1207 free_file(flist->files[i]);
1211 /* FIXME: There is a bug here when filenames are repeated more
1212 * than once, because we don't handle freed files when doing
1213 * the comparison. */
1216 /* we need to strip off the root directory in the case
1217 of relative paths, but this must be done _after_
1218 the sorting phase */
1219 for (i = 0; i < flist->count; i++) {
1220 if (flist->files[i]->dirname &&
1221 flist->files[i]->dirname[0] == '/') {
1222 memmove(&flist->files[i]->dirname[0],
1223 &flist->files[i]->dirname[1],
1224 strlen(flist->files[i]->dirname));
1227 if (flist->files[i]->dirname &&
1228 !flist->files[i]->dirname[0]) {
1229 flist->files[i]->dirname = NULL;
1238 for (i = 0; i < flist->count; i++) {
1239 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1241 NS(flist->files[i]->dirname),
1242 NS(flist->files[i]->basename),
1243 (int) flist->files[i]->mode,
1244 (double) flist->files[i]->length);
1250 * return the full filename of a flist entry
1252 * This function is too expensive at the moment, because it copies
1253 * strings when often we only want to compare them. In any case,
1254 * using strlcat is silly because it will walk the string repeatedly.
1256 char *f_name(struct file_struct *f)
1258 static char names[10][MAXPATHLEN];
1262 if (!f || !f->basename)
1270 off = strlcpy(p, f->dirname, MAXPATHLEN);
1271 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1272 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1274 strlcpy(p, f->basename, MAXPATHLEN);