2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2009 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int always_checksum;
37 extern int ignore_errors;
38 extern int numeric_ids;
42 extern int filesfrom_fd;
43 extern int one_file_system;
44 extern int copy_dirlinks;
45 extern int keep_dirlinks;
46 extern int preserve_uid;
47 extern int preserve_gid;
48 extern int preserve_acls;
49 extern int preserve_xattrs;
50 extern int preserve_links;
51 extern int preserve_hard_links;
52 extern int preserve_devices;
53 extern int preserve_specials;
54 extern int missing_args;
58 extern int relative_paths;
59 extern int implied_dirs;
60 extern int file_extra_cnt;
61 extern int ignore_perishable;
62 extern int non_perishable_cnt;
63 extern int prune_empty_dirs;
64 extern int copy_links;
65 extern int copy_unsafe_links;
66 extern int protocol_version;
67 extern int sanitize_paths;
68 extern int munge_symlinks;
69 extern int need_unsorted_flist;
70 extern int sender_symlink_iconv;
71 extern int output_needs_newline;
72 extern int sender_keeps_checksum;
73 extern int unsort_ndx;
74 extern struct stats stats;
75 extern char *filesfrom_host;
76 extern char *usermap, *groupmap;
78 extern char curr_dir[MAXPATHLEN];
80 extern struct chmod_mode_struct *chmod_modes;
82 extern struct filter_list_struct filter_list;
83 extern struct filter_list_struct daemon_filter_list;
84 extern struct filter_struct *last_hit_filter;
87 extern int filesfrom_convert;
88 extern iconv_t ic_send, ic_recv;
91 #define PTR_SIZE (sizeof (struct file_struct *))
95 dev_t filesystem_dev; /* used to implement -x */
97 struct file_list *cur_flist, *first_flist, *dir_flist;
98 int send_dir_ndx = -1, send_dir_depth = -1;
99 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
100 int file_total = 0; /* total of all active items over all file-lists */
101 int flist_eof = 0; /* all the file-lists are now known */
103 #define NORMAL_NAME 0
104 #define SLASH_ENDING_NAME 1
105 #define DOTDIR_NAME 2
107 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
108 * internally, even if this platform does not allow files to have 64-bit inums.
109 * The only exception is if we're on a platform with no 64-bit type at all.
111 * Because we use read_longint() to get these off the wire, if you transfer
112 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
113 * machine with no 64-bit types then you will get an overflow error.
115 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
116 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
117 * be truncated. But it's a kind of silly thing to do anyhow. */
119 /* The tmp_* vars are used as a cache area by make_file() to store data
120 * that the sender doesn't need to remember in its file list. The data
121 * will survive just long enough to be used by send_file_entry(). */
122 static dev_t tmp_rdev;
123 #ifdef SUPPORT_HARD_LINKS
124 static int64 tmp_dev, tmp_ino;
126 static char tmp_sum[MAX_DIGEST_LEN];
128 static char empty_sum[MAX_DIGEST_LEN];
129 static int flist_count_offset; /* for --delete --progress */
131 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
132 static void output_flist(struct file_list *flist);
134 void init_flist(void)
136 if (DEBUG_GTE(FLIST, 4)) {
137 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
138 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
140 checksum_len = protocol_version < 21 ? 2
141 : protocol_version < 30 ? MD4_DIGEST_LEN
145 static int show_filelist_p(void)
147 return INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
150 static void start_filelist_progress(char *kind)
152 rprintf(FCLIENT, "%s ... ", kind);
153 output_needs_newline = 1;
157 static void emit_filelist_progress(int count)
159 rprintf(FCLIENT, " %d files...\r", count);
162 static void maybe_emit_filelist_progress(int count)
164 if (INFO_GTE(FLIST, 2) && show_filelist_p() && (count % 100) == 0)
165 emit_filelist_progress(count);
168 static void finish_filelist_progress(const struct file_list *flist)
170 if (INFO_GTE(FLIST, 2)) {
171 /* This overwrites the progress line */
172 rprintf(FINFO, "%d file%sto consider\n",
173 flist->used, flist->used == 1 ? " " : "s ");
175 output_needs_newline = 0;
176 rprintf(FINFO, "done\n");
180 void show_flist_stats(void)
185 /* Stat either a symlink or its referent, depending on the settings of
186 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
188 * If path is the name of a symlink, then the linkbuf buffer (which must hold
189 * MAXPATHLEN chars) will be set to the symlink's target string.
191 * The stat structure pointed to by stp will contain information about the
192 * link or the referent as appropriate, if they exist. */
193 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
196 if (link_stat(path, stp, copy_dirlinks) < 0)
198 if (S_ISLNK(stp->st_mode)) {
199 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
202 linkbuf[llen] = '\0';
203 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
204 if (INFO_GTE(SYMSAFE, 1)) {
205 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
208 return x_stat(path, stp, NULL);
210 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
211 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
212 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
213 llen - SYMLINK_PREFIX_LEN + 1);
218 return x_stat(path, stp, NULL);
222 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
226 return x_stat(path, stp, NULL);
227 if (x_lstat(path, stp, NULL) < 0)
229 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
231 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
236 return x_stat(path, stp, NULL);
240 static inline int is_daemon_excluded(const char *fname, int is_dir)
242 if (daemon_filter_list.head
243 && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
250 static inline int path_is_daemon_excluded(char *path, int ignore_filename)
252 if (daemon_filter_list.head) {
255 while ((slash = strchr(slash+1, '/')) != NULL) {
258 ret = check_filter(&daemon_filter_list, FLOG, path, 1);
267 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
276 /* This function is used to check if a file should be included/excluded
277 * from the list of files based on its name and type etc. The value of
278 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS.
279 * "last_hit_filter" will be set to the operative filter, or NULL if none. */
280 static int is_excluded(const char *fname, int is_dir, int filter_level)
282 #if 0 /* This currently never happens, so avoid a useless compare. */
283 if (filter_level == NO_FILTERS)
286 if (is_daemon_excluded(fname, is_dir))
288 /* Don't leave a daemon include in last_hit_filter. */
289 last_hit_filter = NULL;
290 if (filter_level != ALL_FILTERS)
293 && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
298 static void send_directory(int f, struct file_list *flist,
299 char *fbuf, int len, int flags);
301 static const char *pathname, *orig_dir;
302 static int pathname_len;
304 /* Make sure flist can hold at least flist->used + extra entries. */
305 static void flist_expand(struct file_list *flist, int extra)
307 struct file_struct **new_ptr;
309 if (flist->used + extra <= flist->malloced)
312 if (flist->malloced < FLIST_START)
313 flist->malloced = FLIST_START;
314 else if (flist->malloced >= FLIST_LINEAR)
315 flist->malloced += FLIST_LINEAR;
317 flist->malloced *= 2;
319 /* In case count jumped or we are starting the list
320 * with a known size just set it. */
321 if (flist->malloced < flist->used + extra)
322 flist->malloced = flist->used + extra;
324 new_ptr = realloc_array(flist->files, struct file_struct *,
327 if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
328 rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
330 big_num(sizeof flist->files[0] * flist->malloced),
331 (new_ptr == flist->files) ? " not" : "");
334 flist->files = new_ptr;
337 out_of_memory("flist_expand");
340 static void flist_done_allocating(struct file_list *flist)
342 void *ptr = pool_boundary(flist->file_pool, 8*1024);
343 if (flist->pool_boundary == ptr)
344 flist->pool_boundary = NULL; /* list didn't use any pool memory */
346 flist->pool_boundary = ptr;
349 /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
350 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
351 * with dir == NULL taken to be the starting directory, and dirlen < 0
352 * indicating that strdup(dir) should be called and then the -dirlen length
353 * value checked to ensure that it is not daemon-excluded. */
354 int change_pathname(struct file_struct *file, const char *dir, int dirlen)
357 char *cpy = strdup(dir);
359 change_dir(orig_dir, CD_SKIP_CHDIR);
360 if (path_is_daemon_excluded(cpy, 0))
366 if (pathname == F_PATHNAME(file))
368 dir = F_PATHNAME(file);
370 dirlen = strlen(dir);
371 } else if (pathname == dir)
373 if (dir && *dir != '/')
374 change_dir(orig_dir, CD_SKIP_CHDIR);
378 pathname_len = dirlen;
383 if (!change_dir(dir, CD_NORMAL)) {
385 io_error |= IOERR_GENERAL;
386 rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
388 change_dir(orig_dir, CD_NORMAL);
397 static void send_file_entry(int f, const char *fname, struct file_struct *file,
399 const char *symlink_name, int symlink_len,
401 int ndx, int first_ndx)
403 static time_t modtime;
405 #ifdef SUPPORT_HARD_LINKS
409 static uint32 rdev_major;
412 static const char *user_name, *group_name;
413 static char lastname[MAXPATHLEN];
414 #ifdef SUPPORT_HARD_LINKS
415 int first_hlink_ndx = -1;
420 /* Initialize starting value of xflags and adjust counts. */
421 if (S_ISREG(file->mode))
423 else if (S_ISDIR(file->mode)) {
425 if (protocol_version >= 30) {
426 if (file->flags & FLAG_CONTENT_DIR)
427 xflags = file->flags & FLAG_TOP_DIR;
428 else if (file->flags & FLAG_IMPLIED_DIR)
429 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
431 xflags = XMIT_NO_CONTENT_DIR;
433 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
435 if (S_ISLNK(file->mode))
436 stats.num_symlinks++;
437 else if (IS_DEVICE(file->mode))
439 else if (IS_SPECIAL(file->mode))
440 stats.num_specials++;
444 if (file->mode == mode)
445 xflags |= XMIT_SAME_MODE;
449 if (preserve_devices && IS_DEVICE(mode)) {
450 if (protocol_version < 28) {
451 if (tmp_rdev == rdev)
452 xflags |= XMIT_SAME_RDEV_pre28;
457 if ((uint32)major(rdev) == rdev_major)
458 xflags |= XMIT_SAME_RDEV_MAJOR;
460 rdev_major = major(rdev);
461 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
462 xflags |= XMIT_RDEV_MINOR_8_pre30;
464 } else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) {
465 /* Special files don't need an rdev number, so just make
466 * the historical transmission of the value efficient. */
467 if (protocol_version < 28)
468 xflags |= XMIT_SAME_RDEV_pre28;
470 rdev = MAKEDEV(major(rdev), 0);
471 xflags |= XMIT_SAME_RDEV_MAJOR;
472 if (protocol_version < 30)
473 xflags |= XMIT_RDEV_MINOR_8_pre30;
475 } else if (protocol_version < 28)
476 rdev = MAKEDEV(0, 0);
477 if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
478 xflags |= XMIT_SAME_UID;
482 user_name = add_uid(uid);
483 if (inc_recurse && user_name)
484 xflags |= XMIT_USER_NAME_FOLLOWS;
487 if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
488 xflags |= XMIT_SAME_GID;
492 group_name = add_gid(gid);
493 if (inc_recurse && group_name)
494 xflags |= XMIT_GROUP_NAME_FOLLOWS;
497 if (file->modtime == modtime)
498 xflags |= XMIT_SAME_TIME;
500 modtime = file->modtime;
502 #ifdef SUPPORT_HARD_LINKS
504 if (protocol_version >= 30) {
505 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
506 first_hlink_ndx = (int32)(long)np->data - 1;
507 if (first_hlink_ndx < 0) {
508 np->data = (void*)(long)(first_ndx + ndx + 1);
509 xflags |= XMIT_HLINK_FIRST;
511 if (DEBUG_GTE(HLINK, 1)) {
512 if (first_hlink_ndx >= 0) {
513 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
514 who_am_i(), first_ndx + ndx, first_hlink_ndx,
515 first_hlink_ndx >= first_ndx ? "" : "un");
516 } else if (DEBUG_GTE(HLINK, 3)) {
517 rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
518 who_am_i(), first_ndx + ndx,
519 big_num(tmp_dev), big_num(tmp_ino));
523 if (tmp_dev == dev) {
524 if (protocol_version >= 28)
525 xflags |= XMIT_SAME_DEV_pre30;
529 xflags |= XMIT_HLINKED;
534 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
536 l2 = strlen(fname+l1);
539 xflags |= XMIT_SAME_NAME;
541 xflags |= XMIT_LONG_NAME;
543 /* We must make sure we don't send a zero flag byte or the
544 * other end will terminate the flist transfer. Note that
545 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
546 * it's harmless way to add a bit to the first flag byte. */
547 if (protocol_version >= 28) {
548 if (!xflags && !S_ISDIR(mode))
549 xflags |= XMIT_TOP_DIR;
550 if ((xflags & 0xFF00) || !xflags) {
551 xflags |= XMIT_EXTENDED_FLAGS;
552 write_shortint(f, xflags);
554 write_byte(f, xflags);
556 if (!(xflags & 0xFF))
557 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
558 write_byte(f, xflags);
560 if (xflags & XMIT_SAME_NAME)
562 if (xflags & XMIT_LONG_NAME)
563 write_varint30(f, l2);
566 write_buf(f, fname + l1, l2);
568 #ifdef SUPPORT_HARD_LINKS
569 if (first_hlink_ndx >= 0) {
570 write_varint(f, first_hlink_ndx);
571 if (first_hlink_ndx >= first_ndx)
576 write_varlong30(f, F_LENGTH(file), 3);
577 if (!(xflags & XMIT_SAME_TIME)) {
578 if (protocol_version >= 30)
579 write_varlong(f, modtime, 4);
581 write_int(f, modtime);
583 if (!(xflags & XMIT_SAME_MODE))
584 write_int(f, to_wire_mode(mode));
585 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
586 if (protocol_version < 30)
589 write_varint(f, uid);
590 if (xflags & XMIT_USER_NAME_FOLLOWS) {
591 int len = strlen(user_name);
593 write_buf(f, user_name, len);
597 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
598 if (protocol_version < 30)
601 write_varint(f, gid);
602 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
603 int len = strlen(group_name);
605 write_buf(f, group_name, len);
609 if ((preserve_devices && IS_DEVICE(mode))
610 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
611 if (protocol_version < 28) {
612 if (!(xflags & XMIT_SAME_RDEV_pre28))
613 write_int(f, (int)rdev);
615 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
616 write_varint30(f, major(rdev));
617 if (protocol_version >= 30)
618 write_varint(f, minor(rdev));
619 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
620 write_byte(f, minor(rdev));
622 write_int(f, minor(rdev));
628 write_varint30(f, symlink_len);
629 write_buf(f, symlink_name, symlink_len);
633 #ifdef SUPPORT_HARD_LINKS
634 if (tmp_dev != 0 && protocol_version < 30) {
635 if (protocol_version < 26) {
636 /* 32-bit dev_t and ino_t */
637 write_int(f, (int32)dev);
638 write_int(f, (int32)tmp_ino);
640 /* 64-bit dev_t and ino_t */
641 if (!(xflags & XMIT_SAME_DEV_pre30))
642 write_longint(f, dev);
643 write_longint(f, tmp_ino);
648 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
653 /* Prior to 28, we sent a useless set of nulls. */
656 write_buf(f, sum, checksum_len);
659 #ifdef SUPPORT_HARD_LINKS
662 strlcpy(lastname, fname, MAXPATHLEN);
664 if (S_ISREG(mode) || S_ISLNK(mode))
665 stats.total_size += F_LENGTH(file);
668 static struct file_struct *recv_file_entry(struct file_list *flist,
671 static int64 modtime;
673 #ifdef SUPPORT_HARD_LINKS
677 static uint32 rdev_major;
680 static uint16 gid_flags;
681 static char lastname[MAXPATHLEN], *lastdir;
682 static int lastdir_depth, lastdir_len = -1;
683 static unsigned int del_hier_name_len = 0;
684 static int in_del_hier = 0;
685 char thisname[MAXPATHLEN];
686 unsigned int l1 = 0, l2 = 0;
687 int alloc_len, basename_len, linkname_len;
688 int extra_len = file_extra_cnt * EXTRA_LEN;
689 int first_hlink_ndx = -1;
691 const char *basename;
692 struct file_struct *file;
696 if (xflags & XMIT_SAME_NAME)
699 if (xflags & XMIT_LONG_NAME)
700 l2 = read_varint30(f);
704 if (l2 >= MAXPATHLEN - l1) {
706 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
707 xflags, l1, l2, lastname, who_am_i());
708 overflow_exit("recv_file_entry");
711 strlcpy(thisname, lastname, l1 + 1);
712 read_sbuf(f, &thisname[l1], l2);
713 thisname[l1 + l2] = 0;
715 /* Abuse basename_len for a moment... */
716 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
719 if (ic_recv != (iconv_t)-1) {
722 INIT_CONST_XBUF(outbuf, thisname);
723 INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
725 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
726 io_error |= IOERR_GENERAL;
728 "[%s] cannot convert filename: %s (%s)\n",
729 who_am_i(), lastname, strerror(errno));
732 thisname[outbuf.len] = '\0';
737 clean_fname(thisname, 0);
740 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
742 if ((basename = strrchr(thisname, '/')) != NULL) {
743 int len = basename++ - thisname;
744 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
745 lastdir = new_array(char, len + 1);
746 memcpy(lastdir, thisname, len);
749 lastdir_depth = count_dir_elements(lastdir);
753 basename_len = strlen(basename) + 1; /* count the '\0' */
755 #ifdef SUPPORT_HARD_LINKS
756 if (protocol_version >= 30
757 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
758 first_hlink_ndx = read_varint(f);
759 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
761 "hard-link reference out of range: %d (%d)\n",
762 first_hlink_ndx, flist->ndx_start + flist->used);
763 exit_cleanup(RERR_PROTOCOL);
765 if (DEBUG_GTE(HLINK, 1)) {
766 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
767 who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
768 first_hlink_ndx >= flist->ndx_start ? "" : "un");
770 if (first_hlink_ndx >= flist->ndx_start) {
771 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
772 file_length = F_LENGTH(first);
773 modtime = first->modtime;
776 uid = F_OWNER(first);
778 gid = F_GROUP(first);
779 if (preserve_devices && IS_DEVICE(mode)) {
780 uint32 *devp = F_RDEV_P(first);
781 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
782 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
784 if (preserve_links && S_ISLNK(mode))
785 linkname_len = strlen(F_SYMLINK(first)) + 1;
793 file_length = read_varlong30(f, 3);
794 if (!(xflags & XMIT_SAME_TIME)) {
795 if (protocol_version >= 30) {
796 modtime = read_varlong(f, 4);
797 #if SIZEOF_TIME_T < SIZEOF_INT64
798 if (!am_generator && (int64)(time_t)modtime != modtime) {
800 "Time value of %s truncated on receiver.\n",
805 modtime = read_int(f);
807 if (!(xflags & XMIT_SAME_MODE))
808 mode = from_wire_mode(read_int(f));
810 if (chmod_modes && !S_ISLNK(mode) && mode)
811 mode = tweak_mode(mode, chmod_modes);
813 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
814 if (protocol_version < 30)
815 uid = (uid_t)read_int(f);
817 uid = (uid_t)read_varint(f);
818 if (xflags & XMIT_USER_NAME_FOLLOWS)
819 uid = recv_user_name(f, uid);
820 else if (inc_recurse && am_root && (!numeric_ids || usermap))
821 uid = match_uid(uid);
824 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
825 if (protocol_version < 30)
826 gid = (gid_t)read_int(f);
828 gid = (gid_t)read_varint(f);
830 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
831 gid = recv_group_name(f, gid, &gid_flags);
832 else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
833 gid = match_gid(gid, &gid_flags);
837 if ((preserve_devices && IS_DEVICE(mode))
838 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
839 if (protocol_version < 28) {
840 if (!(xflags & XMIT_SAME_RDEV_pre28))
841 rdev = (dev_t)read_int(f);
844 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
845 rdev_major = read_varint30(f);
846 if (protocol_version >= 30)
847 rdev_minor = read_varint(f);
848 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
849 rdev_minor = read_byte(f);
851 rdev_minor = read_int(f);
852 rdev = MAKEDEV(rdev_major, rdev_minor);
855 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
857 } else if (protocol_version < 28)
858 rdev = MAKEDEV(0, 0);
861 if (preserve_links && S_ISLNK(mode)) {
862 linkname_len = read_varint30(f) + 1; /* count the '\0' */
863 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
864 rprintf(FERROR, "overflow: linkname_len=%d\n",
866 overflow_exit("recv_file_entry");
869 /* We don't know how much extra room we need to convert
870 * the as-yet-unread symlink data, so let's hope that a
871 * double-size buffer is plenty. */
872 if (sender_symlink_iconv)
876 linkname_len += SYMLINK_PREFIX_LEN;
882 #ifdef SUPPORT_HARD_LINKS
884 if (preserve_hard_links) {
885 if (protocol_version < 28 && S_ISREG(mode))
886 xflags |= XMIT_HLINKED;
887 if (xflags & XMIT_HLINKED)
888 extra_len += (inc_recurse+1) * EXTRA_LEN;
893 /* Directories need an extra int32 for the default ACL. */
894 if (preserve_acls && S_ISDIR(mode))
895 extra_len += EXTRA_LEN;
898 if (always_checksum && S_ISREG(mode))
899 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
901 #if SIZEOF_INT64 >= 8
902 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
903 extra_len += EXTRA_LEN;
905 if (file_length < 0) {
906 rprintf(FERROR, "Offset underflow: file-length is negative\n");
907 exit_cleanup(RERR_UNSUPPORTED);
910 if (inc_recurse && S_ISDIR(mode)) {
911 if (one_file_system) {
912 /* Room to save the dir's device for -x */
913 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
915 pool = dir_flist->file_pool;
917 pool = flist->file_pool;
919 #if EXTRA_ROUNDING > 0
920 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
921 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
924 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
926 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
928 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
930 file = (struct file_struct *)bp;
931 bp += FILE_STRUCT_LEN;
933 memcpy(bp, basename, basename_len);
935 #ifdef SUPPORT_HARD_LINKS
936 if (xflags & XMIT_HLINKED)
937 file->flags |= FLAG_HLINKED;
939 file->modtime = (time_t)modtime;
940 file->len32 = (uint32)file_length;
941 #if SIZEOF_INT64 >= 8
942 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
943 #if SIZEOF_CAPITAL_OFF_T < 8
944 rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
945 exit_cleanup(RERR_UNSUPPORTED);
947 file->flags |= FLAG_LENGTH64;
948 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
957 file->flags |= gid_flags;
960 F_NDX(file) = flist->used + flist->ndx_start;
962 if (basename != thisname) {
963 file->dirname = lastdir;
964 F_DEPTH(file) = lastdir_depth + 1;
969 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
971 if (protocol_version >= 30) {
972 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
973 if (xflags & XMIT_TOP_DIR)
974 file->flags |= FLAG_TOP_DIR;
975 file->flags |= FLAG_CONTENT_DIR;
976 } else if (xflags & XMIT_TOP_DIR)
977 file->flags |= FLAG_IMPLIED_DIR;
978 } else if (xflags & XMIT_TOP_DIR) {
979 in_del_hier = recurse;
980 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
981 if (relative_paths && del_hier_name_len > 2
982 && lastname[del_hier_name_len-1] == '.'
983 && lastname[del_hier_name_len-2] == '/')
984 del_hier_name_len -= 2;
985 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
986 } else if (in_del_hier) {
987 if (!relative_paths || !del_hier_name_len
988 || (l1 >= del_hier_name_len
989 && lastname[del_hier_name_len] == '/'))
990 file->flags |= FLAG_CONTENT_DIR;
996 if (preserve_devices && IS_DEVICE(mode)) {
997 uint32 *devp = F_RDEV_P(file);
998 DEV_MAJOR(devp) = major(rdev);
999 DEV_MINOR(devp) = minor(rdev);
1002 #ifdef SUPPORT_LINKS
1005 if (first_hlink_ndx >= flist->ndx_start) {
1006 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1007 memcpy(bp, F_SYMLINK(first), linkname_len);
1009 if (munge_symlinks) {
1010 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
1011 bp += SYMLINK_PREFIX_LEN;
1012 linkname_len -= SYMLINK_PREFIX_LEN;
1015 if (sender_symlink_iconv) {
1018 alloc_len = linkname_len;
1021 /* Read the symlink data into the end of our double-sized
1022 * buffer and then convert it into the right spot. */
1023 INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
1024 linkname_len - 1, (size_t)-1);
1025 read_sbuf(f, inbuf.buf, inbuf.len);
1026 INIT_XBUF(outbuf, bp, 0, alloc_len);
1028 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
1029 io_error |= IOERR_GENERAL;
1030 rprintf(FERROR_XFER,
1031 "[%s] cannot convert symlink data for: %s (%s)\n",
1032 who_am_i(), full_fname(thisname), strerror(errno));
1033 bp = (char*)file->basename;
1037 bp[outbuf.len] = '\0';
1040 read_sbuf(f, bp, linkname_len - 1);
1041 if (sanitize_paths && !munge_symlinks && *bp)
1042 sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
1047 #ifdef SUPPORT_HARD_LINKS
1048 if (preserve_hard_links && xflags & XMIT_HLINKED) {
1049 if (protocol_version >= 30) {
1050 if (xflags & XMIT_HLINK_FIRST) {
1051 F_HL_GNUM(file) = flist->ndx_start + flist->used;
1053 F_HL_GNUM(file) = first_hlink_ndx;
1055 static int32 cnt = 0;
1056 struct ht_int64_node *np;
1059 if (protocol_version < 26) {
1063 if (!(xflags & XMIT_SAME_DEV_pre30))
1064 dev = read_longint(f);
1065 ino = read_longint(f);
1067 np = idev_find(dev, ino);
1068 ndx = (int32)(long)np->data - 1;
1071 np->data = (void*)(long)cnt;
1073 F_HL_GNUM(file) = ndx;
1078 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
1082 /* Prior to 28, we get a useless set of nulls. */
1085 if (first_hlink_ndx >= flist->ndx_start) {
1086 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1087 memcpy(bp, F_SUM(first), checksum_len);
1089 read_buf(f, bp, checksum_len);
1093 if (preserve_acls && !S_ISLNK(mode))
1094 receive_acl(file, f);
1096 #ifdef SUPPORT_XATTRS
1097 if (preserve_xattrs)
1098 receive_xattr(file, f );
1101 if (S_ISREG(mode) || S_ISLNK(mode))
1102 stats.total_size += file_length;
1107 /* Create a file_struct for a named file by reading its stat() information
1108 * and performing extensive checks against global options.
1110 * Returns a pointer to the new file struct, or NULL if there was an error
1111 * or this file should be excluded.
1113 * Note: Any error (here or in send_file_name) that results in the omission of
1114 * an existent source file from the file list should set
1115 * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1116 * destination if --delete is on. */
1117 struct file_struct *make_file(const char *fname, struct file_list *flist,
1118 STRUCT_STAT *stp, int flags, int filter_level)
1120 static char *lastdir;
1121 static int lastdir_len = -1;
1122 struct file_struct *file;
1123 char thisname[MAXPATHLEN];
1124 char linkname[MAXPATHLEN];
1125 int alloc_len, basename_len, linkname_len;
1126 int extra_len = file_extra_cnt * EXTRA_LEN;
1127 const char *basename;
1132 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1133 io_error |= IOERR_GENERAL;
1134 rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
1137 clean_fname(thisname, 0);
1139 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
1141 if (stp && (S_ISDIR(stp->st_mode) || stp->st_mode == 0)) {
1142 /* This is needed to handle a "symlink/." with a --relative
1143 * dir, or a request to delete a specific file. */
1145 *linkname = '\0'; /* make IBM code checker happy */
1146 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1147 int save_errno = errno;
1148 /* See if file is excluded before reporting an error. */
1149 if (filter_level != NO_FILTERS && filter_level != ALL_FILTERS_NO_EXCLUDE
1150 && (is_excluded(thisname, 0, filter_level)
1151 || is_excluded(thisname, 1, filter_level))) {
1152 if (ignore_perishable && save_errno != ENOENT)
1153 non_perishable_cnt++;
1156 if (save_errno == ENOENT) {
1157 #ifdef SUPPORT_LINKS
1158 /* When our options tell us to follow a symlink that
1159 * points nowhere, tell the user about the symlink
1160 * instead of giving a "vanished" message. We only
1161 * dereference a symlink if one of the --copy*links
1162 * options was specified, so there's no need for the
1163 * extra lstat() if one of these options isn't on. */
1164 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1165 && x_lstat(thisname, &st, NULL) == 0
1166 && S_ISLNK(st.st_mode)) {
1167 io_error |= IOERR_GENERAL;
1168 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1169 full_fname(thisname));
1173 enum logcode c = am_daemon && protocol_version < 28
1174 ? FERROR : FWARNING;
1175 io_error |= IOERR_VANISHED;
1176 rprintf(c, "file has vanished: %s\n",
1177 full_fname(thisname));
1180 io_error |= IOERR_GENERAL;
1181 rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
1182 full_fname(thisname));
1185 } else if (st.st_mode == 0) {
1186 io_error |= IOERR_GENERAL;
1187 rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n",
1188 full_fname(thisname));
1192 if (filter_level == NO_FILTERS)
1194 if (filter_level == ALL_FILTERS_NO_EXCLUDE) {
1195 /* Call only for the side effect of setting last_hit_filter to
1196 * any operative include filter, which might affect attributes. */
1197 is_excluded(thisname, S_ISDIR(st.st_mode) != 0, ALL_FILTERS);
1201 if (S_ISDIR(st.st_mode)) {
1203 rprintf(FINFO, "skipping directory %s\n", thisname);
1206 /* -x only affects dirs because we need to avoid recursing
1207 * into a mount-point directory, not to avoid copying a
1208 * symlinked file if -L (or similar) was specified. */
1209 if (one_file_system && st.st_dev != filesystem_dev
1210 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1211 if (one_file_system > 1) {
1212 if (INFO_GTE(MOUNT, 1)) {
1214 "[%s] skipping mount-point dir %s\n",
1215 who_am_i(), thisname);
1219 flags |= FLAG_MOUNT_DIR;
1220 flags &= ~FLAG_CONTENT_DIR;
1223 flags &= ~FLAG_CONTENT_DIR;
1225 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1226 if (ignore_perishable)
1227 non_perishable_cnt++;
1231 if (lp_ignore_nonreadable(module_id)) {
1232 #ifdef SUPPORT_LINKS
1233 if (!S_ISLNK(st.st_mode))
1235 if (access(thisname, R_OK) != 0)
1241 /* Only divert a directory in the main transfer. */
1243 if (flist->prev && S_ISDIR(st.st_mode)
1244 && flags & FLAG_DIVERT_DIRS) {
1245 /* Room for parent/sibling/next-child info. */
1246 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1248 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1249 pool = dir_flist->file_pool;
1251 pool = flist->file_pool;
1254 /* Directories need an extra int32 for the default ACL. */
1255 if (preserve_acls && S_ISDIR(st.st_mode))
1256 extra_len += EXTRA_LEN;
1261 if (DEBUG_GTE(FLIST, 2)) {
1262 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1263 who_am_i(), thisname, filter_level);
1266 if ((basename = strrchr(thisname, '/')) != NULL) {
1267 int len = basename++ - thisname;
1268 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1269 lastdir = new_array(char, len + 1);
1270 memcpy(lastdir, thisname, len);
1271 lastdir[len] = '\0';
1275 basename = thisname;
1276 basename_len = strlen(basename) + 1; /* count the '\0' */
1278 #ifdef SUPPORT_LINKS
1279 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1284 #if SIZEOF_CAPITAL_OFF_T >= 8
1285 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1286 extra_len += EXTRA_LEN;
1289 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1290 file_checksum(thisname, tmp_sum, st.st_size);
1291 if (sender_keeps_checksum)
1292 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1295 #if EXTRA_ROUNDING > 0
1296 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1297 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1300 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1303 bp = pool_alloc(pool, alloc_len, "make_file");
1305 if (!(bp = new_array(char, alloc_len)))
1306 out_of_memory("make_file");
1309 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1311 file = (struct file_struct *)bp;
1312 bp += FILE_STRUCT_LEN;
1314 memcpy(bp, basename, basename_len);
1316 #ifdef SUPPORT_HARD_LINKS
1317 if (preserve_hard_links && flist && flist->prev) {
1318 if (protocol_version >= 28
1319 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1320 : S_ISREG(st.st_mode)) {
1321 tmp_dev = (int64)st.st_dev + 1;
1322 tmp_ino = (int64)st.st_ino;
1328 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1329 if (IS_DEVICE(st.st_mode)) {
1330 tmp_rdev = st.st_rdev;
1332 } else if (IS_SPECIAL(st.st_mode))
1336 file->flags = flags;
1337 file->modtime = st.st_mtime;
1338 file->len32 = (uint32)st.st_size;
1339 #if SIZEOF_CAPITAL_OFF_T >= 8
1340 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1341 file->flags |= FLAG_LENGTH64;
1342 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1345 file->mode = st.st_mode;
1346 if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
1347 F_OWNER(file) = st.st_uid;
1348 if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
1349 F_GROUP(file) = st.st_gid;
1351 if (basename != thisname)
1352 file->dirname = lastdir;
1354 #ifdef SUPPORT_LINKS
1356 memcpy(bp + basename_len, linkname, linkname_len);
1360 F_PATHNAME(file) = pathname;
1362 F_DEPTH(file) = extra_len / EXTRA_LEN;
1364 if (basename_len == 0+1) {
1370 if (sender_keeps_checksum && S_ISREG(st.st_mode))
1371 memcpy(F_SUM(file), tmp_sum, checksum_len);
1374 F_NDX(file) = stats.num_dirs;
1379 /* Only called for temporary file_struct entries created by make_file(). */
1380 void unmake_file(struct file_struct *file)
1382 free(REQ_EXTRA(file, F_DEPTH(file)));
1385 static struct file_struct *send_file_name(int f, struct file_list *flist,
1386 const char *fname, STRUCT_STAT *stp,
1387 int flags, int filter_level)
1389 struct file_struct *file;
1390 BOOL can_tweak_mode;
1392 file = make_file(fname, flist, stp, flags, filter_level);
1396 can_tweak_mode = !S_ISLNK(file->mode) && file->mode;
1397 if ((filter_level == ALL_FILTERS || filter_level == ALL_FILTERS_NO_EXCLUDE)
1398 && last_hit_filter) {
1399 if ((last_hit_filter->flags & MATCHFLG_CHMOD) && can_tweak_mode)
1400 file->mode = tweak_mode(file->mode, last_hit_filter->chmod->modes);
1401 if ((last_hit_filter->flags & MATCHFLG_FORCE_OWNER) && uid_ndx)
1402 F_OWNER(file) = last_hit_filter->force_uid;
1403 if ((last_hit_filter->flags & MATCHFLG_FORCE_GROUP) && gid_ndx)
1404 F_GROUP(file) = last_hit_filter->force_gid;
1406 if (chmod_modes && can_tweak_mode)
1407 file->mode = tweak_mode(file->mode, chmod_modes);
1410 char fbuf[MAXPATHLEN];
1411 #ifdef SUPPORT_LINKS
1412 const char *symlink_name;
1415 char symlink_buf[MAXPATHLEN];
1418 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1423 #ifdef SUPPORT_LINKS
1424 if (preserve_links && S_ISLNK(file->mode)) {
1425 symlink_name = F_SYMLINK(file);
1426 symlink_len = strlen(symlink_name);
1427 if (symlink_len == 0) {
1428 io_error |= IOERR_GENERAL;
1430 rprintf(FERROR_XFER,
1431 "skipping symlink with 0-length value: %s\n",
1436 symlink_name = NULL;
1442 if (ic_send != (iconv_t)-1) {
1445 INIT_CONST_XBUF(outbuf, fbuf);
1447 if (file->dirname) {
1448 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1449 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1450 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
1453 fbuf[outbuf.len++] = '/';
1456 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1457 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1459 io_error |= IOERR_GENERAL;
1460 rprintf(FERROR_XFER,
1461 "[%s] cannot convert filename: %s (%s)\n",
1462 who_am_i(), f_name(file, fbuf), strerror(errno));
1465 fbuf[outbuf.len] = '\0';
1467 #ifdef SUPPORT_LINKS
1468 if (symlink_len && sender_symlink_iconv) {
1469 INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1470 INIT_CONST_XBUF(outbuf, symlink_buf);
1471 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1472 io_error |= IOERR_GENERAL;
1474 rprintf(FERROR_XFER,
1475 "[%s] cannot convert symlink data for: %s (%s)\n",
1476 who_am_i(), full_fname(fbuf), strerror(errno));
1479 symlink_buf[outbuf.len] = '\0';
1481 symlink_name = symlink_buf;
1482 symlink_len = outbuf.len;
1490 if (preserve_acls && !S_ISLNK(file->mode)) {
1491 sx.st.st_mode = file->mode;
1492 if (get_acl(fname, &sx) < 0) {
1493 io_error |= IOERR_GENERAL;
1498 #ifdef SUPPORT_XATTRS
1499 if (preserve_xattrs) {
1500 if (get_xattr(fname, &sx) < 0) {
1501 io_error |= IOERR_GENERAL;
1507 send_file_entry(f, fbuf, file,
1508 #ifdef SUPPORT_LINKS
1509 symlink_name, symlink_len,
1511 flist->used, flist->ndx_start);
1514 if (preserve_acls && !S_ISLNK(file->mode)) {
1519 #ifdef SUPPORT_XATTRS
1520 if (preserve_xattrs) {
1521 F_XATTR(file) = send_xattr(&sx, f);
1527 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1529 flist_expand(flist, 1);
1530 flist->files[flist->used++] = file;
1535 static void send_if_directory(int f, struct file_list *flist,
1536 struct file_struct *file,
1537 char *fbuf, unsigned int ol,
1540 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1542 if (S_ISDIR(file->mode)
1543 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1545 unsigned int len = strlen(fbuf);
1546 if (len > 1 && fbuf[len-1] == '/')
1548 if (len >= MAXPATHLEN - 1) {
1549 io_error |= IOERR_GENERAL;
1550 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1554 save_filters = push_local_filters(fbuf, len);
1555 send_directory(f, flist, fbuf, len, flags);
1556 pop_local_filters(save_filters);
1563 static int file_compare(const void *file1, const void *file2)
1565 return f_name_cmp(*(struct file_struct **)file1,
1566 *(struct file_struct **)file2);
1569 /* The guts of a merge-sort algorithm. This was derived from the glibc
1570 * version, but I (Wayne) changed the merge code to do less copying and
1571 * to require only half the amount of temporary memory. */
1572 static void fsort_tmp(struct file_struct **fp, size_t num,
1573 struct file_struct **tmp)
1575 struct file_struct **f1, **f2, **t;
1584 fsort_tmp(f1, n1, tmp);
1586 fsort_tmp(f2, n2, tmp);
1588 while (f_name_cmp(*f1, *f2) <= 0) {
1595 memcpy(t, f1, n1 * PTR_SIZE);
1597 *f1++ = *f2++, n2--;
1599 while (n1 > 0 && n2 > 0) {
1600 if (f_name_cmp(*t, *f2) <= 0)
1603 *f1++ = *f2++, n2--;
1607 memcpy(f1, t, n1 * PTR_SIZE);
1610 /* This file-struct sorting routine makes sure that any identical names in
1611 * the file list stay in the same order as they were in the original list.
1612 * This is particularly vital in inc_recurse mode where we expect a sort
1613 * on the flist to match the exact order of a sort on the dir_flist. */
1614 static void fsort(struct file_struct **fp, size_t num)
1620 qsort(fp, num, PTR_SIZE, file_compare);
1622 struct file_struct **tmp = new_array(struct file_struct *,
1624 fsort_tmp(fp, num, tmp);
1629 /* We take an entire set of sibling dirs from the sorted flist and link them
1630 * into the tree, setting the appropriate parent/child/sibling pointers. */
1631 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1636 int32 *parent_dp = parent_ndx < 0 ? NULL
1637 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1639 flist_expand(dir_flist, dir_cnt);
1640 dir_flist->sorted = dir_flist->files;
1642 for (i = 0; dir_cnt; i++) {
1643 struct file_struct *file = from_flist->sorted[i];
1645 if (!S_ISDIR(file->mode))
1648 dir_flist->files[dir_flist->used++] = file;
1651 if (file->basename[0] == '.' && file->basename[1] == '\0')
1655 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1657 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1659 send_dir_ndx = dir_flist->used - 1;
1661 dp = F_DIR_NODE_P(file);
1662 DIR_PARENT(dp) = parent_ndx;
1663 DIR_FIRST_CHILD(dp) = -1;
1666 DIR_NEXT_SIBLING(dp) = -1;
1669 static void interpret_stat_error(const char *fname, int is_dir)
1671 if (errno == ENOENT) {
1672 io_error |= IOERR_VANISHED;
1673 rprintf(FWARNING, "%s has vanished: %s\n",
1674 is_dir ? "directory" : "file", full_fname(fname));
1676 io_error |= IOERR_GENERAL;
1677 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1682 /* This function is normally called by the sender, but the receiving side also
1683 * calls it from get_dirlist() with f set to -1 so that we just construct the
1684 * file list in memory without sending it over the wire. Also, get_dirlist()
1685 * might call this with f set to -2, which also indicates that local filter
1686 * rules should be ignored. */
1687 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1694 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1695 int start = flist->used;
1696 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1698 assert(flist != NULL);
1700 if (!(d = opendir(fbuf))) {
1701 if (errno == ENOENT) {
1702 if (am_sender) /* Can abuse this for vanished error w/ENOENT: */
1703 interpret_stat_error(fbuf, True);
1706 io_error |= IOERR_GENERAL;
1707 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1712 if (len != 1 || *fbuf != '/')
1715 remainder = MAXPATHLEN - (p - fbuf);
1717 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1718 char *dname = d_name(di);
1719 if (dname[0] == '.' && (dname[1] == '\0'
1720 || (dname[1] == '.' && dname[2] == '\0')))
1722 if (strlcpy(p, dname, remainder) >= remainder) {
1723 io_error |= IOERR_GENERAL;
1724 rprintf(FERROR_XFER,
1725 "cannot send long-named file %s\n",
1729 if (dname[0] == '\0') {
1730 io_error |= IOERR_GENERAL;
1731 rprintf(FERROR_XFER,
1732 "cannot send file with empty name in %s\n",
1737 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1743 io_error |= IOERR_GENERAL;
1744 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1749 if (f >= 0 && recurse && !divert_dirs) {
1750 int i, end = flist->used - 1;
1751 /* send_if_directory() bumps flist->used, so use "end". */
1752 for (i = start; i <= end; i++)
1753 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1757 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1758 char *start, char *limit, int flags, char name_type)
1760 static char lastpath[MAXPATHLEN] = "";
1761 static int lastpath_len = 0;
1762 static struct file_struct *lastpath_struct = NULL;
1763 struct file_struct *file;
1764 item_list *relname_list;
1765 relnamecache **rnpp;
1766 int len, need_new_dir, depth = 0;
1767 struct filter_list_struct save_filter_list = filter_list;
1769 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1770 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1773 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1774 && lastpath_len == limit - fname
1775 && strncmp(lastpath, fname, lastpath_len) == 0)
1780 char *tp = fname, *lp = lastpath;
1781 /* Skip any initial directories in our path that we
1782 * have in common with lastpath. */
1783 assert(start == fname);
1784 for ( ; ; tp++, lp++) {
1786 if (*lp == '/' || *lp == '\0')
1801 int save_copy_links = copy_links;
1802 int save_xfer_dirs = xfer_dirs;
1805 copy_links = xfer_dirs = 1;
1809 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1811 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1813 if (!inc_recurse && file && S_ISDIR(file->mode))
1814 change_local_filter_dir(fname, strlen(fname), depth);
1818 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1820 if (file && !S_ISDIR(file->mode))
1822 lastpath_struct = file;
1823 } else if (file && S_ISDIR(file->mode))
1824 change_local_filter_dir(fname, strlen(fname), ++depth);
1826 strlcpy(lastpath, fname, sizeof lastpath);
1827 lastpath_len = limit - fname;
1831 copy_links = save_copy_links;
1832 xfer_dirs = save_xfer_dirs;
1838 if (!lastpath_struct)
1839 goto done; /* dir must have vanished */
1841 len = strlen(limit+1);
1842 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1843 if (!relname_list) {
1844 if (!(relname_list = new0(item_list)))
1845 out_of_memory("send_implied_dirs");
1846 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1848 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1849 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1850 out_of_memory("send_implied_dirs");
1851 (*rnpp)->name_type = name_type;
1852 strlcpy((*rnpp)->fname, limit+1, len + 1);
1855 filter_list = save_filter_list;
1858 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1860 char fbuf[MAXPATHLEN];
1861 item_list *relname_list;
1862 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1866 dlen = strlen(fbuf);
1868 if (!change_pathname(file, NULL, 0))
1869 exit_cleanup(RERR_FILESELECT);
1871 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1873 if (file->flags & FLAG_CONTENT_DIR) {
1874 if (one_file_system) {
1876 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1877 interpret_stat_error(fbuf, True);
1880 filesystem_dev = st.st_dev;
1882 send_directory(f, flist, fbuf, dlen, flags);
1885 if (!relative_paths)
1888 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1892 for (j = 0; j < relname_list->count; j++) {
1894 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1895 char name_type = rnp->name_type;
1898 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1900 if (len >= (int)sizeof fbuf)
1901 continue; /* Impossible... */
1903 slash = strchr(fbuf+dlen+1, '/');
1905 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1909 if (name_type != NORMAL_NAME) {
1911 if (link_stat(fbuf, &st, 1) != 0) {
1912 interpret_stat_error(fbuf, True);
1915 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1917 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1923 void send_extra_file_list(int f, int at_least)
1925 struct file_list *flist;
1928 int old_cnt, save_io_error = io_error;
1933 /* Keep sending data until we have the requested number of
1934 * files in the upcoming file-lists. */
1935 old_cnt = cur_flist->used;
1936 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1937 old_cnt += flist->used;
1938 while (file_total - old_cnt < at_least) {
1939 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1940 int dir_ndx, dstart = stats.num_dirs;
1941 const char *pathname = F_PATHNAME(file);
1944 flist = flist_new(0, "send_extra_file_list");
1945 start_write = stats.total_written;
1948 dir_ndx = F_NDX(file);
1950 dir_ndx = send_dir_ndx;
1951 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1952 flist->parent_ndx = dir_ndx;
1954 send1extra(f, file, flist);
1955 prev_flags = file->flags;
1956 dp = F_DIR_NODE_P(file);
1958 /* If there are any duplicate directory names that follow, we
1959 * send all the dirs together in one file-list. The dir_flist
1960 * tree links all the child subdirs onto the last dup dir. */
1961 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1962 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1963 send_dir_ndx = dir_ndx;
1964 file = dir_flist->sorted[dir_ndx];
1965 /* Try to avoid some duplicate scanning of identical dirs. */
1966 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1967 file->flags &= ~FLAG_CONTENT_DIR;
1968 send1extra(f, file, flist);
1969 prev_flags = file->flags;
1970 dp = F_DIR_NODE_P(file);
1973 if (protocol_version < 31 || io_error == save_io_error || ignore_errors)
1976 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
1977 write_varint(f, io_error);
1980 if (need_unsorted_flist) {
1981 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1982 out_of_memory("send_extra_file_list");
1983 memcpy(flist->sorted, flist->files,
1984 flist->used * sizeof (struct file_struct*));
1986 flist->sorted = flist->files;
1988 flist_sort_and_clean(flist, 0);
1990 add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart);
1991 flist_done_allocating(flist);
1993 file_total += flist->used;
1994 stats.flist_size += stats.total_written - start_write;
1995 stats.num_files += flist->used;
1996 if (DEBUG_GTE(FLIST, 3))
1997 output_flist(flist);
1999 if (DIR_FIRST_CHILD(dp) >= 0) {
2000 send_dir_ndx = DIR_FIRST_CHILD(dp);
2003 while (DIR_NEXT_SIBLING(dp) < 0) {
2004 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
2005 write_ndx(f, NDX_FLIST_EOF);
2007 change_local_filter_dir(NULL, 0, 0);
2011 file = dir_flist->sorted[send_dir_ndx];
2012 dp = F_DIR_NODE_P(file);
2014 send_dir_ndx = DIR_NEXT_SIBLING(dp);
2019 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2020 send_msg_int(MSG_IO_ERROR, io_error);
2023 struct file_list *send_file_list(int f, int argc, char *argv[])
2025 static const char *lastdir;
2026 static int lastdir_len = -1;
2030 struct file_list *flist;
2031 struct timeval start_tv, end_tv;
2034 int disable_buffering;
2035 int flags = recurse ? FLAG_CONTENT_DIR : 0;
2036 int reading_remotely = filesfrom_host != NULL;
2037 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2039 | (filesfrom_convert ? RL_CONVERT : 0)
2041 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2042 int implied_dot_dir = 0;
2044 rprintf(FLOG, "building file list\n");
2045 if (show_filelist_p())
2046 start_filelist_progress("building file list");
2047 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2048 rprintf(FCLIENT, "sending incremental file list\n");
2050 start_write = stats.total_written;
2051 gettimeofday(&start_tv, NULL);
2053 if (relative_paths && protocol_version >= 30)
2054 implied_dirs = 1; /* We send flagged implied dirs */
2056 #ifdef SUPPORT_HARD_LINKS
2057 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2061 flist = cur_flist = flist_new(0, "send_file_list");
2063 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2064 flags |= FLAG_DIVERT_DIRS;
2066 dir_flist = cur_flist;
2068 disable_buffering = io_start_buffering_out(f);
2069 if (filesfrom_fd >= 0) {
2070 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2071 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2072 full_fname(argv[0]));
2073 exit_cleanup(RERR_FILESELECT);
2079 orig_dir = strdup(curr_dir);
2082 char fbuf[MAXPATHLEN], *fn, name_type;
2085 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2087 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2091 strlcpy(fbuf, *argv++, MAXPATHLEN);
2093 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2097 if (relative_paths) {
2098 /* We clean up fbuf below. */
2099 name_type = NORMAL_NAME;
2100 } else if (!len || fbuf[len - 1] == '/') {
2101 if (len == 2 && fbuf[0] == '.') {
2102 /* Turn "./" into just "." rather than "./." */
2105 if (len + 1 >= MAXPATHLEN)
2106 overflow_exit("send_file_list");
2110 name_type = DOTDIR_NAME;
2111 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2112 && (len == 2 || fbuf[len-3] == '/')) {
2113 if (len + 2 >= MAXPATHLEN)
2114 overflow_exit("send_file_list");
2118 name_type = DOTDIR_NAME;
2119 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2120 name_type = DOTDIR_NAME;
2122 name_type = NORMAL_NAME;
2126 if (!relative_paths) {
2127 p = strrchr(fbuf, '/');
2134 len -= p - fbuf + 1;
2139 if ((p = strstr(fbuf, "/./")) != NULL) {
2145 clean_fname(dir, 0);
2151 *--fn = '\0'; /* ensure room for '.' */
2154 /* A leading ./ can be used in relative mode to affect
2155 * the dest dir without its name being in the path. */
2156 if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
2157 send_file_name(f, flist, ".", NULL,
2158 (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
2160 implied_dot_dir = 1;
2162 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2163 | CFN_DROP_TRAILING_DOT_DIR);
2168 name_type = DOTDIR_NAME;
2169 } else if (fn[0] == '.')
2170 name_type = DOTDIR_NAME;
2171 } else if (fn[len-1] == '/') {
2173 if (len == 1 && *fn == '.')
2174 name_type = DOTDIR_NAME;
2176 name_type = SLASH_ENDING_NAME;
2178 /* Reject a ".." dir in the active part of the path. */
2179 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2180 if ((p[2] == '/' || p[2] == '\0')
2181 && (p == fn || p[-1] == '/')) {
2183 "found \"..\" dir in relative path: %s\n",
2185 exit_cleanup(RERR_SYNTAX);
2193 name_type = DOTDIR_NAME;
2196 dirlen = dir ? strlen(dir) : 0;
2197 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
2198 if (!change_pathname(NULL, dir, -dirlen))
2201 lastdir_len = pathname_len;
2202 } else if (!change_pathname(NULL, lastdir, lastdir_len))
2206 memmove(fbuf, fn, len + 1);
2208 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2209 || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
2210 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2211 if (errno != ENOENT || missing_args == 0) {
2212 /* This is a transfer error, but inhibit deletion
2213 * only if we might be omitting an existing file. */
2214 if (errno != ENOENT)
2215 io_error |= IOERR_GENERAL;
2216 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2219 } else if (missing_args == 1) {
2220 /* Just ignore the arg. */
2222 } else /* (missing_args == 2) */ {
2223 /* Send the arg as a "missing" entry with
2224 * mode 0, which tells the generator to delete it. */
2225 memset(&st, 0, sizeof st);
2229 /* A dot-dir should not be excluded! */
2230 if (name_type != DOTDIR_NAME && st.st_mode != 0
2231 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2234 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2235 rprintf(FINFO, "skipping directory %s\n", fbuf);
2239 if (inc_recurse && relative_paths && *fbuf) {
2240 if ((p = strchr(fbuf+1, '/')) != NULL) {
2241 if (p - fbuf == 1 && *fbuf == '.') {
2242 if ((fn = strchr(p+1, '/')) != NULL)
2246 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2250 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2251 /* Send the implied directories at the start of the
2252 * source spec, so we get their permissions right. */
2253 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2256 if (one_file_system)
2257 filesystem_dev = st.st_dev;
2259 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2260 struct file_struct *file;
2261 file = send_file_name(f, flist, fbuf, &st,
2262 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2263 ALL_FILTERS_NO_EXCLUDE);
2267 if (name_type == DOTDIR_NAME) {
2268 if (send_dir_depth < 0) {
2270 change_local_filter_dir(fbuf, len, send_dir_depth);
2272 send_directory(f, flist, fbuf, len, flags);
2275 send_if_directory(f, flist, file, fbuf, len, flags);
2277 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS_NO_EXCLUDE);
2280 gettimeofday(&end_tv, NULL);
2281 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2282 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2283 if (stats.flist_buildtime == 0)
2284 stats.flist_buildtime = 1;
2287 /* Indicate end of file list */
2288 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2291 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2292 write_varint(f, io_error);
2295 #ifdef SUPPORT_HARD_LINKS
2296 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2300 if (show_filelist_p())
2301 finish_filelist_progress(flist);
2303 gettimeofday(&end_tv, NULL);
2304 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2305 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2307 /* When converting names, both sides keep an unsorted file-list array
2308 * because the names will differ on the sending and receiving sides
2309 * (both sides will use the unsorted index number for each item). */
2311 /* Sort the list without removing any duplicates. This allows the
2312 * receiving side to ask for whatever name it kept. For incremental
2313 * recursion mode, the sender marks duplicate dirs so that it can
2314 * send them together in a single file-list. */
2315 if (need_unsorted_flist) {
2316 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2317 out_of_memory("send_file_list");
2318 memcpy(flist->sorted, flist->files,
2319 flist->used * sizeof (struct file_struct*));
2321 flist->sorted = flist->files;
2322 flist_sort_and_clean(flist, 0);
2323 file_total += flist->used;
2325 if (numeric_ids <= 0 && !inc_recurse)
2330 /* send the io_error flag */
2331 if (protocol_version < 30)
2332 write_int(f, ignore_errors ? 0 : io_error);
2333 else if (io_error && protocol_version == 30 && !ignore_errors)
2334 send_msg_int(MSG_IO_ERROR, io_error);
2336 if (disable_buffering)
2337 io_end_buffering_out();
2339 stats.flist_size = stats.total_written - start_write;
2340 stats.num_files = flist->used;
2342 if (DEBUG_GTE(FLIST, 3))
2343 output_flist(flist);
2345 if (DEBUG_GTE(FLIST, 2))
2346 rprintf(FINFO, "send_file_list done\n");
2350 add_dirs_to_tree(-1, flist, stats.num_dirs);
2351 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2352 flist->parent_ndx = -1;
2353 flist_done_allocating(flist);
2354 if (send_dir_ndx < 0) {
2355 write_ndx(f, NDX_FLIST_EOF);
2358 else if (file_total == 1) {
2359 /* If we're creating incremental file-lists and there
2360 * was just 1 item in the first file-list, send 1 more
2361 * file-list to check if this is a 1-file xfer. */
2362 send_extra_file_list(f, 1);
2370 struct file_list *recv_file_list(int f)
2372 struct file_list *flist;
2377 if (show_filelist_p())
2378 start_filelist_progress("receiving file list");
2379 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2380 rprintf(FCLIENT, "receiving incremental file list\n");
2381 rprintf(FLOG, "receiving file list\n");
2383 parse_name_map(usermap, True);
2385 parse_name_map(groupmap, False);
2388 start_read = stats.total_read;
2390 #ifdef SUPPORT_HARD_LINKS
2391 if (preserve_hard_links && !first_flist)
2395 flist = flist_new(0, "recv_file_list");
2398 if (flist->ndx_start == 1)
2399 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2400 dstart = dir_flist->used;
2406 while ((flags = read_byte(f)) != 0) {
2407 struct file_struct *file;
2409 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2410 flags |= read_byte(f) << 8;
2412 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2414 if (protocol_version < 31) {
2415 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2416 exit_cleanup(RERR_PROTOCOL);
2418 err = read_varint(f);
2424 flist_expand(flist, 1);
2425 file = recv_file_entry(flist, flags, f);
2427 if (S_ISREG(file->mode)) {
2428 /* Already counted */
2429 } else if (S_ISDIR(file->mode)) {
2431 flist_expand(dir_flist, 1);
2432 dir_flist->files[dir_flist->used++] = file;
2435 } else if (S_ISLNK(file->mode))
2436 stats.num_symlinks++;
2437 else if (IS_DEVICE(file->mode))
2438 stats.num_symlinks++;
2440 stats.num_specials++;
2442 flist->files[flist->used++] = file;
2444 maybe_emit_filelist_progress(flist->used);
2446 if (DEBUG_GTE(FLIST, 2)) {
2447 char *name = f_name(file, NULL);
2448 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2451 file_total += flist->used;
2453 if (DEBUG_GTE(FLIST, 2))
2454 rprintf(FINFO, "received %d names\n", flist->used);
2456 if (show_filelist_p())
2457 finish_filelist_progress(flist);
2459 if (need_unsorted_flist) {
2460 /* Create an extra array of index pointers that we can sort for
2461 * the generator's use (for wading through the files in sorted
2462 * order and for calling flist_find()). We keep the "files"
2463 * list unsorted for our exchange of index numbers with the
2464 * other side (since their names may not sort the same). */
2465 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2466 out_of_memory("recv_file_list");
2467 memcpy(flist->sorted, flist->files,
2468 flist->used * sizeof (struct file_struct*));
2469 if (inc_recurse && dir_flist->used > dstart) {
2470 static int dir_flist_malloced = 0;
2471 if (dir_flist_malloced < dir_flist->malloced) {
2472 dir_flist->sorted = realloc_array(dir_flist->sorted,
2473 struct file_struct *,
2474 dir_flist->malloced);
2475 dir_flist_malloced = dir_flist->malloced;
2477 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2478 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2479 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2482 flist->sorted = flist->files;
2483 if (inc_recurse && dir_flist->used > dstart) {
2484 dir_flist->sorted = dir_flist->files;
2485 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2490 flist_done_allocating(flist);
2492 recv_id_list(f, flist);
2496 flist_sort_and_clean(flist, relative_paths);
2498 if (protocol_version < 30) {
2499 /* Recv the io_error flag */
2500 int err = read_int(f);
2503 } else if (inc_recurse && flist->ndx_start == 1) {
2504 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2505 flist->parent_ndx = -1;
2508 if (DEBUG_GTE(FLIST, 3))
2509 output_flist(flist);
2511 if (DEBUG_GTE(FLIST, 2))
2512 rprintf(FINFO, "recv_file_list done\n");
2514 stats.flist_size += stats.total_read - start_read;
2515 stats.num_files += flist->used;
2520 /* This is only used once by the receiver if the very first file-list
2521 * has exactly one item in it. */
2522 void recv_additional_file_list(int f)
2524 struct file_list *flist;
2525 int ndx = read_ndx(f);
2526 if (ndx == NDX_FLIST_EOF) {
2528 change_local_filter_dir(NULL, 0, 0);
2530 ndx = NDX_FLIST_OFFSET - ndx;
2531 if (ndx < 0 || ndx >= dir_flist->used) {
2532 ndx = NDX_FLIST_OFFSET - ndx;
2534 "[%s] Invalid dir index: %d (%d - %d)\n",
2535 who_am_i(), ndx, NDX_FLIST_OFFSET,
2536 NDX_FLIST_OFFSET - dir_flist->used + 1);
2537 exit_cleanup(RERR_PROTOCOL);
2539 if (DEBUG_GTE(FLIST, 3)) {
2540 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2543 flist = recv_file_list(f);
2544 flist->parent_ndx = ndx;
2548 /* Search for an identically-named item in the file list. Note that the
2549 * items must agree in their directory-ness, or no match is returned. */
2550 int flist_find(struct file_list *flist, struct file_struct *f)
2552 int low = flist->low, high = flist->high;
2553 int diff, mid, mid_up;
2555 while (low <= high) {
2556 mid = (low + high) / 2;
2557 if (F_IS_ACTIVE(flist->sorted[mid]))
2560 /* Scan for the next non-empty entry using the cached
2561 * distance values. If the value isn't fully up-to-
2562 * date, update it. */
2563 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2564 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2566 mid_up += F_DEPTH(flist->sorted[mid_up]);
2567 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2568 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2570 if (mid_up > high) {
2571 /* If there's nothing left above us, set high to
2572 * a non-empty entry below us and continue. */
2573 high = mid - (int)flist->sorted[mid]->len32;
2574 if (!F_IS_ACTIVE(flist->sorted[high])) {
2576 high -= (int)flist->sorted[high]->len32;
2577 } while (!F_IS_ACTIVE(flist->sorted[high]));
2578 flist->sorted[mid]->len32 = mid - high;
2583 diff = f_name_cmp(flist->sorted[mid_up], f);
2585 if (protocol_version < 29
2586 && S_ISDIR(flist->sorted[mid_up]->mode)
2587 != S_ISDIR(f->mode))
2599 /* Search for an identically-named item in the file list. Differs from
2600 * flist_find in that an item that agrees with "f" in directory-ness is
2601 * preferred but one that does not is still found. */
2602 int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2607 /* First look for an item that agrees in directory-ness. */
2608 ndx = flist_find(flist, f);
2612 /* Temporarily flip f->mode to look for an item of opposite
2613 * directory-ness. */
2614 save_mode = f->mode;
2615 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2616 ndx = flist_find(flist, f);
2617 f->mode = save_mode;
2622 * Free up any resources a file_struct has allocated
2623 * and clear the file.
2625 void clear_file(struct file_struct *file)
2627 /* The +1 zeros out the first char of the basename. */
2628 memset(file, 0, FILE_STRUCT_LEN + 1);
2629 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2630 * entry. Likewise for len32 in the opposite direction. We assume
2631 * that we're alone for now since flist_find() will adjust the counts
2632 * it runs into that aren't up-to-date. */
2633 file->len32 = F_DEPTH(file) = 1;
2636 /* Allocate a new file list. */
2637 struct file_list *flist_new(int flags, char *msg)
2639 struct file_list *flist;
2641 if (!(flist = new0(struct file_list)))
2644 if (flags & FLIST_TEMP) {
2645 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2650 /* This is a doubly linked list with prev looping back to
2651 * the end of the list, but the last next pointer is NULL. */
2653 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2656 if (!flist->file_pool)
2659 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2661 first_flist = cur_flist = flist->prev = flist;
2663 struct file_list *prev = first_flist->prev;
2665 flist->file_pool = first_flist->file_pool;
2667 flist->ndx_start = prev->ndx_start + prev->used + 1;
2668 flist->flist_num = prev->flist_num + 1;
2671 prev->next = first_flist->prev = flist;
2673 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2680 /* Free up all elements in a flist. */
2681 void flist_free(struct file_list *flist)
2684 /* Was FLIST_TEMP dir-list. */
2685 } else if (flist == flist->prev) {
2686 first_flist = cur_flist = NULL;
2690 if (flist == cur_flist)
2691 cur_flist = flist->next;
2692 if (flist == first_flist)
2693 first_flist = first_flist->next;
2695 flist->prev->next = flist->next;
2697 flist->next = first_flist;
2699 flist->next->prev = flist->prev;
2700 file_total -= flist->used;
2704 if (!flist->prev || !flist_cnt)
2705 pool_destroy(flist->file_pool);
2707 pool_free_old(flist->file_pool, flist->pool_boundary);
2709 if (flist->sorted && flist->sorted != flist->files)
2710 free(flist->sorted);
2715 /* This routine ensures we don't have any duplicate names in our file list.
2716 * duplicate names can cause corruption because of the pipelining. */
2717 static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2719 char fbuf[MAXPATHLEN];
2724 if (flist->used == 0) {
2730 fsort(flist->sorted, flist->used);
2732 if (!am_sender || inc_recurse) {
2733 for (i = prev_i = 0; i < flist->used; i++) {
2734 if (F_IS_ACTIVE(flist->sorted[i])) {
2739 flist->low = prev_i;
2741 i = prev_i = flist->used - 1;
2745 while (++i < flist->used) {
2747 struct file_struct *file = flist->sorted[i];
2749 if (!F_IS_ACTIVE(file))
2751 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2753 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2754 int save_mode = file->mode;
2755 /* Make sure that this directory doesn't duplicate a
2756 * non-directory earlier in the list. */
2757 flist->high = prev_i;
2758 file->mode = S_IFREG;
2759 j = flist_find(flist, file);
2760 file->mode = save_mode;
2765 /* If one is a dir and the other is not, we want to
2766 * keep the dir because it might have contents in the
2767 * list. Otherwise keep the first one. */
2768 if (S_ISDIR(file->mode)) {
2769 struct file_struct *fp = flist->sorted[j];
2770 if (!S_ISDIR(fp->mode))
2774 file->flags |= FLAG_DUPLICATE;
2775 else { /* Make sure we merge our vital flags. */
2776 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2777 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2785 if (DEBUG_GTE(DUP, 1)) {
2787 "removing duplicate name %s from file list (%d)\n",
2788 f_name(file, fbuf), drop + flist->ndx_start);
2790 clear_file(flist->sorted[drop]);
2794 if (flist->low == drop) {
2796 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2805 flist->high = prev_i;
2808 /* We need to strip off the leading slashes for relative
2809 * paths, but this must be done _after_ the sorting phase. */
2810 for (i = flist->low; i <= flist->high; i++) {
2811 struct file_struct *file = flist->sorted[i];
2815 while (*file->dirname == '/')
2817 if (!*file->dirname)
2818 file->dirname = NULL;
2822 if (prune_empty_dirs && !am_sender) {
2823 int j, prev_depth = 0;
2825 prev_i = 0; /* It's OK that this isn't really true. */
2827 for (i = flist->low; i <= flist->high; i++) {
2828 struct file_struct *fp, *file = flist->sorted[i];
2830 /* This temporarily abuses the F_DEPTH() value for a
2831 * directory that is in a chain that might get pruned.
2832 * We restore the old value if it gets a reprieve. */
2833 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2834 /* Dump empty dirs when coming back down. */
2835 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2836 fp = flist->sorted[prev_i];
2837 if (F_DEPTH(fp) >= 0)
2839 prev_i = -F_DEPTH(fp)-1;
2842 prev_depth = F_DEPTH(file);
2843 if (is_excluded(f_name(file, fbuf), 1,
2845 /* Keep dirs through this dir. */
2846 for (j = prev_depth-1; ; j--) {
2847 fp = flist->sorted[prev_i];
2848 if (F_DEPTH(fp) >= 0)
2850 prev_i = -F_DEPTH(fp)-1;
2854 F_DEPTH(file) = -prev_i-1;
2857 /* Keep dirs through this non-dir. */
2858 for (j = prev_depth; ; j--) {
2859 fp = flist->sorted[prev_i];
2860 if (F_DEPTH(fp) >= 0)
2862 prev_i = -F_DEPTH(fp)-1;
2867 /* Dump all remaining empty dirs. */
2869 struct file_struct *fp = flist->sorted[prev_i];
2870 if (F_DEPTH(fp) >= 0)
2872 prev_i = -F_DEPTH(fp)-1;
2876 for (i = flist->low; i <= flist->high; i++) {
2877 if (F_IS_ACTIVE(flist->sorted[i]))
2881 for (i = flist->high; i >= flist->low; i--) {
2882 if (F_IS_ACTIVE(flist->sorted[i]))
2889 static void output_flist(struct file_list *flist)
2891 char uidbuf[16], gidbuf[16], depthbuf[16];
2892 struct file_struct *file;
2893 const char *root, *dir, *slash, *name, *trail;
2894 const char *who = who_am_i();
2897 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2898 who, flist->ndx_start, flist->used, flist->low, flist->high);
2899 for (i = 0; i < flist->used; i++) {
2900 file = flist->files[i];
2901 if ((am_root || am_sender) && uid_ndx) {
2902 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2907 static char parens[] = "(\0)\0\0\0";
2908 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2909 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2910 pp, F_GROUP(file), pp + 2);
2914 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2915 if (F_IS_ACTIVE(file)) {
2916 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2917 if ((dir = file->dirname) == NULL)
2921 name = file->basename;
2922 trail = S_ISDIR(file->mode) ? "/" : "";
2924 root = dir = slash = name = trail = "";
2926 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
2927 who, i + flist->ndx_start,
2928 root, dir, slash, name, trail,
2929 (int)file->mode, comma_num(F_LENGTH(file)),
2930 uidbuf, gidbuf, file->flags);
2934 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2935 enum fnc_type { t_PATH, t_ITEM };
2937 static int found_prefix;
2939 /* Compare the names of two file_struct entities, similar to how strcmp()
2940 * would do if it were operating on the joined strings.
2942 * Some differences beginning with protocol_version 29: (1) directory names
2943 * are compared with an assumed trailing slash so that they compare in a
2944 * way that would cause them to sort immediately prior to any content they
2945 * may have; (2) a directory of any name compares after a non-directory of
2946 * any name at the same depth; (3) a directory with name "." compares prior
2947 * to anything else. These changes mean that a directory and a non-dir
2948 * with the same name will not compare as equal (protocol_version >= 29).
2950 * The dirname component can be an empty string, but the basename component
2951 * cannot (and never is in the current codebase). The basename component
2952 * may be NULL (for a removed item), in which case it is considered to be
2953 * after any existing item. */
2954 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2957 const uchar *c1, *c2;
2958 enum fnc_state state1, state2;
2959 enum fnc_type type1, type2;
2960 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2962 if (!f1 || !F_IS_ACTIVE(f1)) {
2963 if (!f2 || !F_IS_ACTIVE(f2))
2967 if (!f2 || !F_IS_ACTIVE(f2))
2970 c1 = (uchar*)f1->dirname;
2971 c2 = (uchar*)f2->dirname;
2975 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2976 c1 = (const uchar*)f1->basename;
2977 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2979 state1 = s_TRAILING;
2988 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2989 c2 = (const uchar*)f2->basename;
2990 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2992 state2 = s_TRAILING;
3002 return type1 == t_PATH ? 1 : -1;
3012 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3013 c1 = (const uchar*)f1->basename;
3014 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3016 state1 = s_TRAILING;
3022 state1 = s_TRAILING;
3023 if (type1 == t_PATH) {
3032 if (*c2 && type1 != type2)
3033 return type1 == t_PATH ? 1 : -1;
3042 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3043 c2 = (const uchar*)f2->basename;
3044 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3046 state2 = s_TRAILING;
3052 state2 = s_TRAILING;
3053 if (type2 == t_PATH) {
3066 return type1 == t_PATH ? 1 : -1;
3068 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3073 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3074 * not match if f2's basename is not an exact match of a path element in f1.
3075 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3076 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3080 return found_prefix;
3083 char *f_name_buf(void)
3085 static char names[5][MAXPATHLEN];
3086 static unsigned int n;
3088 n = (n + 1) % (sizeof names / sizeof names[0]);
3093 /* Return a copy of the full filename of a flist entry, using the indicated
3094 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3095 * done because we checked the size when creating the file_struct entry.
3097 char *f_name(const struct file_struct *f, char *fbuf)
3099 if (!f || !F_IS_ACTIVE(f))
3103 fbuf = f_name_buf();
3106 int len = strlen(f->dirname);
3107 memcpy(fbuf, f->dirname, len);
3109 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3111 strlcpy(fbuf, f->basename, MAXPATHLEN);
3116 /* Do a non-recursive scan of the named directory, possibly ignoring all
3117 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3118 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3119 * buffer (the functions we call will append names onto the end, but the old
3120 * dir value will be restored on exit). */
3121 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
3123 struct file_list *dirlist;
3124 char dirbuf[MAXPATHLEN];
3125 int save_recurse = recurse;
3126 int save_xfer_dirs = xfer_dirs;
3127 int save_prune_empty_dirs = prune_empty_dirs;
3130 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3131 if (dlen >= MAXPATHLEN)
3136 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3140 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, FLAG_CONTENT_DIR);
3141 xfer_dirs = save_xfer_dirs;
3142 recurse = save_recurse;
3143 if (INFO_GTE(PROGRESS, 1))
3144 flist_count_offset += dirlist->used;
3146 prune_empty_dirs = 0;
3147 dirlist->sorted = dirlist->files;
3148 flist_sort_and_clean(dirlist, 0);
3149 prune_empty_dirs = save_prune_empty_dirs;
3151 if (DEBUG_GTE(FLIST, 3))
3152 output_flist(dirlist);