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 file%sto consider\n",
105 flist->count, flist->count == 1 ? " " : "s ");
107 rprintf(FINFO, "done\n");
111 void show_flist_stats(void)
117 static struct string_area *string_area_new(int size)
119 struct string_area *a;
123 a = malloc(sizeof(*a));
125 out_of_memory("string_area_new");
126 a->current = a->base = malloc(size);
128 out_of_memory("string_area_new buffer");
129 a->end = a->base + size;
135 static void string_area_free(struct string_area *a)
137 struct string_area *next;
139 for (; a; a = next) {
145 static char *string_area_malloc(struct string_area **ap, int size)
148 struct string_area *a;
150 /* does the request fit into the current space? */
152 if (a->current + size >= a->end) {
153 /* no; get space, move new string_area to front of the list */
154 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
159 /* have space; do the "allocation." */
165 static char *string_area_strdup(struct string_area **ap, const char *src)
167 char *dest = string_area_malloc(ap, strlen(src) + 1);
168 return strcpy(dest, src);
171 static void list_file_entry(struct file_struct *f)
176 /* this can happen if duplicate names were removed */
179 permstring(perms, f->mode);
181 if (preserve_links && S_ISLNK(f->mode)) {
182 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
184 (double) f->length, timestring(f->modtime),
187 rprintf(FINFO, "%s %11.0f %s %s\n",
189 (double) f->length, timestring(f->modtime),
196 * Stat either a symlink or its referent, depending on the settings of
197 * copy_links, copy_unsafe_links, etc.
199 * @retval -1 on error
201 * @retval 0 for success
203 * @post If @p path is a symlink, then @p linkbuf (of size @c
204 * MAXPATHLEN) contains the symlink target.
206 * @post @p buffer contains information about the link or the
207 * referrent as appropriate, if they exist.
209 int readlink_stat(const char *path, STRUCT_STAT * buffer, char *linkbuf)
213 return do_stat(path, buffer);
215 if (do_lstat(path, buffer) == -1) {
218 if (S_ISLNK(buffer->st_mode)) {
220 l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
224 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
225 unsafe_symlink(linkbuf, topsrcname)) {
226 return do_stat(path, buffer);
231 return do_stat(path, buffer);
235 int link_stat(const char *path, STRUCT_STAT * buffer)
239 return do_stat(path, buffer);
241 return do_lstat(path, buffer);
244 return do_stat(path, buffer);
249 This function is used to check if a file should be included/excluded
250 from the list of files based on its name and type etc
252 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
254 extern int delete_excluded;
256 /* f is set to -1 when calculating deletion file list */
257 if ((f == -1) && delete_excluded) {
260 if (check_exclude(fname, local_exclude_list, st)) {
266 /* used by the one_file_system code */
267 static dev_t filesystem_dev;
269 static void set_filesystem(char *fname)
272 if (link_stat(fname, &st) != 0)
274 filesystem_dev = st.st_dev;
278 static int to_wire_mode(mode_t mode)
280 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
281 return (mode & ~(_S_IFMT)) | 0120000;
286 static mode_t from_wire_mode(int mode)
288 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
289 return (mode & ~(_S_IFMT)) | _S_IFLNK;
291 return (mode_t) mode;
295 static void send_directory(int f, struct file_list *flist, char *dir);
297 static char *flist_dir;
301 * Make sure @p flist is big enough to hold at least @p flist->count
304 static void flist_expand(struct file_list *flist)
306 if (flist->count >= flist->malloced) {
310 if (flist->malloced < 1000)
311 flist->malloced += 1000;
313 flist->malloced *= 2;
315 new_bytes = sizeof(flist->files[0]) * flist->malloced;
318 new_ptr = realloc(flist->files, new_bytes);
320 new_ptr = malloc(new_bytes);
323 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
325 (new_ptr == flist->files) ? " not" : "");
328 flist->files = (struct file_struct **) new_ptr;
331 out_of_memory("flist_expand");
336 static void send_file_entry(struct file_struct *file, int f,
340 static time_t last_time;
341 static mode_t last_mode;
342 static DEV64_T last_rdev;
343 static uid_t last_uid;
344 static gid_t last_gid;
345 static char lastname[MAXPATHLEN];
357 fname = f_name(file);
361 if (file->mode == last_mode)
363 if (file->rdev == last_rdev)
365 if (file->uid == last_uid)
367 if (file->gid == last_gid)
369 if (file->modtime == last_time)
373 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
375 l2 = strlen(fname) - l1;
382 /* we must make sure we don't send a zero flags byte or the other
383 end will terminate the flist transfer */
384 if (flags == 0 && !S_ISDIR(file->mode))
385 flags |= FLAG_DELETE;
389 write_byte(f, flags);
390 if (flags & SAME_NAME)
392 if (flags & LONG_NAME)
396 write_buf(f, fname + l1, l2);
398 write_longint(f, file->length);
399 if (!(flags & SAME_TIME))
400 write_int(f, (int) file->modtime);
401 if (!(flags & SAME_MODE))
402 write_int(f, to_wire_mode(file->mode));
403 if (preserve_uid && !(flags & SAME_UID)) {
405 write_int(f, (int) file->uid);
407 if (preserve_gid && !(flags & SAME_GID)) {
409 write_int(f, (int) file->gid);
411 if (preserve_devices && IS_DEVICE(file->mode)
412 && !(flags & SAME_RDEV))
413 write_int(f, (int) file->rdev);
416 if (preserve_links && S_ISLNK(file->mode)) {
417 write_int(f, strlen(file->link));
418 write_buf(f, file->link, strlen(file->link));
422 #if SUPPORT_HARD_LINKS
423 if (preserve_hard_links && S_ISREG(file->mode)) {
424 if (remote_version < 26) {
425 /* 32-bit dev_t and ino_t */
426 write_int(f, (int) file->dev);
427 write_int(f, (int) file->inode);
429 /* 64-bit dev_t and ino_t */
430 write_longint(f, file->dev);
431 write_longint(f, file->inode);
436 if (always_checksum) {
437 if (remote_version < 21) {
438 write_buf(f, file->sum, 2);
440 write_buf(f, file->sum, MD4_SUM_LENGTH);
444 last_mode = file->mode;
445 last_rdev = file->rdev;
446 last_uid = file->uid;
447 last_gid = file->gid;
448 last_time = file->modtime;
450 strlcpy(lastname, fname, MAXPATHLEN);
451 lastname[MAXPATHLEN - 1] = 0;
456 static void receive_file_entry(struct file_struct **fptr,
457 unsigned flags, int f)
459 static time_t last_time;
460 static mode_t last_mode;
461 static DEV64_T last_rdev;
462 static uid_t last_uid;
463 static gid_t last_gid;
464 static char lastname[MAXPATHLEN];
465 char thisname[MAXPATHLEN];
466 unsigned int l1 = 0, l2 = 0;
468 struct file_struct *file;
470 if (flags & SAME_NAME)
473 if (flags & LONG_NAME)
478 file = (struct file_struct *) malloc(sizeof(*file));
480 out_of_memory("receive_file_entry");
481 memset((char *) file, 0, sizeof(*file));
484 if (l2 >= MAXPATHLEN - l1) {
486 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
487 flags, l1, l2, lastname);
488 overflow("receive_file_entry");
491 strlcpy(thisname, lastname, l1 + 1);
492 read_sbuf(f, &thisname[l1], l2);
493 thisname[l1 + l2] = 0;
495 strlcpy(lastname, thisname, MAXPATHLEN);
496 lastname[MAXPATHLEN - 1] = 0;
498 clean_fname(thisname);
500 if (sanitize_paths) {
501 sanitize_path(thisname, NULL);
504 if ((p = strrchr(thisname, '/'))) {
505 static char *lastdir;
507 if (lastdir && strcmp(thisname, lastdir) == 0) {
508 file->dirname = lastdir;
510 file->dirname = strdup(thisname);
511 lastdir = file->dirname;
513 file->basename = strdup(p + 1);
515 file->dirname = NULL;
516 file->basename = strdup(thisname);
520 out_of_memory("receive_file_entry 1");
524 file->length = read_longint(f);
526 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
528 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
531 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
534 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
535 if (preserve_devices && IS_DEVICE(file->mode))
537 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
539 if (preserve_links && S_ISLNK(file->mode)) {
542 rprintf(FERROR, "overflow: l=%d\n", l);
543 overflow("receive_file_entry");
545 file->link = (char *) malloc(l + 1);
547 out_of_memory("receive_file_entry 2");
548 read_sbuf(f, file->link, l);
549 if (sanitize_paths) {
550 sanitize_path(file->link, file->dirname);
553 #if SUPPORT_HARD_LINKS
554 if (preserve_hard_links && S_ISREG(file->mode)) {
555 if (remote_version < 26) {
556 file->dev = read_int(f);
557 file->inode = read_int(f);
559 file->dev = read_longint(f);
560 file->inode = read_longint(f);
565 if (always_checksum) {
566 file->sum = (char *) malloc(MD4_SUM_LENGTH);
568 out_of_memory("md4 sum");
569 if (remote_version < 21) {
570 read_buf(f, file->sum, 2);
572 read_buf(f, file->sum, MD4_SUM_LENGTH);
576 last_mode = file->mode;
577 last_rdev = file->rdev;
578 last_uid = file->uid;
579 last_gid = file->gid;
580 last_time = file->modtime;
582 if (!preserve_perms) {
583 extern int orig_umask;
584 /* set an appropriate set of permissions based on original
585 permissions and umask. This emulates what GNU cp does */
586 file->mode &= ~orig_umask;
591 /* determine if a file in a different filesstem should be skipped
592 when one_file_system is set. We bascally only want to include
593 the mount points - but they can be hard to find! */
594 static int skip_filesystem(char *fname, STRUCT_STAT * st)
597 char *p = strrchr(fname, '/');
599 /* skip all but directories */
600 if (!S_ISDIR(st->st_mode))
603 /* if its not a subdirectory then allow */
608 if (link_stat(fname, &st2)) {
614 return (st2.st_dev != filesystem_dev);
617 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
618 /* IRIX cc cares that the operands to the ternary have the same type. */
619 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
622 * Create a file_struct for a named file by reading its stat()
623 * information and performing extensive checks against global
626 * @return the new file, or NULL if there was an error or this file
627 * should be excluded.
629 * @todo There is a small optimization opportunity here to avoid
630 * stat()ing the file in some circumstances, which has a certain cost.
631 * We are called immediately after doing readdir(), and so we may
632 * already know the d_type of the file. We could for example avoid
633 * statting directories if we're not recursing, but this is not a very
634 * important case. Some systems may not have d_type.
636 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
639 struct file_struct *file;
641 char sum[SUM_LENGTH];
643 char cleaned_name[MAXPATHLEN];
644 char linkbuf[MAXPATHLEN];
645 extern int module_id;
647 strlcpy(cleaned_name, fname, MAXPATHLEN);
648 cleaned_name[MAXPATHLEN - 1] = 0;
649 clean_fname(cleaned_name);
650 if (sanitize_paths) {
651 sanitize_path(cleaned_name, NULL);
653 fname = cleaned_name;
655 memset(sum, 0, SUM_LENGTH);
657 if (readlink_stat(fname, &st, linkbuf) != 0) {
658 int save_errno = errno;
659 if ((errno == ENOENT) && copy_links && !noexcludes) {
660 /* symlink pointing nowhere, see if excluded */
661 memset((char *) &st, 0, sizeof(st));
662 if (check_exclude_file(f, fname, &st)) {
663 /* file is excluded anyway, ignore silently */
668 rprintf(FERROR, "readlink %s: %s\n",
669 fname, strerror(save_errno));
673 /* we use noexcludes from backup.c */
677 if (S_ISDIR(st.st_mode) && !recurse) {
678 rprintf(FINFO, "skipping directory %s\n", fname);
682 if (one_file_system && st.st_dev != filesystem_dev) {
683 if (skip_filesystem(fname, &st))
687 if (check_exclude_file(f, fname, &st))
691 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
697 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
699 file = (struct file_struct *) malloc(sizeof(*file));
701 out_of_memory("make_file");
702 memset((char *) file, 0, sizeof(*file));
704 if ((p = strrchr(fname, '/'))) {
705 static char *lastdir;
707 if (lastdir && strcmp(fname, lastdir) == 0) {
708 file->dirname = lastdir;
710 file->dirname = strdup(fname);
711 lastdir = file->dirname;
713 file->basename = STRDUP(ap, p + 1);
716 file->dirname = NULL;
717 file->basename = STRDUP(ap, fname);
720 file->modtime = st.st_mtime;
721 file->length = st.st_size;
722 file->mode = st.st_mode;
723 file->uid = st.st_uid;
724 file->gid = st.st_gid;
725 file->dev = st.st_dev;
726 file->inode = st.st_ino;
727 #ifdef HAVE_STRUCT_STAT_ST_RDEV
728 file->rdev = st.st_rdev;
732 if (S_ISLNK(st.st_mode)) {
733 file->link = STRDUP(ap, linkbuf);
737 if (always_checksum) {
738 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
740 out_of_memory("md4 sum");
741 /* drat. we have to provide a null checksum for non-regular
742 files in order to be compatible with earlier versions
744 if (S_ISREG(st.st_mode)) {
745 file_checksum(fname, file->sum, st.st_size);
747 memset(file->sum, 0, MD4_SUM_LENGTH);
752 static char *lastdir;
753 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
754 file->basedir = lastdir;
756 file->basedir = strdup(flist_dir);
757 lastdir = file->basedir;
760 file->basedir = NULL;
763 if (!S_ISDIR(st.st_mode))
764 stats.total_size += st.st_size;
771 void send_file_name(int f, struct file_list *flist, char *fname,
772 int recursive, unsigned base_flags)
774 struct file_struct *file;
776 file = make_file(f, fname, &flist->string_area, 0);
781 maybe_emit_filelist_progress(flist);
785 if (write_batch) /* dw */
786 file->flags = FLAG_DELETE;
788 if (strcmp(file->basename, "")) {
789 flist->files[flist->count++] = file;
790 send_file_entry(file, f, base_flags);
793 if (S_ISDIR(file->mode) && recursive) {
794 struct exclude_struct **last_exclude_list =
796 send_directory(f, flist, f_name(file));
797 local_exclude_list = last_exclude_list;
804 static void send_directory(int f, struct file_list *flist, char *dir)
808 char fname[MAXPATHLEN];
815 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
819 strlcpy(fname, dir, MAXPATHLEN);
821 if (fname[l - 1] != '/') {
822 if (l == MAXPATHLEN - 1) {
825 "skipping long-named directory %s\n",
830 strlcat(fname, "/", MAXPATHLEN);
833 p = fname + strlen(fname);
835 local_exclude_list = NULL;
838 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
839 strcpy(p, ".cvsignore");
841 make_exclude_list(fname, NULL, 0, 0);
845 "cannot cvs-exclude in long-named directory %s\n",
850 for (di = readdir(d); di; di = readdir(d)) {
851 char *dname = d_name(di);
852 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
854 strlcpy(p, dname, MAXPATHLEN - l);
855 send_file_name(f, flist, fname, recurse, 0);
858 if (local_exclude_list) {
859 add_exclude_list("!", &local_exclude_list, 0);
868 * I *think* f==-1 means that the list should just be built in memory
869 * and not transmitted. But who can tell? -- mbp
871 struct file_list *send_file_list(int f, int argc, char *argv[])
875 char *p, *dir, *olddir;
876 char lastpath[MAXPATHLEN] = "";
877 struct file_list *flist;
880 if (show_filelist_p() && f != -1)
881 start_filelist_progress("building file list");
883 start_write = stats.total_written;
888 io_start_buffering(f);
891 for (i = 0; i < argc; i++) {
892 char *fname = topsrcname;
894 strlcpy(fname, argv[i], MAXPATHLEN);
897 if (l != 1 && fname[l - 1] == '/') {
898 if ((l == 2) && (fname[0] == '.')) {
899 /* Turn ./ into just . rather than ./.
900 This was put in to avoid a problem with
901 rsync -aR --delete from ./
902 The send_file_name() below of ./ was
903 mysteriously preventing deletes */
906 strlcat(fname, ".", MAXPATHLEN);
910 if (link_stat(fname, &st) != 0) {
913 rprintf(FERROR, "link_stat %s : %s\n",
914 fname, strerror(errno));
919 if (S_ISDIR(st.st_mode) && !recurse) {
920 rprintf(FINFO, "skipping directory %s\n", fname);
927 if (!relative_paths) {
928 p = strrchr(fname, '/');
937 } else if (f != -1 && (p = strrchr(fname, '/'))) {
938 /* this ensures we send the intermediate directories,
939 thus getting their permissions right */
941 if (strcmp(lastpath, fname)) {
942 strlcpy(lastpath, fname, sizeof(lastpath));
944 for (p = fname + 1; (p = strchr(p, '/'));
946 int copy_links_saved = copy_links;
947 int recurse_saved = recurse;
949 copy_links = copy_unsafe_links;
950 /* set recurse to 1 to prevent make_file
951 from ignoring directory, but still
952 turn off the recursive parameter to
955 send_file_name(f, flist, fname, 0,
957 copy_links = copy_links_saved;
958 recurse = recurse_saved;
970 olddir = push_dir(dir, 1);
974 rprintf(FERROR, "push_dir %s : %s\n",
975 dir, strerror(errno));
983 set_filesystem(fname);
985 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
987 if (olddir != NULL) {
989 if (pop_dir(olddir) != 0) {
990 rprintf(FERROR, "pop_dir %s : %s\n",
991 dir, strerror(errno));
992 exit_cleanup(RERR_FILESELECT);
997 topsrcname[0] = '\0';
1000 send_file_entry(NULL, f, 0);
1003 if (show_filelist_p() && f != -1) {
1004 finish_filelist_progress(flist);
1007 clean_flist(flist, 0);
1009 /* now send the uid/gid list. This was introduced in protocol
1011 if (f != -1 && remote_version >= 15) {
1015 /* if protocol version is >= 17 then send the io_error flag */
1016 if (f != -1 && remote_version >= 17) {
1017 extern int module_id;
1018 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1023 stats.flist_size = stats.total_written - start_write;
1024 stats.num_files = flist->count;
1025 if (write_batch) /* dw */
1026 write_batch_flist_info(flist->count, flist->files);
1030 rprintf(FINFO, "send_file_list done\n");
1036 struct file_list *recv_file_list(int f)
1038 struct file_list *flist;
1039 unsigned char flags;
1041 extern int list_only;
1043 if (show_filelist_p())
1044 start_filelist_progress("receiving file list");
1046 start_read = stats.total_read;
1048 flist = (struct file_list *) malloc(sizeof(flist[0]));
1053 flist->malloced = 1000;
1055 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1061 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1062 int i = flist->count;
1064 flist_expand(flist);
1066 receive_file_entry(&flist->files[i], flags, f);
1068 if (S_ISREG(flist->files[i]->mode))
1069 stats.total_size += flist->files[i]->length;
1073 maybe_emit_filelist_progress(flist);
1076 rprintf(FINFO, "recv_file_name(%s)\n",
1077 f_name(flist->files[i]));
1082 rprintf(FINFO, "received %d names\n", flist->count);
1084 clean_flist(flist, relative_paths);
1086 if (show_filelist_p()) {
1087 finish_filelist_progress(flist);
1090 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1091 if (f != -1 && remote_version >= 15) {
1092 recv_uid_list(f, flist);
1095 /* if protocol version is >= 17 then recv the io_error flag */
1096 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1097 extern int module_id;
1098 extern int ignore_errors;
1099 if (lp_ignore_errors(module_id) || ignore_errors) {
1102 io_error |= read_int(f);
1108 for (i = 0; i < flist->count; i++) {
1109 list_file_entry(flist->files[i]);
1115 rprintf(FINFO, "recv_file_list done\n");
1117 stats.flist_size = stats.total_read - start_read;
1118 stats.num_files = flist->count;
1123 out_of_memory("recv_file_list");
1124 return NULL; /* not reached */
1129 * XXX: This is currently the hottest function while building the file
1130 * list, because building f_name()s every time is expensive.
1132 int file_compare(struct file_struct **f1, struct file_struct **f2)
1134 if (!(*f1)->basename && !(*f2)->basename)
1136 if (!(*f1)->basename)
1138 if (!(*f2)->basename)
1140 if ((*f1)->dirname == (*f2)->dirname)
1141 return u_strcmp((*f1)->basename, (*f2)->basename);
1142 return u_strcmp(f_name(*f1), f_name(*f2));
1146 int flist_find(struct file_list *flist, struct file_struct *f)
1148 int low = 0, high = flist->count - 1;
1150 if (flist->count <= 0)
1153 while (low != high) {
1154 int mid = (low + high) / 2;
1156 file_compare(&flist->files[flist_up(flist, mid)], &f);
1158 return flist_up(flist, mid);
1166 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1167 return flist_up(flist, low);
1175 void free_file(struct file_struct *file)
1180 free(file->basename);
1190 * allocate a new file list
1192 struct file_list *flist_new()
1194 struct file_list *flist;
1196 flist = (struct file_list *) malloc(sizeof(flist[0]));
1198 out_of_memory("send_file_list");
1201 flist->malloced = 0;
1202 flist->files = NULL;
1205 flist->string_area = string_area_new(0);
1207 flist->string_area = NULL;
1213 * free up all elements in a flist
1215 void flist_free(struct file_list *flist)
1218 for (i = 1; i < flist->count; i++) {
1219 if (!flist->string_area)
1220 free_file(flist->files[i]);
1221 free(flist->files[i]);
1223 /* FIXME: I don't think we generally need to blank the flist
1224 * since it's about to be freed. This will just cause more
1225 * memory traffic. If you want a freed-memory debugger, you
1226 * know where to get it. */
1227 memset((char *) flist->files, 0,
1228 sizeof(flist->files[0]) * flist->count);
1230 if (flist->string_area)
1231 string_area_free(flist->string_area);
1232 memset((char *) flist, 0, sizeof(*flist));
1238 * This routine ensures we don't have any duplicate names in our file list.
1239 * duplicate names can cause corruption because of the pipelining
1241 static void clean_flist(struct file_list *flist, int strip_root)
1245 if (!flist || flist->count == 0)
1248 qsort(flist->files, flist->count,
1249 sizeof(flist->files[0]), (int (*)()) file_compare);
1251 for (i = 1; i < flist->count; i++) {
1252 if (flist->files[i]->basename &&
1253 flist->files[i - 1]->basename &&
1254 strcmp(f_name(flist->files[i]),
1255 f_name(flist->files[i - 1])) == 0) {
1256 if (verbose > 1 && !am_server)
1258 "removing duplicate name %s from file list %d\n",
1259 f_name(flist->files[i - 1]),
1261 /* it's not great that the flist knows the semantics of the
1262 * file memory usage, but i'd rather not add a flag byte
1263 * to that struct. XXX can i use a bit in the flags field? */
1264 if (flist->string_area)
1265 flist->files[i][0] = null_file;
1267 free_file(flist->files[i]);
1271 /* FIXME: There is a bug here when filenames are repeated more
1272 * than once, because we don't handle freed files when doing
1273 * the comparison. */
1276 /* we need to strip off the root directory in the case
1277 of relative paths, but this must be done _after_
1278 the sorting phase */
1279 for (i = 0; i < flist->count; i++) {
1280 if (flist->files[i]->dirname &&
1281 flist->files[i]->dirname[0] == '/') {
1282 memmove(&flist->files[i]->dirname[0],
1283 &flist->files[i]->dirname[1],
1284 strlen(flist->files[i]->dirname));
1287 if (flist->files[i]->dirname &&
1288 !flist->files[i]->dirname[0]) {
1289 flist->files[i]->dirname = NULL;
1298 for (i = 0; i < flist->count; i++) {
1299 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1301 NS(flist->files[i]->dirname),
1302 NS(flist->files[i]->basename),
1303 (int) flist->files[i]->mode,
1304 (double) flist->files[i]->length);
1310 * return the full filename of a flist entry
1312 * This function is too expensive at the moment, because it copies
1313 * strings when often we only want to compare them. In any case,
1314 * using strlcat is silly because it will walk the string repeatedly.
1316 char *f_name(struct file_struct *f)
1318 static char names[10][MAXPATHLEN];
1322 if (!f || !f->basename)
1330 off = strlcpy(p, f->dirname, MAXPATHLEN);
1331 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1332 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1334 strlcpy(p, f->basename, MAXPATHLEN);