2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
33 extern int 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;
42 extern int filesfrom_fd;
43 extern int one_file_system;
44 extern int copy_dirlinks;
45 extern int keep_dirlinks;
46 extern int preserve_acls;
47 extern int preserve_xattrs;
48 extern int preserve_links;
49 extern int preserve_hard_links;
50 extern int preserve_devices;
51 extern int preserve_specials;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int relative_paths;
55 extern int implied_dirs;
56 extern int file_extra_cnt;
57 extern int ignore_perishable;
58 extern int non_perishable_cnt;
59 extern int prune_empty_dirs;
60 extern int copy_links;
61 extern int copy_unsafe_links;
62 extern int protocol_version;
63 extern int sanitize_paths;
64 extern struct stats stats;
66 extern char curr_dir[MAXPATHLEN];
68 extern struct chmod_mode_struct *chmod_modes;
70 extern struct filter_list_struct filter_list;
71 extern struct filter_list_struct server_filter_list;
75 extern int need_unsorted_flist;
76 extern iconv_t ic_send, ic_recv;
81 dev_t filesystem_dev; /* used to implement -x */
83 struct file_list *cur_flist, *first_flist, *dir_flist;
84 int send_dir_ndx = -1, send_dir_depth = 0;
85 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
86 int file_total = 0; /* total of all active items over all file-lists */
87 int flist_eof = 0; /* all the file-lists are now known */
89 /* The tmp_* vars are used as a cache area by make_file() to store data
90 * that the sender doesn't need to remember in its file list. The data
91 * will survive just long enough to be used by send_file_entry(). */
92 static dev_t tmp_rdev;
93 #ifdef SUPPORT_HARD_LINKS
94 static int64 tmp_dev, tmp_ino;
96 static char tmp_sum[MAX_DIGEST_LEN];
98 static char empty_sum[MAX_DIGEST_LEN];
99 static int flist_count_offset; /* for --delete --progress */
101 static void clean_flist(struct file_list *flist, int strip_root);
102 static void output_flist(struct file_list *flist);
104 void init_flist(void)
107 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
108 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
110 checksum_len = protocol_version < 21 ? 2
111 : protocol_version < 30 ? MD4_DIGEST_LEN
115 static int show_filelist_p(void)
117 return verbose && xfer_dirs && !am_server && !inc_recurse;
120 static void start_filelist_progress(char *kind)
122 rprintf(FCLIENT, "%s ... ", kind);
123 if (verbose > 1 || do_progress)
124 rprintf(FCLIENT, "\n");
128 static void emit_filelist_progress(int count)
130 rprintf(FCLIENT, " %d files...\r", count);
133 static void maybe_emit_filelist_progress(int count)
135 if (do_progress && show_filelist_p() && (count % 100) == 0)
136 emit_filelist_progress(count);
139 static void finish_filelist_progress(const struct file_list *flist)
142 /* This overwrites the progress line */
143 rprintf(FINFO, "%d file%sto consider\n",
144 flist->count, flist->count == 1 ? " " : "s ");
146 rprintf(FINFO, "done\n");
149 void show_flist_stats(void)
154 static void list_file_entry(struct file_struct *f)
156 char permbuf[PERMSTRING_SIZE];
159 if (!F_IS_ACTIVE(f)) {
160 /* this can happen if duplicate names were removed */
164 permstring(permbuf, f->mode);
167 /* TODO: indicate '+' if the entry has an ACL. */
170 if (preserve_links && S_ISLNK(f->mode)) {
171 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
172 permbuf, len, timestring(f->modtime),
173 f_name(f, NULL), F_SYMLINK(f));
177 rprintf(FINFO, "%s %11.0f %s %s\n",
178 permbuf, len, timestring(f->modtime),
183 /* Stat either a symlink or its referent, depending on the settings of
184 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
186 * If path is the name of a symlink, then the linkbuf buffer (which must hold
187 * MAXPATHLEN chars) will be set to the symlink's target string.
189 * The stat structure pointed to by stp will contain information about the
190 * link or the referent as appropriate, if they exist. */
191 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
194 if (link_stat(path, stp, copy_dirlinks) < 0)
196 if (S_ISLNK(stp->st_mode)) {
197 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
200 linkbuf[llen] = '\0';
201 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
203 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
206 return x_stat(path, stp, NULL);
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;
272 * Make sure @p flist is big enough to hold at least @p flist->count
275 void flist_expand(struct file_list *flist)
277 struct file_struct **new_ptr;
279 if (flist->count < flist->malloced)
282 if (flist->malloced < FLIST_START)
283 flist->malloced = FLIST_START;
284 else if (flist->malloced >= FLIST_LINEAR)
285 flist->malloced += FLIST_LINEAR;
287 flist->malloced *= 2;
289 /* In case count jumped or we are starting the list
290 * with a known size just set it. */
291 if (flist->malloced < flist->count)
292 flist->malloced = flist->count;
295 if (inc_recurse && flist == dir_flist && need_unsorted_flist) {
296 flist->sorted = realloc_array(flist->sorted, struct file_struct *,
301 new_ptr = realloc_array(flist->files, struct file_struct *,
304 if (verbose >= 2 && flist->malloced != FLIST_START) {
305 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
307 (double)sizeof flist->files[0] * flist->malloced,
308 (new_ptr == flist->files) ? " not" : "");
311 flist->files = new_ptr;
314 out_of_memory("flist_expand");
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)
346 static time_t modtime;
350 static uint32 rdev_major;
353 static char *user_name, *group_name;
354 static char lastname[MAXPATHLEN];
355 char fname[MAXPATHLEN];
356 int first_hlink_ndx = -1;
361 if (ic_send != (iconv_t)-1) {
362 ICONV_CONST char *ibuf;
364 size_t ocnt = MAXPATHLEN, icnt;
366 iconv(ic_send, NULL,0, NULL,0);
367 if ((ibuf = (ICONV_CONST char *)file->dirname) != NULL) {
369 ocnt--; /* pre-subtract the space for the '/' */
370 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1)
375 ibuf = (ICONV_CONST char *)file->basename;
377 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
379 io_error |= IOERR_GENERAL;
381 "[%s] cannot convert filename: %s (%s)\n",
382 who_am_i(), f_name(file, fname), strerror(errno));
390 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
392 if (file->mode == mode)
393 flags |= XMIT_SAME_MODE;
396 if ((preserve_devices && IS_DEVICE(mode))
397 || (preserve_specials && IS_SPECIAL(mode))) {
398 if (protocol_version < 28) {
399 if (tmp_rdev == rdev)
400 flags |= XMIT_SAME_RDEV_pre28;
405 if ((uint32)major(rdev) == rdev_major)
406 flags |= XMIT_SAME_RDEV_MAJOR;
408 rdev_major = major(rdev);
409 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
410 flags |= XMIT_RDEV_MINOR_8_pre30;
412 } else if (protocol_version < 28)
413 rdev = MAKEDEV(0, 0);
415 if ((uid_t)F_OWNER(file) == uid && *lastname)
416 flags |= XMIT_SAME_UID;
419 if (preserve_uid && !numeric_ids) {
420 user_name = add_uid(uid);
421 if (inc_recurse && user_name)
422 flags |= XMIT_USER_NAME_FOLLOWS;
427 if ((gid_t)F_GROUP(file) == gid && *lastname)
428 flags |= XMIT_SAME_GID;
431 if (preserve_gid && !numeric_ids) {
432 group_name = add_gid(gid);
433 if (inc_recurse && group_name)
434 flags |= XMIT_GROUP_NAME_FOLLOWS;
438 if (file->modtime == modtime)
439 flags |= XMIT_SAME_TIME;
441 modtime = file->modtime;
443 #ifdef SUPPORT_HARD_LINKS
445 if (protocol_version >= 30) {
446 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
447 first_hlink_ndx = (int32)(long)np->data - 1;
448 if (first_hlink_ndx < 0) {
449 np->data = (void*)(long)(ndx + 1);
450 flags |= XMIT_HLINK_FIRST;
452 flags |= XMIT_HLINKED;
454 if (tmp_dev == dev) {
455 if (protocol_version >= 28)
456 flags |= XMIT_SAME_DEV_pre30;
459 flags |= XMIT_HLINKED;
465 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
467 l2 = strlen(fname+l1);
470 flags |= XMIT_SAME_NAME;
472 flags |= XMIT_LONG_NAME;
474 /* We must make sure we don't send a zero flag byte or the
475 * other end will terminate the flist transfer. Note that
476 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
477 * it's harmless way to add a bit to the first flag byte. */
478 if (protocol_version >= 28) {
479 if (!flags && !S_ISDIR(mode))
480 flags |= XMIT_TOP_DIR;
481 if ((flags & 0xFF00) || !flags) {
482 flags |= XMIT_EXTENDED_FLAGS;
483 write_shortint(f, flags);
485 write_byte(f, flags);
488 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
489 write_byte(f, flags);
491 if (flags & XMIT_SAME_NAME)
493 if (flags & XMIT_LONG_NAME)
494 write_varint30(f, l2);
497 write_buf(f, fname + l1, l2);
499 if (first_hlink_ndx >= 0) {
500 write_varint30(f, first_hlink_ndx);
504 write_varlong30(f, F_LENGTH(file), 3);
505 if (!(flags & XMIT_SAME_TIME)) {
506 if (protocol_version >= 30)
507 write_varlong(f, modtime, 4);
509 write_int(f, modtime);
511 if (!(flags & XMIT_SAME_MODE))
512 write_int(f, to_wire_mode(mode));
513 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
514 if (protocol_version < 30)
517 write_varint(f, uid);
518 if (flags & XMIT_USER_NAME_FOLLOWS) {
519 int len = strlen(user_name);
521 write_buf(f, user_name, len);
525 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
526 if (protocol_version < 30)
529 write_varint(f, gid);
530 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
531 int len = strlen(group_name);
533 write_buf(f, group_name, len);
537 if ((preserve_devices && IS_DEVICE(mode))
538 || (preserve_specials && IS_SPECIAL(mode))) {
539 if (protocol_version < 28) {
540 if (!(flags & XMIT_SAME_RDEV_pre28))
541 write_int(f, (int)rdev);
543 if (!(flags & XMIT_SAME_RDEV_MAJOR))
544 write_varint30(f, major(rdev));
545 if (protocol_version >= 30)
546 write_varint(f, minor(rdev));
547 else if (flags & XMIT_RDEV_MINOR_8_pre30)
548 write_byte(f, minor(rdev));
550 write_int(f, minor(rdev));
555 if (preserve_links && S_ISLNK(mode)) {
556 const char *sl = F_SYMLINK(file);
557 int len = strlen(sl);
558 write_varint30(f, len);
559 write_buf(f, sl, len);
563 #ifdef SUPPORT_HARD_LINKS
564 if (tmp_dev != 0 && protocol_version < 30) {
565 if (protocol_version < 26) {
566 /* 32-bit dev_t and ino_t */
567 write_int(f, (int32)dev);
568 write_int(f, (int32)tmp_ino);
570 /* 64-bit dev_t and ino_t */
571 if (!(flags & XMIT_SAME_DEV_pre30))
572 write_longint(f, dev);
573 write_longint(f, tmp_ino);
578 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
583 /* Prior to 28, we sent a useless set of nulls. */
586 write_buf(f, sum, checksum_len);
590 strlcpy(lastname, fname, MAXPATHLEN);
592 if (S_ISREG(mode) || S_ISLNK(mode))
593 stats.total_size += F_LENGTH(file);
596 static struct file_struct *recv_file_entry(struct file_list *flist,
599 static int64 modtime;
603 static uint32 rdev_major;
606 static uint16 gid_flags;
607 static char lastname[MAXPATHLEN], *lastdir;
608 static int lastdir_depth, lastdir_len = -1;
609 static unsigned int del_hier_name_len = 0;
610 static int in_del_hier = 0;
611 char thisname[MAXPATHLEN];
612 unsigned int l1 = 0, l2 = 0;
613 int alloc_len, basename_len, linkname_len;
614 int extra_len = file_extra_cnt * EXTRA_LEN;
615 int first_hlink_ndx = -1;
617 const char *basename;
618 struct file_struct *file;
622 if (xflags & XMIT_SAME_NAME)
625 if (xflags & XMIT_LONG_NAME)
626 l2 = read_varint30(f);
630 if (l2 >= MAXPATHLEN - l1) {
632 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
633 xflags, l1, l2, lastname, who_am_i());
634 overflow_exit("recv_file_entry");
637 strlcpy(thisname, lastname, l1 + 1);
638 read_sbuf(f, &thisname[l1], l2);
639 thisname[l1 + l2] = 0;
641 /* Abuse basename_len for a moment... */
642 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
645 if (ic_recv != (iconv_t)-1) {
646 char *obuf = thisname, *ibuf = lastname;
647 size_t ocnt = MAXPATHLEN, icnt = basename_len;
649 if (icnt >= MAXPATHLEN) {
654 iconv(ic_recv, NULL,0, NULL,0);
655 if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
657 io_error |= IOERR_GENERAL;
659 "[%s] cannot convert filename: %s (%s)\n",
660 who_am_i(), lastname, strerror(errno));
667 clean_fname(thisname, 0);
670 sanitize_path(thisname, thisname, "", 0, NULL);
672 if ((basename = strrchr(thisname, '/')) != NULL) {
673 int len = basename++ - thisname;
674 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
675 lastdir = new_array(char, len + 1);
676 memcpy(lastdir, thisname, len);
679 lastdir_depth = count_dir_elements(lastdir);
683 basename_len = strlen(basename) + 1; /* count the '\0' */
685 #ifdef SUPPORT_HARD_LINKS
686 if (protocol_version >= 30
687 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
688 struct file_struct *first;
689 first_hlink_ndx = read_varint30(f);
690 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
692 "hard-link reference out of range: %d (%d)\n",
693 first_hlink_ndx, flist->count);
694 exit_cleanup(RERR_PROTOCOL);
696 first = flist->files[first_hlink_ndx];
697 file_length = F_LENGTH(first);
698 modtime = first->modtime;
701 uid = F_OWNER(first);
703 gid = F_GROUP(first);
704 if ((preserve_devices && IS_DEVICE(mode))
705 || (preserve_specials && IS_SPECIAL(mode))) {
706 uint32 *devp = F_RDEV_P(first);
707 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
708 extra_len += 2 * EXTRA_LEN;
710 if (preserve_links && S_ISLNK(mode))
711 linkname_len = strlen(F_SYMLINK(first)) + 1;
718 file_length = read_varlong30(f, 3);
719 if (!(xflags & XMIT_SAME_TIME)) {
720 if (protocol_version >= 30) {
721 modtime = read_varlong(f, 4);
722 #if SIZEOF_TIME_T < SIZEOF_INT64
723 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
725 "Time value of %s truncated on receiver.\n",
730 modtime = read_int(f);
732 if (!(xflags & XMIT_SAME_MODE))
733 mode = from_wire_mode(read_int(f));
735 if (chmod_modes && !S_ISLNK(mode))
736 mode = tweak_mode(mode, chmod_modes);
738 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
739 if (protocol_version < 30)
740 uid = (uid_t)read_int(f);
742 uid = (uid_t)read_varint(f);
743 if (xflags & XMIT_USER_NAME_FOLLOWS)
744 uid = recv_user_name(f, uid);
745 else if (inc_recurse && am_root && !numeric_ids)
746 uid = match_uid(uid);
749 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
750 if (protocol_version < 30)
751 gid = (gid_t)read_int(f);
753 gid = (gid_t)read_varint(f);
755 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
756 gid = recv_group_name(f, gid, &gid_flags);
757 else if (inc_recurse && (!am_root || !numeric_ids))
758 gid = match_gid(gid, &gid_flags);
762 if ((preserve_devices && IS_DEVICE(mode))
763 || (preserve_specials && IS_SPECIAL(mode))) {
764 if (protocol_version < 28) {
765 if (!(xflags & XMIT_SAME_RDEV_pre28))
766 rdev = (dev_t)read_int(f);
769 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
770 rdev_major = read_varint30(f);
771 if (protocol_version >= 30)
772 rdev_minor = read_varint(f);
773 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
774 rdev_minor = read_byte(f);
776 rdev_minor = read_int(f);
777 rdev = MAKEDEV(rdev_major, rdev_minor);
779 extra_len += 2 * EXTRA_LEN;
781 } else if (protocol_version < 28)
782 rdev = MAKEDEV(0, 0);
785 if (preserve_links && S_ISLNK(mode)) {
786 linkname_len = read_varint30(f) + 1; /* count the '\0' */
787 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
788 rprintf(FERROR, "overflow: linkname_len=%d\n",
790 overflow_exit("recv_file_entry");
797 #ifdef SUPPORT_HARD_LINKS
799 if (preserve_hard_links) {
800 if (protocol_version < 28 && S_ISREG(mode))
801 xflags |= XMIT_HLINKED;
802 if (xflags & XMIT_HLINKED)
803 extra_len += EXTRA_LEN;
808 /* We need one or two index int32s when we're preserving ACLs. */
810 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
813 if (always_checksum && S_ISREG(mode))
814 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
816 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
817 extra_len += EXTRA_LEN;
819 #if EXTRA_ROUNDING > 0
820 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
821 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
824 if (inc_recurse && S_ISDIR(mode)) {
825 if (one_file_system) {
826 /* Room to save the dir's device for -x */
827 extra_len += 2 * EXTRA_LEN;
829 pool = dir_flist->file_pool;
831 pool = flist->file_pool;
833 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
835 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
837 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
839 file = (struct file_struct *)bp;
840 bp += FILE_STRUCT_LEN;
842 memcpy(bp, basename, basename_len);
843 bp += basename_len + linkname_len; /* skip space for symlink too */
845 #ifdef SUPPORT_HARD_LINKS
846 if (xflags & XMIT_HLINKED)
847 file->flags |= FLAG_HLINKED;
849 file->modtime = (time_t)modtime;
850 file->len32 = (uint32)file_length;
851 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
852 file->flags |= FLAG_LENGTH64;
853 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
860 file->flags |= gid_flags;
864 F_NDX(file) = flist->count + flist->ndx_start;
867 if (basename != thisname) {
868 file->dirname = lastdir;
869 F_DEPTH(file) = lastdir_depth + 1;
874 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
876 if (xflags & XMIT_TOP_DIR) {
877 in_del_hier = recurse;
878 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
879 if (relative_paths && del_hier_name_len > 2
880 && lastname[del_hier_name_len-1] == '.'
881 && lastname[del_hier_name_len-2] == '/')
882 del_hier_name_len -= 2;
883 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
884 } else if (in_del_hier) {
885 if (!relative_paths || !del_hier_name_len
886 || (l1 >= del_hier_name_len
887 && lastname[del_hier_name_len] == '/'))
888 file->flags |= FLAG_XFER_DIR;
894 if ((preserve_devices && IS_DEVICE(mode))
895 || (preserve_specials && IS_SPECIAL(mode))) {
896 uint32 *devp = F_RDEV_P(file);
897 DEV_MAJOR(devp) = major(rdev);
898 DEV_MINOR(devp) = minor(rdev);
903 bp = (char*)file->basename + basename_len;
904 if (first_hlink_ndx >= 0) {
905 struct file_struct *first = flist->files[first_hlink_ndx];
906 memcpy(bp, F_SYMLINK(first), linkname_len);
908 read_sbuf(f, bp, linkname_len - 1);
910 sanitize_path(bp, bp, "", lastdir_depth, NULL);
914 #ifdef SUPPORT_HARD_LINKS
915 if (preserve_hard_links && xflags & XMIT_HLINKED) {
916 if (protocol_version >= 30) {
917 F_HL_GNUM(file) = xflags & XMIT_HLINK_FIRST
918 ? flist->count : first_hlink_ndx;
920 static int32 cnt = 0;
921 struct idev_node *np;
924 if (protocol_version < 26) {
928 if (!(xflags & XMIT_SAME_DEV_pre30))
929 dev = read_longint(f);
930 ino = read_longint(f);
932 np = idev_node(dev, ino);
933 ndx = (int32)(long)np->data - 1;
936 np->data = (void*)(long)cnt;
938 F_HL_GNUM(file) = ndx;
943 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
945 bp = (char*)F_SUM(file);
947 /* Prior to 28, we get a useless set of nulls. */
950 if (first_hlink_ndx >= 0) {
951 struct file_struct *first = flist->files[first_hlink_ndx];
952 memcpy(bp, F_SUM(first), checksum_len);
954 read_buf(f, bp, checksum_len);
958 if (preserve_acls && !S_ISLNK(mode))
959 receive_acl(file, f);
961 #ifdef SUPPORT_XATTRS
963 receive_xattr(file, f );
966 if (S_ISREG(mode) || S_ISLNK(mode))
967 stats.total_size += file_length;
973 * Create a file_struct for a named file by reading its stat()
974 * information and performing extensive checks against global
977 * @return the new file, or NULL if there was an error or this file
978 * should be excluded.
980 * @todo There is a small optimization opportunity here to avoid
981 * stat()ing the file in some circumstances, which has a certain cost.
982 * We are called immediately after doing readdir(), and so we may
983 * already know the d_type of the file. We could for example avoid
984 * statting directories if we're not recursing, but this is not a very
985 * important case. Some systems may not have d_type.
987 struct file_struct *make_file(const char *fname, struct file_list *flist,
988 STRUCT_STAT *stp, int flags, int filter_level)
990 static char *lastdir;
991 static int lastdir_len = -1;
992 struct file_struct *file;
994 char thisname[MAXPATHLEN];
995 char linkname[MAXPATHLEN];
996 int alloc_len, basename_len, linkname_len;
997 int extra_len = file_extra_cnt * EXTRA_LEN;
998 const char *basename;
1001 if (strlcpy(thisname, fname, sizeof thisname)
1002 >= sizeof thisname - pathname_len) {
1003 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1006 clean_fname(thisname, 0);
1008 sanitize_path(thisname, thisname, "", 0, NULL);
1010 if (stp && S_ISDIR(stp->st_mode)) {
1011 st = *stp; /* Needed for "symlink/." with --relative. */
1012 *linkname = '\0'; /* make IBM code checker happy */
1013 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1014 int save_errno = errno;
1015 /* See if file is excluded before reporting an error. */
1016 if (filter_level != NO_FILTERS
1017 && (is_excluded(thisname, 0, filter_level)
1018 || is_excluded(thisname, 1, filter_level))) {
1019 if (ignore_perishable && save_errno != ENOENT)
1020 non_perishable_cnt++;
1023 if (save_errno == ENOENT) {
1024 #ifdef SUPPORT_LINKS
1025 /* Avoid "vanished" error if symlink points nowhere. */
1026 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1027 && S_ISLNK(st.st_mode)) {
1028 io_error |= IOERR_GENERAL;
1029 rprintf(FERROR, "symlink has no referent: %s\n",
1030 full_fname(thisname));
1034 enum logcode c = am_daemon && protocol_version < 28
1036 io_error |= IOERR_VANISHED;
1037 rprintf(c, "file has vanished: %s\n",
1038 full_fname(thisname));
1041 io_error |= IOERR_GENERAL;
1042 rsyserr(FERROR, save_errno, "readlink %s failed",
1043 full_fname(thisname));
1048 /* backup.c calls us with filter_level set to NO_FILTERS. */
1049 if (filter_level == NO_FILTERS)
1052 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1053 rprintf(FINFO, "skipping directory %s\n", thisname);
1057 /* -x only affects directories because we need to avoid recursing
1058 * into a mount-point directory, not to avoid copying a symlinked
1059 * file if -L (or similar) was specified. */
1060 if (one_file_system && st.st_dev != filesystem_dev
1061 && S_ISDIR(st.st_mode)) {
1062 if (one_file_system > 1) {
1064 rprintf(FINFO, "skipping mount-point dir %s\n",
1069 flags |= FLAG_MOUNT_DIR;
1072 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1073 if (ignore_perishable)
1074 non_perishable_cnt++;
1078 if (lp_ignore_nonreadable(module_id)) {
1079 #ifdef SUPPORT_LINKS
1080 if (!S_ISLNK(st.st_mode))
1082 if (access(thisname, R_OK) != 0)
1088 /* Only divert a directory in the main transfer. */
1089 if (flist && flist->prev && S_ISDIR(st.st_mode)
1090 && flags & FLAG_DIVERT_DIRS) {
1092 /* Room for parent/sibling/next-child info. */
1093 extra_len += 3 * EXTRA_LEN;
1097 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1098 who_am_i(), thisname, filter_level);
1101 if ((basename = strrchr(thisname, '/')) != NULL) {
1102 int len = basename++ - thisname;
1103 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1104 lastdir = new_array(char, len + 1);
1105 memcpy(lastdir, thisname, len);
1106 lastdir[len] = '\0';
1110 basename = thisname;
1111 basename_len = strlen(basename) + 1; /* count the '\0' */
1113 #ifdef SUPPORT_LINKS
1114 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1119 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1120 extra_len += EXTRA_LEN;
1122 #if EXTRA_ROUNDING > 0
1123 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1124 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1127 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1130 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1132 if (!(bp = new_array(char, alloc_len)))
1133 out_of_memory("make_file");
1136 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1138 file = (struct file_struct *)bp;
1139 bp += FILE_STRUCT_LEN;
1141 memcpy(bp, basename, basename_len);
1142 bp += basename_len + linkname_len; /* skip space for symlink too */
1144 #ifdef SUPPORT_HARD_LINKS
1145 if (preserve_hard_links && flist && flist->prev) {
1146 if (protocol_version >= 28
1147 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1148 : S_ISREG(st.st_mode)) {
1149 tmp_dev = st.st_dev;
1150 tmp_ino = st.st_ino;
1156 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1157 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1158 tmp_rdev = st.st_rdev;
1163 file->flags = flags;
1164 file->modtime = st.st_mtime;
1165 file->len32 = (uint32)st.st_size;
1166 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1167 file->flags |= FLAG_LENGTH64;
1168 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1170 file->mode = st.st_mode;
1172 F_OWNER(file) = st.st_uid;
1174 F_GROUP(file) = st.st_gid;
1176 if (basename != thisname)
1177 file->dirname = lastdir;
1179 #ifdef SUPPORT_LINKS
1181 bp = (char*)file->basename + basename_len;
1182 memcpy(bp, linkname, linkname_len);
1186 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1187 file_checksum(thisname, tmp_sum, st.st_size);
1189 F_PATHNAME(file) = pathname;
1191 /* This code is only used by the receiver when it is building
1192 * a list of files for a delete pass. */
1193 if (keep_dirlinks && linkname_len && flist) {
1195 int save_mode = file->mode;
1196 file->mode = S_IFDIR; /* Find a directory with our name. */
1197 if (flist_find(dir_flist, file) >= 0
1198 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1199 file->modtime = st2.st_mtime;
1201 file->mode = st2.st_mode;
1203 F_OWNER(file) = st2.st_uid;
1205 F_GROUP(file) = st2.st_gid;
1207 file->mode = save_mode;
1210 if (basename_len == 0+1)
1213 if (inc_recurse && flist == dir_flist) {
1214 flist_expand(dir_flist);
1217 F_NDX(file) = dir_flist->count;
1218 if (need_unsorted_flist)
1219 dir_flist->sorted[dir_flist->count] = file;
1221 dir_flist->files[dir_flist->count++] = file;
1227 /* Only called for temporary file_struct entries created by make_file(). */
1228 void unmake_file(struct file_struct *file)
1230 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1231 #if EXTRA_ROUNDING > 0
1232 if (extra_cnt & EXTRA_ROUNDING)
1233 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1235 free(REQ_EXTRA(file, extra_cnt));
1238 static struct file_struct *send_file_name(int f, struct file_list *flist,
1239 char *fname, STRUCT_STAT *stp,
1240 int flags, int filter_flags)
1242 struct file_struct *file;
1243 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1247 file = make_file(fname, flist, stp, flags, filter_flags);
1251 if (chmod_modes && !S_ISLNK(file->mode))
1252 file->mode = tweak_mode(file->mode, chmod_modes);
1255 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1256 sx.st.st_mode = file->mode;
1257 sx.acc_acl = sx.def_acl = NULL;
1258 if (get_acl(fname, &sx) < 0)
1262 #ifdef SUPPORT_XATTRS
1263 if (preserve_xattrs && f >= 0) {
1265 if (get_xattr(fname, &sx) < 0)
1270 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1272 flist_expand(flist);
1273 flist->files[flist->count++] = file;
1275 send_file_entry(f, file, flist->count - 1);
1277 if (preserve_acls && !S_ISLNK(file->mode)) {
1282 #ifdef SUPPORT_XATTRS
1283 if (preserve_xattrs) {
1284 F_XATTR(file) = send_xattr(&sx, f);
1292 static void send_if_directory(int f, struct file_list *flist,
1293 struct file_struct *file,
1294 char *fbuf, unsigned int ol,
1297 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1299 if (S_ISDIR(file->mode)
1300 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1302 unsigned int len = strlen(fbuf);
1303 if (len > 1 && fbuf[len-1] == '/')
1305 if (len >= MAXPATHLEN - 1) {
1306 io_error |= IOERR_GENERAL;
1307 rprintf(FERROR, "skipping long-named directory: %s\n",
1311 save_filters = push_local_filters(fbuf, len);
1312 send_directory(f, flist, fbuf, len, flags);
1313 pop_local_filters(save_filters);
1320 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1322 return f_name_cmp(*file1, *file2);
1325 /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1326 * sort them by name, and link them into the tree, setting the appropriate
1327 * parent/child/sibling pointers. */
1328 static void add_dirs_to_tree(int parent_ndx, int start, int end)
1332 int32 *parent_dp = parent_ndx < 0 ? NULL
1333 : F_DIRNODE_P(dir_flist->sorted[parent_ndx]);
1335 qsort(dir_flist->sorted + start, end - start + 1,
1336 sizeof dir_flist->sorted[0], (int (*)())file_compare);
1338 for (i = start; i <= end; i++) {
1339 struct file_struct *file = dir_flist->sorted[i];
1340 if (!(file->flags & FLAG_XFER_DIR)
1341 || file->flags & FLAG_MOUNT_DIR)
1344 DIR_NEXT_SIBLING(dp) = i;
1346 DIR_FIRST_CHILD(parent_dp) = i;
1349 dp = F_DIRNODE_P(file);
1350 DIR_PARENT(dp) = parent_ndx;
1351 DIR_FIRST_CHILD(dp) = -1;
1354 DIR_NEXT_SIBLING(dp) = -1;
1357 /* This function is normally called by the sender, but the receiving side also
1358 * calls it from get_dirlist() with f set to -1 so that we just construct the
1359 * file list in memory without sending it over the wire. Also, get_dirlist()
1360 * might call this with f set to -2, which also indicates that local filter
1361 * rules should be ignored. */
1362 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1369 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1370 int start = flist->count;
1371 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1373 assert(flist != NULL);
1375 if (!(d = opendir(fbuf))) {
1376 io_error |= IOERR_GENERAL;
1377 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1382 if (len != 1 || *fbuf != '/')
1385 remainder = MAXPATHLEN - (p - fbuf);
1387 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1388 char *dname = d_name(di);
1389 if (dname[0] == '.' && (dname[1] == '\0'
1390 || (dname[1] == '.' && dname[2] == '\0')))
1392 if (strlcpy(p, dname, remainder) >= remainder) {
1393 io_error |= IOERR_GENERAL;
1395 "cannot send long-named file %s\n",
1400 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
1406 io_error |= IOERR_GENERAL;
1407 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1412 if (f >= 0 && recurse && !divert_dirs) {
1413 int i, end = flist->count - 1;
1414 /* send_if_directory() bumps flist->count, so use "end". */
1415 for (i = start; i <= end; i++)
1416 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1420 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1422 char fbuf[MAXPATHLEN];
1426 dlen = strlen(fbuf);
1428 if (F_PATHNAME(file) != pathname) {
1429 if (!push_pathname(F_PATHNAME(file), -1))
1430 exit_cleanup(RERR_FILESELECT);
1433 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1435 send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1438 void send_extra_file_list(int f, int at_least)
1440 struct file_list *flist;
1442 int future_cnt, save_io_error = io_error;
1447 /* Keep sending data until we have the requested number of
1448 * files in the upcoming file-lists. */
1449 if (cur_flist->next) {
1450 flist = first_flist->prev; /* the newest flist */
1451 future_cnt = flist->count + flist->ndx_start
1452 - cur_flist->next->ndx_start;
1455 while (future_cnt < at_least) {
1456 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1457 int start = dir_flist->count;
1460 flist = flist_new(0, "send_extra_file_list");
1461 start_write = stats.total_written;
1463 /* If this is the first of a set of duplicate dirs, we must
1464 * send all the dirs together in a single file-list. We must
1465 * also send the index of the last dir in the header. */
1466 if (file->flags & FLAG_DUPLICATE) {
1467 int dir_ndx, end_ndx = send_dir_ndx;
1468 struct file_struct *fp = file;
1471 dp = F_DIRNODE_P(fp);
1472 end_ndx = DIR_NEXT_SIBLING(dp);
1473 fp = dir_flist->sorted[end_ndx];
1474 if (!(fp->flags & FLAG_DUPLICATE))
1480 dir_ndx = F_NDX(fp);
1484 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1487 send1extra(f, file, flist);
1488 if (send_dir_ndx == end_ndx)
1490 dp = F_DIRNODE_P(file);
1491 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1492 file = dir_flist->sorted[send_dir_ndx];
1498 dir_ndx = F_NDX(file);
1501 dir_ndx = send_dir_ndx;
1502 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1504 send1extra(f, file, flist);
1509 if (!need_unsorted_flist)
1511 dir_flist->sorted = dir_flist->files;
1512 add_dirs_to_tree(send_dir_ndx, start, dir_flist->count - 1);
1515 if (need_unsorted_flist) {
1517 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
1518 out_of_memory("send_extra_file_list");
1519 memcpy(flist->sorted, flist->files,
1520 flist->count * sizeof (struct file_struct*));
1521 clean_flist(flist, 0);
1523 flist->sorted = flist->files;
1527 flist->sorted = flist->files;
1528 clean_flist(flist, 0);
1531 file_total += flist->count;
1532 future_cnt += flist->count;
1533 stats.flist_size += stats.total_written - start_write;
1534 stats.num_files += flist->count;
1536 output_flist(flist);
1538 dp = F_DIRNODE_P(file);
1539 if (DIR_FIRST_CHILD(dp) >= 0) {
1540 send_dir_ndx = DIR_FIRST_CHILD(dp);
1543 while (DIR_NEXT_SIBLING(dp) < 0) {
1544 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1545 write_ndx(f, NDX_FLIST_EOF);
1547 change_local_filter_dir(NULL, 0, 0);
1551 file = dir_flist->sorted[send_dir_ndx];
1552 dp = F_DIRNODE_P(file);
1554 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1559 if (io_error != save_io_error && !ignore_errors)
1560 send_msg_int(MSG_IO_ERROR, io_error);
1563 struct file_list *send_file_list(int f, int argc, char *argv[])
1565 static const char *lastdir;
1566 static int lastdir_len = -1;
1570 char lastpath[MAXPATHLEN] = "";
1571 struct file_list *flist;
1572 struct timeval start_tv, end_tv;
1575 int flags, disable_buffering;
1577 rprintf(FLOG, "building file list\n");
1578 if (show_filelist_p())
1579 start_filelist_progress("building file list");
1580 else if (inc_recurse && verbose && !am_server)
1581 rprintf(FCLIENT, "sending incremental file list\n");
1583 start_write = stats.total_written;
1584 gettimeofday(&start_tv, NULL);
1586 #ifdef SUPPORT_HARD_LINKS
1587 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1591 flist = cur_flist = flist_new(0, "send_file_list");
1593 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1594 flags = FLAG_DIVERT_DIRS;
1596 dir_flist = cur_flist;
1600 disable_buffering = io_start_buffering_out(f);
1601 if (filesfrom_fd >= 0) {
1602 if (argv[0] && !push_dir(argv[0], 0)) {
1603 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1604 full_fname(argv[0]), curr_dir);
1605 exit_cleanup(RERR_FILESELECT);
1611 char fbuf[MAXPATHLEN];
1616 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1618 sanitize_path(fbuf, fbuf, "", 0, NULL);
1622 strlcpy(fbuf, *argv++, MAXPATHLEN);
1624 sanitize_path(fbuf, fbuf, "", 0, NULL);
1628 if (relative_paths) {
1629 /* We clean up fbuf below. */
1631 } else if (!len || fbuf[len - 1] == '/') {
1632 if (len == 2 && fbuf[0] == '.') {
1633 /* Turn "./" into just "." rather than "./." */
1636 if (len + 1 >= MAXPATHLEN)
1637 overflow_exit("send_file_list");
1642 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1643 && (len == 2 || fbuf[len-3] == '/')) {
1644 if (len + 2 >= MAXPATHLEN)
1645 overflow_exit("send_file_list");
1651 is_dot_dir = fbuf[len-1] == '.'
1652 && (len == 1 || fbuf[len-2] == '/');
1657 if (!relative_paths) {
1658 p = strrchr(fbuf, '/');
1665 len -= p - fbuf + 1;
1670 if ((p = strstr(fbuf, "/./")) != NULL) {
1676 len -= p - fbuf + 3;
1680 /* Get rid of trailing "/" and "/.". */
1682 if (fn[len - 1] == '/') {
1684 if (!--len && !dir) {
1689 else if (len >= 2 && fn[len - 1] == '.'
1690 && fn[len - 2] == '/') {
1692 if (!(len -= 2) && !dir) {
1699 if (len == 1 && fn[0] == '/')
1702 /* Reject a ".." dir in the active part of the path. */
1703 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1704 if ((p[2] == '/' || p[2] == '\0')
1705 && (p == fn || p[-1] == '/')) {
1707 "found \"..\" dir in relative path: %s\n",
1709 exit_cleanup(RERR_SYNTAX);
1719 dirlen = dir ? strlen(dir) : 0;
1720 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1721 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1724 lastdir_len = pathname_len;
1725 } else if (!push_pathname(lastdir, lastdir_len)) {
1727 io_error |= IOERR_GENERAL;
1728 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1729 full_fname(dir), curr_dir);
1734 memmove(fbuf, fn, len + 1);
1736 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1737 io_error |= IOERR_GENERAL;
1738 rsyserr(FERROR, errno, "link_stat %s failed",
1743 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1744 rprintf(FINFO, "skipping directory %s\n", fbuf);
1748 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1749 /* Send the implied directories at the start of the
1750 * source spec, so we get their permissions right. */
1751 char *lp = lastpath, *slash = fbuf;
1753 /* Skip any initial directories in our path that we
1754 * have in common with lastpath. */
1755 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1760 if (fn != p || (*lp && *lp != '/')) {
1761 int save_copy_links = copy_links;
1762 int save_xfer_dirs = xfer_dirs;
1763 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1764 copy_links |= copy_unsafe_links;
1766 while ((slash = strchr(slash+1, '/')) != 0) {
1768 send_file_name(f, flist, fbuf, NULL,
1769 dir_flags, ALL_FILTERS);
1772 copy_links = save_copy_links;
1773 xfer_dirs = save_xfer_dirs;
1775 strlcpy(lastpath, fbuf, sizeof lastpath);
1780 if (one_file_system)
1781 filesystem_dev = st.st_dev;
1783 if (recurse || (xfer_dirs && is_dot_dir)) {
1784 struct file_struct *file;
1785 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags;
1786 file = send_file_name(f, flist, fbuf, &st,
1787 top_flags, ALL_FILTERS);
1788 if (file && !inc_recurse)
1789 send_if_directory(f, flist, file, fbuf, len, flags);
1791 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1794 gettimeofday(&end_tv, NULL);
1795 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1796 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1797 if (stats.flist_buildtime == 0)
1798 stats.flist_buildtime = 1;
1801 write_byte(f, 0); /* Indicate end of file list */
1803 #ifdef SUPPORT_HARD_LINKS
1804 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1808 if (show_filelist_p())
1809 finish_filelist_progress(flist);
1811 gettimeofday(&end_tv, NULL);
1812 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1813 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1815 /* When converting names, both sides keep an unsorted file-list array
1816 * because the names will differ on the sending and receiving sides
1817 * (both sides will use the unsorted index number for each item). */
1819 /* Sort the list without removing any duplicates. This allows the
1820 * receiving side to ask for whatever name it kept. For incremental
1821 * recursion mode, the sender marks duplicate dirs so that it can
1822 * send them together in a single file-list. */
1824 if (need_unsorted_flist) {
1826 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
1827 out_of_memory("send_file_list");
1828 memcpy(flist->sorted, flist->files,
1829 flist->count * sizeof (struct file_struct*));
1830 clean_flist(flist, 0);
1832 flist->sorted = flist->files;
1836 flist->sorted = flist->files;
1837 clean_flist(flist, 0);
1839 file_total += flist->count;
1841 if (!numeric_ids && !inc_recurse)
1844 /* send the io_error flag */
1845 if (protocol_version < 30)
1846 write_int(f, ignore_errors ? 0 : io_error);
1847 else if (io_error && !ignore_errors)
1848 send_msg_int(MSG_IO_ERROR, io_error);
1850 if (disable_buffering)
1851 io_end_buffering_out();
1853 stats.flist_size = stats.total_written - start_write;
1854 stats.num_files = flist->count;
1857 output_flist(flist);
1860 rprintf(FINFO, "send_file_list done\n");
1864 if (!need_unsorted_flist)
1866 dir_flist->sorted = dir_flist->files;
1867 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
1868 if (send_dir_ndx < 0) {
1869 write_ndx(f, NDX_FLIST_EOF);
1872 else if (file_total == 1) {
1873 /* If we're creating incremental file-lists and there
1874 * was just 1 item in the first file-list, send 1 more
1875 * file-list to check if this is a 1-file xfer. */
1876 send_extra_file_list(f, 1);
1883 struct file_list *recv_file_list(int f)
1885 struct file_list *flist;
1890 rprintf(FLOG, "receiving file list\n");
1891 if (show_filelist_p())
1892 start_filelist_progress("receiving file list");
1893 else if (inc_recurse && verbose && !am_server && !first_flist)
1894 rprintf(FCLIENT, "receiving incremental file list\n");
1896 start_read = stats.total_read;
1898 flist = flist_new(0, "recv_file_list");
1900 #ifdef SUPPORT_HARD_LINKS
1901 if (preserve_hard_links && protocol_version < 30)
1906 if (flist->ndx_start == 0)
1907 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1908 dstart = dir_flist->count;
1914 while ((flags = read_byte(f)) != 0) {
1915 struct file_struct *file;
1917 flist_expand(flist);
1919 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1920 flags |= read_byte(f) << 8;
1921 file = recv_file_entry(flist, flags, f);
1923 if (inc_recurse && S_ISDIR(file->mode)) {
1924 flist_expand(dir_flist);
1926 if (need_unsorted_flist)
1927 dir_flist->sorted[dir_flist->count] = file;
1929 dir_flist->files[dir_flist->count++] = file;
1932 flist->files[flist->count++] = file;
1934 maybe_emit_filelist_progress(flist->count);
1937 rprintf(FINFO, "recv_file_name(%s)\n",
1938 f_name(file, NULL));
1941 file_total += flist->count;
1944 rprintf(FINFO, "received %d names\n", flist->count);
1946 if (show_filelist_p())
1947 finish_filelist_progress(flist);
1950 if (need_unsorted_flist) {
1951 /* Create an extra array of index pointers that we can sort for
1952 * the generator's use (for wading through the files in sorted
1953 * order and for calling flist_find()). We keep the "files"
1954 * list unsorted for our exchange of index numbers with the
1955 * other side (since their names may not sort the same). */
1956 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
1957 out_of_memory("recv_file_list");
1958 memcpy(flist->sorted, flist->files,
1959 flist->count * sizeof (struct file_struct*));
1961 qsort(dir_flist->sorted + dstart, dir_flist->count - dstart,
1962 sizeof (struct file_struct*), (int (*)())file_compare);
1967 flist->sorted = flist->files;
1969 dir_flist->sorted = dir_flist->files;
1970 qsort(dir_flist->sorted + dstart, dir_flist->count - dstart,
1971 sizeof (struct file_struct*), (int (*)())file_compare);
1975 if (!inc_recurse && f >= 0)
1976 recv_id_list(f, flist);
1978 clean_flist(flist, relative_paths);
1980 if (protocol_version < 30) {
1981 /* Recv the io_error flag */
1985 io_error |= read_int(f);
1989 output_flist(flist);
1993 for (i = 0; i < flist->count; i++)
1994 list_file_entry(flist->files[i]);
1998 rprintf(FINFO, "recv_file_list done\n");
2000 stats.flist_size += stats.total_read - start_read;
2001 stats.num_files += flist->count;
2006 /* This is only used once by the receiver if the very first file-list
2007 * has exactly one item in it. */
2008 void recv_additional_file_list(int f)
2010 struct file_list *flist;
2011 int ndx = read_ndx(f);
2012 if (ndx == NDX_FLIST_EOF) {
2014 change_local_filter_dir(NULL, 0, 0);
2016 ndx = NDX_FLIST_OFFSET - ndx;
2017 if (ndx < 0 || ndx >= dir_flist->count) {
2018 ndx = NDX_FLIST_OFFSET - ndx;
2020 "Invalid dir index: %d (%d - %d)\n",
2021 ndx, NDX_FLIST_OFFSET,
2022 NDX_FLIST_OFFSET - dir_flist->count);
2023 exit_cleanup(RERR_PROTOCOL);
2025 flist = recv_file_list(f);
2026 flist->parent_ndx = ndx;
2030 /* Search for an identically-named item in the file list. Note that the
2031 * items must agree in their directory-ness, or no match is returned. */
2032 int flist_find(struct file_list *flist, struct file_struct *f)
2034 int low = flist->low, high = flist->high;
2035 int diff, mid, mid_up;
2037 while (low <= high) {
2038 mid = (low + high) / 2;
2039 if (F_IS_ACTIVE(flist->sorted[mid]))
2042 /* Scan for the next non-empty entry using the cached
2043 * distance values. If the value isn't fully up-to-
2044 * date, update it. */
2045 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2046 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2048 mid_up += F_DEPTH(flist->sorted[mid_up]);
2049 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2050 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2052 if (mid_up > high) {
2053 /* If there's nothing left above us, set high to
2054 * a non-empty entry below us and continue. */
2055 high = mid - (int)flist->sorted[mid]->len32;
2056 if (!F_IS_ACTIVE(flist->sorted[high])) {
2058 high -= (int)flist->sorted[high]->len32;
2059 } while (!F_IS_ACTIVE(flist->sorted[high]));
2060 flist->sorted[mid]->len32 = mid - high;
2065 diff = f_name_cmp(flist->sorted[mid_up], f);
2067 if (protocol_version < 29
2068 && S_ISDIR(flist->sorted[mid_up]->mode)
2069 != S_ISDIR(f->mode))
2082 * Free up any resources a file_struct has allocated
2083 * and clear the file.
2085 void clear_file(struct file_struct *file)
2087 /* The +1 zeros out the first char of the basename. */
2088 memset(file, 0, FILE_STRUCT_LEN + 1);
2089 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2090 * entry. Likewise for len32 in the opposite direction. We assume
2091 * that we're alone for now since flist_find() will adjust the counts
2092 * it runs into that aren't up-to-date. */
2093 file->len32 = F_DEPTH(file) = 1;
2096 /* Allocate a new file list. */
2097 struct file_list *flist_new(int flags, char *msg)
2099 struct file_list *flist;
2101 flist = new(struct file_list);
2105 memset(flist, 0, sizeof flist[0]);
2107 if (!(flags & FLIST_TEMP)) {
2109 flist->ndx_start = first_flist->prev->ndx_start
2110 + first_flist->prev->count;
2112 /* This is a doubly linked list with prev looping back to
2113 * the end of the list, but the last next pointer is NULL. */
2115 first_flist = cur_flist = flist->prev = flist;
2117 flist->prev = first_flist->prev;
2118 flist->prev->next = first_flist->prev = flist;
2123 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
2129 /* Free up all elements in a flist. */
2130 void flist_free(struct file_list *flist)
2133 ; /* Was FLIST_TEMP dir-list. */
2134 else if (flist == flist->prev) {
2135 first_flist = cur_flist = NULL;
2139 if (flist == cur_flist)
2140 cur_flist = flist->next;
2141 if (flist == first_flist)
2142 first_flist = first_flist->next;
2144 flist->prev->next = flist->next;
2146 flist->next = first_flist;
2148 flist->next->prev = flist->prev;
2149 file_total -= flist->count;
2153 pool_destroy(flist->file_pool);
2154 if (flist->sorted && flist->sorted != flist->files)
2155 free(flist->sorted);
2160 /* This routine ensures we don't have any duplicate names in our file list.
2161 * duplicate names can cause corruption because of the pipelining. */
2162 static void clean_flist(struct file_list *flist, int strip_root)
2164 char fbuf[MAXPATHLEN];
2169 if (flist->count == 0) {
2174 qsort(flist->sorted, flist->count,
2175 sizeof flist->sorted[0], (int (*)())file_compare);
2177 if (!am_sender || inc_recurse) {
2178 for (i = prev_i = 0; i < flist->count; i++) {
2179 if (F_IS_ACTIVE(flist->sorted[i])) {
2184 flist->low = prev_i;
2186 i = prev_i = flist->count - 1;
2190 while (++i < flist->count) {
2192 struct file_struct *file = flist->sorted[i];
2194 if (!F_IS_ACTIVE(file))
2196 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2198 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2199 int save_mode = file->mode;
2200 /* Make sure that this directory doesn't duplicate a
2201 * non-directory earlier in the list. */
2202 flist->high = prev_i;
2203 file->mode = S_IFREG;
2204 j = flist_find(flist, file);
2205 file->mode = save_mode;
2209 struct file_struct *fp = flist->sorted[j];
2211 /* If one is a dir and the other is not, we want to
2212 * keep the dir because it might have contents in the
2214 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2215 if (S_ISDIR(file->mode))
2219 } else if (protocol_version < 27)
2225 flist->sorted[drop]->flags |= FLAG_DUPLICATE;
2229 "removing duplicate name %s from file list (%d)\n",
2230 f_name(file, fbuf), drop);
2232 /* Make sure we don't lose track of a user-specified
2234 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2235 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2237 clear_file(flist->sorted[drop]);
2241 if (flist->low == drop) {
2243 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2252 flist->high = prev_i;
2255 /* We need to strip off the leading slashes for relative
2256 * paths, but this must be done _after_ the sorting phase. */
2257 for (i = flist->low; i <= flist->high; i++) {
2258 struct file_struct *file = flist->sorted[i];
2262 while (*file->dirname == '/')
2264 if (!*file->dirname)
2265 file->dirname = NULL;
2269 if (prune_empty_dirs && !am_sender) {
2270 int j, prev_depth = 0;
2272 prev_i = 0; /* It's OK that this isn't really true. */
2274 for (i = flist->low; i <= flist->high; i++) {
2275 struct file_struct *fp, *file = flist->sorted[i];
2277 /* This temporarily abuses the F_DEPTH() value for a
2278 * directory that is in a chain that might get pruned.
2279 * We restore the old value if it gets a reprieve. */
2280 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2281 /* Dump empty dirs when coming back down. */
2282 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2283 fp = flist->sorted[prev_i];
2284 if (F_DEPTH(fp) >= 0)
2286 prev_i = -F_DEPTH(fp)-1;
2289 prev_depth = F_DEPTH(file);
2290 if (is_excluded(f_name(file, fbuf), 1,
2292 /* Keep dirs through this dir. */
2293 for (j = prev_depth-1; ; j--) {
2294 fp = flist->sorted[prev_i];
2295 if (F_DEPTH(fp) >= 0)
2297 prev_i = -F_DEPTH(fp)-1;
2301 F_DEPTH(file) = -prev_i-1;
2304 /* Keep dirs through this non-dir. */
2305 for (j = prev_depth; ; j--) {
2306 fp = flist->sorted[prev_i];
2307 if (F_DEPTH(fp) >= 0)
2309 prev_i = -F_DEPTH(fp)-1;
2314 /* Dump all remaining empty dirs. */
2316 struct file_struct *fp = flist->sorted[prev_i];
2317 if (F_DEPTH(fp) >= 0)
2319 prev_i = -F_DEPTH(fp)-1;
2323 for (i = flist->low; i <= flist->high; i++) {
2324 if (F_IS_ACTIVE(flist->sorted[i]))
2328 for (i = flist->high; i >= flist->low; i--) {
2329 if (F_IS_ACTIVE(flist->sorted[i]))
2336 static void output_flist(struct file_list *flist)
2338 char uidbuf[16], gidbuf[16], depthbuf[16];
2339 struct file_struct *file;
2340 const char *root, *dir, *slash, *name, *trail;
2341 const char *who = who_am_i();
2344 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2345 who, flist->ndx_start, flist->count, flist->low, flist->high);
2346 for (i = 0; i < flist->count; i++) {
2347 file = flist->sorted[i];
2348 if ((am_root || am_sender) && preserve_uid) {
2349 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2354 static char parens[] = "(\0)\0\0\0";
2355 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2356 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2357 pp, F_GROUP(file), pp + 2);
2361 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2362 if (F_IS_ACTIVE(file)) {
2363 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2364 if ((dir = file->dirname) == NULL)
2368 name = file->basename;
2369 trail = S_ISDIR(file->mode) ? "/" : "";
2371 root = dir = slash = name = trail = "";
2372 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2373 who, i, root, dir, slash, name, trail, (int)file->mode,
2374 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
2378 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2379 enum fnc_type { t_PATH, t_ITEM };
2381 /* Compare the names of two file_struct entities, similar to how strcmp()
2382 * would do if it were operating on the joined strings.
2384 * Some differences beginning with protocol_version 29: (1) directory names
2385 * are compared with an assumed trailing slash so that they compare in a
2386 * way that would cause them to sort immediately prior to any content they
2387 * may have; (2) a directory of any name compares after a non-directory of
2388 * any name at the same depth; (3) a directory with name "." compares prior
2389 * to anything else. These changes mean that a directory and a non-dir
2390 * with the same name will not compare as equal (protocol_version >= 29).
2392 * The dirname component can be an empty string, but the basename component
2393 * cannot (and never is in the current codebase). The basename component
2394 * may be NULL (for a removed item), in which case it is considered to be
2395 * after any existing item. */
2396 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
2399 const uchar *c1, *c2;
2400 enum fnc_state state1, state2;
2401 enum fnc_type type1, type2;
2402 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2404 if (!f1 || !F_IS_ACTIVE(f1)) {
2405 if (!f2 || !F_IS_ACTIVE(f2))
2409 if (!f2 || !F_IS_ACTIVE(f2))
2412 c1 = (uchar*)f1->dirname;
2413 c2 = (uchar*)f2->dirname;
2417 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2418 c1 = (const uchar*)f1->basename;
2419 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2421 state1 = s_TRAILING;
2430 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2431 c2 = (const uchar*)f2->basename;
2432 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2434 state2 = s_TRAILING;
2444 return type1 == t_PATH ? 1 : -1;
2454 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2455 c1 = (const uchar*)f1->basename;
2456 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2458 state1 = s_TRAILING;
2464 state1 = s_TRAILING;
2465 if (type1 == t_PATH) {
2474 if (*c2 && type1 != type2)
2475 return type1 == t_PATH ? 1 : -1;
2484 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2485 c2 = (const uchar*)f2->basename;
2486 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2488 state2 = s_TRAILING;
2494 state2 = s_TRAILING;
2495 if (type2 == t_PATH) {
2507 return type1 == t_PATH ? 1 : -1;
2509 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2514 char *f_name_buf(void)
2516 static char names[5][MAXPATHLEN];
2517 static unsigned int n;
2519 n = (n + 1) % (sizeof names / sizeof names[0]);
2524 /* Return a copy of the full filename of a flist entry, using the indicated
2525 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2526 * done because we checked the size when creating the file_struct entry.
2528 char *f_name(struct file_struct *f, char *fbuf)
2530 if (!f || !F_IS_ACTIVE(f))
2534 fbuf = f_name_buf();
2537 int len = strlen(f->dirname);
2538 memcpy(fbuf, f->dirname, len);
2540 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2542 strlcpy(fbuf, f->basename, MAXPATHLEN);
2547 /* Do a non-recursive scan of the named directory, possibly ignoring all
2548 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2549 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2550 * buffer (the functions we call will append names onto the end, but the old
2551 * dir value will be restored on exit). */
2552 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2554 struct file_list *dirlist;
2555 char dirbuf[MAXPATHLEN];
2556 int save_recurse = recurse;
2557 int save_xfer_dirs = xfer_dirs;
2560 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2561 if (dlen >= MAXPATHLEN)
2566 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2570 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2571 xfer_dirs = save_xfer_dirs;
2572 recurse = save_recurse;
2574 flist_count_offset += dirlist->count;
2576 dirlist->sorted = dirlist->files;
2577 clean_flist(dirlist, 0);
2580 output_flist(dirlist);