A few more tests.
[rsync.git] / flist.c
blobf6ae224097cf7b5cc3652ee42d71bf210abafeb3
1 /*
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.
21 /** @file flist.c
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
30 **/
32 #include "rsync.h"
34 extern struct stats stats;
36 extern int verbose;
37 extern int do_progress;
38 extern int am_server;
39 extern int always_checksum;
41 extern int cvs_exclude;
43 extern int recurse;
44 extern char *files_from;
45 extern int filesfrom_fd;
47 extern int one_file_system;
48 extern int make_backups;
49 extern int preserve_links;
50 extern int preserve_hard_links;
51 extern int preserve_perms;
52 extern int preserve_devices;
53 extern int preserve_uid;
54 extern int preserve_gid;
55 extern int preserve_times;
56 extern int relative_paths;
57 extern int implied_dirs;
58 extern int copy_links;
59 extern int copy_unsafe_links;
60 extern int remote_version;
61 extern int io_error;
62 extern int sanitize_paths;
64 extern int read_batch;
65 extern int write_batch;
67 extern struct exclude_struct **exclude_list;
68 extern struct exclude_struct **server_exclude_list;
69 extern struct exclude_struct **local_exclude_list;
71 static struct file_struct null_file;
73 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
76 static int show_filelist_p(void)
78 return verbose && (recurse || files_from) && !am_server;
81 static void start_filelist_progress(char *kind)
83 rprintf(FINFO, "%s ... ", kind);
84 if ((verbose > 1) || do_progress)
85 rprintf(FINFO, "\n");
86 rflush(FINFO);
90 static void emit_filelist_progress(const struct file_list *flist)
92 rprintf(FINFO, " %d files...\r", flist->count);
96 static void maybe_emit_filelist_progress(const struct file_list *flist)
98 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
99 emit_filelist_progress(flist);
103 static void finish_filelist_progress(const struct file_list *flist)
105 if (do_progress) {
106 /* This overwrites the progress line */
107 rprintf(FINFO, "%d file%sto consider\n",
108 flist->count, flist->count == 1 ? " " : "s ");
109 } else {
110 rprintf(FINFO, "done\n");
114 void show_flist_stats(void)
116 /* Nothing yet */
120 static struct string_area *string_area_new(int size)
122 struct string_area *a;
124 if (size <= 0)
125 size = ARENA_SIZE;
126 a = malloc(sizeof(*a));
127 if (!a)
128 out_of_memory("string_area_new");
129 a->current = a->base = malloc(size);
130 if (!a->current)
131 out_of_memory("string_area_new buffer");
132 a->end = a->base + size;
133 a->next = NULL;
135 return a;
138 static void string_area_free(struct string_area *a)
140 struct string_area *next;
142 for (; a; a = next) {
143 next = a->next;
144 free(a->base);
148 static char *string_area_malloc(struct string_area **ap, int size)
150 char *p;
151 struct string_area *a;
153 /* does the request fit into the current space? */
154 a = *ap;
155 if (a->current + size >= a->end) {
156 /* no; get space, move new string_area to front of the list */
157 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
158 a->next = *ap;
159 *ap = a;
162 /* have space; do the "allocation." */
163 p = a->current;
164 a->current += size;
165 return p;
168 static char *string_area_strdup(struct string_area **ap, const char *src)
170 char *dest = string_area_malloc(ap, strlen(src) + 1);
171 return strcpy(dest, src);
174 static void list_file_entry(struct file_struct *f)
176 char perms[11];
178 if (!f->basename)
179 /* this can happen if duplicate names were removed */
180 return;
182 permstring(perms, f->mode);
184 if (preserve_links && S_ISLNK(f->mode)) {
185 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
186 perms,
187 (double) f->length, timestring(f->modtime),
188 f_name(f), f->link);
189 } else {
190 rprintf(FINFO, "%s %11.0f %s %s\n",
191 perms,
192 (double) f->length, timestring(f->modtime),
193 f_name(f));
199 * Stat either a symlink or its referent, depending on the settings of
200 * copy_links, copy_unsafe_links, etc.
202 * @retval -1 on error
204 * @retval 0 for success
206 * @post If @p path is a symlink, then @p linkbuf (of size @c
207 * MAXPATHLEN) contains the symlink target.
209 * @post @p buffer contains information about the link or the
210 * referrent as appropriate, if they exist.
212 int readlink_stat(const char *path, STRUCT_STAT * buffer, char *linkbuf)
214 #if SUPPORT_LINKS
215 if (copy_links) {
216 return do_stat(path, buffer);
218 if (do_lstat(path, buffer) == -1) {
219 return -1;
221 if (S_ISLNK(buffer->st_mode)) {
222 int l;
223 l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
224 if (l == -1)
225 return -1;
226 linkbuf[l] = 0;
227 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
228 if (verbose > 1) {
229 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
230 path, linkbuf);
232 return do_stat(path, buffer);
235 return 0;
236 #else
237 return do_stat(path, buffer);
238 #endif
241 int link_stat(const char *path, STRUCT_STAT * buffer)
243 #if SUPPORT_LINKS
244 if (copy_links) {
245 return do_stat(path, buffer);
246 } else {
247 return do_lstat(path, buffer);
249 #else
250 return do_stat(path, buffer);
251 #endif
255 * This function is used to check if a file should be included/excluded
256 * from the list of files based on its name and type etc. The value of
257 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
259 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
261 #if 0 /* This currently never happens, so avoid a useless compare. */
262 if (exclude_level == NO_EXCLUDES)
263 return 0;
264 #endif
265 if (fname && fname[0] == '.' && !fname[1]) {
266 /* never exclude '.', even if somebody does --exclude '*' */
267 return 0;
269 if (server_exclude_list &&
270 check_exclude(server_exclude_list, fname, is_dir))
271 return 1;
272 if (exclude_level != ALL_EXCLUDES)
273 return 0;
274 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
275 return 1;
276 if (local_exclude_list &&
277 check_exclude(local_exclude_list, fname, is_dir))
278 return 1;
279 return 0;
282 /* used by the one_file_system code */
283 static dev_t filesystem_dev;
285 static void set_filesystem(char *fname)
287 STRUCT_STAT st;
288 if (link_stat(fname, &st) != 0)
289 return;
290 filesystem_dev = st.st_dev;
294 static int to_wire_mode(mode_t mode)
296 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
297 return (mode & ~(_S_IFMT)) | 0120000;
299 return (int) mode;
302 static mode_t from_wire_mode(int mode)
304 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
305 return (mode & ~(_S_IFMT)) | _S_IFLNK;
307 return (mode_t) mode;
311 static void send_directory(int f, struct file_list *flist, char *dir);
313 static char *flist_dir;
317 * Make sure @p flist is big enough to hold at least @p flist->count
318 * entries.
320 static void flist_expand(struct file_list *flist)
322 if (flist->count >= flist->malloced) {
323 size_t new_bytes;
324 void *new_ptr;
326 if (flist->malloced < 1000)
327 flist->malloced += 1000;
328 else
329 flist->malloced *= 2;
331 new_bytes = sizeof(flist->files[0]) * flist->malloced;
333 if (flist->files)
334 new_ptr = realloc(flist->files, new_bytes);
335 else
336 new_ptr = malloc(new_bytes);
338 if (verbose >= 2) {
339 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
340 (double) new_bytes,
341 (new_ptr == flist->files) ? " not" : "");
344 flist->files = (struct file_struct **) new_ptr;
346 if (!flist->files)
347 out_of_memory("flist_expand");
352 static void send_file_entry(struct file_struct *file, int f,
353 unsigned base_flags)
355 unsigned char flags;
356 static time_t last_time;
357 static mode_t last_mode;
358 static DEV64_T last_rdev;
359 static uid_t last_uid;
360 static gid_t last_gid;
361 static char lastname[MAXPATHLEN];
362 char *fname;
363 int l1, l2;
365 if (f == -1)
366 return;
368 if (!file) {
369 write_byte(f, 0);
370 return;
373 io_write_phase = "send_file_entry";
375 fname = f_name(file);
377 flags = base_flags;
379 if (file->mode == last_mode)
380 flags |= SAME_MODE;
381 if (file->rdev == last_rdev)
382 flags |= SAME_RDEV;
383 if (file->uid == last_uid)
384 flags |= SAME_UID;
385 if (file->gid == last_gid)
386 flags |= SAME_GID;
387 if (file->modtime == last_time)
388 flags |= SAME_TIME;
390 for (l1 = 0;
391 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
392 l1++) {}
393 l2 = strlen(fname) - l1;
395 if (l1 > 0)
396 flags |= SAME_NAME;
397 if (l2 > 255)
398 flags |= LONG_NAME;
400 /* we must make sure we don't send a zero flags byte or the other
401 end will terminate the flist transfer */
402 if (flags == 0 && !S_ISDIR(file->mode))
403 flags |= FLAG_DELETE;
404 if (flags == 0)
405 flags |= LONG_NAME;
407 write_byte(f, flags);
408 if (flags & SAME_NAME)
409 write_byte(f, l1);
410 if (flags & LONG_NAME)
411 write_int(f, l2);
412 else
413 write_byte(f, l2);
414 write_buf(f, fname + l1, l2);
416 write_longint(f, file->length);
417 if (!(flags & SAME_TIME))
418 write_int(f, (int) file->modtime);
419 if (!(flags & SAME_MODE))
420 write_int(f, to_wire_mode(file->mode));
421 if (preserve_uid && !(flags & SAME_UID)) {
422 add_uid(file->uid);
423 write_int(f, (int) file->uid);
425 if (preserve_gid && !(flags & SAME_GID)) {
426 add_gid(file->gid);
427 write_int(f, (int) file->gid);
429 if (preserve_devices && IS_DEVICE(file->mode)
430 && !(flags & SAME_RDEV))
431 write_int(f, (int) file->rdev);
433 #if SUPPORT_LINKS
434 if (preserve_links && S_ISLNK(file->mode)) {
435 write_int(f, strlen(file->link));
436 write_buf(f, file->link, strlen(file->link));
438 #endif
440 #if SUPPORT_HARD_LINKS
441 if (preserve_hard_links && S_ISREG(file->mode)) {
442 if (remote_version < 26) {
443 /* 32-bit dev_t and ino_t */
444 write_int(f, (int) file->dev);
445 write_int(f, (int) file->inode);
446 } else {
447 /* 64-bit dev_t and ino_t */
448 write_longint(f, file->dev);
449 write_longint(f, file->inode);
452 #endif
454 if (always_checksum) {
455 if (remote_version < 21) {
456 write_buf(f, file->sum, 2);
457 } else {
458 write_buf(f, file->sum, MD4_SUM_LENGTH);
462 last_mode = file->mode;
463 last_rdev = file->rdev;
464 last_uid = file->uid;
465 last_gid = file->gid;
466 last_time = file->modtime;
468 strlcpy(lastname, fname, MAXPATHLEN);
469 lastname[MAXPATHLEN - 1] = 0;
471 io_write_phase = "unknown";
476 static void receive_file_entry(struct file_struct **fptr,
477 unsigned flags, int f)
479 static time_t last_time;
480 static mode_t last_mode;
481 static DEV64_T last_rdev;
482 static uid_t last_uid;
483 static gid_t last_gid;
484 static char lastname[MAXPATHLEN];
485 char thisname[MAXPATHLEN];
486 unsigned int l1 = 0, l2 = 0;
487 char *p;
488 struct file_struct *file;
490 if (flags & SAME_NAME)
491 l1 = read_byte(f);
493 if (flags & LONG_NAME)
494 l2 = read_int(f);
495 else
496 l2 = read_byte(f);
498 file = (struct file_struct *) malloc(sizeof(*file));
499 if (!file)
500 out_of_memory("receive_file_entry");
501 memset((char *) file, 0, sizeof(*file));
502 (*fptr) = file;
504 if (l2 >= MAXPATHLEN - l1) {
505 rprintf(FERROR,
506 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
507 flags, l1, l2, lastname);
508 overflow("receive_file_entry");
511 strlcpy(thisname, lastname, l1 + 1);
512 read_sbuf(f, &thisname[l1], l2);
513 thisname[l1 + l2] = 0;
515 strlcpy(lastname, thisname, MAXPATHLEN);
516 lastname[MAXPATHLEN - 1] = 0;
518 clean_fname(thisname);
520 if (sanitize_paths) {
521 sanitize_path(thisname, NULL);
524 if ((p = strrchr(thisname, '/'))) {
525 static char *lastdir;
526 *p = 0;
527 if (lastdir && strcmp(thisname, lastdir) == 0) {
528 file->dirname = lastdir;
529 } else {
530 file->dirname = strdup(thisname);
531 lastdir = file->dirname;
533 file->basename = strdup(p + 1);
534 } else {
535 file->dirname = NULL;
536 file->basename = strdup(thisname);
539 if (!file->basename)
540 out_of_memory("receive_file_entry 1");
543 file->flags = flags;
544 file->length = read_longint(f);
545 file->modtime =
546 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
547 file->mode =
548 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
549 if (preserve_uid)
550 file->uid =
551 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
552 if (preserve_gid)
553 file->gid =
554 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
555 if (preserve_devices && IS_DEVICE(file->mode))
556 file->rdev =
557 (flags & SAME_RDEV) ? last_rdev : (DEV64_T) read_int(f);
559 if (preserve_links && S_ISLNK(file->mode)) {
560 int l = read_int(f);
561 if (l < 0) {
562 rprintf(FERROR, "overflow: l=%d\n", l);
563 overflow("receive_file_entry");
565 file->link = (char *) malloc(l + 1);
566 if (!file->link)
567 out_of_memory("receive_file_entry 2");
568 read_sbuf(f, file->link, l);
569 if (sanitize_paths) {
570 sanitize_path(file->link, file->dirname);
573 #if SUPPORT_HARD_LINKS
574 if (preserve_hard_links && S_ISREG(file->mode)) {
575 if (remote_version < 26) {
576 file->dev = read_int(f);
577 file->inode = read_int(f);
578 } else {
579 file->dev = read_longint(f);
580 file->inode = read_longint(f);
583 #endif
585 if (always_checksum) {
586 file->sum = (char *) malloc(MD4_SUM_LENGTH);
587 if (!file->sum)
588 out_of_memory("md4 sum");
589 if (remote_version < 21) {
590 read_buf(f, file->sum, 2);
591 } else {
592 read_buf(f, file->sum, MD4_SUM_LENGTH);
596 last_mode = file->mode;
597 last_rdev = file->rdev;
598 last_uid = file->uid;
599 last_gid = file->gid;
600 last_time = file->modtime;
602 if (!preserve_perms) {
603 extern int orig_umask;
604 /* set an appropriate set of permissions based on original
605 permissions and umask. This emulates what GNU cp does */
606 file->mode &= ~orig_umask;
611 /* determine if a file in a different filesstem should be skipped
612 when one_file_system is set. We bascally only want to include
613 the mount points - but they can be hard to find! */
614 static int skip_filesystem(char *fname, STRUCT_STAT * st)
616 STRUCT_STAT st2;
617 char *p = strrchr(fname, '/');
619 /* skip all but directories */
620 if (!S_ISDIR(st->st_mode))
621 return 1;
623 /* if its not a subdirectory then allow */
624 if (!p)
625 return 0;
627 *p = 0;
628 if (link_stat(fname, &st2)) {
629 *p = '/';
630 return 0;
632 *p = '/';
634 return (st2.st_dev != filesystem_dev);
637 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
638 /* IRIX cc cares that the operands to the ternary have the same type. */
639 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
642 * Create a file_struct for a named file by reading its stat()
643 * information and performing extensive checks against global
644 * options.
646 * @return the new file, or NULL if there was an error or this file
647 * should be excluded.
649 * @todo There is a small optimization opportunity here to avoid
650 * stat()ing the file in some circumstances, which has a certain cost.
651 * We are called immediately after doing readdir(), and so we may
652 * already know the d_type of the file. We could for example avoid
653 * statting directories if we're not recursing, but this is not a very
654 * important case. Some systems may not have d_type.
656 struct file_struct *make_file(char *fname, struct string_area **ap,
657 int exclude_level)
659 struct file_struct *file;
660 STRUCT_STAT st;
661 char sum[SUM_LENGTH];
662 char *p;
663 char cleaned_name[MAXPATHLEN];
664 char linkbuf[MAXPATHLEN];
665 extern int module_id;
667 strlcpy(cleaned_name, fname, MAXPATHLEN);
668 cleaned_name[MAXPATHLEN - 1] = 0;
669 clean_fname(cleaned_name);
670 if (sanitize_paths) {
671 sanitize_path(cleaned_name, NULL);
673 fname = cleaned_name;
675 memset(sum, 0, SUM_LENGTH);
677 if (readlink_stat(fname, &st, linkbuf) != 0) {
678 int save_errno = errno;
679 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
680 /* either symlink pointing nowhere or file that
681 * was removed during rsync run; see if excluded
682 * before reporting an error */
683 if (check_exclude_file(fname, 0, exclude_level)) {
684 /* file is excluded anyway, ignore silently */
685 return NULL;
688 io_error = 1;
689 rprintf(FERROR, "readlink %s: %s\n",
690 fname, strerror(save_errno));
691 return NULL;
694 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
695 if (exclude_level == NO_EXCLUDES)
696 goto skip_excludes;
698 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
699 rprintf(FINFO, "skipping directory %s\n", fname);
700 return NULL;
703 if (one_file_system && st.st_dev != filesystem_dev) {
704 if (skip_filesystem(fname, &st))
705 return NULL;
708 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
709 return NULL;
711 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
712 return NULL;
714 skip_excludes:
716 if (verbose > 2)
717 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
719 file = (struct file_struct *) malloc(sizeof(*file));
720 if (!file)
721 out_of_memory("make_file");
722 memset((char *) file, 0, sizeof(*file));
724 if ((p = strrchr(fname, '/'))) {
725 static char *lastdir;
726 *p = 0;
727 if (lastdir && strcmp(fname, lastdir) == 0) {
728 file->dirname = lastdir;
729 } else {
730 file->dirname = strdup(fname);
731 lastdir = file->dirname;
733 file->basename = STRDUP(ap, p + 1);
734 *p = '/';
735 } else {
736 file->dirname = NULL;
737 file->basename = STRDUP(ap, fname);
740 file->modtime = st.st_mtime;
741 file->length = st.st_size;
742 file->mode = st.st_mode;
743 file->uid = st.st_uid;
744 file->gid = st.st_gid;
745 file->dev = st.st_dev;
746 file->inode = st.st_ino;
747 #ifdef HAVE_STRUCT_STAT_ST_RDEV
748 file->rdev = st.st_rdev;
749 #endif
751 #if SUPPORT_LINKS
752 if (S_ISLNK(st.st_mode)) {
753 file->link = STRDUP(ap, linkbuf);
755 #endif
757 if (always_checksum) {
758 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
759 if (!file->sum)
760 out_of_memory("md4 sum");
761 /* drat. we have to provide a null checksum for non-regular
762 files in order to be compatible with earlier versions
763 of rsync */
764 if (S_ISREG(st.st_mode)) {
765 file_checksum(fname, file->sum, st.st_size);
766 } else {
767 memset(file->sum, 0, MD4_SUM_LENGTH);
771 if (flist_dir) {
772 static char *lastdir;
773 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
774 file->basedir = lastdir;
775 } else {
776 file->basedir = strdup(flist_dir);
777 lastdir = file->basedir;
779 } else {
780 file->basedir = NULL;
783 if (!S_ISDIR(st.st_mode))
784 stats.total_size += st.st_size;
786 return file;
791 void send_file_name(int f, struct file_list *flist, char *fname,
792 int recursive, unsigned base_flags)
794 struct file_struct *file;
795 extern int delete_excluded;
797 /* f is set to -1 when calculating deletion file list */
798 file = make_file(fname, &flist->string_area,
799 f == -1 && delete_excluded? SERVER_EXCLUDES
800 : ALL_EXCLUDES);
802 if (!file)
803 return;
805 maybe_emit_filelist_progress(flist);
807 flist_expand(flist);
809 if (write_batch) /* dw */
810 file->flags = FLAG_DELETE;
812 if (file->basename[0]) {
813 flist->files[flist->count++] = file;
814 send_file_entry(file, f, base_flags);
817 if (S_ISDIR(file->mode) && recursive) {
818 struct exclude_struct **last_exclude_list =
819 local_exclude_list;
820 send_directory(f, flist, f_name(file));
821 local_exclude_list = last_exclude_list;
822 return;
828 static void send_directory(int f, struct file_list *flist, char *dir)
830 DIR *d;
831 struct dirent *di;
832 char fname[MAXPATHLEN];
833 int l;
834 char *p;
836 d = opendir(dir);
837 if (!d) {
838 io_error = 1;
839 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
840 return;
843 strlcpy(fname, dir, MAXPATHLEN);
844 l = strlen(fname);
845 if (fname[l - 1] != '/') {
846 if (l == MAXPATHLEN - 1) {
847 io_error = 1;
848 rprintf(FERROR,
849 "skipping long-named directory %s\n",
850 fname);
851 closedir(d);
852 return;
854 strlcat(fname, "/", MAXPATHLEN);
855 l++;
857 p = fname + strlen(fname);
859 local_exclude_list = NULL;
861 if (cvs_exclude) {
862 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
863 strcpy(p, ".cvsignore");
864 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
865 } else {
866 io_error = 1;
867 rprintf(FINFO,
868 "cannot cvs-exclude in long-named directory %s\n",
869 fname);
873 for (di = readdir(d); di; di = readdir(d)) {
874 char *dname = d_name(di);
875 if (dname[0] == '.' && (dname[1] == '\0' ||
876 (dname[1] == '.' && dname[2] == '\0')))
877 continue;
878 strlcpy(p, dname, MAXPATHLEN - l);
879 send_file_name(f, flist, fname, recurse, 0);
882 if (local_exclude_list)
883 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
885 closedir(d);
890 * The delete_files() function in receiver.c sets f to -1 so that we just
891 * construct the file list in memory without sending it over the wire. It
892 * also has the side-effect of ignoring user-excludes if delete_excluded
893 * is set (so that the delete list includes user-excluded files).
895 struct file_list *send_file_list(int f, int argc, char *argv[])
897 int l;
898 STRUCT_STAT st;
899 char *p, *dir, *olddir;
900 char lastpath[MAXPATHLEN] = "";
901 struct file_list *flist;
902 int64 start_write;
903 int use_ff_fd = 0;
905 if (show_filelist_p() && f != -1)
906 start_filelist_progress("building file list");
908 start_write = stats.total_written;
910 flist = flist_new();
912 if (f != -1) {
913 io_start_buffering(f);
914 if (filesfrom_fd >= 0) {
915 if (argv[0] && !push_dir(argv[0], 0)) {
916 rprintf(FERROR, "push_dir %s : %s\n",
917 argv[0], strerror(errno));
918 exit_cleanup(RERR_FILESELECT);
920 use_ff_fd = 1;
924 while (1) {
925 char fname2[MAXPATHLEN];
926 char *fname = fname2;
928 if (use_ff_fd) {
929 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
930 break;
931 sanitize_path(fname, NULL);
932 } else {
933 if (argc-- == 0)
934 break;
935 strlcpy(fname, *argv++, MAXPATHLEN);
936 if (sanitize_paths)
937 sanitize_path(fname, NULL);
940 l = strlen(fname);
941 if (l != 1 && fname[l - 1] == '/') {
942 if ((l == 2) && (fname[0] == '.')) {
943 /* Turn ./ into just . rather than ./.
944 This was put in to avoid a problem with
945 rsync -aR --delete from ./
946 The send_file_name() below of ./ was
947 mysteriously preventing deletes */
948 fname[1] = 0;
949 } else {
950 strlcat(fname, ".", MAXPATHLEN);
954 if (link_stat(fname, &st) != 0) {
955 if (f != -1) {
956 io_error = 1;
957 rprintf(FERROR, "link_stat %s : %s\n",
958 fname, strerror(errno));
960 continue;
963 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
964 rprintf(FINFO, "skipping directory %s\n", fname);
965 continue;
968 dir = NULL;
969 olddir = NULL;
971 if (!relative_paths) {
972 p = strrchr(fname, '/');
973 if (p) {
974 *p = 0;
975 if (p == fname)
976 dir = "/";
977 else
978 dir = fname;
979 fname = p + 1;
981 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
982 /* this ensures we send the intermediate directories,
983 thus getting their permissions right */
984 char *lp = lastpath, *fn = fname, *slash = fname;
985 *p = 0;
986 /* Skip any initial directories in our path that we
987 * have in common with lastpath. */
988 while (*fn && *lp == *fn) {
989 if (*fn == '/')
990 slash = fn;
991 lp++, fn++;
993 *p = '/';
994 if (fn != p || (*lp && *lp != '/')) {
995 int copy_links_saved = copy_links;
996 int recurse_saved = recurse;
997 copy_links = copy_unsafe_links;
998 /* set recurse to 1 to prevent make_file
999 * from ignoring directory, but still
1000 * turn off the recursive parameter to
1001 * send_file_name */
1002 recurse = 1;
1003 while ((slash = strchr(slash+1, '/')) != 0) {
1004 *slash = 0;
1005 send_file_name(f, flist, fname, 0, 0);
1006 *slash = '/';
1008 copy_links = copy_links_saved;
1009 recurse = recurse_saved;
1010 *p = 0;
1011 strlcpy(lastpath, fname, sizeof lastpath);
1012 *p = '/';
1016 if (!*fname)
1017 fname = ".";
1019 if (dir && *dir) {
1020 olddir = push_dir(dir, 1);
1022 if (!olddir) {
1023 io_error = 1;
1024 rprintf(FERROR, "push_dir %s : %s\n",
1025 dir, strerror(errno));
1026 continue;
1029 flist_dir = dir;
1032 if (one_file_system)
1033 set_filesystem(fname);
1035 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1037 if (olddir != NULL) {
1038 flist_dir = NULL;
1039 if (pop_dir(olddir) != 0) {
1040 rprintf(FERROR, "pop_dir %s : %s\n",
1041 dir, strerror(errno));
1042 exit_cleanup(RERR_FILESELECT);
1047 if (f != -1) {
1048 send_file_entry(NULL, f, 0);
1051 if (show_filelist_p() && f != -1) {
1052 finish_filelist_progress(flist);
1055 clean_flist(flist, 0, 0);
1057 /* now send the uid/gid list. This was introduced in protocol
1058 version 15 */
1059 if (f != -1) {
1060 send_uid_list(f);
1063 /* send the io_error flag */
1064 if (f != -1) {
1065 extern int module_id;
1066 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1069 if (f != -1) {
1070 io_end_buffering();
1071 stats.flist_size = stats.total_written - start_write;
1072 stats.num_files = flist->count;
1073 if (write_batch) /* dw */
1074 write_batch_flist_info(flist->count, flist->files);
1077 if (verbose > 2)
1078 rprintf(FINFO, "send_file_list done\n");
1080 return flist;
1084 struct file_list *recv_file_list(int f)
1086 struct file_list *flist;
1087 unsigned char flags;
1088 int64 start_read;
1089 extern int list_only;
1091 if (show_filelist_p())
1092 start_filelist_progress("receiving file list");
1094 start_read = stats.total_read;
1096 flist = (struct file_list *) malloc(sizeof(flist[0]));
1097 if (!flist)
1098 goto oom;
1100 flist->count = 0;
1101 flist->malloced = 1000;
1102 flist->files =
1103 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1104 flist->malloced);
1105 if (!flist->files)
1106 goto oom;
1109 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1110 int i = flist->count;
1112 flist_expand(flist);
1114 receive_file_entry(&flist->files[i], flags, f);
1116 if (S_ISREG(flist->files[i]->mode))
1117 stats.total_size += flist->files[i]->length;
1119 flist->count++;
1121 maybe_emit_filelist_progress(flist);
1123 if (verbose > 2)
1124 rprintf(FINFO, "recv_file_name(%s)\n",
1125 f_name(flist->files[i]));
1129 if (verbose > 2)
1130 rprintf(FINFO, "received %d names\n", flist->count);
1132 clean_flist(flist, relative_paths, 1);
1134 if (show_filelist_p()) {
1135 finish_filelist_progress(flist);
1138 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1139 if (f != -1) {
1140 recv_uid_list(f, flist);
1143 /* recv the io_error flag */
1144 if (f != -1 && !read_batch) { /* dw-added readbatch */
1145 extern int module_id;
1146 extern int ignore_errors;
1147 if (lp_ignore_errors(module_id) || ignore_errors) {
1148 read_int(f);
1149 } else {
1150 io_error |= read_int(f);
1154 if (list_only) {
1155 int i;
1156 for (i = 0; i < flist->count; i++) {
1157 list_file_entry(flist->files[i]);
1162 if (verbose > 2)
1163 rprintf(FINFO, "recv_file_list done\n");
1165 stats.flist_size = stats.total_read - start_read;
1166 stats.num_files = flist->count;
1168 return flist;
1170 oom:
1171 out_of_memory("recv_file_list");
1172 return NULL; /* not reached */
1177 * XXX: This is currently the hottest function while building the file
1178 * list, because building f_name()s every time is expensive.
1180 int file_compare(struct file_struct **f1, struct file_struct **f2)
1182 if (!(*f1)->basename && !(*f2)->basename)
1183 return 0;
1184 if (!(*f1)->basename)
1185 return -1;
1186 if (!(*f2)->basename)
1187 return 1;
1188 if ((*f1)->dirname == (*f2)->dirname)
1189 return u_strcmp((*f1)->basename, (*f2)->basename);
1190 return u_strcmp(f_name(*f1), f_name(*f2));
1194 int flist_find(struct file_list *flist, struct file_struct *f)
1196 int low = 0, high = flist->count - 1;
1198 while (high >= 0 && !flist->files[high]->basename) high--;
1200 if (high < 0)
1201 return -1;
1203 while (low != high) {
1204 int mid = (low + high) / 2;
1205 int ret =
1206 file_compare(&flist->files[flist_up(flist, mid)], &f);
1207 if (ret == 0)
1208 return flist_up(flist, mid);
1209 if (ret > 0) {
1210 high = mid;
1211 } else {
1212 low = mid + 1;
1216 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1217 return flist_up(flist, low);
1218 return -1;
1223 * free up one file
1225 void free_file(struct file_struct *file)
1227 if (!file)
1228 return;
1229 if (file->basename)
1230 free(file->basename);
1231 if (file->link)
1232 free(file->link);
1233 if (file->sum)
1234 free(file->sum);
1235 *file = null_file;
1240 * allocate a new file list
1242 struct file_list *flist_new(void)
1244 struct file_list *flist;
1246 flist = (struct file_list *) malloc(sizeof(flist[0]));
1247 if (!flist)
1248 out_of_memory("send_file_list");
1250 flist->count = 0;
1251 flist->malloced = 0;
1252 flist->files = NULL;
1254 #if ARENA_SIZE > 0
1255 flist->string_area = string_area_new(0);
1256 #else
1257 flist->string_area = NULL;
1258 #endif
1259 return flist;
1263 * free up all elements in a flist
1265 void flist_free(struct file_list *flist)
1267 int i;
1268 for (i = 1; i < flist->count; i++) {
1269 if (!flist->string_area)
1270 free_file(flist->files[i]);
1271 free(flist->files[i]);
1273 /* FIXME: I don't think we generally need to blank the flist
1274 * since it's about to be freed. This will just cause more
1275 * memory traffic. If you want a freed-memory debugger, you
1276 * know where to get it. */
1277 memset((char *) flist->files, 0,
1278 sizeof(flist->files[0]) * flist->count);
1279 free(flist->files);
1280 if (flist->string_area)
1281 string_area_free(flist->string_area);
1282 memset((char *) flist, 0, sizeof(*flist));
1283 free(flist);
1288 * This routine ensures we don't have any duplicate names in our file list.
1289 * duplicate names can cause corruption because of the pipelining
1291 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1293 int i;
1294 char *name, *prev_name = NULL;
1296 if (!flist || flist->count == 0)
1297 return;
1299 qsort(flist->files, flist->count,
1300 sizeof(flist->files[0]), (int (*)()) file_compare);
1302 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1303 if (flist->files[i]->basename) {
1304 prev_name = f_name(flist->files[i]);
1305 break;
1308 while (++i < flist->count) {
1309 if (!flist->files[i]->basename)
1310 continue;
1311 name = f_name(flist->files[i]);
1312 if (strcmp(name, prev_name) == 0) {
1313 if (verbose > 1 && !am_server) {
1314 rprintf(FINFO,
1315 "removing duplicate name %s from file list %d\n",
1316 name, i);
1318 /* it's not great that the flist knows the semantics of
1319 * the file memory usage, but i'd rather not add a flag
1320 * byte to that struct.
1321 * XXX can i use a bit in the flags field? */
1322 if (flist->string_area)
1323 flist->files[i][0] = null_file;
1324 else
1325 free_file(flist->files[i]);
1327 prev_name = name;
1330 if (strip_root) {
1331 /* we need to strip off the root directory in the case
1332 of relative paths, but this must be done _after_
1333 the sorting phase */
1334 for (i = 0; i < flist->count; i++) {
1335 if (flist->files[i]->dirname &&
1336 flist->files[i]->dirname[0] == '/') {
1337 memmove(&flist->files[i]->dirname[0],
1338 &flist->files[i]->dirname[1],
1339 strlen(flist->files[i]->dirname));
1342 if (flist->files[i]->dirname &&
1343 !flist->files[i]->dirname[0]) {
1344 flist->files[i]->dirname = NULL;
1349 if (verbose <= 3)
1350 return;
1352 for (i = 0; i < flist->count; i++) {
1353 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1354 (int) getpid(), i,
1355 NS(flist->files[i]->dirname),
1356 NS(flist->files[i]->basename),
1357 (int) flist->files[i]->mode,
1358 (double) flist->files[i]->length);
1364 * return the full filename of a flist entry
1366 * This function is too expensive at the moment, because it copies
1367 * strings when often we only want to compare them. In any case,
1368 * using strlcat is silly because it will walk the string repeatedly.
1370 char *f_name(struct file_struct *f)
1372 static char names[10][MAXPATHLEN];
1373 static int n;
1374 char *p = names[n];
1376 if (!f || !f->basename)
1377 return NULL;
1379 n = (n + 1) % 10;
1381 if (f->dirname) {
1382 int off;
1384 off = strlcpy(p, f->dirname, MAXPATHLEN);
1385 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1386 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1387 } else {
1388 strlcpy(p, f->basename, MAXPATHLEN);
1391 return p;