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)
87 static void emit_filelist_progress(const struct file_list *flist)
89 rprintf(FINFO, " %d files...\r", flist->count);
93 static void maybe_emit_filelist_progress(const struct file_list *flist)
95 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
96 emit_filelist_progress(flist);
100 static void finish_filelist_progress(const struct file_list *flist)
103 /* This overwrites the progress line */
104 rprintf(FINFO, "%d files to consider\n", flist->count);
106 rprintf(FINFO, "done\n");
110 void show_flist_stats(void)
116 static struct string_area *string_area_new(int size)
118 struct string_area *a;
122 a = malloc(sizeof(*a));
124 out_of_memory("string_area_new");
125 a->current = a->base = malloc(size);
127 out_of_memory("string_area_new buffer");
128 a->end = a->base + size;
134 static void string_area_free(struct string_area *a)
136 struct string_area *next;
138 for (; a; a = next) {
144 static char *string_area_malloc(struct string_area **ap, int size)
147 struct string_area *a;
149 /* does the request fit into the current space? */
151 if (a->current + size >= a->end) {
152 /* no; get space, move new string_area to front of the list */
153 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
158 /* have space; do the "allocation." */
164 static char *string_area_strdup(struct string_area **ap, const char *src)
166 char *dest = string_area_malloc(ap, strlen(src) + 1);
167 return strcpy(dest, src);
170 static void list_file_entry(struct file_struct *f)
175 /* this can happen if duplicate names were removed */
178 permstring(perms, f->mode);
180 if (preserve_links && S_ISLNK(f->mode)) {
181 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
183 (double) f->length, timestring(f->modtime),
186 rprintf(FINFO, "%s %11.0f %s %s\n",
188 (double) f->length, timestring(f->modtime),
194 int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
198 return do_stat(Path, Buffer);
200 if (do_lstat(Path, Buffer) == -1) {
203 if (S_ISLNK(Buffer->st_mode)) {
206 readlink((char *) Path, Linkbuf,
207 MAXPATHLEN - 1)) == -1) {
211 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
212 unsafe_symlink(Linkbuf, topsrcname)) {
213 return do_stat(Path, Buffer);
218 return do_stat(Path, Buffer);
222 int link_stat(const char *Path, STRUCT_STAT * Buffer)
226 return do_stat(Path, Buffer);
228 return do_lstat(Path, Buffer);
231 return do_stat(Path, Buffer);
236 This function is used to check if a file should be included/excluded
237 from the list of files based on its name and type etc
239 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
241 extern int delete_excluded;
243 /* f is set to -1 when calculating deletion file list */
244 if ((f == -1) && delete_excluded) {
247 if (check_exclude(fname, local_exclude_list, st)) {
253 /* used by the one_file_system code */
254 static dev_t filesystem_dev;
256 static void set_filesystem(char *fname)
259 if (link_stat(fname, &st) != 0)
261 filesystem_dev = st.st_dev;
265 static int to_wire_mode(mode_t mode)
267 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
268 return (mode & ~(_S_IFMT)) | 0120000;
273 static mode_t from_wire_mode(int mode)
275 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
276 return (mode & ~(_S_IFMT)) | _S_IFLNK;
278 return (mode_t) mode;
282 static void send_directory(int f, struct file_list *flist, char *dir);
284 static char *flist_dir;
288 * Make sure @p flist is big enough to hold at least @p flist->count
291 static void flist_expand(struct file_list *flist)
293 if (flist->count >= flist->malloced) {
297 if (flist->malloced < 1000)
298 flist->malloced += 1000;
300 flist->malloced *= 2;
302 new_bytes = sizeof(flist->files[0]) * flist->malloced;
305 new_ptr = realloc(flist->files, new_bytes);
307 new_ptr = malloc(new_bytes);
310 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
312 (new_ptr == flist->files) ? " not" : "");
315 flist->files = (struct file_struct **) new_ptr;
318 out_of_memory("flist_expand");
323 static void send_file_entry(struct file_struct *file, int f,
327 static time_t last_time;
328 static mode_t last_mode;
329 static DEV64_T last_rdev;
330 static uid_t last_uid;
331 static gid_t last_gid;
332 static char lastname[MAXPATHLEN];
344 fname = f_name(file);
348 if (file->mode == last_mode)
350 if (file->rdev == last_rdev)
352 if (file->uid == last_uid)
354 if (file->gid == last_gid)
356 if (file->modtime == last_time)
360 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
362 l2 = strlen(fname) - l1;
369 /* we must make sure we don't send a zero flags byte or the other
370 end will terminate the flist transfer */
371 if (flags == 0 && !S_ISDIR(file->mode))
372 flags |= FLAG_DELETE;
376 write_byte(f, flags);
377 if (flags & SAME_NAME)
379 if (flags & LONG_NAME)
383 write_buf(f, fname + l1, l2);
385 write_longint(f, file->length);
386 if (!(flags & SAME_TIME))
387 write_int(f, (int) file->modtime);
388 if (!(flags & SAME_MODE))
389 write_int(f, to_wire_mode(file->mode));
390 if (preserve_uid && !(flags & SAME_UID)) {
392 write_int(f, (int) file->uid);
394 if (preserve_gid && !(flags & SAME_GID)) {
396 write_int(f, (int) file->gid);
398 if (preserve_devices && IS_DEVICE(file->mode)
399 && !(flags & SAME_RDEV))
400 write_int(f, (int) file->rdev);
403 if (preserve_links && S_ISLNK(file->mode)) {
404 write_int(f, strlen(file->link));
405 write_buf(f, file->link, strlen(file->link));
409 #if SUPPORT_HARD_LINKS
410 if (preserve_hard_links && S_ISREG(file->mode)) {
411 if (remote_version < 26) {
412 /* 32-bit dev_t and ino_t */
413 write_int(f, (int) file->dev);
414 write_int(f, (int) file->inode);
416 /* 64-bit dev_t and ino_t */
417 write_longint(f, file->dev);
418 write_longint(f, file->inode);
423 if (always_checksum) {
424 if (remote_version < 21) {
425 write_buf(f, file->sum, 2);
427 write_buf(f, file->sum, MD4_SUM_LENGTH);
431 last_mode = file->mode;
432 last_rdev = file->rdev;
433 last_uid = file->uid;
434 last_gid = file->gid;
435 last_time = file->modtime;
437 strlcpy(lastname, fname, MAXPATHLEN);
438 lastname[MAXPATHLEN - 1] = 0;
443 static void receive_file_entry(struct file_struct **fptr,
444 unsigned flags, int f)
446 static time_t last_time;
447 static mode_t last_mode;
448 static DEV64_T last_rdev;
449 static uid_t last_uid;
450 static gid_t last_gid;
451 static char lastname[MAXPATHLEN];
452 char thisname[MAXPATHLEN];
453 unsigned int l1 = 0, l2 = 0;
455 struct file_struct *file;
457 if (flags & SAME_NAME)
460 if (flags & LONG_NAME)
465 file = (struct file_struct *) malloc(sizeof(*file));
467 out_of_memory("receive_file_entry");
468 memset((char *) file, 0, sizeof(*file));
471 if (l2 >= MAXPATHLEN - l1) {
473 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
474 flags, l1, l2, lastname);
475 overflow("receive_file_entry");
478 strlcpy(thisname, lastname, l1 + 1);
479 read_sbuf(f, &thisname[l1], l2);
480 thisname[l1 + l2] = 0;
482 strlcpy(lastname, thisname, MAXPATHLEN);
483 lastname[MAXPATHLEN - 1] = 0;
485 clean_fname(thisname);
487 if (sanitize_paths) {
488 sanitize_path(thisname, NULL);
491 if ((p = strrchr(thisname, '/'))) {
492 static char *lastdir;
494 if (lastdir && strcmp(thisname, lastdir) == 0) {
495 file->dirname = lastdir;
497 file->dirname = strdup(thisname);
498 lastdir = file->dirname;
500 file->basename = strdup(p + 1);
502 file->dirname = NULL;
503 file->basename = strdup(thisname);
507 out_of_memory("receive_file_entry 1");
511 file->length = read_longint(f);
513 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
515 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
518 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
521 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
522 if (preserve_devices && IS_DEVICE(file->mode))
524 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
526 if (preserve_links && S_ISLNK(file->mode)) {
529 rprintf(FERROR, "overflow: l=%d\n", l);
530 overflow("receive_file_entry");
532 file->link = (char *) malloc(l + 1);
534 out_of_memory("receive_file_entry 2");
535 read_sbuf(f, file->link, l);
536 if (sanitize_paths) {
537 sanitize_path(file->link, file->dirname);
540 #if SUPPORT_HARD_LINKS
541 if (preserve_hard_links && S_ISREG(file->mode)) {
542 if (remote_version < 26) {
543 file->dev = read_int(f);
544 file->inode = read_int(f);
546 file->dev = read_longint(f);
547 file->inode = read_longint(f);
552 if (always_checksum) {
553 file->sum = (char *) malloc(MD4_SUM_LENGTH);
555 out_of_memory("md4 sum");
556 if (remote_version < 21) {
557 read_buf(f, file->sum, 2);
559 read_buf(f, file->sum, MD4_SUM_LENGTH);
563 last_mode = file->mode;
564 last_rdev = file->rdev;
565 last_uid = file->uid;
566 last_gid = file->gid;
567 last_time = file->modtime;
569 if (!preserve_perms) {
570 extern int orig_umask;
571 /* set an appropriate set of permissions based on original
572 permissions and umask. This emulates what GNU cp does */
573 file->mode &= ~orig_umask;
578 /* determine if a file in a different filesstem should be skipped
579 when one_file_system is set. We bascally only want to include
580 the mount points - but they can be hard to find! */
581 static int skip_filesystem(char *fname, STRUCT_STAT * st)
584 char *p = strrchr(fname, '/');
586 /* skip all but directories */
587 if (!S_ISDIR(st->st_mode))
590 /* if its not a subdirectory then allow */
595 if (link_stat(fname, &st2)) {
601 return (st2.st_dev != filesystem_dev);
604 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
605 /* IRIX cc cares that the operands to the ternary have the same type. */
606 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
609 * Create a file_struct for a named file by reading its stat()
610 * information and performing extensive checks against global
613 * @return the new file, or NULL if there was an error or this file
614 * should be excluded.
616 * @todo There is a small optimization opportunity here to avoid
617 * stat()ing the file in some circumstances, which has a certain cost.
618 * We are called immediately after doing readdir(), and so we may
619 * already know the d_type of the file. We could for example avoid
620 * statting directories if we're not recursing, but this is not a very
621 * important case. Some systems may not have d_type.
623 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
626 struct file_struct *file;
628 char sum[SUM_LENGTH];
630 char cleaned_name[MAXPATHLEN];
631 char linkbuf[MAXPATHLEN];
632 extern int module_id;
634 strlcpy(cleaned_name, fname, MAXPATHLEN);
635 cleaned_name[MAXPATHLEN - 1] = 0;
636 clean_fname(cleaned_name);
637 if (sanitize_paths) {
638 sanitize_path(cleaned_name, NULL);
640 fname = cleaned_name;
642 memset(sum, 0, SUM_LENGTH);
644 if (readlink_stat(fname, &st, linkbuf) != 0) {
645 int save_errno = errno;
646 if ((errno == ENOENT) && copy_links && !noexcludes) {
647 /* symlink pointing nowhere, see if excluded */
648 memset((char *) &st, 0, sizeof(st));
649 if (check_exclude_file(f, fname, &st)) {
650 /* file is excluded anyway, ignore silently */
655 rprintf(FERROR, "readlink %s: %s\n",
656 fname, strerror(save_errno));
660 /* we use noexcludes from backup.c */
664 if (S_ISDIR(st.st_mode) && !recurse) {
665 rprintf(FINFO, "skipping directory %s\n", fname);
669 if (one_file_system && st.st_dev != filesystem_dev) {
670 if (skip_filesystem(fname, &st))
674 if (check_exclude_file(f, fname, &st))
678 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
684 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
686 file = (struct file_struct *) malloc(sizeof(*file));
688 out_of_memory("make_file");
689 memset((char *) file, 0, sizeof(*file));
691 if ((p = strrchr(fname, '/'))) {
692 static char *lastdir;
694 if (lastdir && strcmp(fname, lastdir) == 0) {
695 file->dirname = lastdir;
697 file->dirname = strdup(fname);
698 lastdir = file->dirname;
700 file->basename = STRDUP(ap, p + 1);
703 file->dirname = NULL;
704 file->basename = STRDUP(ap, fname);
707 file->modtime = st.st_mtime;
708 file->length = st.st_size;
709 file->mode = st.st_mode;
710 file->uid = st.st_uid;
711 file->gid = st.st_gid;
712 file->dev = st.st_dev;
713 file->inode = st.st_ino;
714 #ifdef HAVE_STRUCT_STAT_ST_RDEV
715 file->rdev = st.st_rdev;
719 if (S_ISLNK(st.st_mode)) {
720 file->link = STRDUP(ap, linkbuf);
724 if (always_checksum) {
725 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
727 out_of_memory("md4 sum");
728 /* drat. we have to provide a null checksum for non-regular
729 files in order to be compatible with earlier versions
731 if (S_ISREG(st.st_mode)) {
732 file_checksum(fname, file->sum, st.st_size);
734 memset(file->sum, 0, MD4_SUM_LENGTH);
739 static char *lastdir;
740 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
741 file->basedir = lastdir;
743 file->basedir = strdup(flist_dir);
744 lastdir = file->basedir;
747 file->basedir = NULL;
750 if (!S_ISDIR(st.st_mode))
751 stats.total_size += st.st_size;
758 void send_file_name(int f, struct file_list *flist, char *fname,
759 int recursive, unsigned base_flags)
761 struct file_struct *file;
763 file = make_file(f, fname, &flist->string_area, 0);
768 maybe_emit_filelist_progress(flist);
772 if (write_batch) /* dw */
773 file->flags = FLAG_DELETE;
775 if (strcmp(file->basename, "")) {
776 flist->files[flist->count++] = file;
777 send_file_entry(file, f, base_flags);
780 if (S_ISDIR(file->mode) && recursive) {
781 struct exclude_struct **last_exclude_list =
783 send_directory(f, flist, f_name(file));
784 local_exclude_list = last_exclude_list;
791 static void send_directory(int f, struct file_list *flist, char *dir)
795 char fname[MAXPATHLEN];
802 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
806 strlcpy(fname, dir, MAXPATHLEN);
808 if (fname[l - 1] != '/') {
809 if (l == MAXPATHLEN - 1) {
812 "skipping long-named directory %s\n",
817 strlcat(fname, "/", MAXPATHLEN);
820 p = fname + strlen(fname);
822 local_exclude_list = NULL;
825 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
826 strcpy(p, ".cvsignore");
828 make_exclude_list(fname, NULL, 0, 0);
832 "cannot cvs-exclude in long-named directory %s\n",
837 for (di = readdir(d); di; di = readdir(d)) {
838 char *dname = d_name(di);
839 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
841 strlcpy(p, dname, MAXPATHLEN - l);
842 send_file_name(f, flist, fname, recurse, 0);
845 if (local_exclude_list) {
846 add_exclude_list("!", &local_exclude_list, 0);
855 * I *think* f==-1 means that the list should just be built in memory
856 * and not transmitted. But who can tell? -- mbp
858 struct file_list *send_file_list(int f, int argc, char *argv[])
862 char *p, *dir, *olddir;
863 char lastpath[MAXPATHLEN] = "";
864 struct file_list *flist;
867 if (show_filelist_p() && f != -1)
868 start_filelist_progress("building file list");
870 start_write = stats.total_written;
875 io_start_buffering(f);
878 for (i = 0; i < argc; i++) {
879 char *fname = topsrcname;
881 strlcpy(fname, argv[i], MAXPATHLEN);
884 if (l != 1 && fname[l - 1] == '/') {
885 if ((l == 2) && (fname[0] == '.')) {
886 /* Turn ./ into just . rather than ./.
887 This was put in to avoid a problem with
888 rsync -aR --delete from ./
889 The send_file_name() below of ./ was
890 mysteriously preventing deletes */
893 strlcat(fname, ".", MAXPATHLEN);
897 if (link_stat(fname, &st) != 0) {
900 rprintf(FERROR, "link_stat %s : %s\n",
901 fname, strerror(errno));
906 if (S_ISDIR(st.st_mode) && !recurse) {
907 rprintf(FINFO, "skipping directory %s\n", fname);
914 if (!relative_paths) {
915 p = strrchr(fname, '/');
924 } else if (f != -1 && (p = strrchr(fname, '/'))) {
925 /* this ensures we send the intermediate directories,
926 thus getting their permissions right */
928 if (strcmp(lastpath, fname)) {
929 strlcpy(lastpath, fname, sizeof(lastpath));
931 for (p = fname + 1; (p = strchr(p, '/'));
933 int copy_links_saved = copy_links;
934 int recurse_saved = recurse;
936 copy_links = copy_unsafe_links;
937 /* set recurse to 1 to prevent make_file
938 from ignoring directory, but still
939 turn off the recursive parameter to
942 send_file_name(f, flist, fname, 0,
944 copy_links = copy_links_saved;
945 recurse = recurse_saved;
957 olddir = push_dir(dir, 1);
961 rprintf(FERROR, "push_dir %s : %s\n",
962 dir, strerror(errno));
970 set_filesystem(fname);
972 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
974 if (olddir != NULL) {
976 if (pop_dir(olddir) != 0) {
977 rprintf(FERROR, "pop_dir %s : %s\n",
978 dir, strerror(errno));
979 exit_cleanup(RERR_FILESELECT);
984 topsrcname[0] = '\0';
987 send_file_entry(NULL, f, 0);
990 if (show_filelist_p())
991 finish_filelist_progress(flist);
993 clean_flist(flist, 0);
995 /* now send the uid/gid list. This was introduced in protocol
997 if (f != -1 && remote_version >= 15) {
1001 /* if protocol version is >= 17 then send the io_error flag */
1002 if (f != -1 && remote_version >= 17) {
1003 extern int module_id;
1004 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1009 stats.flist_size = stats.total_written - start_write;
1010 stats.num_files = flist->count;
1011 if (write_batch) /* dw */
1012 write_batch_flist_info(flist->count, flist->files);
1016 rprintf(FINFO, "send_file_list done\n");
1022 struct file_list *recv_file_list(int f)
1024 struct file_list *flist;
1025 unsigned char flags;
1027 extern int list_only;
1029 if (show_filelist_p())
1030 start_filelist_progress("receiving file list");
1032 start_read = stats.total_read;
1034 flist = (struct file_list *) malloc(sizeof(flist[0]));
1039 flist->malloced = 1000;
1041 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1047 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1048 int i = flist->count;
1050 flist_expand(flist);
1052 receive_file_entry(&flist->files[i], flags, f);
1054 if (S_ISREG(flist->files[i]->mode))
1055 stats.total_size += flist->files[i]->length;
1059 maybe_emit_filelist_progress(flist);
1062 rprintf(FINFO, "recv_file_name(%s)\n",
1063 f_name(flist->files[i]));
1068 rprintf(FINFO, "received %d names\n", flist->count);
1070 clean_flist(flist, relative_paths);
1072 if (show_filelist_p()) {
1073 finish_filelist_progress(flist);
1076 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1077 if (f != -1 && remote_version >= 15) {
1078 recv_uid_list(f, flist);
1081 /* if protocol version is >= 17 then recv the io_error flag */
1082 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1083 extern int module_id;
1084 extern int ignore_errors;
1085 if (lp_ignore_errors(module_id) || ignore_errors) {
1088 io_error |= read_int(f);
1094 for (i = 0; i < flist->count; i++) {
1095 list_file_entry(flist->files[i]);
1101 rprintf(FINFO, "recv_file_list done\n");
1103 stats.flist_size = stats.total_read - start_read;
1104 stats.num_files = flist->count;
1109 out_of_memory("recv_file_list");
1110 return NULL; /* not reached */
1115 * XXX: This is currently the hottest function while building the file
1116 * list, because building f_name()s every time is expensive.
1118 int file_compare(struct file_struct **f1, struct file_struct **f2)
1120 if (!(*f1)->basename && !(*f2)->basename)
1122 if (!(*f1)->basename)
1124 if (!(*f2)->basename)
1126 if ((*f1)->dirname == (*f2)->dirname)
1127 return u_strcmp((*f1)->basename, (*f2)->basename);
1128 return u_strcmp(f_name(*f1), f_name(*f2));
1132 int flist_find(struct file_list *flist, struct file_struct *f)
1134 int low = 0, high = flist->count - 1;
1136 if (flist->count <= 0)
1139 while (low != high) {
1140 int mid = (low + high) / 2;
1142 file_compare(&flist->files[flist_up(flist, mid)], &f);
1144 return flist_up(flist, mid);
1152 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1153 return flist_up(flist, low);
1161 void free_file(struct file_struct *file)
1166 free(file->basename);
1176 * allocate a new file list
1178 struct file_list *flist_new()
1180 struct file_list *flist;
1182 flist = (struct file_list *) malloc(sizeof(flist[0]));
1184 out_of_memory("send_file_list");
1187 flist->malloced = 0;
1188 flist->files = NULL;
1191 flist->string_area = string_area_new(0);
1193 flist->string_area = NULL;
1199 * free up all elements in a flist
1201 void flist_free(struct file_list *flist)
1204 for (i = 1; i < flist->count; i++) {
1205 if (!flist->string_area)
1206 free_file(flist->files[i]);
1207 free(flist->files[i]);
1209 /* FIXME: I don't think we generally need to blank the flist
1210 * since it's about to be freed. This will just cause more
1211 * memory traffic. If you want a freed-memory debugger, you
1212 * know where to get it. */
1213 memset((char *) flist->files, 0,
1214 sizeof(flist->files[0]) * flist->count);
1216 if (flist->string_area)
1217 string_area_free(flist->string_area);
1218 memset((char *) flist, 0, sizeof(*flist));
1224 * This routine ensures we don't have any duplicate names in our file list.
1225 * duplicate names can cause corruption because of the pipelining
1227 static void clean_flist(struct file_list *flist, int strip_root)
1231 if (!flist || flist->count == 0)
1234 qsort(flist->files, flist->count,
1235 sizeof(flist->files[0]), (int (*)()) file_compare);
1237 for (i = 1; i < flist->count; i++) {
1238 if (flist->files[i]->basename &&
1239 flist->files[i - 1]->basename &&
1240 strcmp(f_name(flist->files[i]),
1241 f_name(flist->files[i - 1])) == 0) {
1242 if (verbose > 1 && !am_server)
1244 "removing duplicate name %s from file list %d\n",
1245 f_name(flist->files[i - 1]),
1247 /* it's not great that the flist knows the semantics of the
1248 * file memory usage, but i'd rather not add a flag byte
1249 * to that struct. XXX can i use a bit in the flags field? */
1250 if (flist->string_area)
1251 flist->files[i][0] = null_file;
1253 free_file(flist->files[i]);
1257 /* FIXME: There is a bug here when filenames are repeated more
1258 * than once, because we don't handle freed files when doing
1259 * the comparison. */
1262 /* we need to strip off the root directory in the case
1263 of relative paths, but this must be done _after_
1264 the sorting phase */
1265 for (i = 0; i < flist->count; i++) {
1266 if (flist->files[i]->dirname &&
1267 flist->files[i]->dirname[0] == '/') {
1268 memmove(&flist->files[i]->dirname[0],
1269 &flist->files[i]->dirname[1],
1270 strlen(flist->files[i]->dirname));
1273 if (flist->files[i]->dirname &&
1274 !flist->files[i]->dirname[0]) {
1275 flist->files[i]->dirname = NULL;
1284 for (i = 0; i < flist->count; i++) {
1285 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1287 NS(flist->files[i]->dirname),
1288 NS(flist->files[i]->basename),
1289 (int) flist->files[i]->mode,
1290 (double) flist->files[i]->length);
1296 * return the full filename of a flist entry
1298 * This function is too expensive at the moment, because it copies
1299 * strings when often we only want to compare them. In any case,
1300 * using strlcat is silly because it will walk the string repeatedly.
1302 char *f_name(struct file_struct *f)
1304 static char names[10][MAXPATHLEN];
1308 if (!f || !f->basename)
1316 off = strlcpy(p, f->dirname, MAXPATHLEN);
1317 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1318 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1320 strlcpy(p, f->basename, MAXPATHLEN);