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 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 do_progress;
36 extern int always_checksum;
38 extern int ignore_errors;
39 extern int numeric_ids;
43 extern int filesfrom_fd;
44 extern int one_file_system;
45 extern int copy_dirlinks;
46 extern int keep_dirlinks;
47 extern int preserve_acls;
48 extern int preserve_xattrs;
49 extern int preserve_links;
50 extern int preserve_hard_links;
51 extern int preserve_devices;
52 extern int preserve_specials;
56 extern int relative_paths;
57 extern int implied_dirs;
58 extern int file_extra_cnt;
59 extern int ignore_perishable;
60 extern int non_perishable_cnt;
61 extern int prune_empty_dirs;
62 extern int copy_links;
63 extern int copy_unsafe_links;
64 extern int protocol_version;
65 extern int sanitize_paths;
66 extern int munge_symlinks;
67 extern int need_unsorted_flist;
68 extern int unsort_ndx;
69 extern struct stats stats;
70 extern char *filesfrom_host;
72 extern char curr_dir[MAXPATHLEN];
74 extern struct chmod_mode_struct *chmod_modes;
76 extern struct filter_list_struct filter_list;
77 extern struct filter_list_struct server_filter_list;
80 extern int filesfrom_convert;
81 extern iconv_t ic_send, ic_recv;
84 #define PTR_SIZE (sizeof (struct file_struct *))
88 dev_t filesystem_dev; /* used to implement -x */
90 struct file_list *cur_flist, *first_flist, *dir_flist;
91 int send_dir_ndx = -1, send_dir_depth = -1;
92 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
93 int file_total = 0; /* total of all active items over all file-lists */
94 int flist_eof = 0; /* all the file-lists are now known */
97 #define SLASH_ENDING_NAME 1
100 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
101 * internally, even if this platform does not allow files to have 64-bit inums.
102 * The only exception is if we're on a platform with no 64-bit type at all.
104 * Because we use read_longint() to get these off the wire, if you transfer
105 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
106 * machine with no 64-bit types then you will get an overflow error.
108 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
109 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
110 * be truncated. But it's a kind of silly thing to do anyhow. */
112 /* The tmp_* vars are used as a cache area by make_file() to store data
113 * that the sender doesn't need to remember in its file list. The data
114 * will survive just long enough to be used by send_file_entry(). */
115 static dev_t tmp_rdev;
116 #ifdef SUPPORT_HARD_LINKS
117 static int64 tmp_dev, tmp_ino;
119 static char tmp_sum[MAX_DIGEST_LEN];
121 static char empty_sum[MAX_DIGEST_LEN];
122 static int flist_count_offset; /* for --delete --progress */
123 static int dir_count = 0;
125 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
126 static void output_flist(struct file_list *flist);
128 void init_flist(void)
131 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
132 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
134 checksum_len = protocol_version < 21 ? 2
135 : protocol_version < 30 ? MD4_DIGEST_LEN
139 static int show_filelist_p(void)
141 return verbose && xfer_dirs && !am_server && !inc_recurse;
144 static void start_filelist_progress(char *kind)
146 rprintf(FCLIENT, "%s ... ", kind);
147 if (verbose > 1 || do_progress)
148 rprintf(FCLIENT, "\n");
152 static void emit_filelist_progress(int count)
154 rprintf(FCLIENT, " %d files...\r", count);
157 static void maybe_emit_filelist_progress(int count)
159 if (do_progress && show_filelist_p() && (count % 100) == 0)
160 emit_filelist_progress(count);
163 static void finish_filelist_progress(const struct file_list *flist)
166 /* This overwrites the progress line */
167 rprintf(FINFO, "%d file%sto consider\n",
168 flist->used, flist->used == 1 ? " " : "s ");
170 rprintf(FINFO, "done\n");
173 void show_flist_stats(void)
178 /* Stat either a symlink or its referent, depending on the settings of
179 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
181 * If path is the name of a symlink, then the linkbuf buffer (which must hold
182 * MAXPATHLEN chars) will be set to the symlink's target string.
184 * The stat structure pointed to by stp will contain information about the
185 * link or the referent as appropriate, if they exist. */
186 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
189 if (link_stat(path, stp, copy_dirlinks) < 0)
191 if (S_ISLNK(stp->st_mode)) {
192 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
195 linkbuf[llen] = '\0';
196 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
198 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
201 return x_stat(path, stp, NULL);
203 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
204 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
205 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
206 llen - SYMLINK_PREFIX_LEN + 1);
211 return x_stat(path, stp, NULL);
215 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
219 return x_stat(path, stp, NULL);
220 if (x_lstat(path, stp, NULL) < 0)
222 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
224 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
229 return x_stat(path, stp, NULL);
233 /* This function is used to check if a file should be included/excluded
234 * from the list of files based on its name and type etc. The value of
235 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
236 static int is_excluded(char *fname, int is_dir, int filter_level)
238 #if 0 /* This currently never happens, so avoid a useless compare. */
239 if (filter_level == NO_FILTERS)
243 /* never exclude '.', even if somebody does --exclude '*' */
244 if (fname[0] == '.' && !fname[1])
246 /* Handle the -R version of the '.' dir. */
247 if (fname[0] == '/') {
248 int len = strlen(fname);
249 if (fname[len-1] == '.' && fname[len-2] == '/')
253 if (server_filter_list.head
254 && check_filter(&server_filter_list, fname, is_dir) < 0)
256 if (filter_level != ALL_FILTERS)
259 && check_filter(&filter_list, fname, is_dir) < 0)
264 static void send_directory(int f, struct file_list *flist,
265 char *fbuf, int len, int flags);
267 static const char *pathname, *orig_dir;
268 static int pathname_len;
271 /* Make sure flist can hold at least flist->used + extra entries. */
272 static void flist_expand(struct file_list *flist, int extra)
274 struct file_struct **new_ptr;
276 if (flist->used + extra <= flist->malloced)
279 if (flist->malloced < FLIST_START)
280 flist->malloced = FLIST_START;
281 else if (flist->malloced >= FLIST_LINEAR)
282 flist->malloced += FLIST_LINEAR;
284 flist->malloced *= 2;
286 /* In case count jumped or we are starting the list
287 * with a known size just set it. */
288 if (flist->malloced < flist->used + extra)
289 flist->malloced = flist->used + extra;
291 new_ptr = realloc_array(flist->files, struct file_struct *,
294 if (verbose >= 2 && flist->malloced != FLIST_START) {
295 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
297 (double)sizeof flist->files[0] * flist->malloced,
298 (new_ptr == flist->files) ? " not" : "");
301 flist->files = new_ptr;
304 out_of_memory("flist_expand");
307 static void flist_done_allocating(struct file_list *flist)
309 void *ptr = pool_boundary(flist->file_pool, 8*1024);
310 if (flist->pool_boundary == ptr)
311 flist->pool_boundary = NULL; /* list didn't use any pool memory */
313 flist->pool_boundary = ptr;
316 int push_pathname(const char *dir, int len)
322 orig_dir = strdup(curr_dir);
324 if (pathname && !pop_dir(orig_dir)) {
325 rsyserr(FERROR, errno, "pop_dir %s failed",
326 full_fname(orig_dir));
327 exit_cleanup(RERR_FILESELECT);
330 if (dir && !push_dir(dir, 0)) {
331 io_error |= IOERR_GENERAL;
332 rsyserr(FERROR, errno, "push_dir %s failed in %s",
333 full_fname(dir), curr_dir);
338 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
343 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
345 static time_t modtime;
347 #ifdef SUPPORT_HARD_LINKS
351 static uint32 rdev_major;
354 static const char *user_name, *group_name;
355 static char lastname[MAXPATHLEN];
356 char fname[MAXPATHLEN];
357 int first_hlink_ndx = -1;
362 if (ic_send != (iconv_t)-1) {
365 INIT_CONST_XBUF(outbuf, fname);
368 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
369 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
370 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
373 outbuf.buf[outbuf.len++] = '/';
376 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
377 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
379 io_error |= IOERR_GENERAL;
381 "[%s] cannot convert filename: %s (%s)\n",
382 who_am_i(), f_name(file, fname), strerror(errno));
385 outbuf.buf[outbuf.len] = '\0';
390 /* Initialize starting value of xflags. */
391 if (protocol_version >= 30 && S_ISDIR(file->mode)) {
393 if (file->flags & FLAG_CONTENT_DIR)
394 xflags = file->flags & FLAG_TOP_DIR;
395 else if (file->flags & FLAG_IMPLIED_DIR)
396 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
398 xflags = XMIT_NO_CONTENT_DIR;
400 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
402 if (file->mode == mode)
403 xflags |= XMIT_SAME_MODE;
407 if ((preserve_devices && IS_DEVICE(mode))
408 || (preserve_specials && IS_SPECIAL(mode))) {
409 if (protocol_version < 28) {
410 if (tmp_rdev == rdev)
411 xflags |= XMIT_SAME_RDEV_pre28;
416 if ((uint32)major(rdev) == rdev_major)
417 xflags |= XMIT_SAME_RDEV_MAJOR;
419 rdev_major = major(rdev);
420 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
421 xflags |= XMIT_RDEV_MINOR_8_pre30;
423 } else if (protocol_version < 28)
424 rdev = MAKEDEV(0, 0);
426 if ((uid_t)F_OWNER(file) == uid && *lastname)
427 xflags |= XMIT_SAME_UID;
430 if (uid_ndx && !numeric_ids) {
431 user_name = add_uid(uid);
432 if (inc_recurse && user_name)
433 xflags |= XMIT_USER_NAME_FOLLOWS;
438 if ((gid_t)F_GROUP(file) == gid && *lastname)
439 xflags |= XMIT_SAME_GID;
442 if (gid_ndx && !numeric_ids) {
443 group_name = add_gid(gid);
444 if (inc_recurse && group_name)
445 xflags |= XMIT_GROUP_NAME_FOLLOWS;
449 if (file->modtime == modtime)
450 xflags |= XMIT_SAME_TIME;
452 modtime = file->modtime;
454 #ifdef SUPPORT_HARD_LINKS
456 if (protocol_version >= 30) {
457 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
458 first_hlink_ndx = (int32)(long)np->data - 1;
459 if (first_hlink_ndx < 0) {
460 np->data = (void*)(long)(first_ndx + ndx + 1);
461 xflags |= XMIT_HLINK_FIRST;
463 xflags |= XMIT_HLINKED;
465 if (tmp_dev == dev) {
466 if (protocol_version >= 28)
467 xflags |= XMIT_SAME_DEV_pre30;
470 xflags |= XMIT_HLINKED;
476 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
478 l2 = strlen(fname+l1);
481 xflags |= XMIT_SAME_NAME;
483 xflags |= XMIT_LONG_NAME;
485 /* We must make sure we don't send a zero flag byte or the
486 * other end will terminate the flist transfer. Note that
487 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
488 * it's harmless way to add a bit to the first flag byte. */
489 if (protocol_version >= 28) {
490 if (!xflags && !S_ISDIR(mode))
491 xflags |= XMIT_TOP_DIR;
492 if ((xflags & 0xFF00) || !xflags) {
493 xflags |= XMIT_EXTENDED_FLAGS;
494 write_shortint(f, xflags);
496 write_byte(f, xflags);
498 if (!(xflags & 0xFF))
499 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
500 write_byte(f, xflags);
502 if (xflags & XMIT_SAME_NAME)
504 if (xflags & XMIT_LONG_NAME)
505 write_varint30(f, l2);
508 write_buf(f, fname + l1, l2);
510 if (first_hlink_ndx >= 0) {
511 write_varint(f, first_hlink_ndx);
512 if (first_hlink_ndx >= first_ndx)
516 write_varlong30(f, F_LENGTH(file), 3);
517 if (!(xflags & XMIT_SAME_TIME)) {
518 if (protocol_version >= 30)
519 write_varlong(f, modtime, 4);
521 write_int(f, modtime);
523 if (!(xflags & XMIT_SAME_MODE))
524 write_int(f, to_wire_mode(mode));
525 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
526 if (protocol_version < 30)
529 write_varint(f, uid);
530 if (xflags & XMIT_USER_NAME_FOLLOWS) {
531 int len = strlen(user_name);
533 write_buf(f, user_name, len);
537 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
538 if (protocol_version < 30)
541 write_varint(f, gid);
542 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
543 int len = strlen(group_name);
545 write_buf(f, group_name, len);
549 if ((preserve_devices && IS_DEVICE(mode))
550 || (preserve_specials && IS_SPECIAL(mode))) {
551 if (protocol_version < 28) {
552 if (!(xflags & XMIT_SAME_RDEV_pre28))
553 write_int(f, (int)rdev);
555 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
556 write_varint30(f, major(rdev));
557 if (protocol_version >= 30)
558 write_varint(f, minor(rdev));
559 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
560 write_byte(f, minor(rdev));
562 write_int(f, minor(rdev));
567 if (preserve_links && S_ISLNK(mode)) {
568 const char *sl = F_SYMLINK(file);
569 int len = strlen(sl);
570 write_varint30(f, len);
571 write_buf(f, sl, len);
575 #ifdef SUPPORT_HARD_LINKS
576 if (tmp_dev != 0 && protocol_version < 30) {
577 if (protocol_version < 26) {
578 /* 32-bit dev_t and ino_t */
579 write_int(f, (int32)dev);
580 write_int(f, (int32)tmp_ino);
582 /* 64-bit dev_t and ino_t */
583 if (!(xflags & XMIT_SAME_DEV_pre30))
584 write_longint(f, dev);
585 write_longint(f, tmp_ino);
590 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
595 /* Prior to 28, we sent a useless set of nulls. */
598 write_buf(f, sum, checksum_len);
602 strlcpy(lastname, fname, MAXPATHLEN);
604 if (S_ISREG(mode) || S_ISLNK(mode))
605 stats.total_size += F_LENGTH(file);
608 static struct file_struct *recv_file_entry(struct file_list *flist,
611 static int64 modtime;
613 #ifdef SUPPORT_HARD_LINKS
617 static uint32 rdev_major;
620 static uint16 gid_flags;
621 static char lastname[MAXPATHLEN], *lastdir;
622 static int lastdir_depth, lastdir_len = -1;
623 static unsigned int del_hier_name_len = 0;
624 static int in_del_hier = 0;
625 char thisname[MAXPATHLEN];
626 unsigned int l1 = 0, l2 = 0;
627 int alloc_len, basename_len, linkname_len;
628 int extra_len = file_extra_cnt * EXTRA_LEN;
629 int first_hlink_ndx = -1;
631 const char *basename;
632 struct file_struct *file;
636 if (xflags & XMIT_SAME_NAME)
639 if (xflags & XMIT_LONG_NAME)
640 l2 = read_varint30(f);
644 if (l2 >= MAXPATHLEN - l1) {
646 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
647 xflags, l1, l2, lastname, who_am_i());
648 overflow_exit("recv_file_entry");
651 strlcpy(thisname, lastname, l1 + 1);
652 read_sbuf(f, &thisname[l1], l2);
653 thisname[l1 + l2] = 0;
655 /* Abuse basename_len for a moment... */
656 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
659 if (ic_recv != (iconv_t)-1) {
662 INIT_CONST_XBUF(outbuf, thisname);
663 INIT_XBUF(inbuf, lastname, basename_len, -1);
665 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
666 io_error |= IOERR_GENERAL;
668 "[%s] cannot convert filename: %s (%s)\n",
669 who_am_i(), lastname, strerror(errno));
672 outbuf.buf[outbuf.len] = '\0';
676 clean_fname(thisname, 0);
679 sanitize_path(thisname, thisname, "", 0);
681 if ((basename = strrchr(thisname, '/')) != NULL) {
682 int len = basename++ - thisname;
683 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
684 lastdir = new_array(char, len + 1);
685 memcpy(lastdir, thisname, len);
688 lastdir_depth = count_dir_elements(lastdir);
692 basename_len = strlen(basename) + 1; /* count the '\0' */
694 #ifdef SUPPORT_HARD_LINKS
695 if (protocol_version >= 30
696 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
697 first_hlink_ndx = read_varint(f);
698 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
700 "hard-link reference out of range: %d (%d)\n",
701 first_hlink_ndx, flist->ndx_start + flist->used);
702 exit_cleanup(RERR_PROTOCOL);
704 if (first_hlink_ndx >= flist->ndx_start) {
705 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
706 file_length = F_LENGTH(first);
707 modtime = first->modtime;
710 uid = F_OWNER(first);
712 gid = F_GROUP(first);
713 if ((preserve_devices && IS_DEVICE(mode))
714 || (preserve_specials && IS_SPECIAL(mode))) {
715 uint32 *devp = F_RDEV_P(first);
716 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
717 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
719 if (preserve_links && S_ISLNK(mode))
720 linkname_len = strlen(F_SYMLINK(first)) + 1;
728 file_length = read_varlong30(f, 3);
729 if (!(xflags & XMIT_SAME_TIME)) {
730 if (protocol_version >= 30) {
731 modtime = read_varlong(f, 4);
732 #if SIZEOF_TIME_T < SIZEOF_INT64
733 if (!am_generator && (int64)(time_t)modtime != modtime) {
735 "Time value of %s truncated on receiver.\n",
740 modtime = read_int(f);
742 if (!(xflags & XMIT_SAME_MODE))
743 mode = from_wire_mode(read_int(f));
745 if (chmod_modes && !S_ISLNK(mode))
746 mode = tweak_mode(mode, chmod_modes);
748 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
749 if (protocol_version < 30)
750 uid = (uid_t)read_int(f);
752 uid = (uid_t)read_varint(f);
753 if (xflags & XMIT_USER_NAME_FOLLOWS)
754 uid = recv_user_name(f, uid);
755 else if (inc_recurse && am_root && !numeric_ids)
756 uid = match_uid(uid);
759 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
760 if (protocol_version < 30)
761 gid = (gid_t)read_int(f);
763 gid = (gid_t)read_varint(f);
765 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
766 gid = recv_group_name(f, gid, &gid_flags);
767 else if (inc_recurse && (!am_root || !numeric_ids))
768 gid = match_gid(gid, &gid_flags);
772 if ((preserve_devices && IS_DEVICE(mode))
773 || (preserve_specials && IS_SPECIAL(mode))) {
774 if (protocol_version < 28) {
775 if (!(xflags & XMIT_SAME_RDEV_pre28))
776 rdev = (dev_t)read_int(f);
779 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
780 rdev_major = read_varint30(f);
781 if (protocol_version >= 30)
782 rdev_minor = read_varint(f);
783 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
784 rdev_minor = read_byte(f);
786 rdev_minor = read_int(f);
787 rdev = MAKEDEV(rdev_major, rdev_minor);
789 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
791 } else if (protocol_version < 28)
792 rdev = MAKEDEV(0, 0);
795 if (preserve_links && S_ISLNK(mode)) {
796 linkname_len = read_varint30(f) + 1; /* count the '\0' */
797 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
798 rprintf(FERROR, "overflow: linkname_len=%d\n",
800 overflow_exit("recv_file_entry");
803 linkname_len += SYMLINK_PREFIX_LEN;
809 #ifdef SUPPORT_HARD_LINKS
811 if (preserve_hard_links) {
812 if (protocol_version < 28 && S_ISREG(mode))
813 xflags |= XMIT_HLINKED;
814 if (xflags & XMIT_HLINKED)
815 extra_len += (inc_recurse+1) * EXTRA_LEN;
820 /* We need one or two index int32s when we're preserving ACLs. */
822 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
825 if (always_checksum && S_ISREG(mode))
826 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
828 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
829 extra_len += EXTRA_LEN;
831 if (inc_recurse && S_ISDIR(mode)) {
832 if (one_file_system) {
833 /* Room to save the dir's device for -x */
834 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
836 pool = dir_flist->file_pool;
838 pool = flist->file_pool;
840 #if EXTRA_ROUNDING > 0
841 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
842 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
845 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
847 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
849 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
851 file = (struct file_struct *)bp;
852 bp += FILE_STRUCT_LEN;
854 memcpy(bp, basename, basename_len);
855 bp += basename_len + linkname_len; /* skip space for symlink too */
857 #ifdef SUPPORT_HARD_LINKS
858 if (xflags & XMIT_HLINKED)
859 file->flags |= FLAG_HLINKED;
861 file->modtime = (time_t)modtime;
862 file->len32 = (uint32)file_length;
863 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
864 file->flags |= FLAG_LENGTH64;
865 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
872 file->flags |= gid_flags;
875 F_NDX(file) = flist->used + flist->ndx_start;
877 if (basename != thisname) {
878 file->dirname = lastdir;
879 F_DEPTH(file) = lastdir_depth + 1;
884 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
886 if (protocol_version >= 30) {
887 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
888 if (xflags & XMIT_TOP_DIR)
889 file->flags |= FLAG_TOP_DIR;
890 file->flags |= FLAG_CONTENT_DIR;
891 } else if (xflags & XMIT_TOP_DIR)
892 file->flags |= FLAG_IMPLIED_DIR;
893 } else if (xflags & XMIT_TOP_DIR) {
894 in_del_hier = recurse;
895 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
896 if (relative_paths && del_hier_name_len > 2
897 && lastname[del_hier_name_len-1] == '.'
898 && lastname[del_hier_name_len-2] == '/')
899 del_hier_name_len -= 2;
900 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
901 } else if (in_del_hier) {
902 if (!relative_paths || !del_hier_name_len
903 || (l1 >= del_hier_name_len
904 && lastname[del_hier_name_len] == '/'))
905 file->flags |= FLAG_CONTENT_DIR;
911 if ((preserve_devices && IS_DEVICE(mode))
912 || (preserve_specials && IS_SPECIAL(mode))) {
913 uint32 *devp = F_RDEV_P(file);
914 DEV_MAJOR(devp) = major(rdev);
915 DEV_MINOR(devp) = minor(rdev);
920 bp = (char*)file->basename + basename_len;
921 if (first_hlink_ndx >= flist->ndx_start) {
922 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
923 memcpy(bp, F_SYMLINK(first), linkname_len);
924 } else if (munge_symlinks) {
925 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
926 bp += SYMLINK_PREFIX_LEN;
927 linkname_len -= SYMLINK_PREFIX_LEN;
928 read_sbuf(f, bp, linkname_len - 1);
930 read_sbuf(f, bp, linkname_len - 1);
932 sanitize_path(bp, bp, "", lastdir_depth);
937 #ifdef SUPPORT_HARD_LINKS
938 if (preserve_hard_links && xflags & XMIT_HLINKED) {
939 if (protocol_version >= 30) {
940 if (xflags & XMIT_HLINK_FIRST) {
941 F_HL_GNUM(file) = flist->ndx_start + flist->used;
943 F_HL_GNUM(file) = first_hlink_ndx;
945 static int32 cnt = 0;
946 struct ht_int64_node *np;
949 if (protocol_version < 26) {
953 if (!(xflags & XMIT_SAME_DEV_pre30))
954 dev = read_longint(f);
955 ino = read_longint(f);
957 np = idev_find(dev, ino);
958 ndx = (int32)(long)np->data - 1;
961 np->data = (void*)(long)cnt;
963 F_HL_GNUM(file) = ndx;
968 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
972 /* Prior to 28, we get a useless set of nulls. */
975 if (first_hlink_ndx >= flist->ndx_start) {
976 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
977 memcpy(bp, F_SUM(first), checksum_len);
979 read_buf(f, bp, checksum_len);
983 if (preserve_acls && !S_ISLNK(mode))
984 receive_acl(file, f);
986 #ifdef SUPPORT_XATTRS
988 receive_xattr(file, f );
991 if (S_ISREG(mode) || S_ISLNK(mode))
992 stats.total_size += file_length;
997 /* Create a file_struct for a named file by reading its stat() information
998 * and performing extensive checks against global options.
1000 * Returns a pointer to the new file struct, or NULL if there was an error
1001 * or this file should be excluded. */
1002 struct file_struct *make_file(const char *fname, struct file_list *flist,
1003 STRUCT_STAT *stp, int flags, int filter_level)
1005 static char *lastdir;
1006 static int lastdir_len = -1;
1007 struct file_struct *file;
1008 char thisname[MAXPATHLEN];
1009 char linkname[MAXPATHLEN];
1010 int alloc_len, basename_len, linkname_len;
1011 int extra_len = file_extra_cnt * EXTRA_LEN;
1012 const char *basename;
1017 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1018 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1021 clean_fname(thisname, 0);
1023 sanitize_path(thisname, thisname, "", 0);
1025 if (stp && S_ISDIR(stp->st_mode)) {
1026 st = *stp; /* Needed for "symlink/." with --relative. */
1027 *linkname = '\0'; /* make IBM code checker happy */
1028 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1029 int save_errno = errno;
1030 /* See if file is excluded before reporting an error. */
1031 if (filter_level != NO_FILTERS
1032 && (is_excluded(thisname, 0, filter_level)
1033 || is_excluded(thisname, 1, filter_level))) {
1034 if (ignore_perishable && save_errno != ENOENT)
1035 non_perishable_cnt++;
1038 if (save_errno == ENOENT) {
1039 #ifdef SUPPORT_LINKS
1040 /* When our options tell us to follow a symlink that
1041 * points nowhere, tell the user about the symlink
1042 * instead of giving a "vanished" message. We only
1043 * dereference a symlink if one of the --copy*links
1044 * options was specified, so there's no need for the
1045 * extra lstat() if one of these options isn't on. */
1046 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1047 && x_lstat(thisname, &st, NULL) == 0
1048 && S_ISLNK(st.st_mode)) {
1049 io_error |= IOERR_GENERAL;
1050 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1051 full_fname(thisname));
1055 enum logcode c = am_daemon && protocol_version < 28
1056 ? FERROR : FWARNING;
1057 io_error |= IOERR_VANISHED;
1058 rprintf(c, "file has vanished: %s\n",
1059 full_fname(thisname));
1062 io_error |= IOERR_GENERAL;
1063 rsyserr(FERROR_XFER, save_errno, "readlink %s failed",
1064 full_fname(thisname));
1069 /* backup.c calls us with filter_level set to NO_FILTERS. */
1070 if (filter_level == NO_FILTERS)
1073 if (S_ISDIR(st.st_mode)) {
1075 rprintf(FINFO, "skipping directory %s\n", thisname);
1078 /* -x only affects dirs because we need to avoid recursing
1079 * into a mount-point directory, not to avoid copying a
1080 * symlinked file if -L (or similar) was specified. */
1081 if (one_file_system && st.st_dev != filesystem_dev
1082 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1083 if (one_file_system > 1) {
1086 "[%s] skipping mount-point dir %s\n",
1087 who_am_i(), thisname);
1091 flags |= FLAG_MOUNT_DIR;
1092 flags &= ~FLAG_CONTENT_DIR;
1095 flags &= ~FLAG_CONTENT_DIR;
1097 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1098 if (ignore_perishable)
1099 non_perishable_cnt++;
1103 if (lp_ignore_nonreadable(module_id)) {
1104 #ifdef SUPPORT_LINKS
1105 if (!S_ISLNK(st.st_mode))
1107 if (access(thisname, R_OK) != 0)
1113 /* Only divert a directory in the main transfer. */
1115 if (flist->prev && S_ISDIR(st.st_mode)
1116 && flags & FLAG_DIVERT_DIRS) {
1117 /* Room for parent/sibling/next-child info. */
1118 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1120 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1121 pool = dir_flist->file_pool;
1123 pool = flist->file_pool;
1128 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1129 who_am_i(), thisname, filter_level);
1132 if ((basename = strrchr(thisname, '/')) != NULL) {
1133 int len = basename++ - thisname;
1134 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1135 lastdir = new_array(char, len + 1);
1136 memcpy(lastdir, thisname, len);
1137 lastdir[len] = '\0';
1141 basename = thisname;
1142 basename_len = strlen(basename) + 1; /* count the '\0' */
1144 #ifdef SUPPORT_LINKS
1145 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1150 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1151 extra_len += EXTRA_LEN;
1153 #if EXTRA_ROUNDING > 0
1154 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1155 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1158 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1161 bp = pool_alloc(pool, alloc_len, "make_file");
1163 if (!(bp = new_array(char, alloc_len)))
1164 out_of_memory("make_file");
1167 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1169 file = (struct file_struct *)bp;
1170 bp += FILE_STRUCT_LEN;
1172 memcpy(bp, basename, basename_len);
1173 bp += basename_len + linkname_len; /* skip space for symlink too */
1175 #ifdef SUPPORT_HARD_LINKS
1176 if (preserve_hard_links && flist && flist->prev) {
1177 if (protocol_version >= 28
1178 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1179 : S_ISREG(st.st_mode)) {
1180 tmp_dev = st.st_dev;
1181 tmp_ino = st.st_ino;
1187 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1188 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1189 tmp_rdev = st.st_rdev;
1194 file->flags = flags;
1195 file->modtime = st.st_mtime;
1196 file->len32 = (uint32)st.st_size;
1197 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1198 file->flags |= FLAG_LENGTH64;
1199 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1201 file->mode = st.st_mode;
1203 F_OWNER(file) = st.st_uid;
1205 F_GROUP(file) = st.st_gid;
1207 if (basename != thisname)
1208 file->dirname = lastdir;
1210 #ifdef SUPPORT_LINKS
1212 bp = (char*)file->basename + basename_len;
1213 memcpy(bp, linkname, linkname_len);
1217 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1218 file_checksum(thisname, tmp_sum, st.st_size);
1220 F_PATHNAME(file) = pathname;
1222 /* This code is only used by the receiver when it is building
1223 * a list of files for a delete pass. */
1224 if (keep_dirlinks && linkname_len && flist) {
1226 int save_mode = file->mode;
1227 file->mode = S_IFDIR; /* Find a directory with our name. */
1228 if (flist_find(dir_flist, file) >= 0
1229 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1230 file->modtime = st2.st_mtime;
1232 file->mode = st2.st_mode;
1234 F_OWNER(file) = st2.st_uid;
1236 F_GROUP(file) = st2.st_gid;
1238 file->mode = save_mode;
1241 if (basename_len == 0+1) {
1248 F_NDX(file) = dir_count;
1253 /* Only called for temporary file_struct entries created by make_file(). */
1254 void unmake_file(struct file_struct *file)
1256 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1257 #if EXTRA_ROUNDING > 0
1258 if (extra_cnt & EXTRA_ROUNDING)
1259 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1261 free(REQ_EXTRA(file, extra_cnt));
1264 static struct file_struct *send_file_name(int f, struct file_list *flist,
1265 char *fname, STRUCT_STAT *stp,
1266 int flags, int filter_level)
1268 struct file_struct *file;
1269 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1273 file = make_file(fname, flist, stp, flags, filter_level);
1277 if (chmod_modes && !S_ISLNK(file->mode))
1278 file->mode = tweak_mode(file->mode, chmod_modes);
1281 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1282 sx.st.st_mode = file->mode;
1283 sx.acc_acl = sx.def_acl = NULL;
1284 if (get_acl(fname, &sx) < 0)
1288 #ifdef SUPPORT_XATTRS
1289 if (preserve_xattrs && f >= 0) {
1291 if (get_xattr(fname, &sx) < 0)
1296 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1298 flist_expand(flist, 1);
1299 flist->files[flist->used++] = file;
1301 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1303 if (preserve_acls && !S_ISLNK(file->mode)) {
1308 #ifdef SUPPORT_XATTRS
1309 if (preserve_xattrs) {
1310 F_XATTR(file) = send_xattr(&sx, f);
1318 static void send_if_directory(int f, struct file_list *flist,
1319 struct file_struct *file,
1320 char *fbuf, unsigned int ol,
1323 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1325 if (S_ISDIR(file->mode)
1326 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1328 unsigned int len = strlen(fbuf);
1329 if (len > 1 && fbuf[len-1] == '/')
1331 if (len >= MAXPATHLEN - 1) {
1332 io_error |= IOERR_GENERAL;
1333 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1337 save_filters = push_local_filters(fbuf, len);
1338 send_directory(f, flist, fbuf, len, flags);
1339 pop_local_filters(save_filters);
1346 static int file_compare(const void *file1, const void *file2)
1348 return f_name_cmp(*(struct file_struct **)file1,
1349 *(struct file_struct **)file2);
1352 /* The guts of a merge-sort algorithm. This was derived from the glibc
1353 * version, but I (Wayne) changed the merge code to do less copying and
1354 * to require only half the amount of temporary memory. */
1355 static void fsort_tmp(struct file_struct **fp, size_t num,
1356 struct file_struct **tmp)
1358 struct file_struct **f1, **f2, **t;
1367 fsort_tmp(f1, n1, tmp);
1369 fsort_tmp(f2, n2, tmp);
1371 while (f_name_cmp(*f1, *f2) <= 0) {
1378 memcpy(t, f1, n1 * PTR_SIZE);
1380 *f1++ = *f2++, n2--;
1382 while (n1 > 0 && n2 > 0) {
1383 if (f_name_cmp(*t, *f2) <= 0)
1386 *f1++ = *f2++, n2--;
1390 memcpy(f1, t, n1 * PTR_SIZE);
1393 /* This file-struct sorting routine makes sure that any identical names in
1394 * the file list stay in the same order as they were in the original list.
1395 * This is particularly vital in inc_recurse mode where we expect a sort
1396 * on the flist to match the exact order of a sort on the dir_flist. */
1397 static void fsort(struct file_struct **fp, size_t num)
1403 qsort(fp, num, PTR_SIZE, file_compare);
1405 struct file_struct **tmp = new_array(struct file_struct *,
1407 fsort_tmp(fp, num, tmp);
1412 /* We take an entire set of sibling dirs from the sorted flist and link them
1413 * into the tree, setting the appropriate parent/child/sibling pointers. */
1414 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1419 int32 *parent_dp = parent_ndx < 0 ? NULL
1420 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1422 flist_expand(dir_flist, dir_cnt);
1423 dir_flist->sorted = dir_flist->files;
1425 for (i = 0; dir_cnt; i++) {
1426 struct file_struct *file = from_flist->sorted[i];
1428 if (!S_ISDIR(file->mode))
1431 dir_flist->files[dir_flist->used++] = file;
1434 if (file->basename[0] == '.' && file->basename[1] == '\0')
1438 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1440 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1442 send_dir_ndx = dir_flist->used - 1;
1444 dp = F_DIR_NODE_P(file);
1445 DIR_PARENT(dp) = parent_ndx;
1446 DIR_FIRST_CHILD(dp) = -1;
1449 DIR_NEXT_SIBLING(dp) = -1;
1452 /* This function is normally called by the sender, but the receiving side also
1453 * calls it from get_dirlist() with f set to -1 so that we just construct the
1454 * file list in memory without sending it over the wire. Also, get_dirlist()
1455 * might call this with f set to -2, which also indicates that local filter
1456 * rules should be ignored. */
1457 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1464 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1465 int start = flist->used;
1466 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1468 assert(flist != NULL);
1470 if (!(d = opendir(fbuf))) {
1471 io_error |= IOERR_GENERAL;
1472 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1477 if (len != 1 || *fbuf != '/')
1480 remainder = MAXPATHLEN - (p - fbuf);
1482 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1483 char *dname = d_name(di);
1484 if (dname[0] == '.' && (dname[1] == '\0'
1485 || (dname[1] == '.' && dname[2] == '\0')))
1487 if (strlcpy(p, dname, remainder) >= remainder) {
1488 io_error |= IOERR_GENERAL;
1490 "cannot send long-named file %s\n",
1494 if (dname[0] == '\0') {
1495 io_error |= IOERR_GENERAL;
1497 "cannot send file with empty name in %s\n",
1502 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1508 io_error |= IOERR_GENERAL;
1509 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1514 if (f >= 0 && recurse && !divert_dirs) {
1515 int i, end = flist->used - 1;
1516 /* send_if_directory() bumps flist->used, so use "end". */
1517 for (i = start; i <= end; i++)
1518 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1522 static char lastpath[MAXPATHLEN] = "";
1523 static int lastpath_len = 0;
1524 static struct file_struct *lastpath_struct;
1526 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1527 char *start, char *limit, int flags, char name_type)
1529 struct file_struct *file;
1530 item_list *relname_list;
1531 relnamecache **rnpp;
1533 int len, need_new_dir;
1534 struct filter_list_struct save_filter_list = filter_list;
1536 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1537 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1540 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1541 && lastpath_len == limit - fname
1542 && strncmp(lastpath, fname, lastpath_len) == 0)
1550 int save_copy_links = copy_links;
1551 int save_xfer_dirs = xfer_dirs;
1553 copy_links = xfer_dirs = 1;
1557 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1559 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1563 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1565 if (file && !S_ISDIR(file->mode))
1567 lastpath_struct = file;
1570 strlcpy(lastpath, fname, sizeof lastpath);
1571 lastpath_len = limit - fname;
1575 copy_links = save_copy_links;
1576 xfer_dirs = save_xfer_dirs;
1582 if (!lastpath_struct)
1583 goto done; /* dir must have vanished */
1585 len = strlen(limit+1);
1586 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1587 if (!relname_list) {
1588 if (!(relname_list = new0(item_list)))
1589 out_of_memory("send_implied_dirs");
1590 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1592 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1593 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1594 out_of_memory("send_implied_dirs");
1595 (*rnpp)->name_type = name_type;
1596 strlcpy((*rnpp)->fname, limit+1, len + 1);
1599 filter_list = save_filter_list;
1602 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1604 char fbuf[MAXPATHLEN];
1605 item_list *relname_list;
1606 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1610 dlen = strlen(fbuf);
1612 if (F_PATHNAME(file) != pathname) {
1613 if (!push_pathname(F_PATHNAME(file), -1))
1614 exit_cleanup(RERR_FILESELECT);
1617 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1619 if (file->flags & FLAG_CONTENT_DIR) {
1620 if (one_file_system) {
1622 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1623 io_error |= IOERR_GENERAL;
1624 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1628 filesystem_dev = st.st_dev;
1630 send_directory(f, flist, fbuf, dlen, flags);
1633 if (!relative_paths)
1636 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1640 for (j = 0; j < relname_list->count; j++) {
1642 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1643 char name_type = rnp->name_type;
1646 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1648 if (len >= (int)sizeof fbuf)
1649 continue; /* Impossible... */
1651 slash = strchr(fbuf+dlen+1, '/');
1653 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1657 if (name_type != NORMAL_NAME) {
1659 if (link_stat(fbuf, &st, 1) != 0) {
1660 io_error |= IOERR_GENERAL;
1661 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1665 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1667 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1673 void send_extra_file_list(int f, int at_least)
1675 struct file_list *flist;
1678 int old_cnt, save_io_error = io_error;
1683 /* Keep sending data until we have the requested number of
1684 * files in the upcoming file-lists. */
1685 old_cnt = cur_flist->used;
1686 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1687 old_cnt += flist->used;
1688 while (file_total - old_cnt < at_least) {
1689 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1690 int dir_ndx, dstart = dir_count;
1691 const char *pathname = F_PATHNAME(file);
1694 flist = flist_new(0, "send_extra_file_list");
1695 start_write = stats.total_written;
1698 dir_ndx = F_NDX(file);
1700 dir_ndx = send_dir_ndx;
1701 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1702 flist->parent_ndx = dir_ndx;
1704 send1extra(f, file, flist);
1705 prev_flags = file->flags;
1706 dp = F_DIR_NODE_P(file);
1708 /* If there are any duplicate directory names that follow, we
1709 * send all the dirs together in one file-list. The dir_flist
1710 * tree links all the child subdirs onto the last dup dir. */
1711 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1712 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1713 send_dir_ndx = dir_ndx;
1714 file = dir_flist->sorted[dir_ndx];
1715 /* Try to avoid some duplicate scanning of identical dirs. */
1716 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1717 file->flags &= ~FLAG_CONTENT_DIR;
1718 send1extra(f, file, flist);
1719 prev_flags = file->flags;
1720 dp = F_DIR_NODE_P(file);
1725 if (need_unsorted_flist) {
1726 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1727 out_of_memory("send_extra_file_list");
1728 memcpy(flist->sorted, flist->files,
1729 flist->used * sizeof (struct file_struct*));
1731 flist->sorted = flist->files;
1733 flist_sort_and_clean(flist, 0);
1735 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1736 flist_done_allocating(flist);
1738 file_total += flist->used;
1739 stats.flist_size += stats.total_written - start_write;
1740 stats.num_files += flist->used;
1742 output_flist(flist);
1744 if (DIR_FIRST_CHILD(dp) >= 0) {
1745 send_dir_ndx = DIR_FIRST_CHILD(dp);
1748 while (DIR_NEXT_SIBLING(dp) < 0) {
1749 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1750 write_ndx(f, NDX_FLIST_EOF);
1752 change_local_filter_dir(NULL, 0, 0);
1756 file = dir_flist->sorted[send_dir_ndx];
1757 dp = F_DIR_NODE_P(file);
1759 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1764 if (io_error != save_io_error && !ignore_errors)
1765 send_msg_int(MSG_IO_ERROR, io_error);
1768 struct file_list *send_file_list(int f, int argc, char *argv[])
1770 static const char *lastdir;
1771 static int lastdir_len = -1;
1775 struct file_list *flist;
1776 struct timeval start_tv, end_tv;
1779 int disable_buffering;
1780 int flags = recurse ? FLAG_CONTENT_DIR : 0;
1781 int reading_remotely = filesfrom_host != NULL;
1782 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1784 | (filesfrom_convert ? RL_CONVERT : 0)
1786 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1788 rprintf(FLOG, "building file list\n");
1789 if (show_filelist_p())
1790 start_filelist_progress("building file list");
1791 else if (inc_recurse && verbose && !am_server)
1792 rprintf(FCLIENT, "sending incremental file list\n");
1794 start_write = stats.total_written;
1795 gettimeofday(&start_tv, NULL);
1797 if (relative_paths && protocol_version >= 30)
1798 implied_dirs = 1; /* We send flagged implied dirs */
1800 #ifdef SUPPORT_HARD_LINKS
1801 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1805 flist = cur_flist = flist_new(0, "send_file_list");
1807 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1808 flags |= FLAG_DIVERT_DIRS;
1810 dir_flist = cur_flist;
1812 disable_buffering = io_start_buffering_out(f);
1813 if (filesfrom_fd >= 0) {
1814 if (argv[0] && !push_dir(argv[0], 0)) {
1815 rsyserr(FERROR_XFER, errno, "push_dir %s failed in %s",
1816 full_fname(argv[0]), curr_dir);
1817 exit_cleanup(RERR_FILESELECT);
1823 char fbuf[MAXPATHLEN], *fn, name_type;
1826 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1828 sanitize_path(fbuf, fbuf, "", 0);
1832 strlcpy(fbuf, *argv++, MAXPATHLEN);
1834 sanitize_path(fbuf, fbuf, "", 0);
1838 if (relative_paths) {
1839 /* We clean up fbuf below. */
1840 name_type = NORMAL_NAME;
1841 } else if (!len || fbuf[len - 1] == '/') {
1842 if (len == 2 && fbuf[0] == '.') {
1843 /* Turn "./" into just "." rather than "./." */
1846 if (len + 1 >= MAXPATHLEN)
1847 overflow_exit("send_file_list");
1851 name_type = DOT_NAME;
1852 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1853 && (len == 2 || fbuf[len-3] == '/')) {
1854 if (len + 2 >= MAXPATHLEN)
1855 overflow_exit("send_file_list");
1859 name_type = DOT_NAME;
1860 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
1861 name_type = DOT_NAME;
1863 name_type = NORMAL_NAME;
1867 if (!relative_paths) {
1868 p = strrchr(fbuf, '/');
1875 len -= p - fbuf + 1;
1880 if ((p = strstr(fbuf, "/./")) != NULL) {
1890 *--fn = '\0'; /* ensure room for '.' */
1893 len = clean_fname(fn, CFN_KEEP_LEADING_DOT_DIR
1894 | CFN_KEEP_TRAILING_SLASH
1895 | CFN_DROP_TRAILING_DOT_DIR);
1900 name_type = DOT_NAME;
1901 } else if (fn[0] == '.')
1902 name_type = DOT_NAME;
1903 } else if (fn[len-1] == '/') {
1905 if (len == 1 && *fn == '.')
1906 name_type = DOT_NAME;
1908 name_type = SLASH_ENDING_NAME;
1910 /* Reject a ".." dir in the active part of the path. */
1911 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1912 if ((p[2] == '/' || p[2] == '\0')
1913 && (p == fn || p[-1] == '/')) {
1915 "found \"..\" dir in relative path: %s\n",
1917 exit_cleanup(RERR_SYNTAX);
1925 name_type = DOT_NAME;
1928 dirlen = dir ? strlen(dir) : 0;
1929 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1930 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1933 lastdir_len = pathname_len;
1934 } else if (!push_pathname(lastdir, lastdir_len))
1938 memmove(fbuf, fn, len + 1);
1940 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0) {
1941 io_error |= IOERR_GENERAL;
1942 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1947 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1948 rprintf(FINFO, "skipping directory %s\n", fbuf);
1952 if (inc_recurse && relative_paths && *fbuf) {
1953 if ((p = strchr(fbuf+1, '/')) != NULL) {
1954 if (p - fbuf == 1 && *fbuf == '.') {
1955 if ((fn = strchr(p+1, '/')) != NULL)
1959 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
1963 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1964 /* Send the implied directories at the start of the
1965 * source spec, so we get their permissions right. */
1966 char *lp = lastpath, *slash = fbuf;
1968 /* Skip any initial directories in our path that we
1969 * have in common with lastpath. */
1970 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1975 if (fn != p || (*lp && *lp != '/'))
1976 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1979 if (one_file_system)
1980 filesystem_dev = st.st_dev;
1982 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
1983 struct file_struct *file;
1984 int top_flags = FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags;
1985 file = send_file_name(f, flist, fbuf, &st,
1986 top_flags, ALL_FILTERS);
1988 if (name_type == DOT_NAME && file) {
1989 if (send_dir_depth < 0) {
1991 change_local_filter_dir(fbuf, len, send_dir_depth);
1993 send_directory(f, flist, fbuf, len, flags);
1996 send_if_directory(f, flist, file, fbuf, len, flags);
1998 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
2001 gettimeofday(&end_tv, NULL);
2002 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2003 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2004 if (stats.flist_buildtime == 0)
2005 stats.flist_buildtime = 1;
2008 write_byte(f, 0); /* Indicate end of file list */
2010 #ifdef SUPPORT_HARD_LINKS
2011 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2015 if (show_filelist_p())
2016 finish_filelist_progress(flist);
2018 gettimeofday(&end_tv, NULL);
2019 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2020 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2022 /* When converting names, both sides keep an unsorted file-list array
2023 * because the names will differ on the sending and receiving sides
2024 * (both sides will use the unsorted index number for each item). */
2026 /* Sort the list without removing any duplicates. This allows the
2027 * receiving side to ask for whatever name it kept. For incremental
2028 * recursion mode, the sender marks duplicate dirs so that it can
2029 * send them together in a single file-list. */
2030 if (need_unsorted_flist) {
2031 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2032 out_of_memory("send_file_list");
2033 memcpy(flist->sorted, flist->files,
2034 flist->used * sizeof (struct file_struct*));
2036 flist->sorted = flist->files;
2037 flist_sort_and_clean(flist, 0);
2038 file_total += flist->used;
2040 if (numeric_ids <= 0 && !inc_recurse)
2043 /* send the io_error flag */
2044 if (protocol_version < 30)
2045 write_int(f, ignore_errors ? 0 : io_error);
2046 else if (io_error && !ignore_errors)
2047 send_msg_int(MSG_IO_ERROR, io_error);
2049 if (disable_buffering)
2050 io_end_buffering_out();
2052 stats.flist_size = stats.total_written - start_write;
2053 stats.num_files = flist->used;
2056 output_flist(flist);
2059 rprintf(FINFO, "send_file_list done\n");
2063 add_dirs_to_tree(-1, flist, dir_count);
2064 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2065 flist->parent_ndx = -1;
2066 flist_done_allocating(flist);
2067 if (send_dir_ndx < 0) {
2068 write_ndx(f, NDX_FLIST_EOF);
2071 else if (file_total == 1) {
2072 /* If we're creating incremental file-lists and there
2073 * was just 1 item in the first file-list, send 1 more
2074 * file-list to check if this is a 1-file xfer. */
2075 send_extra_file_list(f, 1);
2082 struct file_list *recv_file_list(int f)
2084 struct file_list *flist;
2089 rprintf(FLOG, "receiving file list\n");
2090 if (show_filelist_p())
2091 start_filelist_progress("receiving file list");
2092 else if (inc_recurse && verbose && !am_server && !first_flist)
2093 rprintf(FCLIENT, "receiving incremental file list\n");
2095 start_read = stats.total_read;
2097 #ifdef SUPPORT_HARD_LINKS
2098 if (preserve_hard_links && !first_flist)
2102 flist = flist_new(0, "recv_file_list");
2105 if (flist->ndx_start == 1)
2106 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2107 dstart = dir_flist->used;
2113 while ((flags = read_byte(f)) != 0) {
2114 struct file_struct *file;
2116 flist_expand(flist, 1);
2118 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2119 flags |= read_byte(f) << 8;
2120 file = recv_file_entry(flist, flags, f);
2122 if (inc_recurse && S_ISDIR(file->mode)) {
2123 flist_expand(dir_flist, 1);
2124 dir_flist->files[dir_flist->used++] = file;
2127 flist->files[flist->used++] = file;
2129 maybe_emit_filelist_progress(flist->used);
2132 rprintf(FINFO, "recv_file_name(%s)\n",
2133 f_name(file, NULL));
2136 file_total += flist->used;
2139 rprintf(FINFO, "received %d names\n", flist->used);
2141 if (show_filelist_p())
2142 finish_filelist_progress(flist);
2144 if (need_unsorted_flist) {
2145 /* Create an extra array of index pointers that we can sort for
2146 * the generator's use (for wading through the files in sorted
2147 * order and for calling flist_find()). We keep the "files"
2148 * list unsorted for our exchange of index numbers with the
2149 * other side (since their names may not sort the same). */
2150 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2151 out_of_memory("recv_file_list");
2152 memcpy(flist->sorted, flist->files,
2153 flist->used * sizeof (struct file_struct*));
2154 if (inc_recurse && dir_flist->used > dstart) {
2155 static int dir_flist_malloced = 0;
2156 if (dir_flist_malloced < dir_flist->malloced) {
2157 dir_flist->sorted = realloc_array(dir_flist->sorted,
2158 struct file_struct *,
2159 dir_flist->malloced);
2160 dir_flist_malloced = dir_flist->malloced;
2162 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2163 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2164 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2167 flist->sorted = flist->files;
2168 if (inc_recurse && dir_flist->used > dstart) {
2169 dir_flist->sorted = dir_flist->files;
2170 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2175 flist_done_allocating(flist);
2177 recv_id_list(f, flist);
2179 flist_sort_and_clean(flist, relative_paths);
2181 if (protocol_version < 30) {
2182 /* Recv the io_error flag */
2186 io_error |= read_int(f);
2187 } else if (inc_recurse && flist->ndx_start == 1) {
2188 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2189 flist->parent_ndx = -1;
2193 output_flist(flist);
2196 rprintf(FINFO, "recv_file_list done\n");
2198 stats.flist_size += stats.total_read - start_read;
2199 stats.num_files += flist->used;
2204 /* This is only used once by the receiver if the very first file-list
2205 * has exactly one item in it. */
2206 void recv_additional_file_list(int f)
2208 struct file_list *flist;
2209 int ndx = read_ndx(f);
2210 if (ndx == NDX_FLIST_EOF) {
2212 change_local_filter_dir(NULL, 0, 0);
2214 ndx = NDX_FLIST_OFFSET - ndx;
2215 if (ndx < 0 || ndx >= dir_flist->used) {
2216 ndx = NDX_FLIST_OFFSET - ndx;
2218 "[%s] Invalid dir index: %d (%d - %d)\n",
2219 who_am_i(), ndx, NDX_FLIST_OFFSET,
2220 NDX_FLIST_OFFSET - dir_flist->used + 1);
2221 exit_cleanup(RERR_PROTOCOL);
2224 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2227 flist = recv_file_list(f);
2228 flist->parent_ndx = ndx;
2232 /* Search for an identically-named item in the file list. Note that the
2233 * items must agree in their directory-ness, or no match is returned. */
2234 int flist_find(struct file_list *flist, struct file_struct *f)
2236 int low = flist->low, high = flist->high;
2237 int diff, mid, mid_up;
2239 while (low <= high) {
2240 mid = (low + high) / 2;
2241 if (F_IS_ACTIVE(flist->sorted[mid]))
2244 /* Scan for the next non-empty entry using the cached
2245 * distance values. If the value isn't fully up-to-
2246 * date, update it. */
2247 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2248 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2250 mid_up += F_DEPTH(flist->sorted[mid_up]);
2251 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2252 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2254 if (mid_up > high) {
2255 /* If there's nothing left above us, set high to
2256 * a non-empty entry below us and continue. */
2257 high = mid - (int)flist->sorted[mid]->len32;
2258 if (!F_IS_ACTIVE(flist->sorted[high])) {
2260 high -= (int)flist->sorted[high]->len32;
2261 } while (!F_IS_ACTIVE(flist->sorted[high]));
2262 flist->sorted[mid]->len32 = mid - high;
2267 diff = f_name_cmp(flist->sorted[mid_up], f);
2269 if (protocol_version < 29
2270 && S_ISDIR(flist->sorted[mid_up]->mode)
2271 != S_ISDIR(f->mode))
2284 * Free up any resources a file_struct has allocated
2285 * and clear the file.
2287 void clear_file(struct file_struct *file)
2289 /* The +1 zeros out the first char of the basename. */
2290 memset(file, 0, FILE_STRUCT_LEN + 1);
2291 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2292 * entry. Likewise for len32 in the opposite direction. We assume
2293 * that we're alone for now since flist_find() will adjust the counts
2294 * it runs into that aren't up-to-date. */
2295 file->len32 = F_DEPTH(file) = 1;
2298 /* Allocate a new file list. */
2299 struct file_list *flist_new(int flags, char *msg)
2301 struct file_list *flist;
2303 if (!(flist = new0(struct file_list)))
2306 if (flags & FLIST_TEMP) {
2307 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2308 out_of_memory, POOL_INTERN)))
2311 /* This is a doubly linked list with prev looping back to
2312 * the end of the list, but the last next pointer is NULL. */
2314 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2315 out_of_memory, POOL_INTERN);
2316 if (!flist->file_pool)
2319 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2321 first_flist = cur_flist = flist->prev = flist;
2323 struct file_list *prev = first_flist->prev;
2325 flist->file_pool = first_flist->file_pool;
2327 flist->ndx_start = prev->ndx_start + prev->used + 1;
2328 flist->flist_num = prev->flist_num + 1;
2331 prev->next = first_flist->prev = flist;
2333 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2340 /* Free up all elements in a flist. */
2341 void flist_free(struct file_list *flist)
2344 /* Was FLIST_TEMP dir-list. */
2345 } else if (flist == flist->prev) {
2346 first_flist = cur_flist = NULL;
2350 if (flist == cur_flist)
2351 cur_flist = flist->next;
2352 if (flist == first_flist)
2353 first_flist = first_flist->next;
2355 flist->prev->next = flist->next;
2357 flist->next = first_flist;
2359 flist->next->prev = flist->prev;
2360 file_total -= flist->used;
2364 if (!flist->prev || !flist_cnt)
2365 pool_destroy(flist->file_pool);
2367 pool_free_old(flist->file_pool, flist->pool_boundary);
2369 if (flist->sorted && flist->sorted != flist->files)
2370 free(flist->sorted);
2375 /* This routine ensures we don't have any duplicate names in our file list.
2376 * duplicate names can cause corruption because of the pipelining. */
2377 static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2379 char fbuf[MAXPATHLEN];
2384 if (flist->used == 0) {
2390 fsort(flist->sorted, flist->used);
2392 if (!am_sender || inc_recurse) {
2393 for (i = prev_i = 0; i < flist->used; i++) {
2394 if (F_IS_ACTIVE(flist->sorted[i])) {
2399 flist->low = prev_i;
2401 i = prev_i = flist->used - 1;
2405 while (++i < flist->used) {
2407 struct file_struct *file = flist->sorted[i];
2409 if (!F_IS_ACTIVE(file))
2411 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2413 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2414 int save_mode = file->mode;
2415 /* Make sure that this directory doesn't duplicate a
2416 * non-directory earlier in the list. */
2417 flist->high = prev_i;
2418 file->mode = S_IFREG;
2419 j = flist_find(flist, file);
2420 file->mode = save_mode;
2425 /* If one is a dir and the other is not, we want to
2426 * keep the dir because it might have contents in the
2427 * list. Otherwise keep the first one. */
2428 if (S_ISDIR(file->mode)) {
2429 struct file_struct *fp = flist->sorted[j];
2430 if (!S_ISDIR(fp->mode))
2434 file->flags |= FLAG_DUPLICATE;
2435 else { /* Make sure we merge our vital flags. */
2436 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2437 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2447 "removing duplicate name %s from file list (%d)\n",
2448 f_name(file, fbuf), drop + flist->ndx_start);
2450 clear_file(flist->sorted[drop]);
2454 if (flist->low == drop) {
2456 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2465 flist->high = prev_i;
2468 /* We need to strip off the leading slashes for relative
2469 * paths, but this must be done _after_ the sorting phase. */
2470 for (i = flist->low; i <= flist->high; i++) {
2471 struct file_struct *file = flist->sorted[i];
2475 while (*file->dirname == '/')
2477 if (!*file->dirname)
2478 file->dirname = NULL;
2482 if (prune_empty_dirs && !am_sender) {
2483 int j, prev_depth = 0;
2485 prev_i = 0; /* It's OK that this isn't really true. */
2487 for (i = flist->low; i <= flist->high; i++) {
2488 struct file_struct *fp, *file = flist->sorted[i];
2490 /* This temporarily abuses the F_DEPTH() value for a
2491 * directory that is in a chain that might get pruned.
2492 * We restore the old value if it gets a reprieve. */
2493 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2494 /* Dump empty dirs when coming back down. */
2495 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2496 fp = flist->sorted[prev_i];
2497 if (F_DEPTH(fp) >= 0)
2499 prev_i = -F_DEPTH(fp)-1;
2502 prev_depth = F_DEPTH(file);
2503 if (is_excluded(f_name(file, fbuf), 1,
2505 /* Keep dirs through this dir. */
2506 for (j = prev_depth-1; ; j--) {
2507 fp = flist->sorted[prev_i];
2508 if (F_DEPTH(fp) >= 0)
2510 prev_i = -F_DEPTH(fp)-1;
2514 F_DEPTH(file) = -prev_i-1;
2517 /* Keep dirs through this non-dir. */
2518 for (j = prev_depth; ; j--) {
2519 fp = flist->sorted[prev_i];
2520 if (F_DEPTH(fp) >= 0)
2522 prev_i = -F_DEPTH(fp)-1;
2527 /* Dump all remaining empty dirs. */
2529 struct file_struct *fp = flist->sorted[prev_i];
2530 if (F_DEPTH(fp) >= 0)
2532 prev_i = -F_DEPTH(fp)-1;
2536 for (i = flist->low; i <= flist->high; i++) {
2537 if (F_IS_ACTIVE(flist->sorted[i]))
2541 for (i = flist->high; i >= flist->low; i--) {
2542 if (F_IS_ACTIVE(flist->sorted[i]))
2549 static void output_flist(struct file_list *flist)
2551 char uidbuf[16], gidbuf[16], depthbuf[16];
2552 struct file_struct *file;
2553 const char *root, *dir, *slash, *name, *trail;
2554 const char *who = who_am_i();
2557 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2558 who, flist->ndx_start, flist->used, flist->low, flist->high);
2559 for (i = 0; i < flist->used; i++) {
2560 file = flist->files[i];
2561 if ((am_root || am_sender) && uid_ndx) {
2562 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2567 static char parens[] = "(\0)\0\0\0";
2568 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2569 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2570 pp, F_GROUP(file), pp + 2);
2574 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2575 if (F_IS_ACTIVE(file)) {
2576 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2577 if ((dir = file->dirname) == NULL)
2581 name = file->basename;
2582 trail = S_ISDIR(file->mode) ? "/" : "";
2584 root = dir = slash = name = trail = "";
2586 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2587 who, i + flist->ndx_start,
2588 root, dir, slash, name, trail,
2589 (int)file->mode, (double)F_LENGTH(file),
2590 uidbuf, gidbuf, file->flags);
2594 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2595 enum fnc_type { t_PATH, t_ITEM };
2597 static int found_prefix;
2599 /* Compare the names of two file_struct entities, similar to how strcmp()
2600 * would do if it were operating on the joined strings.
2602 * Some differences beginning with protocol_version 29: (1) directory names
2603 * are compared with an assumed trailing slash so that they compare in a
2604 * way that would cause them to sort immediately prior to any content they
2605 * may have; (2) a directory of any name compares after a non-directory of
2606 * any name at the same depth; (3) a directory with name "." compares prior
2607 * to anything else. These changes mean that a directory and a non-dir
2608 * with the same name will not compare as equal (protocol_version >= 29).
2610 * The dirname component can be an empty string, but the basename component
2611 * cannot (and never is in the current codebase). The basename component
2612 * may be NULL (for a removed item), in which case it is considered to be
2613 * after any existing item. */
2614 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2617 const uchar *c1, *c2;
2618 enum fnc_state state1, state2;
2619 enum fnc_type type1, type2;
2620 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2622 if (!f1 || !F_IS_ACTIVE(f1)) {
2623 if (!f2 || !F_IS_ACTIVE(f2))
2627 if (!f2 || !F_IS_ACTIVE(f2))
2630 c1 = (uchar*)f1->dirname;
2631 c2 = (uchar*)f2->dirname;
2635 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2636 c1 = (const uchar*)f1->basename;
2637 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2639 state1 = s_TRAILING;
2648 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2649 c2 = (const uchar*)f2->basename;
2650 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2652 state2 = s_TRAILING;
2662 return type1 == t_PATH ? 1 : -1;
2672 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2673 c1 = (const uchar*)f1->basename;
2674 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2676 state1 = s_TRAILING;
2682 state1 = s_TRAILING;
2683 if (type1 == t_PATH) {
2692 if (*c2 && type1 != type2)
2693 return type1 == t_PATH ? 1 : -1;
2702 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2703 c2 = (const uchar*)f2->basename;
2704 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2706 state2 = s_TRAILING;
2712 state2 = s_TRAILING;
2713 if (type2 == t_PATH) {
2726 return type1 == t_PATH ? 1 : -1;
2728 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2733 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2734 * not match if f2's basename is not an exact match of a path element in f1.
2735 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2736 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2740 return found_prefix;
2743 char *f_name_buf(void)
2745 static char names[5][MAXPATHLEN];
2746 static unsigned int n;
2748 n = (n + 1) % (sizeof names / sizeof names[0]);
2753 /* Return a copy of the full filename of a flist entry, using the indicated
2754 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2755 * done because we checked the size when creating the file_struct entry.
2757 char *f_name(const struct file_struct *f, char *fbuf)
2759 if (!f || !F_IS_ACTIVE(f))
2763 fbuf = f_name_buf();
2766 int len = strlen(f->dirname);
2767 memcpy(fbuf, f->dirname, len);
2769 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2771 strlcpy(fbuf, f->basename, MAXPATHLEN);
2776 /* Do a non-recursive scan of the named directory, possibly ignoring all
2777 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2778 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2779 * buffer (the functions we call will append names onto the end, but the old
2780 * dir value will be restored on exit). */
2781 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2783 struct file_list *dirlist;
2784 char dirbuf[MAXPATHLEN];
2785 int save_recurse = recurse;
2786 int save_xfer_dirs = xfer_dirs;
2787 int save_prune_empty_dirs = prune_empty_dirs;
2790 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2791 if (dlen >= MAXPATHLEN)
2796 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2800 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2801 xfer_dirs = save_xfer_dirs;
2802 recurse = save_recurse;
2804 flist_count_offset += dirlist->used;
2806 prune_empty_dirs = 0;
2807 dirlist->sorted = dirlist->files;
2808 flist_sort_and_clean(dirlist, 0);
2809 prune_empty_dirs = save_prune_empty_dirs;
2812 output_flist(dirlist);