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;
124 static int high_hlink_ndx;
126 static void clean_flist(struct file_list *flist, int strip_root);
127 static void output_flist(struct file_list *flist);
129 void init_flist(void)
132 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
133 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
135 checksum_len = protocol_version < 21 ? 2
136 : protocol_version < 30 ? MD4_DIGEST_LEN
140 static int show_filelist_p(void)
142 return verbose && xfer_dirs && !am_server && !inc_recurse;
145 static void start_filelist_progress(char *kind)
147 rprintf(FCLIENT, "%s ... ", kind);
148 if (verbose > 1 || do_progress)
149 rprintf(FCLIENT, "\n");
153 static void emit_filelist_progress(int count)
155 rprintf(FCLIENT, " %d files...\r", count);
158 static void maybe_emit_filelist_progress(int count)
160 if (do_progress && show_filelist_p() && (count % 100) == 0)
161 emit_filelist_progress(count);
164 static void finish_filelist_progress(const struct file_list *flist)
167 /* This overwrites the progress line */
168 rprintf(FINFO, "%d file%sto consider\n",
169 flist->used, flist->used == 1 ? " " : "s ");
171 rprintf(FINFO, "done\n");
174 void show_flist_stats(void)
179 /* Stat either a symlink or its referent, depending on the settings of
180 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
182 * If path is the name of a symlink, then the linkbuf buffer (which must hold
183 * MAXPATHLEN chars) will be set to the symlink's target string.
185 * The stat structure pointed to by stp will contain information about the
186 * link or the referent as appropriate, if they exist. */
187 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
190 if (link_stat(path, stp, copy_dirlinks) < 0)
192 if (S_ISLNK(stp->st_mode)) {
193 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
196 linkbuf[llen] = '\0';
197 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
199 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
202 return x_stat(path, stp, NULL);
204 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
205 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
206 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
207 llen - SYMLINK_PREFIX_LEN + 1);
212 return x_stat(path, stp, NULL);
216 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
220 return x_stat(path, stp, NULL);
221 if (x_lstat(path, stp, NULL) < 0)
223 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
225 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
230 return x_stat(path, stp, NULL);
234 /* This function is used to check if a file should be included/excluded
235 * from the list of files based on its name and type etc. The value of
236 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
237 static int is_excluded(char *fname, int is_dir, int filter_level)
239 #if 0 /* This currently never happens, so avoid a useless compare. */
240 if (filter_level == NO_FILTERS)
244 /* never exclude '.', even if somebody does --exclude '*' */
245 if (fname[0] == '.' && !fname[1])
247 /* Handle the -R version of the '.' dir. */
248 if (fname[0] == '/') {
249 int len = strlen(fname);
250 if (fname[len-1] == '.' && fname[len-2] == '/')
254 if (server_filter_list.head
255 && check_filter(&server_filter_list, fname, is_dir) < 0)
257 if (filter_level != ALL_FILTERS)
260 && check_filter(&filter_list, fname, is_dir) < 0)
265 static void send_directory(int f, struct file_list *flist,
266 char *fbuf, int len, int flags);
268 static const char *pathname, *orig_dir;
269 static int pathname_len;
272 /* Make sure flist can hold at least flist->used + extra entries. */
273 static void flist_expand(struct file_list *flist, int extra)
275 struct file_struct **new_ptr;
277 if (flist->used + extra <= flist->malloced)
280 if (flist->malloced < FLIST_START)
281 flist->malloced = FLIST_START;
282 else if (flist->malloced >= FLIST_LINEAR)
283 flist->malloced += FLIST_LINEAR;
285 flist->malloced *= 2;
287 /* In case count jumped or we are starting the list
288 * with a known size just set it. */
289 if (flist->malloced < flist->used + extra)
290 flist->malloced = flist->used + extra;
292 new_ptr = realloc_array(flist->files, struct file_struct *,
295 if (verbose >= 2 && flist->malloced != FLIST_START) {
296 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
298 (double)sizeof flist->files[0] * flist->malloced,
299 (new_ptr == flist->files) ? " not" : "");
302 flist->files = new_ptr;
305 out_of_memory("flist_expand");
308 static void flist_done_allocating(struct file_list *flist)
310 void *ptr = pool_boundary(flist->file_pool, 8*1024);
311 if (flist->pool_boundary == ptr)
312 flist->pool_boundary = NULL; /* list didn't use any pool memory */
314 flist->pool_boundary = ptr;
317 int push_pathname(const char *dir, int len)
323 orig_dir = strdup(curr_dir);
325 if (pathname && !pop_dir(orig_dir)) {
326 rsyserr(FERROR, errno, "pop_dir %s failed",
327 full_fname(orig_dir));
328 exit_cleanup(RERR_FILESELECT);
331 if (dir && !push_dir(dir, 0)) {
332 io_error |= IOERR_GENERAL;
333 rsyserr(FERROR, errno, "push_dir %s failed in %s",
334 full_fname(dir), curr_dir);
339 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
344 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
346 static time_t modtime;
348 #ifdef SUPPORT_HARD_LINKS
352 static uint32 rdev_major;
355 static char *user_name, *group_name;
356 static char lastname[MAXPATHLEN];
357 char fname[MAXPATHLEN];
358 int first_hlink_ndx = -1;
363 if (ic_send != (iconv_t)-1) {
366 INIT_CONST_XBUF(outbuf, fname);
369 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
370 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
371 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
374 outbuf.buf[outbuf.len++] = '/';
377 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
378 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
380 io_error |= IOERR_GENERAL;
382 "[%s] cannot convert filename: %s (%s)\n",
383 who_am_i(), f_name(file, fname), strerror(errno));
386 outbuf.buf[outbuf.len] = '\0';
391 /* Initialize starting value of xflags. */
392 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 high_hlink_ndx = ndx + first_ndx;
461 np->data = (void*)(long)(high_hlink_ndx + 1);
462 xflags |= XMIT_HLINK_FIRST;
464 xflags |= XMIT_HLINKED;
466 if (tmp_dev == dev) {
467 if (protocol_version >= 28)
468 xflags |= XMIT_SAME_DEV_pre30;
471 xflags |= XMIT_HLINKED;
477 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
479 l2 = strlen(fname+l1);
482 xflags |= XMIT_SAME_NAME;
484 xflags |= XMIT_LONG_NAME;
486 /* We must make sure we don't send a zero flag byte or the
487 * other end will terminate the flist transfer. Note that
488 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
489 * it's harmless way to add a bit to the first flag byte. */
490 if (protocol_version >= 28) {
491 if (!xflags && !S_ISDIR(mode))
492 xflags |= XMIT_TOP_DIR;
493 if ((xflags & 0xFF00) || !xflags) {
494 xflags |= XMIT_EXTENDED_FLAGS;
495 write_shortint(f, xflags);
497 write_byte(f, xflags);
499 if (!(xflags & 0xFF))
500 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
501 write_byte(f, xflags);
503 if (xflags & XMIT_SAME_NAME)
505 if (xflags & XMIT_LONG_NAME)
506 write_varint30(f, l2);
509 write_buf(f, fname + l1, l2);
511 if (first_hlink_ndx >= 0) {
512 write_varint(f, first_hlink_ndx);
513 if (first_hlink_ndx >= first_ndx)
517 write_varlong30(f, F_LENGTH(file), 3);
518 if (!(xflags & XMIT_SAME_TIME)) {
519 if (protocol_version >= 30)
520 write_varlong(f, modtime, 4);
522 write_int(f, modtime);
524 if (!(xflags & XMIT_SAME_MODE))
525 write_int(f, to_wire_mode(mode));
526 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
527 if (protocol_version < 30)
530 write_varint(f, uid);
531 if (xflags & XMIT_USER_NAME_FOLLOWS) {
532 int len = strlen(user_name);
534 write_buf(f, user_name, len);
538 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
539 if (protocol_version < 30)
542 write_varint(f, gid);
543 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
544 int len = strlen(group_name);
546 write_buf(f, group_name, len);
550 if ((preserve_devices && IS_DEVICE(mode))
551 || (preserve_specials && IS_SPECIAL(mode))) {
552 if (protocol_version < 28) {
553 if (!(xflags & XMIT_SAME_RDEV_pre28))
554 write_int(f, (int)rdev);
556 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
557 write_varint30(f, major(rdev));
558 if (protocol_version >= 30)
559 write_varint(f, minor(rdev));
560 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
561 write_byte(f, minor(rdev));
563 write_int(f, minor(rdev));
568 if (preserve_links && S_ISLNK(mode)) {
569 const char *sl = F_SYMLINK(file);
570 int len = strlen(sl);
571 write_varint30(f, len);
572 write_buf(f, sl, len);
576 #ifdef SUPPORT_HARD_LINKS
577 if (tmp_dev != 0 && protocol_version < 30) {
578 if (protocol_version < 26) {
579 /* 32-bit dev_t and ino_t */
580 write_int(f, (int32)dev);
581 write_int(f, (int32)tmp_ino);
583 /* 64-bit dev_t and ino_t */
584 if (!(xflags & XMIT_SAME_DEV_pre30))
585 write_longint(f, dev);
586 write_longint(f, tmp_ino);
591 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
596 /* Prior to 28, we sent a useless set of nulls. */
599 write_buf(f, sum, checksum_len);
603 strlcpy(lastname, fname, MAXPATHLEN);
605 if (S_ISREG(mode) || S_ISLNK(mode))
606 stats.total_size += F_LENGTH(file);
609 static struct file_struct *recv_file_entry(struct file_list *flist,
612 static int64 modtime;
614 #ifdef SUPPORT_HARD_LINKS
618 static uint32 rdev_major;
621 static uint16 gid_flags;
622 static char lastname[MAXPATHLEN], *lastdir;
623 static int lastdir_depth, lastdir_len = -1;
624 static unsigned int del_hier_name_len = 0;
625 static int in_del_hier = 0;
626 char thisname[MAXPATHLEN];
627 unsigned int l1 = 0, l2 = 0;
628 int alloc_len, basename_len, linkname_len;
629 int extra_len = file_extra_cnt * EXTRA_LEN;
630 int first_hlink_ndx = -1;
632 const char *basename;
633 struct file_struct *file;
637 if (xflags & XMIT_SAME_NAME)
640 if (xflags & XMIT_LONG_NAME)
641 l2 = read_varint30(f);
645 if (l2 >= MAXPATHLEN - l1) {
647 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
648 xflags, l1, l2, lastname, who_am_i());
649 overflow_exit("recv_file_entry");
652 strlcpy(thisname, lastname, l1 + 1);
653 read_sbuf(f, &thisname[l1], l2);
654 thisname[l1 + l2] = 0;
656 /* Abuse basename_len for a moment... */
657 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
660 if (ic_recv != (iconv_t)-1) {
663 INIT_CONST_XBUF(outbuf, thisname);
664 INIT_XBUF(inbuf, lastname, basename_len, -1);
666 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
667 io_error |= IOERR_GENERAL;
669 "[%s] cannot convert filename: %s (%s)\n",
670 who_am_i(), lastname, strerror(errno));
673 outbuf.buf[outbuf.len] = '\0';
677 clean_fname(thisname, 0);
680 sanitize_path(thisname, thisname, "", 0);
682 if ((basename = strrchr(thisname, '/')) != NULL) {
683 int len = basename++ - thisname;
684 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
685 lastdir = new_array(char, len + 1);
686 memcpy(lastdir, thisname, len);
689 lastdir_depth = count_dir_elements(lastdir);
693 basename_len = strlen(basename) + 1; /* count the '\0' */
695 #ifdef SUPPORT_HARD_LINKS
696 if (protocol_version >= 30
697 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
698 first_hlink_ndx = read_varint(f);
699 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
701 "hard-link reference out of range: %d (%d)\n",
702 first_hlink_ndx, flist->ndx_start + flist->used);
703 exit_cleanup(RERR_PROTOCOL);
705 if (first_hlink_ndx >= flist->ndx_start) {
706 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
707 file_length = F_LENGTH(first);
708 modtime = first->modtime;
711 uid = F_OWNER(first);
713 gid = F_GROUP(first);
714 if ((preserve_devices && IS_DEVICE(mode))
715 || (preserve_specials && IS_SPECIAL(mode))) {
716 uint32 *devp = F_RDEV_P(first);
717 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
718 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
720 if (preserve_links && S_ISLNK(mode))
721 linkname_len = strlen(F_SYMLINK(first)) + 1;
729 file_length = read_varlong30(f, 3);
730 if (!(xflags & XMIT_SAME_TIME)) {
731 if (protocol_version >= 30) {
732 modtime = read_varlong(f, 4);
733 #if SIZEOF_TIME_T < SIZEOF_INT64
734 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
736 "Time value of %s truncated on receiver.\n",
741 modtime = read_int(f);
743 if (!(xflags & XMIT_SAME_MODE))
744 mode = from_wire_mode(read_int(f));
746 if (chmod_modes && !S_ISLNK(mode))
747 mode = tweak_mode(mode, chmod_modes);
749 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
750 if (protocol_version < 30)
751 uid = (uid_t)read_int(f);
753 uid = (uid_t)read_varint(f);
754 if (xflags & XMIT_USER_NAME_FOLLOWS)
755 uid = recv_user_name(f, uid);
756 else if (inc_recurse && am_root && !numeric_ids)
757 uid = match_uid(uid);
760 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
761 if (protocol_version < 30)
762 gid = (gid_t)read_int(f);
764 gid = (gid_t)read_varint(f);
766 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
767 gid = recv_group_name(f, gid, &gid_flags);
768 else if (inc_recurse && (!am_root || !numeric_ids))
769 gid = match_gid(gid, &gid_flags);
773 if ((preserve_devices && IS_DEVICE(mode))
774 || (preserve_specials && IS_SPECIAL(mode))) {
775 if (protocol_version < 28) {
776 if (!(xflags & XMIT_SAME_RDEV_pre28))
777 rdev = (dev_t)read_int(f);
780 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
781 rdev_major = read_varint30(f);
782 if (protocol_version >= 30)
783 rdev_minor = read_varint(f);
784 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
785 rdev_minor = read_byte(f);
787 rdev_minor = read_int(f);
788 rdev = MAKEDEV(rdev_major, rdev_minor);
790 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
792 } else if (protocol_version < 28)
793 rdev = MAKEDEV(0, 0);
796 if (preserve_links && S_ISLNK(mode)) {
797 linkname_len = read_varint30(f) + 1; /* count the '\0' */
798 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
799 rprintf(FERROR, "overflow: linkname_len=%d\n",
801 overflow_exit("recv_file_entry");
804 linkname_len += SYMLINK_PREFIX_LEN;
810 #ifdef SUPPORT_HARD_LINKS
812 if (preserve_hard_links) {
813 if (protocol_version < 28 && S_ISREG(mode))
814 xflags |= XMIT_HLINKED;
815 if (xflags & XMIT_HLINKED)
816 extra_len += (inc_recurse+1) * EXTRA_LEN;
821 /* We need one or two index int32s when we're preserving ACLs. */
823 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
826 if (always_checksum && S_ISREG(mode))
827 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
829 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
830 extra_len += EXTRA_LEN;
832 #if EXTRA_ROUNDING > 0
833 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
834 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
837 if (inc_recurse && S_ISDIR(mode)) {
838 if (one_file_system) {
839 /* Room to save the dir's device for -x */
840 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
842 pool = dir_flist->file_pool;
844 pool = flist->file_pool;
846 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
848 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
850 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
852 file = (struct file_struct *)bp;
853 bp += FILE_STRUCT_LEN;
855 memcpy(bp, basename, basename_len);
856 bp += basename_len + linkname_len; /* skip space for symlink too */
858 #ifdef SUPPORT_HARD_LINKS
859 if (xflags & XMIT_HLINKED)
860 file->flags |= FLAG_HLINKED;
862 file->modtime = (time_t)modtime;
863 file->len32 = (uint32)file_length;
864 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
865 file->flags |= FLAG_LENGTH64;
866 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
873 file->flags |= gid_flags;
876 F_NDX(file) = flist->used + flist->ndx_start;
878 if (basename != thisname) {
879 file->dirname = lastdir;
880 F_DEPTH(file) = lastdir_depth + 1;
885 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
887 if (protocol_version >= 30) {
888 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
889 if (xflags & XMIT_TOP_DIR)
890 file->flags |= FLAG_TOP_DIR;
891 file->flags |= FLAG_CONTENT_DIR;
892 } else if (xflags & XMIT_TOP_DIR)
893 file->flags |= FLAG_IMPLIED_DIR;
894 } else if (xflags & XMIT_TOP_DIR) {
895 in_del_hier = recurse;
896 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
897 if (relative_paths && del_hier_name_len > 2
898 && lastname[del_hier_name_len-1] == '.'
899 && lastname[del_hier_name_len-2] == '/')
900 del_hier_name_len -= 2;
901 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
902 } else if (in_del_hier) {
903 if (!relative_paths || !del_hier_name_len
904 || (l1 >= del_hier_name_len
905 && lastname[del_hier_name_len] == '/'))
906 file->flags |= FLAG_CONTENT_DIR;
912 if ((preserve_devices && IS_DEVICE(mode))
913 || (preserve_specials && IS_SPECIAL(mode))) {
914 uint32 *devp = F_RDEV_P(file);
915 DEV_MAJOR(devp) = major(rdev);
916 DEV_MINOR(devp) = minor(rdev);
921 bp = (char*)file->basename + basename_len;
922 if (first_hlink_ndx >= flist->ndx_start) {
923 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
924 memcpy(bp, F_SYMLINK(first), linkname_len);
925 } else if (munge_symlinks) {
926 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
927 bp += SYMLINK_PREFIX_LEN;
928 linkname_len -= SYMLINK_PREFIX_LEN;
929 read_sbuf(f, bp, linkname_len - 1);
931 read_sbuf(f, bp, linkname_len - 1);
935 #ifdef SUPPORT_HARD_LINKS
936 if (preserve_hard_links && xflags & XMIT_HLINKED) {
937 if (protocol_version >= 30) {
938 if (xflags & XMIT_HLINK_FIRST) {
939 high_hlink_ndx = flist->ndx_start + flist->used;
940 F_HL_GNUM(file) = high_hlink_ndx;
942 F_HL_GNUM(file) = first_hlink_ndx;
944 static int32 cnt = 0;
945 struct ht_int64_node *np;
948 if (protocol_version < 26) {
952 if (!(xflags & XMIT_SAME_DEV_pre30))
953 dev = read_longint(f);
954 ino = read_longint(f);
956 np = idev_find(dev, ino);
957 ndx = (int32)(long)np->data - 1;
960 np->data = (void*)(long)cnt;
962 F_HL_GNUM(file) = ndx;
967 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
971 /* Prior to 28, we get a useless set of nulls. */
974 if (first_hlink_ndx >= flist->ndx_start) {
975 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
976 memcpy(bp, F_SUM(first), checksum_len);
978 read_buf(f, bp, checksum_len);
982 if (preserve_acls && !S_ISLNK(mode))
983 receive_acl(file, f);
985 #ifdef SUPPORT_XATTRS
987 receive_xattr(file, f );
990 if (S_ISREG(mode) || S_ISLNK(mode))
991 stats.total_size += file_length;
996 /* Create a file_struct for a named file by reading its stat() information
997 * and performing extensive checks against global options.
999 * Returns a pointer to the new file struct, or NULL if there was an error
1000 * or this file should be excluded. */
1001 struct file_struct *make_file(const char *fname, struct file_list *flist,
1002 STRUCT_STAT *stp, int flags, int filter_level)
1004 static char *lastdir;
1005 static int lastdir_len = -1;
1006 struct file_struct *file;
1007 char thisname[MAXPATHLEN];
1008 char linkname[MAXPATHLEN];
1009 int alloc_len, basename_len, linkname_len;
1010 int extra_len = file_extra_cnt * EXTRA_LEN;
1011 const char *basename;
1016 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1017 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1020 clean_fname(thisname, 0);
1022 sanitize_path(thisname, thisname, "", 0);
1024 if (stp && S_ISDIR(stp->st_mode)) {
1025 st = *stp; /* Needed for "symlink/." with --relative. */
1026 *linkname = '\0'; /* make IBM code checker happy */
1027 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1028 int save_errno = errno;
1029 /* See if file is excluded before reporting an error. */
1030 if (filter_level != NO_FILTERS
1031 && (is_excluded(thisname, 0, filter_level)
1032 || is_excluded(thisname, 1, filter_level))) {
1033 if (ignore_perishable && save_errno != ENOENT)
1034 non_perishable_cnt++;
1037 if (save_errno == ENOENT) {
1038 #ifdef SUPPORT_LINKS
1039 /* Avoid "vanished" error if symlink points nowhere. */
1040 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1041 && S_ISLNK(st.st_mode)) {
1042 io_error |= IOERR_GENERAL;
1043 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1044 full_fname(thisname));
1048 enum logcode c = am_daemon && protocol_version < 28
1049 ? FERROR : FWARNING;
1050 io_error |= IOERR_VANISHED;
1051 rprintf(c, "file has vanished: %s\n",
1052 full_fname(thisname));
1055 io_error |= IOERR_GENERAL;
1056 rsyserr(FERROR_XFER, save_errno, "readlink %s failed",
1057 full_fname(thisname));
1062 /* backup.c calls us with filter_level set to NO_FILTERS. */
1063 if (filter_level == NO_FILTERS)
1066 if (S_ISDIR(st.st_mode)) {
1068 rprintf(FINFO, "skipping directory %s\n", thisname);
1071 /* -x only affects dirs because we need to avoid recursing
1072 * into a mount-point directory, not to avoid copying a
1073 * symlinked file if -L (or similar) was specified. */
1074 if (one_file_system && st.st_dev != filesystem_dev
1075 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1076 if (one_file_system > 1) {
1079 "[%s] skipping mount-point dir %s\n",
1080 who_am_i(), thisname);
1084 flags |= FLAG_MOUNT_DIR;
1085 flags &= ~FLAG_CONTENT_DIR;
1088 flags &= ~FLAG_CONTENT_DIR;
1090 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1091 if (ignore_perishable)
1092 non_perishable_cnt++;
1096 if (lp_ignore_nonreadable(module_id)) {
1097 #ifdef SUPPORT_LINKS
1098 if (!S_ISLNK(st.st_mode))
1100 if (access(thisname, R_OK) != 0)
1106 /* Only divert a directory in the main transfer. */
1108 if (flist->prev && S_ISDIR(st.st_mode)
1109 && flags & FLAG_DIVERT_DIRS) {
1110 /* Room for parent/sibling/next-child info. */
1111 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1113 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1115 pool = dir_flist->file_pool;
1117 pool = flist->file_pool;
1122 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1123 who_am_i(), thisname, filter_level);
1126 if ((basename = strrchr(thisname, '/')) != NULL) {
1127 int len = basename++ - thisname;
1128 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1129 lastdir = new_array(char, len + 1);
1130 memcpy(lastdir, thisname, len);
1131 lastdir[len] = '\0';
1135 basename = thisname;
1136 basename_len = strlen(basename) + 1; /* count the '\0' */
1138 #ifdef SUPPORT_LINKS
1139 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1144 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1145 extra_len += EXTRA_LEN;
1147 #if EXTRA_ROUNDING > 0
1148 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1149 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1152 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1155 bp = pool_alloc(pool, alloc_len, "make_file");
1157 if (!(bp = new_array(char, alloc_len)))
1158 out_of_memory("make_file");
1161 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1163 file = (struct file_struct *)bp;
1164 bp += FILE_STRUCT_LEN;
1166 memcpy(bp, basename, basename_len);
1167 bp += basename_len + linkname_len; /* skip space for symlink too */
1169 #ifdef SUPPORT_HARD_LINKS
1170 if (preserve_hard_links && flist && flist->prev) {
1171 if (protocol_version >= 28
1172 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1173 : S_ISREG(st.st_mode)) {
1174 tmp_dev = st.st_dev;
1175 tmp_ino = st.st_ino;
1181 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1182 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1183 tmp_rdev = st.st_rdev;
1188 file->flags = flags;
1189 file->modtime = st.st_mtime;
1190 file->len32 = (uint32)st.st_size;
1191 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1192 file->flags |= FLAG_LENGTH64;
1193 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1195 file->mode = st.st_mode;
1197 F_OWNER(file) = st.st_uid;
1199 F_GROUP(file) = st.st_gid;
1201 if (basename != thisname)
1202 file->dirname = lastdir;
1204 #ifdef SUPPORT_LINKS
1206 bp = (char*)file->basename + basename_len;
1207 memcpy(bp, linkname, linkname_len);
1211 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1212 file_checksum(thisname, tmp_sum, st.st_size);
1214 F_PATHNAME(file) = pathname;
1216 /* This code is only used by the receiver when it is building
1217 * a list of files for a delete pass. */
1218 if (keep_dirlinks && linkname_len && flist) {
1220 int save_mode = file->mode;
1221 file->mode = S_IFDIR; /* Find a directory with our name. */
1222 if (flist_find(dir_flist, file) >= 0
1223 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1224 file->modtime = st2.st_mtime;
1226 file->mode = st2.st_mode;
1228 F_OWNER(file) = st2.st_uid;
1230 F_GROUP(file) = st2.st_gid;
1232 file->mode = save_mode;
1235 if (basename_len == 0+1)
1239 F_NDX(file) = dir_count - 1;
1244 /* Only called for temporary file_struct entries created by make_file(). */
1245 void unmake_file(struct file_struct *file)
1247 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1248 #if EXTRA_ROUNDING > 0
1249 if (extra_cnt & EXTRA_ROUNDING)
1250 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1252 free(REQ_EXTRA(file, extra_cnt));
1255 static struct file_struct *send_file_name(int f, struct file_list *flist,
1256 char *fname, STRUCT_STAT *stp,
1257 int flags, int filter_level)
1259 struct file_struct *file;
1260 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1264 file = make_file(fname, flist, stp, flags, filter_level);
1268 if (chmod_modes && !S_ISLNK(file->mode))
1269 file->mode = tweak_mode(file->mode, chmod_modes);
1272 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1273 sx.st.st_mode = file->mode;
1274 sx.acc_acl = sx.def_acl = NULL;
1275 if (get_acl(fname, &sx) < 0)
1279 #ifdef SUPPORT_XATTRS
1280 if (preserve_xattrs && f >= 0) {
1282 if (get_xattr(fname, &sx) < 0)
1287 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1289 flist_expand(flist, 1);
1290 flist->files[flist->used++] = file;
1292 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1294 if (preserve_acls && !S_ISLNK(file->mode)) {
1299 #ifdef SUPPORT_XATTRS
1300 if (preserve_xattrs) {
1301 F_XATTR(file) = send_xattr(&sx, f);
1309 static void send_if_directory(int f, struct file_list *flist,
1310 struct file_struct *file,
1311 char *fbuf, unsigned int ol,
1314 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1316 if (S_ISDIR(file->mode)
1317 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1319 unsigned int len = strlen(fbuf);
1320 if (len > 1 && fbuf[len-1] == '/')
1322 if (len >= MAXPATHLEN - 1) {
1323 io_error |= IOERR_GENERAL;
1324 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1328 save_filters = push_local_filters(fbuf, len);
1329 send_directory(f, flist, fbuf, len, flags);
1330 pop_local_filters(save_filters);
1337 static int file_compare(const void *file1, const void *file2)
1339 return f_name_cmp(*(struct file_struct **)file1,
1340 *(struct file_struct **)file2);
1343 /* The guts of a merge-sort algorithm. This was derived from the glibc
1344 * version, but I (Wayne) changed the merge code to do less copying and
1345 * to require only half the amount of temporary memory. */
1346 static void fsort_tmp(struct file_struct **fp, size_t num,
1347 struct file_struct **tmp)
1349 struct file_struct **f1, **f2, **t;
1358 fsort_tmp(f1, n1, tmp);
1360 fsort_tmp(f2, n2, tmp);
1362 while (f_name_cmp(*f1, *f2) <= 0) {
1369 memcpy(t, f1, n1 * PTR_SIZE);
1371 *f1++ = *f2++, n2--;
1373 while (n1 > 0 && n2 > 0) {
1374 if (f_name_cmp(*t, *f2) <= 0)
1377 *f1++ = *f2++, n2--;
1381 memcpy(f1, t, n1 * PTR_SIZE);
1384 /* This file-struct sorting routine makes sure that any identical names in
1385 * the file list stay in the same order as they were in the original list.
1386 * This is particularly vital in inc_recurse mode where we expect a sort
1387 * on the flist to match the exact order of a sort on the dir_flist. */
1388 static void fsort(struct file_struct **fp, size_t num)
1394 qsort(fp, num, PTR_SIZE, file_compare);
1396 struct file_struct **tmp = new_array(struct file_struct *,
1398 fsort_tmp(fp, num, tmp);
1403 /* We take an entire set of sibling dirs from the sorted flist and link them
1404 * into the tree, setting the appropriate parent/child/sibling pointers. */
1405 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1410 int32 *parent_dp = parent_ndx < 0 ? NULL
1411 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1413 flist_expand(dir_flist, dir_cnt);
1414 dir_flist->sorted = dir_flist->files;
1416 for (i = 0; dir_cnt; i++) {
1417 struct file_struct *file = from_flist->sorted[i];
1419 if (!S_ISDIR(file->mode))
1422 dir_flist->files[dir_flist->used++] = file;
1425 if (file->basename[0] == '.' && file->basename[1] == '\0')
1429 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1431 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1433 send_dir_ndx = dir_flist->used - 1;
1435 dp = F_DIR_NODE_P(file);
1436 DIR_PARENT(dp) = parent_ndx;
1437 DIR_FIRST_CHILD(dp) = -1;
1440 DIR_NEXT_SIBLING(dp) = -1;
1443 /* This function is normally called by the sender, but the receiving side also
1444 * calls it from get_dirlist() with f set to -1 so that we just construct the
1445 * file list in memory without sending it over the wire. Also, get_dirlist()
1446 * might call this with f set to -2, which also indicates that local filter
1447 * rules should be ignored. */
1448 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1455 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1456 int start = flist->used;
1457 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1459 assert(flist != NULL);
1461 if (!(d = opendir(fbuf))) {
1462 io_error |= IOERR_GENERAL;
1463 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1468 if (len != 1 || *fbuf != '/')
1471 remainder = MAXPATHLEN - (p - fbuf);
1473 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1474 char *dname = d_name(di);
1475 if (dname[0] == '.' && (dname[1] == '\0'
1476 || (dname[1] == '.' && dname[2] == '\0')))
1478 if (strlcpy(p, dname, remainder) >= remainder) {
1479 io_error |= IOERR_GENERAL;
1481 "cannot send long-named file %s\n",
1485 if (dname[0] == '\0') {
1486 io_error |= IOERR_GENERAL;
1488 "cannot send file with empty name in %s\n",
1493 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1499 io_error |= IOERR_GENERAL;
1500 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1505 if (f >= 0 && recurse && !divert_dirs) {
1506 int i, end = flist->used - 1;
1507 /* send_if_directory() bumps flist->used, so use "end". */
1508 for (i = start; i <= end; i++)
1509 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1513 static char lastpath[MAXPATHLEN] = "";
1514 static int lastpath_len = 0;
1515 static struct file_struct *lastpath_struct;
1517 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1518 char *start, char *limit, int flags, char name_type)
1520 struct file_struct *file;
1521 item_list *relname_list;
1522 relnamecache **rnpp;
1524 int len, need_new_dir;
1526 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1529 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1530 && lastpath_len == limit - fname
1531 && strncmp(lastpath, fname, lastpath_len) == 0)
1539 int save_copy_links = copy_links;
1540 int save_xfer_dirs = xfer_dirs;
1542 copy_links = xfer_dirs = 1;
1546 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1548 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1552 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1554 if (file && !S_ISDIR(file->mode))
1556 lastpath_struct = file;
1559 strlcpy(lastpath, fname, sizeof lastpath);
1560 lastpath_len = limit - fname;
1564 copy_links = save_copy_links;
1565 xfer_dirs = save_xfer_dirs;
1571 if (!lastpath_struct)
1572 return; /* dir must have vanished */
1574 len = strlen(limit+1);
1575 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1576 if (!relname_list) {
1577 if (!(relname_list = new0(item_list)))
1578 out_of_memory("send_implied_dirs");
1579 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1581 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1582 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1583 out_of_memory("send_implied_dirs");
1584 (*rnpp)->name_type = name_type;
1585 strlcpy((*rnpp)->fname, limit+1, len + 1);
1588 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1590 char fbuf[MAXPATHLEN];
1591 item_list *relname_list;
1592 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1596 dlen = strlen(fbuf);
1598 if (F_PATHNAME(file) != pathname) {
1599 if (!push_pathname(F_PATHNAME(file), -1))
1600 exit_cleanup(RERR_FILESELECT);
1603 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1605 if (file->flags & FLAG_CONTENT_DIR) {
1606 if (one_file_system) {
1608 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1609 io_error |= IOERR_GENERAL;
1610 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1614 filesystem_dev = st.st_dev;
1616 send_directory(f, flist, fbuf, dlen, flags);
1619 if (!relative_paths)
1622 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1626 for (j = 0; j < relname_list->count; j++) {
1628 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1629 char name_type = rnp->name_type;
1632 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1634 if (len >= (int)sizeof fbuf)
1635 continue; /* Impossible... */
1637 slash = strchr(fbuf+dlen+1, '/');
1639 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1643 if (name_type != NORMAL_NAME) {
1645 if (link_stat(fbuf, &st, 1) != 0) {
1646 io_error |= IOERR_GENERAL;
1647 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1651 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1653 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1659 void send_extra_file_list(int f, int at_least)
1661 struct file_list *flist;
1664 int old_cnt, save_io_error = io_error;
1669 /* Keep sending data until we have the requested number of
1670 * files in the upcoming file-lists. */
1671 old_cnt = cur_flist->used;
1672 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1673 old_cnt += flist->used;
1674 while (file_total - old_cnt < at_least) {
1675 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1676 int dir_ndx, dstart = dir_count;
1677 const char *pathname = F_PATHNAME(file);
1680 flist = flist_new(0, "send_extra_file_list");
1681 start_write = stats.total_written;
1684 dir_ndx = F_NDX(file);
1686 dir_ndx = send_dir_ndx;
1687 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1688 flist->parent_ndx = dir_ndx;
1690 send1extra(f, file, flist);
1691 prev_flags = file->flags;
1692 dp = F_DIR_NODE_P(file);
1694 /* If there are any duplicate directory names that follow, we
1695 * send all the dirs together in one file-list. The dir_flist
1696 * tree links all the child subdirs onto the last dup dir. */
1697 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1698 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1699 send_dir_ndx = dir_ndx;
1700 file = dir_flist->sorted[dir_ndx];
1701 /* Try to avoid some duplicate scanning of identical dirs. */
1702 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1703 file->flags &= ~FLAG_CONTENT_DIR;
1704 send1extra(f, file, flist);
1705 prev_flags = file->flags;
1706 dp = F_DIR_NODE_P(file);
1711 if (need_unsorted_flist) {
1712 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1713 out_of_memory("send_extra_file_list");
1714 memcpy(flist->sorted, flist->files,
1715 flist->used * sizeof (struct file_struct*));
1717 flist->sorted = flist->files;
1719 clean_flist(flist, 0);
1721 flist->ndx_end = flist->ndx_start + flist->used - 1;
1722 if (!need_unsorted_flist)
1723 flist->ndx_end -= (dir_count - dstart);
1725 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1726 flist_done_allocating(flist);
1728 file_total += flist->used;
1729 stats.flist_size += stats.total_written - start_write;
1730 stats.num_files += flist->used;
1732 output_flist(flist);
1734 if (DIR_FIRST_CHILD(dp) >= 0) {
1735 send_dir_ndx = DIR_FIRST_CHILD(dp);
1738 while (DIR_NEXT_SIBLING(dp) < 0) {
1739 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1740 write_ndx(f, NDX_FLIST_EOF);
1742 change_local_filter_dir(NULL, 0, 0);
1746 file = dir_flist->sorted[send_dir_ndx];
1747 dp = F_DIR_NODE_P(file);
1749 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1754 if (io_error != save_io_error && !ignore_errors)
1755 send_msg_int(MSG_IO_ERROR, io_error);
1758 struct file_list *send_file_list(int f, int argc, char *argv[])
1760 static const char *lastdir;
1761 static int lastdir_len = -1;
1765 struct file_list *flist;
1766 struct timeval start_tv, end_tv;
1769 int disable_buffering;
1770 int flags = recurse ? FLAG_CONTENT_DIR : 0;
1771 int reading_remotely = filesfrom_host != NULL;
1772 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1774 | (filesfrom_convert ? RL_CONVERT : 0)
1776 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1778 rprintf(FLOG, "building file list\n");
1779 if (show_filelist_p())
1780 start_filelist_progress("building file list");
1781 else if (inc_recurse && verbose && !am_server)
1782 rprintf(FCLIENT, "sending incremental file list\n");
1784 start_write = stats.total_written;
1785 gettimeofday(&start_tv, NULL);
1787 if (relative_paths && protocol_version >= 30)
1788 implied_dirs = 1; /* We send flagged implied dirs */
1790 #ifdef SUPPORT_HARD_LINKS
1791 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1795 flist = cur_flist = flist_new(0, "send_file_list");
1797 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1798 flags |= FLAG_DIVERT_DIRS;
1800 dir_flist = cur_flist;
1802 disable_buffering = io_start_buffering_out(f);
1803 if (filesfrom_fd >= 0) {
1804 if (argv[0] && !push_dir(argv[0], 0)) {
1805 rsyserr(FERROR_XFER, errno, "push_dir %s failed in %s",
1806 full_fname(argv[0]), curr_dir);
1807 exit_cleanup(RERR_FILESELECT);
1813 char fbuf[MAXPATHLEN], *fn, name_type;
1816 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1818 sanitize_path(fbuf, fbuf, "", 0);
1822 strlcpy(fbuf, *argv++, MAXPATHLEN);
1824 sanitize_path(fbuf, fbuf, "", 0);
1828 if (relative_paths) {
1829 /* We clean up fbuf below. */
1830 name_type = NORMAL_NAME;
1831 } else if (!len || fbuf[len - 1] == '/') {
1832 if (len == 2 && fbuf[0] == '.') {
1833 /* Turn "./" into just "." rather than "./." */
1836 if (len + 1 >= MAXPATHLEN)
1837 overflow_exit("send_file_list");
1841 name_type = DOT_NAME;
1842 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1843 && (len == 2 || fbuf[len-3] == '/')) {
1844 if (len + 2 >= MAXPATHLEN)
1845 overflow_exit("send_file_list");
1849 name_type = DOT_NAME;
1850 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
1851 name_type = DOT_NAME;
1853 name_type = NORMAL_NAME;
1857 if (!relative_paths) {
1858 p = strrchr(fbuf, '/');
1865 len -= p - fbuf + 1;
1870 if ((p = strstr(fbuf, "/./")) != NULL) {
1880 *--fn = '\0'; /* ensure room for '.' */
1883 len = clean_fname(fn, CFN_KEEP_LEADING_DOT_DIR
1884 | CFN_KEEP_TRAILING_SLASH
1885 | CFN_DROP_TRAILING_DOT_DIR);
1890 name_type = DOT_NAME;
1891 } else if (fn[0] == '.')
1892 name_type = DOT_NAME;
1893 } else if (fn[len-1] == '/') {
1895 if (len == 1 && *fn == '.')
1896 name_type = DOT_NAME;
1898 name_type = SLASH_ENDING_NAME;
1900 /* Reject a ".." dir in the active part of the path. */
1901 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1902 if ((p[2] == '/' || p[2] == '\0')
1903 && (p == fn || p[-1] == '/')) {
1905 "found \"..\" dir in relative path: %s\n",
1907 exit_cleanup(RERR_SYNTAX);
1915 name_type = DOT_NAME;
1918 dirlen = dir ? strlen(dir) : 0;
1919 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1920 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1923 lastdir_len = pathname_len;
1924 } else if (!push_pathname(lastdir, lastdir_len))
1928 memmove(fbuf, fn, len + 1);
1930 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0) {
1931 io_error |= IOERR_GENERAL;
1932 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1937 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1938 rprintf(FINFO, "skipping directory %s\n", fbuf);
1942 if (inc_recurse && relative_paths && *fbuf) {
1943 if ((p = strchr(fbuf+1, '/')) != NULL) {
1944 if (p - fbuf == 1 && *fbuf == '.') {
1945 if ((fn = strchr(p+1, '/')) != NULL)
1949 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
1953 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1954 /* Send the implied directories at the start of the
1955 * source spec, so we get their permissions right. */
1956 char *lp = lastpath, *slash = fbuf;
1958 /* Skip any initial directories in our path that we
1959 * have in common with lastpath. */
1960 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1965 if (fn != p || (*lp && *lp != '/'))
1966 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1969 if (one_file_system)
1970 filesystem_dev = st.st_dev;
1972 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
1973 struct file_struct *file;
1974 int top_flags = FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags;
1975 file = send_file_name(f, flist, fbuf, &st,
1976 top_flags, ALL_FILTERS);
1978 if (name_type == DOT_NAME) {
1979 if (send_dir_depth < 0) {
1981 change_local_filter_dir(fbuf, len, send_dir_depth);
1983 send_directory(f, flist, fbuf, len, flags);
1986 send_if_directory(f, flist, file, fbuf, len, flags);
1988 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1991 gettimeofday(&end_tv, NULL);
1992 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1993 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1994 if (stats.flist_buildtime == 0)
1995 stats.flist_buildtime = 1;
1998 write_byte(f, 0); /* Indicate end of file list */
2000 #ifdef SUPPORT_HARD_LINKS
2001 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2005 if (show_filelist_p())
2006 finish_filelist_progress(flist);
2008 gettimeofday(&end_tv, NULL);
2009 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2010 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2012 /* When converting names, both sides keep an unsorted file-list array
2013 * because the names will differ on the sending and receiving sides
2014 * (both sides will use the unsorted index number for each item). */
2016 /* Sort the list without removing any duplicates. This allows the
2017 * receiving side to ask for whatever name it kept. For incremental
2018 * recursion mode, the sender marks duplicate dirs so that it can
2019 * send them together in a single file-list. */
2020 if (need_unsorted_flist) {
2022 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2023 out_of_memory("send_file_list");
2024 memcpy(flist->sorted, flist->files,
2025 flist->used * sizeof (struct file_struct*));
2026 clean_flist(flist, 0);
2028 flist->sorted = flist->files;
2030 flist->high = flist->used - 1;
2033 flist->sorted = flist->files;
2034 clean_flist(flist, 0);
2036 file_total += flist->used;
2038 /* We don't subtract dir_count for the first send since we
2039 * might have one or more dot dirs which need to get sent. */
2040 flist->ndx_end = flist->ndx_start + flist->used - 1;
2042 if (!numeric_ids && !inc_recurse)
2045 /* send the io_error flag */
2046 if (protocol_version < 30)
2047 write_int(f, ignore_errors ? 0 : io_error);
2048 else if (io_error && !ignore_errors)
2049 send_msg_int(MSG_IO_ERROR, io_error);
2051 if (disable_buffering)
2052 io_end_buffering_out();
2054 stats.flist_size = stats.total_written - start_write;
2055 stats.num_files = flist->used;
2058 output_flist(flist);
2061 rprintf(FINFO, "send_file_list done\n");
2065 add_dirs_to_tree(-1, flist, dir_count);
2066 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2067 flist->parent_ndx = -1;
2068 flist_done_allocating(flist);
2069 if (send_dir_ndx < 0) {
2070 write_ndx(f, NDX_FLIST_EOF);
2073 else if (file_total == 1) {
2074 /* If we're creating incremental file-lists and there
2075 * was just 1 item in the first file-list, send 1 more
2076 * file-list to check if this is a 1-file xfer. */
2077 send_extra_file_list(f, 1);
2084 struct file_list *recv_file_list(int f)
2086 struct file_list *flist;
2091 rprintf(FLOG, "receiving file list\n");
2092 if (show_filelist_p())
2093 start_filelist_progress("receiving file list");
2094 else if (inc_recurse && verbose && !am_server && !first_flist)
2095 rprintf(FCLIENT, "receiving incremental file list\n");
2097 start_read = stats.total_read;
2099 #ifdef SUPPORT_HARD_LINKS
2100 if (preserve_hard_links && !first_flist)
2104 flist = flist_new(0, "recv_file_list");
2107 if (flist->ndx_start == 1)
2108 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2109 dstart = dir_flist->used;
2115 while ((flags = read_byte(f)) != 0) {
2116 struct file_struct *file;
2118 flist_expand(flist, 1);
2120 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2121 flags |= read_byte(f) << 8;
2122 file = recv_file_entry(flist, flags, f);
2124 if (inc_recurse && S_ISDIR(file->mode)) {
2125 flist_expand(dir_flist, 1);
2126 dir_flist->files[dir_flist->used++] = file;
2129 flist->files[flist->used++] = file;
2131 maybe_emit_filelist_progress(flist->used);
2134 rprintf(FINFO, "recv_file_name(%s)\n",
2135 f_name(file, NULL));
2138 file_total += flist->used;
2140 flist->ndx_end = flist->ndx_start + flist->used - 1;
2141 if (inc_recurse && !need_unsorted_flist && flist->ndx_start > 1)
2142 flist->ndx_end -= dir_flist->used - dstart;
2145 rprintf(FINFO, "received %d names\n", flist->used);
2147 if (show_filelist_p())
2148 finish_filelist_progress(flist);
2150 if (need_unsorted_flist) {
2151 /* Create an extra array of index pointers that we can sort for
2152 * the generator's use (for wading through the files in sorted
2153 * order and for calling flist_find()). We keep the "files"
2154 * list unsorted for our exchange of index numbers with the
2155 * other side (since their names may not sort the same). */
2156 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2157 out_of_memory("recv_file_list");
2158 memcpy(flist->sorted, flist->files,
2159 flist->used * sizeof (struct file_struct*));
2160 if (inc_recurse && dir_flist->used > dstart) {
2161 dir_flist->sorted = realloc_array(dir_flist->sorted,
2162 struct file_struct *,
2164 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2165 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2166 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2169 flist->sorted = flist->files;
2170 if (inc_recurse && dir_flist->used > dstart) {
2171 dir_flist->sorted = dir_flist->files;
2172 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2177 flist_done_allocating(flist);
2179 recv_id_list(f, flist);
2181 clean_flist(flist, relative_paths);
2183 if (protocol_version < 30) {
2184 /* Recv the io_error flag */
2188 io_error |= read_int(f);
2189 } else if (inc_recurse && flist->ndx_start == 1) {
2190 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2191 flist->parent_ndx = -1;
2195 output_flist(flist);
2198 rprintf(FINFO, "recv_file_list done\n");
2200 stats.flist_size += stats.total_read - start_read;
2201 stats.num_files += flist->used;
2206 /* This is only used once by the receiver if the very first file-list
2207 * has exactly one item in it. */
2208 void recv_additional_file_list(int f)
2210 struct file_list *flist;
2211 int ndx = read_ndx(f);
2212 if (ndx == NDX_FLIST_EOF) {
2214 change_local_filter_dir(NULL, 0, 0);
2216 ndx = NDX_FLIST_OFFSET - ndx;
2217 if (ndx < 0 || ndx >= dir_flist->used) {
2218 ndx = NDX_FLIST_OFFSET - ndx;
2220 "[%s] Invalid dir index: %d (%d - %d)\n",
2221 who_am_i(), ndx, NDX_FLIST_OFFSET,
2222 NDX_FLIST_OFFSET - dir_flist->used + 1);
2223 exit_cleanup(RERR_PROTOCOL);
2226 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2229 flist = recv_file_list(f);
2230 flist->parent_ndx = ndx;
2234 /* Search for an identically-named item in the file list. Note that the
2235 * items must agree in their directory-ness, or no match is returned. */
2236 int flist_find(struct file_list *flist, struct file_struct *f)
2238 int low = flist->low, high = flist->high;
2239 int diff, mid, mid_up;
2241 while (low <= high) {
2242 mid = (low + high) / 2;
2243 if (F_IS_ACTIVE(flist->sorted[mid]))
2246 /* Scan for the next non-empty entry using the cached
2247 * distance values. If the value isn't fully up-to-
2248 * date, update it. */
2249 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2250 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2252 mid_up += F_DEPTH(flist->sorted[mid_up]);
2253 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2254 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2256 if (mid_up > high) {
2257 /* If there's nothing left above us, set high to
2258 * a non-empty entry below us and continue. */
2259 high = mid - (int)flist->sorted[mid]->len32;
2260 if (!F_IS_ACTIVE(flist->sorted[high])) {
2262 high -= (int)flist->sorted[high]->len32;
2263 } while (!F_IS_ACTIVE(flist->sorted[high]));
2264 flist->sorted[mid]->len32 = mid - high;
2269 diff = f_name_cmp(flist->sorted[mid_up], f);
2271 if (protocol_version < 29
2272 && S_ISDIR(flist->sorted[mid_up]->mode)
2273 != S_ISDIR(f->mode))
2286 * Free up any resources a file_struct has allocated
2287 * and clear the file.
2289 void clear_file(struct file_struct *file)
2291 /* The +1 zeros out the first char of the basename. */
2292 memset(file, 0, FILE_STRUCT_LEN + 1);
2293 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2294 * entry. Likewise for len32 in the opposite direction. We assume
2295 * that we're alone for now since flist_find() will adjust the counts
2296 * it runs into that aren't up-to-date. */
2297 file->len32 = F_DEPTH(file) = 1;
2300 /* Allocate a new file list. */
2301 struct file_list *flist_new(int flags, char *msg)
2303 struct file_list *flist;
2305 if (!(flist = new0(struct file_list)))
2308 if (flags & FLIST_TEMP) {
2309 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2310 out_of_memory, POOL_INTERN)))
2313 /* This is a doubly linked list with prev looping back to
2314 * the end of the list, but the last next pointer is NULL. */
2316 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2317 out_of_memory, POOL_INTERN);
2318 if (!flist->file_pool)
2321 flist->ndx_start = inc_recurse ? 1 : 0;
2323 first_flist = cur_flist = flist->prev = flist;
2325 flist->file_pool = first_flist->file_pool;
2327 flist->ndx_start = first_flist->prev->ndx_end + 2;
2328 if (flist->ndx_start <= high_hlink_ndx)
2329 flist->ndx_start = high_hlink_ndx + 1;
2331 flist->prev = first_flist->prev;
2332 flist->prev->next = first_flist->prev = flist;
2334 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2341 /* Free up all elements in a flist. */
2342 void flist_free(struct file_list *flist)
2345 /* Was FLIST_TEMP dir-list. */
2346 } else if (flist == flist->prev) {
2347 first_flist = cur_flist = NULL;
2351 if (flist == cur_flist)
2352 cur_flist = flist->next;
2353 if (flist == first_flist)
2354 first_flist = first_flist->next;
2356 flist->prev->next = flist->next;
2358 flist->next = first_flist;
2360 flist->next->prev = flist->prev;
2361 file_total -= flist->used;
2365 if (!flist->prev || !flist_cnt)
2366 pool_destroy(flist->file_pool);
2368 pool_free_old(flist->file_pool, flist->pool_boundary);
2370 if (flist->sorted && flist->sorted != flist->files)
2371 free(flist->sorted);
2376 /* This routine ensures we don't have any duplicate names in our file list.
2377 * duplicate names can cause corruption because of the pipelining. */
2378 static void clean_flist(struct file_list *flist, int strip_root)
2380 char fbuf[MAXPATHLEN];
2385 if (flist->used == 0) {
2391 fsort(flist->sorted, flist->used);
2393 if (!am_sender || inc_recurse) {
2394 for (i = prev_i = 0; i < flist->used; i++) {
2395 if (F_IS_ACTIVE(flist->sorted[i])) {
2400 flist->low = prev_i;
2402 i = prev_i = flist->used - 1;
2406 while (++i < flist->used) {
2408 struct file_struct *file = flist->sorted[i];
2410 if (!F_IS_ACTIVE(file))
2412 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2414 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2415 int save_mode = file->mode;
2416 /* Make sure that this directory doesn't duplicate a
2417 * non-directory earlier in the list. */
2418 flist->high = prev_i;
2419 file->mode = S_IFREG;
2420 j = flist_find(flist, file);
2421 file->mode = save_mode;
2426 /* If one is a dir and the other is not, we want to
2427 * keep the dir because it might have contents in the
2428 * list. Otherwise keep the first one. */
2429 if (S_ISDIR(file->mode)) {
2430 struct file_struct *fp = flist->sorted[j];
2431 if (!S_ISDIR(fp->mode))
2435 file->flags |= FLAG_DUPLICATE;
2436 else { /* Make sure we merge our vital flags. */
2437 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2438 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2448 "removing duplicate name %s from file list (%d)\n",
2449 f_name(file, fbuf), drop + flist->ndx_start);
2451 clear_file(flist->sorted[drop]);
2455 if (flist->low == drop) {
2457 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2466 flist->high = prev_i;
2469 /* We need to strip off the leading slashes for relative
2470 * paths, but this must be done _after_ the sorting phase. */
2471 for (i = flist->low; i <= flist->high; i++) {
2472 struct file_struct *file = flist->sorted[i];
2476 while (*file->dirname == '/')
2478 if (!*file->dirname)
2479 file->dirname = NULL;
2483 if (prune_empty_dirs && !am_sender) {
2484 int j, prev_depth = 0;
2486 prev_i = 0; /* It's OK that this isn't really true. */
2488 for (i = flist->low; i <= flist->high; i++) {
2489 struct file_struct *fp, *file = flist->sorted[i];
2491 /* This temporarily abuses the F_DEPTH() value for a
2492 * directory that is in a chain that might get pruned.
2493 * We restore the old value if it gets a reprieve. */
2494 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2495 /* Dump empty dirs when coming back down. */
2496 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2497 fp = flist->sorted[prev_i];
2498 if (F_DEPTH(fp) >= 0)
2500 prev_i = -F_DEPTH(fp)-1;
2503 prev_depth = F_DEPTH(file);
2504 if (is_excluded(f_name(file, fbuf), 1,
2506 /* Keep dirs through this dir. */
2507 for (j = prev_depth-1; ; j--) {
2508 fp = flist->sorted[prev_i];
2509 if (F_DEPTH(fp) >= 0)
2511 prev_i = -F_DEPTH(fp)-1;
2515 F_DEPTH(file) = -prev_i-1;
2518 /* Keep dirs through this non-dir. */
2519 for (j = prev_depth; ; j--) {
2520 fp = flist->sorted[prev_i];
2521 if (F_DEPTH(fp) >= 0)
2523 prev_i = -F_DEPTH(fp)-1;
2528 /* Dump all remaining empty dirs. */
2530 struct file_struct *fp = flist->sorted[prev_i];
2531 if (F_DEPTH(fp) >= 0)
2533 prev_i = -F_DEPTH(fp)-1;
2537 for (i = flist->low; i <= flist->high; i++) {
2538 if (F_IS_ACTIVE(flist->sorted[i]))
2542 for (i = flist->high; i >= flist->low; i--) {
2543 if (F_IS_ACTIVE(flist->sorted[i]))
2550 static void output_flist(struct file_list *flist)
2552 char uidbuf[16], gidbuf[16], depthbuf[16];
2553 struct file_struct *file;
2554 const char *root, *dir, *slash, *name, *trail;
2555 const char *who = who_am_i();
2558 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2559 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2560 for (i = 0; i < flist->used; i++) {
2561 file = flist->files[i];
2562 if ((am_root || am_sender) && uid_ndx) {
2563 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2568 static char parens[] = "(\0)\0\0\0";
2569 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2570 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2571 pp, F_GROUP(file), pp + 2);
2575 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2576 if (F_IS_ACTIVE(file)) {
2577 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2578 if ((dir = file->dirname) == NULL)
2582 name = file->basename;
2583 trail = S_ISDIR(file->mode) ? "/" : "";
2585 root = dir = slash = name = trail = "";
2587 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2588 who, i + flist->ndx_start,
2589 root, dir, slash, name, trail,
2590 (int)file->mode, (double)F_LENGTH(file),
2591 uidbuf, gidbuf, file->flags);
2595 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2596 enum fnc_type { t_PATH, t_ITEM };
2598 static int found_prefix;
2600 /* Compare the names of two file_struct entities, similar to how strcmp()
2601 * would do if it were operating on the joined strings.
2603 * Some differences beginning with protocol_version 29: (1) directory names
2604 * are compared with an assumed trailing slash so that they compare in a
2605 * way that would cause them to sort immediately prior to any content they
2606 * may have; (2) a directory of any name compares after a non-directory of
2607 * any name at the same depth; (3) a directory with name "." compares prior
2608 * to anything else. These changes mean that a directory and a non-dir
2609 * with the same name will not compare as equal (protocol_version >= 29).
2611 * The dirname component can be an empty string, but the basename component
2612 * cannot (and never is in the current codebase). The basename component
2613 * may be NULL (for a removed item), in which case it is considered to be
2614 * after any existing item. */
2615 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2618 const uchar *c1, *c2;
2619 enum fnc_state state1, state2;
2620 enum fnc_type type1, type2;
2621 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2623 if (!f1 || !F_IS_ACTIVE(f1)) {
2624 if (!f2 || !F_IS_ACTIVE(f2))
2628 if (!f2 || !F_IS_ACTIVE(f2))
2631 c1 = (uchar*)f1->dirname;
2632 c2 = (uchar*)f2->dirname;
2636 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2637 c1 = (const uchar*)f1->basename;
2638 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2640 state1 = s_TRAILING;
2649 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2650 c2 = (const uchar*)f2->basename;
2651 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2653 state2 = s_TRAILING;
2663 return type1 == t_PATH ? 1 : -1;
2673 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2674 c1 = (const uchar*)f1->basename;
2675 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2677 state1 = s_TRAILING;
2683 state1 = s_TRAILING;
2684 if (type1 == t_PATH) {
2693 if (*c2 && type1 != type2)
2694 return type1 == t_PATH ? 1 : -1;
2703 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2704 c2 = (const uchar*)f2->basename;
2705 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2707 state2 = s_TRAILING;
2713 state2 = s_TRAILING;
2714 if (type2 == t_PATH) {
2727 return type1 == t_PATH ? 1 : -1;
2729 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2734 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2735 * not match if f2's basename is not an exact match of a path element in f1.
2736 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2737 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2741 return found_prefix;
2744 char *f_name_buf(void)
2746 static char names[5][MAXPATHLEN];
2747 static unsigned int n;
2749 n = (n + 1) % (sizeof names / sizeof names[0]);
2754 /* Return a copy of the full filename of a flist entry, using the indicated
2755 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2756 * done because we checked the size when creating the file_struct entry.
2758 char *f_name(const struct file_struct *f, char *fbuf)
2760 if (!f || !F_IS_ACTIVE(f))
2764 fbuf = f_name_buf();
2767 int len = strlen(f->dirname);
2768 memcpy(fbuf, f->dirname, len);
2770 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2772 strlcpy(fbuf, f->basename, MAXPATHLEN);
2777 /* Do a non-recursive scan of the named directory, possibly ignoring all
2778 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2779 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2780 * buffer (the functions we call will append names onto the end, but the old
2781 * dir value will be restored on exit). */
2782 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2784 struct file_list *dirlist;
2785 char dirbuf[MAXPATHLEN];
2786 int save_recurse = recurse;
2787 int save_xfer_dirs = xfer_dirs;
2788 int save_prune_empty_dirs = prune_empty_dirs;
2791 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2792 if (dlen >= MAXPATHLEN)
2797 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2801 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2802 xfer_dirs = save_xfer_dirs;
2803 recurse = save_recurse;
2805 flist_count_offset += dirlist->used;
2807 prune_empty_dirs = 0;
2808 dirlist->sorted = dirlist->files;
2809 clean_flist(dirlist, 0);
2810 prune_empty_dirs = save_prune_empty_dirs;
2813 output_flist(dirlist);