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;
37 extern int do_progress;
39 extern int always_checksum;
41 extern int cvs_exclude;
45 extern int one_file_system;
46 extern int make_backups;
47 extern int preserve_links;
48 extern int preserve_hard_links;
49 extern int preserve_perms;
50 extern int preserve_devices;
51 extern int preserve_uid;
52 extern int preserve_gid;
53 extern int preserve_times;
54 extern int relative_paths;
55 extern int copy_links;
56 extern int copy_unsafe_links;
57 extern int remote_version;
59 extern int sanitize_paths;
61 extern int read_batch;
62 extern int write_batch;
64 static char topsrcname[MAXPATHLEN];
66 static struct exclude_struct **local_exclude_list;
68 static struct file_struct null_file;
70 static void clean_flist(struct file_list *flist, int strip_root);
73 static int show_filelist_p(void)
75 return verbose && recurse && !am_server;
78 static void start_filelist_progress(char *kind)
80 rprintf(FINFO, "%s ... ", kind);
81 if ((verbose > 1) || do_progress)
86 static void emit_filelist_progress(const struct file_list *flist)
88 rprintf(FINFO, " %d files...\r", flist->count);
92 static void finish_filelist_progress(const struct file_list *flist)
95 /* This overwrites the progress line */
96 rprintf(FINFO, "%d files to consider\n", flist->count);
98 rprintf(FINFO, "done\n");
102 void show_flist_stats(void)
108 static struct string_area *string_area_new(int size)
110 struct string_area *a;
114 a = malloc(sizeof(*a));
116 out_of_memory("string_area_new");
117 a->current = a->base = malloc(size);
119 out_of_memory("string_area_new buffer");
120 a->end = a->base + size;
126 static void string_area_free(struct string_area *a)
128 struct string_area *next;
130 for (; a; a = next) {
136 static char *string_area_malloc(struct string_area **ap, int size)
139 struct string_area *a;
141 /* does the request fit into the current space? */
143 if (a->current + size >= a->end) {
144 /* no; get space, move new string_area to front of the list */
145 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
150 /* have space; do the "allocation." */
156 static char *string_area_strdup(struct string_area **ap, const char *src)
158 char *dest = string_area_malloc(ap, strlen(src) + 1);
159 return strcpy(dest, src);
162 static void list_file_entry(struct file_struct *f)
167 /* this can happen if duplicate names were removed */
170 permstring(perms, f->mode);
172 if (preserve_links && S_ISLNK(f->mode)) {
173 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
175 (double) f->length, timestring(f->modtime),
178 rprintf(FINFO, "%s %11.0f %s %s\n",
180 (double) f->length, timestring(f->modtime),
186 int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
190 return do_stat(Path, Buffer);
192 if (do_lstat(Path, Buffer) == -1) {
195 if (S_ISLNK(Buffer->st_mode)) {
198 readlink((char *) Path, Linkbuf,
199 MAXPATHLEN - 1)) == -1) {
203 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
204 unsafe_symlink(Linkbuf, topsrcname)) {
205 return do_stat(Path, Buffer);
210 return do_stat(Path, Buffer);
214 int link_stat(const char *Path, STRUCT_STAT * Buffer)
218 return do_stat(Path, Buffer);
220 return do_lstat(Path, Buffer);
223 return do_stat(Path, Buffer);
228 This function is used to check if a file should be included/excluded
229 from the list of files based on its name and type etc
231 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
233 extern int delete_excluded;
235 /* f is set to -1 when calculating deletion file list */
236 if ((f == -1) && delete_excluded) {
239 if (check_exclude(fname, local_exclude_list, st)) {
245 /* used by the one_file_system code */
246 static dev_t filesystem_dev;
248 static void set_filesystem(char *fname)
251 if (link_stat(fname, &st) != 0)
253 filesystem_dev = st.st_dev;
257 static int to_wire_mode(mode_t mode)
259 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
260 return (mode & ~(_S_IFMT)) | 0120000;
265 static mode_t from_wire_mode(int mode)
267 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
268 return (mode & ~(_S_IFMT)) | _S_IFLNK;
270 return (mode_t) mode;
274 static void send_directory(int f, struct file_list *flist, char *dir);
276 static char *flist_dir;
280 * Make sure @p flist is big enough to hold at least @p flist->count
283 static void flist_expand(struct file_list *flist)
285 if (flist->count >= flist->malloced) {
289 if (flist->malloced < 1000)
290 flist->malloced += 1000;
292 flist->malloced *= 2;
294 new_bytes = sizeof(flist->files[0]) * flist->malloced;
296 new_ptr = realloc(flist->files, new_bytes);
299 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
301 (new_ptr == flist->files) ? " not" : "");
304 flist->files = (struct file_struct **) new_ptr;
307 out_of_memory("flist_expand");
312 static void send_file_entry(struct file_struct *file, int f,
316 static time_t last_time;
317 static mode_t last_mode;
318 static DEV64_T last_rdev;
319 static uid_t last_uid;
320 static gid_t last_gid;
321 static char lastname[MAXPATHLEN];
333 fname = f_name(file);
337 if (file->mode == last_mode)
339 if (file->rdev == last_rdev)
341 if (file->uid == last_uid)
343 if (file->gid == last_gid)
345 if (file->modtime == last_time)
349 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
351 l2 = strlen(fname) - l1;
358 /* we must make sure we don't send a zero flags byte or the other
359 end will terminate the flist transfer */
360 if (flags == 0 && !S_ISDIR(file->mode))
361 flags |= FLAG_DELETE;
365 write_byte(f, flags);
366 if (flags & SAME_NAME)
368 if (flags & LONG_NAME)
372 write_buf(f, fname + l1, l2);
374 write_longint(f, file->length);
375 if (!(flags & SAME_TIME))
376 write_int(f, (int) file->modtime);
377 if (!(flags & SAME_MODE))
378 write_int(f, to_wire_mode(file->mode));
379 if (preserve_uid && !(flags & SAME_UID)) {
381 write_int(f, (int) file->uid);
383 if (preserve_gid && !(flags & SAME_GID)) {
385 write_int(f, (int) file->gid);
387 if (preserve_devices && IS_DEVICE(file->mode)
388 && !(flags & SAME_RDEV))
389 write_int(f, (int) file->rdev);
392 if (preserve_links && S_ISLNK(file->mode)) {
393 write_int(f, strlen(file->link));
394 write_buf(f, file->link, strlen(file->link));
398 #if SUPPORT_HARD_LINKS
399 if (preserve_hard_links && S_ISREG(file->mode)) {
400 if (remote_version < 26) {
401 /* 32-bit dev_t and ino_t */
402 write_int(f, (int) file->dev);
403 write_int(f, (int) file->inode);
405 /* 64-bit dev_t and ino_t */
406 write_longint(f, file->dev);
407 write_longint(f, file->inode);
412 if (always_checksum) {
413 if (remote_version < 21) {
414 write_buf(f, file->sum, 2);
416 write_buf(f, file->sum, MD4_SUM_LENGTH);
420 last_mode = file->mode;
421 last_rdev = file->rdev;
422 last_uid = file->uid;
423 last_gid = file->gid;
424 last_time = file->modtime;
426 strlcpy(lastname, fname, MAXPATHLEN);
427 lastname[MAXPATHLEN - 1] = 0;
432 static void receive_file_entry(struct file_struct **fptr,
433 unsigned flags, int f)
435 static time_t last_time;
436 static mode_t last_mode;
437 static DEV64_T last_rdev;
438 static uid_t last_uid;
439 static gid_t last_gid;
440 static char lastname[MAXPATHLEN];
441 char thisname[MAXPATHLEN];
442 unsigned int l1 = 0, l2 = 0;
444 struct file_struct *file;
446 if (flags & SAME_NAME)
449 if (flags & LONG_NAME)
454 file = (struct file_struct *) malloc(sizeof(*file));
456 out_of_memory("receive_file_entry");
457 memset((char *) file, 0, sizeof(*file));
460 if (l2 >= MAXPATHLEN - l1) {
462 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
463 flags, l1, l2, lastname);
464 overflow("receive_file_entry");
467 strlcpy(thisname, lastname, l1 + 1);
468 read_sbuf(f, &thisname[l1], l2);
469 thisname[l1 + l2] = 0;
471 strlcpy(lastname, thisname, MAXPATHLEN);
472 lastname[MAXPATHLEN - 1] = 0;
474 clean_fname(thisname);
476 if (sanitize_paths) {
477 sanitize_path(thisname, NULL);
480 if ((p = strrchr(thisname, '/'))) {
481 static char *lastdir;
483 if (lastdir && strcmp(thisname, lastdir) == 0) {
484 file->dirname = lastdir;
486 file->dirname = strdup(thisname);
487 lastdir = file->dirname;
489 file->basename = strdup(p + 1);
491 file->dirname = NULL;
492 file->basename = strdup(thisname);
496 out_of_memory("receive_file_entry 1");
500 file->length = read_longint(f);
502 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
504 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
507 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
510 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
511 if (preserve_devices && IS_DEVICE(file->mode))
513 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
515 if (preserve_links && S_ISLNK(file->mode)) {
518 rprintf(FERROR, "overflow: l=%d\n", l);
519 overflow("receive_file_entry");
521 file->link = (char *) malloc(l + 1);
523 out_of_memory("receive_file_entry 2");
524 read_sbuf(f, file->link, l);
525 if (sanitize_paths) {
526 sanitize_path(file->link, file->dirname);
529 #if SUPPORT_HARD_LINKS
530 if (preserve_hard_links && S_ISREG(file->mode)) {
531 if (remote_version < 26) {
532 file->dev = read_int(f);
533 file->inode = read_int(f);
535 file->dev = read_longint(f);
536 file->inode = read_longint(f);
541 if (always_checksum) {
542 file->sum = (char *) malloc(MD4_SUM_LENGTH);
544 out_of_memory("md4 sum");
545 if (remote_version < 21) {
546 read_buf(f, file->sum, 2);
548 read_buf(f, file->sum, MD4_SUM_LENGTH);
552 last_mode = file->mode;
553 last_rdev = file->rdev;
554 last_uid = file->uid;
555 last_gid = file->gid;
556 last_time = file->modtime;
558 if (!preserve_perms) {
559 extern int orig_umask;
560 /* set an appropriate set of permissions based on original
561 permissions and umask. This emulates what GNU cp does */
562 file->mode &= ~orig_umask;
567 /* determine if a file in a different filesstem should be skipped
568 when one_file_system is set. We bascally only want to include
569 the mount points - but they can be hard to find! */
570 static int skip_filesystem(char *fname, STRUCT_STAT * st)
573 char *p = strrchr(fname, '/');
575 /* skip all but directories */
576 if (!S_ISDIR(st->st_mode))
579 /* if its not a subdirectory then allow */
584 if (link_stat(fname, &st2)) {
590 return (st2.st_dev != filesystem_dev);
593 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
594 /* IRIX cc cares that the operands to the ternary have the same type. */
595 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
597 /* create a file_struct for a named file */
598 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
601 struct file_struct *file;
603 char sum[SUM_LENGTH];
605 char cleaned_name[MAXPATHLEN];
606 char linkbuf[MAXPATHLEN];
607 extern int module_id;
609 strlcpy(cleaned_name, fname, MAXPATHLEN);
610 cleaned_name[MAXPATHLEN - 1] = 0;
611 clean_fname(cleaned_name);
612 if (sanitize_paths) {
613 sanitize_path(cleaned_name, NULL);
615 fname = cleaned_name;
617 memset(sum, 0, SUM_LENGTH);
619 if (readlink_stat(fname, &st, linkbuf) != 0) {
620 int save_errno = errno;
621 if ((errno == ENOENT) && copy_links && !noexcludes) {
622 /* symlink pointing nowhere, see if excluded */
623 memset((char *) &st, 0, sizeof(st));
624 if (check_exclude_file(f, fname, &st)) {
625 /* file is excluded anyway, ignore silently */
630 rprintf(FERROR, "readlink %s: %s\n",
631 fname, strerror(save_errno));
635 /* we use noexcludes from backup.c */
639 if (S_ISDIR(st.st_mode) && !recurse) {
640 rprintf(FINFO, "skipping directory %s\n", fname);
644 if (one_file_system && st.st_dev != filesystem_dev) {
645 if (skip_filesystem(fname, &st))
649 if (check_exclude_file(f, fname, &st))
653 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
659 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
661 file = (struct file_struct *) malloc(sizeof(*file));
663 out_of_memory("make_file");
664 memset((char *) file, 0, sizeof(*file));
666 if ((p = strrchr(fname, '/'))) {
667 static char *lastdir;
669 if (lastdir && strcmp(fname, lastdir) == 0) {
670 file->dirname = lastdir;
672 file->dirname = strdup(fname);
673 lastdir = file->dirname;
675 file->basename = STRDUP(ap, p + 1);
678 file->dirname = NULL;
679 file->basename = STRDUP(ap, fname);
682 file->modtime = st.st_mtime;
683 file->length = st.st_size;
684 file->mode = st.st_mode;
685 file->uid = st.st_uid;
686 file->gid = st.st_gid;
687 file->dev = st.st_dev;
688 file->inode = st.st_ino;
689 #ifdef HAVE_STRUCT_STAT_ST_RDEV
690 file->rdev = st.st_rdev;
694 if (S_ISLNK(st.st_mode)) {
695 file->link = STRDUP(ap, linkbuf);
699 if (always_checksum) {
700 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
702 out_of_memory("md4 sum");
703 /* drat. we have to provide a null checksum for non-regular
704 files in order to be compatible with earlier versions
706 if (S_ISREG(st.st_mode)) {
707 file_checksum(fname, file->sum, st.st_size);
709 memset(file->sum, 0, MD4_SUM_LENGTH);
714 static char *lastdir;
715 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
716 file->basedir = lastdir;
718 file->basedir = strdup(flist_dir);
719 lastdir = file->basedir;
722 file->basedir = NULL;
725 if (!S_ISDIR(st.st_mode))
726 stats.total_size += st.st_size;
733 void send_file_name(int f, struct file_list *flist, char *fname,
734 int recursive, unsigned base_flags)
736 struct file_struct *file;
738 file = make_file(f, fname, &flist->string_area, 0);
743 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
744 emit_filelist_progress(flist);
748 if (write_batch) /* dw */
749 file->flags = FLAG_DELETE;
751 if (strcmp(file->basename, "")) {
752 flist->files[flist->count++] = file;
753 send_file_entry(file, f, base_flags);
756 if (S_ISDIR(file->mode) && recursive) {
757 struct exclude_struct **last_exclude_list =
759 send_directory(f, flist, f_name(file));
760 local_exclude_list = last_exclude_list;
767 static void send_directory(int f, struct file_list *flist, char *dir)
771 char fname[MAXPATHLEN];
778 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
782 strlcpy(fname, dir, MAXPATHLEN);
784 if (fname[l - 1] != '/') {
785 if (l == MAXPATHLEN - 1) {
788 "skipping long-named directory %s\n",
793 strlcat(fname, "/", MAXPATHLEN);
796 p = fname + strlen(fname);
798 local_exclude_list = NULL;
801 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
802 strcpy(p, ".cvsignore");
804 make_exclude_list(fname, NULL, 0, 0);
808 "cannot cvs-exclude in long-named directory %s\n",
813 for (di = readdir(d); di; di = readdir(d)) {
814 char *dname = d_name(di);
815 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
817 strlcpy(p, dname, MAXPATHLEN - l);
818 send_file_name(f, flist, fname, recurse, 0);
821 if (local_exclude_list) {
822 add_exclude_list("!", &local_exclude_list, 0);
831 * I *think* f==-1 means that the list should just be built in memory
832 * and not transmitted. But who can tell? -- mbp
834 struct file_list *send_file_list(int f, int argc, char *argv[])
838 char *p, *dir, *olddir;
839 char lastpath[MAXPATHLEN] = "";
840 struct file_list *flist;
843 if (show_filelist_p() && f != -1)
844 start_filelist_progress("building file list");
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 if (show_filelist_p())
967 finish_filelist_progress(flist);
969 clean_flist(flist, 0);
971 /* now send the uid/gid list. This was introduced in protocol
973 if (f != -1 && remote_version >= 15) {
977 /* if protocol version is >= 17 then send the io_error flag */
978 if (f != -1 && remote_version >= 17) {
979 extern int module_id;
980 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
985 stats.flist_size = stats.total_written - start_write;
986 stats.num_files = flist->count;
987 if (write_batch) /* dw */
988 write_batch_flist_info(flist->count, flist->files);
992 rprintf(FINFO, "send_file_list done\n");
998 struct file_list *recv_file_list(int f)
1000 struct file_list *flist;
1001 unsigned char flags;
1003 extern int list_only;
1005 if (show_filelist_p())
1006 start_filelist_progress("receiving file list");
1008 start_read = stats.total_read;
1010 flist = (struct file_list *) malloc(sizeof(flist[0]));
1015 flist->malloced = 1000;
1017 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1023 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1024 int i = flist->count;
1026 flist_expand(flist);
1028 receive_file_entry(&flist->files[i], flags, f);
1030 if (S_ISREG(flist->files[i]->mode))
1031 stats.total_size += flist->files[i]->length;
1035 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
1036 emit_filelist_progress(flist);
1039 rprintf(FINFO, "recv_file_name(%s)\n",
1040 f_name(flist->files[i]));
1045 rprintf(FINFO, "received %d names\n", flist->count);
1047 clean_flist(flist, relative_paths);
1049 if (show_filelist_p()) {
1050 finish_filelist_progress(flist);
1053 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1054 if (f != -1 && remote_version >= 15) {
1055 recv_uid_list(f, flist);
1058 /* if protocol version is >= 17 then recv the io_error flag */
1059 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1060 extern int module_id;
1061 extern int ignore_errors;
1062 if (lp_ignore_errors(module_id) || ignore_errors) {
1065 io_error |= read_int(f);
1071 for (i = 0; i < flist->count; i++) {
1072 list_file_entry(flist->files[i]);
1078 rprintf(FINFO, "recv_file_list done\n");
1080 stats.flist_size = stats.total_read - start_read;
1081 stats.num_files = flist->count;
1086 out_of_memory("recv_file_list");
1087 return NULL; /* not reached */
1092 * XXX: This is currently the hottest function while building the file
1093 * list, because building f_name()s every time is expensive.
1095 int file_compare(struct file_struct **f1, struct file_struct **f2)
1097 if (!(*f1)->basename && !(*f2)->basename)
1099 if (!(*f1)->basename)
1101 if (!(*f2)->basename)
1103 if ((*f1)->dirname == (*f2)->dirname)
1104 return u_strcmp((*f1)->basename, (*f2)->basename);
1105 return u_strcmp(f_name(*f1), f_name(*f2));
1109 int flist_find(struct file_list *flist, struct file_struct *f)
1111 int low = 0, high = flist->count - 1;
1113 if (flist->count <= 0)
1116 while (low != high) {
1117 int mid = (low + high) / 2;
1119 file_compare(&flist->files[flist_up(flist, mid)], &f);
1121 return flist_up(flist, mid);
1129 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1130 return flist_up(flist, low);
1138 void free_file(struct file_struct *file)
1143 free(file->basename);
1153 * allocate a new file list
1155 struct file_list *flist_new()
1157 struct file_list *flist;
1159 flist = (struct file_list *) malloc(sizeof(flist[0]));
1161 out_of_memory("send_file_list");
1164 flist->malloced = 0;
1165 flist->files = NULL;
1168 flist->string_area = string_area_new(0);
1170 flist->string_area = NULL;
1176 * free up all elements in a flist
1178 void flist_free(struct file_list *flist)
1181 for (i = 1; i < flist->count; i++) {
1182 if (!flist->string_area)
1183 free_file(flist->files[i]);
1184 free(flist->files[i]);
1186 /* FIXME: I don't think we generally need to blank the flist
1187 * since it's about to be freed. This will just cause more
1188 * memory traffic. If you want a freed-memory debugger, you
1189 * know where to get it. */
1190 memset((char *) flist->files, 0,
1191 sizeof(flist->files[0]) * flist->count);
1193 if (flist->string_area)
1194 string_area_free(flist->string_area);
1195 memset((char *) flist, 0, sizeof(*flist));
1201 * This routine ensures we don't have any duplicate names in our file list.
1202 * duplicate names can cause corruption because of the pipelining
1204 static void clean_flist(struct file_list *flist, int strip_root)
1208 if (!flist || flist->count == 0)
1211 qsort(flist->files, flist->count,
1212 sizeof(flist->files[0]), (int (*)()) file_compare);
1214 for (i = 1; i < flist->count; i++) {
1215 if (flist->files[i]->basename &&
1216 flist->files[i - 1]->basename &&
1217 strcmp(f_name(flist->files[i]),
1218 f_name(flist->files[i - 1])) == 0) {
1219 if (verbose > 1 && !am_server)
1221 "removing duplicate name %s from file list %d\n",
1222 f_name(flist->files[i - 1]),
1224 /* it's not great that the flist knows the semantics of the
1225 * file memory usage, but i'd rather not add a flag byte
1226 * to that struct. XXX can i use a bit in the flags field? */
1227 if (flist->string_area)
1228 flist->files[i][0] = null_file;
1230 free_file(flist->files[i]);
1234 /* FIXME: There is a bug here when filenames are repeated more
1235 * than once, because we don't handle freed files when doing
1236 * the comparison. */
1239 /* we need to strip off the root directory in the case
1240 of relative paths, but this must be done _after_
1241 the sorting phase */
1242 for (i = 0; i < flist->count; i++) {
1243 if (flist->files[i]->dirname &&
1244 flist->files[i]->dirname[0] == '/') {
1245 memmove(&flist->files[i]->dirname[0],
1246 &flist->files[i]->dirname[1],
1247 strlen(flist->files[i]->dirname));
1250 if (flist->files[i]->dirname &&
1251 !flist->files[i]->dirname[0]) {
1252 flist->files[i]->dirname = NULL;
1261 for (i = 0; i < flist->count; i++) {
1262 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1264 NS(flist->files[i]->dirname),
1265 NS(flist->files[i]->basename),
1266 (int) flist->files[i]->mode,
1267 (double) flist->files[i]->length);
1273 * return the full filename of a flist entry
1275 * This function is too expensive at the moment, because it copies
1276 * strings when often we only want to compare them. In any case,
1277 * using strlcat is silly because it will walk the string repeatedly.
1279 char *f_name(struct file_struct *f)
1281 static char names[10][MAXPATHLEN];
1285 if (!f || !f->basename)
1293 off = strlcpy(p, f->dirname, MAXPATHLEN);
1294 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1295 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1297 strlcpy(p, f->basename, MAXPATHLEN);