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_varint30(f, l2);
477 write_buf(f, fname + l1, l2);
479 if (first_hlink_ndx >= 0) {
480 write_varint30(f, first_hlink_ndx);
484 write_varlong30(f, F_LENGTH(file), 3);
485 if (!(flags & XMIT_SAME_TIME)) {
486 if (protocol_version >= 30)
487 write_varlong(f, modtime, 4);
489 write_int(f, modtime);
491 if (!(flags & XMIT_SAME_MODE))
492 write_int(f, to_wire_mode(mode));
493 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
494 if (protocol_version < 30)
497 write_varint(f, uid);
498 if (flags & XMIT_USER_NAME_FOLLOWS) {
499 int len = strlen(user_name);
501 write_buf(f, user_name, len);
505 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
506 if (protocol_version < 30)
509 write_varint(f, gid);
510 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
511 int len = strlen(group_name);
513 write_buf(f, group_name, len);
517 if ((preserve_devices && IS_DEVICE(mode))
518 || (preserve_specials && IS_SPECIAL(mode))) {
519 if (protocol_version < 28) {
520 if (!(flags & XMIT_SAME_RDEV_pre28))
521 write_int(f, (int)rdev);
523 if (!(flags & XMIT_SAME_RDEV_MAJOR))
524 write_varint30(f, major(rdev));
525 if (protocol_version >= 30)
526 write_varint(f, minor(rdev));
527 else if (flags & XMIT_RDEV_MINOR_8_pre30)
528 write_byte(f, minor(rdev));
530 write_int(f, minor(rdev));
535 if (preserve_links && S_ISLNK(mode)) {
536 const char *sl = F_SYMLINK(file);
537 int len = strlen(sl);
538 write_varint30(f, len);
539 write_buf(f, sl, len);
543 #ifdef SUPPORT_HARD_LINKS
544 if (tmp_dev != 0 && protocol_version < 30) {
545 if (protocol_version < 26) {
546 /* 32-bit dev_t and ino_t */
547 write_int(f, (int32)dev);
548 write_int(f, (int32)tmp_ino);
550 /* 64-bit dev_t and ino_t */
551 if (!(flags & XMIT_SAME_DEV_pre30))
552 write_longint(f, dev);
553 write_longint(f, tmp_ino);
558 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
563 /* Prior to 28, we sent a useless set of nulls. */
566 write_buf(f, sum, checksum_len);
570 strlcpy(lastname, fname, MAXPATHLEN);
572 if (S_ISREG(mode) || S_ISLNK(mode))
573 stats.total_size += F_LENGTH(file);
576 static struct file_struct *recv_file_entry(struct file_list *flist,
579 static time_t modtime;
583 static uint32 rdev_major;
586 static char lastname[MAXPATHLEN], *lastdir;
587 static int lastdir_depth, lastdir_len = -1;
588 static unsigned int del_hier_name_len = 0;
589 static int in_del_hier = 0;
590 char thisname[MAXPATHLEN];
591 unsigned int l1 = 0, l2 = 0;
592 int alloc_len, basename_len, linkname_len;
593 int extra_len = file_extra_cnt * EXTRA_LEN;
594 int first_hlink_ndx = -1;
596 const char *basename;
598 struct file_struct *file;
600 if (flags & XMIT_SAME_NAME)
603 if (flags & XMIT_LONG_NAME)
604 l2 = read_varint30(f);
608 if (l2 >= MAXPATHLEN - l1) {
610 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
611 flags, l1, l2, lastname, who_am_i());
612 overflow_exit("recv_file_entry");
615 strlcpy(thisname, lastname, l1 + 1);
616 read_sbuf(f, &thisname[l1], l2);
617 thisname[l1 + l2] = 0;
619 strlcpy(lastname, thisname, MAXPATHLEN);
621 clean_fname(thisname, 0);
624 sanitize_path(thisname, thisname, "", 0, NULL);
626 if ((basename = strrchr(thisname, '/')) != NULL) {
627 int len = basename++ - thisname;
628 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
629 lastdir = new_array(char, len + 1);
630 memcpy(lastdir, thisname, len);
633 lastdir_depth = count_dir_elements(lastdir);
637 basename_len = strlen(basename) + 1; /* count the '\0' */
639 #ifdef SUPPORT_HARD_LINKS
640 if (protocol_version >= 30
641 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
642 struct file_struct *first;
643 first_hlink_ndx = read_varint30(f);
644 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
646 "hard-link reference out of range: %d (%d)\n",
647 first_hlink_ndx, flist->count);
648 exit_cleanup(RERR_PROTOCOL);
650 first = flist->files[first_hlink_ndx];
651 file_length = F_LENGTH(first);
652 modtime = first->modtime;
658 if ((preserve_devices && IS_DEVICE(mode))
659 || (preserve_specials && IS_SPECIAL(mode))) {
660 uint32 *devp = F_RDEV_P(first);
661 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
662 extra_len += 2 * EXTRA_LEN;
664 if (preserve_links && S_ISLNK(mode))
665 linkname_len = strlen(F_SYMLINK(first)) + 1;
672 file_length = read_varlong30(f, 3);
673 if (!(flags & XMIT_SAME_TIME)) {
674 if (protocol_version >= 30)
675 modtime = (time_t)read_varlong(f, 4);
677 modtime = (time_t)read_int(f);
679 if (!(flags & XMIT_SAME_MODE))
680 mode = from_wire_mode(read_int(f));
682 if (chmod_modes && !S_ISLNK(mode))
683 mode = tweak_mode(mode, chmod_modes);
685 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
686 if (protocol_version < 30)
687 uid = (uid_t)read_int(f);
689 uid = (uid_t)read_varint(f);
690 if (flags & XMIT_USER_NAME_FOLLOWS)
691 uid = recv_user_name(f, uid);
692 else if (inc_recurse && am_root && !numeric_ids)
693 uid = match_uid(uid);
696 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
697 if (protocol_version < 30)
698 gid = (gid_t)read_int(f);
700 gid = (gid_t)read_varint(f);
701 if (flags & XMIT_GROUP_NAME_FOLLOWS)
702 gid = recv_group_name(f, gid);
703 else if (inc_recurse && (!am_root || !numeric_ids))
704 gid = match_gid(gid);
708 if ((preserve_devices && IS_DEVICE(mode))
709 || (preserve_specials && IS_SPECIAL(mode))) {
710 if (protocol_version < 28) {
711 if (!(flags & XMIT_SAME_RDEV_pre28))
712 rdev = (dev_t)read_int(f);
715 if (!(flags & XMIT_SAME_RDEV_MAJOR))
716 rdev_major = read_varint30(f);
717 if (protocol_version >= 30)
718 rdev_minor = read_varint(f);
719 else if (flags & XMIT_RDEV_MINOR_8_pre30)
720 rdev_minor = read_byte(f);
722 rdev_minor = read_int(f);
723 rdev = MAKEDEV(rdev_major, rdev_minor);
725 extra_len += 2 * EXTRA_LEN;
727 } else if (protocol_version < 28)
728 rdev = MAKEDEV(0, 0);
731 if (preserve_links && S_ISLNK(mode)) {
732 linkname_len = read_varint30(f) + 1; /* count the '\0' */
733 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
734 rprintf(FERROR, "overflow: linkname_len=%d\n",
736 overflow_exit("recv_file_entry");
743 #ifdef SUPPORT_HARD_LINKS
745 if (preserve_hard_links) {
746 if (protocol_version < 28 && S_ISREG(mode))
747 flags |= XMIT_HLINKED;
748 if (flags & XMIT_HLINKED)
749 extra_len += EXTRA_LEN;
754 /* We need one or two index int32s when we're preserving ACLs. */
756 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
759 if (always_checksum && S_ISREG(mode))
760 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
762 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
763 extra_len += EXTRA_LEN;
765 #if EXTRA_ROUNDING > 0
766 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
767 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
770 if (inc_recurse && S_ISDIR(mode)) {
771 if (one_file_system) {
772 /* Room to save the dir's device for -x */
773 extra_len += 2 * EXTRA_LEN;
778 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
780 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
782 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
784 file = (struct file_struct *)bp;
785 bp += FILE_STRUCT_LEN;
787 memcpy(bp, basename, basename_len);
788 bp += basename_len + linkname_len; /* skip space for symlink too */
790 #ifdef SUPPORT_HARD_LINKS
791 if (flags & XMIT_HLINKED)
792 file->flags |= FLAG_HLINKED;
794 file->modtime = modtime;
795 file->len32 = (uint32)file_length;
796 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
797 file->flags |= FLAG_LENGTH64;
798 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
806 if (basename != thisname) {
807 file->dirname = lastdir;
808 F_DEPTH(file) = lastdir_depth + 1;
813 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
815 if (flags & XMIT_TOP_DIR) {
816 in_del_hier = recurse;
817 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
818 if (relative_paths && del_hier_name_len > 2
819 && lastname[del_hier_name_len-1] == '.'
820 && lastname[del_hier_name_len-2] == '/')
821 del_hier_name_len -= 2;
822 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
823 } else if (in_del_hier) {
824 if (!relative_paths || !del_hier_name_len
825 || (l1 >= del_hier_name_len
826 && lastname[del_hier_name_len] == '/'))
827 file->flags |= FLAG_XFER_DIR;
833 if ((preserve_devices && IS_DEVICE(mode))
834 || (preserve_specials && IS_SPECIAL(mode))) {
835 uint32 *devp = F_RDEV_P(file);
836 DEV_MAJOR(devp) = major(rdev);
837 DEV_MINOR(devp) = minor(rdev);
842 bp = (char*)file->basename + basename_len;
843 if (first_hlink_ndx >= 0) {
844 struct file_struct *first = flist->files[first_hlink_ndx];
845 memcpy(bp, F_SYMLINK(first), linkname_len);
847 read_sbuf(f, bp, linkname_len - 1);
849 sanitize_path(bp, bp, "", lastdir_depth, NULL);
853 #ifdef SUPPORT_HARD_LINKS
854 if (preserve_hard_links && flags & XMIT_HLINKED) {
855 if (protocol_version >= 30) {
856 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
857 ? flist->count : first_hlink_ndx;
859 static int32 cnt = 0;
860 struct idev_node *np;
863 if (protocol_version < 26) {
867 if (!(flags & XMIT_SAME_DEV_pre30))
868 dev = read_longint(f);
869 ino = read_longint(f);
871 np = idev_node(dev, ino);
872 ndx = (int32)(long)np->data - 1;
875 np->data = (void*)(long)cnt;
877 F_HL_GNUM(file) = ndx;
882 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
884 bp = (char*)F_SUM(file);
886 /* Prior to 28, we get a useless set of nulls. */
889 if (first_hlink_ndx >= 0) {
890 struct file_struct *first = flist->files[first_hlink_ndx];
891 memcpy(bp, F_SUM(first), checksum_len);
893 read_buf(f, bp, checksum_len);
897 if (preserve_acls && !S_ISLNK(mode))
898 receive_acl(file, f);
900 #ifdef SUPPORT_XATTRS
902 receive_xattr(file, f );
905 if (S_ISREG(mode) || S_ISLNK(mode))
906 stats.total_size += file_length;
912 * Create a file_struct for a named file by reading its stat()
913 * information and performing extensive checks against global
916 * @return the new file, or NULL if there was an error or this file
917 * should be excluded.
919 * @todo There is a small optimization opportunity here to avoid
920 * stat()ing the file in some circumstances, which has a certain cost.
921 * We are called immediately after doing readdir(), and so we may
922 * already know the d_type of the file. We could for example avoid
923 * statting directories if we're not recursing, but this is not a very
924 * important case. Some systems may not have d_type.
926 struct file_struct *make_file(const char *fname, struct file_list *flist,
927 STRUCT_STAT *stp, int flags, int filter_level)
929 static char *lastdir;
930 static int lastdir_len = -1;
931 struct file_struct *file;
933 char thisname[MAXPATHLEN];
934 char linkname[MAXPATHLEN];
935 int alloc_len, basename_len, linkname_len;
936 int extra_len = file_extra_cnt * EXTRA_LEN;
937 const char *basename;
940 if (strlcpy(thisname, fname, sizeof thisname)
941 >= sizeof thisname - flist_dir_len) {
942 rprintf(FINFO, "skipping overly long name: %s\n", fname);
945 clean_fname(thisname, 0);
947 sanitize_path(thisname, thisname, "", 0, NULL);
949 if (stp && S_ISDIR(stp->st_mode)) {
950 st = *stp; /* Needed for "symlink/." with --relative. */
951 *linkname = '\0'; /* make IBM code checker happy */
952 } else if (readlink_stat(thisname, &st, linkname) != 0) {
953 int save_errno = errno;
954 /* See if file is excluded before reporting an error. */
955 if (filter_level != NO_FILTERS
956 && (is_excluded(thisname, 0, filter_level)
957 || is_excluded(thisname, 1, filter_level))) {
958 if (ignore_perishable && save_errno != ENOENT)
959 non_perishable_cnt++;
962 if (save_errno == ENOENT) {
964 /* Avoid "vanished" error if symlink points nowhere. */
965 if (copy_links && do_lstat(thisname, &st) == 0
966 && S_ISLNK(st.st_mode)) {
967 io_error |= IOERR_GENERAL;
968 rprintf(FERROR, "symlink has no referent: %s\n",
969 full_fname(thisname));
973 enum logcode c = am_daemon && protocol_version < 28
975 io_error |= IOERR_VANISHED;
976 rprintf(c, "file has vanished: %s\n",
977 full_fname(thisname));
980 io_error |= IOERR_GENERAL;
981 rsyserr(FERROR, save_errno, "readlink %s failed",
982 full_fname(thisname));
987 /* backup.c calls us with filter_level set to NO_FILTERS. */
988 if (filter_level == NO_FILTERS)
991 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
992 rprintf(FINFO, "skipping directory %s\n", thisname);
996 /* -x only affects directories because we need to avoid recursing
997 * into a mount-point directory, not to avoid copying a symlinked
998 * file if -L (or similar) was specified. */
999 if (one_file_system && st.st_dev != filesystem_dev
1000 && S_ISDIR(st.st_mode)) {
1001 if (one_file_system > 1) {
1003 rprintf(FINFO, "skipping mount-point dir %s\n",
1008 flags |= FLAG_MOUNT_DIR;
1011 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1012 if (ignore_perishable)
1013 non_perishable_cnt++;
1017 if (lp_ignore_nonreadable(module_id)) {
1018 #ifdef SUPPORT_LINKS
1019 if (!S_ISLNK(st.st_mode))
1021 if (access(thisname, R_OK) != 0)
1027 /* Only divert a directory in the main transfer. */
1028 if (flist && flist->prev && S_ISDIR(st.st_mode)
1029 && flags & FLAG_DIVERT_DIRS) {
1031 /* Room for parent/sibling/next-child info. */
1032 extra_len += 3 * EXTRA_LEN;
1036 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1037 who_am_i(), thisname, filter_level);
1040 if ((basename = strrchr(thisname, '/')) != NULL) {
1041 int len = basename++ - thisname;
1042 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1043 lastdir = new_array(char, len + 1);
1044 memcpy(lastdir, thisname, len);
1045 lastdir[len] = '\0';
1049 basename = thisname;
1050 basename_len = strlen(basename) + 1; /* count the '\0' */
1052 #ifdef SUPPORT_LINKS
1053 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1058 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1059 extra_len += EXTRA_LEN;
1061 #if EXTRA_ROUNDING > 0
1062 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1063 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1066 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1069 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1071 if (!(bp = new_array(char, alloc_len)))
1072 out_of_memory("make_file");
1075 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1077 file = (struct file_struct *)bp;
1078 bp += FILE_STRUCT_LEN;
1080 memcpy(bp, basename, basename_len);
1081 bp += basename_len + linkname_len; /* skip space for symlink too */
1083 #ifdef SUPPORT_HARD_LINKS
1084 if (preserve_hard_links && flist && flist->prev) {
1085 if (protocol_version >= 28
1086 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1087 : S_ISREG(st.st_mode)) {
1088 tmp_dev = st.st_dev;
1089 tmp_ino = st.st_ino;
1095 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1096 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1097 tmp_rdev = st.st_rdev;
1102 file->flags = flags;
1103 file->modtime = st.st_mtime;
1104 file->len32 = (uint32)st.st_size;
1105 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1106 file->flags |= FLAG_LENGTH64;
1107 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1109 file->mode = st.st_mode;
1111 F_OWNER(file) = st.st_uid;
1113 F_GROUP(file) = st.st_gid;
1115 if (basename != thisname)
1116 file->dirname = lastdir;
1118 #ifdef SUPPORT_LINKS
1120 bp = (char*)file->basename + basename_len;
1121 memcpy(bp, linkname, linkname_len);
1125 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1126 file_checksum(thisname, tmp_sum, st.st_size);
1128 F_ROOTDIR(file) = flist_dir;
1130 /* This code is only used by the receiver when it is building
1131 * a list of files for a delete pass. */
1132 if (keep_dirlinks && linkname_len && flist) {
1134 int save_mode = file->mode;
1135 file->mode = S_IFDIR; /* Find a directory with our name. */
1136 if (flist_find(dir_flist, file) >= 0
1137 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1138 file->modtime = st2.st_mtime;
1140 file->mode = st2.st_mode;
1142 F_OWNER(file) = st2.st_uid;
1144 F_GROUP(file) = st2.st_gid;
1146 file->mode = save_mode;
1149 if (basename_len == 0+1)
1152 if (inc_recurse && flist == dir_flist) {
1153 flist_expand(flist);
1154 flist->files[flist->count++] = file;
1160 /* Only called for temporary file_struct entries created by make_file(). */
1161 void unmake_file(struct file_struct *file)
1163 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1164 #if EXTRA_ROUNDING > 0
1165 if (extra_cnt & EXTRA_ROUNDING)
1166 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1168 free(REQ_EXTRA(file, extra_cnt));
1171 static struct file_struct *send_file_name(int f, struct file_list *flist,
1172 char *fname, STRUCT_STAT *stp,
1173 int flags, int filter_flags)
1175 struct file_struct *file;
1176 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1180 file = make_file(fname, flist, stp, flags, filter_flags);
1184 if (chmod_modes && !S_ISLNK(file->mode))
1185 file->mode = tweak_mode(file->mode, chmod_modes);
1188 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1189 sx.st.st_mode = file->mode;
1190 sx.acc_acl = sx.def_acl = NULL;
1191 if (get_acl(fname, &sx) < 0)
1195 #ifdef SUPPORT_XATTRS
1196 if (preserve_xattrs && f >= 0) {
1198 if (get_xattr(fname, &sx) < 0)
1203 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1205 flist_expand(flist);
1206 flist->files[flist->count++] = file;
1208 send_file_entry(f, file, flist->count - 1);
1210 if (preserve_acls && !S_ISLNK(file->mode)) {
1215 #ifdef SUPPORT_XATTRS
1216 if (preserve_xattrs) {
1217 F_XATTR(file) = send_xattr(&sx, f);
1225 static void send_if_directory(int f, struct file_list *flist,
1226 struct file_struct *file,
1227 char *fbuf, unsigned int ol,
1230 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1232 if (S_ISDIR(file->mode)
1233 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1235 unsigned int len = strlen(fbuf);
1236 if (len > 1 && fbuf[len-1] == '/')
1238 if (len >= MAXPATHLEN - 1) {
1239 io_error |= IOERR_GENERAL;
1240 rprintf(FERROR, "skipping long-named directory: %s\n",
1244 save_filters = push_local_filters(fbuf, len);
1245 send_directory(f, flist, -1, fbuf, len, flags);
1246 pop_local_filters(save_filters);
1253 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1255 return f_name_cmp(*file1, *file2);
1258 /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1259 * sort them by name, and link them into the tree, setting the appropriate
1260 * parent/child/sibling pointers. */
1261 static void add_dirs_to_tree(int parent_ndx, int start, int end)
1265 int32 *parent_dp = parent_ndx < 0 ? NULL
1266 : F_DIRNODE_P(dir_flist->files[parent_ndx]);
1268 qsort(dir_flist->files + start, end - start + 1,
1269 sizeof dir_flist->files[0], (int (*)())file_compare);
1271 for (i = start; i <= end; i++) {
1272 struct file_struct *file = dir_flist->files[i];
1273 if (!(file->flags & FLAG_XFER_DIR)
1274 || file->flags & FLAG_MOUNT_DIR)
1277 DIR_NEXT_SIBLING(dp) = i;
1279 DIR_FIRST_CHILD(parent_dp) = i;
1282 dp = F_DIRNODE_P(file);
1283 DIR_PARENT(dp) = parent_ndx;
1284 DIR_FIRST_CHILD(dp) = -1;
1287 DIR_NEXT_SIBLING(dp) = -1;
1290 /* This function is normally called by the sender, but the receiving side also
1291 * calls it from get_dirlist() with f set to -1 so that we just construct the
1292 * file list in memory without sending it over the wire. Also, get_dirlist()
1293 * might call this with f set to -2, which also indicates that local filter
1294 * rules should be ignored. */
1295 static void send_directory(int f, struct file_list *flist, int parent_ndx,
1296 char *fbuf, int len, int flags)
1302 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1303 int start = divert_dirs ? dir_flist->count : flist->count;
1304 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1306 assert(flist != NULL);
1308 if (!(d = opendir(fbuf))) {
1309 io_error |= IOERR_GENERAL;
1310 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1315 if (len != 1 || *fbuf != '/')
1318 remainder = MAXPATHLEN - (p - fbuf);
1320 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1321 char *dname = d_name(di);
1322 if (dname[0] == '.' && (dname[1] == '\0'
1323 || (dname[1] == '.' && dname[2] == '\0')))
1325 if (strlcpy(p, dname, remainder) >= remainder) {
1326 io_error |= IOERR_GENERAL;
1328 "cannot send long-named file %s\n",
1333 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
1339 io_error |= IOERR_GENERAL;
1340 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1349 add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1);
1351 int i, end = flist->count - 1;
1352 /* send_if_directory() bumps flist->count, so use "end". */
1353 for (i = start; i <= end; i++)
1354 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1358 void send_extra_file_list(int f, int at_least)
1360 char fbuf[MAXPATHLEN];
1361 struct file_list *flist;
1363 int future_cnt, save_io_error = io_error;
1368 /* Keep sending data until we have the requested number of
1369 * files in the upcoming file-lists. */
1370 if (cur_flist->next) {
1371 flist = first_flist->prev; /* the newest flist */
1372 future_cnt = flist->count
1373 + flist->ndx_start - cur_flist->next->ndx_start;
1376 while (future_cnt < at_least) {
1377 struct file_struct *file = dir_flist->files[send_dir_ndx];
1382 dlen = strlen(fbuf);
1384 if (F_ROOTDIR(file) != flist_dir) {
1385 if (!push_flist_dir(F_ROOTDIR(file), -1))
1386 exit_cleanup(RERR_FILESELECT);
1389 flist = flist_new(0, "send_extra_file_list");
1390 start_write = stats.total_written;
1392 write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx);
1393 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1394 send_directory(f, flist, send_dir_ndx, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1397 clean_flist(flist, 0, 0);
1398 file_total += flist->count;
1399 future_cnt += flist->count;
1400 stats.flist_size += stats.total_written - start_write;
1401 stats.num_files += flist->count;
1403 output_flist(flist);
1405 dp = F_DIRNODE_P(file);
1406 if (DIR_FIRST_CHILD(dp) >= 0) {
1407 send_dir_ndx = DIR_FIRST_CHILD(dp);
1410 while (DIR_NEXT_SIBLING(dp) < 0) {
1411 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1412 write_ndx(f, NDX_FLIST_EOF);
1414 change_local_filter_dir(NULL, 0, 0);
1418 file = dir_flist->files[send_dir_ndx];
1419 dp = F_DIRNODE_P(file);
1421 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1426 if (io_error != save_io_error && !ignore_errors)
1427 send_msg_int(MSG_IO_ERROR, io_error);
1430 struct file_list *send_file_list(int f, int argc, char *argv[])
1435 char lastpath[MAXPATHLEN] = "";
1436 struct file_list *flist;
1437 struct timeval start_tv, end_tv;
1440 int flags, disable_buffering;
1442 rprintf(FLOG, "building file list\n");
1443 if (show_filelist_p())
1444 start_filelist_progress("building file list");
1445 else if (inc_recurse && verbose && !am_server)
1446 rprintf(FCLIENT, "sending incremental file list\n");
1448 start_write = stats.total_written;
1449 gettimeofday(&start_tv, NULL);
1451 #ifdef SUPPORT_HARD_LINKS
1452 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1456 flist = cur_flist = flist_new(0, "send_file_list");
1458 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1459 flags = FLAG_DIVERT_DIRS;
1461 dir_flist = cur_flist;
1465 disable_buffering = io_start_buffering_out(f);
1466 if (filesfrom_fd >= 0) {
1467 if (argv[0] && !push_dir(argv[0], 0)) {
1468 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1469 full_fname(argv[0]), curr_dir);
1470 exit_cleanup(RERR_FILESELECT);
1476 char fbuf[MAXPATHLEN];
1481 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1483 sanitize_path(fbuf, fbuf, "", 0, NULL);
1487 strlcpy(fbuf, *argv++, MAXPATHLEN);
1489 sanitize_path(fbuf, fbuf, "", 0, NULL);
1493 if (relative_paths) {
1494 /* We clean up fbuf below. */
1496 } else if (!len || fbuf[len - 1] == '/') {
1497 if (len == 2 && fbuf[0] == '.') {
1498 /* Turn "./" into just "." rather than "./." */
1501 if (len + 1 >= MAXPATHLEN)
1502 overflow_exit("send_file_list");
1507 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1508 && (len == 2 || fbuf[len-3] == '/')) {
1509 if (len + 2 >= MAXPATHLEN)
1510 overflow_exit("send_file_list");
1516 is_dot_dir = fbuf[len-1] == '.'
1517 && (len == 1 || fbuf[len-2] == '/');
1522 if (!relative_paths) {
1523 p = strrchr(fbuf, '/');
1530 len -= p - fbuf + 1;
1535 if ((p = strstr(fbuf, "/./")) != NULL) {
1541 len -= p - fbuf + 3;
1545 /* Get rid of trailing "/" and "/.". */
1547 if (fn[len - 1] == '/') {
1549 if (!--len && !dir) {
1554 else if (len >= 2 && fn[len - 1] == '.'
1555 && fn[len - 2] == '/') {
1557 if (!(len -= 2) && !dir) {
1564 if (len == 1 && fn[0] == '/')
1567 /* Reject a ".." dir in the active part of the path. */
1568 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1569 if ((p[2] == '/' || p[2] == '\0')
1570 && (p == fn || p[-1] == '/')) {
1572 "found \"..\" dir in relative path: %s\n",
1574 exit_cleanup(RERR_SYNTAX);
1585 static const char *lastdir;
1586 static int lastdir_len = -1;
1587 int len = strlen(dir);
1589 if (len != lastdir_len || memcmp(lastdir, dir, len) != 0) {
1590 if (!push_flist_dir(strdup(dir), len))
1592 lastdir = flist_dir;
1593 lastdir_len = flist_dir_len;
1594 } else if (!push_flist_dir(lastdir, lastdir_len)) {
1596 io_error |= IOERR_GENERAL;
1597 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1598 full_fname(dir), curr_dir);
1604 memmove(fbuf, fn, len + 1);
1606 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1607 io_error |= IOERR_GENERAL;
1608 rsyserr(FERROR, errno, "link_stat %s failed",
1613 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1614 rprintf(FINFO, "skipping directory %s\n", fbuf);
1618 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1619 /* Send the implied directories at the start of the
1620 * source spec, so we get their permissions right. */
1621 char *lp = lastpath, *slash = fbuf;
1623 /* Skip any initial directories in our path that we
1624 * have in common with lastpath. */
1625 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1630 if (fn != p || (*lp && *lp != '/')) {
1631 int save_copy_links = copy_links;
1632 int save_xfer_dirs = xfer_dirs;
1633 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1634 copy_links |= copy_unsafe_links;
1636 while ((slash = strchr(slash+1, '/')) != 0) {
1638 send_file_name(f, flist, fbuf, NULL,
1639 dir_flags, ALL_FILTERS);
1642 copy_links = save_copy_links;
1643 xfer_dirs = save_xfer_dirs;
1645 strlcpy(lastpath, fbuf, sizeof lastpath);
1650 if (one_file_system)
1651 filesystem_dev = st.st_dev;
1653 if (recurse || (xfer_dirs && is_dot_dir)) {
1654 struct file_struct *file;
1655 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR
1656 | (is_dot_dir ? 0 : flags)
1657 | (inc_recurse ? FLAG_DIVERT_DIRS : 0);
1658 file = send_file_name(f, flist, fbuf, &st,
1659 top_flags, ALL_FILTERS);
1660 if (file && !inc_recurse)
1661 send_if_directory(f, flist, file, fbuf, len, flags);
1663 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1666 gettimeofday(&end_tv, NULL);
1667 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1668 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1669 if (stats.flist_buildtime == 0)
1670 stats.flist_buildtime = 1;
1673 write_byte(f, 0); /* Indicate end of file list */
1675 #ifdef SUPPORT_HARD_LINKS
1676 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1680 if (show_filelist_p())
1681 finish_filelist_progress(flist);
1683 gettimeofday(&end_tv, NULL);
1684 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1685 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1687 /* Sort the list without removing any duplicates in non-incremental
1688 * mode. This allows the receiving side to ask for whatever name it
1689 * kept. For incremental mode, the sender also removes duplicates
1690 * in this initial file-list so that it avoids re-sending duplicated
1692 clean_flist(flist, 0, inc_recurse);
1693 file_total += flist->count;
1695 if (!numeric_ids && !inc_recurse)
1698 /* send the io_error flag */
1699 if (protocol_version < 30)
1700 write_int(f, ignore_errors ? 0 : io_error);
1701 else if (io_error && !ignore_errors)
1702 send_msg_int(MSG_IO_ERROR, io_error);
1704 if (disable_buffering)
1705 io_end_buffering_out();
1707 stats.flist_size = stats.total_written - start_write;
1708 stats.num_files = flist->count;
1711 output_flist(flist);
1714 rprintf(FINFO, "send_file_list done\n");
1717 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
1718 if (send_dir_ndx < 0) {
1719 write_ndx(f, NDX_FLIST_EOF);
1722 else if (file_total == 1) {
1723 /* If we're creating incremental file-lists and there
1724 * was just 1 item in the first file-list, send 1 more
1725 * file-list to check if this is a 1-file xfer. */
1726 send_extra_file_list(f, 1);
1733 struct file_list *recv_file_list(int f)
1735 struct file_list *flist;
1740 rprintf(FLOG, "receiving file list\n");
1741 if (show_filelist_p())
1742 start_filelist_progress("receiving file list");
1743 else if (inc_recurse && verbose && !am_server && !first_flist)
1744 rprintf(FCLIENT, "receiving incremental file list\n");
1746 start_read = stats.total_read;
1748 flist = flist_new(0, "recv_file_list");
1750 #ifdef SUPPORT_HARD_LINKS
1751 if (preserve_hard_links && protocol_version < 30)
1756 if (flist->ndx_start == 0)
1757 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1758 dstart = dir_flist->count;
1764 while ((flags = read_byte(f)) != 0) {
1765 struct file_struct *file;
1767 flist_expand(flist);
1769 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1770 flags |= read_byte(f) << 8;
1771 file = recv_file_entry(flist, flags, f);
1773 if (inc_recurse && S_ISDIR(file->mode)) {
1774 flist_expand(dir_flist);
1775 dir_flist->files[dir_flist->count++] = file;
1778 flist->files[flist->count++] = file;
1780 maybe_emit_filelist_progress(flist->count);
1783 rprintf(FINFO, "recv_file_name(%s)\n",
1784 f_name(file, NULL));
1787 file_total += flist->count;
1790 rprintf(FINFO, "received %d names\n", flist->count);
1792 if (show_filelist_p())
1793 finish_filelist_progress(flist);
1795 clean_flist(flist, relative_paths, 1);
1798 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
1799 sizeof dir_flist->files[0], (int (*)())file_compare);
1801 recv_uid_list(f, flist);
1803 if (protocol_version < 30) {
1804 /* Recv the io_error flag */
1808 io_error |= read_int(f);
1812 output_flist(flist);
1816 for (i = 0; i < flist->count; i++)
1817 list_file_entry(flist->files[i]);
1821 rprintf(FINFO, "recv_file_list done\n");
1823 stats.flist_size += stats.total_read - start_read;
1824 stats.num_files += flist->count;
1829 /* This is only used once by the receiver if the very first file-list
1830 * has exactly one item in it. */
1831 void recv_additional_file_list(int f)
1833 struct file_list *flist;
1834 int ndx = read_ndx(f);
1835 if (ndx == NDX_FLIST_EOF) {
1837 change_local_filter_dir(NULL, 0, 0);
1839 ndx = NDX_FLIST_OFFSET - ndx;
1840 if (ndx < 0 || ndx >= dir_flist->count) {
1841 ndx = NDX_FLIST_OFFSET - ndx;
1843 "Invalid dir index: %d (%d - %d)\n",
1844 ndx, NDX_FLIST_OFFSET,
1845 NDX_FLIST_OFFSET - dir_flist->count);
1846 exit_cleanup(RERR_PROTOCOL);
1848 flist = recv_file_list(f);
1849 flist->parent_ndx = ndx;
1853 /* Search for an identically-named item in the file list. Note that the
1854 * items must agree in their directory-ness, or no match is returned. */
1855 int flist_find(struct file_list *flist, struct file_struct *f)
1857 int low = flist->low, high = flist->high;
1858 int diff, mid, mid_up;
1860 while (low <= high) {
1861 mid = (low + high) / 2;
1862 if (F_IS_ACTIVE(flist->files[mid]))
1865 /* Scan for the next non-empty entry using the cached
1866 * distance values. If the value isn't fully up-to-
1867 * date, update it. */
1868 mid_up = mid + F_DEPTH(flist->files[mid]);
1869 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1871 mid_up += F_DEPTH(flist->files[mid_up]);
1872 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1873 F_DEPTH(flist->files[mid]) = mid_up - mid;
1875 if (mid_up > high) {
1876 /* If there's nothing left above us, set high to
1877 * a non-empty entry below us and continue. */
1878 high = mid - (int)flist->files[mid]->len32;
1879 if (!F_IS_ACTIVE(flist->files[high])) {
1881 high -= (int)flist->files[high]->len32;
1882 } while (!F_IS_ACTIVE(flist->files[high]));
1883 flist->files[mid]->len32 = mid - high;
1888 diff = f_name_cmp(flist->files[mid_up], f);
1890 if (protocol_version < 29
1891 && S_ISDIR(flist->files[mid_up]->mode)
1892 != S_ISDIR(f->mode))
1905 * Free up any resources a file_struct has allocated
1906 * and clear the file.
1908 void clear_file(struct file_struct *file)
1910 /* The +1 zeros out the first char of the basename. */
1911 memset(file, 0, FILE_STRUCT_LEN + 1);
1912 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1913 * entry. Likewise for len32 in the opposite direction. We assume
1914 * that we're alone for now since flist_find() will adjust the counts
1915 * it runs into that aren't up-to-date. */
1916 file->len32 = F_DEPTH(file) = 1;
1919 /* Allocate a new file list. */
1920 struct file_list *flist_new(int flags, char *msg)
1922 struct file_list *flist;
1924 flist = new(struct file_list);
1928 memset(flist, 0, sizeof flist[0]);
1930 if (!(flags & FLIST_TEMP)) {
1932 flist->ndx_start = first_flist->prev->ndx_start
1933 + first_flist->prev->count;
1935 /* This is a doubly linked list with prev looping back to
1936 * the end of the list, but the last next pointer is NULL. */
1938 first_flist = cur_flist = flist->prev = flist;
1940 flist->prev = first_flist->prev;
1941 flist->prev->next = first_flist->prev = flist;
1946 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1952 /* Free up all elements in a flist. */
1953 void flist_free(struct file_list *flist)
1956 ; /* Was FLIST_TEMP dir-list. */
1957 else if (flist == flist->prev) {
1958 first_flist = cur_flist = NULL;
1962 if (flist == cur_flist)
1963 cur_flist = flist->next;
1964 if (flist == first_flist)
1965 first_flist = first_flist->next;
1967 flist->prev->next = flist->next;
1969 flist->next = first_flist;
1971 flist->next->prev = flist->prev;
1972 file_total -= flist->count;
1976 pool_destroy(flist->file_pool);
1982 * This routine ensures we don't have any duplicate names in our file list.
1983 * duplicate names can cause corruption because of the pipelining
1985 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1987 char fbuf[MAXPATHLEN];
1992 if (flist->count == 0) {
1997 qsort(flist->files, flist->count,
1998 sizeof flist->files[0], (int (*)())file_compare);
2000 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
2001 if (F_IS_ACTIVE(flist->files[i])) {
2006 flist->low = prev_i;
2007 while (++i < flist->count) {
2009 struct file_struct *file = flist->files[i];
2011 if (!F_IS_ACTIVE(file))
2013 if (f_name_cmp(file, flist->files[prev_i]) == 0)
2015 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2016 int save_mode = file->mode;
2017 /* Make sure that this directory doesn't duplicate a
2018 * non-directory earlier in the list. */
2019 flist->high = prev_i;
2020 file->mode = S_IFREG;
2021 j = flist_find(flist, file);
2022 file->mode = save_mode;
2026 struct file_struct *fp = flist->files[j];
2028 /* If one is a dir and the other is not, we want to
2029 * keep the dir because it might have contents in the
2031 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2032 if (S_ISDIR(file->mode))
2036 } else if (protocol_version < 27)
2040 if (verbose > 1 && !am_server) {
2042 "removing duplicate name %s from file list (%d)\n",
2043 f_name(file, fbuf), drop);
2045 /* Make sure we don't lose track of a user-specified
2047 flist->files[keep]->flags |= flist->files[drop]->flags
2048 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2050 clear_file(flist->files[drop]);
2053 if (flist->low == drop) {
2055 j < i && !F_IS_ACTIVE(flist->files[j]);
2064 flist->high = no_dups ? prev_i : flist->count - 1;
2067 /* We need to strip off the leading slashes for relative
2068 * paths, but this must be done _after_ the sorting phase. */
2069 for (i = flist->low; i <= flist->high; i++) {
2070 struct file_struct *file = flist->files[i];
2074 while (*file->dirname == '/')
2076 if (!*file->dirname)
2077 file->dirname = NULL;
2081 if (prune_empty_dirs && no_dups) {
2082 int j, prev_depth = 0;
2084 prev_i = 0; /* It's OK that this isn't really true. */
2086 for (i = flist->low; i <= flist->high; i++) {
2087 struct file_struct *fp, *file = flist->files[i];
2089 /* This temporarily abuses the F_DEPTH() value for a
2090 * directory that is in a chain that might get pruned.
2091 * We restore the old value if it gets a reprieve. */
2092 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2093 /* Dump empty dirs when coming back down. */
2094 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2095 fp = flist->files[prev_i];
2096 if (F_DEPTH(fp) >= 0)
2098 prev_i = -F_DEPTH(fp)-1;
2101 prev_depth = F_DEPTH(file);
2102 if (is_excluded(f_name(file, fbuf), 1,
2104 /* Keep dirs through this dir. */
2105 for (j = prev_depth-1; ; j--) {
2106 fp = flist->files[prev_i];
2107 if (F_DEPTH(fp) >= 0)
2109 prev_i = -F_DEPTH(fp)-1;
2113 F_DEPTH(file) = -prev_i-1;
2116 /* Keep dirs through this non-dir. */
2117 for (j = prev_depth; ; j--) {
2118 fp = flist->files[prev_i];
2119 if (F_DEPTH(fp) >= 0)
2121 prev_i = -F_DEPTH(fp)-1;
2126 /* Dump all remaining empty dirs. */
2128 struct file_struct *fp = flist->files[prev_i];
2129 if (F_DEPTH(fp) >= 0)
2131 prev_i = -F_DEPTH(fp)-1;
2135 for (i = flist->low; i <= flist->high; i++) {
2136 if (F_IS_ACTIVE(flist->files[i]))
2140 for (i = flist->high; i >= flist->low; i--) {
2141 if (F_IS_ACTIVE(flist->files[i]))
2148 static void output_flist(struct file_list *flist)
2150 char uidbuf[16], gidbuf[16], depthbuf[16];
2151 struct file_struct *file;
2152 const char *root, *dir, *slash, *name, *trail;
2153 const char *who = who_am_i();
2156 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2157 who, flist->ndx_start, flist->count, flist->low, flist->high);
2158 for (i = 0; i < flist->count; i++) {
2159 file = flist->files[i];
2160 if ((am_root || am_sender) && preserve_uid) {
2161 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
2165 if (preserve_gid && F_GID(file) != GID_NONE) {
2166 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
2171 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2172 if (F_IS_ACTIVE(file)) {
2173 root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
2174 if ((dir = file->dirname) == NULL)
2178 name = file->basename;
2179 trail = S_ISDIR(file->mode) ? "/" : "";
2181 root = dir = slash = name = trail = "";
2182 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2183 who, i, root, dir, slash, name, trail, (int)file->mode,
2184 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
2188 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2189 enum fnc_type { t_PATH, t_ITEM };
2191 /* Compare the names of two file_struct entities, similar to how strcmp()
2192 * would do if it were operating on the joined strings.
2194 * Some differences beginning with protocol_version 29: (1) directory names
2195 * are compared with an assumed trailing slash so that they compare in a
2196 * way that would cause them to sort immediately prior to any content they
2197 * may have; (2) a directory of any name compares after a non-directory of
2198 * any name at the same depth; (3) a directory with name "." compares prior
2199 * to anything else. These changes mean that a directory and a non-dir
2200 * with the same name will not compare as equal (protocol_version >= 29).
2202 * The dirname component can be an empty string, but the basename component
2203 * cannot (and never is in the current codebase). The basename component
2204 * may be NULL (for a removed item), in which case it is considered to be
2205 * after any existing item. */
2206 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
2209 const uchar *c1, *c2;
2210 enum fnc_state state1, state2;
2211 enum fnc_type type1, type2;
2212 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2214 if (!f1 || !F_IS_ACTIVE(f1)) {
2215 if (!f2 || !F_IS_ACTIVE(f2))
2219 if (!f2 || !F_IS_ACTIVE(f2))
2222 c1 = (uchar*)f1->dirname;
2223 c2 = (uchar*)f2->dirname;
2227 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2228 c1 = (const uchar*)f1->basename;
2229 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2231 state1 = s_TRAILING;
2240 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2241 c2 = (const uchar*)f2->basename;
2242 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2244 state2 = s_TRAILING;
2254 return type1 == t_PATH ? 1 : -1;
2264 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2265 c1 = (const uchar*)f1->basename;
2266 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2268 state1 = s_TRAILING;
2274 state1 = s_TRAILING;
2275 if (type1 == t_PATH) {
2284 if (*c2 && type1 != type2)
2285 return type1 == t_PATH ? 1 : -1;
2294 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2295 c2 = (const uchar*)f2->basename;
2296 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2298 state2 = s_TRAILING;
2304 state2 = s_TRAILING;
2305 if (type2 == t_PATH) {
2317 return type1 == t_PATH ? 1 : -1;
2319 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2324 char *f_name_buf(void)
2326 static char names[5][MAXPATHLEN];
2327 static unsigned int n;
2329 n = (n + 1) % (sizeof names / sizeof names[0]);
2334 /* Return a copy of the full filename of a flist entry, using the indicated
2335 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2336 * done because we checked the size when creating the file_struct entry.
2338 char *f_name(struct file_struct *f, char *fbuf)
2340 if (!f || !F_IS_ACTIVE(f))
2344 fbuf = f_name_buf();
2347 int len = strlen(f->dirname);
2348 memcpy(fbuf, f->dirname, len);
2350 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2352 strlcpy(fbuf, f->basename, MAXPATHLEN);
2357 /* Do a non-recursive scan of the named directory, possibly ignoring all
2358 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2359 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2360 * buffer (the functions we call will append names onto the end, but the old
2361 * dir value will be restored on exit). */
2362 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2364 struct file_list *dirlist;
2365 char dirbuf[MAXPATHLEN];
2366 int save_recurse = recurse;
2367 int save_xfer_dirs = xfer_dirs;
2370 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2371 if (dlen >= MAXPATHLEN)
2376 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2380 send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0);
2381 xfer_dirs = save_xfer_dirs;
2382 recurse = save_recurse;
2384 flist_count_offset += dirlist->count;
2386 clean_flist(dirlist, 0, 0);
2389 output_flist(dirlist);