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 * @return -1 on error; or 0. If a symlink, then @p Linkbuf (of size
200 * MAXPATHLEN) contains the symlink target.
202 int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
206 return do_stat(Path, Buffer);
208 if (do_lstat(Path, Buffer) == -1) {
211 if (S_ISLNK(Buffer->st_mode)) {
214 readlink((char *) Path, Linkbuf,
215 MAXPATHLEN - 1)) == -1) {
219 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
220 unsafe_symlink(Linkbuf, topsrcname)) {
221 return do_stat(Path, Buffer);
226 return do_stat(Path, Buffer);
230 int link_stat(const char *Path, STRUCT_STAT * Buffer)
234 return do_stat(Path, Buffer);
236 return do_lstat(Path, Buffer);
239 return do_stat(Path, Buffer);
244 This function is used to check if a file should be included/excluded
245 from the list of files based on its name and type etc
247 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
249 extern int delete_excluded;
251 /* f is set to -1 when calculating deletion file list */
252 if ((f == -1) && delete_excluded) {
255 if (check_exclude(fname, local_exclude_list, st)) {
261 /* used by the one_file_system code */
262 static dev_t filesystem_dev;
264 static void set_filesystem(char *fname)
267 if (link_stat(fname, &st) != 0)
269 filesystem_dev = st.st_dev;
273 static int to_wire_mode(mode_t mode)
275 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
276 return (mode & ~(_S_IFMT)) | 0120000;
281 static mode_t from_wire_mode(int mode)
283 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
284 return (mode & ~(_S_IFMT)) | _S_IFLNK;
286 return (mode_t) mode;
290 static void send_directory(int f, struct file_list *flist, char *dir);
292 static char *flist_dir;
296 * Make sure @p flist is big enough to hold at least @p flist->count
299 static void flist_expand(struct file_list *flist)
301 if (flist->count >= flist->malloced) {
305 if (flist->malloced < 1000)
306 flist->malloced += 1000;
308 flist->malloced *= 2;
310 new_bytes = sizeof(flist->files[0]) * flist->malloced;
313 new_ptr = realloc(flist->files, new_bytes);
315 new_ptr = malloc(new_bytes);
318 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
320 (new_ptr == flist->files) ? " not" : "");
323 flist->files = (struct file_struct **) new_ptr;
326 out_of_memory("flist_expand");
331 static void send_file_entry(struct file_struct *file, int f,
335 static time_t last_time;
336 static mode_t last_mode;
337 static DEV64_T last_rdev;
338 static uid_t last_uid;
339 static gid_t last_gid;
340 static char lastname[MAXPATHLEN];
352 fname = f_name(file);
356 if (file->mode == last_mode)
358 if (file->rdev == last_rdev)
360 if (file->uid == last_uid)
362 if (file->gid == last_gid)
364 if (file->modtime == last_time)
368 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
370 l2 = strlen(fname) - l1;
377 /* we must make sure we don't send a zero flags byte or the other
378 end will terminate the flist transfer */
379 if (flags == 0 && !S_ISDIR(file->mode))
380 flags |= FLAG_DELETE;
384 write_byte(f, flags);
385 if (flags & SAME_NAME)
387 if (flags & LONG_NAME)
391 write_buf(f, fname + l1, l2);
393 write_longint(f, file->length);
394 if (!(flags & SAME_TIME))
395 write_int(f, (int) file->modtime);
396 if (!(flags & SAME_MODE))
397 write_int(f, to_wire_mode(file->mode));
398 if (preserve_uid && !(flags & SAME_UID)) {
400 write_int(f, (int) file->uid);
402 if (preserve_gid && !(flags & SAME_GID)) {
404 write_int(f, (int) file->gid);
406 if (preserve_devices && IS_DEVICE(file->mode)
407 && !(flags & SAME_RDEV))
408 write_int(f, (int) file->rdev);
411 if (preserve_links && S_ISLNK(file->mode)) {
412 write_int(f, strlen(file->link));
413 write_buf(f, file->link, strlen(file->link));
417 #if SUPPORT_HARD_LINKS
418 if (preserve_hard_links && S_ISREG(file->mode)) {
419 if (remote_version < 26) {
420 /* 32-bit dev_t and ino_t */
421 write_int(f, (int) file->dev);
422 write_int(f, (int) file->inode);
424 /* 64-bit dev_t and ino_t */
425 write_longint(f, file->dev);
426 write_longint(f, file->inode);
431 if (always_checksum) {
432 if (remote_version < 21) {
433 write_buf(f, file->sum, 2);
435 write_buf(f, file->sum, MD4_SUM_LENGTH);
439 last_mode = file->mode;
440 last_rdev = file->rdev;
441 last_uid = file->uid;
442 last_gid = file->gid;
443 last_time = file->modtime;
445 strlcpy(lastname, fname, MAXPATHLEN);
446 lastname[MAXPATHLEN - 1] = 0;
451 static void receive_file_entry(struct file_struct **fptr,
452 unsigned flags, int f)
454 static time_t last_time;
455 static mode_t last_mode;
456 static DEV64_T last_rdev;
457 static uid_t last_uid;
458 static gid_t last_gid;
459 static char lastname[MAXPATHLEN];
460 char thisname[MAXPATHLEN];
461 unsigned int l1 = 0, l2 = 0;
463 struct file_struct *file;
465 if (flags & SAME_NAME)
468 if (flags & LONG_NAME)
473 file = (struct file_struct *) malloc(sizeof(*file));
475 out_of_memory("receive_file_entry");
476 memset((char *) file, 0, sizeof(*file));
479 if (l2 >= MAXPATHLEN - l1) {
481 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
482 flags, l1, l2, lastname);
483 overflow("receive_file_entry");
486 strlcpy(thisname, lastname, l1 + 1);
487 read_sbuf(f, &thisname[l1], l2);
488 thisname[l1 + l2] = 0;
490 strlcpy(lastname, thisname, MAXPATHLEN);
491 lastname[MAXPATHLEN - 1] = 0;
493 clean_fname(thisname);
495 if (sanitize_paths) {
496 sanitize_path(thisname, NULL);
499 if ((p = strrchr(thisname, '/'))) {
500 static char *lastdir;
502 if (lastdir && strcmp(thisname, lastdir) == 0) {
503 file->dirname = lastdir;
505 file->dirname = strdup(thisname);
506 lastdir = file->dirname;
508 file->basename = strdup(p + 1);
510 file->dirname = NULL;
511 file->basename = strdup(thisname);
515 out_of_memory("receive_file_entry 1");
519 file->length = read_longint(f);
521 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
523 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
526 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
529 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
530 if (preserve_devices && IS_DEVICE(file->mode))
532 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
534 if (preserve_links && S_ISLNK(file->mode)) {
537 rprintf(FERROR, "overflow: l=%d\n", l);
538 overflow("receive_file_entry");
540 file->link = (char *) malloc(l + 1);
542 out_of_memory("receive_file_entry 2");
543 read_sbuf(f, file->link, l);
544 if (sanitize_paths) {
545 sanitize_path(file->link, file->dirname);
548 #if SUPPORT_HARD_LINKS
549 if (preserve_hard_links && S_ISREG(file->mode)) {
550 if (remote_version < 26) {
551 file->dev = read_int(f);
552 file->inode = read_int(f);
554 file->dev = read_longint(f);
555 file->inode = read_longint(f);
560 if (always_checksum) {
561 file->sum = (char *) malloc(MD4_SUM_LENGTH);
563 out_of_memory("md4 sum");
564 if (remote_version < 21) {
565 read_buf(f, file->sum, 2);
567 read_buf(f, file->sum, MD4_SUM_LENGTH);
571 last_mode = file->mode;
572 last_rdev = file->rdev;
573 last_uid = file->uid;
574 last_gid = file->gid;
575 last_time = file->modtime;
577 if (!preserve_perms) {
578 extern int orig_umask;
579 /* set an appropriate set of permissions based on original
580 permissions and umask. This emulates what GNU cp does */
581 file->mode &= ~orig_umask;
586 /* determine if a file in a different filesstem should be skipped
587 when one_file_system is set. We bascally only want to include
588 the mount points - but they can be hard to find! */
589 static int skip_filesystem(char *fname, STRUCT_STAT * st)
592 char *p = strrchr(fname, '/');
594 /* skip all but directories */
595 if (!S_ISDIR(st->st_mode))
598 /* if its not a subdirectory then allow */
603 if (link_stat(fname, &st2)) {
609 return (st2.st_dev != filesystem_dev);
612 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
613 /* IRIX cc cares that the operands to the ternary have the same type. */
614 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
617 * Create a file_struct for a named file by reading its stat()
618 * information and performing extensive checks against global
621 * @return the new file, or NULL if there was an error or this file
622 * should be excluded.
624 * @todo There is a small optimization opportunity here to avoid
625 * stat()ing the file in some circumstances, which has a certain cost.
626 * We are called immediately after doing readdir(), and so we may
627 * already know the d_type of the file. We could for example avoid
628 * statting directories if we're not recursing, but this is not a very
629 * important case. Some systems may not have d_type.
631 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
634 struct file_struct *file;
636 char sum[SUM_LENGTH];
638 char cleaned_name[MAXPATHLEN];
639 char linkbuf[MAXPATHLEN];
640 extern int module_id;
642 strlcpy(cleaned_name, fname, MAXPATHLEN);
643 cleaned_name[MAXPATHLEN - 1] = 0;
644 clean_fname(cleaned_name);
645 if (sanitize_paths) {
646 sanitize_path(cleaned_name, NULL);
648 fname = cleaned_name;
650 memset(sum, 0, SUM_LENGTH);
652 if (readlink_stat(fname, &st, linkbuf) != 0) {
653 int save_errno = errno;
654 if ((errno == ENOENT) && copy_links && !noexcludes) {
655 /* symlink pointing nowhere, see if excluded */
656 memset((char *) &st, 0, sizeof(st));
657 if (check_exclude_file(f, fname, &st)) {
658 /* file is excluded anyway, ignore silently */
663 rprintf(FERROR, "readlink %s: %s\n",
664 fname, strerror(save_errno));
668 /* we use noexcludes from backup.c */
672 if (S_ISDIR(st.st_mode) && !recurse) {
673 rprintf(FINFO, "skipping directory %s\n", fname);
677 if (one_file_system && st.st_dev != filesystem_dev) {
678 if (skip_filesystem(fname, &st))
682 if (check_exclude_file(f, fname, &st))
686 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
692 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
694 file = (struct file_struct *) malloc(sizeof(*file));
696 out_of_memory("make_file");
697 memset((char *) file, 0, sizeof(*file));
699 if ((p = strrchr(fname, '/'))) {
700 static char *lastdir;
702 if (lastdir && strcmp(fname, lastdir) == 0) {
703 file->dirname = lastdir;
705 file->dirname = strdup(fname);
706 lastdir = file->dirname;
708 file->basename = STRDUP(ap, p + 1);
711 file->dirname = NULL;
712 file->basename = STRDUP(ap, fname);
715 file->modtime = st.st_mtime;
716 file->length = st.st_size;
717 file->mode = st.st_mode;
718 file->uid = st.st_uid;
719 file->gid = st.st_gid;
720 file->dev = st.st_dev;
721 file->inode = st.st_ino;
722 #ifdef HAVE_STRUCT_STAT_ST_RDEV
723 file->rdev = st.st_rdev;
727 if (S_ISLNK(st.st_mode)) {
728 file->link = STRDUP(ap, linkbuf);
732 if (always_checksum) {
733 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
735 out_of_memory("md4 sum");
736 /* drat. we have to provide a null checksum for non-regular
737 files in order to be compatible with earlier versions
739 if (S_ISREG(st.st_mode)) {
740 file_checksum(fname, file->sum, st.st_size);
742 memset(file->sum, 0, MD4_SUM_LENGTH);
747 static char *lastdir;
748 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
749 file->basedir = lastdir;
751 file->basedir = strdup(flist_dir);
752 lastdir = file->basedir;
755 file->basedir = NULL;
758 if (!S_ISDIR(st.st_mode))
759 stats.total_size += st.st_size;
766 void send_file_name(int f, struct file_list *flist, char *fname,
767 int recursive, unsigned base_flags)
769 struct file_struct *file;
771 file = make_file(f, fname, &flist->string_area, 0);
776 maybe_emit_filelist_progress(flist);
780 if (write_batch) /* dw */
781 file->flags = FLAG_DELETE;
783 if (strcmp(file->basename, "")) {
784 flist->files[flist->count++] = file;
785 send_file_entry(file, f, base_flags);
788 if (S_ISDIR(file->mode) && recursive) {
789 struct exclude_struct **last_exclude_list =
791 send_directory(f, flist, f_name(file));
792 local_exclude_list = last_exclude_list;
799 static void send_directory(int f, struct file_list *flist, char *dir)
803 char fname[MAXPATHLEN];
810 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
814 strlcpy(fname, dir, MAXPATHLEN);
816 if (fname[l - 1] != '/') {
817 if (l == MAXPATHLEN - 1) {
820 "skipping long-named directory %s\n",
825 strlcat(fname, "/", MAXPATHLEN);
828 p = fname + strlen(fname);
830 local_exclude_list = NULL;
833 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
834 strcpy(p, ".cvsignore");
836 make_exclude_list(fname, NULL, 0, 0);
840 "cannot cvs-exclude in long-named directory %s\n",
845 for (di = readdir(d); di; di = readdir(d)) {
846 char *dname = d_name(di);
847 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
849 strlcpy(p, dname, MAXPATHLEN - l);
850 send_file_name(f, flist, fname, recurse, 0);
853 if (local_exclude_list) {
854 add_exclude_list("!", &local_exclude_list, 0);
863 * I *think* f==-1 means that the list should just be built in memory
864 * and not transmitted. But who can tell? -- mbp
866 struct file_list *send_file_list(int f, int argc, char *argv[])
870 char *p, *dir, *olddir;
871 char lastpath[MAXPATHLEN] = "";
872 struct file_list *flist;
875 if (show_filelist_p() && f != -1)
876 start_filelist_progress("building file list");
878 start_write = stats.total_written;
883 io_start_buffering(f);
886 for (i = 0; i < argc; i++) {
887 char *fname = topsrcname;
889 strlcpy(fname, argv[i], MAXPATHLEN);
892 if (l != 1 && fname[l - 1] == '/') {
893 if ((l == 2) && (fname[0] == '.')) {
894 /* Turn ./ into just . rather than ./.
895 This was put in to avoid a problem with
896 rsync -aR --delete from ./
897 The send_file_name() below of ./ was
898 mysteriously preventing deletes */
901 strlcat(fname, ".", MAXPATHLEN);
905 if (link_stat(fname, &st) != 0) {
908 rprintf(FERROR, "link_stat %s : %s\n",
909 fname, strerror(errno));
914 if (S_ISDIR(st.st_mode) && !recurse) {
915 rprintf(FINFO, "skipping directory %s\n", fname);
922 if (!relative_paths) {
923 p = strrchr(fname, '/');
932 } else if (f != -1 && (p = strrchr(fname, '/'))) {
933 /* this ensures we send the intermediate directories,
934 thus getting their permissions right */
936 if (strcmp(lastpath, fname)) {
937 strlcpy(lastpath, fname, sizeof(lastpath));
939 for (p = fname + 1; (p = strchr(p, '/'));
941 int copy_links_saved = copy_links;
942 int recurse_saved = recurse;
944 copy_links = copy_unsafe_links;
945 /* set recurse to 1 to prevent make_file
946 from ignoring directory, but still
947 turn off the recursive parameter to
950 send_file_name(f, flist, fname, 0,
952 copy_links = copy_links_saved;
953 recurse = recurse_saved;
965 olddir = push_dir(dir, 1);
969 rprintf(FERROR, "push_dir %s : %s\n",
970 dir, strerror(errno));
978 set_filesystem(fname);
980 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
982 if (olddir != NULL) {
984 if (pop_dir(olddir) != 0) {
985 rprintf(FERROR, "pop_dir %s : %s\n",
986 dir, strerror(errno));
987 exit_cleanup(RERR_FILESELECT);
992 topsrcname[0] = '\0';
995 send_file_entry(NULL, f, 0);
998 if (show_filelist_p() && f != -1) {
999 finish_filelist_progress(flist);
1002 clean_flist(flist, 0);
1004 /* now send the uid/gid list. This was introduced in protocol
1006 if (f != -1 && remote_version >= 15) {
1010 /* if protocol version is >= 17 then send the io_error flag */
1011 if (f != -1 && remote_version >= 17) {
1012 extern int module_id;
1013 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1018 stats.flist_size = stats.total_written - start_write;
1019 stats.num_files = flist->count;
1020 if (write_batch) /* dw */
1021 write_batch_flist_info(flist->count, flist->files);
1025 rprintf(FINFO, "send_file_list done\n");
1031 struct file_list *recv_file_list(int f)
1033 struct file_list *flist;
1034 unsigned char flags;
1036 extern int list_only;
1038 if (show_filelist_p())
1039 start_filelist_progress("receiving file list");
1041 start_read = stats.total_read;
1043 flist = (struct file_list *) malloc(sizeof(flist[0]));
1048 flist->malloced = 1000;
1050 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1056 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1057 int i = flist->count;
1059 flist_expand(flist);
1061 receive_file_entry(&flist->files[i], flags, f);
1063 if (S_ISREG(flist->files[i]->mode))
1064 stats.total_size += flist->files[i]->length;
1068 maybe_emit_filelist_progress(flist);
1071 rprintf(FINFO, "recv_file_name(%s)\n",
1072 f_name(flist->files[i]));
1077 rprintf(FINFO, "received %d names\n", flist->count);
1079 clean_flist(flist, relative_paths);
1081 if (show_filelist_p()) {
1082 finish_filelist_progress(flist);
1085 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1086 if (f != -1 && remote_version >= 15) {
1087 recv_uid_list(f, flist);
1090 /* if protocol version is >= 17 then recv the io_error flag */
1091 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1092 extern int module_id;
1093 extern int ignore_errors;
1094 if (lp_ignore_errors(module_id) || ignore_errors) {
1097 io_error |= read_int(f);
1103 for (i = 0; i < flist->count; i++) {
1104 list_file_entry(flist->files[i]);
1110 rprintf(FINFO, "recv_file_list done\n");
1112 stats.flist_size = stats.total_read - start_read;
1113 stats.num_files = flist->count;
1118 out_of_memory("recv_file_list");
1119 return NULL; /* not reached */
1124 * XXX: This is currently the hottest function while building the file
1125 * list, because building f_name()s every time is expensive.
1127 int file_compare(struct file_struct **f1, struct file_struct **f2)
1129 if (!(*f1)->basename && !(*f2)->basename)
1131 if (!(*f1)->basename)
1133 if (!(*f2)->basename)
1135 if ((*f1)->dirname == (*f2)->dirname)
1136 return u_strcmp((*f1)->basename, (*f2)->basename);
1137 return u_strcmp(f_name(*f1), f_name(*f2));
1141 int flist_find(struct file_list *flist, struct file_struct *f)
1143 int low = 0, high = flist->count - 1;
1145 if (flist->count <= 0)
1148 while (low != high) {
1149 int mid = (low + high) / 2;
1151 file_compare(&flist->files[flist_up(flist, mid)], &f);
1153 return flist_up(flist, mid);
1161 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1162 return flist_up(flist, low);
1170 void free_file(struct file_struct *file)
1175 free(file->basename);
1185 * allocate a new file list
1187 struct file_list *flist_new()
1189 struct file_list *flist;
1191 flist = (struct file_list *) malloc(sizeof(flist[0]));
1193 out_of_memory("send_file_list");
1196 flist->malloced = 0;
1197 flist->files = NULL;
1200 flist->string_area = string_area_new(0);
1202 flist->string_area = NULL;
1208 * free up all elements in a flist
1210 void flist_free(struct file_list *flist)
1213 for (i = 1; i < flist->count; i++) {
1214 if (!flist->string_area)
1215 free_file(flist->files[i]);
1216 free(flist->files[i]);
1218 /* FIXME: I don't think we generally need to blank the flist
1219 * since it's about to be freed. This will just cause more
1220 * memory traffic. If you want a freed-memory debugger, you
1221 * know where to get it. */
1222 memset((char *) flist->files, 0,
1223 sizeof(flist->files[0]) * flist->count);
1225 if (flist->string_area)
1226 string_area_free(flist->string_area);
1227 memset((char *) flist, 0, sizeof(*flist));
1233 * This routine ensures we don't have any duplicate names in our file list.
1234 * duplicate names can cause corruption because of the pipelining
1236 static void clean_flist(struct file_list *flist, int strip_root)
1240 if (!flist || flist->count == 0)
1243 qsort(flist->files, flist->count,
1244 sizeof(flist->files[0]), (int (*)()) file_compare);
1246 for (i = 1; i < flist->count; i++) {
1247 if (flist->files[i]->basename &&
1248 flist->files[i - 1]->basename &&
1249 strcmp(f_name(flist->files[i]),
1250 f_name(flist->files[i - 1])) == 0) {
1251 if (verbose > 1 && !am_server)
1253 "removing duplicate name %s from file list %d\n",
1254 f_name(flist->files[i - 1]),
1256 /* it's not great that the flist knows the semantics of the
1257 * file memory usage, but i'd rather not add a flag byte
1258 * to that struct. XXX can i use a bit in the flags field? */
1259 if (flist->string_area)
1260 flist->files[i][0] = null_file;
1262 free_file(flist->files[i]);
1266 /* FIXME: There is a bug here when filenames are repeated more
1267 * than once, because we don't handle freed files when doing
1268 * the comparison. */
1271 /* we need to strip off the root directory in the case
1272 of relative paths, but this must be done _after_
1273 the sorting phase */
1274 for (i = 0; i < flist->count; i++) {
1275 if (flist->files[i]->dirname &&
1276 flist->files[i]->dirname[0] == '/') {
1277 memmove(&flist->files[i]->dirname[0],
1278 &flist->files[i]->dirname[1],
1279 strlen(flist->files[i]->dirname));
1282 if (flist->files[i]->dirname &&
1283 !flist->files[i]->dirname[0]) {
1284 flist->files[i]->dirname = NULL;
1293 for (i = 0; i < flist->count; i++) {
1294 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1296 NS(flist->files[i]->dirname),
1297 NS(flist->files[i]->basename),
1298 (int) flist->files[i]->mode,
1299 (double) flist->files[i]->length);
1305 * return the full filename of a flist entry
1307 * This function is too expensive at the moment, because it copies
1308 * strings when often we only want to compare them. In any case,
1309 * using strlcat is silly because it will walk the string repeatedly.
1311 char *f_name(struct file_struct *f)
1313 static char names[10][MAXPATHLEN];
1317 if (!f || !f->basename)
1325 off = strlcpy(p, f->dirname, MAXPATHLEN);
1326 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1327 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1329 strlcpy(p, f->basename, MAXPATHLEN);