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-2009 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 as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int always_checksum;
37 extern int ignore_errors;
38 extern int numeric_ids;
42 extern int filesfrom_fd;
43 extern int one_file_system;
44 extern int copy_dirlinks;
45 extern int keep_dirlinks;
46 extern int preserve_uid;
47 extern int preserve_gid;
48 extern int preserve_acls;
49 extern int preserve_xattrs;
50 extern int preserve_links;
51 extern int preserve_hard_links;
52 extern int preserve_devices;
53 extern int preserve_specials;
54 extern int missing_args;
58 extern int relative_paths;
59 extern int implied_dirs;
60 extern int file_extra_cnt;
61 extern int ignore_perishable;
62 extern int non_perishable_cnt;
63 extern int prune_empty_dirs;
64 extern int copy_links;
65 extern int copy_unsafe_links;
66 extern int protocol_version;
67 extern int sanitize_paths;
68 extern int munge_symlinks;
69 extern int need_unsorted_flist;
70 extern int sender_symlink_iconv;
71 extern int output_needs_newline;
72 extern int sender_keeps_checksum;
73 extern int unsort_ndx;
74 extern struct stats stats;
75 extern char *filesfrom_host;
76 extern char *usermap, *groupmap;
78 extern char curr_dir[MAXPATHLEN];
80 extern struct chmod_mode_struct *chmod_modes;
82 extern struct filter_list_struct filter_list;
83 extern struct filter_list_struct daemon_filter_list;
86 extern int filesfrom_convert;
87 extern iconv_t ic_send, ic_recv;
90 #define PTR_SIZE (sizeof (struct file_struct *))
94 dev_t filesystem_dev; /* used to implement -x */
96 struct file_list *cur_flist, *first_flist, *dir_flist;
97 int send_dir_ndx = -1, send_dir_depth = -1;
98 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
99 int file_total = 0; /* total of all active items over all file-lists */
100 int flist_eof = 0; /* all the file-lists are now known */
102 #define NORMAL_NAME 0
103 #define SLASH_ENDING_NAME 1
104 #define DOTDIR_NAME 2
106 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
107 * internally, even if this platform does not allow files to have 64-bit inums.
108 * The only exception is if we're on a platform with no 64-bit type at all.
110 * Because we use read_longint() to get these off the wire, if you transfer
111 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
112 * machine with no 64-bit types then you will get an overflow error.
114 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
115 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
116 * be truncated. But it's a kind of silly thing to do anyhow. */
118 /* The tmp_* vars are used as a cache area by make_file() to store data
119 * that the sender doesn't need to remember in its file list. The data
120 * will survive just long enough to be used by send_file_entry(). */
121 static dev_t tmp_rdev;
122 #ifdef SUPPORT_HARD_LINKS
123 static int64 tmp_dev, tmp_ino;
125 static char tmp_sum[MAX_DIGEST_LEN];
127 static char empty_sum[MAX_DIGEST_LEN];
128 static int flist_count_offset; /* for --delete --progress */
130 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
131 static void output_flist(struct file_list *flist);
133 void init_flist(void)
135 if (DEBUG_GTE(FLIST, 4)) {
136 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
137 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
139 checksum_len = protocol_version < 21 ? 2
140 : protocol_version < 30 ? MD4_DIGEST_LEN
144 static int show_filelist_p(void)
146 return INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
149 static void start_filelist_progress(char *kind)
151 rprintf(FCLIENT, "%s ... ", kind);
152 output_needs_newline = 1;
156 static void emit_filelist_progress(int count)
158 rprintf(FCLIENT, " %d files...\r", count);
161 static void maybe_emit_filelist_progress(int count)
163 if (INFO_GTE(FLIST, 2) && show_filelist_p() && (count % 100) == 0)
164 emit_filelist_progress(count);
167 static void finish_filelist_progress(const struct file_list *flist)
169 if (INFO_GTE(FLIST, 2)) {
170 /* This overwrites the progress line */
171 rprintf(FINFO, "%d file%sto consider\n",
172 flist->used, flist->used == 1 ? " " : "s ");
174 output_needs_newline = 0;
175 rprintf(FINFO, "done\n");
179 void show_flist_stats(void)
184 /* Stat either a symlink or its referent, depending on the settings of
185 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
187 * If path is the name of a symlink, then the linkbuf buffer (which must hold
188 * MAXPATHLEN chars) will be set to the symlink's target string.
190 * The stat structure pointed to by stp will contain information about the
191 * link or the referent as appropriate, if they exist. */
192 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
195 if (link_stat(path, stp, copy_dirlinks) < 0)
197 if (S_ISLNK(stp->st_mode)) {
198 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
201 linkbuf[llen] = '\0';
202 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
203 if (INFO_GTE(SYMSAFE, 1)) {
204 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
207 return x_stat(path, stp, NULL);
209 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
210 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
211 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
212 llen - SYMLINK_PREFIX_LEN + 1);
217 return x_stat(path, stp, NULL);
221 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
225 return x_stat(path, stp, NULL);
226 if (x_lstat(path, stp, NULL) < 0)
228 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
230 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
235 return x_stat(path, stp, NULL);
239 static inline int is_daemon_excluded(const char *fname, int is_dir)
241 if (daemon_filter_list.head
242 && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
249 static inline int path_is_daemon_excluded(char *path, int ignore_filename)
251 if (daemon_filter_list.head) {
254 while ((slash = strchr(slash+1, '/')) != NULL) {
257 ret = check_filter(&daemon_filter_list, FLOG, path, 1);
266 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
275 /* This function is used to check if a file should be included/excluded
276 * from the list of files based on its name and type etc. The value of
277 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
278 static int is_excluded(const char *fname, int is_dir, int filter_level)
280 #if 0 /* This currently never happens, so avoid a useless compare. */
281 if (filter_level == NO_FILTERS)
284 if (is_daemon_excluded(fname, is_dir))
286 if (filter_level != ALL_FILTERS)
289 && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
294 static void send_directory(int f, struct file_list *flist,
295 char *fbuf, int len, int flags);
297 static const char *pathname, *orig_dir;
298 static int pathname_len;
300 /* Make sure flist can hold at least flist->used + extra entries. */
301 static void flist_expand(struct file_list *flist, int extra)
303 struct file_struct **new_ptr;
305 if (flist->used + extra <= flist->malloced)
308 if (flist->malloced < FLIST_START)
309 flist->malloced = FLIST_START;
310 else if (flist->malloced >= FLIST_LINEAR)
311 flist->malloced += FLIST_LINEAR;
313 flist->malloced *= 2;
315 /* In case count jumped or we are starting the list
316 * with a known size just set it. */
317 if (flist->malloced < flist->used + extra)
318 flist->malloced = flist->used + extra;
320 new_ptr = realloc_array(flist->files, struct file_struct *,
323 if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
324 rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
326 big_num(sizeof flist->files[0] * flist->malloced),
327 (new_ptr == flist->files) ? " not" : "");
330 flist->files = new_ptr;
333 out_of_memory("flist_expand");
336 static void flist_done_allocating(struct file_list *flist)
338 void *ptr = pool_boundary(flist->file_pool, 8*1024);
339 if (flist->pool_boundary == ptr)
340 flist->pool_boundary = NULL; /* list didn't use any pool memory */
342 flist->pool_boundary = ptr;
345 /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
346 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
347 * with dir == NULL taken to be the starting directory, and dirlen < 0
348 * indicating that strdup(dir) should be called and then the -dirlen length
349 * value checked to ensure that it is not daemon-excluded. */
350 int change_pathname(struct file_struct *file, const char *dir, int dirlen)
353 char *cpy = strdup(dir);
355 change_dir(orig_dir, CD_SKIP_CHDIR);
356 if (path_is_daemon_excluded(cpy, 0))
362 if (pathname == F_PATHNAME(file))
364 dir = F_PATHNAME(file);
366 dirlen = strlen(dir);
367 } else if (pathname == dir)
369 if (dir && *dir != '/')
370 change_dir(orig_dir, CD_SKIP_CHDIR);
374 pathname_len = dirlen;
379 if (!change_dir(dir, CD_NORMAL)) {
381 io_error |= IOERR_GENERAL;
382 rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
384 change_dir(orig_dir, CD_NORMAL);
393 static void send_file_entry(int f, const char *fname, struct file_struct *file,
395 const char *symlink_name, int symlink_len,
397 int ndx, int first_ndx)
399 static time_t modtime;
401 #ifdef SUPPORT_HARD_LINKS
405 static uint32 rdev_major;
408 static const char *user_name, *group_name;
409 static char lastname[MAXPATHLEN];
410 #ifdef SUPPORT_HARD_LINKS
411 int first_hlink_ndx = -1;
416 /* Initialize starting value of xflags and adjust counts. */
417 if (S_ISREG(file->mode))
419 else if (S_ISDIR(file->mode)) {
421 if (protocol_version >= 30) {
422 if (file->flags & FLAG_CONTENT_DIR)
423 xflags = file->flags & FLAG_TOP_DIR;
424 else if (file->flags & FLAG_IMPLIED_DIR)
425 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
427 xflags = XMIT_NO_CONTENT_DIR;
429 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
431 if (S_ISLNK(file->mode))
432 stats.num_symlinks++;
433 else if (IS_DEVICE(file->mode))
435 else if (IS_SPECIAL(file->mode))
436 stats.num_specials++;
440 if (file->mode == mode)
441 xflags |= XMIT_SAME_MODE;
445 if ((preserve_devices && IS_DEVICE(mode))
446 || (preserve_specials && IS_SPECIAL(mode))) {
447 if (protocol_version < 28) {
448 if (tmp_rdev == rdev)
449 xflags |= XMIT_SAME_RDEV_pre28;
454 if ((uint32)major(rdev) == rdev_major)
455 xflags |= XMIT_SAME_RDEV_MAJOR;
457 rdev_major = major(rdev);
458 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
459 xflags |= XMIT_RDEV_MINOR_8_pre30;
461 } else if (protocol_version < 28)
462 rdev = MAKEDEV(0, 0);
463 if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
464 xflags |= XMIT_SAME_UID;
468 user_name = add_uid(uid);
469 if (inc_recurse && user_name)
470 xflags |= XMIT_USER_NAME_FOLLOWS;
473 if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
474 xflags |= XMIT_SAME_GID;
478 group_name = add_gid(gid);
479 if (inc_recurse && group_name)
480 xflags |= XMIT_GROUP_NAME_FOLLOWS;
483 if (file->modtime == modtime)
484 xflags |= XMIT_SAME_TIME;
486 modtime = file->modtime;
488 #ifdef SUPPORT_HARD_LINKS
490 if (protocol_version >= 30) {
491 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
492 first_hlink_ndx = (int32)(long)np->data - 1;
493 if (first_hlink_ndx < 0) {
494 np->data = (void*)(long)(first_ndx + ndx + 1);
495 xflags |= XMIT_HLINK_FIRST;
497 if (DEBUG_GTE(HLINK, 1)) {
498 if (first_hlink_ndx >= 0) {
499 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
500 who_am_i(), first_ndx + ndx, first_hlink_ndx,
501 first_hlink_ndx >= first_ndx ? "" : "un");
502 } else if (DEBUG_GTE(HLINK, 3)) {
503 rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
504 who_am_i(), first_ndx + ndx,
505 big_num(tmp_dev), big_num(tmp_ino));
509 if (tmp_dev == dev) {
510 if (protocol_version >= 28)
511 xflags |= XMIT_SAME_DEV_pre30;
515 xflags |= XMIT_HLINKED;
520 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
522 l2 = strlen(fname+l1);
525 xflags |= XMIT_SAME_NAME;
527 xflags |= XMIT_LONG_NAME;
529 /* We must make sure we don't send a zero flag byte or the
530 * other end will terminate the flist transfer. Note that
531 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
532 * it's harmless way to add a bit to the first flag byte. */
533 if (protocol_version >= 28) {
534 if (!xflags && !S_ISDIR(mode))
535 xflags |= XMIT_TOP_DIR;
536 if ((xflags & 0xFF00) || !xflags) {
537 xflags |= XMIT_EXTENDED_FLAGS;
538 write_shortint(f, xflags);
540 write_byte(f, xflags);
542 if (!(xflags & 0xFF))
543 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
544 write_byte(f, xflags);
546 if (xflags & XMIT_SAME_NAME)
548 if (xflags & XMIT_LONG_NAME)
549 write_varint30(f, l2);
552 write_buf(f, fname + l1, l2);
554 #ifdef SUPPORT_HARD_LINKS
555 if (first_hlink_ndx >= 0) {
556 write_varint(f, first_hlink_ndx);
557 if (first_hlink_ndx >= first_ndx)
562 write_varlong30(f, F_LENGTH(file), 3);
563 if (!(xflags & XMIT_SAME_TIME)) {
564 if (protocol_version >= 30)
565 write_varlong(f, modtime, 4);
567 write_int(f, modtime);
569 if (!(xflags & XMIT_SAME_MODE))
570 write_int(f, to_wire_mode(mode));
571 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
572 if (protocol_version < 30)
575 write_varint(f, uid);
576 if (xflags & XMIT_USER_NAME_FOLLOWS) {
577 int len = strlen(user_name);
579 write_buf(f, user_name, len);
583 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
584 if (protocol_version < 30)
587 write_varint(f, gid);
588 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
589 int len = strlen(group_name);
591 write_buf(f, group_name, len);
595 if ((preserve_devices && IS_DEVICE(mode))
596 || (preserve_specials && IS_SPECIAL(mode))) {
597 if (protocol_version < 28) {
598 if (!(xflags & XMIT_SAME_RDEV_pre28))
599 write_int(f, (int)rdev);
601 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
602 write_varint30(f, major(rdev));
603 if (protocol_version >= 30)
604 write_varint(f, minor(rdev));
605 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
606 write_byte(f, minor(rdev));
608 write_int(f, minor(rdev));
614 write_varint30(f, symlink_len);
615 write_buf(f, symlink_name, symlink_len);
619 #ifdef SUPPORT_HARD_LINKS
620 if (tmp_dev != 0 && protocol_version < 30) {
621 if (protocol_version < 26) {
622 /* 32-bit dev_t and ino_t */
623 write_int(f, (int32)dev);
624 write_int(f, (int32)tmp_ino);
626 /* 64-bit dev_t and ino_t */
627 if (!(xflags & XMIT_SAME_DEV_pre30))
628 write_longint(f, dev);
629 write_longint(f, tmp_ino);
634 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
639 /* Prior to 28, we sent a useless set of nulls. */
642 write_buf(f, sum, checksum_len);
645 #ifdef SUPPORT_HARD_LINKS
648 strlcpy(lastname, fname, MAXPATHLEN);
650 if (S_ISREG(mode) || S_ISLNK(mode))
651 stats.total_size += F_LENGTH(file);
654 static struct file_struct *recv_file_entry(struct file_list *flist,
657 static int64 modtime;
659 #ifdef SUPPORT_HARD_LINKS
663 static uint32 rdev_major;
666 static uint16 gid_flags;
667 static char lastname[MAXPATHLEN], *lastdir;
668 static int lastdir_depth, lastdir_len = -1;
669 static unsigned int del_hier_name_len = 0;
670 static int in_del_hier = 0;
671 char thisname[MAXPATHLEN];
672 unsigned int l1 = 0, l2 = 0;
673 int alloc_len, basename_len, linkname_len;
674 int extra_len = file_extra_cnt * EXTRA_LEN;
675 int first_hlink_ndx = -1;
677 const char *basename;
678 struct file_struct *file;
682 if (xflags & XMIT_SAME_NAME)
685 if (xflags & XMIT_LONG_NAME)
686 l2 = read_varint30(f);
690 if (l2 >= MAXPATHLEN - l1) {
692 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
693 xflags, l1, l2, lastname, who_am_i());
694 overflow_exit("recv_file_entry");
697 strlcpy(thisname, lastname, l1 + 1);
698 read_sbuf(f, &thisname[l1], l2);
699 thisname[l1 + l2] = 0;
701 /* Abuse basename_len for a moment... */
702 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
705 if (ic_recv != (iconv_t)-1) {
708 INIT_CONST_XBUF(outbuf, thisname);
709 INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
711 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
712 io_error |= IOERR_GENERAL;
714 "[%s] cannot convert filename: %s (%s)\n",
715 who_am_i(), lastname, strerror(errno));
718 thisname[outbuf.len] = '\0';
723 clean_fname(thisname, 0);
726 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
728 if ((basename = strrchr(thisname, '/')) != NULL) {
729 int len = basename++ - thisname;
730 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
731 lastdir = new_array(char, len + 1);
732 memcpy(lastdir, thisname, len);
735 lastdir_depth = count_dir_elements(lastdir);
739 basename_len = strlen(basename) + 1; /* count the '\0' */
741 #ifdef SUPPORT_HARD_LINKS
742 if (protocol_version >= 30
743 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
744 first_hlink_ndx = read_varint(f);
745 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
747 "hard-link reference out of range: %d (%d)\n",
748 first_hlink_ndx, flist->ndx_start + flist->used);
749 exit_cleanup(RERR_PROTOCOL);
751 if (DEBUG_GTE(HLINK, 1)) {
752 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
753 who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
754 first_hlink_ndx >= flist->ndx_start ? "" : "un");
756 if (first_hlink_ndx >= flist->ndx_start) {
757 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
758 file_length = F_LENGTH(first);
759 modtime = first->modtime;
762 uid = F_OWNER(first);
764 gid = F_GROUP(first);
765 if ((preserve_devices && IS_DEVICE(mode))
766 || (preserve_specials && IS_SPECIAL(mode))) {
767 uint32 *devp = F_RDEV_P(first);
768 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
769 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
771 if (preserve_links && S_ISLNK(mode))
772 linkname_len = strlen(F_SYMLINK(first)) + 1;
780 file_length = read_varlong30(f, 3);
781 if (!(xflags & XMIT_SAME_TIME)) {
782 if (protocol_version >= 30) {
783 modtime = read_varlong(f, 4);
784 #if SIZEOF_TIME_T < SIZEOF_INT64
785 if (!am_generator && (int64)(time_t)modtime != modtime) {
787 "Time value of %s truncated on receiver.\n",
792 modtime = read_int(f);
794 if (!(xflags & XMIT_SAME_MODE))
795 mode = from_wire_mode(read_int(f));
797 if (chmod_modes && !S_ISLNK(mode) && mode)
798 mode = tweak_mode(mode, chmod_modes);
800 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
801 if (protocol_version < 30)
802 uid = (uid_t)read_int(f);
804 uid = (uid_t)read_varint(f);
805 if (xflags & XMIT_USER_NAME_FOLLOWS)
806 uid = recv_user_name(f, uid);
807 else if (inc_recurse && am_root && (!numeric_ids || usermap))
808 uid = match_uid(uid);
811 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
812 if (protocol_version < 30)
813 gid = (gid_t)read_int(f);
815 gid = (gid_t)read_varint(f);
817 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
818 gid = recv_group_name(f, gid, &gid_flags);
819 else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
820 gid = match_gid(gid, &gid_flags);
824 if ((preserve_devices && IS_DEVICE(mode))
825 || (preserve_specials && IS_SPECIAL(mode))) {
826 if (protocol_version < 28) {
827 if (!(xflags & XMIT_SAME_RDEV_pre28))
828 rdev = (dev_t)read_int(f);
831 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
832 rdev_major = read_varint30(f);
833 if (protocol_version >= 30)
834 rdev_minor = read_varint(f);
835 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
836 rdev_minor = read_byte(f);
838 rdev_minor = read_int(f);
839 rdev = MAKEDEV(rdev_major, rdev_minor);
841 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
843 } else if (protocol_version < 28)
844 rdev = MAKEDEV(0, 0);
847 if (preserve_links && S_ISLNK(mode)) {
848 linkname_len = read_varint30(f) + 1; /* count the '\0' */
849 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
850 rprintf(FERROR, "overflow: linkname_len=%d\n",
852 overflow_exit("recv_file_entry");
855 /* We don't know how much extra room we need to convert
856 * the as-yet-unread symlink data, so let's hope that a
857 * double-size buffer is plenty. */
858 if (sender_symlink_iconv)
862 linkname_len += SYMLINK_PREFIX_LEN;
868 #ifdef SUPPORT_HARD_LINKS
870 if (preserve_hard_links) {
871 if (protocol_version < 28 && S_ISREG(mode))
872 xflags |= XMIT_HLINKED;
873 if (xflags & XMIT_HLINKED)
874 extra_len += (inc_recurse+1) * EXTRA_LEN;
879 /* Directories need an extra int32 for the default ACL. */
880 if (preserve_acls && S_ISDIR(mode))
881 extra_len += EXTRA_LEN;
884 if (always_checksum && S_ISREG(mode))
885 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
887 #if SIZEOF_INT64 >= 8
888 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
889 extra_len += EXTRA_LEN;
891 if (file_length < 0) {
892 rprintf(FERROR, "Offset underflow: file-length is negative\n");
893 exit_cleanup(RERR_UNSUPPORTED);
896 if (inc_recurse && S_ISDIR(mode)) {
897 if (one_file_system) {
898 /* Room to save the dir's device for -x */
899 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
901 pool = dir_flist->file_pool;
903 pool = flist->file_pool;
905 #if EXTRA_ROUNDING > 0
906 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
907 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
910 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
912 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
914 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
916 file = (struct file_struct *)bp;
917 bp += FILE_STRUCT_LEN;
919 memcpy(bp, basename, basename_len);
921 #ifdef SUPPORT_HARD_LINKS
922 if (xflags & XMIT_HLINKED)
923 file->flags |= FLAG_HLINKED;
925 file->modtime = (time_t)modtime;
926 file->len32 = (uint32)file_length;
927 #if SIZEOF_INT64 >= 8
928 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
929 #if SIZEOF_CAPITAL_OFF_T < 8
930 rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
931 exit_cleanup(RERR_UNSUPPORTED);
933 file->flags |= FLAG_LENGTH64;
934 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
943 file->flags |= gid_flags;
946 F_NDX(file) = flist->used + flist->ndx_start;
948 if (basename != thisname) {
949 file->dirname = lastdir;
950 F_DEPTH(file) = lastdir_depth + 1;
955 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
957 if (protocol_version >= 30) {
958 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
959 if (xflags & XMIT_TOP_DIR)
960 file->flags |= FLAG_TOP_DIR;
961 file->flags |= FLAG_CONTENT_DIR;
962 } else if (xflags & XMIT_TOP_DIR)
963 file->flags |= FLAG_IMPLIED_DIR;
964 } else if (xflags & XMIT_TOP_DIR) {
965 in_del_hier = recurse;
966 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
967 if (relative_paths && del_hier_name_len > 2
968 && lastname[del_hier_name_len-1] == '.'
969 && lastname[del_hier_name_len-2] == '/')
970 del_hier_name_len -= 2;
971 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
972 } else if (in_del_hier) {
973 if (!relative_paths || !del_hier_name_len
974 || (l1 >= del_hier_name_len
975 && lastname[del_hier_name_len] == '/'))
976 file->flags |= FLAG_CONTENT_DIR;
982 if ((preserve_devices && IS_DEVICE(mode))
983 || (preserve_specials && IS_SPECIAL(mode))) {
984 uint32 *devp = F_RDEV_P(file);
985 DEV_MAJOR(devp) = major(rdev);
986 DEV_MINOR(devp) = minor(rdev);
992 if (first_hlink_ndx >= flist->ndx_start) {
993 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
994 memcpy(bp, F_SYMLINK(first), linkname_len);
996 if (munge_symlinks) {
997 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
998 bp += SYMLINK_PREFIX_LEN;
999 linkname_len -= SYMLINK_PREFIX_LEN;
1002 if (sender_symlink_iconv) {
1005 alloc_len = linkname_len;
1008 /* Read the symlink data into the end of our double-sized
1009 * buffer and then convert it into the right spot. */
1010 INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
1011 linkname_len - 1, (size_t)-1);
1012 read_sbuf(f, inbuf.buf, inbuf.len);
1013 INIT_XBUF(outbuf, bp, 0, alloc_len);
1015 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
1016 io_error |= IOERR_GENERAL;
1017 rprintf(FERROR_XFER,
1018 "[%s] cannot convert symlink data for: %s (%s)\n",
1019 who_am_i(), full_fname(thisname), strerror(errno));
1020 bp = (char*)file->basename;
1024 bp[outbuf.len] = '\0';
1027 read_sbuf(f, bp, linkname_len - 1);
1028 if (sanitize_paths && !munge_symlinks && *bp)
1029 sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
1034 #ifdef SUPPORT_HARD_LINKS
1035 if (preserve_hard_links && xflags & XMIT_HLINKED) {
1036 if (protocol_version >= 30) {
1037 if (xflags & XMIT_HLINK_FIRST) {
1038 F_HL_GNUM(file) = flist->ndx_start + flist->used;
1040 F_HL_GNUM(file) = first_hlink_ndx;
1042 static int32 cnt = 0;
1043 struct ht_int64_node *np;
1046 if (protocol_version < 26) {
1050 if (!(xflags & XMIT_SAME_DEV_pre30))
1051 dev = read_longint(f);
1052 ino = read_longint(f);
1054 np = idev_find(dev, ino);
1055 ndx = (int32)(long)np->data - 1;
1058 np->data = (void*)(long)cnt;
1060 F_HL_GNUM(file) = ndx;
1065 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
1069 /* Prior to 28, we get a useless set of nulls. */
1072 if (first_hlink_ndx >= flist->ndx_start) {
1073 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1074 memcpy(bp, F_SUM(first), checksum_len);
1076 read_buf(f, bp, checksum_len);
1080 if (preserve_acls && !S_ISLNK(mode))
1081 receive_acl(file, f);
1083 #ifdef SUPPORT_XATTRS
1084 if (preserve_xattrs)
1085 receive_xattr(file, f );
1088 if (S_ISREG(mode) || S_ISLNK(mode))
1089 stats.total_size += file_length;
1094 /* Create a file_struct for a named file by reading its stat() information
1095 * and performing extensive checks against global options.
1097 * Returns a pointer to the new file struct, or NULL if there was an error
1098 * or this file should be excluded.
1100 * Note: Any error (here or in send_file_name) that results in the omission of
1101 * an existent source file from the file list should set
1102 * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1103 * destination if --delete is on. */
1104 struct file_struct *make_file(const char *fname, struct file_list *flist,
1105 STRUCT_STAT *stp, int flags, int filter_level)
1107 static char *lastdir;
1108 static int lastdir_len = -1;
1109 struct file_struct *file;
1110 char thisname[MAXPATHLEN];
1111 char linkname[MAXPATHLEN];
1112 int alloc_len, basename_len, linkname_len;
1113 int extra_len = file_extra_cnt * EXTRA_LEN;
1114 const char *basename;
1119 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1120 io_error |= IOERR_GENERAL;
1121 rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
1124 clean_fname(thisname, 0);
1126 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
1128 if (stp && (S_ISDIR(stp->st_mode) || stp->st_mode == 0)) {
1129 /* This is needed to handle a "symlink/." with a --relative
1130 * dir, or a request to delete a specific file. */
1132 *linkname = '\0'; /* make IBM code checker happy */
1133 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1134 int save_errno = errno;
1135 /* See if file is excluded before reporting an error. */
1136 if (filter_level != NO_FILTERS
1137 && (is_excluded(thisname, 0, filter_level)
1138 || is_excluded(thisname, 1, filter_level))) {
1139 if (ignore_perishable && save_errno != ENOENT)
1140 non_perishable_cnt++;
1143 if (save_errno == ENOENT) {
1144 #ifdef SUPPORT_LINKS
1145 /* When our options tell us to follow a symlink that
1146 * points nowhere, tell the user about the symlink
1147 * instead of giving a "vanished" message. We only
1148 * dereference a symlink if one of the --copy*links
1149 * options was specified, so there's no need for the
1150 * extra lstat() if one of these options isn't on. */
1151 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1152 && x_lstat(thisname, &st, NULL) == 0
1153 && S_ISLNK(st.st_mode)) {
1154 io_error |= IOERR_GENERAL;
1155 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1156 full_fname(thisname));
1160 enum logcode c = am_daemon && protocol_version < 28
1161 ? FERROR : FWARNING;
1162 io_error |= IOERR_VANISHED;
1163 rprintf(c, "file has vanished: %s\n",
1164 full_fname(thisname));
1167 io_error |= IOERR_GENERAL;
1168 rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
1169 full_fname(thisname));
1172 } else if (st.st_mode == 0) {
1173 io_error |= IOERR_GENERAL;
1174 rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n",
1175 full_fname(thisname));
1179 if (filter_level == NO_FILTERS)
1182 if (S_ISDIR(st.st_mode)) {
1184 rprintf(FINFO, "skipping directory %s\n", thisname);
1187 /* -x only affects dirs because we need to avoid recursing
1188 * into a mount-point directory, not to avoid copying a
1189 * symlinked file if -L (or similar) was specified. */
1190 if (one_file_system && st.st_dev != filesystem_dev
1191 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1192 if (one_file_system > 1) {
1193 if (INFO_GTE(MOUNT, 1)) {
1195 "[%s] skipping mount-point dir %s\n",
1196 who_am_i(), thisname);
1200 flags |= FLAG_MOUNT_DIR;
1201 flags &= ~FLAG_CONTENT_DIR;
1204 flags &= ~FLAG_CONTENT_DIR;
1206 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1207 if (ignore_perishable)
1208 non_perishable_cnt++;
1212 if (lp_ignore_nonreadable(module_id)) {
1213 #ifdef SUPPORT_LINKS
1214 if (!S_ISLNK(st.st_mode))
1216 if (access(thisname, R_OK) != 0)
1222 /* Only divert a directory in the main transfer. */
1224 if (flist->prev && S_ISDIR(st.st_mode)
1225 && flags & FLAG_DIVERT_DIRS) {
1226 /* Room for parent/sibling/next-child info. */
1227 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1229 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1230 pool = dir_flist->file_pool;
1232 pool = flist->file_pool;
1235 /* Directories need an extra int32 for the default ACL. */
1236 if (preserve_acls && S_ISDIR(st.st_mode))
1237 extra_len += EXTRA_LEN;
1242 if (DEBUG_GTE(FLIST, 2)) {
1243 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1244 who_am_i(), thisname, filter_level);
1247 if ((basename = strrchr(thisname, '/')) != NULL) {
1248 int len = basename++ - thisname;
1249 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1250 lastdir = new_array(char, len + 1);
1251 memcpy(lastdir, thisname, len);
1252 lastdir[len] = '\0';
1256 basename = thisname;
1257 basename_len = strlen(basename) + 1; /* count the '\0' */
1259 #ifdef SUPPORT_LINKS
1260 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1265 #if SIZEOF_CAPITAL_OFF_T >= 8
1266 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1267 extra_len += EXTRA_LEN;
1270 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1271 file_checksum(thisname, tmp_sum, st.st_size);
1272 if (sender_keeps_checksum)
1273 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1276 #if EXTRA_ROUNDING > 0
1277 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1278 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1281 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1284 bp = pool_alloc(pool, alloc_len, "make_file");
1286 if (!(bp = new_array(char, alloc_len)))
1287 out_of_memory("make_file");
1290 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1292 file = (struct file_struct *)bp;
1293 bp += FILE_STRUCT_LEN;
1295 memcpy(bp, basename, basename_len);
1297 #ifdef SUPPORT_HARD_LINKS
1298 if (preserve_hard_links && flist && flist->prev) {
1299 if (protocol_version >= 28
1300 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1301 : S_ISREG(st.st_mode)) {
1302 tmp_dev = (int64)st.st_dev + 1;
1303 tmp_ino = (int64)st.st_ino;
1309 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1310 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1311 tmp_rdev = st.st_rdev;
1316 file->flags = flags;
1317 file->modtime = st.st_mtime;
1318 file->len32 = (uint32)st.st_size;
1319 #if SIZEOF_CAPITAL_OFF_T >= 8
1320 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1321 file->flags |= FLAG_LENGTH64;
1322 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1325 file->mode = st.st_mode;
1326 if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
1327 F_OWNER(file) = st.st_uid;
1328 if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
1329 F_GROUP(file) = st.st_gid;
1331 if (basename != thisname)
1332 file->dirname = lastdir;
1334 #ifdef SUPPORT_LINKS
1336 memcpy(bp + basename_len, linkname, linkname_len);
1340 F_PATHNAME(file) = pathname;
1342 F_DEPTH(file) = extra_len / EXTRA_LEN;
1344 if (basename_len == 0+1) {
1350 if (sender_keeps_checksum && S_ISREG(st.st_mode))
1351 memcpy(F_SUM(file), tmp_sum, checksum_len);
1354 F_NDX(file) = stats.num_dirs;
1359 /* Only called for temporary file_struct entries created by make_file(). */
1360 void unmake_file(struct file_struct *file)
1362 free(REQ_EXTRA(file, F_DEPTH(file)));
1365 static struct file_struct *send_file_name(int f, struct file_list *flist,
1366 const char *fname, STRUCT_STAT *stp,
1367 int flags, int filter_level)
1369 struct file_struct *file;
1371 file = make_file(fname, flist, stp, flags, filter_level);
1375 if (chmod_modes && !S_ISLNK(file->mode) && file->mode)
1376 file->mode = tweak_mode(file->mode, chmod_modes);
1379 char fbuf[MAXPATHLEN];
1380 #ifdef SUPPORT_LINKS
1381 const char *symlink_name;
1384 char symlink_buf[MAXPATHLEN];
1387 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1392 #ifdef SUPPORT_LINKS
1393 if (preserve_links && S_ISLNK(file->mode)) {
1394 symlink_name = F_SYMLINK(file);
1395 symlink_len = strlen(symlink_name);
1396 if (symlink_len == 0) {
1397 io_error |= IOERR_GENERAL;
1399 rprintf(FERROR_XFER,
1400 "skipping symlink with 0-length value: %s\n",
1405 symlink_name = NULL;
1411 if (ic_send != (iconv_t)-1) {
1414 INIT_CONST_XBUF(outbuf, fbuf);
1416 if (file->dirname) {
1417 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1418 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1419 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
1422 fbuf[outbuf.len++] = '/';
1425 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1426 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1428 io_error |= IOERR_GENERAL;
1429 rprintf(FERROR_XFER,
1430 "[%s] cannot convert filename: %s (%s)\n",
1431 who_am_i(), f_name(file, fbuf), strerror(errno));
1434 fbuf[outbuf.len] = '\0';
1436 #ifdef SUPPORT_LINKS
1437 if (symlink_len && sender_symlink_iconv) {
1438 INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1439 INIT_CONST_XBUF(outbuf, symlink_buf);
1440 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1441 io_error |= IOERR_GENERAL;
1443 rprintf(FERROR_XFER,
1444 "[%s] cannot convert symlink data for: %s (%s)\n",
1445 who_am_i(), full_fname(fbuf), strerror(errno));
1448 symlink_buf[outbuf.len] = '\0';
1450 symlink_name = symlink_buf;
1451 symlink_len = outbuf.len;
1459 if (preserve_acls && !S_ISLNK(file->mode)) {
1460 sx.st.st_mode = file->mode;
1461 if (get_acl(fname, &sx) < 0) {
1462 io_error |= IOERR_GENERAL;
1467 #ifdef SUPPORT_XATTRS
1468 if (preserve_xattrs) {
1469 if (get_xattr(fname, &sx) < 0) {
1470 io_error |= IOERR_GENERAL;
1476 send_file_entry(f, fbuf, file,
1477 #ifdef SUPPORT_LINKS
1478 symlink_name, symlink_len,
1480 flist->used, flist->ndx_start);
1483 if (preserve_acls && !S_ISLNK(file->mode)) {
1488 #ifdef SUPPORT_XATTRS
1489 if (preserve_xattrs) {
1490 F_XATTR(file) = send_xattr(&sx, f);
1496 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1498 flist_expand(flist, 1);
1499 flist->files[flist->used++] = file;
1504 static void send_if_directory(int f, struct file_list *flist,
1505 struct file_struct *file,
1506 char *fbuf, unsigned int ol,
1509 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1511 if (S_ISDIR(file->mode)
1512 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1514 unsigned int len = strlen(fbuf);
1515 if (len > 1 && fbuf[len-1] == '/')
1517 if (len >= MAXPATHLEN - 1) {
1518 io_error |= IOERR_GENERAL;
1519 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1523 save_filters = push_local_filters(fbuf, len);
1524 send_directory(f, flist, fbuf, len, flags);
1525 pop_local_filters(save_filters);
1532 static int file_compare(const void *file1, const void *file2)
1534 return f_name_cmp(*(struct file_struct **)file1,
1535 *(struct file_struct **)file2);
1538 /* The guts of a merge-sort algorithm. This was derived from the glibc
1539 * version, but I (Wayne) changed the merge code to do less copying and
1540 * to require only half the amount of temporary memory. */
1541 static void fsort_tmp(struct file_struct **fp, size_t num,
1542 struct file_struct **tmp)
1544 struct file_struct **f1, **f2, **t;
1553 fsort_tmp(f1, n1, tmp);
1555 fsort_tmp(f2, n2, tmp);
1557 while (f_name_cmp(*f1, *f2) <= 0) {
1564 memcpy(t, f1, n1 * PTR_SIZE);
1566 *f1++ = *f2++, n2--;
1568 while (n1 > 0 && n2 > 0) {
1569 if (f_name_cmp(*t, *f2) <= 0)
1572 *f1++ = *f2++, n2--;
1576 memcpy(f1, t, n1 * PTR_SIZE);
1579 /* This file-struct sorting routine makes sure that any identical names in
1580 * the file list stay in the same order as they were in the original list.
1581 * This is particularly vital in inc_recurse mode where we expect a sort
1582 * on the flist to match the exact order of a sort on the dir_flist. */
1583 static void fsort(struct file_struct **fp, size_t num)
1589 qsort(fp, num, PTR_SIZE, file_compare);
1591 struct file_struct **tmp = new_array(struct file_struct *,
1593 fsort_tmp(fp, num, tmp);
1598 /* We take an entire set of sibling dirs from the sorted flist and link them
1599 * into the tree, setting the appropriate parent/child/sibling pointers. */
1600 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1605 int32 *parent_dp = parent_ndx < 0 ? NULL
1606 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1608 flist_expand(dir_flist, dir_cnt);
1609 dir_flist->sorted = dir_flist->files;
1611 for (i = 0; dir_cnt; i++) {
1612 struct file_struct *file = from_flist->sorted[i];
1614 if (!S_ISDIR(file->mode))
1617 dir_flist->files[dir_flist->used++] = file;
1620 if (file->basename[0] == '.' && file->basename[1] == '\0')
1624 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1626 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1628 send_dir_ndx = dir_flist->used - 1;
1630 dp = F_DIR_NODE_P(file);
1631 DIR_PARENT(dp) = parent_ndx;
1632 DIR_FIRST_CHILD(dp) = -1;
1635 DIR_NEXT_SIBLING(dp) = -1;
1638 static void interpret_stat_error(const char *fname, int is_dir)
1640 if (errno == ENOENT) {
1641 io_error |= IOERR_VANISHED;
1642 rprintf(FWARNING, "%s has vanished: %s\n",
1643 is_dir ? "directory" : "file", full_fname(fname));
1645 io_error |= IOERR_GENERAL;
1646 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1651 /* This function is normally called by the sender, but the receiving side also
1652 * calls it from get_dirlist() with f set to -1 so that we just construct the
1653 * file list in memory without sending it over the wire. Also, get_dirlist()
1654 * might call this with f set to -2, which also indicates that local filter
1655 * rules should be ignored. */
1656 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1663 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1664 int start = flist->used;
1665 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1667 assert(flist != NULL);
1669 if (!(d = opendir(fbuf))) {
1670 if (errno == ENOENT) {
1671 if (am_sender) /* Can abuse this for vanished error w/ENOENT: */
1672 interpret_stat_error(fbuf, True);
1675 io_error |= IOERR_GENERAL;
1676 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1681 if (len != 1 || *fbuf != '/')
1684 remainder = MAXPATHLEN - (p - fbuf);
1686 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1687 char *dname = d_name(di);
1688 if (dname[0] == '.' && (dname[1] == '\0'
1689 || (dname[1] == '.' && dname[2] == '\0')))
1691 if (strlcpy(p, dname, remainder) >= remainder) {
1692 io_error |= IOERR_GENERAL;
1693 rprintf(FERROR_XFER,
1694 "cannot send long-named file %s\n",
1698 if (dname[0] == '\0') {
1699 io_error |= IOERR_GENERAL;
1700 rprintf(FERROR_XFER,
1701 "cannot send file with empty name in %s\n",
1706 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1712 io_error |= IOERR_GENERAL;
1713 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1718 if (f >= 0 && recurse && !divert_dirs) {
1719 int i, end = flist->used - 1;
1720 /* send_if_directory() bumps flist->used, so use "end". */
1721 for (i = start; i <= end; i++)
1722 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1726 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1727 char *start, char *limit, int flags, char name_type)
1729 static char lastpath[MAXPATHLEN] = "";
1730 static int lastpath_len = 0;
1731 static struct file_struct *lastpath_struct = NULL;
1732 struct file_struct *file;
1733 item_list *relname_list;
1734 relnamecache **rnpp;
1735 int len, need_new_dir, depth = 0;
1736 struct filter_list_struct save_filter_list = filter_list;
1738 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1739 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1742 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1743 && lastpath_len == limit - fname
1744 && strncmp(lastpath, fname, lastpath_len) == 0)
1749 char *tp = fname, *lp = lastpath;
1750 /* Skip any initial directories in our path that we
1751 * have in common with lastpath. */
1752 assert(start == fname);
1753 for ( ; ; tp++, lp++) {
1755 if (*lp == '/' || *lp == '\0')
1770 int save_copy_links = copy_links;
1771 int save_xfer_dirs = xfer_dirs;
1774 copy_links = xfer_dirs = 1;
1778 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1780 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1782 if (!inc_recurse && file && S_ISDIR(file->mode))
1783 change_local_filter_dir(fname, strlen(fname), depth);
1787 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1789 if (file && !S_ISDIR(file->mode))
1791 lastpath_struct = file;
1792 } else if (file && S_ISDIR(file->mode))
1793 change_local_filter_dir(fname, strlen(fname), ++depth);
1795 strlcpy(lastpath, fname, sizeof lastpath);
1796 lastpath_len = limit - fname;
1800 copy_links = save_copy_links;
1801 xfer_dirs = save_xfer_dirs;
1807 if (!lastpath_struct)
1808 goto done; /* dir must have vanished */
1810 len = strlen(limit+1);
1811 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1812 if (!relname_list) {
1813 if (!(relname_list = new0(item_list)))
1814 out_of_memory("send_implied_dirs");
1815 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1817 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1818 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1819 out_of_memory("send_implied_dirs");
1820 (*rnpp)->name_type = name_type;
1821 strlcpy((*rnpp)->fname, limit+1, len + 1);
1824 filter_list = save_filter_list;
1827 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1829 char fbuf[MAXPATHLEN];
1830 item_list *relname_list;
1831 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1835 dlen = strlen(fbuf);
1837 if (!change_pathname(file, NULL, 0))
1838 exit_cleanup(RERR_FILESELECT);
1840 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1842 if (file->flags & FLAG_CONTENT_DIR) {
1843 if (one_file_system) {
1845 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1846 interpret_stat_error(fbuf, True);
1849 filesystem_dev = st.st_dev;
1851 send_directory(f, flist, fbuf, dlen, flags);
1854 if (!relative_paths)
1857 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1861 for (j = 0; j < relname_list->count; j++) {
1863 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1864 char name_type = rnp->name_type;
1867 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1869 if (len >= (int)sizeof fbuf)
1870 continue; /* Impossible... */
1872 slash = strchr(fbuf+dlen+1, '/');
1874 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1878 if (name_type != NORMAL_NAME) {
1880 if (link_stat(fbuf, &st, 1) != 0) {
1881 interpret_stat_error(fbuf, True);
1884 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1886 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1892 void send_extra_file_list(int f, int at_least)
1894 struct file_list *flist;
1897 int old_cnt, save_io_error = io_error;
1902 /* Keep sending data until we have the requested number of
1903 * files in the upcoming file-lists. */
1904 old_cnt = cur_flist->used;
1905 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1906 old_cnt += flist->used;
1907 while (file_total - old_cnt < at_least) {
1908 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1909 int dir_ndx, dstart = stats.num_dirs;
1910 const char *pathname = F_PATHNAME(file);
1913 flist = flist_new(0, "send_extra_file_list");
1914 start_write = stats.total_written;
1917 dir_ndx = F_NDX(file);
1919 dir_ndx = send_dir_ndx;
1920 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1921 flist->parent_ndx = dir_ndx;
1923 send1extra(f, file, flist);
1924 prev_flags = file->flags;
1925 dp = F_DIR_NODE_P(file);
1927 /* If there are any duplicate directory names that follow, we
1928 * send all the dirs together in one file-list. The dir_flist
1929 * tree links all the child subdirs onto the last dup dir. */
1930 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1931 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1932 send_dir_ndx = dir_ndx;
1933 file = dir_flist->sorted[dir_ndx];
1934 /* Try to avoid some duplicate scanning of identical dirs. */
1935 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1936 file->flags &= ~FLAG_CONTENT_DIR;
1937 send1extra(f, file, flist);
1938 prev_flags = file->flags;
1939 dp = F_DIR_NODE_P(file);
1942 if (protocol_version < 31 || io_error == save_io_error || ignore_errors)
1945 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
1946 write_varint(f, io_error);
1949 if (need_unsorted_flist) {
1950 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1951 out_of_memory("send_extra_file_list");
1952 memcpy(flist->sorted, flist->files,
1953 flist->used * sizeof (struct file_struct*));
1955 flist->sorted = flist->files;
1957 flist_sort_and_clean(flist, 0);
1959 add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart);
1960 flist_done_allocating(flist);
1962 file_total += flist->used;
1963 stats.flist_size += stats.total_written - start_write;
1964 stats.num_files += flist->used;
1965 if (DEBUG_GTE(FLIST, 3))
1966 output_flist(flist);
1968 if (DIR_FIRST_CHILD(dp) >= 0) {
1969 send_dir_ndx = DIR_FIRST_CHILD(dp);
1972 while (DIR_NEXT_SIBLING(dp) < 0) {
1973 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1974 write_ndx(f, NDX_FLIST_EOF);
1976 change_local_filter_dir(NULL, 0, 0);
1980 file = dir_flist->sorted[send_dir_ndx];
1981 dp = F_DIR_NODE_P(file);
1983 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1988 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
1989 send_msg_int(MSG_IO_ERROR, io_error);
1992 struct file_list *send_file_list(int f, int argc, char *argv[])
1994 static const char *lastdir;
1995 static int lastdir_len = -1;
1999 struct file_list *flist;
2000 struct timeval start_tv, end_tv;
2003 int disable_buffering;
2004 int flags = recurse ? FLAG_CONTENT_DIR : 0;
2005 int reading_remotely = filesfrom_host != NULL;
2006 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2008 | (filesfrom_convert ? RL_CONVERT : 0)
2010 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2011 int implied_dot_dir = 0;
2013 rprintf(FLOG, "building file list\n");
2014 if (show_filelist_p())
2015 start_filelist_progress("building file list");
2016 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2017 rprintf(FCLIENT, "sending incremental file list\n");
2019 start_write = stats.total_written;
2020 gettimeofday(&start_tv, NULL);
2022 if (relative_paths && protocol_version >= 30)
2023 implied_dirs = 1; /* We send flagged implied dirs */
2025 #ifdef SUPPORT_HARD_LINKS
2026 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2030 flist = cur_flist = flist_new(0, "send_file_list");
2032 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2033 flags |= FLAG_DIVERT_DIRS;
2035 dir_flist = cur_flist;
2037 disable_buffering = io_start_buffering_out(f);
2038 if (filesfrom_fd >= 0) {
2039 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2040 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2041 full_fname(argv[0]));
2042 exit_cleanup(RERR_FILESELECT);
2048 orig_dir = strdup(curr_dir);
2051 char fbuf[MAXPATHLEN], *fn, name_type;
2054 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2056 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2060 strlcpy(fbuf, *argv++, MAXPATHLEN);
2062 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2066 if (relative_paths) {
2067 /* We clean up fbuf below. */
2068 name_type = NORMAL_NAME;
2069 } else if (!len || fbuf[len - 1] == '/') {
2070 if (len == 2 && fbuf[0] == '.') {
2071 /* Turn "./" into just "." rather than "./." */
2074 if (len + 1 >= MAXPATHLEN)
2075 overflow_exit("send_file_list");
2079 name_type = DOTDIR_NAME;
2080 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2081 && (len == 2 || fbuf[len-3] == '/')) {
2082 if (len + 2 >= MAXPATHLEN)
2083 overflow_exit("send_file_list");
2087 name_type = DOTDIR_NAME;
2088 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2089 name_type = DOTDIR_NAME;
2091 name_type = NORMAL_NAME;
2095 if (!relative_paths) {
2096 p = strrchr(fbuf, '/');
2103 len -= p - fbuf + 1;
2108 if ((p = strstr(fbuf, "/./")) != NULL) {
2114 clean_fname(dir, 0);
2120 *--fn = '\0'; /* ensure room for '.' */
2123 /* A leading ./ can be used in relative mode to affect
2124 * the dest dir without its name being in the path. */
2125 if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
2126 send_file_name(f, flist, ".", NULL,
2127 (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
2129 implied_dot_dir = 1;
2131 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2132 | CFN_DROP_TRAILING_DOT_DIR);
2137 name_type = DOTDIR_NAME;
2138 } else if (fn[0] == '.')
2139 name_type = DOTDIR_NAME;
2140 } else if (fn[len-1] == '/') {
2142 if (len == 1 && *fn == '.')
2143 name_type = DOTDIR_NAME;
2145 name_type = SLASH_ENDING_NAME;
2147 /* Reject a ".." dir in the active part of the path. */
2148 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2149 if ((p[2] == '/' || p[2] == '\0')
2150 && (p == fn || p[-1] == '/')) {
2152 "found \"..\" dir in relative path: %s\n",
2154 exit_cleanup(RERR_SYNTAX);
2162 name_type = DOTDIR_NAME;
2165 dirlen = dir ? strlen(dir) : 0;
2166 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
2167 if (!change_pathname(NULL, dir, -dirlen))
2170 lastdir_len = pathname_len;
2171 } else if (!change_pathname(NULL, lastdir, lastdir_len))
2175 memmove(fbuf, fn, len + 1);
2177 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2178 || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
2179 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2180 if (errno != ENOENT || missing_args == 0) {
2181 /* This is a transfer error, but inhibit deletion
2182 * only if we might be omitting an existing file. */
2183 if (errno != ENOENT)
2184 io_error |= IOERR_GENERAL;
2185 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2188 } else if (missing_args == 1) {
2189 /* Just ignore the arg. */
2191 } else /* (missing_args == 2) */ {
2192 /* Send the arg as a "missing" entry with
2193 * mode 0, which tells the generator to delete it. */
2194 memset(&st, 0, sizeof st);
2198 /* A dot-dir should not be excluded! */
2199 if (name_type != DOTDIR_NAME && st.st_mode != 0
2200 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2203 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2204 rprintf(FINFO, "skipping directory %s\n", fbuf);
2208 if (inc_recurse && relative_paths && *fbuf) {
2209 if ((p = strchr(fbuf+1, '/')) != NULL) {
2210 if (p - fbuf == 1 && *fbuf == '.') {
2211 if ((fn = strchr(p+1, '/')) != NULL)
2215 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2219 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2220 /* Send the implied directories at the start of the
2221 * source spec, so we get their permissions right. */
2222 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2225 if (one_file_system)
2226 filesystem_dev = st.st_dev;
2228 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2229 struct file_struct *file;
2230 file = send_file_name(f, flist, fbuf, &st,
2231 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2236 if (name_type == DOTDIR_NAME) {
2237 if (send_dir_depth < 0) {
2239 change_local_filter_dir(fbuf, len, send_dir_depth);
2241 send_directory(f, flist, fbuf, len, flags);
2244 send_if_directory(f, flist, file, fbuf, len, flags);
2246 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2249 gettimeofday(&end_tv, NULL);
2250 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2251 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2252 if (stats.flist_buildtime == 0)
2253 stats.flist_buildtime = 1;
2256 /* Indicate end of file list */
2257 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2260 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2261 write_varint(f, io_error);
2264 #ifdef SUPPORT_HARD_LINKS
2265 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2269 if (show_filelist_p())
2270 finish_filelist_progress(flist);
2272 gettimeofday(&end_tv, NULL);
2273 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2274 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2276 /* When converting names, both sides keep an unsorted file-list array
2277 * because the names will differ on the sending and receiving sides
2278 * (both sides will use the unsorted index number for each item). */
2280 /* Sort the list without removing any duplicates. This allows the
2281 * receiving side to ask for whatever name it kept. For incremental
2282 * recursion mode, the sender marks duplicate dirs so that it can
2283 * send them together in a single file-list. */
2284 if (need_unsorted_flist) {
2285 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2286 out_of_memory("send_file_list");
2287 memcpy(flist->sorted, flist->files,
2288 flist->used * sizeof (struct file_struct*));
2290 flist->sorted = flist->files;
2291 flist_sort_and_clean(flist, 0);
2292 file_total += flist->used;
2294 if (numeric_ids <= 0 && !inc_recurse)
2299 /* send the io_error flag */
2300 if (protocol_version < 30)
2301 write_int(f, ignore_errors ? 0 : io_error);
2302 else if (io_error && protocol_version == 30 && !ignore_errors)
2303 send_msg_int(MSG_IO_ERROR, io_error);
2305 if (disable_buffering)
2306 io_end_buffering_out();
2308 stats.flist_size = stats.total_written - start_write;
2309 stats.num_files = flist->used;
2311 if (DEBUG_GTE(FLIST, 3))
2312 output_flist(flist);
2314 if (DEBUG_GTE(FLIST, 2))
2315 rprintf(FINFO, "send_file_list done\n");
2319 add_dirs_to_tree(-1, flist, stats.num_dirs);
2320 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2321 flist->parent_ndx = -1;
2322 flist_done_allocating(flist);
2323 if (send_dir_ndx < 0) {
2324 write_ndx(f, NDX_FLIST_EOF);
2327 else if (file_total == 1) {
2328 /* If we're creating incremental file-lists and there
2329 * was just 1 item in the first file-list, send 1 more
2330 * file-list to check if this is a 1-file xfer. */
2331 send_extra_file_list(f, 1);
2339 struct file_list *recv_file_list(int f)
2341 struct file_list *flist;
2346 if (show_filelist_p())
2347 start_filelist_progress("receiving file list");
2348 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2349 rprintf(FCLIENT, "receiving incremental file list\n");
2350 rprintf(FLOG, "receiving file list\n");
2352 parse_name_map(usermap, True);
2354 parse_name_map(groupmap, False);
2357 start_read = stats.total_read;
2359 #ifdef SUPPORT_HARD_LINKS
2360 if (preserve_hard_links && !first_flist)
2364 flist = flist_new(0, "recv_file_list");
2367 if (flist->ndx_start == 1)
2368 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2369 dstart = dir_flist->used;
2375 while ((flags = read_byte(f)) != 0) {
2376 struct file_struct *file;
2378 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2379 flags |= read_byte(f) << 8;
2381 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2383 if (protocol_version < 31) {
2384 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2385 exit_cleanup(RERR_PROTOCOL);
2387 err = read_varint(f);
2393 flist_expand(flist, 1);
2394 file = recv_file_entry(flist, flags, f);
2396 if (S_ISREG(file->mode)) {
2397 /* Already counted */
2398 } else if (S_ISDIR(file->mode)) {
2400 flist_expand(dir_flist, 1);
2401 dir_flist->files[dir_flist->used++] = file;
2404 } else if (S_ISLNK(file->mode))
2405 stats.num_symlinks++;
2406 else if (IS_DEVICE(file->mode))
2407 stats.num_symlinks++;
2409 stats.num_specials++;
2411 flist->files[flist->used++] = file;
2413 maybe_emit_filelist_progress(flist->used);
2415 if (DEBUG_GTE(FLIST, 2)) {
2416 char *name = f_name(file, NULL);
2417 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2420 file_total += flist->used;
2422 if (DEBUG_GTE(FLIST, 2))
2423 rprintf(FINFO, "received %d names\n", flist->used);
2425 if (show_filelist_p())
2426 finish_filelist_progress(flist);
2428 if (need_unsorted_flist) {
2429 /* Create an extra array of index pointers that we can sort for
2430 * the generator's use (for wading through the files in sorted
2431 * order and for calling flist_find()). We keep the "files"
2432 * list unsorted for our exchange of index numbers with the
2433 * other side (since their names may not sort the same). */
2434 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2435 out_of_memory("recv_file_list");
2436 memcpy(flist->sorted, flist->files,
2437 flist->used * sizeof (struct file_struct*));
2438 if (inc_recurse && dir_flist->used > dstart) {
2439 static int dir_flist_malloced = 0;
2440 if (dir_flist_malloced < dir_flist->malloced) {
2441 dir_flist->sorted = realloc_array(dir_flist->sorted,
2442 struct file_struct *,
2443 dir_flist->malloced);
2444 dir_flist_malloced = dir_flist->malloced;
2446 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2447 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2448 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2451 flist->sorted = flist->files;
2452 if (inc_recurse && dir_flist->used > dstart) {
2453 dir_flist->sorted = dir_flist->files;
2454 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2459 flist_done_allocating(flist);
2461 recv_id_list(f, flist);
2465 flist_sort_and_clean(flist, relative_paths);
2467 if (protocol_version < 30) {
2468 /* Recv the io_error flag */
2469 int err = read_int(f);
2472 } else if (inc_recurse && flist->ndx_start == 1) {
2473 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2474 flist->parent_ndx = -1;
2477 if (DEBUG_GTE(FLIST, 3))
2478 output_flist(flist);
2480 if (DEBUG_GTE(FLIST, 2))
2481 rprintf(FINFO, "recv_file_list done\n");
2483 stats.flist_size += stats.total_read - start_read;
2484 stats.num_files += flist->used;
2489 /* This is only used once by the receiver if the very first file-list
2490 * has exactly one item in it. */
2491 void recv_additional_file_list(int f)
2493 struct file_list *flist;
2494 int ndx = read_ndx(f);
2495 if (ndx == NDX_FLIST_EOF) {
2497 change_local_filter_dir(NULL, 0, 0);
2499 ndx = NDX_FLIST_OFFSET - ndx;
2500 if (ndx < 0 || ndx >= dir_flist->used) {
2501 ndx = NDX_FLIST_OFFSET - ndx;
2503 "[%s] Invalid dir index: %d (%d - %d)\n",
2504 who_am_i(), ndx, NDX_FLIST_OFFSET,
2505 NDX_FLIST_OFFSET - dir_flist->used + 1);
2506 exit_cleanup(RERR_PROTOCOL);
2508 if (DEBUG_GTE(FLIST, 3)) {
2509 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2512 flist = recv_file_list(f);
2513 flist->parent_ndx = ndx;
2517 /* Search for an identically-named item in the file list. Note that the
2518 * items must agree in their directory-ness, or no match is returned. */
2519 int flist_find(struct file_list *flist, struct file_struct *f)
2521 int low = flist->low, high = flist->high;
2522 int diff, mid, mid_up;
2524 while (low <= high) {
2525 mid = (low + high) / 2;
2526 if (F_IS_ACTIVE(flist->sorted[mid]))
2529 /* Scan for the next non-empty entry using the cached
2530 * distance values. If the value isn't fully up-to-
2531 * date, update it. */
2532 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2533 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2535 mid_up += F_DEPTH(flist->sorted[mid_up]);
2536 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2537 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2539 if (mid_up > high) {
2540 /* If there's nothing left above us, set high to
2541 * a non-empty entry below us and continue. */
2542 high = mid - (int)flist->sorted[mid]->len32;
2543 if (!F_IS_ACTIVE(flist->sorted[high])) {
2545 high -= (int)flist->sorted[high]->len32;
2546 } while (!F_IS_ACTIVE(flist->sorted[high]));
2547 flist->sorted[mid]->len32 = mid - high;
2552 diff = f_name_cmp(flist->sorted[mid_up], f);
2554 if (protocol_version < 29
2555 && S_ISDIR(flist->sorted[mid_up]->mode)
2556 != S_ISDIR(f->mode))
2568 /* Search for an identically-named item in the file list. Differs from
2569 * flist_find in that an item that agrees with "f" in directory-ness is
2570 * preferred but one that does not is still found. */
2571 int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2576 /* First look for an item that agrees in directory-ness. */
2577 ndx = flist_find(flist, f);
2581 /* Temporarily flip f->mode to look for an item of opposite
2582 * directory-ness. */
2583 save_mode = f->mode;
2584 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2585 ndx = flist_find(flist, f);
2586 f->mode = save_mode;
2591 * Free up any resources a file_struct has allocated
2592 * and clear the file.
2594 void clear_file(struct file_struct *file)
2596 /* The +1 zeros out the first char of the basename. */
2597 memset(file, 0, FILE_STRUCT_LEN + 1);
2598 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2599 * entry. Likewise for len32 in the opposite direction. We assume
2600 * that we're alone for now since flist_find() will adjust the counts
2601 * it runs into that aren't up-to-date. */
2602 file->len32 = F_DEPTH(file) = 1;
2605 /* Allocate a new file list. */
2606 struct file_list *flist_new(int flags, char *msg)
2608 struct file_list *flist;
2610 if (!(flist = new0(struct file_list)))
2613 if (flags & FLIST_TEMP) {
2614 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2619 /* This is a doubly linked list with prev looping back to
2620 * the end of the list, but the last next pointer is NULL. */
2622 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2625 if (!flist->file_pool)
2628 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2630 first_flist = cur_flist = flist->prev = flist;
2632 struct file_list *prev = first_flist->prev;
2634 flist->file_pool = first_flist->file_pool;
2636 flist->ndx_start = prev->ndx_start + prev->used + 1;
2637 flist->flist_num = prev->flist_num + 1;
2640 prev->next = first_flist->prev = flist;
2642 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2649 /* Free up all elements in a flist. */
2650 void flist_free(struct file_list *flist)
2653 /* Was FLIST_TEMP dir-list. */
2654 } else if (flist == flist->prev) {
2655 first_flist = cur_flist = NULL;
2659 if (flist == cur_flist)
2660 cur_flist = flist->next;
2661 if (flist == first_flist)
2662 first_flist = first_flist->next;
2664 flist->prev->next = flist->next;
2666 flist->next = first_flist;
2668 flist->next->prev = flist->prev;
2669 file_total -= flist->used;
2673 if (!flist->prev || !flist_cnt)
2674 pool_destroy(flist->file_pool);
2676 pool_free_old(flist->file_pool, flist->pool_boundary);
2678 if (flist->sorted && flist->sorted != flist->files)
2679 free(flist->sorted);
2684 /* This routine ensures we don't have any duplicate names in our file list.
2685 * duplicate names can cause corruption because of the pipelining. */
2686 static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2688 char fbuf[MAXPATHLEN];
2693 if (flist->used == 0) {
2699 fsort(flist->sorted, flist->used);
2701 if (!am_sender || inc_recurse) {
2702 for (i = prev_i = 0; i < flist->used; i++) {
2703 if (F_IS_ACTIVE(flist->sorted[i])) {
2708 flist->low = prev_i;
2710 i = prev_i = flist->used - 1;
2714 while (++i < flist->used) {
2716 struct file_struct *file = flist->sorted[i];
2718 if (!F_IS_ACTIVE(file))
2720 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2722 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2723 int save_mode = file->mode;
2724 /* Make sure that this directory doesn't duplicate a
2725 * non-directory earlier in the list. */
2726 flist->high = prev_i;
2727 file->mode = S_IFREG;
2728 j = flist_find(flist, file);
2729 file->mode = save_mode;
2734 /* If one is a dir and the other is not, we want to
2735 * keep the dir because it might have contents in the
2736 * list. Otherwise keep the first one. */
2737 if (S_ISDIR(file->mode)) {
2738 struct file_struct *fp = flist->sorted[j];
2739 if (!S_ISDIR(fp->mode))
2743 file->flags |= FLAG_DUPLICATE;
2744 else { /* Make sure we merge our vital flags. */
2745 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2746 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2754 if (DEBUG_GTE(DUP, 1)) {
2756 "removing duplicate name %s from file list (%d)\n",
2757 f_name(file, fbuf), drop + flist->ndx_start);
2759 clear_file(flist->sorted[drop]);
2763 if (flist->low == drop) {
2765 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2774 flist->high = prev_i;
2777 /* We need to strip off the leading slashes for relative
2778 * paths, but this must be done _after_ the sorting phase. */
2779 for (i = flist->low; i <= flist->high; i++) {
2780 struct file_struct *file = flist->sorted[i];
2784 while (*file->dirname == '/')
2786 if (!*file->dirname)
2787 file->dirname = NULL;
2791 if (prune_empty_dirs && !am_sender) {
2792 int j, prev_depth = 0;
2794 prev_i = 0; /* It's OK that this isn't really true. */
2796 for (i = flist->low; i <= flist->high; i++) {
2797 struct file_struct *fp, *file = flist->sorted[i];
2799 /* This temporarily abuses the F_DEPTH() value for a
2800 * directory that is in a chain that might get pruned.
2801 * We restore the old value if it gets a reprieve. */
2802 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2803 /* Dump empty dirs when coming back down. */
2804 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2805 fp = flist->sorted[prev_i];
2806 if (F_DEPTH(fp) >= 0)
2808 prev_i = -F_DEPTH(fp)-1;
2811 prev_depth = F_DEPTH(file);
2812 if (is_excluded(f_name(file, fbuf), 1,
2814 /* Keep dirs through this dir. */
2815 for (j = prev_depth-1; ; j--) {
2816 fp = flist->sorted[prev_i];
2817 if (F_DEPTH(fp) >= 0)
2819 prev_i = -F_DEPTH(fp)-1;
2823 F_DEPTH(file) = -prev_i-1;
2826 /* Keep dirs through this non-dir. */
2827 for (j = prev_depth; ; j--) {
2828 fp = flist->sorted[prev_i];
2829 if (F_DEPTH(fp) >= 0)
2831 prev_i = -F_DEPTH(fp)-1;
2836 /* Dump all remaining empty dirs. */
2838 struct file_struct *fp = flist->sorted[prev_i];
2839 if (F_DEPTH(fp) >= 0)
2841 prev_i = -F_DEPTH(fp)-1;
2845 for (i = flist->low; i <= flist->high; i++) {
2846 if (F_IS_ACTIVE(flist->sorted[i]))
2850 for (i = flist->high; i >= flist->low; i--) {
2851 if (F_IS_ACTIVE(flist->sorted[i]))
2858 static void output_flist(struct file_list *flist)
2860 char uidbuf[16], gidbuf[16], depthbuf[16];
2861 struct file_struct *file;
2862 const char *root, *dir, *slash, *name, *trail;
2863 const char *who = who_am_i();
2866 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2867 who, flist->ndx_start, flist->used, flist->low, flist->high);
2868 for (i = 0; i < flist->used; i++) {
2869 file = flist->files[i];
2870 if ((am_root || am_sender) && uid_ndx) {
2871 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2876 static char parens[] = "(\0)\0\0\0";
2877 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2878 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2879 pp, F_GROUP(file), pp + 2);
2883 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2884 if (F_IS_ACTIVE(file)) {
2885 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2886 if ((dir = file->dirname) == NULL)
2890 name = file->basename;
2891 trail = S_ISDIR(file->mode) ? "/" : "";
2893 root = dir = slash = name = trail = "";
2895 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
2896 who, i + flist->ndx_start,
2897 root, dir, slash, name, trail,
2898 (int)file->mode, comma_num(F_LENGTH(file)),
2899 uidbuf, gidbuf, file->flags);
2903 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2904 enum fnc_type { t_PATH, t_ITEM };
2906 static int found_prefix;
2908 /* Compare the names of two file_struct entities, similar to how strcmp()
2909 * would do if it were operating on the joined strings.
2911 * Some differences beginning with protocol_version 29: (1) directory names
2912 * are compared with an assumed trailing slash so that they compare in a
2913 * way that would cause them to sort immediately prior to any content they
2914 * may have; (2) a directory of any name compares after a non-directory of
2915 * any name at the same depth; (3) a directory with name "." compares prior
2916 * to anything else. These changes mean that a directory and a non-dir
2917 * with the same name will not compare as equal (protocol_version >= 29).
2919 * The dirname component can be an empty string, but the basename component
2920 * cannot (and never is in the current codebase). The basename component
2921 * may be NULL (for a removed item), in which case it is considered to be
2922 * after any existing item. */
2923 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2926 const uchar *c1, *c2;
2927 enum fnc_state state1, state2;
2928 enum fnc_type type1, type2;
2929 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2931 if (!f1 || !F_IS_ACTIVE(f1)) {
2932 if (!f2 || !F_IS_ACTIVE(f2))
2936 if (!f2 || !F_IS_ACTIVE(f2))
2939 c1 = (uchar*)f1->dirname;
2940 c2 = (uchar*)f2->dirname;
2944 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2945 c1 = (const uchar*)f1->basename;
2946 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2948 state1 = s_TRAILING;
2957 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2958 c2 = (const uchar*)f2->basename;
2959 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2961 state2 = s_TRAILING;
2971 return type1 == t_PATH ? 1 : -1;
2981 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2982 c1 = (const uchar*)f1->basename;
2983 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2985 state1 = s_TRAILING;
2991 state1 = s_TRAILING;
2992 if (type1 == t_PATH) {
3001 if (*c2 && type1 != type2)
3002 return type1 == t_PATH ? 1 : -1;
3011 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3012 c2 = (const uchar*)f2->basename;
3013 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3015 state2 = s_TRAILING;
3021 state2 = s_TRAILING;
3022 if (type2 == t_PATH) {
3035 return type1 == t_PATH ? 1 : -1;
3037 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3042 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3043 * not match if f2's basename is not an exact match of a path element in f1.
3044 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3045 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3049 return found_prefix;
3052 char *f_name_buf(void)
3054 static char names[5][MAXPATHLEN];
3055 static unsigned int n;
3057 n = (n + 1) % (sizeof names / sizeof names[0]);
3062 /* Return a copy of the full filename of a flist entry, using the indicated
3063 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3064 * done because we checked the size when creating the file_struct entry.
3066 char *f_name(const struct file_struct *f, char *fbuf)
3068 if (!f || !F_IS_ACTIVE(f))
3072 fbuf = f_name_buf();
3075 int len = strlen(f->dirname);
3076 memcpy(fbuf, f->dirname, len);
3078 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3080 strlcpy(fbuf, f->basename, MAXPATHLEN);
3085 /* Do a non-recursive scan of the named directory, possibly ignoring all
3086 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3087 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3088 * buffer (the functions we call will append names onto the end, but the old
3089 * dir value will be restored on exit). */
3090 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
3092 struct file_list *dirlist;
3093 char dirbuf[MAXPATHLEN];
3094 int save_recurse = recurse;
3095 int save_xfer_dirs = xfer_dirs;
3096 int save_prune_empty_dirs = prune_empty_dirs;
3099 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3100 if (dlen >= MAXPATHLEN)
3105 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3109 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
3110 xfer_dirs = save_xfer_dirs;
3111 recurse = save_recurse;
3112 if (INFO_GTE(PROGRESS, 1))
3113 flist_count_offset += dirlist->used;
3115 prune_empty_dirs = 0;
3116 dirlist->sorted = dirlist->files;
3117 flist_sort_and_clean(dirlist, 0);
3118 prune_empty_dirs = save_prune_empty_dirs;
3120 if (DEBUG_GTE(FLIST, 3))
3121 output_flist(dirlist);