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 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
30 extern struct stats stats;
33 extern int do_progress;
38 extern int delete_during;
39 extern int always_checksum;
41 extern int ignore_errors;
42 extern int numeric_ids;
46 extern char curr_dir[MAXPATHLEN];
47 extern unsigned int curr_dir_len;
48 extern char *backup_dir;
49 extern char *backup_suffix;
50 extern int filesfrom_fd;
52 extern int one_file_system;
53 extern int keep_dirlinks;
54 extern int preserve_links;
55 extern int preserve_hard_links;
56 extern int preserve_perms;
57 extern int preserve_devices;
58 extern int preserve_uid;
59 extern int preserve_gid;
60 extern int relative_paths;
61 extern int implied_dirs;
62 extern int make_backups;
63 extern int backup_suffix_len;
64 extern int copy_links;
65 extern int copy_unsafe_links;
66 extern int protocol_version;
67 extern int sanitize_paths;
68 extern int delete_excluded;
69 extern int max_delete;
70 extern int orig_umask;
73 extern struct filter_list_struct filter_list;
74 extern struct filter_list_struct server_filter_list;
78 static char empty_sum[MD4_SUM_LENGTH];
79 static unsigned int file_struct_len;
80 static struct file_list *received_flist;
81 static dev_t filesystem_dev; /* used to implement -x */
82 static int deletion_count = 0; /* used to implement --max-delete */
84 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
85 static void output_flist(struct file_list *flist, const char *whose_list);
91 /* Figure out how big the file_struct is without trailing padding */
92 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
96 static int show_filelist_p(void)
98 return verbose && xfer_dirs && !am_server;
101 static void start_filelist_progress(char *kind)
103 rprintf(FINFO, "%s ... ", kind);
104 if (verbose > 1 || do_progress)
105 rprintf(FINFO, "\n");
110 static void emit_filelist_progress(const struct file_list *flist)
112 rprintf(FINFO, " %d files...\r", flist->count);
116 static void maybe_emit_filelist_progress(const struct file_list *flist)
118 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
119 emit_filelist_progress(flist);
123 static void finish_filelist_progress(const struct file_list *flist)
126 /* This overwrites the progress line */
127 rprintf(FINFO, "%d file%sto consider\n",
128 flist->count, flist->count == 1 ? " " : "s ");
130 rprintf(FINFO, "done\n");
133 void show_flist_stats(void)
139 static void list_file_entry(struct file_struct *f)
144 /* this can happen if duplicate names were removed */
148 permstring(perms, f->mode);
151 if (preserve_links && S_ISLNK(f->mode)) {
152 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
154 (double)f->length, timestring(f->modtime),
155 f_name(f), f->u.link);
159 rprintf(FINFO, "%s %11.0f %s %s\n",
161 (double)f->length, timestring(f->modtime),
168 * Stat either a symlink or its referent, depending on the settings of
169 * copy_links, copy_unsafe_links, etc.
171 * @retval -1 on error
173 * @retval 0 for success
175 * @post If @p path is a symlink, then @p linkbuf (of size @c
176 * MAXPATHLEN) contains the symlink target.
178 * @post @p buffer contains information about the link or the
179 * referrent as appropriate, if they exist.
181 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
185 return do_stat(path, buffer);
186 if (link_stat(path, buffer, 0) < 0)
188 if (S_ISLNK(buffer->st_mode)) {
189 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
193 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
195 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
198 return do_stat(path, buffer);
203 return do_stat(path, buffer);
207 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
211 return do_stat(path, buffer);
212 if (do_lstat(path, buffer) < 0)
214 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
216 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
221 return do_stat(path, buffer);
225 /* This function is used to check if a file should be included/excluded
226 * from the list of files based on its name and type etc. The value of
227 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
228 static int is_excluded(char *fname, int is_dir, int filter_level)
230 #if 0 /* This currently never happens, so avoid a useless compare. */
231 if (filter_level == NO_FILTERS)
235 /* never exclude '.', even if somebody does --exclude '*' */
236 if (fname[0] == '.' && !fname[1])
238 /* Handle the -R version of the '.' dir. */
239 if (fname[0] == '/') {
240 int len = strlen(fname);
241 if (fname[len-1] == '.' && fname[len-2] == '/')
245 if (server_filter_list.head
246 && check_filter(&server_filter_list, fname, is_dir) < 0)
248 if (filter_level != ALL_FILTERS)
251 && check_filter(&filter_list, fname, is_dir) < 0)
256 static int to_wire_mode(mode_t mode)
259 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
260 return (mode & ~(_S_IFMT)) | 0120000;
265 static mode_t from_wire_mode(int mode)
267 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
268 return (mode & ~(_S_IFMT)) | _S_IFLNK;
273 static void send_directory(int f, struct file_list *flist,
274 char *fbuf, unsigned int offset);
276 static char *flist_dir;
277 static int flist_dir_len;
281 * Make sure @p flist is big enough to hold at least @p flist->count
284 void flist_expand(struct file_list *flist)
286 struct file_struct **new_ptr;
288 if (flist->count < flist->malloced)
291 if (flist->malloced < FLIST_START)
292 flist->malloced = FLIST_START;
293 else if (flist->malloced >= FLIST_LINEAR)
294 flist->malloced += FLIST_LINEAR;
296 flist->malloced *= 2;
299 * In case count jumped or we are starting the list
300 * with a known size just set it.
302 if (flist->malloced < flist->count)
303 flist->malloced = flist->count;
305 new_ptr = realloc_array(flist->files, struct file_struct *,
308 if (verbose >= 2 && flist->malloced != FLIST_START) {
309 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
311 (double)sizeof flist->files[0] * flist->malloced,
312 (new_ptr == flist->files) ? " not" : "");
315 flist->files = new_ptr;
318 out_of_memory("flist_expand");
321 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
323 unsigned short flags;
324 static time_t modtime;
328 static uint32 rdev_major;
331 static char lastname[MAXPATHLEN];
332 char fname[MAXPATHLEN];
340 modtime = 0, mode = 0;
341 dev = 0, rdev = makedev(0, 0);
348 io_write_phase = "send_file_entry";
350 f_name_to(file, fname);
354 if (file->mode == mode)
355 flags |= XMIT_SAME_MODE;
358 if (preserve_devices) {
359 if (protocol_version < 28) {
360 if (IS_DEVICE(mode)) {
361 if (file->u.rdev == rdev)
362 flags |= XMIT_SAME_RDEV_pre28;
366 rdev = makedev(0, 0);
367 } else if (IS_DEVICE(mode)) {
369 if ((uint32)major(rdev) == rdev_major)
370 flags |= XMIT_SAME_RDEV_MAJOR;
372 rdev_major = major(rdev);
373 if ((uint32)minor(rdev) <= 0xFFu)
374 flags |= XMIT_RDEV_MINOR_IS_SMALL;
377 if (file->uid == uid)
378 flags |= XMIT_SAME_UID;
381 if (file->gid == gid)
382 flags |= XMIT_SAME_GID;
385 if (file->modtime == modtime)
386 flags |= XMIT_SAME_TIME;
388 modtime = file->modtime;
390 #if SUPPORT_HARD_LINKS
391 if (file->link_u.idev) {
392 if (file->F_DEV == dev) {
393 if (protocol_version >= 28)
394 flags |= XMIT_SAME_DEV;
397 flags |= XMIT_HAS_IDEV_DATA;
402 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
404 l2 = strlen(fname+l1);
407 flags |= XMIT_SAME_NAME;
409 flags |= XMIT_LONG_NAME;
411 /* We must make sure we don't send a zero flag byte or the
412 * other end will terminate the flist transfer. Note that
413 * the use of XMIT_DEL_START on a non-dir has no meaning, so
414 * it's harmless way to add a bit to the first flag byte. */
415 if (protocol_version >= 28) {
416 if (!flags && !S_ISDIR(mode))
417 flags |= XMIT_DEL_START;
418 if ((flags & 0xFF00) || !flags) {
419 flags |= XMIT_EXTENDED_FLAGS;
420 write_byte(f, flags);
421 write_byte(f, flags >> 8);
423 write_byte(f, flags);
425 if (!(flags & 0xFF) && !S_ISDIR(mode))
426 flags |= XMIT_DEL_START;
428 flags |= XMIT_LONG_NAME;
429 write_byte(f, flags);
431 if (flags & XMIT_SAME_NAME)
433 if (flags & XMIT_LONG_NAME)
437 write_buf(f, fname + l1, l2);
439 write_longint(f, file->length);
440 if (!(flags & XMIT_SAME_TIME))
441 write_int(f, modtime);
442 if (!(flags & XMIT_SAME_MODE))
443 write_int(f, to_wire_mode(mode));
444 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
449 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
454 if (preserve_devices && IS_DEVICE(mode)) {
455 if (protocol_version < 28) {
456 if (!(flags & XMIT_SAME_RDEV_pre28))
457 write_int(f, (int)rdev);
459 if (!(flags & XMIT_SAME_RDEV_MAJOR))
460 write_int(f, major(rdev));
461 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
462 write_byte(f, minor(rdev));
464 write_int(f, minor(rdev));
469 if (preserve_links && S_ISLNK(mode)) {
470 int len = strlen(file->u.link);
472 write_buf(f, file->u.link, len);
476 #if SUPPORT_HARD_LINKS
477 if (flags & XMIT_HAS_IDEV_DATA) {
478 if (protocol_version < 26) {
479 /* 32-bit dev_t and ino_t */
481 write_int(f, file->F_INODE);
483 /* 64-bit dev_t and ino_t */
484 if (!(flags & XMIT_SAME_DEV))
485 write_longint(f, dev);
486 write_longint(f, file->F_INODE);
491 if (always_checksum) {
495 else if (protocol_version < 28) {
496 /* Prior to 28, we sent a useless set of nulls. */
502 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
506 strlcpy(lastname, fname, MAXPATHLEN);
508 io_write_phase = "unknown";
513 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
514 struct file_list *flist, int f)
516 static time_t modtime;
520 static uint32 rdev_major;
523 static char lastname[MAXPATHLEN], *lastdir;
524 static int lastdir_depth, lastdir_len = -1;
525 static unsigned int del_heir_name_len = 0;
526 static int in_del_hier = 0;
527 char thisname[MAXPATHLEN];
528 unsigned int l1 = 0, l2 = 0;
529 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
531 char *basename, *dirname, *bp;
532 struct file_struct *file;
535 modtime = 0, mode = 0;
536 dev = 0, rdev = makedev(0, 0);
545 if (flags & XMIT_SAME_NAME)
548 if (flags & XMIT_LONG_NAME)
553 if (l2 >= MAXPATHLEN - l1) {
555 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
556 flags, l1, l2, lastname);
557 overflow("receive_file_entry");
560 strlcpy(thisname, lastname, l1 + 1);
561 read_sbuf(f, &thisname[l1], l2);
562 thisname[l1 + l2] = 0;
564 strlcpy(lastname, thisname, MAXPATHLEN);
566 clean_fname(thisname, 0);
569 sanitize_path(thisname, thisname, "", 0);
571 if ((basename = strrchr(thisname, '/')) != NULL) {
572 dirname_len = ++basename - thisname; /* counts future '\0' */
573 if (lastdir_len == dirname_len - 1
574 && strncmp(thisname, lastdir, lastdir_len) == 0) {
576 dirname_len = 0; /* indicates no copy is needed */
584 basename_len = strlen(basename) + 1; /* count the '\0' */
586 file_length = read_longint(f);
587 if (!(flags & XMIT_SAME_TIME))
588 modtime = (time_t)read_int(f);
589 if (!(flags & XMIT_SAME_MODE))
590 mode = from_wire_mode(read_int(f));
592 if (preserve_uid && !(flags & XMIT_SAME_UID))
593 uid = (uid_t)read_int(f);
594 if (preserve_gid && !(flags & XMIT_SAME_GID))
595 gid = (gid_t)read_int(f);
597 if (preserve_devices) {
598 if (protocol_version < 28) {
599 if (IS_DEVICE(mode)) {
600 if (!(flags & XMIT_SAME_RDEV_pre28))
601 rdev = (dev_t)read_int(f);
603 rdev = makedev(0, 0);
604 } else if (IS_DEVICE(mode)) {
606 if (!(flags & XMIT_SAME_RDEV_MAJOR))
607 rdev_major = read_int(f);
608 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
609 rdev_minor = read_byte(f);
611 rdev_minor = read_int(f);
612 rdev = makedev(rdev_major, rdev_minor);
617 if (preserve_links && S_ISLNK(mode)) {
618 linkname_len = read_int(f) + 1; /* count the '\0' */
619 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
620 rprintf(FERROR, "overflow: linkname_len=%d\n",
622 overflow("receive_file_entry");
629 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
631 alloc_len = file_struct_len + dirname_len + basename_len
632 + linkname_len + sum_len;
633 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
635 file = *fptr = (struct file_struct *)bp;
636 memset(bp, 0, file_struct_len);
637 bp += file_struct_len;
640 file->modtime = modtime;
641 file->length = file_length;
647 if (flags & XMIT_DEL_START) {
649 del_heir_name_len = l1 + l2;
650 file->flags |= FLAG_DEL_START;
651 } else if (delete_during && in_del_hier) {
652 if (!relative_paths || (l1 >= del_heir_name_len
653 && thisname[del_heir_name_len] == '/'))
654 file->flags |= FLAG_DEL_START;
661 file->dirname = lastdir = bp;
662 lastdir_len = dirname_len - 1;
663 memcpy(bp, dirname, dirname_len - 1);
667 lastdir_depth = count_dir_elements(lastdir);
669 file->dirname = dirname;
672 memcpy(bp, basename, basename_len);
675 if (preserve_devices && IS_DEVICE(mode))
681 read_sbuf(f, bp, linkname_len - 1);
683 sanitize_path(bp, bp, "", lastdir_depth);
688 #if SUPPORT_HARD_LINKS
689 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
690 flags |= XMIT_HAS_IDEV_DATA;
691 if (flags & XMIT_HAS_IDEV_DATA) {
693 if (protocol_version < 26) {
697 if (!(flags & XMIT_SAME_DEV))
698 dev = read_longint(f);
699 inode = read_longint(f);
701 if (flist->hlink_pool) {
702 file->link_u.idev = pool_talloc(flist->hlink_pool,
703 struct idev, 1, "inode_table");
704 file->F_INODE = inode;
710 if (always_checksum) {
713 file->u.sum = sum = bp;
715 } else if (protocol_version < 28) {
716 /* Prior to 28, we get a useless set of nulls. */
722 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
726 if (!preserve_perms) {
727 /* set an appropriate set of permissions based on original
728 * permissions and umask. This emulates what GNU cp does */
729 file->mode &= ~orig_umask;
735 * Create a file_struct for a named file by reading its stat()
736 * information and performing extensive checks against global
739 * @return the new file, or NULL if there was an error or this file
740 * should be excluded.
742 * @todo There is a small optimization opportunity here to avoid
743 * stat()ing the file in some circumstances, which has a certain cost.
744 * We are called immediately after doing readdir(), and so we may
745 * already know the d_type of the file. We could for example avoid
746 * statting directories if we're not recursing, but this is not a very
747 * important case. Some systems may not have d_type.
749 struct file_struct *make_file(char *fname, struct file_list *flist,
752 static char *lastdir;
753 static int lastdir_len = -1;
754 struct file_struct *file;
756 char sum[SUM_LENGTH];
757 char thisname[MAXPATHLEN];
758 char linkname[MAXPATHLEN];
759 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
760 char *basename, *dirname, *bp;
761 unsigned short flags = 0;
763 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
766 if (strlcpy(thisname, fname, sizeof thisname)
767 >= sizeof thisname - flist_dir_len) {
768 rprintf(FINFO, "skipping overly long name: %s\n", fname);
771 clean_fname(thisname, 0);
773 sanitize_path(thisname, thisname, "", 0);
775 memset(sum, 0, SUM_LENGTH);
777 if (readlink_stat(thisname, &st, linkname) != 0) {
778 int save_errno = errno;
779 /* See if file is excluded before reporting an error. */
780 if (filter_level != NO_FILTERS
781 && is_excluded(thisname, 0, filter_level))
783 if (save_errno == ENOENT) {
785 /* Avoid "vanished" error if symlink points nowhere. */
786 if (copy_links && do_lstat(thisname, &st) == 0
787 && S_ISLNK(st.st_mode)) {
788 io_error |= IOERR_GENERAL;
789 rprintf(FERROR, "symlink has no referent: %s\n",
790 full_fname(thisname));
794 enum logcode c = am_daemon && protocol_version < 28
796 io_error |= IOERR_VANISHED;
797 rprintf(c, "file has vanished: %s\n",
798 full_fname(thisname));
801 io_error |= IOERR_GENERAL;
802 rsyserr(FERROR, save_errno, "readlink %s failed",
803 full_fname(thisname));
808 /* backup.c calls us with filter_level set to NO_FILTERS. */
809 if (filter_level == NO_FILTERS)
812 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
813 rprintf(FINFO, "skipping directory %s\n", thisname);
817 /* We only care about directories because we need to avoid recursing
818 * into a mount-point directory, not to avoid copying a symlinked
819 * file if -L (or similar) was specified. */
820 if (one_file_system && st.st_dev != filesystem_dev
821 && S_ISDIR(st.st_mode))
822 flags |= FLAG_MOUNT_POINT;
824 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
827 if (lp_ignore_nonreadable(module_id)) {
829 if (!S_ISLNK(st.st_mode))
831 if (access(thisname, R_OK) != 0)
838 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
839 who_am_i(), thisname, filter_level);
842 if ((basename = strrchr(thisname, '/')) != NULL) {
843 dirname_len = ++basename - thisname; /* counts future '\0' */
844 if (lastdir_len == dirname_len - 1
845 && strncmp(thisname, lastdir, lastdir_len) == 0) {
847 dirname_len = 0; /* indicates no copy is needed */
855 basename_len = strlen(basename) + 1; /* count the '\0' */
858 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
863 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
865 alloc_len = file_struct_len + dirname_len + basename_len
866 + linkname_len + sum_len;
868 bp = pool_alloc(flist->file_pool, alloc_len,
869 "receive_file_entry");
871 if (!(bp = new_array(char, alloc_len)))
872 out_of_memory("receive_file_entry");
875 file = (struct file_struct *)bp;
876 memset(bp, 0, file_struct_len);
877 bp += file_struct_len;
880 file->modtime = st.st_mtime;
881 file->length = st.st_size;
882 file->mode = st.st_mode;
883 file->uid = st.st_uid;
884 file->gid = st.st_gid;
886 #if SUPPORT_HARD_LINKS
887 if (flist && flist->hlink_pool) {
888 if (protocol_version < 28) {
889 if (S_ISREG(st.st_mode))
890 file->link_u.idev = pool_talloc(
891 flist->hlink_pool, struct idev, 1,
894 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
895 file->link_u.idev = pool_talloc(
896 flist->hlink_pool, struct idev, 1,
900 if (file->link_u.idev) {
901 file->F_DEV = st.st_dev;
902 file->F_INODE = st.st_ino;
907 file->dirname = lastdir = bp;
908 lastdir_len = dirname_len - 1;
909 memcpy(bp, dirname, dirname_len - 1);
913 file->dirname = dirname;
916 memcpy(bp, basename, basename_len);
919 #if HAVE_STRUCT_STAT_ST_RDEV
920 if (preserve_devices && IS_DEVICE(st.st_mode))
921 file->u.rdev = st.st_rdev;
927 memcpy(bp, linkname, linkname_len);
934 file_checksum(thisname, bp, st.st_size);
938 file->basedir = flist_dir;
940 /* This code is only used by the receiver when it is building
941 * a list of files for a delete pass. */
942 if (keep_dirlinks && linkname_len && flist) {
944 int i = flist_find(received_flist, file);
945 if (i >= 0 && S_ISDIR(received_flist->files[i]->mode)
946 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
947 file->modtime = st2.st_mtime;
948 file->length = st2.st_size;
949 file->mode = st2.st_mode;
950 file->uid = st2.st_uid;
951 file->gid = st2.st_gid;
953 if (file->link_u.idev) {
954 pool_free(flist->hlink_pool, 0, file->link_u.idev);
955 file->link_u.idev = NULL;
960 if (!S_ISDIR(st.st_mode))
961 stats.total_size += st.st_size;
967 void send_file_name(int f, struct file_list *flist, char *fname,
968 int recursive, unsigned short base_flags)
970 struct file_struct *file;
971 char fbuf[MAXPATHLEN];
973 /* f is set to -1 when calculating deletion file list */
974 file = make_file(fname, flist,
975 f == -1 && delete_excluded? SERVER_FILTERS : ALL_FILTERS);
980 maybe_emit_filelist_progress(flist);
984 if (file->basename[0]) {
985 flist->files[flist->count++] = file;
986 send_file_entry(file, f, base_flags);
989 if (recursive && S_ISDIR(file->mode)
990 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
992 unsigned int len = strlen(fbuf);
993 if (len > 1 && fbuf[len-1] == '/')
995 if (len >= MAXPATHLEN - 1) {
996 io_error |= IOERR_GENERAL;
997 rprintf(FERROR, "skipping long-named directory: %s\n",
1001 save_filters = push_local_filters(fbuf, len);
1002 send_directory(f, flist, fbuf, len);
1003 pop_local_filters(save_filters);
1008 /* Note that the "recurse" value either contains -1, for infinite recursion,
1009 * or a number >= 0 indicating how many levels of recursion we will allow. */
1010 static void send_directory(int f, struct file_list *flist,
1011 char *fbuf, unsigned int len)
1017 if (!(d = opendir(fbuf))) {
1018 io_error |= IOERR_GENERAL;
1019 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1024 if (len != 1 || *fbuf != '/')
1028 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1029 char *dname = d_name(di);
1030 if (dname[0] == '.' && (dname[1] == '\0'
1031 || (dname[1] == '.' && dname[2] == '\0')))
1033 if (strlcpy(p, dname, MAXPATHLEN - len) < MAXPATHLEN - len) {
1034 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1035 send_file_name(f, flist, fbuf, do_subdirs, 0);
1037 io_error |= IOERR_GENERAL;
1039 "cannot send long-named file %s\n",
1044 io_error |= IOERR_GENERAL;
1046 rsyserr(FERROR, errno, "readdir(%s)", fbuf);
1053 /* This function is normally called by the sender, but the receiving side
1054 * also uses it to construct one or more file lists if one of the --delete
1055 * options have been specified. The delete_files() function sets f to -1
1056 * so that we just construct the file list in memory without sending it
1057 * over the wire. It also has the side-effect of ignoring user-excludes if
1058 * delete_excluded is set (so that the delete list includes user-excluded
1060 struct file_list *send_file_list(int f, int argc, char *argv[])
1064 char *p, *dir, olddir[sizeof curr_dir];
1065 char lastpath[MAXPATHLEN] = "";
1066 struct file_list *flist;
1067 BOOL need_first_push = True;
1068 struct timeval start_tv, end_tv;
1072 if (show_filelist_p() && f != -1)
1073 start_filelist_progress("building file list");
1075 start_write = stats.total_written;
1076 gettimeofday(&start_tv, NULL);
1078 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1082 io_start_buffering_out();
1083 if (filesfrom_fd >= 0) {
1084 if (argv[0] && !push_dir(argv[0])) {
1085 rsyserr(FERROR, errno, "push_dir %s failed",
1086 full_fname(argv[0]));
1087 exit_cleanup(RERR_FILESELECT);
1090 if (curr_dir_len < MAXPATHLEN - 1) {
1091 push_local_filters(curr_dir, curr_dir_len);
1092 need_first_push = False;
1098 char fname2[MAXPATHLEN];
1099 char *fname = fname2;
1103 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1105 sanitize_path(fname, fname, "", 0);
1109 strlcpy(fname, *argv++, MAXPATHLEN);
1111 sanitize_path(fname, fname, "", 0);
1115 if (!l || fname[l - 1] == '/') {
1116 if (l == 2 && fname[0] == '.') {
1117 /* Turn "./" into just "." rather than "./." */
1119 } else if (l < MAXPATHLEN) {
1124 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1125 if (!recurse && xfer_dirs)
1126 recurse = 1; /* allow one level */
1127 } else if (recurse > 0)
1130 if (need_first_push) {
1131 if ((p = strrchr(fname, '/')) != NULL) {
1132 if (*++p && strcmp(p, ".") != 0)
1133 push_local_filters(fname, p - fname);
1134 } else if (strcmp(fname, ".") != 0)
1135 push_local_filters(fname, 0);
1136 need_first_push = False;
1139 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1141 io_error |= IOERR_GENERAL;
1142 rsyserr(FERROR, errno, "link_stat %s failed",
1148 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1149 rprintf(FINFO, "skipping directory %s\n", fname);
1156 if (!relative_paths) {
1157 p = strrchr(fname, '/');
1166 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1167 /* this ensures we send the intermediate directories,
1168 thus getting their permissions right */
1169 char *lp = lastpath, *fn = fname, *slash = fname;
1171 /* Skip any initial directories in our path that we
1172 * have in common with lastpath. */
1173 while (*fn && *lp == *fn) {
1179 if (fn != p || (*lp && *lp != '/')) {
1180 int save_copy_links = copy_links;
1181 int save_xfer_dirs = xfer_dirs;
1182 copy_links = copy_unsafe_links;
1184 while ((slash = strchr(slash+1, '/')) != 0) {
1186 send_file_name(f, flist, fname, 0, 0);
1189 copy_links = save_copy_links;
1190 xfer_dirs = save_xfer_dirs;
1192 strlcpy(lastpath, fname, sizeof lastpath);
1201 static char *lastdir;
1202 static int lastdir_len;
1204 strcpy(olddir, curr_dir); /* can't overflow */
1206 if (!push_dir(dir)) {
1207 io_error |= IOERR_GENERAL;
1208 rsyserr(FERROR, errno, "push_dir %s failed",
1213 if (lastdir && strcmp(lastdir, dir) == 0) {
1214 flist_dir = lastdir;
1215 flist_dir_len = lastdir_len;
1217 flist_dir = lastdir = strdup(dir);
1218 flist_dir_len = lastdir_len = strlen(dir);
1222 if (one_file_system)
1223 filesystem_dev = st.st_dev;
1225 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1226 send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START);
1231 if (!pop_dir(olddir)) {
1232 rsyserr(FERROR, errno, "pop_dir %s failed",
1234 exit_cleanup(RERR_FILESELECT);
1240 gettimeofday(&end_tv, NULL);
1241 stats.flist_buildtime =
1242 (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1243 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1244 if (stats.flist_buildtime == 0)
1245 stats.flist_buildtime = 1;
1248 send_file_entry(NULL, f, 0);
1250 if (show_filelist_p())
1251 finish_filelist_progress(flist);
1253 gettimeofday(&end_tv, NULL);
1254 stats.flist_xfertime =
1255 (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1256 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1259 if (flist->hlink_pool) {
1260 pool_destroy(flist->hlink_pool);
1261 flist->hlink_pool = NULL;
1264 clean_flist(flist, 0, 0);
1267 /* Now send the uid/gid list. This was introduced in
1268 * protocol version 15 */
1271 /* send the io_error flag */
1272 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1275 stats.flist_size = stats.total_written - start_write;
1276 stats.num_files = flist->count;
1280 output_flist(flist, f < 0 ? "delete" : who_am_i());
1283 rprintf(FINFO, "send_file_list done\n");
1289 struct file_list *recv_file_list(int f)
1291 struct file_list *flist;
1292 unsigned short flags;
1295 if (show_filelist_p())
1296 start_filelist_progress("receiving file list");
1298 start_read = stats.total_read;
1300 flist = flist_new(WITH_HLINK, "recv_file_list");
1301 received_flist = flist;
1304 flist->malloced = 1000;
1305 flist->files = new_array(struct file_struct *, flist->malloced);
1310 while ((flags = read_byte(f)) != 0) {
1311 int i = flist->count;
1313 flist_expand(flist);
1315 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1316 flags |= read_byte(f) << 8;
1317 receive_file_entry(&flist->files[i], flags, flist, f);
1319 if (S_ISREG(flist->files[i]->mode))
1320 stats.total_size += flist->files[i]->length;
1324 maybe_emit_filelist_progress(flist);
1327 rprintf(FINFO, "recv_file_name(%s)\n",
1328 f_name(flist->files[i]));
1331 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1334 rprintf(FINFO, "received %d names\n", flist->count);
1336 if (show_filelist_p())
1337 finish_filelist_progress(flist);
1339 clean_flist(flist, relative_paths, 1);
1342 /* Now send the uid/gid list. This was introduced in
1343 * protocol version 15 */
1344 recv_uid_list(f, flist);
1346 /* Recv the io_error flag */
1347 if (lp_ignore_errors(module_id) || ignore_errors)
1350 io_error |= read_int(f);
1354 output_flist(flist, who_am_i());
1358 for (i = 0; i < flist->count; i++)
1359 list_file_entry(flist->files[i]);
1363 rprintf(FINFO, "recv_file_list done\n");
1365 stats.flist_size = stats.total_read - start_read;
1366 stats.num_files = flist->count;
1371 out_of_memory("recv_file_list");
1372 return NULL; /* not reached */
1376 int file_compare(struct file_struct **file1, struct file_struct **file2)
1378 struct file_struct *f1 = *file1;
1379 struct file_struct *f2 = *file2;
1381 if (!f1->basename && !f2->basename)
1387 if (f1->dirname == f2->dirname)
1388 return u_strcmp(f1->basename, f2->basename);
1389 return f_name_cmp(f1, f2);
1393 int flist_find(struct file_list *flist, struct file_struct *f)
1395 int low = 0, high = flist->count - 1;
1397 while (high >= 0 && !flist->files[high]->basename) high--;
1402 while (low != high) {
1403 int mid = (low + high) / 2;
1404 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1406 return flist_up(flist, mid);
1413 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1414 return flist_up(flist, low);
1419 * Free up any resources a file_struct has allocated
1420 * and clear the file.
1422 void clear_file(int i, struct file_list *flist)
1424 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1425 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1426 memset(flist->files[i], 0, file_struct_len);
1431 * allocate a new file list
1433 struct file_list *flist_new(int with_hlink, char *msg)
1435 struct file_list *flist;
1437 flist = new(struct file_list);
1441 memset(flist, 0, sizeof (struct file_list));
1443 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1444 out_of_memory, POOL_INTERN)))
1447 #if SUPPORT_HARD_LINKS
1448 if (with_hlink && preserve_hard_links) {
1449 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1450 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1459 * free up all elements in a flist
1461 void flist_free(struct file_list *flist)
1463 pool_destroy(flist->file_pool);
1464 pool_destroy(flist->hlink_pool);
1471 * This routine ensures we don't have any duplicate names in our file list.
1472 * duplicate names can cause corruption because of the pipelining
1474 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1478 if (!flist || flist->count == 0)
1481 qsort(flist->files, flist->count,
1482 sizeof flist->files[0], (int (*)())file_compare);
1484 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1485 if (flist->files[i]->basename) {
1490 while (++i < flist->count) {
1491 if (!flist->files[i]->basename)
1493 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1494 if (verbose > 1 && !am_server) {
1496 "removing duplicate name %s from file list %d\n",
1497 f_name(flist->files[i]), i);
1499 /* Make sure that if we unduplicate '.', that we don't
1500 * lose track of a user-specified starting point (or
1501 * else deletions will mysteriously fail with -R). */
1502 if (flist->files[i]->flags & FLAG_DEL_START)
1503 flist->files[prev_i]->flags |= FLAG_DEL_START;
1505 clear_file(i, flist);
1511 /* we need to strip off the root directory in the case
1512 of relative paths, but this must be done _after_
1513 the sorting phase */
1514 for (i = 0; i < flist->count; i++) {
1515 if (flist->files[i]->dirname &&
1516 flist->files[i]->dirname[0] == '/') {
1517 memmove(&flist->files[i]->dirname[0],
1518 &flist->files[i]->dirname[1],
1519 strlen(flist->files[i]->dirname));
1522 if (flist->files[i]->dirname &&
1523 !flist->files[i]->dirname[0]) {
1524 flist->files[i]->dirname = NULL;
1530 static void output_flist(struct file_list *flist, const char *whose_list)
1532 char uidbuf[16], gidbuf[16];
1533 struct file_struct *file;
1536 for (i = 0; i < flist->count; i++) {
1537 file = flist->files[i];
1538 if ((am_root || am_sender) && preserve_uid)
1539 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1542 if (preserve_gid && file->gid != GID_NONE)
1543 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1546 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1547 whose_list, i, NS(file->basedir), NS(file->dirname),
1548 NS(file->basename), (int)file->mode,
1549 (double)file->length, uidbuf, gidbuf);
1554 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1556 /* Compare the names of two file_struct entities, just like strcmp()
1557 * would do if it were operating on the joined strings. We assume
1558 * that there are no 0-length strings.
1560 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1563 const uchar *c1, *c2;
1564 enum fnc_state state1, state2;
1566 if (!f1 || !f1->basename) {
1567 if (!f2 || !f2->basename)
1571 if (!f2 || !f2->basename)
1574 if (!(c1 = (uchar*)f1->dirname)) {
1576 c1 = (uchar*)f1->basename;
1582 if (!(c2 = (uchar*)f2->dirname)) {
1584 c2 = (uchar*)f2->basename;
1592 if ((dif = (int)*c1 - (int)*c2) != 0)
1602 c1 = (uchar*)f1->basename;
1616 c2 = (uchar*)f2->basename;
1630 /* Return a copy of the full filename of a flist entry, using the indicated
1631 * buffer. No size-checking is done because we checked the size when creating
1632 * the file_struct entry.
1634 char *f_name_to(struct file_struct *f, char *fbuf)
1636 if (!f || !f->basename)
1640 int len = strlen(f->dirname);
1641 memcpy(fbuf, f->dirname, len);
1643 strcpy(fbuf + len + 1, f->basename);
1645 strcpy(fbuf, f->basename);
1650 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1651 char *f_name(struct file_struct *f)
1653 static char names[5][MAXPATHLEN];
1654 static unsigned int n;
1656 n = (n + 1) % (sizeof names / sizeof names[0]);
1658 return f_name_to(f, names[n]);
1662 static int is_backup_file(char *fn)
1664 int k = strlen(fn) - backup_suffix_len;
1665 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1669 /* This function is used to implement --delete-during. */
1670 void delete_in_dir(struct file_list *flist, char *fname)
1672 static void *filt_array[MAXPATHLEN/2];
1673 static BOOL need_first_push = True;
1674 static int fa_lvl = 0;
1675 static char fbuf[MAXPATHLEN];
1676 struct file_list *dir_list;
1682 pop_local_filters(filt_array[--fa_lvl]);
1683 need_first_push = True;
1688 if (max_delete && deletion_count >= max_delete)
1691 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1693 "IO error encountered -- skipping file deletion\n");
1694 max_delete = -1; /* avoid duplicating the above warning */
1698 for (j = 0; fbuf[j]; j++) {
1699 if (fbuf[j] != fname[j]) {
1702 pop_local_filters(filt_array[--fa_lvl]);
1710 dlen = strlcpy(fbuf, fname, MAXPATHLEN);
1711 if (need_first_push) {
1712 if (dlen != 1 || fbuf[0] != '.') {
1713 char *s = strrchr(fbuf, '/');
1716 first_dlen = s - fbuf;
1719 if (!s || s[1] != '.' || s[2] != '\0') {
1720 filt_array[fa_lvl++] = push_local_filters(fbuf,
1724 need_first_push = False;
1727 if (dlen >= MAXPATHLEN - 1)
1729 if (fa_lvl >= MAXPATHLEN/2)
1730 return; /* impossible... */
1732 if (link_stat(fname, &st, keep_dirlinks) < 0)
1735 if (one_file_system)
1736 filesystem_dev = st.st_dev;
1738 dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
1741 filt_array[fa_lvl++] = push_local_filters(fbuf, dlen);
1742 send_directory(-1, dir_list, fbuf, dlen);
1745 if (dlen == 1 && *fbuf == '.')
1748 clean_flist(dir_list, 0, 0);
1751 output_flist(dir_list, "delete");
1753 delete_missing(flist, dir_list, fname);
1755 flist_free(dir_list);
1759 /* If an item in dir_list is not found in full_list, delete it from the
1761 void delete_missing(struct file_list *full_list, struct file_list *dir_list,
1762 const char *dirname)
1766 if (max_delete && deletion_count >= max_delete)
1770 rprintf(FINFO, "deleting in %s\n", safe_fname(dirname));
1772 for (i = dir_list->count; i--; ) {
1773 if (!dir_list->files[i]->basename)
1775 mode = dir_list->files[i]->mode;
1776 if ((j = flist_find(full_list, dir_list->files[i])) < 0
1777 || (delete_during && S_ISDIR(mode)
1778 && !S_ISDIR(full_list->files[j]->mode))) {
1779 char *f = f_name(dir_list->files[i]);
1780 if (make_backups && (backup_dir || !is_backup_file(f))
1781 && !S_ISDIR(mode)) {
1784 rprintf(FINFO, "deleting %s\n",
1787 } else if (S_ISDIR(mode)) {
1788 int dflag = delete_during ? DEL_FORCE_RECURSE
1790 delete_file(f, DEL_DIR | dflag);
1795 if (max_delete && deletion_count >= max_delete)