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) {
285 if (flist->malloced < 1000)
286 flist->malloced += 1000;
288 flist->malloced *= 2;
290 new_bytes = sizeof(flist->files[0]) * flist->malloced;
292 new_ptr = realloc(flist->files, new_bytes);
295 rprintf(FINFO, RSYNC_NAME ": expand file_list to %.0f bytes, did%s move\n",
297 (new_ptr == flist->files) ? " not" : "");
300 flist->files = (struct file_struct **) new_ptr;
303 out_of_memory("flist_expand");
308 static void send_file_entry(struct file_struct *file, int f,
312 static time_t last_time;
313 static mode_t last_mode;
314 static DEV64_T last_rdev;
315 static uid_t last_uid;
316 static gid_t last_gid;
317 static char lastname[MAXPATHLEN];
329 fname = f_name(file);
333 if (file->mode == last_mode)
335 if (file->rdev == last_rdev)
337 if (file->uid == last_uid)
339 if (file->gid == last_gid)
341 if (file->modtime == last_time)
345 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
347 l2 = strlen(fname) - l1;
354 /* we must make sure we don't send a zero flags byte or the other
355 end will terminate the flist transfer */
356 if (flags == 0 && !S_ISDIR(file->mode))
357 flags |= FLAG_DELETE;
361 write_byte(f, flags);
362 if (flags & SAME_NAME)
364 if (flags & LONG_NAME)
368 write_buf(f, fname + l1, l2);
370 write_longint(f, file->length);
371 if (!(flags & SAME_TIME))
372 write_int(f, (int) file->modtime);
373 if (!(flags & SAME_MODE))
374 write_int(f, to_wire_mode(file->mode));
375 if (preserve_uid && !(flags & SAME_UID)) {
377 write_int(f, (int) file->uid);
379 if (preserve_gid && !(flags & SAME_GID)) {
381 write_int(f, (int) file->gid);
383 if (preserve_devices && IS_DEVICE(file->mode)
384 && !(flags & SAME_RDEV))
385 write_int(f, (int) file->rdev);
388 if (preserve_links && S_ISLNK(file->mode)) {
389 write_int(f, strlen(file->link));
390 write_buf(f, file->link, strlen(file->link));
394 #if SUPPORT_HARD_LINKS
395 if (preserve_hard_links && S_ISREG(file->mode)) {
396 if (remote_version < 26) {
397 /* 32-bit dev_t and ino_t */
398 write_int(f, (int) file->dev);
399 write_int(f, (int) file->inode);
401 /* 64-bit dev_t and ino_t */
402 write_longint(f, file->dev);
403 write_longint(f, file->inode);
408 if (always_checksum) {
409 if (remote_version < 21) {
410 write_buf(f, file->sum, 2);
412 write_buf(f, file->sum, MD4_SUM_LENGTH);
416 last_mode = file->mode;
417 last_rdev = file->rdev;
418 last_uid = file->uid;
419 last_gid = file->gid;
420 last_time = file->modtime;
422 strlcpy(lastname, fname, MAXPATHLEN);
423 lastname[MAXPATHLEN - 1] = 0;
428 static void receive_file_entry(struct file_struct **fptr,
429 unsigned flags, int f)
431 static time_t last_time;
432 static mode_t last_mode;
433 static DEV64_T last_rdev;
434 static uid_t last_uid;
435 static gid_t last_gid;
436 static char lastname[MAXPATHLEN];
437 char thisname[MAXPATHLEN];
438 unsigned int l1 = 0, l2 = 0;
440 struct file_struct *file;
442 if (flags & SAME_NAME)
445 if (flags & LONG_NAME)
450 file = (struct file_struct *) malloc(sizeof(*file));
452 out_of_memory("receive_file_entry");
453 memset((char *) file, 0, sizeof(*file));
456 if (l2 >= MAXPATHLEN - l1) {
458 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
459 flags, l1, l2, lastname);
460 overflow("receive_file_entry");
463 strlcpy(thisname, lastname, l1 + 1);
464 read_sbuf(f, &thisname[l1], l2);
465 thisname[l1 + l2] = 0;
467 strlcpy(lastname, thisname, MAXPATHLEN);
468 lastname[MAXPATHLEN - 1] = 0;
470 clean_fname(thisname);
472 if (sanitize_paths) {
473 sanitize_path(thisname, NULL);
476 if ((p = strrchr(thisname, '/'))) {
477 static char *lastdir;
479 if (lastdir && strcmp(thisname, lastdir) == 0) {
480 file->dirname = lastdir;
482 file->dirname = strdup(thisname);
483 lastdir = file->dirname;
485 file->basename = strdup(p + 1);
487 file->dirname = NULL;
488 file->basename = strdup(thisname);
492 out_of_memory("receive_file_entry 1");
496 file->length = read_longint(f);
498 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
500 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
503 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
506 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
507 if (preserve_devices && IS_DEVICE(file->mode))
509 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
511 if (preserve_links && S_ISLNK(file->mode)) {
514 rprintf(FERROR, "overflow: l=%d\n", l);
515 overflow("receive_file_entry");
517 file->link = (char *) malloc(l + 1);
519 out_of_memory("receive_file_entry 2");
520 read_sbuf(f, file->link, l);
521 if (sanitize_paths) {
522 sanitize_path(file->link, file->dirname);
525 #if SUPPORT_HARD_LINKS
526 if (preserve_hard_links && S_ISREG(file->mode)) {
527 if (remote_version < 26) {
528 file->dev = read_int(f);
529 file->inode = read_int(f);
531 file->dev = read_longint(f);
532 file->inode = read_longint(f);
537 if (always_checksum) {
538 file->sum = (char *) malloc(MD4_SUM_LENGTH);
540 out_of_memory("md4 sum");
541 if (remote_version < 21) {
542 read_buf(f, file->sum, 2);
544 read_buf(f, file->sum, MD4_SUM_LENGTH);
548 last_mode = file->mode;
549 last_rdev = file->rdev;
550 last_uid = file->uid;
551 last_gid = file->gid;
552 last_time = file->modtime;
554 if (!preserve_perms) {
555 extern int orig_umask;
556 /* set an appropriate set of permissions based on original
557 permissions and umask. This emulates what GNU cp does */
558 file->mode &= ~orig_umask;
563 /* determine if a file in a different filesstem should be skipped
564 when one_file_system is set. We bascally only want to include
565 the mount points - but they can be hard to find! */
566 static int skip_filesystem(char *fname, STRUCT_STAT * st)
569 char *p = strrchr(fname, '/');
571 /* skip all but directories */
572 if (!S_ISDIR(st->st_mode))
575 /* if its not a subdirectory then allow */
580 if (link_stat(fname, &st2)) {
586 return (st2.st_dev != filesystem_dev);
589 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
590 /* IRIX cc cares that the operands to the ternary have the same type. */
591 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
593 /* create a file_struct for a named file */
594 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
597 struct file_struct *file;
599 char sum[SUM_LENGTH];
601 char cleaned_name[MAXPATHLEN];
602 char linkbuf[MAXPATHLEN];
603 extern int module_id;
605 strlcpy(cleaned_name, fname, MAXPATHLEN);
606 cleaned_name[MAXPATHLEN - 1] = 0;
607 clean_fname(cleaned_name);
608 if (sanitize_paths) {
609 sanitize_path(cleaned_name, NULL);
611 fname = cleaned_name;
613 memset(sum, 0, SUM_LENGTH);
615 if (readlink_stat(fname, &st, linkbuf) != 0) {
616 int save_errno = errno;
617 if ((errno == ENOENT) && copy_links && !noexcludes) {
618 /* symlink pointing nowhere, see if excluded */
619 memset((char *) &st, 0, sizeof(st));
620 if (check_exclude_file(f, fname, &st)) {
621 /* file is excluded anyway, ignore silently */
626 rprintf(FERROR, "readlink %s: %s\n",
627 fname, strerror(save_errno));
631 /* we use noexcludes from backup.c */
635 if (S_ISDIR(st.st_mode) && !recurse) {
636 rprintf(FINFO, "skipping directory %s\n", fname);
640 if (one_file_system && st.st_dev != filesystem_dev) {
641 if (skip_filesystem(fname, &st))
645 if (check_exclude_file(f, fname, &st))
649 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
655 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
657 file = (struct file_struct *) malloc(sizeof(*file));
659 out_of_memory("make_file");
660 memset((char *) file, 0, sizeof(*file));
662 if ((p = strrchr(fname, '/'))) {
663 static char *lastdir;
665 if (lastdir && strcmp(fname, lastdir) == 0) {
666 file->dirname = lastdir;
668 file->dirname = strdup(fname);
669 lastdir = file->dirname;
671 file->basename = STRDUP(ap, p + 1);
674 file->dirname = NULL;
675 file->basename = STRDUP(ap, fname);
678 file->modtime = st.st_mtime;
679 file->length = st.st_size;
680 file->mode = st.st_mode;
681 file->uid = st.st_uid;
682 file->gid = st.st_gid;
683 file->dev = st.st_dev;
684 file->inode = st.st_ino;
685 #ifdef HAVE_STRUCT_STAT_ST_RDEV
686 file->rdev = st.st_rdev;
690 if (S_ISLNK(st.st_mode)) {
691 file->link = STRDUP(ap, linkbuf);
695 if (always_checksum) {
696 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
698 out_of_memory("md4 sum");
699 /* drat. we have to provide a null checksum for non-regular
700 files in order to be compatible with earlier versions
702 if (S_ISREG(st.st_mode)) {
703 file_checksum(fname, file->sum, st.st_size);
705 memset(file->sum, 0, MD4_SUM_LENGTH);
710 static char *lastdir;
711 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
712 file->basedir = lastdir;
714 file->basedir = strdup(flist_dir);
715 lastdir = file->basedir;
718 file->basedir = NULL;
721 if (!S_ISDIR(st.st_mode))
722 stats.total_size += st.st_size;
729 void send_file_name(int f, struct file_list *flist, char *fname,
730 int recursive, unsigned base_flags)
732 struct file_struct *file;
734 file = make_file(f, fname, &flist->string_area, 0);
739 if (show_build_progress_p() & !(flist->count % 100))
740 emit_build_progress(flist);
744 if (write_batch) /* dw */
745 file->flags = FLAG_DELETE;
747 if (strcmp(file->basename, "")) {
748 flist->files[flist->count++] = file;
749 send_file_entry(file, f, base_flags);
752 if (S_ISDIR(file->mode) && recursive) {
753 struct exclude_struct **last_exclude_list =
755 send_directory(f, flist, f_name(file));
756 local_exclude_list = last_exclude_list;
763 static void send_directory(int f, struct file_list *flist, char *dir)
767 char fname[MAXPATHLEN];
774 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
778 strlcpy(fname, dir, MAXPATHLEN);
780 if (fname[l - 1] != '/') {
781 if (l == MAXPATHLEN - 1) {
784 "skipping long-named directory %s\n",
789 strlcat(fname, "/", MAXPATHLEN);
792 p = fname + strlen(fname);
794 local_exclude_list = NULL;
797 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
798 strcpy(p, ".cvsignore");
800 make_exclude_list(fname, NULL, 0, 0);
804 "cannot cvs-exclude in long-named directory %s\n",
809 for (di = readdir(d); di; di = readdir(d)) {
810 char *dname = d_name(di);
811 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
813 strlcpy(p, dname, MAXPATHLEN - l);
814 send_file_name(f, flist, fname, recurse, 0);
817 if (local_exclude_list) {
818 add_exclude_list("!", &local_exclude_list, 0);
827 * I *think* f==-1 means that the list should just be built in memory
828 * and not transmitted. But who can tell? -- mbp
830 struct file_list *send_file_list(int f, int argc, char *argv[])
834 char *p, *dir, *olddir;
835 char lastpath[MAXPATHLEN] = "";
836 struct file_list *flist;
839 if (verbose && recurse && !am_server && f != -1) {
840 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
842 rprintf(FINFO, "\n");
846 start_write = stats.total_written;
851 io_start_buffering(f);
854 for (i = 0; i < argc; i++) {
855 char *fname = topsrcname;
857 strlcpy(fname, argv[i], MAXPATHLEN);
860 if (l != 1 && fname[l - 1] == '/') {
861 if ((l == 2) && (fname[0] == '.')) {
862 /* Turn ./ into just . rather than ./.
863 This was put in to avoid a problem with
864 rsync -aR --delete from ./
865 The send_file_name() below of ./ was
866 mysteriously preventing deletes */
869 strlcat(fname, ".", MAXPATHLEN);
873 if (link_stat(fname, &st) != 0) {
876 rprintf(FERROR, "link_stat %s : %s\n",
877 fname, strerror(errno));
882 if (S_ISDIR(st.st_mode) && !recurse) {
883 rprintf(FINFO, "skipping directory %s\n", fname);
890 if (!relative_paths) {
891 p = strrchr(fname, '/');
900 } else if (f != -1 && (p = strrchr(fname, '/'))) {
901 /* this ensures we send the intermediate directories,
902 thus getting their permissions right */
904 if (strcmp(lastpath, fname)) {
905 strlcpy(lastpath, fname, sizeof(lastpath));
907 for (p = fname + 1; (p = strchr(p, '/'));
909 int copy_links_saved = copy_links;
910 int recurse_saved = recurse;
912 copy_links = copy_unsafe_links;
913 /* set recurse to 1 to prevent make_file
914 from ignoring directory, but still
915 turn off the recursive parameter to
918 send_file_name(f, flist, fname, 0,
920 copy_links = copy_links_saved;
921 recurse = recurse_saved;
933 olddir = push_dir(dir, 1);
937 rprintf(FERROR, "push_dir %s : %s\n",
938 dir, strerror(errno));
946 set_filesystem(fname);
948 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
950 if (olddir != NULL) {
952 if (pop_dir(olddir) != 0) {
953 rprintf(FERROR, "pop_dir %s : %s\n",
954 dir, strerror(errno));
955 exit_cleanup(RERR_FILESELECT);
960 topsrcname[0] = '\0';
963 send_file_entry(NULL, f, 0);
966 finish_build_progress(flist);
968 clean_flist(flist, 0);
970 /* now send the uid/gid list. This was introduced in protocol
972 if (f != -1 && remote_version >= 15) {
976 /* if protocol version is >= 17 then send the io_error flag */
977 if (f != -1 && remote_version >= 17) {
978 extern int module_id;
979 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
984 stats.flist_size = stats.total_written - start_write;
985 stats.num_files = flist->count;
986 if (write_batch) /* dw */
987 write_batch_flist_info(flist->count, flist->files);
991 rprintf(FINFO, "send_file_list done\n");
997 struct file_list *recv_file_list(int f)
999 struct file_list *flist;
1000 unsigned char flags;
1002 extern int list_only;
1004 if (verbose && recurse && !am_server) {
1005 rprintf(FINFO, "receiving file list ... ");
1009 start_read = stats.total_read;
1011 flist = (struct file_list *) malloc(sizeof(flist[0]));
1016 flist->malloced = 1000;
1018 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1024 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1025 int i = flist->count;
1027 flist_expand(flist);
1029 receive_file_entry(&flist->files[i], flags, f);
1031 if (S_ISREG(flist->files[i]->mode))
1032 stats.total_size += flist->files[i]->length;
1037 rprintf(FINFO, "recv_file_name(%s)\n",
1038 f_name(flist->files[i]));
1043 rprintf(FINFO, "received %d names\n", flist->count);
1045 clean_flist(flist, relative_paths);
1047 if (verbose && recurse && !am_server) {
1048 rprintf(FINFO, "done\n");
1051 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1052 if (f != -1 && remote_version >= 15) {
1053 recv_uid_list(f, flist);
1056 /* if protocol version is >= 17 then recv the io_error flag */
1057 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1058 extern int module_id;
1059 extern int ignore_errors;
1060 if (lp_ignore_errors(module_id) || ignore_errors) {
1063 io_error |= read_int(f);
1069 for (i = 0; i < flist->count; i++) {
1070 list_file_entry(flist->files[i]);
1076 rprintf(FINFO, "recv_file_list done\n");
1078 stats.flist_size = stats.total_read - start_read;
1079 stats.num_files = flist->count;
1084 out_of_memory("recv_file_list");
1085 return NULL; /* not reached */
1090 * XXX: This is currently the hottest function while building the file
1091 * list, because building f_name()s every time is expensive.
1093 int file_compare(struct file_struct **f1, struct file_struct **f2)
1095 if (!(*f1)->basename && !(*f2)->basename)
1097 if (!(*f1)->basename)
1099 if (!(*f2)->basename)
1101 if ((*f1)->dirname == (*f2)->dirname)
1102 return u_strcmp((*f1)->basename, (*f2)->basename);
1103 return u_strcmp(f_name(*f1), f_name(*f2));
1107 int flist_find(struct file_list *flist, struct file_struct *f)
1109 int low = 0, high = flist->count - 1;
1111 if (flist->count <= 0)
1114 while (low != high) {
1115 int mid = (low + high) / 2;
1117 file_compare(&flist->files[flist_up(flist, mid)], &f);
1119 return flist_up(flist, mid);
1127 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1128 return flist_up(flist, low);
1136 void free_file(struct file_struct *file)
1141 free(file->basename);
1151 * allocate a new file list
1153 struct file_list *flist_new()
1155 struct file_list *flist;
1157 flist = (struct file_list *) malloc(sizeof(flist[0]));
1159 out_of_memory("send_file_list");
1162 flist->malloced = 0;
1163 flist->files = NULL;
1166 flist->string_area = string_area_new(0);
1168 flist->string_area = NULL;
1174 * free up all elements in a flist
1176 void flist_free(struct file_list *flist)
1179 for (i = 1; i < flist->count; i++) {
1180 if (!flist->string_area)
1181 free_file(flist->files[i]);
1182 free(flist->files[i]);
1184 /* FIXME: I don't think we generally need to blank the flist
1185 * since it's about to be freed. This will just cause more
1186 * memory traffic. If you want a freed-memory debugger, you
1187 * know where to get it. */
1188 memset((char *) flist->files, 0,
1189 sizeof(flist->files[0]) * flist->count);
1191 if (flist->string_area)
1192 string_area_free(flist->string_area);
1193 memset((char *) flist, 0, sizeof(*flist));
1199 * This routine ensures we don't have any duplicate names in our file list.
1200 * duplicate names can cause corruption because of the pipelining
1202 static void clean_flist(struct file_list *flist, int strip_root)
1206 if (!flist || flist->count == 0)
1209 qsort(flist->files, flist->count,
1210 sizeof(flist->files[0]), (int (*)()) file_compare);
1212 for (i = 1; i < flist->count; i++) {
1213 if (flist->files[i]->basename &&
1214 flist->files[i - 1]->basename &&
1215 strcmp(f_name(flist->files[i]),
1216 f_name(flist->files[i - 1])) == 0) {
1217 if (verbose > 1 && !am_server)
1219 "removing duplicate name %s from file list %d\n",
1220 f_name(flist->files[i - 1]),
1222 /* it's not great that the flist knows the semantics of the
1223 * file memory usage, but i'd rather not add a flag byte
1224 * to that struct. XXX can i use a bit in the flags field? */
1225 if (flist->string_area)
1226 flist->files[i][0] = null_file;
1228 free_file(flist->files[i]);
1232 /* FIXME: There is a bug here when filenames are repeated more
1233 * than once, because we don't handle freed files when doing
1234 * the comparison. */
1237 /* we need to strip off the root directory in the case
1238 of relative paths, but this must be done _after_
1239 the sorting phase */
1240 for (i = 0; i < flist->count; i++) {
1241 if (flist->files[i]->dirname &&
1242 flist->files[i]->dirname[0] == '/') {
1243 memmove(&flist->files[i]->dirname[0],
1244 &flist->files[i]->dirname[1],
1245 strlen(flist->files[i]->dirname));
1248 if (flist->files[i]->dirname &&
1249 !flist->files[i]->dirname[0]) {
1250 flist->files[i]->dirname = NULL;
1259 for (i = 0; i < flist->count; i++) {
1260 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1262 NS(flist->files[i]->dirname),
1263 NS(flist->files[i]->basename),
1264 (int) flist->files[i]->mode,
1265 (double) flist->files[i]->length);
1271 * return the full filename of a flist entry
1273 * This function is too expensive at the moment, because it copies
1274 * strings when often we only want to compare them. In any case,
1275 * using strlcat is silly because it will walk the string repeatedly.
1277 char *f_name(struct file_struct *f)
1279 static char names[10][MAXPATHLEN];
1283 if (!f || !f->basename)
1291 off = strlcpy(p, f->dirname, MAXPATHLEN);
1292 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1293 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1295 strlcpy(p, f->basename, MAXPATHLEN);