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.
32 extern int inc_recurse;
33 extern int do_progress;
34 extern int always_checksum;
36 extern int ignore_errors;
37 extern int numeric_ids;
40 extern int filesfrom_fd;
41 extern int one_file_system;
42 extern int copy_dirlinks;
43 extern int keep_dirlinks;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_devices;
47 extern int preserve_specials;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int relative_paths;
51 extern int implied_dirs;
52 extern int file_extra_cnt;
53 extern int ignore_perishable;
54 extern int non_perishable_cnt;
55 extern int prune_empty_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern struct stats stats;
62 extern char curr_dir[MAXPATHLEN];
64 extern struct chmod_mode_struct *chmod_modes;
66 extern struct filter_list_struct filter_list;
67 extern struct filter_list_struct server_filter_list;
71 dev_t filesystem_dev; /* used to implement -x */
73 struct file_list *cur_flist, *first_flist, *dir_flist;
74 int send_dir_ndx = -1, send_dir_depth = 0;
75 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
76 int file_total = 0; /* total of all active items over all file-lists */
77 int flist_eof = 0; /* all the file-lists are now known */
79 /* The tmp_* vars are used as a cache area by make_file() to store data
80 * that the sender doesn't need to remember in its file list. The data
81 * will survive just long enough to be used by send_file_entry(). */
82 static dev_t tmp_rdev;
83 #ifdef SUPPORT_HARD_LINKS
84 static int64 tmp_dev, tmp_ino;
86 static char tmp_sum[MD4_SUM_LENGTH];
88 static char empty_sum[MD4_SUM_LENGTH];
89 static int flist_count_offset; /* for --delete --progress */
91 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
92 static void output_flist(struct file_list *flist);
97 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
98 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
100 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
103 static int show_filelist_p(void)
105 return verbose && xfer_dirs && !am_server && !inc_recurse;
108 static void start_filelist_progress(char *kind)
110 rprintf(FCLIENT, "%s ... ", kind);
111 if (verbose > 1 || do_progress)
112 rprintf(FCLIENT, "\n");
116 static void emit_filelist_progress(int count)
118 rprintf(FCLIENT, " %d files...\r", count);
121 static void maybe_emit_filelist_progress(int count)
123 if (do_progress && show_filelist_p() && (count % 100) == 0)
124 emit_filelist_progress(count);
127 static void finish_filelist_progress(const struct file_list *flist)
130 /* This overwrites the progress line */
131 rprintf(FINFO, "%d file%sto consider\n",
132 flist->count, flist->count == 1 ? " " : "s ");
134 rprintf(FINFO, "done\n");
137 void show_flist_stats(void)
142 static void list_file_entry(struct file_struct *f)
144 char permbuf[PERMSTRING_SIZE];
147 if (!F_IS_ACTIVE(f)) {
148 /* this can happen if duplicate names were removed */
152 permstring(permbuf, f->mode);
156 if (preserve_links && S_ISLNK(f->mode)) {
157 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
158 permbuf, len, timestring(f->modtime),
159 f_name(f, NULL), F_SYMLINK(f));
163 rprintf(FINFO, "%s %11.0f %s %s\n",
164 permbuf, len, timestring(f->modtime),
169 /* Stat either a symlink or its referent, depending on the settings of
170 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
172 * If path is the name of a symlink, then the linkbuf buffer (which must hold
173 * MAXPATHLEN chars) will be set to the symlink's target string.
175 * The stat structure pointed to by stp will contain information about the
176 * link or the referent as appropriate, if they exist. */
177 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
180 if (link_stat(path, stp, copy_dirlinks) < 0)
182 if (S_ISLNK(stp->st_mode)) {
183 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
186 linkbuf[llen] = '\0';
187 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
189 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
192 return do_stat(path, stp);
197 return do_stat(path, stp);
201 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
205 return do_stat(path, stp);
206 if (do_lstat(path, stp) < 0)
208 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
210 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
215 return do_stat(path, stp);
219 /* This function is used to check if a file should be included/excluded
220 * from the list of files based on its name and type etc. The value of
221 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
222 static int is_excluded(char *fname, int is_dir, int filter_level)
224 #if 0 /* This currently never happens, so avoid a useless compare. */
225 if (filter_level == NO_FILTERS)
229 /* never exclude '.', even if somebody does --exclude '*' */
230 if (fname[0] == '.' && !fname[1])
232 /* Handle the -R version of the '.' dir. */
233 if (fname[0] == '/') {
234 int len = strlen(fname);
235 if (fname[len-1] == '.' && fname[len-2] == '/')
239 if (server_filter_list.head
240 && check_filter(&server_filter_list, fname, is_dir) < 0)
242 if (filter_level != ALL_FILTERS)
245 && check_filter(&filter_list, fname, is_dir) < 0)
250 static int to_wire_mode(mode_t mode)
253 #if _S_IFLNK != 0120000
255 return (mode & ~(_S_IFMT)) | 0120000;
261 static mode_t from_wire_mode(int mode)
263 #if _S_IFLNK != 0120000
264 if ((mode & (_S_IFMT)) == 0120000)
265 return (mode & ~(_S_IFMT)) | _S_IFLNK;
270 static void send_directory(int f, struct file_list *flist, int ndx,
271 char *fbuf, int len, int flags);
273 static const char *flist_dir, *orig_dir;
274 static int flist_dir_len;
278 * Make sure @p flist is big enough to hold at least @p flist->count
281 void flist_expand(struct file_list *flist)
283 struct file_struct **new_ptr;
285 if (flist->count < flist->malloced)
288 if (flist->malloced < FLIST_START)
289 flist->malloced = FLIST_START;
290 else if (flist->malloced >= FLIST_LINEAR)
291 flist->malloced += FLIST_LINEAR;
293 flist->malloced *= 2;
296 * In case count jumped or we are starting the list
297 * with a known size just set it.
299 if (flist->malloced < flist->count)
300 flist->malloced = flist->count;
302 new_ptr = realloc_array(flist->files, struct file_struct *,
305 if (verbose >= 2 && flist->malloced != FLIST_START) {
306 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
308 (double)sizeof flist->files[0] * flist->malloced,
309 (new_ptr == flist->files) ? " not" : "");
312 flist->files = new_ptr;
315 out_of_memory("flist_expand");
318 int push_flist_dir(const char *dir, int len)
320 if (dir == flist_dir)
324 orig_dir = strdup(curr_dir);
326 if (flist_dir && !pop_dir(orig_dir)) {
327 rsyserr(FERROR, errno, "pop_dir %s failed",
328 full_fname(orig_dir));
329 exit_cleanup(RERR_FILESELECT);
332 if (dir && !push_dir(dir, 0)) {
333 io_error |= IOERR_GENERAL;
334 rsyserr(FERROR, errno, "push_dir %s failed",
340 flist_dir_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
345 static void send_file_entry(int f, struct file_struct *file, int ndx)
347 static time_t modtime;
351 static uint32 rdev_major;
354 static char *user_name, *group_name;
355 static char lastname[MAXPATHLEN];
356 char fname[MAXPATHLEN];
357 int first_hlink_ndx = -1;
363 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
365 if (file->mode == mode)
366 flags |= XMIT_SAME_MODE;
369 if ((preserve_devices && IS_DEVICE(mode))
370 || (preserve_specials && IS_SPECIAL(mode))) {
371 if (protocol_version < 28) {
372 if (tmp_rdev == rdev)
373 flags |= XMIT_SAME_RDEV_pre28;
378 if ((uint32)major(rdev) == rdev_major)
379 flags |= XMIT_SAME_RDEV_MAJOR;
381 rdev_major = major(rdev);
382 if ((uint32)minor(rdev) <= 0xFFu)
383 flags |= XMIT_RDEV_MINOR_IS_SMALL;
385 } else if (protocol_version < 28)
386 rdev = MAKEDEV(0, 0);
388 if (F_UID(file) == uid && *lastname)
389 flags |= XMIT_SAME_UID;
392 if (preserve_uid && !numeric_ids) {
393 user_name = add_uid(uid);
394 if (inc_recurse && user_name)
395 flags |= XMIT_USER_NAME_FOLLOWS;
400 if (F_GID(file) == gid && *lastname)
401 flags |= XMIT_SAME_GID;
404 if (preserve_gid && !numeric_ids) {
405 group_name = add_gid(gid);
406 if (inc_recurse && group_name)
407 flags |= XMIT_GROUP_NAME_FOLLOWS;
411 if (file->modtime == modtime)
412 flags |= XMIT_SAME_TIME;
414 modtime = file->modtime;
416 #ifdef SUPPORT_HARD_LINKS
418 if (protocol_version >= 30) {
419 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
420 first_hlink_ndx = (int32)(long)np->data - 1;
421 if (first_hlink_ndx < 0) {
422 np->data = (void*)(long)(ndx + 1);
423 flags |= XMIT_HLINK_FIRST;
425 flags |= XMIT_HLINKED;
427 if (tmp_dev == dev) {
428 if (protocol_version >= 28)
429 flags |= XMIT_SAME_DEV_pre30;
432 flags |= XMIT_HLINKED;
438 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
440 l2 = strlen(fname+l1);
443 flags |= XMIT_SAME_NAME;
445 flags |= XMIT_LONG_NAME;
447 /* We must make sure we don't send a zero flag byte or the
448 * other end will terminate the flist transfer. Note that
449 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
450 * it's harmless way to add a bit to the first flag byte. */
451 if (protocol_version >= 28) {
452 if (!flags && !S_ISDIR(mode))
453 flags |= XMIT_TOP_DIR;
454 if ((flags & 0xFF00) || !flags) {
455 flags |= XMIT_EXTENDED_FLAGS;
456 write_shortint(f, flags);
458 write_byte(f, flags);
461 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
462 write_byte(f, flags);
464 if (flags & XMIT_SAME_NAME)
466 if (flags & XMIT_LONG_NAME)
470 write_buf(f, fname + l1, l2);
472 if (first_hlink_ndx >= 0) {
473 write_int(f, first_hlink_ndx);
477 write_longint(f, F_LENGTH(file));
478 if (!(flags & XMIT_SAME_TIME))
479 write_int(f, modtime);
480 if (!(flags & XMIT_SAME_MODE))
481 write_int(f, to_wire_mode(mode));
482 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
484 if (flags & XMIT_USER_NAME_FOLLOWS) {
485 int len = strlen(user_name);
487 write_buf(f, user_name, len);
490 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
492 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
493 int len = strlen(group_name);
495 write_buf(f, group_name, len);
498 if ((preserve_devices && IS_DEVICE(mode))
499 || (preserve_specials && IS_SPECIAL(mode))) {
500 if (protocol_version < 28) {
501 if (!(flags & XMIT_SAME_RDEV_pre28))
502 write_int(f, (int)rdev);
504 if (!(flags & XMIT_SAME_RDEV_MAJOR))
505 write_int(f, major(rdev));
506 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
507 write_byte(f, minor(rdev));
509 write_int(f, minor(rdev));
514 if (preserve_links && S_ISLNK(mode)) {
515 const char *sl = F_SYMLINK(file);
516 int len = strlen(sl);
518 write_buf(f, sl, len);
522 #ifdef SUPPORT_HARD_LINKS
523 if (tmp_dev != 0 && protocol_version < 30) {
524 if (protocol_version < 26) {
525 /* 32-bit dev_t and ino_t */
526 write_int(f, (int32)dev);
527 write_int(f, (int32)tmp_ino);
529 /* 64-bit dev_t and ino_t */
530 if (!(flags & XMIT_SAME_DEV_pre30))
531 write_longint(f, dev);
532 write_longint(f, tmp_ino);
537 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
542 /* Prior to 28, we sent a useless set of nulls. */
545 write_buf(f, sum, checksum_len);
549 strlcpy(lastname, fname, MAXPATHLEN);
551 if (S_ISREG(mode) || S_ISLNK(mode))
552 stats.total_size += F_LENGTH(file);
555 static struct file_struct *recv_file_entry(struct file_list *flist,
558 static time_t modtime;
562 static uint32 rdev_major;
565 static char lastname[MAXPATHLEN], *lastdir;
566 static int lastdir_depth, lastdir_len = -1;
567 static unsigned int del_hier_name_len = 0;
568 static int in_del_hier = 0;
569 char thisname[MAXPATHLEN];
570 unsigned int l1 = 0, l2 = 0;
571 int alloc_len, basename_len, linkname_len;
572 int extra_len = file_extra_cnt * EXTRA_LEN;
573 int first_hlink_ndx = -1;
575 const char *basename;
577 struct file_struct *file;
579 if (flags & XMIT_SAME_NAME)
582 if (flags & XMIT_LONG_NAME)
587 if (l2 >= MAXPATHLEN - l1) {
589 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
590 flags, l1, l2, lastname, who_am_i());
591 overflow_exit("recv_file_entry");
594 strlcpy(thisname, lastname, l1 + 1);
595 read_sbuf(f, &thisname[l1], l2);
596 thisname[l1 + l2] = 0;
598 strlcpy(lastname, thisname, MAXPATHLEN);
600 clean_fname(thisname, 0);
603 sanitize_path(thisname, thisname, "", 0, NULL);
605 if ((basename = strrchr(thisname, '/')) != NULL) {
606 int len = basename++ - thisname;
607 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
608 lastdir = new_array(char, len + 1);
609 memcpy(lastdir, thisname, len);
612 lastdir_depth = count_dir_elements(lastdir);
616 basename_len = strlen(basename) + 1; /* count the '\0' */
618 #ifdef SUPPORT_HARD_LINKS
619 if (protocol_version >= 30
620 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
621 struct file_struct *first;
622 first_hlink_ndx = read_int(f);
623 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
625 "hard-link reference out of range: %d (%d)\n",
626 first_hlink_ndx, flist->count);
627 exit_cleanup(RERR_PROTOCOL);
629 first = flist->files[first_hlink_ndx];
630 file_length = F_LENGTH(first);
631 modtime = first->modtime;
637 if ((preserve_devices && IS_DEVICE(mode))
638 || (preserve_specials && IS_SPECIAL(mode))) {
639 uint32 *devp = F_RDEV_P(first);
640 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
642 if (preserve_links && S_ISLNK(mode))
643 linkname_len = strlen(F_SYMLINK(first)) + 1;
650 file_length = read_longint(f);
651 if (!(flags & XMIT_SAME_TIME))
652 modtime = (time_t)read_int(f);
653 if (!(flags & XMIT_SAME_MODE))
654 mode = from_wire_mode(read_int(f));
656 if (chmod_modes && !S_ISLNK(mode))
657 mode = tweak_mode(mode, chmod_modes);
659 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
660 uid = (uid_t)read_int(f);
661 if (flags & XMIT_USER_NAME_FOLLOWS)
662 uid = recv_user_name(f, uid);
663 else if (inc_recurse && am_root && !numeric_ids)
664 uid = match_uid(uid);
666 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
667 gid = (gid_t)read_int(f);
668 if (flags & XMIT_GROUP_NAME_FOLLOWS)
669 gid = recv_group_name(f, gid);
670 else if (inc_recurse && (!am_root || !numeric_ids))
671 gid = match_gid(gid);
674 if ((preserve_devices && IS_DEVICE(mode))
675 || (preserve_specials && IS_SPECIAL(mode))) {
676 if (protocol_version < 28) {
677 if (!(flags & XMIT_SAME_RDEV_pre28))
678 rdev = (dev_t)read_int(f);
681 if (!(flags & XMIT_SAME_RDEV_MAJOR))
682 rdev_major = read_int(f);
683 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
684 rdev_minor = read_byte(f);
686 rdev_minor = read_int(f);
687 rdev = MAKEDEV(rdev_major, rdev_minor);
689 extra_len += 2 * EXTRA_LEN;
691 } else if (protocol_version < 28)
692 rdev = MAKEDEV(0, 0);
695 if (preserve_links && S_ISLNK(mode)) {
696 linkname_len = read_int(f) + 1; /* count the '\0' */
697 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
698 rprintf(FERROR, "overflow: linkname_len=%d\n",
700 overflow_exit("recv_file_entry");
707 #ifdef SUPPORT_HARD_LINKS
709 if (preserve_hard_links) {
710 if (protocol_version < 28 && S_ISREG(mode))
711 flags |= XMIT_HLINKED;
712 if (flags & XMIT_HLINKED)
713 extra_len += EXTRA_LEN;
717 if (always_checksum && S_ISREG(mode))
718 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
720 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
721 extra_len += EXTRA_LEN;
723 #if EXTRA_ROUNDING > 0
724 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
725 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
728 if (inc_recurse && S_ISDIR(mode)) {
729 if (one_file_system) {
730 /* Room to save the dir's device for -x */
731 extra_len += 2 * EXTRA_LEN;
736 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
738 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
740 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
742 file = (struct file_struct *)bp;
743 bp += FILE_STRUCT_LEN;
745 memcpy(bp, basename, basename_len);
746 bp += basename_len + linkname_len; /* skip space for symlink too */
748 #ifdef SUPPORT_HARD_LINKS
749 if (flags & XMIT_HLINKED)
750 file->flags |= FLAG_HLINKED;
752 file->modtime = modtime;
753 file->len32 = (uint32)file_length;
754 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
755 file->flags |= FLAG_LENGTH64;
756 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
764 if (basename != thisname) {
765 file->dirname = lastdir;
766 F_DEPTH(file) = lastdir_depth + 1;
771 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
773 if (flags & XMIT_TOP_DIR) {
774 in_del_hier = recurse;
775 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
776 if (relative_paths && del_hier_name_len > 2
777 && lastname[del_hier_name_len-1] == '.'
778 && lastname[del_hier_name_len-2] == '/')
779 del_hier_name_len -= 2;
780 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
781 } else if (in_del_hier) {
782 if (!relative_paths || !del_hier_name_len
783 || (l1 >= del_hier_name_len
784 && lastname[del_hier_name_len] == '/'))
785 file->flags |= FLAG_XFER_DIR;
791 if ((preserve_devices && IS_DEVICE(mode))
792 || (preserve_specials && IS_SPECIAL(mode))) {
793 uint32 *devp = F_RDEV_P(file);
794 DEV_MAJOR(devp) = major(rdev);
795 DEV_MINOR(devp) = minor(rdev);
800 bp = (char*)file->basename + basename_len;
801 if (first_hlink_ndx >= 0) {
802 struct file_struct *first = flist->files[first_hlink_ndx];
803 memcpy(bp, F_SYMLINK(first), linkname_len);
805 read_sbuf(f, bp, linkname_len - 1);
807 sanitize_path(bp, bp, "", lastdir_depth, NULL);
811 #ifdef SUPPORT_HARD_LINKS
812 if (preserve_hard_links && flags & XMIT_HLINKED) {
813 if (protocol_version >= 30) {
814 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
815 ? flist->count : first_hlink_ndx;
817 static int32 cnt = 0;
818 struct idev_node *np;
821 if (protocol_version < 26) {
825 if (!(flags & XMIT_SAME_DEV_pre30))
826 dev = read_longint(f);
827 ino = read_longint(f);
829 np = idev_node(dev, ino);
830 ndx = (int32)(long)np->data - 1;
833 np->data = (void*)(long)cnt;
835 F_HL_GNUM(file) = ndx;
840 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
842 bp = (char*)F_SUM(file);
844 /* Prior to 28, we get a useless set of nulls. */
847 if (first_hlink_ndx >= 0) {
848 struct file_struct *first = flist->files[first_hlink_ndx];
849 memcpy(bp, F_SUM(first), checksum_len);
851 read_buf(f, bp, checksum_len);
854 if (S_ISREG(mode) || S_ISLNK(mode))
855 stats.total_size += file_length;
861 * Create a file_struct for a named file by reading its stat()
862 * information and performing extensive checks against global
865 * @return the new file, or NULL if there was an error or this file
866 * should be excluded.
868 * @todo There is a small optimization opportunity here to avoid
869 * stat()ing the file in some circumstances, which has a certain cost.
870 * We are called immediately after doing readdir(), and so we may
871 * already know the d_type of the file. We could for example avoid
872 * statting directories if we're not recursing, but this is not a very
873 * important case. Some systems may not have d_type.
875 struct file_struct *make_file(const char *fname, struct file_list *flist,
876 STRUCT_STAT *stp, int flags, int filter_level)
878 static char *lastdir;
879 static int lastdir_len = -1;
880 struct file_struct *file;
882 char thisname[MAXPATHLEN];
883 char linkname[MAXPATHLEN];
884 int alloc_len, basename_len, linkname_len;
885 int extra_len = file_extra_cnt * EXTRA_LEN;
886 const char *basename;
889 if (strlcpy(thisname, fname, sizeof thisname)
890 >= sizeof thisname - flist_dir_len) {
891 rprintf(FINFO, "skipping overly long name: %s\n", fname);
894 clean_fname(thisname, 0);
896 sanitize_path(thisname, thisname, "", 0, NULL);
898 if (stp && S_ISDIR(stp->st_mode)) {
899 st = *stp; /* Needed for "symlink/." with --relative. */
900 *linkname = '\0'; /* make IBM code checker happy */
901 } else if (readlink_stat(thisname, &st, linkname) != 0) {
902 int save_errno = errno;
903 /* See if file is excluded before reporting an error. */
904 if (filter_level != NO_FILTERS
905 && (is_excluded(thisname, 0, filter_level)
906 || is_excluded(thisname, 1, filter_level))) {
907 if (ignore_perishable && save_errno != ENOENT)
908 non_perishable_cnt++;
911 if (save_errno == ENOENT) {
913 /* Avoid "vanished" error if symlink points nowhere. */
914 if (copy_links && do_lstat(thisname, &st) == 0
915 && S_ISLNK(st.st_mode)) {
916 io_error |= IOERR_GENERAL;
917 rprintf(FERROR, "symlink has no referent: %s\n",
918 full_fname(thisname));
922 enum logcode c = am_daemon && protocol_version < 28
924 io_error |= IOERR_VANISHED;
925 rprintf(c, "file has vanished: %s\n",
926 full_fname(thisname));
929 io_error |= IOERR_GENERAL;
930 rsyserr(FERROR, save_errno, "readlink %s failed",
931 full_fname(thisname));
936 /* backup.c calls us with filter_level set to NO_FILTERS. */
937 if (filter_level == NO_FILTERS)
940 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
941 rprintf(FINFO, "skipping directory %s\n", thisname);
945 /* -x only affects directories because we need to avoid recursing
946 * into a mount-point directory, not to avoid copying a symlinked
947 * file if -L (or similar) was specified. */
948 if (one_file_system && st.st_dev != filesystem_dev
949 && S_ISDIR(st.st_mode)) {
950 if (one_file_system > 1) {
952 rprintf(FINFO, "skipping mount-point dir %s\n",
957 flags |= FLAG_MOUNT_DIR;
960 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
961 if (ignore_perishable)
962 non_perishable_cnt++;
966 if (lp_ignore_nonreadable(module_id)) {
968 if (!S_ISLNK(st.st_mode))
970 if (access(thisname, R_OK) != 0)
976 /* Only divert a directory in the main transfer. */
977 if (flist && flist->prev && S_ISDIR(st.st_mode)
978 && flags & FLAG_DIVERT_DIRS) {
980 /* Room for parent/sibling/next-child info. */
981 extra_len += 3 * EXTRA_LEN;
985 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
986 who_am_i(), thisname, filter_level);
989 if ((basename = strrchr(thisname, '/')) != NULL) {
990 int len = basename++ - thisname;
991 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
992 lastdir = new_array(char, len + 1);
993 memcpy(lastdir, thisname, len);
999 basename_len = strlen(basename) + 1; /* count the '\0' */
1001 #ifdef SUPPORT_LINKS
1002 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1007 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1008 extra_len += EXTRA_LEN;
1010 #if EXTRA_ROUNDING > 0
1011 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1012 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1015 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1018 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1020 if (!(bp = new_array(char, alloc_len)))
1021 out_of_memory("make_file");
1024 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1026 file = (struct file_struct *)bp;
1027 bp += FILE_STRUCT_LEN;
1029 memcpy(bp, basename, basename_len);
1030 bp += basename_len + linkname_len; /* skip space for symlink too */
1032 #ifdef SUPPORT_HARD_LINKS
1033 if (preserve_hard_links && flist && flist->prev) {
1034 if (protocol_version >= 28
1035 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1036 : S_ISREG(st.st_mode)) {
1037 tmp_dev = st.st_dev;
1038 tmp_ino = st.st_ino;
1044 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1045 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1046 tmp_rdev = st.st_rdev;
1051 file->flags = flags;
1052 file->modtime = st.st_mtime;
1053 file->len32 = (uint32)st.st_size;
1054 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1055 file->flags |= FLAG_LENGTH64;
1056 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1058 file->mode = st.st_mode;
1060 F_OWNER(file) = st.st_uid;
1062 F_GROUP(file) = st.st_gid;
1064 if (basename != thisname)
1065 file->dirname = lastdir;
1067 #ifdef SUPPORT_LINKS
1069 bp = (char*)file->basename + basename_len;
1070 memcpy(bp, linkname, linkname_len);
1074 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1075 file_checksum(thisname, tmp_sum, st.st_size);
1077 F_ROOTDIR(file) = flist_dir;
1079 /* This code is only used by the receiver when it is building
1080 * a list of files for a delete pass. */
1081 if (keep_dirlinks && linkname_len && flist) {
1083 int save_mode = file->mode;
1084 file->mode = S_IFDIR; /* Find a directory with our name. */
1085 if (flist_find(dir_flist, file) >= 0
1086 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1087 file->modtime = st2.st_mtime;
1089 file->mode = st2.st_mode;
1091 F_OWNER(file) = st2.st_uid;
1093 F_GROUP(file) = st2.st_gid;
1095 file->mode = save_mode;
1098 if (basename_len == 0+1)
1101 if (inc_recurse && flist == dir_flist) {
1102 flist_expand(flist);
1103 flist->files[flist->count++] = file;
1109 /* Only called for temporary file_struct entries created by make_file(). */
1110 void unmake_file(struct file_struct *file)
1112 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1113 #if EXTRA_ROUNDING > 0
1114 if (extra_cnt & EXTRA_ROUNDING)
1115 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1117 free(REQ_EXTRA(file, extra_cnt));
1120 static struct file_struct *send_file_name(int f, struct file_list *flist,
1121 char *fname, STRUCT_STAT *stp,
1122 int flags, int filter_flags)
1124 struct file_struct *file;
1126 file = make_file(fname, flist, stp, flags, filter_flags);
1130 if (chmod_modes && !S_ISLNK(file->mode))
1131 file->mode = tweak_mode(file->mode, chmod_modes);
1133 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1135 flist_expand(flist);
1136 flist->files[flist->count++] = file;
1138 send_file_entry(f, file, flist->count - 1);
1142 static void send_if_directory(int f, struct file_list *flist,
1143 struct file_struct *file,
1144 char *fbuf, unsigned int ol,
1147 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1149 if (S_ISDIR(file->mode)
1150 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1152 unsigned int len = strlen(fbuf);
1153 if (len > 1 && fbuf[len-1] == '/')
1155 if (len >= MAXPATHLEN - 1) {
1156 io_error |= IOERR_GENERAL;
1157 rprintf(FERROR, "skipping long-named directory: %s\n",
1161 save_filters = push_local_filters(fbuf, len);
1162 send_directory(f, flist, -1, fbuf, len, flags);
1163 pop_local_filters(save_filters);
1170 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1172 return f_name_cmp(*file1, *file2);
1175 /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1176 * sort them by name, and link them into the tree, setting the appropriate
1177 * parent/child/sibling pointers. */
1178 static void add_dirs_to_tree(int parent_ndx, int start, int end)
1182 int32 *parent_dp = parent_ndx < 0 ? NULL
1183 : F_DIRNODE_P(dir_flist->files[parent_ndx]);
1185 qsort(dir_flist->files + start, end - start + 1,
1186 sizeof dir_flist->files[0], (int (*)())file_compare);
1188 for (i = start; i <= end; i++) {
1189 struct file_struct *file = dir_flist->files[i];
1190 if (!(file->flags & FLAG_XFER_DIR)
1191 || file->flags & FLAG_MOUNT_DIR)
1194 DIR_NEXT_SIBLING(dp) = i;
1196 DIR_FIRST_CHILD(parent_dp) = i;
1199 dp = F_DIRNODE_P(file);
1200 DIR_PARENT(dp) = parent_ndx;
1201 DIR_FIRST_CHILD(dp) = -1;
1204 DIR_NEXT_SIBLING(dp) = -1;
1207 /* This function is normally called by the sender, but the receiving side also
1208 * calls it from get_dirlist() with f set to -1 so that we just construct the
1209 * file list in memory without sending it over the wire. Also, get_dirlist()
1210 * might call this with f set to -2, which also indicates that local filter
1211 * rules should be ignored. */
1212 static void send_directory(int f, struct file_list *flist, int parent_ndx,
1213 char *fbuf, int len, int flags)
1219 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1220 int start = divert_dirs ? dir_flist->count : flist->count;
1221 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1223 assert(flist != NULL);
1225 if (!(d = opendir(fbuf))) {
1226 io_error |= IOERR_GENERAL;
1227 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1232 if (len != 1 || *fbuf != '/')
1235 remainder = MAXPATHLEN - (p - fbuf);
1237 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1238 char *dname = d_name(di);
1239 if (dname[0] == '.' && (dname[1] == '\0'
1240 || (dname[1] == '.' && dname[2] == '\0')))
1242 if (strlcpy(p, dname, remainder) >= remainder) {
1243 io_error |= IOERR_GENERAL;
1245 "cannot send long-named file %s\n",
1250 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
1256 io_error |= IOERR_GENERAL;
1257 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1266 add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1);
1268 int i, end = flist->count - 1;
1269 /* send_if_directory() bumps flist->count, so use "end". */
1270 for (i = start; i <= end; i++)
1271 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1275 void send_extra_file_list(int f, int at_least)
1277 char fbuf[MAXPATHLEN];
1278 struct file_list *flist;
1280 int future_cnt, save_io_error = io_error;
1282 if (send_dir_ndx < 0)
1285 /* Keep sending data until we have the requested number of
1286 * files in the upcoming file-lists. */
1287 if (cur_flist->next) {
1288 flist = first_flist->prev; /* the newest flist */
1289 future_cnt = flist->count
1290 + flist->ndx_start - cur_flist->next->ndx_start;
1293 while (future_cnt < at_least) {
1294 struct file_struct *file = dir_flist->files[send_dir_ndx];
1299 dlen = strlen(fbuf);
1301 if (F_ROOTDIR(file) != flist_dir) {
1302 if (!push_flist_dir(F_ROOTDIR(file), -1))
1303 exit_cleanup(RERR_FILESELECT);
1306 flist = flist_new(0, "send_extra_file_list");
1307 start_write = stats.total_written;
1309 write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx);
1310 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1311 send_directory(f, flist, send_dir_ndx, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1314 clean_flist(flist, 0, 0);
1315 file_total += flist->count;
1316 future_cnt += flist->count;
1317 stats.flist_size += stats.total_written - start_write;
1318 stats.num_files += flist->count;
1320 output_flist(flist);
1322 dp = F_DIRNODE_P(file);
1323 if (DIR_FIRST_CHILD(dp) >= 0) {
1324 send_dir_ndx = DIR_FIRST_CHILD(dp);
1327 while (DIR_NEXT_SIBLING(dp) < 0) {
1328 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1329 write_ndx(f, NDX_FLIST_EOF);
1331 change_local_filter_dir(NULL, 0, 0);
1335 file = dir_flist->files[send_dir_ndx];
1336 dp = F_DIRNODE_P(file);
1338 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1343 if (io_error != save_io_error && !ignore_errors)
1344 send_msg_int(MSG_IO_ERROR, io_error);
1347 struct file_list *send_file_list(int f, int argc, char *argv[])
1352 char lastpath[MAXPATHLEN] = "";
1353 struct file_list *flist;
1354 struct timeval start_tv, end_tv;
1357 int flags, disable_buffering;
1359 rprintf(FLOG, "building file list\n");
1360 if (show_filelist_p())
1361 start_filelist_progress("building file list");
1362 else if (inc_recurse && verbose && !am_server)
1363 rprintf(FCLIENT, "sending incremental file list\n");
1365 start_write = stats.total_written;
1366 gettimeofday(&start_tv, NULL);
1368 #ifdef SUPPORT_HARD_LINKS
1369 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1373 flist = cur_flist = flist_new(0, "send_file_list");
1375 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1376 flags = FLAG_DIVERT_DIRS;
1378 dir_flist = cur_flist;
1382 disable_buffering = io_start_buffering_out(f);
1383 if (filesfrom_fd >= 0) {
1384 if (argv[0] && !push_dir(argv[0], 0)) {
1385 rsyserr(FERROR, errno, "push_dir %s failed",
1386 full_fname(argv[0]));
1387 exit_cleanup(RERR_FILESELECT);
1393 char fbuf[MAXPATHLEN];
1398 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1400 sanitize_path(fbuf, fbuf, "", 0, NULL);
1404 strlcpy(fbuf, *argv++, MAXPATHLEN);
1406 sanitize_path(fbuf, fbuf, "", 0, NULL);
1410 if (relative_paths) {
1411 /* We clean up fbuf below. */
1413 } else if (!len || fbuf[len - 1] == '/') {
1414 if (len == 2 && fbuf[0] == '.') {
1415 /* Turn "./" into just "." rather than "./." */
1418 if (len + 1 >= MAXPATHLEN)
1419 overflow_exit("send_file_list");
1424 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1425 && (len == 2 || fbuf[len-3] == '/')) {
1426 if (len + 2 >= MAXPATHLEN)
1427 overflow_exit("send_file_list");
1433 is_dot_dir = fbuf[len-1] == '.'
1434 && (len == 1 || fbuf[len-2] == '/');
1437 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1438 io_error |= IOERR_GENERAL;
1439 rsyserr(FERROR, errno, "link_stat %s failed",
1444 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1445 rprintf(FINFO, "skipping directory %s\n", fbuf);
1451 if (!relative_paths) {
1452 p = strrchr(fbuf, '/');
1459 len -= p - fbuf + 1;
1464 if ((p = strstr(fbuf, "/./")) != NULL) {
1470 len -= p - fbuf + 3;
1474 /* Get rid of trailing "/" and "/.". */
1476 if (fn[len - 1] == '/') {
1478 if (!--len && !dir) {
1483 else if (len >= 2 && fn[len - 1] == '.'
1484 && fn[len - 2] == '/') {
1486 if (!(len -= 2) && !dir) {
1493 if (len == 1 && fn[0] == '/')
1496 /* Reject a ".." dir in the active part of the path. */
1497 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1498 if ((p[2] == '/' || p[2] == '\0')
1499 && (p == fn || p[-1] == '/')) {
1501 "found \"..\" dir in relative path: %s\n",
1503 exit_cleanup(RERR_SYNTAX);
1514 static const char *lastdir;
1515 static int lastdir_len = -1;
1516 int len = strlen(dir);
1518 if (len != lastdir_len || memcmp(lastdir, dir, len) != 0) {
1519 if (!push_flist_dir(strdup(dir), len))
1521 lastdir = flist_dir;
1522 lastdir_len = flist_dir_len;
1523 } else if (!push_flist_dir(lastdir, lastdir_len)) {
1525 io_error |= IOERR_GENERAL;
1526 rsyserr(FERROR, errno, "push_dir %s failed",
1533 memmove(fbuf, fn, len + 1);
1535 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1536 /* Send the implied directories at the start of the
1537 * source spec, so we get their permissions right. */
1538 char *lp = lastpath, *slash = fbuf;
1540 /* Skip any initial directories in our path that we
1541 * have in common with lastpath. */
1542 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1547 if (fn != p || (*lp && *lp != '/')) {
1548 int save_copy_links = copy_links;
1549 int save_xfer_dirs = xfer_dirs;
1550 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1551 copy_links |= copy_unsafe_links;
1553 while ((slash = strchr(slash+1, '/')) != 0) {
1555 send_file_name(f, flist, fbuf, NULL,
1556 dir_flags, ALL_FILTERS);
1559 copy_links = save_copy_links;
1560 xfer_dirs = save_xfer_dirs;
1562 strlcpy(lastpath, fbuf, sizeof lastpath);
1567 if (one_file_system)
1568 filesystem_dev = st.st_dev;
1570 if (recurse || (xfer_dirs && is_dot_dir)) {
1571 struct file_struct *file;
1572 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR
1573 | (is_dot_dir ? 0 : flags)
1574 | (inc_recurse ? FLAG_DIVERT_DIRS : 0);
1575 file = send_file_name(f, flist, fbuf, &st,
1576 top_flags, ALL_FILTERS);
1577 if (file && !inc_recurse)
1578 send_if_directory(f, flist, file, fbuf, len, flags);
1580 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1583 gettimeofday(&end_tv, NULL);
1584 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1585 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1586 if (stats.flist_buildtime == 0)
1587 stats.flist_buildtime = 1;
1590 write_byte(f, 0); /* Indicate end of file list */
1592 #ifdef SUPPORT_HARD_LINKS
1593 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1597 if (show_filelist_p())
1598 finish_filelist_progress(flist);
1600 gettimeofday(&end_tv, NULL);
1601 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1602 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1604 /* Sort the list without removing any duplicates in non-incremental
1605 * mode. This allows the receiving side to ask for whatever name it
1606 * kept. For incremental mode, the sender also removes duplicates
1607 * in this initial file-list so that it avoids re-sending duplicated
1609 clean_flist(flist, 0, inc_recurse);
1610 file_total += flist->count;
1612 if (!numeric_ids && !inc_recurse)
1615 /* send the io_error flag */
1616 if (protocol_version < 30)
1617 write_int(f, ignore_errors ? 0 : io_error);
1618 else if (io_error && !ignore_errors)
1619 send_msg_int(MSG_IO_ERROR, io_error);
1621 if (disable_buffering)
1622 io_end_buffering_out();
1624 stats.flist_size = stats.total_written - start_write;
1625 stats.num_files = flist->count;
1628 output_flist(flist);
1631 rprintf(FINFO, "send_file_list done\n");
1634 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
1635 if (file_total == 1) {
1636 /* If we're creating incremental file-lists and there
1637 * was just 1 item in the first file-list, send 1 more
1638 * file-list to check if this is a 1-file xfer. */
1639 if (send_dir_ndx < 0)
1640 write_ndx(f, NDX_DONE);
1642 send_extra_file_list(f, 1);
1649 struct file_list *recv_file_list(int f)
1651 struct file_list *flist;
1656 rprintf(FLOG, "receiving file list\n");
1657 if (show_filelist_p())
1658 start_filelist_progress("receiving file list");
1659 else if (inc_recurse && verbose && !am_server && !first_flist)
1660 rprintf(FCLIENT, "receiving incremental file list\n");
1662 start_read = stats.total_read;
1664 flist = flist_new(0, "recv_file_list");
1666 #ifdef SUPPORT_HARD_LINKS
1667 if (preserve_hard_links && protocol_version < 30)
1672 if (flist->ndx_start == 0)
1673 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1674 dstart = dir_flist->count;
1680 while ((flags = read_byte(f)) != 0) {
1681 struct file_struct *file;
1683 flist_expand(flist);
1685 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1686 flags |= read_byte(f) << 8;
1687 file = recv_file_entry(flist, flags, f);
1689 if (inc_recurse && S_ISDIR(file->mode)) {
1690 flist_expand(dir_flist);
1691 dir_flist->files[dir_flist->count++] = file;
1694 flist->files[flist->count++] = file;
1696 maybe_emit_filelist_progress(flist->count);
1699 rprintf(FINFO, "recv_file_name(%s)\n",
1700 f_name(file, NULL));
1703 file_total += flist->count;
1706 rprintf(FINFO, "received %d names\n", flist->count);
1708 if (show_filelist_p())
1709 finish_filelist_progress(flist);
1711 clean_flist(flist, relative_paths, 1);
1714 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
1715 sizeof dir_flist->files[0], (int (*)())file_compare);
1717 recv_uid_list(f, flist);
1719 if (protocol_version < 30) {
1720 /* Recv the io_error flag */
1724 io_error |= read_int(f);
1728 output_flist(flist);
1732 for (i = 0; i < flist->count; i++)
1733 list_file_entry(flist->files[i]);
1737 rprintf(FINFO, "recv_file_list done\n");
1739 stats.flist_size += stats.total_read - start_read;
1740 stats.num_files += flist->count;
1745 /* This is only used once by the receiver if the very first file-list
1746 * has exactly one item in it. */
1747 void recv_additional_file_list(int f)
1749 struct file_list *flist;
1750 int ndx = read_ndx(f);
1751 if (ndx == NDX_DONE) {
1753 change_local_filter_dir(NULL, 0, 0);
1755 ndx = NDX_FLIST_OFFSET - ndx;
1756 if (ndx < 0 || ndx >= dir_flist->count) {
1757 ndx = NDX_FLIST_OFFSET - ndx;
1759 "Invalid dir index: %d (%d - %d)\n",
1760 ndx, NDX_FLIST_OFFSET,
1761 NDX_FLIST_OFFSET - dir_flist->count);
1762 exit_cleanup(RERR_PROTOCOL);
1764 flist = recv_file_list(f);
1765 flist->parent_ndx = ndx;
1769 /* Search for an identically-named item in the file list. Note that the
1770 * items must agree in their directory-ness, or no match is returned. */
1771 int flist_find(struct file_list *flist, struct file_struct *f)
1773 int low = flist->low, high = flist->high;
1774 int diff, mid, mid_up;
1776 while (low <= high) {
1777 mid = (low + high) / 2;
1778 if (F_IS_ACTIVE(flist->files[mid]))
1781 /* Scan for the next non-empty entry using the cached
1782 * distance values. If the value isn't fully up-to-
1783 * date, update it. */
1784 mid_up = mid + F_DEPTH(flist->files[mid]);
1785 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1787 mid_up += F_DEPTH(flist->files[mid_up]);
1788 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1789 F_DEPTH(flist->files[mid]) = mid_up - mid;
1791 if (mid_up > high) {
1792 /* If there's nothing left above us, set high to
1793 * a non-empty entry below us and continue. */
1794 high = mid - (int)flist->files[mid]->len32;
1795 if (!F_IS_ACTIVE(flist->files[high])) {
1797 high -= (int)flist->files[high]->len32;
1798 } while (!F_IS_ACTIVE(flist->files[high]));
1799 flist->files[mid]->len32 = mid - high;
1804 diff = f_name_cmp(flist->files[mid_up], f);
1806 if (protocol_version < 29
1807 && S_ISDIR(flist->files[mid_up]->mode)
1808 != S_ISDIR(f->mode))
1821 * Free up any resources a file_struct has allocated
1822 * and clear the file.
1824 void clear_file(struct file_struct *file)
1826 /* The +1 zeros out the first char of the basename. */
1827 memset(file, 0, FILE_STRUCT_LEN + 1);
1828 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1829 * entry. Likewise for len32 in the opposite direction. We assume
1830 * that we're alone for now since flist_find() will adjust the counts
1831 * it runs into that aren't up-to-date. */
1832 file->len32 = F_DEPTH(file) = 1;
1835 /* Allocate a new file list. */
1836 struct file_list *flist_new(int flags, char *msg)
1838 struct file_list *flist;
1840 flist = new(struct file_list);
1844 memset(flist, 0, sizeof flist[0]);
1846 if (!(flags & FLIST_TEMP)) {
1848 flist->ndx_start = first_flist->prev->ndx_start
1849 + first_flist->prev->count;
1851 /* This is a doubly linked list with prev looping back to
1852 * the end of the list, but the last next pointer is NULL. */
1854 first_flist = cur_flist = flist->prev = flist;
1856 flist->prev = first_flist->prev;
1857 flist->prev->next = first_flist->prev = flist;
1862 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1868 /* Free up all elements in a flist. */
1869 void flist_free(struct file_list *flist)
1872 ; /* Was FLIST_TEMP dir-list. */
1873 else if (flist == flist->prev) {
1874 first_flist = cur_flist = NULL;
1878 if (flist == cur_flist)
1879 cur_flist = flist->next;
1880 if (flist == first_flist)
1881 first_flist = first_flist->next;
1883 flist->prev->next = flist->next;
1885 flist->next = first_flist;
1887 flist->next->prev = flist->prev;
1888 file_total -= flist->count;
1892 pool_destroy(flist->file_pool);
1898 * This routine ensures we don't have any duplicate names in our file list.
1899 * duplicate names can cause corruption because of the pipelining
1901 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1903 char fbuf[MAXPATHLEN];
1908 if (flist->count == 0) {
1913 qsort(flist->files, flist->count,
1914 sizeof flist->files[0], (int (*)())file_compare);
1916 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1917 if (F_IS_ACTIVE(flist->files[i])) {
1922 flist->low = prev_i;
1923 while (++i < flist->count) {
1925 struct file_struct *file = flist->files[i];
1927 if (!F_IS_ACTIVE(file))
1929 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1931 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1932 int save_mode = file->mode;
1933 /* Make sure that this directory doesn't duplicate a
1934 * non-directory earlier in the list. */
1935 flist->high = prev_i;
1936 file->mode = S_IFREG;
1937 j = flist_find(flist, file);
1938 file->mode = save_mode;
1942 struct file_struct *fp = flist->files[j];
1944 /* If one is a dir and the other is not, we want to
1945 * keep the dir because it might have contents in the
1947 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1948 if (S_ISDIR(file->mode))
1952 } else if (protocol_version < 27)
1956 if (verbose > 1 && !am_server) {
1958 "removing duplicate name %s from file list (%d)\n",
1959 f_name(file, fbuf), drop);
1961 /* Make sure we don't lose track of a user-specified
1963 flist->files[keep]->flags |= flist->files[drop]->flags
1964 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1966 clear_file(flist->files[drop]);
1969 if (flist->low == drop) {
1971 j < i && !F_IS_ACTIVE(flist->files[j]);
1980 flist->high = no_dups ? prev_i : flist->count - 1;
1983 /* We need to strip off the leading slashes for relative
1984 * paths, but this must be done _after_ the sorting phase. */
1985 for (i = flist->low; i <= flist->high; i++) {
1986 struct file_struct *file = flist->files[i];
1990 while (*file->dirname == '/')
1992 if (!*file->dirname)
1993 file->dirname = NULL;
1997 if (prune_empty_dirs && no_dups) {
1998 int j, prev_depth = 0;
2000 prev_i = 0; /* It's OK that this isn't really true. */
2002 for (i = flist->low; i <= flist->high; i++) {
2003 struct file_struct *fp, *file = flist->files[i];
2005 /* This temporarily abuses the F_DEPTH() value for a
2006 * directory that is in a chain that might get pruned.
2007 * We restore the old value if it gets a reprieve. */
2008 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2009 /* Dump empty dirs when coming back down. */
2010 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2011 fp = flist->files[prev_i];
2012 if (F_DEPTH(fp) >= 0)
2014 prev_i = -F_DEPTH(fp)-1;
2017 prev_depth = F_DEPTH(file);
2018 if (is_excluded(f_name(file, fbuf), 1,
2020 /* Keep dirs through this dir. */
2021 for (j = prev_depth-1; ; j--) {
2022 fp = flist->files[prev_i];
2023 if (F_DEPTH(fp) >= 0)
2025 prev_i = -F_DEPTH(fp)-1;
2029 F_DEPTH(file) = -prev_i-1;
2032 /* Keep dirs through this non-dir. */
2033 for (j = prev_depth; ; j--) {
2034 fp = flist->files[prev_i];
2035 if (F_DEPTH(fp) >= 0)
2037 prev_i = -F_DEPTH(fp)-1;
2042 /* Dump all remaining empty dirs. */
2044 struct file_struct *fp = flist->files[prev_i];
2045 if (F_DEPTH(fp) >= 0)
2047 prev_i = -F_DEPTH(fp)-1;
2051 for (i = flist->low; i <= flist->high; i++) {
2052 if (F_IS_ACTIVE(flist->files[i]))
2056 for (i = flist->high; i >= flist->low; i--) {
2057 if (F_IS_ACTIVE(flist->files[i]))
2064 static void output_flist(struct file_list *flist)
2066 char uidbuf[16], gidbuf[16], depthbuf[16];
2067 struct file_struct *file;
2068 const char *root, *dir, *slash, *name, *trail;
2069 const char *who = who_am_i();
2072 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2073 who, flist->ndx_start, flist->count, flist->low, flist->high);
2074 for (i = 0; i < flist->count; i++) {
2075 file = flist->files[i];
2076 if ((am_root || am_sender) && preserve_uid) {
2077 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
2081 if (preserve_gid && F_GID(file) != GID_NONE) {
2082 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
2087 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2088 if (F_IS_ACTIVE(file)) {
2089 root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
2090 if ((dir = file->dirname) == NULL)
2094 name = file->basename;
2095 trail = S_ISDIR(file->mode) ? "/" : "";
2097 root = dir = slash = name = trail = "";
2098 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2099 who, i, root, dir, slash, name, trail, (int)file->mode,
2100 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
2104 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2105 enum fnc_type { t_PATH, t_ITEM };
2107 /* Compare the names of two file_struct entities, similar to how strcmp()
2108 * would do if it were operating on the joined strings.
2110 * Some differences beginning with protocol_version 29: (1) directory names
2111 * are compared with an assumed trailing slash so that they compare in a
2112 * way that would cause them to sort immediately prior to any content they
2113 * may have; (2) a directory of any name compares after a non-directory of
2114 * any name at the same depth; (3) a directory with name "." compares prior
2115 * to anything else. These changes mean that a directory and a non-dir
2116 * with the same name will not compare as equal (protocol_version >= 29).
2118 * The dirname component can be an empty string, but the basename component
2119 * cannot (and never is in the current codebase). The basename component
2120 * may be NULL (for a removed item), in which case it is considered to be
2121 * after any existing item. */
2122 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
2125 const uchar *c1, *c2;
2126 enum fnc_state state1, state2;
2127 enum fnc_type type1, type2;
2128 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2130 if (!f1 || !F_IS_ACTIVE(f1)) {
2131 if (!f2 || !F_IS_ACTIVE(f2))
2135 if (!f2 || !F_IS_ACTIVE(f2))
2138 c1 = (uchar*)f1->dirname;
2139 c2 = (uchar*)f2->dirname;
2143 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2144 c1 = (const uchar*)f1->basename;
2145 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2147 state1 = s_TRAILING;
2156 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2157 c2 = (const uchar*)f2->basename;
2158 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2160 state2 = s_TRAILING;
2170 return type1 == t_PATH ? 1 : -1;
2180 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2181 c1 = (const uchar*)f1->basename;
2182 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2184 state1 = s_TRAILING;
2190 state1 = s_TRAILING;
2191 if (type1 == t_PATH) {
2200 if (*c2 && type1 != type2)
2201 return type1 == t_PATH ? 1 : -1;
2210 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2211 c2 = (const uchar*)f2->basename;
2212 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2214 state2 = s_TRAILING;
2220 state2 = s_TRAILING;
2221 if (type2 == t_PATH) {
2233 return type1 == t_PATH ? 1 : -1;
2235 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2240 char *f_name_buf(void)
2242 static char names[5][MAXPATHLEN];
2243 static unsigned int n;
2245 n = (n + 1) % (sizeof names / sizeof names[0]);
2250 /* Return a copy of the full filename of a flist entry, using the indicated
2251 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2252 * done because we checked the size when creating the file_struct entry.
2254 char *f_name(struct file_struct *f, char *fbuf)
2256 if (!f || !F_IS_ACTIVE(f))
2260 fbuf = f_name_buf();
2263 int len = strlen(f->dirname);
2264 memcpy(fbuf, f->dirname, len);
2266 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2268 strlcpy(fbuf, f->basename, MAXPATHLEN);
2273 /* Do a non-recursive scan of the named directory, possibly ignoring all
2274 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2275 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2276 * buffer (the functions we call will append names onto the end, but the old
2277 * dir value will be restored on exit). */
2278 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2280 struct file_list *dirlist;
2281 char dirbuf[MAXPATHLEN];
2282 int save_recurse = recurse;
2283 int save_xfer_dirs = xfer_dirs;
2286 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2287 if (dlen >= MAXPATHLEN)
2292 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2296 send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0);
2297 xfer_dirs = save_xfer_dirs;
2298 recurse = save_recurse;
2300 flist_count_offset += dirlist->count;
2302 clean_flist(dirlist, 0, 0);
2305 output_flist(dirlist);