2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
33 extern int inc_recurse;
34 extern int do_progress;
35 extern int always_checksum;
37 extern int ignore_errors;
38 extern int numeric_ids;
41 extern int filesfrom_fd;
42 extern int one_file_system;
43 extern int copy_dirlinks;
44 extern int keep_dirlinks;
45 extern int preserve_acls;
46 extern int preserve_xattrs;
47 extern int preserve_links;
48 extern int preserve_hard_links;
49 extern int preserve_devices;
50 extern int preserve_specials;
51 extern int preserve_uid;
52 extern int preserve_gid;
53 extern int relative_paths;
54 extern int implied_dirs;
55 extern int file_extra_cnt;
56 extern int ignore_perishable;
57 extern int non_perishable_cnt;
58 extern int prune_empty_dirs;
59 extern int copy_links;
60 extern int copy_unsafe_links;
61 extern int protocol_version;
62 extern int sanitize_paths;
63 extern struct stats stats;
65 extern char curr_dir[MAXPATHLEN];
67 extern struct chmod_mode_struct *chmod_modes;
69 extern struct filter_list_struct filter_list;
70 extern struct filter_list_struct server_filter_list;
74 dev_t filesystem_dev; /* used to implement -x */
76 struct file_list *cur_flist, *first_flist, *dir_flist;
77 int send_dir_ndx = -1, send_dir_depth = 0;
78 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
79 int file_total = 0; /* total of all active items over all file-lists */
80 int flist_eof = 0; /* all the file-lists are now known */
82 /* The tmp_* vars are used as a cache area by make_file() to store data
83 * that the sender doesn't need to remember in its file list. The data
84 * will survive just long enough to be used by send_file_entry(). */
85 static dev_t tmp_rdev;
86 #ifdef SUPPORT_HARD_LINKS
87 static int64 tmp_dev, tmp_ino;
89 static char tmp_sum[MAX_DIGEST_LEN];
91 static char empty_sum[MAX_DIGEST_LEN];
92 static int flist_count_offset; /* for --delete --progress */
94 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
95 static void output_flist(struct file_list *flist);
100 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
101 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
103 checksum_len = protocol_version < 21 ? 2
104 : protocol_version < 30 ? MD4_DIGEST_LEN
108 static int show_filelist_p(void)
110 return verbose && xfer_dirs && !am_server && !inc_recurse;
113 static void start_filelist_progress(char *kind)
115 rprintf(FCLIENT, "%s ... ", kind);
116 if (verbose > 1 || do_progress)
117 rprintf(FCLIENT, "\n");
121 static void emit_filelist_progress(int count)
123 rprintf(FCLIENT, " %d files...\r", count);
126 static void maybe_emit_filelist_progress(int count)
128 if (do_progress && show_filelist_p() && (count % 100) == 0)
129 emit_filelist_progress(count);
132 static void finish_filelist_progress(const struct file_list *flist)
135 /* This overwrites the progress line */
136 rprintf(FINFO, "%d file%sto consider\n",
137 flist->count, flist->count == 1 ? " " : "s ");
139 rprintf(FINFO, "done\n");
142 void show_flist_stats(void)
147 static void list_file_entry(struct file_struct *f)
149 char permbuf[PERMSTRING_SIZE];
152 if (!F_IS_ACTIVE(f)) {
153 /* this can happen if duplicate names were removed */
157 permstring(permbuf, f->mode);
160 /* TODO: indicate '+' if the entry has an ACL. */
163 if (preserve_links && S_ISLNK(f->mode)) {
164 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
165 permbuf, len, timestring(f->modtime),
166 f_name(f, NULL), F_SYMLINK(f));
170 rprintf(FINFO, "%s %11.0f %s %s\n",
171 permbuf, len, timestring(f->modtime),
176 /* Stat either a symlink or its referent, depending on the settings of
177 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
179 * If path is the name of a symlink, then the linkbuf buffer (which must hold
180 * MAXPATHLEN chars) will be set to the symlink's target string.
182 * The stat structure pointed to by stp will contain information about the
183 * link or the referent as appropriate, if they exist. */
184 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
187 if (link_stat(path, stp, copy_dirlinks) < 0)
189 if (S_ISLNK(stp->st_mode)) {
190 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
193 linkbuf[llen] = '\0';
194 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
196 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
199 return do_stat(path, stp);
204 return do_stat(path, stp);
208 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
212 return do_stat(path, stp);
213 if (do_lstat(path, stp) < 0)
215 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
217 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
222 return do_stat(path, stp);
226 /* This function is used to check if a file should be included/excluded
227 * from the list of files based on its name and type etc. The value of
228 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
229 static int is_excluded(char *fname, int is_dir, int filter_level)
231 #if 0 /* This currently never happens, so avoid a useless compare. */
232 if (filter_level == NO_FILTERS)
236 /* never exclude '.', even if somebody does --exclude '*' */
237 if (fname[0] == '.' && !fname[1])
239 /* Handle the -R version of the '.' dir. */
240 if (fname[0] == '/') {
241 int len = strlen(fname);
242 if (fname[len-1] == '.' && fname[len-2] == '/')
246 if (server_filter_list.head
247 && check_filter(&server_filter_list, fname, is_dir) < 0)
249 if (filter_level != ALL_FILTERS)
252 && check_filter(&filter_list, fname, is_dir) < 0)
257 static int to_wire_mode(mode_t mode)
260 #if _S_IFLNK != 0120000
262 return (mode & ~(_S_IFMT)) | 0120000;
268 static mode_t from_wire_mode(int mode)
270 #if _S_IFLNK != 0120000
271 if ((mode & (_S_IFMT)) == 0120000)
272 return (mode & ~(_S_IFMT)) | _S_IFLNK;
277 static void send_directory(int f, struct file_list *flist, int ndx,
278 char *fbuf, int len, int flags);
280 static const char *flist_dir, *orig_dir;
281 static int flist_dir_len;
285 * Make sure @p flist is big enough to hold at least @p flist->count
288 void flist_expand(struct file_list *flist)
290 struct file_struct **new_ptr;
292 if (flist->count < flist->malloced)
295 if (flist->malloced < FLIST_START)
296 flist->malloced = FLIST_START;
297 else if (flist->malloced >= FLIST_LINEAR)
298 flist->malloced += FLIST_LINEAR;
300 flist->malloced *= 2;
303 * In case count jumped or we are starting the list
304 * with a known size just set it.
306 if (flist->malloced < flist->count)
307 flist->malloced = flist->count;
309 new_ptr = realloc_array(flist->files, struct file_struct *,
312 if (verbose >= 2 && flist->malloced != FLIST_START) {
313 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
315 (double)sizeof flist->files[0] * flist->malloced,
316 (new_ptr == flist->files) ? " not" : "");
319 flist->files = new_ptr;
322 out_of_memory("flist_expand");
325 int push_flist_dir(const char *dir, int len)
327 if (dir == flist_dir)
331 orig_dir = strdup(curr_dir);
333 if (flist_dir && !pop_dir(orig_dir)) {
334 rsyserr(FERROR, errno, "pop_dir %s failed",
335 full_fname(orig_dir));
336 exit_cleanup(RERR_FILESELECT);
339 if (dir && !push_dir(dir, 0)) {
340 io_error |= IOERR_GENERAL;
341 rsyserr(FERROR, errno, "push_dir %s failed in %s",
342 full_fname(dir), curr_dir);
347 flist_dir_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
352 static void send_file_entry(int f, struct file_struct *file, int ndx)
354 static time_t modtime;
358 static uint32 rdev_major;
361 static char *user_name, *group_name;
362 static char lastname[MAXPATHLEN];
363 char fname[MAXPATHLEN];
364 int first_hlink_ndx = -1;
370 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
372 if (file->mode == mode)
373 flags |= XMIT_SAME_MODE;
376 if ((preserve_devices && IS_DEVICE(mode))
377 || (preserve_specials && IS_SPECIAL(mode))) {
378 if (protocol_version < 28) {
379 if (tmp_rdev == rdev)
380 flags |= XMIT_SAME_RDEV_pre28;
385 if ((uint32)major(rdev) == rdev_major)
386 flags |= XMIT_SAME_RDEV_MAJOR;
388 rdev_major = major(rdev);
389 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
390 flags |= XMIT_RDEV_MINOR_8_pre30;
392 } else if (protocol_version < 28)
393 rdev = MAKEDEV(0, 0);
395 if (F_UID(file) == uid && *lastname)
396 flags |= XMIT_SAME_UID;
399 if (preserve_uid && !numeric_ids) {
400 user_name = add_uid(uid);
401 if (inc_recurse && user_name)
402 flags |= XMIT_USER_NAME_FOLLOWS;
407 if (F_GID(file) == gid && *lastname)
408 flags |= XMIT_SAME_GID;
411 if (preserve_gid && !numeric_ids) {
412 group_name = add_gid(gid);
413 if (inc_recurse && group_name)
414 flags |= XMIT_GROUP_NAME_FOLLOWS;
418 if (file->modtime == modtime)
419 flags |= XMIT_SAME_TIME;
421 modtime = file->modtime;
423 #ifdef SUPPORT_HARD_LINKS
425 if (protocol_version >= 30) {
426 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
427 first_hlink_ndx = (int32)(long)np->data - 1;
428 if (first_hlink_ndx < 0) {
429 np->data = (void*)(long)(ndx + 1);
430 flags |= XMIT_HLINK_FIRST;
432 flags |= XMIT_HLINKED;
434 if (tmp_dev == dev) {
435 if (protocol_version >= 28)
436 flags |= XMIT_SAME_DEV_pre30;
439 flags |= XMIT_HLINKED;
445 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
447 l2 = strlen(fname+l1);
450 flags |= XMIT_SAME_NAME;
452 flags |= XMIT_LONG_NAME;
454 /* We must make sure we don't send a zero flag byte or the
455 * other end will terminate the flist transfer. Note that
456 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
457 * it's harmless way to add a bit to the first flag byte. */
458 if (protocol_version >= 28) {
459 if (!flags && !S_ISDIR(mode))
460 flags |= XMIT_TOP_DIR;
461 if ((flags & 0xFF00) || !flags) {
462 flags |= XMIT_EXTENDED_FLAGS;
463 write_shortint(f, flags);
465 write_byte(f, flags);
468 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
469 write_byte(f, flags);
471 if (flags & XMIT_SAME_NAME)
473 if (flags & XMIT_LONG_NAME)
474 write_abbrevint30(f, l2);
477 write_buf(f, fname + l1, l2);
479 if (first_hlink_ndx >= 0) {
480 write_abbrevint30(f, first_hlink_ndx);
484 write_longint(f, F_LENGTH(file));
485 if (!(flags & XMIT_SAME_TIME))
486 write_int(f, modtime);
487 if (!(flags & XMIT_SAME_MODE))
488 write_int(f, to_wire_mode(mode));
489 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
490 if (protocol_version < 30)
493 write_abbrevint(f, uid);
494 if (flags & XMIT_USER_NAME_FOLLOWS) {
495 int len = strlen(user_name);
497 write_buf(f, user_name, len);
501 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
502 if (protocol_version < 30)
505 write_abbrevint(f, gid);
506 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
507 int len = strlen(group_name);
509 write_buf(f, group_name, len);
513 if ((preserve_devices && IS_DEVICE(mode))
514 || (preserve_specials && IS_SPECIAL(mode))) {
515 if (protocol_version < 28) {
516 if (!(flags & XMIT_SAME_RDEV_pre28))
517 write_int(f, (int)rdev);
519 if (!(flags & XMIT_SAME_RDEV_MAJOR))
520 write_abbrevint30(f, major(rdev));
521 if (protocol_version >= 30)
522 write_abbrevint(f, minor(rdev));
523 else if (flags & XMIT_RDEV_MINOR_8_pre30)
524 write_byte(f, minor(rdev));
526 write_int(f, minor(rdev));
531 if (preserve_links && S_ISLNK(mode)) {
532 const char *sl = F_SYMLINK(file);
533 int len = strlen(sl);
534 write_abbrevint30(f, len);
535 write_buf(f, sl, len);
539 #ifdef SUPPORT_HARD_LINKS
540 if (tmp_dev != 0 && protocol_version < 30) {
541 if (protocol_version < 26) {
542 /* 32-bit dev_t and ino_t */
543 write_int(f, (int32)dev);
544 write_int(f, (int32)tmp_ino);
546 /* 64-bit dev_t and ino_t */
547 if (!(flags & XMIT_SAME_DEV_pre30))
548 write_longint(f, dev);
549 write_longint(f, tmp_ino);
554 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
559 /* Prior to 28, we sent a useless set of nulls. */
562 write_buf(f, sum, checksum_len);
566 strlcpy(lastname, fname, MAXPATHLEN);
568 if (S_ISREG(mode) || S_ISLNK(mode))
569 stats.total_size += F_LENGTH(file);
572 static struct file_struct *recv_file_entry(struct file_list *flist,
575 static time_t modtime;
579 static uint32 rdev_major;
582 static char lastname[MAXPATHLEN], *lastdir;
583 static int lastdir_depth, lastdir_len = -1;
584 static unsigned int del_hier_name_len = 0;
585 static int in_del_hier = 0;
586 char thisname[MAXPATHLEN];
587 unsigned int l1 = 0, l2 = 0;
588 int alloc_len, basename_len, linkname_len;
589 int extra_len = file_extra_cnt * EXTRA_LEN;
590 int first_hlink_ndx = -1;
592 const char *basename;
594 struct file_struct *file;
596 if (flags & XMIT_SAME_NAME)
599 if (flags & XMIT_LONG_NAME)
600 l2 = read_abbrevint30(f);
604 if (l2 >= MAXPATHLEN - l1) {
606 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
607 flags, l1, l2, lastname, who_am_i());
608 overflow_exit("recv_file_entry");
611 strlcpy(thisname, lastname, l1 + 1);
612 read_sbuf(f, &thisname[l1], l2);
613 thisname[l1 + l2] = 0;
615 strlcpy(lastname, thisname, MAXPATHLEN);
617 clean_fname(thisname, 0);
620 sanitize_path(thisname, thisname, "", 0, NULL);
622 if ((basename = strrchr(thisname, '/')) != NULL) {
623 int len = basename++ - thisname;
624 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
625 lastdir = new_array(char, len + 1);
626 memcpy(lastdir, thisname, len);
629 lastdir_depth = count_dir_elements(lastdir);
633 basename_len = strlen(basename) + 1; /* count the '\0' */
635 #ifdef SUPPORT_HARD_LINKS
636 if (protocol_version >= 30
637 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
638 struct file_struct *first;
639 first_hlink_ndx = read_abbrevint30(f);
640 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
642 "hard-link reference out of range: %d (%d)\n",
643 first_hlink_ndx, flist->count);
644 exit_cleanup(RERR_PROTOCOL);
646 first = flist->files[first_hlink_ndx];
647 file_length = F_LENGTH(first);
648 modtime = first->modtime;
654 if ((preserve_devices && IS_DEVICE(mode))
655 || (preserve_specials && IS_SPECIAL(mode))) {
656 uint32 *devp = F_RDEV_P(first);
657 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
658 extra_len += 2 * EXTRA_LEN;
660 if (preserve_links && S_ISLNK(mode))
661 linkname_len = strlen(F_SYMLINK(first)) + 1;
668 file_length = read_longint(f);
669 if (!(flags & XMIT_SAME_TIME))
670 modtime = (time_t)read_int(f);
671 if (!(flags & XMIT_SAME_MODE))
672 mode = from_wire_mode(read_int(f));
674 if (chmod_modes && !S_ISLNK(mode))
675 mode = tweak_mode(mode, chmod_modes);
677 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
678 if (protocol_version < 30)
679 uid = (uid_t)read_int(f);
681 uid = (uid_t)read_abbrevint(f);
682 if (flags & XMIT_USER_NAME_FOLLOWS)
683 uid = recv_user_name(f, uid);
684 else if (inc_recurse && am_root && !numeric_ids)
685 uid = match_uid(uid);
688 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
689 if (protocol_version < 30)
690 gid = (gid_t)read_int(f);
692 gid = (gid_t)read_abbrevint(f);
693 if (flags & XMIT_GROUP_NAME_FOLLOWS)
694 gid = recv_group_name(f, gid);
695 else if (inc_recurse && (!am_root || !numeric_ids))
696 gid = match_gid(gid);
700 if ((preserve_devices && IS_DEVICE(mode))
701 || (preserve_specials && IS_SPECIAL(mode))) {
702 if (protocol_version < 28) {
703 if (!(flags & XMIT_SAME_RDEV_pre28))
704 rdev = (dev_t)read_int(f);
707 if (!(flags & XMIT_SAME_RDEV_MAJOR))
708 rdev_major = read_abbrevint30(f);
709 if (protocol_version >= 30)
710 rdev_minor = read_abbrevint(f);
711 else if (flags & XMIT_RDEV_MINOR_8_pre30)
712 rdev_minor = read_byte(f);
714 rdev_minor = read_int(f);
715 rdev = MAKEDEV(rdev_major, rdev_minor);
717 extra_len += 2 * EXTRA_LEN;
719 } else if (protocol_version < 28)
720 rdev = MAKEDEV(0, 0);
723 if (preserve_links && S_ISLNK(mode)) {
724 linkname_len = read_abbrevint30(f) + 1; /* count the '\0' */
725 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
726 rprintf(FERROR, "overflow: linkname_len=%d\n",
728 overflow_exit("recv_file_entry");
735 #ifdef SUPPORT_HARD_LINKS
737 if (preserve_hard_links) {
738 if (protocol_version < 28 && S_ISREG(mode))
739 flags |= XMIT_HLINKED;
740 if (flags & XMIT_HLINKED)
741 extra_len += EXTRA_LEN;
746 /* We need one or two index int32s when we're preserving ACLs. */
748 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
751 if (always_checksum && S_ISREG(mode))
752 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
754 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
755 extra_len += EXTRA_LEN;
757 #if EXTRA_ROUNDING > 0
758 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
759 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
762 if (inc_recurse && S_ISDIR(mode)) {
763 if (one_file_system) {
764 /* Room to save the dir's device for -x */
765 extra_len += 2 * EXTRA_LEN;
770 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
772 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
774 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
776 file = (struct file_struct *)bp;
777 bp += FILE_STRUCT_LEN;
779 memcpy(bp, basename, basename_len);
780 bp += basename_len + linkname_len; /* skip space for symlink too */
782 #ifdef SUPPORT_HARD_LINKS
783 if (flags & XMIT_HLINKED)
784 file->flags |= FLAG_HLINKED;
786 file->modtime = modtime;
787 file->len32 = (uint32)file_length;
788 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
789 file->flags |= FLAG_LENGTH64;
790 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
798 if (basename != thisname) {
799 file->dirname = lastdir;
800 F_DEPTH(file) = lastdir_depth + 1;
805 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
807 if (flags & XMIT_TOP_DIR) {
808 in_del_hier = recurse;
809 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
810 if (relative_paths && del_hier_name_len > 2
811 && lastname[del_hier_name_len-1] == '.'
812 && lastname[del_hier_name_len-2] == '/')
813 del_hier_name_len -= 2;
814 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
815 } else if (in_del_hier) {
816 if (!relative_paths || !del_hier_name_len
817 || (l1 >= del_hier_name_len
818 && lastname[del_hier_name_len] == '/'))
819 file->flags |= FLAG_XFER_DIR;
825 if ((preserve_devices && IS_DEVICE(mode))
826 || (preserve_specials && IS_SPECIAL(mode))) {
827 uint32 *devp = F_RDEV_P(file);
828 DEV_MAJOR(devp) = major(rdev);
829 DEV_MINOR(devp) = minor(rdev);
834 bp = (char*)file->basename + basename_len;
835 if (first_hlink_ndx >= 0) {
836 struct file_struct *first = flist->files[first_hlink_ndx];
837 memcpy(bp, F_SYMLINK(first), linkname_len);
839 read_sbuf(f, bp, linkname_len - 1);
841 sanitize_path(bp, bp, "", lastdir_depth, NULL);
845 #ifdef SUPPORT_HARD_LINKS
846 if (preserve_hard_links && flags & XMIT_HLINKED) {
847 if (protocol_version >= 30) {
848 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
849 ? flist->count : first_hlink_ndx;
851 static int32 cnt = 0;
852 struct idev_node *np;
855 if (protocol_version < 26) {
859 if (!(flags & XMIT_SAME_DEV_pre30))
860 dev = read_longint(f);
861 ino = read_longint(f);
863 np = idev_node(dev, ino);
864 ndx = (int32)(long)np->data - 1;
867 np->data = (void*)(long)cnt;
869 F_HL_GNUM(file) = ndx;
874 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
876 bp = (char*)F_SUM(file);
878 /* Prior to 28, we get a useless set of nulls. */
881 if (first_hlink_ndx >= 0) {
882 struct file_struct *first = flist->files[first_hlink_ndx];
883 memcpy(bp, F_SUM(first), checksum_len);
885 read_buf(f, bp, checksum_len);
889 if (preserve_acls && !S_ISLNK(mode))
890 receive_acl(file, f);
892 #ifdef SUPPORT_XATTRS
894 receive_xattr(file, f );
897 if (S_ISREG(mode) || S_ISLNK(mode))
898 stats.total_size += file_length;
904 * Create a file_struct for a named file by reading its stat()
905 * information and performing extensive checks against global
908 * @return the new file, or NULL if there was an error or this file
909 * should be excluded.
911 * @todo There is a small optimization opportunity here to avoid
912 * stat()ing the file in some circumstances, which has a certain cost.
913 * We are called immediately after doing readdir(), and so we may
914 * already know the d_type of the file. We could for example avoid
915 * statting directories if we're not recursing, but this is not a very
916 * important case. Some systems may not have d_type.
918 struct file_struct *make_file(const char *fname, struct file_list *flist,
919 STRUCT_STAT *stp, int flags, int filter_level)
921 static char *lastdir;
922 static int lastdir_len = -1;
923 struct file_struct *file;
925 char thisname[MAXPATHLEN];
926 char linkname[MAXPATHLEN];
927 int alloc_len, basename_len, linkname_len;
928 int extra_len = file_extra_cnt * EXTRA_LEN;
929 const char *basename;
932 if (strlcpy(thisname, fname, sizeof thisname)
933 >= sizeof thisname - flist_dir_len) {
934 rprintf(FINFO, "skipping overly long name: %s\n", fname);
937 clean_fname(thisname, 0);
939 sanitize_path(thisname, thisname, "", 0, NULL);
941 if (stp && S_ISDIR(stp->st_mode)) {
942 st = *stp; /* Needed for "symlink/." with --relative. */
943 *linkname = '\0'; /* make IBM code checker happy */
944 } else if (readlink_stat(thisname, &st, linkname) != 0) {
945 int save_errno = errno;
946 /* See if file is excluded before reporting an error. */
947 if (filter_level != NO_FILTERS
948 && (is_excluded(thisname, 0, filter_level)
949 || is_excluded(thisname, 1, filter_level))) {
950 if (ignore_perishable && save_errno != ENOENT)
951 non_perishable_cnt++;
954 if (save_errno == ENOENT) {
956 /* Avoid "vanished" error if symlink points nowhere. */
957 if (copy_links && do_lstat(thisname, &st) == 0
958 && S_ISLNK(st.st_mode)) {
959 io_error |= IOERR_GENERAL;
960 rprintf(FERROR, "symlink has no referent: %s\n",
961 full_fname(thisname));
965 enum logcode c = am_daemon && protocol_version < 28
967 io_error |= IOERR_VANISHED;
968 rprintf(c, "file has vanished: %s\n",
969 full_fname(thisname));
972 io_error |= IOERR_GENERAL;
973 rsyserr(FERROR, save_errno, "readlink %s failed",
974 full_fname(thisname));
979 /* backup.c calls us with filter_level set to NO_FILTERS. */
980 if (filter_level == NO_FILTERS)
983 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
984 rprintf(FINFO, "skipping directory %s\n", thisname);
988 /* -x only affects directories because we need to avoid recursing
989 * into a mount-point directory, not to avoid copying a symlinked
990 * file if -L (or similar) was specified. */
991 if (one_file_system && st.st_dev != filesystem_dev
992 && S_ISDIR(st.st_mode)) {
993 if (one_file_system > 1) {
995 rprintf(FINFO, "skipping mount-point dir %s\n",
1000 flags |= FLAG_MOUNT_DIR;
1003 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1004 if (ignore_perishable)
1005 non_perishable_cnt++;
1009 if (lp_ignore_nonreadable(module_id)) {
1010 #ifdef SUPPORT_LINKS
1011 if (!S_ISLNK(st.st_mode))
1013 if (access(thisname, R_OK) != 0)
1019 /* Only divert a directory in the main transfer. */
1020 if (flist && flist->prev && S_ISDIR(st.st_mode)
1021 && flags & FLAG_DIVERT_DIRS) {
1023 /* Room for parent/sibling/next-child info. */
1024 extra_len += 3 * EXTRA_LEN;
1028 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1029 who_am_i(), thisname, filter_level);
1032 if ((basename = strrchr(thisname, '/')) != NULL) {
1033 int len = basename++ - thisname;
1034 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1035 lastdir = new_array(char, len + 1);
1036 memcpy(lastdir, thisname, len);
1037 lastdir[len] = '\0';
1041 basename = thisname;
1042 basename_len = strlen(basename) + 1; /* count the '\0' */
1044 #ifdef SUPPORT_LINKS
1045 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1050 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1051 extra_len += EXTRA_LEN;
1053 #if EXTRA_ROUNDING > 0
1054 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1055 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1058 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1061 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1063 if (!(bp = new_array(char, alloc_len)))
1064 out_of_memory("make_file");
1067 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1069 file = (struct file_struct *)bp;
1070 bp += FILE_STRUCT_LEN;
1072 memcpy(bp, basename, basename_len);
1073 bp += basename_len + linkname_len; /* skip space for symlink too */
1075 #ifdef SUPPORT_HARD_LINKS
1076 if (preserve_hard_links && flist && flist->prev) {
1077 if (protocol_version >= 28
1078 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1079 : S_ISREG(st.st_mode)) {
1080 tmp_dev = st.st_dev;
1081 tmp_ino = st.st_ino;
1087 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1088 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1089 tmp_rdev = st.st_rdev;
1094 file->flags = flags;
1095 file->modtime = st.st_mtime;
1096 file->len32 = (uint32)st.st_size;
1097 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1098 file->flags |= FLAG_LENGTH64;
1099 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1101 file->mode = st.st_mode;
1103 F_OWNER(file) = st.st_uid;
1105 F_GROUP(file) = st.st_gid;
1107 if (basename != thisname)
1108 file->dirname = lastdir;
1110 #ifdef SUPPORT_LINKS
1112 bp = (char*)file->basename + basename_len;
1113 memcpy(bp, linkname, linkname_len);
1117 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1118 file_checksum(thisname, tmp_sum, st.st_size);
1120 F_ROOTDIR(file) = flist_dir;
1122 /* This code is only used by the receiver when it is building
1123 * a list of files for a delete pass. */
1124 if (keep_dirlinks && linkname_len && flist) {
1126 int save_mode = file->mode;
1127 file->mode = S_IFDIR; /* Find a directory with our name. */
1128 if (flist_find(dir_flist, file) >= 0
1129 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1130 file->modtime = st2.st_mtime;
1132 file->mode = st2.st_mode;
1134 F_OWNER(file) = st2.st_uid;
1136 F_GROUP(file) = st2.st_gid;
1138 file->mode = save_mode;
1141 if (basename_len == 0+1)
1144 if (inc_recurse && flist == dir_flist) {
1145 flist_expand(flist);
1146 flist->files[flist->count++] = file;
1152 /* Only called for temporary file_struct entries created by make_file(). */
1153 void unmake_file(struct file_struct *file)
1155 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1156 #if EXTRA_ROUNDING > 0
1157 if (extra_cnt & EXTRA_ROUNDING)
1158 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1160 free(REQ_EXTRA(file, extra_cnt));
1163 static struct file_struct *send_file_name(int f, struct file_list *flist,
1164 char *fname, STRUCT_STAT *stp,
1165 int flags, int filter_flags)
1167 struct file_struct *file;
1168 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1172 file = make_file(fname, flist, stp, flags, filter_flags);
1176 if (chmod_modes && !S_ISLNK(file->mode))
1177 file->mode = tweak_mode(file->mode, chmod_modes);
1180 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1181 sx.st.st_mode = file->mode;
1182 sx.acc_acl = sx.def_acl = NULL;
1183 if (get_acl(fname, &sx) < 0)
1187 #ifdef SUPPORT_XATTRS
1188 if (preserve_xattrs && f >= 0) {
1190 if (get_xattr(fname, &sx) < 0)
1195 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1197 flist_expand(flist);
1198 flist->files[flist->count++] = file;
1200 send_file_entry(f, file, flist->count - 1);
1202 if (preserve_acls && !S_ISLNK(file->mode)) {
1207 #ifdef SUPPORT_XATTRS
1208 if (preserve_xattrs) {
1209 F_XATTR(file) = send_xattr(&sx, f);
1217 static void send_if_directory(int f, struct file_list *flist,
1218 struct file_struct *file,
1219 char *fbuf, unsigned int ol,
1222 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1224 if (S_ISDIR(file->mode)
1225 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1227 unsigned int len = strlen(fbuf);
1228 if (len > 1 && fbuf[len-1] == '/')
1230 if (len >= MAXPATHLEN - 1) {
1231 io_error |= IOERR_GENERAL;
1232 rprintf(FERROR, "skipping long-named directory: %s\n",
1236 save_filters = push_local_filters(fbuf, len);
1237 send_directory(f, flist, -1, fbuf, len, flags);
1238 pop_local_filters(save_filters);
1245 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1247 return f_name_cmp(*file1, *file2);
1250 /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1251 * sort them by name, and link them into the tree, setting the appropriate
1252 * parent/child/sibling pointers. */
1253 static void add_dirs_to_tree(int parent_ndx, int start, int end)
1257 int32 *parent_dp = parent_ndx < 0 ? NULL
1258 : F_DIRNODE_P(dir_flist->files[parent_ndx]);
1260 qsort(dir_flist->files + start, end - start + 1,
1261 sizeof dir_flist->files[0], (int (*)())file_compare);
1263 for (i = start; i <= end; i++) {
1264 struct file_struct *file = dir_flist->files[i];
1265 if (!(file->flags & FLAG_XFER_DIR)
1266 || file->flags & FLAG_MOUNT_DIR)
1269 DIR_NEXT_SIBLING(dp) = i;
1271 DIR_FIRST_CHILD(parent_dp) = i;
1274 dp = F_DIRNODE_P(file);
1275 DIR_PARENT(dp) = parent_ndx;
1276 DIR_FIRST_CHILD(dp) = -1;
1279 DIR_NEXT_SIBLING(dp) = -1;
1282 /* This function is normally called by the sender, but the receiving side also
1283 * calls it from get_dirlist() with f set to -1 so that we just construct the
1284 * file list in memory without sending it over the wire. Also, get_dirlist()
1285 * might call this with f set to -2, which also indicates that local filter
1286 * rules should be ignored. */
1287 static void send_directory(int f, struct file_list *flist, int parent_ndx,
1288 char *fbuf, int len, int flags)
1294 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1295 int start = divert_dirs ? dir_flist->count : flist->count;
1296 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1298 assert(flist != NULL);
1300 if (!(d = opendir(fbuf))) {
1301 io_error |= IOERR_GENERAL;
1302 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1307 if (len != 1 || *fbuf != '/')
1310 remainder = MAXPATHLEN - (p - fbuf);
1312 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1313 char *dname = d_name(di);
1314 if (dname[0] == '.' && (dname[1] == '\0'
1315 || (dname[1] == '.' && dname[2] == '\0')))
1317 if (strlcpy(p, dname, remainder) >= remainder) {
1318 io_error |= IOERR_GENERAL;
1320 "cannot send long-named file %s\n",
1325 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
1331 io_error |= IOERR_GENERAL;
1332 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1341 add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1);
1343 int i, end = flist->count - 1;
1344 /* send_if_directory() bumps flist->count, so use "end". */
1345 for (i = start; i <= end; i++)
1346 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1350 void send_extra_file_list(int f, int at_least)
1352 char fbuf[MAXPATHLEN];
1353 struct file_list *flist;
1355 int future_cnt, save_io_error = io_error;
1360 /* Keep sending data until we have the requested number of
1361 * files in the upcoming file-lists. */
1362 if (cur_flist->next) {
1363 flist = first_flist->prev; /* the newest flist */
1364 future_cnt = flist->count
1365 + flist->ndx_start - cur_flist->next->ndx_start;
1368 while (future_cnt < at_least) {
1369 struct file_struct *file = dir_flist->files[send_dir_ndx];
1374 dlen = strlen(fbuf);
1376 if (F_ROOTDIR(file) != flist_dir) {
1377 if (!push_flist_dir(F_ROOTDIR(file), -1))
1378 exit_cleanup(RERR_FILESELECT);
1381 flist = flist_new(0, "send_extra_file_list");
1382 start_write = stats.total_written;
1384 write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx);
1385 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1386 send_directory(f, flist, send_dir_ndx, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1389 clean_flist(flist, 0, 0);
1390 file_total += flist->count;
1391 future_cnt += flist->count;
1392 stats.flist_size += stats.total_written - start_write;
1393 stats.num_files += flist->count;
1395 output_flist(flist);
1397 dp = F_DIRNODE_P(file);
1398 if (DIR_FIRST_CHILD(dp) >= 0) {
1399 send_dir_ndx = DIR_FIRST_CHILD(dp);
1402 while (DIR_NEXT_SIBLING(dp) < 0) {
1403 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1404 write_ndx(f, NDX_FLIST_EOF);
1406 change_local_filter_dir(NULL, 0, 0);
1410 file = dir_flist->files[send_dir_ndx];
1411 dp = F_DIRNODE_P(file);
1413 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1418 if (io_error != save_io_error && !ignore_errors)
1419 send_msg_int(MSG_IO_ERROR, io_error);
1422 struct file_list *send_file_list(int f, int argc, char *argv[])
1427 char lastpath[MAXPATHLEN] = "";
1428 struct file_list *flist;
1429 struct timeval start_tv, end_tv;
1432 int flags, disable_buffering;
1434 rprintf(FLOG, "building file list\n");
1435 if (show_filelist_p())
1436 start_filelist_progress("building file list");
1437 else if (inc_recurse && verbose && !am_server)
1438 rprintf(FCLIENT, "sending incremental file list\n");
1440 start_write = stats.total_written;
1441 gettimeofday(&start_tv, NULL);
1443 #ifdef SUPPORT_HARD_LINKS
1444 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1448 flist = cur_flist = flist_new(0, "send_file_list");
1450 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1451 flags = FLAG_DIVERT_DIRS;
1453 dir_flist = cur_flist;
1457 disable_buffering = io_start_buffering_out(f);
1458 if (filesfrom_fd >= 0) {
1459 if (argv[0] && !push_dir(argv[0], 0)) {
1460 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1461 full_fname(argv[0]), curr_dir);
1462 exit_cleanup(RERR_FILESELECT);
1468 char fbuf[MAXPATHLEN];
1473 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1475 sanitize_path(fbuf, fbuf, "", 0, NULL);
1479 strlcpy(fbuf, *argv++, MAXPATHLEN);
1481 sanitize_path(fbuf, fbuf, "", 0, NULL);
1485 if (relative_paths) {
1486 /* We clean up fbuf below. */
1488 } else if (!len || fbuf[len - 1] == '/') {
1489 if (len == 2 && fbuf[0] == '.') {
1490 /* Turn "./" into just "." rather than "./." */
1493 if (len + 1 >= MAXPATHLEN)
1494 overflow_exit("send_file_list");
1499 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1500 && (len == 2 || fbuf[len-3] == '/')) {
1501 if (len + 2 >= MAXPATHLEN)
1502 overflow_exit("send_file_list");
1508 is_dot_dir = fbuf[len-1] == '.'
1509 && (len == 1 || fbuf[len-2] == '/');
1514 if (!relative_paths) {
1515 p = strrchr(fbuf, '/');
1522 len -= p - fbuf + 1;
1527 if ((p = strstr(fbuf, "/./")) != NULL) {
1533 len -= p - fbuf + 3;
1537 /* Get rid of trailing "/" and "/.". */
1539 if (fn[len - 1] == '/') {
1541 if (!--len && !dir) {
1546 else if (len >= 2 && fn[len - 1] == '.'
1547 && fn[len - 2] == '/') {
1549 if (!(len -= 2) && !dir) {
1556 if (len == 1 && fn[0] == '/')
1559 /* Reject a ".." dir in the active part of the path. */
1560 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1561 if ((p[2] == '/' || p[2] == '\0')
1562 && (p == fn || p[-1] == '/')) {
1564 "found \"..\" dir in relative path: %s\n",
1566 exit_cleanup(RERR_SYNTAX);
1577 static const char *lastdir;
1578 static int lastdir_len = -1;
1579 int len = strlen(dir);
1581 if (len != lastdir_len || memcmp(lastdir, dir, len) != 0) {
1582 if (!push_flist_dir(strdup(dir), len))
1584 lastdir = flist_dir;
1585 lastdir_len = flist_dir_len;
1586 } else if (!push_flist_dir(lastdir, lastdir_len)) {
1588 io_error |= IOERR_GENERAL;
1589 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1590 full_fname(dir), curr_dir);
1596 memmove(fbuf, fn, len + 1);
1598 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1599 io_error |= IOERR_GENERAL;
1600 rsyserr(FERROR, errno, "link_stat %s failed",
1605 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1606 rprintf(FINFO, "skipping directory %s\n", fbuf);
1610 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1611 /* Send the implied directories at the start of the
1612 * source spec, so we get their permissions right. */
1613 char *lp = lastpath, *slash = fbuf;
1615 /* Skip any initial directories in our path that we
1616 * have in common with lastpath. */
1617 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1622 if (fn != p || (*lp && *lp != '/')) {
1623 int save_copy_links = copy_links;
1624 int save_xfer_dirs = xfer_dirs;
1625 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1626 copy_links |= copy_unsafe_links;
1628 while ((slash = strchr(slash+1, '/')) != 0) {
1630 send_file_name(f, flist, fbuf, NULL,
1631 dir_flags, ALL_FILTERS);
1634 copy_links = save_copy_links;
1635 xfer_dirs = save_xfer_dirs;
1637 strlcpy(lastpath, fbuf, sizeof lastpath);
1642 if (one_file_system)
1643 filesystem_dev = st.st_dev;
1645 if (recurse || (xfer_dirs && is_dot_dir)) {
1646 struct file_struct *file;
1647 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR
1648 | (is_dot_dir ? 0 : flags)
1649 | (inc_recurse ? FLAG_DIVERT_DIRS : 0);
1650 file = send_file_name(f, flist, fbuf, &st,
1651 top_flags, ALL_FILTERS);
1652 if (file && !inc_recurse)
1653 send_if_directory(f, flist, file, fbuf, len, flags);
1655 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1658 gettimeofday(&end_tv, NULL);
1659 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1660 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1661 if (stats.flist_buildtime == 0)
1662 stats.flist_buildtime = 1;
1665 write_byte(f, 0); /* Indicate end of file list */
1667 #ifdef SUPPORT_HARD_LINKS
1668 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1672 if (show_filelist_p())
1673 finish_filelist_progress(flist);
1675 gettimeofday(&end_tv, NULL);
1676 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1677 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1679 /* Sort the list without removing any duplicates in non-incremental
1680 * mode. This allows the receiving side to ask for whatever name it
1681 * kept. For incremental mode, the sender also removes duplicates
1682 * in this initial file-list so that it avoids re-sending duplicated
1684 clean_flist(flist, 0, inc_recurse);
1685 file_total += flist->count;
1687 if (!numeric_ids && !inc_recurse)
1690 /* send the io_error flag */
1691 if (protocol_version < 30)
1692 write_int(f, ignore_errors ? 0 : io_error);
1693 else if (io_error && !ignore_errors)
1694 send_msg_int(MSG_IO_ERROR, io_error);
1696 if (disable_buffering)
1697 io_end_buffering_out();
1699 stats.flist_size = stats.total_written - start_write;
1700 stats.num_files = flist->count;
1703 output_flist(flist);
1706 rprintf(FINFO, "send_file_list done\n");
1709 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
1710 if (send_dir_ndx < 0) {
1711 write_ndx(f, NDX_FLIST_EOF);
1714 else if (file_total == 1) {
1715 /* If we're creating incremental file-lists and there
1716 * was just 1 item in the first file-list, send 1 more
1717 * file-list to check if this is a 1-file xfer. */
1718 send_extra_file_list(f, 1);
1725 struct file_list *recv_file_list(int f)
1727 struct file_list *flist;
1732 rprintf(FLOG, "receiving file list\n");
1733 if (show_filelist_p())
1734 start_filelist_progress("receiving file list");
1735 else if (inc_recurse && verbose && !am_server && !first_flist)
1736 rprintf(FCLIENT, "receiving incremental file list\n");
1738 start_read = stats.total_read;
1740 flist = flist_new(0, "recv_file_list");
1742 #ifdef SUPPORT_HARD_LINKS
1743 if (preserve_hard_links && protocol_version < 30)
1748 if (flist->ndx_start == 0)
1749 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1750 dstart = dir_flist->count;
1756 while ((flags = read_byte(f)) != 0) {
1757 struct file_struct *file;
1759 flist_expand(flist);
1761 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1762 flags |= read_byte(f) << 8;
1763 file = recv_file_entry(flist, flags, f);
1765 if (inc_recurse && S_ISDIR(file->mode)) {
1766 flist_expand(dir_flist);
1767 dir_flist->files[dir_flist->count++] = file;
1770 flist->files[flist->count++] = file;
1772 maybe_emit_filelist_progress(flist->count);
1775 rprintf(FINFO, "recv_file_name(%s)\n",
1776 f_name(file, NULL));
1779 file_total += flist->count;
1782 rprintf(FINFO, "received %d names\n", flist->count);
1784 if (show_filelist_p())
1785 finish_filelist_progress(flist);
1787 clean_flist(flist, relative_paths, 1);
1790 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
1791 sizeof dir_flist->files[0], (int (*)())file_compare);
1793 recv_uid_list(f, flist);
1795 if (protocol_version < 30) {
1796 /* Recv the io_error flag */
1800 io_error |= read_int(f);
1804 output_flist(flist);
1808 for (i = 0; i < flist->count; i++)
1809 list_file_entry(flist->files[i]);
1813 rprintf(FINFO, "recv_file_list done\n");
1815 stats.flist_size += stats.total_read - start_read;
1816 stats.num_files += flist->count;
1821 /* This is only used once by the receiver if the very first file-list
1822 * has exactly one item in it. */
1823 void recv_additional_file_list(int f)
1825 struct file_list *flist;
1826 int ndx = read_ndx(f);
1827 if (ndx == NDX_FLIST_EOF) {
1829 change_local_filter_dir(NULL, 0, 0);
1831 ndx = NDX_FLIST_OFFSET - ndx;
1832 if (ndx < 0 || ndx >= dir_flist->count) {
1833 ndx = NDX_FLIST_OFFSET - ndx;
1835 "Invalid dir index: %d (%d - %d)\n",
1836 ndx, NDX_FLIST_OFFSET,
1837 NDX_FLIST_OFFSET - dir_flist->count);
1838 exit_cleanup(RERR_PROTOCOL);
1840 flist = recv_file_list(f);
1841 flist->parent_ndx = ndx;
1845 /* Search for an identically-named item in the file list. Note that the
1846 * items must agree in their directory-ness, or no match is returned. */
1847 int flist_find(struct file_list *flist, struct file_struct *f)
1849 int low = flist->low, high = flist->high;
1850 int diff, mid, mid_up;
1852 while (low <= high) {
1853 mid = (low + high) / 2;
1854 if (F_IS_ACTIVE(flist->files[mid]))
1857 /* Scan for the next non-empty entry using the cached
1858 * distance values. If the value isn't fully up-to-
1859 * date, update it. */
1860 mid_up = mid + F_DEPTH(flist->files[mid]);
1861 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1863 mid_up += F_DEPTH(flist->files[mid_up]);
1864 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1865 F_DEPTH(flist->files[mid]) = mid_up - mid;
1867 if (mid_up > high) {
1868 /* If there's nothing left above us, set high to
1869 * a non-empty entry below us and continue. */
1870 high = mid - (int)flist->files[mid]->len32;
1871 if (!F_IS_ACTIVE(flist->files[high])) {
1873 high -= (int)flist->files[high]->len32;
1874 } while (!F_IS_ACTIVE(flist->files[high]));
1875 flist->files[mid]->len32 = mid - high;
1880 diff = f_name_cmp(flist->files[mid_up], f);
1882 if (protocol_version < 29
1883 && S_ISDIR(flist->files[mid_up]->mode)
1884 != S_ISDIR(f->mode))
1897 * Free up any resources a file_struct has allocated
1898 * and clear the file.
1900 void clear_file(struct file_struct *file)
1902 /* The +1 zeros out the first char of the basename. */
1903 memset(file, 0, FILE_STRUCT_LEN + 1);
1904 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1905 * entry. Likewise for len32 in the opposite direction. We assume
1906 * that we're alone for now since flist_find() will adjust the counts
1907 * it runs into that aren't up-to-date. */
1908 file->len32 = F_DEPTH(file) = 1;
1911 /* Allocate a new file list. */
1912 struct file_list *flist_new(int flags, char *msg)
1914 struct file_list *flist;
1916 flist = new(struct file_list);
1920 memset(flist, 0, sizeof flist[0]);
1922 if (!(flags & FLIST_TEMP)) {
1924 flist->ndx_start = first_flist->prev->ndx_start
1925 + first_flist->prev->count;
1927 /* This is a doubly linked list with prev looping back to
1928 * the end of the list, but the last next pointer is NULL. */
1930 first_flist = cur_flist = flist->prev = flist;
1932 flist->prev = first_flist->prev;
1933 flist->prev->next = first_flist->prev = flist;
1938 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1944 /* Free up all elements in a flist. */
1945 void flist_free(struct file_list *flist)
1948 ; /* Was FLIST_TEMP dir-list. */
1949 else if (flist == flist->prev) {
1950 first_flist = cur_flist = NULL;
1954 if (flist == cur_flist)
1955 cur_flist = flist->next;
1956 if (flist == first_flist)
1957 first_flist = first_flist->next;
1959 flist->prev->next = flist->next;
1961 flist->next = first_flist;
1963 flist->next->prev = flist->prev;
1964 file_total -= flist->count;
1968 pool_destroy(flist->file_pool);
1974 * This routine ensures we don't have any duplicate names in our file list.
1975 * duplicate names can cause corruption because of the pipelining
1977 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1979 char fbuf[MAXPATHLEN];
1984 if (flist->count == 0) {
1989 qsort(flist->files, flist->count,
1990 sizeof flist->files[0], (int (*)())file_compare);
1992 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1993 if (F_IS_ACTIVE(flist->files[i])) {
1998 flist->low = prev_i;
1999 while (++i < flist->count) {
2001 struct file_struct *file = flist->files[i];
2003 if (!F_IS_ACTIVE(file))
2005 if (f_name_cmp(file, flist->files[prev_i]) == 0)
2007 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2008 int save_mode = file->mode;
2009 /* Make sure that this directory doesn't duplicate a
2010 * non-directory earlier in the list. */
2011 flist->high = prev_i;
2012 file->mode = S_IFREG;
2013 j = flist_find(flist, file);
2014 file->mode = save_mode;
2018 struct file_struct *fp = flist->files[j];
2020 /* If one is a dir and the other is not, we want to
2021 * keep the dir because it might have contents in the
2023 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2024 if (S_ISDIR(file->mode))
2028 } else if (protocol_version < 27)
2032 if (verbose > 1 && !am_server) {
2034 "removing duplicate name %s from file list (%d)\n",
2035 f_name(file, fbuf), drop);
2037 /* Make sure we don't lose track of a user-specified
2039 flist->files[keep]->flags |= flist->files[drop]->flags
2040 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2042 clear_file(flist->files[drop]);
2045 if (flist->low == drop) {
2047 j < i && !F_IS_ACTIVE(flist->files[j]);
2056 flist->high = no_dups ? prev_i : flist->count - 1;
2059 /* We need to strip off the leading slashes for relative
2060 * paths, but this must be done _after_ the sorting phase. */
2061 for (i = flist->low; i <= flist->high; i++) {
2062 struct file_struct *file = flist->files[i];
2066 while (*file->dirname == '/')
2068 if (!*file->dirname)
2069 file->dirname = NULL;
2073 if (prune_empty_dirs && no_dups) {
2074 int j, prev_depth = 0;
2076 prev_i = 0; /* It's OK that this isn't really true. */
2078 for (i = flist->low; i <= flist->high; i++) {
2079 struct file_struct *fp, *file = flist->files[i];
2081 /* This temporarily abuses the F_DEPTH() value for a
2082 * directory that is in a chain that might get pruned.
2083 * We restore the old value if it gets a reprieve. */
2084 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2085 /* Dump empty dirs when coming back down. */
2086 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2087 fp = flist->files[prev_i];
2088 if (F_DEPTH(fp) >= 0)
2090 prev_i = -F_DEPTH(fp)-1;
2093 prev_depth = F_DEPTH(file);
2094 if (is_excluded(f_name(file, fbuf), 1,
2096 /* Keep dirs through this dir. */
2097 for (j = prev_depth-1; ; j--) {
2098 fp = flist->files[prev_i];
2099 if (F_DEPTH(fp) >= 0)
2101 prev_i = -F_DEPTH(fp)-1;
2105 F_DEPTH(file) = -prev_i-1;
2108 /* Keep dirs through this non-dir. */
2109 for (j = prev_depth; ; j--) {
2110 fp = flist->files[prev_i];
2111 if (F_DEPTH(fp) >= 0)
2113 prev_i = -F_DEPTH(fp)-1;
2118 /* Dump all remaining empty dirs. */
2120 struct file_struct *fp = flist->files[prev_i];
2121 if (F_DEPTH(fp) >= 0)
2123 prev_i = -F_DEPTH(fp)-1;
2127 for (i = flist->low; i <= flist->high; i++) {
2128 if (F_IS_ACTIVE(flist->files[i]))
2132 for (i = flist->high; i >= flist->low; i--) {
2133 if (F_IS_ACTIVE(flist->files[i]))
2140 static void output_flist(struct file_list *flist)
2142 char uidbuf[16], gidbuf[16], depthbuf[16];
2143 struct file_struct *file;
2144 const char *root, *dir, *slash, *name, *trail;
2145 const char *who = who_am_i();
2148 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2149 who, flist->ndx_start, flist->count, flist->low, flist->high);
2150 for (i = 0; i < flist->count; i++) {
2151 file = flist->files[i];
2152 if ((am_root || am_sender) && preserve_uid) {
2153 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
2157 if (preserve_gid && F_GID(file) != GID_NONE) {
2158 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
2163 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2164 if (F_IS_ACTIVE(file)) {
2165 root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
2166 if ((dir = file->dirname) == NULL)
2170 name = file->basename;
2171 trail = S_ISDIR(file->mode) ? "/" : "";
2173 root = dir = slash = name = trail = "";
2174 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2175 who, i, root, dir, slash, name, trail, (int)file->mode,
2176 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
2180 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2181 enum fnc_type { t_PATH, t_ITEM };
2183 /* Compare the names of two file_struct entities, similar to how strcmp()
2184 * would do if it were operating on the joined strings.
2186 * Some differences beginning with protocol_version 29: (1) directory names
2187 * are compared with an assumed trailing slash so that they compare in a
2188 * way that would cause them to sort immediately prior to any content they
2189 * may have; (2) a directory of any name compares after a non-directory of
2190 * any name at the same depth; (3) a directory with name "." compares prior
2191 * to anything else. These changes mean that a directory and a non-dir
2192 * with the same name will not compare as equal (protocol_version >= 29).
2194 * The dirname component can be an empty string, but the basename component
2195 * cannot (and never is in the current codebase). The basename component
2196 * may be NULL (for a removed item), in which case it is considered to be
2197 * after any existing item. */
2198 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
2201 const uchar *c1, *c2;
2202 enum fnc_state state1, state2;
2203 enum fnc_type type1, type2;
2204 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2206 if (!f1 || !F_IS_ACTIVE(f1)) {
2207 if (!f2 || !F_IS_ACTIVE(f2))
2211 if (!f2 || !F_IS_ACTIVE(f2))
2214 c1 = (uchar*)f1->dirname;
2215 c2 = (uchar*)f2->dirname;
2219 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2220 c1 = (const uchar*)f1->basename;
2221 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2223 state1 = s_TRAILING;
2232 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2233 c2 = (const uchar*)f2->basename;
2234 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2236 state2 = s_TRAILING;
2246 return type1 == t_PATH ? 1 : -1;
2256 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2257 c1 = (const uchar*)f1->basename;
2258 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2260 state1 = s_TRAILING;
2266 state1 = s_TRAILING;
2267 if (type1 == t_PATH) {
2276 if (*c2 && type1 != type2)
2277 return type1 == t_PATH ? 1 : -1;
2286 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2287 c2 = (const uchar*)f2->basename;
2288 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2290 state2 = s_TRAILING;
2296 state2 = s_TRAILING;
2297 if (type2 == t_PATH) {
2309 return type1 == t_PATH ? 1 : -1;
2311 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2316 char *f_name_buf(void)
2318 static char names[5][MAXPATHLEN];
2319 static unsigned int n;
2321 n = (n + 1) % (sizeof names / sizeof names[0]);
2326 /* Return a copy of the full filename of a flist entry, using the indicated
2327 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2328 * done because we checked the size when creating the file_struct entry.
2330 char *f_name(struct file_struct *f, char *fbuf)
2332 if (!f || !F_IS_ACTIVE(f))
2336 fbuf = f_name_buf();
2339 int len = strlen(f->dirname);
2340 memcpy(fbuf, f->dirname, len);
2342 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2344 strlcpy(fbuf, f->basename, MAXPATHLEN);
2349 /* Do a non-recursive scan of the named directory, possibly ignoring all
2350 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2351 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2352 * buffer (the functions we call will append names onto the end, but the old
2353 * dir value will be restored on exit). */
2354 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2356 struct file_list *dirlist;
2357 char dirbuf[MAXPATHLEN];
2358 int save_recurse = recurse;
2359 int save_xfer_dirs = xfer_dirs;
2362 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2363 if (dlen >= MAXPATHLEN)
2368 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2372 send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0);
2373 xfer_dirs = save_xfer_dirs;
2374 recurse = save_recurse;
2376 flist_count_offset += dirlist->count;
2378 clean_flist(dirlist, 0, 0);
2381 output_flist(dirlist);