2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
33 extern int inc_recurse;
34 extern int do_progress;
35 extern int always_checksum;
37 extern int ignore_errors;
38 extern int numeric_ids;
41 extern int filesfrom_fd;
42 extern int one_file_system;
43 extern int copy_dirlinks;
44 extern int keep_dirlinks;
45 extern int preserve_acls;
46 extern int preserve_links;
47 extern int preserve_hard_links;
48 extern int preserve_devices;
49 extern int preserve_specials;
50 extern int preserve_uid;
51 extern int preserve_gid;
52 extern int relative_paths;
53 extern int implied_dirs;
54 extern int file_extra_cnt;
55 extern int ignore_perishable;
56 extern int non_perishable_cnt;
57 extern int prune_empty_dirs;
58 extern int copy_links;
59 extern int copy_unsafe_links;
60 extern int protocol_version;
61 extern int sanitize_paths;
62 extern struct stats stats;
64 extern char curr_dir[MAXPATHLEN];
66 extern struct chmod_mode_struct *chmod_modes;
68 extern struct filter_list_struct filter_list;
69 extern struct filter_list_struct server_filter_list;
73 dev_t filesystem_dev; /* used to implement -x */
75 struct file_list *cur_flist, *first_flist, *dir_flist;
76 int send_dir_ndx = -1, send_dir_depth = 0;
77 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
78 int file_total = 0; /* total of all active items over all file-lists */
79 int flist_eof = 0; /* all the file-lists are now known */
81 /* The tmp_* vars are used as a cache area by make_file() to store data
82 * that the sender doesn't need to remember in its file list. The data
83 * will survive just long enough to be used by send_file_entry(). */
84 static dev_t tmp_rdev;
85 #ifdef SUPPORT_HARD_LINKS
86 static int64 tmp_dev, tmp_ino;
88 static char tmp_sum[MD4_SUM_LENGTH];
90 static char empty_sum[MD4_SUM_LENGTH];
91 static int flist_count_offset; /* for --delete --progress */
93 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
94 static void output_flist(struct file_list *flist);
99 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
100 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
102 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
105 static int show_filelist_p(void)
107 return verbose && xfer_dirs && !am_server && !inc_recurse;
110 static void start_filelist_progress(char *kind)
112 rprintf(FCLIENT, "%s ... ", kind);
113 if (verbose > 1 || do_progress)
114 rprintf(FCLIENT, "\n");
118 static void emit_filelist_progress(int count)
120 rprintf(FCLIENT, " %d files...\r", count);
123 static void maybe_emit_filelist_progress(int count)
125 if (do_progress && show_filelist_p() && (count % 100) == 0)
126 emit_filelist_progress(count);
129 static void finish_filelist_progress(const struct file_list *flist)
132 /* This overwrites the progress line */
133 rprintf(FINFO, "%d file%sto consider\n",
134 flist->count, flist->count == 1 ? " " : "s ");
136 rprintf(FINFO, "done\n");
139 void show_flist_stats(void)
144 static void list_file_entry(struct file_struct *f)
146 char permbuf[PERMSTRING_SIZE];
149 if (!F_IS_ACTIVE(f)) {
150 /* this can happen if duplicate names were removed */
154 permstring(permbuf, f->mode);
157 /* TODO: indicate '+' if the entry has an ACL. */
160 if (preserve_links && S_ISLNK(f->mode)) {
161 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
162 permbuf, len, timestring(f->modtime),
163 f_name(f, NULL), F_SYMLINK(f));
167 rprintf(FINFO, "%s %11.0f %s %s\n",
168 permbuf, len, timestring(f->modtime),
173 /* Stat either a symlink or its referent, depending on the settings of
174 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
176 * If path is the name of a symlink, then the linkbuf buffer (which must hold
177 * MAXPATHLEN chars) will be set to the symlink's target string.
179 * The stat structure pointed to by stp will contain information about the
180 * link or the referent as appropriate, if they exist. */
181 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
184 if (link_stat(path, stp, copy_dirlinks) < 0)
186 if (S_ISLNK(stp->st_mode)) {
187 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
190 linkbuf[llen] = '\0';
191 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
193 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
196 return do_stat(path, stp);
201 return do_stat(path, stp);
205 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
209 return do_stat(path, stp);
210 if (do_lstat(path, stp) < 0)
212 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
214 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
219 return do_stat(path, stp);
223 /* This function is used to check if a file should be included/excluded
224 * from the list of files based on its name and type etc. The value of
225 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
226 static int is_excluded(char *fname, int is_dir, int filter_level)
228 #if 0 /* This currently never happens, so avoid a useless compare. */
229 if (filter_level == NO_FILTERS)
233 /* never exclude '.', even if somebody does --exclude '*' */
234 if (fname[0] == '.' && !fname[1])
236 /* Handle the -R version of the '.' dir. */
237 if (fname[0] == '/') {
238 int len = strlen(fname);
239 if (fname[len-1] == '.' && fname[len-2] == '/')
243 if (server_filter_list.head
244 && check_filter(&server_filter_list, fname, is_dir) < 0)
246 if (filter_level != ALL_FILTERS)
249 && check_filter(&filter_list, fname, is_dir) < 0)
254 static int to_wire_mode(mode_t mode)
257 #if _S_IFLNK != 0120000
259 return (mode & ~(_S_IFMT)) | 0120000;
265 static mode_t from_wire_mode(int mode)
267 #if _S_IFLNK != 0120000
268 if ((mode & (_S_IFMT)) == 0120000)
269 return (mode & ~(_S_IFMT)) | _S_IFLNK;
274 static void send_directory(int f, struct file_list *flist, int ndx,
275 char *fbuf, int len, int flags);
277 static const char *flist_dir, *orig_dir;
278 static int flist_dir_len;
282 * Make sure @p flist is big enough to hold at least @p flist->count
285 void flist_expand(struct file_list *flist)
287 struct file_struct **new_ptr;
289 if (flist->count < flist->malloced)
292 if (flist->malloced < FLIST_START)
293 flist->malloced = FLIST_START;
294 else if (flist->malloced >= FLIST_LINEAR)
295 flist->malloced += FLIST_LINEAR;
297 flist->malloced *= 2;
300 * In case count jumped or we are starting the list
301 * with a known size just set it.
303 if (flist->malloced < flist->count)
304 flist->malloced = flist->count;
306 new_ptr = realloc_array(flist->files, struct file_struct *,
309 if (verbose >= 2 && flist->malloced != FLIST_START) {
310 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
312 (double)sizeof flist->files[0] * flist->malloced,
313 (new_ptr == flist->files) ? " not" : "");
316 flist->files = new_ptr;
319 out_of_memory("flist_expand");
322 int push_flist_dir(const char *dir, int len)
324 if (dir == flist_dir)
328 orig_dir = strdup(curr_dir);
330 if (flist_dir && !pop_dir(orig_dir)) {
331 rsyserr(FERROR, errno, "pop_dir %s failed",
332 full_fname(orig_dir));
333 exit_cleanup(RERR_FILESELECT);
336 if (dir && !push_dir(dir, 0)) {
337 io_error |= IOERR_GENERAL;
338 rsyserr(FERROR, errno, "push_dir %s failed",
344 flist_dir_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
349 static void send_file_entry(int f, struct file_struct *file, int ndx)
351 static time_t modtime;
355 static uint32 rdev_major;
358 static char *user_name, *group_name;
359 static char lastname[MAXPATHLEN];
360 char fname[MAXPATHLEN];
361 int first_hlink_ndx = -1;
367 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
369 if (file->mode == mode)
370 flags |= XMIT_SAME_MODE;
373 if ((preserve_devices && IS_DEVICE(mode))
374 || (preserve_specials && IS_SPECIAL(mode))) {
375 if (protocol_version < 28) {
376 if (tmp_rdev == rdev)
377 flags |= XMIT_SAME_RDEV_pre28;
382 if ((uint32)major(rdev) == rdev_major)
383 flags |= XMIT_SAME_RDEV_MAJOR;
385 rdev_major = major(rdev);
386 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
387 flags |= XMIT_RDEV_MINOR_8_pre30;
389 } else if (protocol_version < 28)
390 rdev = MAKEDEV(0, 0);
392 if (F_UID(file) == uid && *lastname)
393 flags |= XMIT_SAME_UID;
396 if (preserve_uid && !numeric_ids) {
397 user_name = add_uid(uid);
398 if (inc_recurse && user_name)
399 flags |= XMIT_USER_NAME_FOLLOWS;
404 if (F_GID(file) == gid && *lastname)
405 flags |= XMIT_SAME_GID;
408 if (preserve_gid && !numeric_ids) {
409 group_name = add_gid(gid);
410 if (inc_recurse && group_name)
411 flags |= XMIT_GROUP_NAME_FOLLOWS;
415 if (file->modtime == modtime)
416 flags |= XMIT_SAME_TIME;
418 modtime = file->modtime;
420 #ifdef SUPPORT_HARD_LINKS
422 if (protocol_version >= 30) {
423 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
424 first_hlink_ndx = (int32)(long)np->data - 1;
425 if (first_hlink_ndx < 0) {
426 np->data = (void*)(long)(ndx + 1);
427 flags |= XMIT_HLINK_FIRST;
429 flags |= XMIT_HLINKED;
431 if (tmp_dev == dev) {
432 if (protocol_version >= 28)
433 flags |= XMIT_SAME_DEV_pre30;
436 flags |= XMIT_HLINKED;
442 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
444 l2 = strlen(fname+l1);
447 flags |= XMIT_SAME_NAME;
449 flags |= XMIT_LONG_NAME;
451 /* We must make sure we don't send a zero flag byte or the
452 * other end will terminate the flist transfer. Note that
453 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
454 * it's harmless way to add a bit to the first flag byte. */
455 if (protocol_version >= 28) {
456 if (!flags && !S_ISDIR(mode))
457 flags |= XMIT_TOP_DIR;
458 if ((flags & 0xFF00) || !flags) {
459 flags |= XMIT_EXTENDED_FLAGS;
460 write_shortint(f, flags);
462 write_byte(f, flags);
465 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
466 write_byte(f, flags);
468 if (flags & XMIT_SAME_NAME)
470 if (flags & XMIT_LONG_NAME)
471 write_abbrevint30(f, l2);
474 write_buf(f, fname + l1, l2);
476 if (first_hlink_ndx >= 0) {
477 write_abbrevint30(f, first_hlink_ndx);
481 write_longint(f, F_LENGTH(file));
482 if (!(flags & XMIT_SAME_TIME))
483 write_int(f, modtime);
484 if (!(flags & XMIT_SAME_MODE))
485 write_int(f, to_wire_mode(mode));
486 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
487 if (protocol_version < 30)
490 write_abbrevint(f, uid);
491 if (flags & XMIT_USER_NAME_FOLLOWS) {
492 int len = strlen(user_name);
494 write_buf(f, user_name, len);
498 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
499 if (protocol_version < 30)
502 write_abbrevint(f, gid);
503 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
504 int len = strlen(group_name);
506 write_buf(f, group_name, len);
510 if ((preserve_devices && IS_DEVICE(mode))
511 || (preserve_specials && IS_SPECIAL(mode))) {
512 if (protocol_version < 28) {
513 if (!(flags & XMIT_SAME_RDEV_pre28))
514 write_int(f, (int)rdev);
516 if (!(flags & XMIT_SAME_RDEV_MAJOR))
517 write_abbrevint30(f, major(rdev));
518 if (protocol_version >= 30)
519 write_abbrevint(f, minor(rdev));
520 else if (flags & XMIT_RDEV_MINOR_8_pre30)
521 write_byte(f, minor(rdev));
523 write_int(f, minor(rdev));
528 if (preserve_links && S_ISLNK(mode)) {
529 const char *sl = F_SYMLINK(file);
530 int len = strlen(sl);
531 write_abbrevint30(f, len);
532 write_buf(f, sl, len);
536 #ifdef SUPPORT_HARD_LINKS
537 if (tmp_dev != 0 && protocol_version < 30) {
538 if (protocol_version < 26) {
539 /* 32-bit dev_t and ino_t */
540 write_int(f, (int32)dev);
541 write_int(f, (int32)tmp_ino);
543 /* 64-bit dev_t and ino_t */
544 if (!(flags & XMIT_SAME_DEV_pre30))
545 write_longint(f, dev);
546 write_longint(f, tmp_ino);
551 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
556 /* Prior to 28, we sent a useless set of nulls. */
559 write_buf(f, sum, checksum_len);
563 strlcpy(lastname, fname, MAXPATHLEN);
565 if (S_ISREG(mode) || S_ISLNK(mode))
566 stats.total_size += F_LENGTH(file);
569 static struct file_struct *recv_file_entry(struct file_list *flist,
572 static time_t modtime;
576 static uint32 rdev_major;
579 static char lastname[MAXPATHLEN], *lastdir;
580 static int lastdir_depth, lastdir_len = -1;
581 static unsigned int del_hier_name_len = 0;
582 static int in_del_hier = 0;
583 char thisname[MAXPATHLEN];
584 unsigned int l1 = 0, l2 = 0;
585 int alloc_len, basename_len, linkname_len;
586 int extra_len = file_extra_cnt * EXTRA_LEN;
587 int first_hlink_ndx = -1;
589 const char *basename;
591 struct file_struct *file;
593 if (flags & XMIT_SAME_NAME)
596 if (flags & XMIT_LONG_NAME)
597 l2 = read_abbrevint30(f);
601 if (l2 >= MAXPATHLEN - l1) {
603 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
604 flags, l1, l2, lastname, who_am_i());
605 overflow_exit("recv_file_entry");
608 strlcpy(thisname, lastname, l1 + 1);
609 read_sbuf(f, &thisname[l1], l2);
610 thisname[l1 + l2] = 0;
612 strlcpy(lastname, thisname, MAXPATHLEN);
614 clean_fname(thisname, 0);
617 sanitize_path(thisname, thisname, "", 0, NULL);
619 if ((basename = strrchr(thisname, '/')) != NULL) {
620 int len = basename++ - thisname;
621 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
622 lastdir = new_array(char, len + 1);
623 memcpy(lastdir, thisname, len);
626 lastdir_depth = count_dir_elements(lastdir);
630 basename_len = strlen(basename) + 1; /* count the '\0' */
632 #ifdef SUPPORT_HARD_LINKS
633 if (protocol_version >= 30
634 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
635 struct file_struct *first;
636 first_hlink_ndx = read_abbrevint30(f);
637 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
639 "hard-link reference out of range: %d (%d)\n",
640 first_hlink_ndx, flist->count);
641 exit_cleanup(RERR_PROTOCOL);
643 first = flist->files[first_hlink_ndx];
644 file_length = F_LENGTH(first);
645 modtime = first->modtime;
651 if ((preserve_devices && IS_DEVICE(mode))
652 || (preserve_specials && IS_SPECIAL(mode))) {
653 uint32 *devp = F_RDEV_P(first);
654 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
655 extra_len += 2 * EXTRA_LEN;
657 if (preserve_links && S_ISLNK(mode))
658 linkname_len = strlen(F_SYMLINK(first)) + 1;
665 file_length = read_longint(f);
666 if (!(flags & XMIT_SAME_TIME))
667 modtime = (time_t)read_int(f);
668 if (!(flags & XMIT_SAME_MODE))
669 mode = from_wire_mode(read_int(f));
671 if (chmod_modes && !S_ISLNK(mode))
672 mode = tweak_mode(mode, chmod_modes);
674 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
675 if (protocol_version < 30)
676 uid = (uid_t)read_int(f);
678 uid = (uid_t)read_abbrevint(f);
679 if (flags & XMIT_USER_NAME_FOLLOWS)
680 uid = recv_user_name(f, uid);
681 else if (inc_recurse && am_root && !numeric_ids)
682 uid = match_uid(uid);
685 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
686 if (protocol_version < 30)
687 gid = (gid_t)read_int(f);
689 gid = (gid_t)read_abbrevint(f);
690 if (flags & XMIT_GROUP_NAME_FOLLOWS)
691 gid = recv_group_name(f, gid);
692 else if (inc_recurse && (!am_root || !numeric_ids))
693 gid = match_gid(gid);
697 if ((preserve_devices && IS_DEVICE(mode))
698 || (preserve_specials && IS_SPECIAL(mode))) {
699 if (protocol_version < 28) {
700 if (!(flags & XMIT_SAME_RDEV_pre28))
701 rdev = (dev_t)read_int(f);
704 if (!(flags & XMIT_SAME_RDEV_MAJOR))
705 rdev_major = read_abbrevint30(f);
706 if (protocol_version >= 30)
707 rdev_minor = read_abbrevint(f);
708 else if (flags & XMIT_RDEV_MINOR_8_pre30)
709 rdev_minor = read_byte(f);
711 rdev_minor = read_int(f);
712 rdev = MAKEDEV(rdev_major, rdev_minor);
714 extra_len += 2 * EXTRA_LEN;
716 } else if (protocol_version < 28)
717 rdev = MAKEDEV(0, 0);
720 if (preserve_links && S_ISLNK(mode)) {
721 linkname_len = read_abbrevint30(f) + 1; /* count the '\0' */
722 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
723 rprintf(FERROR, "overflow: linkname_len=%d\n",
725 overflow_exit("recv_file_entry");
732 #ifdef SUPPORT_HARD_LINKS
734 if (preserve_hard_links) {
735 if (protocol_version < 28 && S_ISREG(mode))
736 flags |= XMIT_HLINKED;
737 if (flags & XMIT_HLINKED)
738 extra_len += EXTRA_LEN;
743 /* We need one or two index int32s when we're preserving ACLs. */
745 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
748 if (always_checksum && S_ISREG(mode))
749 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
751 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
752 extra_len += EXTRA_LEN;
754 #if EXTRA_ROUNDING > 0
755 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
756 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
759 if (inc_recurse && S_ISDIR(mode)) {
760 if (one_file_system) {
761 /* Room to save the dir's device for -x */
762 extra_len += 2 * EXTRA_LEN;
767 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
769 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
771 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
773 file = (struct file_struct *)bp;
774 bp += FILE_STRUCT_LEN;
776 memcpy(bp, basename, basename_len);
777 bp += basename_len + linkname_len; /* skip space for symlink too */
779 #ifdef SUPPORT_HARD_LINKS
780 if (flags & XMIT_HLINKED)
781 file->flags |= FLAG_HLINKED;
783 file->modtime = modtime;
784 file->len32 = (uint32)file_length;
785 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
786 file->flags |= FLAG_LENGTH64;
787 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
795 if (basename != thisname) {
796 file->dirname = lastdir;
797 F_DEPTH(file) = lastdir_depth + 1;
802 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
804 if (flags & XMIT_TOP_DIR) {
805 in_del_hier = recurse;
806 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
807 if (relative_paths && del_hier_name_len > 2
808 && lastname[del_hier_name_len-1] == '.'
809 && lastname[del_hier_name_len-2] == '/')
810 del_hier_name_len -= 2;
811 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
812 } else if (in_del_hier) {
813 if (!relative_paths || !del_hier_name_len
814 || (l1 >= del_hier_name_len
815 && lastname[del_hier_name_len] == '/'))
816 file->flags |= FLAG_XFER_DIR;
822 if ((preserve_devices && IS_DEVICE(mode))
823 || (preserve_specials && IS_SPECIAL(mode))) {
824 uint32 *devp = F_RDEV_P(file);
825 DEV_MAJOR(devp) = major(rdev);
826 DEV_MINOR(devp) = minor(rdev);
831 bp = (char*)file->basename + basename_len;
832 if (first_hlink_ndx >= 0) {
833 struct file_struct *first = flist->files[first_hlink_ndx];
834 memcpy(bp, F_SYMLINK(first), linkname_len);
836 read_sbuf(f, bp, linkname_len - 1);
838 sanitize_path(bp, bp, "", lastdir_depth, NULL);
842 #ifdef SUPPORT_HARD_LINKS
843 if (preserve_hard_links && flags & XMIT_HLINKED) {
844 if (protocol_version >= 30) {
845 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
846 ? flist->count : first_hlink_ndx;
848 static int32 cnt = 0;
849 struct idev_node *np;
852 if (protocol_version < 26) {
856 if (!(flags & XMIT_SAME_DEV_pre30))
857 dev = read_longint(f);
858 ino = read_longint(f);
860 np = idev_node(dev, ino);
861 ndx = (int32)(long)np->data - 1;
864 np->data = (void*)(long)cnt;
866 F_HL_GNUM(file) = ndx;
871 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
873 bp = (char*)F_SUM(file);
875 /* Prior to 28, we get a useless set of nulls. */
878 if (first_hlink_ndx >= 0) {
879 struct file_struct *first = flist->files[first_hlink_ndx];
880 memcpy(bp, F_SUM(first), checksum_len);
882 read_buf(f, bp, checksum_len);
886 if (preserve_acls && !S_ISLNK(mode))
887 receive_acl(file, f);
890 if (S_ISREG(mode) || S_ISLNK(mode))
891 stats.total_size += file_length;
897 * Create a file_struct for a named file by reading its stat()
898 * information and performing extensive checks against global
901 * @return the new file, or NULL if there was an error or this file
902 * should be excluded.
904 * @todo There is a small optimization opportunity here to avoid
905 * stat()ing the file in some circumstances, which has a certain cost.
906 * We are called immediately after doing readdir(), and so we may
907 * already know the d_type of the file. We could for example avoid
908 * statting directories if we're not recursing, but this is not a very
909 * important case. Some systems may not have d_type.
911 struct file_struct *make_file(const char *fname, struct file_list *flist,
912 STRUCT_STAT *stp, int flags, int filter_level)
914 static char *lastdir;
915 static int lastdir_len = -1;
916 struct file_struct *file;
918 char thisname[MAXPATHLEN];
919 char linkname[MAXPATHLEN];
920 int alloc_len, basename_len, linkname_len;
921 int extra_len = file_extra_cnt * EXTRA_LEN;
922 const char *basename;
925 if (strlcpy(thisname, fname, sizeof thisname)
926 >= sizeof thisname - flist_dir_len) {
927 rprintf(FINFO, "skipping overly long name: %s\n", fname);
930 clean_fname(thisname, 0);
932 sanitize_path(thisname, thisname, "", 0, NULL);
934 if (stp && S_ISDIR(stp->st_mode)) {
935 st = *stp; /* Needed for "symlink/." with --relative. */
936 *linkname = '\0'; /* make IBM code checker happy */
937 } else if (readlink_stat(thisname, &st, linkname) != 0) {
938 int save_errno = errno;
939 /* See if file is excluded before reporting an error. */
940 if (filter_level != NO_FILTERS
941 && (is_excluded(thisname, 0, filter_level)
942 || is_excluded(thisname, 1, filter_level))) {
943 if (ignore_perishable && save_errno != ENOENT)
944 non_perishable_cnt++;
947 if (save_errno == ENOENT) {
949 /* Avoid "vanished" error if symlink points nowhere. */
950 if (copy_links && do_lstat(thisname, &st) == 0
951 && S_ISLNK(st.st_mode)) {
952 io_error |= IOERR_GENERAL;
953 rprintf(FERROR, "symlink has no referent: %s\n",
954 full_fname(thisname));
958 enum logcode c = am_daemon && protocol_version < 28
960 io_error |= IOERR_VANISHED;
961 rprintf(c, "file has vanished: %s\n",
962 full_fname(thisname));
965 io_error |= IOERR_GENERAL;
966 rsyserr(FERROR, save_errno, "readlink %s failed",
967 full_fname(thisname));
972 /* backup.c calls us with filter_level set to NO_FILTERS. */
973 if (filter_level == NO_FILTERS)
976 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
977 rprintf(FINFO, "skipping directory %s\n", thisname);
981 /* -x only affects directories because we need to avoid recursing
982 * into a mount-point directory, not to avoid copying a symlinked
983 * file if -L (or similar) was specified. */
984 if (one_file_system && st.st_dev != filesystem_dev
985 && S_ISDIR(st.st_mode)) {
986 if (one_file_system > 1) {
988 rprintf(FINFO, "skipping mount-point dir %s\n",
993 flags |= FLAG_MOUNT_DIR;
996 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
997 if (ignore_perishable)
998 non_perishable_cnt++;
1002 if (lp_ignore_nonreadable(module_id)) {
1003 #ifdef SUPPORT_LINKS
1004 if (!S_ISLNK(st.st_mode))
1006 if (access(thisname, R_OK) != 0)
1012 /* Only divert a directory in the main transfer. */
1013 if (flist && flist->prev && S_ISDIR(st.st_mode)
1014 && flags & FLAG_DIVERT_DIRS) {
1016 /* Room for parent/sibling/next-child info. */
1017 extra_len += 3 * EXTRA_LEN;
1021 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1022 who_am_i(), thisname, filter_level);
1025 if ((basename = strrchr(thisname, '/')) != NULL) {
1026 int len = basename++ - thisname;
1027 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1028 lastdir = new_array(char, len + 1);
1029 memcpy(lastdir, thisname, len);
1030 lastdir[len] = '\0';
1034 basename = thisname;
1035 basename_len = strlen(basename) + 1; /* count the '\0' */
1037 #ifdef SUPPORT_LINKS
1038 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1043 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1044 extra_len += EXTRA_LEN;
1046 #if EXTRA_ROUNDING > 0
1047 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1048 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1051 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1054 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1056 if (!(bp = new_array(char, alloc_len)))
1057 out_of_memory("make_file");
1060 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1062 file = (struct file_struct *)bp;
1063 bp += FILE_STRUCT_LEN;
1065 memcpy(bp, basename, basename_len);
1066 bp += basename_len + linkname_len; /* skip space for symlink too */
1068 #ifdef SUPPORT_HARD_LINKS
1069 if (preserve_hard_links && flist && flist->prev) {
1070 if (protocol_version >= 28
1071 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1072 : S_ISREG(st.st_mode)) {
1073 tmp_dev = st.st_dev;
1074 tmp_ino = st.st_ino;
1080 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1081 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1082 tmp_rdev = st.st_rdev;
1087 file->flags = flags;
1088 file->modtime = st.st_mtime;
1089 file->len32 = (uint32)st.st_size;
1090 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1091 file->flags |= FLAG_LENGTH64;
1092 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1094 file->mode = st.st_mode;
1096 F_OWNER(file) = st.st_uid;
1098 F_GROUP(file) = st.st_gid;
1100 if (basename != thisname)
1101 file->dirname = lastdir;
1103 #ifdef SUPPORT_LINKS
1105 bp = (char*)file->basename + basename_len;
1106 memcpy(bp, linkname, linkname_len);
1110 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1111 file_checksum(thisname, tmp_sum, st.st_size);
1113 F_ROOTDIR(file) = flist_dir;
1115 /* This code is only used by the receiver when it is building
1116 * a list of files for a delete pass. */
1117 if (keep_dirlinks && linkname_len && flist) {
1119 int save_mode = file->mode;
1120 file->mode = S_IFDIR; /* Find a directory with our name. */
1121 if (flist_find(dir_flist, file) >= 0
1122 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1123 file->modtime = st2.st_mtime;
1125 file->mode = st2.st_mode;
1127 F_OWNER(file) = st2.st_uid;
1129 F_GROUP(file) = st2.st_gid;
1131 file->mode = save_mode;
1134 if (basename_len == 0+1)
1137 if (inc_recurse && flist == dir_flist) {
1138 flist_expand(flist);
1139 flist->files[flist->count++] = file;
1145 /* Only called for temporary file_struct entries created by make_file(). */
1146 void unmake_file(struct file_struct *file)
1148 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1149 #if EXTRA_ROUNDING > 0
1150 if (extra_cnt & EXTRA_ROUNDING)
1151 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1153 free(REQ_EXTRA(file, extra_cnt));
1156 static struct file_struct *send_file_name(int f, struct file_list *flist,
1157 char *fname, STRUCT_STAT *stp,
1158 int flags, int filter_flags)
1160 struct file_struct *file;
1165 file = make_file(fname, flist, stp, flags, filter_flags);
1169 if (chmod_modes && !S_ISLNK(file->mode))
1170 file->mode = tweak_mode(file->mode, chmod_modes);
1173 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1174 sx.st.st_mode = file->mode;
1175 sx.acc_acl = sx.def_acl = NULL;
1176 if (get_acl(fname, &sx) < 0)
1181 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1183 flist_expand(flist);
1184 flist->files[flist->count++] = file;
1186 send_file_entry(f, file, flist->count - 1);
1188 if (preserve_acls && !S_ISLNK(file->mode)) {
1197 static void send_if_directory(int f, struct file_list *flist,
1198 struct file_struct *file,
1199 char *fbuf, unsigned int ol,
1202 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1204 if (S_ISDIR(file->mode)
1205 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1207 unsigned int len = strlen(fbuf);
1208 if (len > 1 && fbuf[len-1] == '/')
1210 if (len >= MAXPATHLEN - 1) {
1211 io_error |= IOERR_GENERAL;
1212 rprintf(FERROR, "skipping long-named directory: %s\n",
1216 save_filters = push_local_filters(fbuf, len);
1217 send_directory(f, flist, -1, fbuf, len, flags);
1218 pop_local_filters(save_filters);
1225 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1227 return f_name_cmp(*file1, *file2);
1230 /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1231 * sort them by name, and link them into the tree, setting the appropriate
1232 * parent/child/sibling pointers. */
1233 static void add_dirs_to_tree(int parent_ndx, int start, int end)
1237 int32 *parent_dp = parent_ndx < 0 ? NULL
1238 : F_DIRNODE_P(dir_flist->files[parent_ndx]);
1240 qsort(dir_flist->files + start, end - start + 1,
1241 sizeof dir_flist->files[0], (int (*)())file_compare);
1243 for (i = start; i <= end; i++) {
1244 struct file_struct *file = dir_flist->files[i];
1245 if (!(file->flags & FLAG_XFER_DIR)
1246 || file->flags & FLAG_MOUNT_DIR)
1249 DIR_NEXT_SIBLING(dp) = i;
1251 DIR_FIRST_CHILD(parent_dp) = i;
1254 dp = F_DIRNODE_P(file);
1255 DIR_PARENT(dp) = parent_ndx;
1256 DIR_FIRST_CHILD(dp) = -1;
1259 DIR_NEXT_SIBLING(dp) = -1;
1262 /* This function is normally called by the sender, but the receiving side also
1263 * calls it from get_dirlist() with f set to -1 so that we just construct the
1264 * file list in memory without sending it over the wire. Also, get_dirlist()
1265 * might call this with f set to -2, which also indicates that local filter
1266 * rules should be ignored. */
1267 static void send_directory(int f, struct file_list *flist, int parent_ndx,
1268 char *fbuf, int len, int flags)
1274 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1275 int start = divert_dirs ? dir_flist->count : flist->count;
1276 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1278 assert(flist != NULL);
1280 if (!(d = opendir(fbuf))) {
1281 io_error |= IOERR_GENERAL;
1282 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1287 if (len != 1 || *fbuf != '/')
1290 remainder = MAXPATHLEN - (p - fbuf);
1292 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1293 char *dname = d_name(di);
1294 if (dname[0] == '.' && (dname[1] == '\0'
1295 || (dname[1] == '.' && dname[2] == '\0')))
1297 if (strlcpy(p, dname, remainder) >= remainder) {
1298 io_error |= IOERR_GENERAL;
1300 "cannot send long-named file %s\n",
1305 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
1311 io_error |= IOERR_GENERAL;
1312 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1321 add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1);
1323 int i, end = flist->count - 1;
1324 /* send_if_directory() bumps flist->count, so use "end". */
1325 for (i = start; i <= end; i++)
1326 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1330 void send_extra_file_list(int f, int at_least)
1332 char fbuf[MAXPATHLEN];
1333 struct file_list *flist;
1335 int future_cnt, save_io_error = io_error;
1337 if (send_dir_ndx < 0)
1340 /* Keep sending data until we have the requested number of
1341 * files in the upcoming file-lists. */
1342 if (cur_flist->next) {
1343 flist = first_flist->prev; /* the newest flist */
1344 future_cnt = flist->count
1345 + flist->ndx_start - cur_flist->next->ndx_start;
1348 while (future_cnt < at_least) {
1349 struct file_struct *file = dir_flist->files[send_dir_ndx];
1354 dlen = strlen(fbuf);
1356 if (F_ROOTDIR(file) != flist_dir) {
1357 if (!push_flist_dir(F_ROOTDIR(file), -1))
1358 exit_cleanup(RERR_FILESELECT);
1361 flist = flist_new(0, "send_extra_file_list");
1362 start_write = stats.total_written;
1364 write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx);
1365 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1366 send_directory(f, flist, send_dir_ndx, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1369 clean_flist(flist, 0, 0);
1370 file_total += flist->count;
1371 future_cnt += flist->count;
1372 stats.flist_size += stats.total_written - start_write;
1373 stats.num_files += flist->count;
1375 output_flist(flist);
1377 dp = F_DIRNODE_P(file);
1378 if (DIR_FIRST_CHILD(dp) >= 0) {
1379 send_dir_ndx = DIR_FIRST_CHILD(dp);
1382 while (DIR_NEXT_SIBLING(dp) < 0) {
1383 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1384 write_ndx(f, NDX_FLIST_EOF);
1386 change_local_filter_dir(NULL, 0, 0);
1390 file = dir_flist->files[send_dir_ndx];
1391 dp = F_DIRNODE_P(file);
1393 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1398 if (io_error != save_io_error && !ignore_errors)
1399 send_msg_int(MSG_IO_ERROR, io_error);
1402 struct file_list *send_file_list(int f, int argc, char *argv[])
1407 char lastpath[MAXPATHLEN] = "";
1408 struct file_list *flist;
1409 struct timeval start_tv, end_tv;
1412 int flags, disable_buffering;
1414 rprintf(FLOG, "building file list\n");
1415 if (show_filelist_p())
1416 start_filelist_progress("building file list");
1417 else if (inc_recurse && verbose && !am_server)
1418 rprintf(FCLIENT, "sending incremental file list\n");
1420 start_write = stats.total_written;
1421 gettimeofday(&start_tv, NULL);
1423 #ifdef SUPPORT_HARD_LINKS
1424 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1428 flist = cur_flist = flist_new(0, "send_file_list");
1430 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1431 flags = FLAG_DIVERT_DIRS;
1433 dir_flist = cur_flist;
1437 disable_buffering = io_start_buffering_out(f);
1438 if (filesfrom_fd >= 0) {
1439 if (argv[0] && !push_dir(argv[0], 0)) {
1440 rsyserr(FERROR, errno, "push_dir %s failed",
1441 full_fname(argv[0]));
1442 exit_cleanup(RERR_FILESELECT);
1448 char fbuf[MAXPATHLEN];
1453 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1455 sanitize_path(fbuf, fbuf, "", 0, NULL);
1459 strlcpy(fbuf, *argv++, MAXPATHLEN);
1461 sanitize_path(fbuf, fbuf, "", 0, NULL);
1465 if (relative_paths) {
1466 /* We clean up fbuf below. */
1468 } else if (!len || fbuf[len - 1] == '/') {
1469 if (len == 2 && fbuf[0] == '.') {
1470 /* Turn "./" into just "." rather than "./." */
1473 if (len + 1 >= MAXPATHLEN)
1474 overflow_exit("send_file_list");
1479 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1480 && (len == 2 || fbuf[len-3] == '/')) {
1481 if (len + 2 >= MAXPATHLEN)
1482 overflow_exit("send_file_list");
1488 is_dot_dir = fbuf[len-1] == '.'
1489 && (len == 1 || fbuf[len-2] == '/');
1492 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1493 io_error |= IOERR_GENERAL;
1494 rsyserr(FERROR, errno, "link_stat %s failed",
1499 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1500 rprintf(FINFO, "skipping directory %s\n", fbuf);
1506 if (!relative_paths) {
1507 p = strrchr(fbuf, '/');
1514 len -= p - fbuf + 1;
1519 if ((p = strstr(fbuf, "/./")) != NULL) {
1525 len -= p - fbuf + 3;
1529 /* Get rid of trailing "/" and "/.". */
1531 if (fn[len - 1] == '/') {
1533 if (!--len && !dir) {
1538 else if (len >= 2 && fn[len - 1] == '.'
1539 && fn[len - 2] == '/') {
1541 if (!(len -= 2) && !dir) {
1548 if (len == 1 && fn[0] == '/')
1551 /* Reject a ".." dir in the active part of the path. */
1552 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1553 if ((p[2] == '/' || p[2] == '\0')
1554 && (p == fn || p[-1] == '/')) {
1556 "found \"..\" dir in relative path: %s\n",
1558 exit_cleanup(RERR_SYNTAX);
1569 static const char *lastdir;
1570 static int lastdir_len = -1;
1571 int len = strlen(dir);
1573 if (len != lastdir_len || memcmp(lastdir, dir, len) != 0) {
1574 if (!push_flist_dir(strdup(dir), len))
1576 lastdir = flist_dir;
1577 lastdir_len = flist_dir_len;
1578 } else if (!push_flist_dir(lastdir, lastdir_len)) {
1580 io_error |= IOERR_GENERAL;
1581 rsyserr(FERROR, errno, "push_dir %s failed",
1588 memmove(fbuf, fn, len + 1);
1590 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1591 /* Send the implied directories at the start of the
1592 * source spec, so we get their permissions right. */
1593 char *lp = lastpath, *slash = fbuf;
1595 /* Skip any initial directories in our path that we
1596 * have in common with lastpath. */
1597 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1602 if (fn != p || (*lp && *lp != '/')) {
1603 int save_copy_links = copy_links;
1604 int save_xfer_dirs = xfer_dirs;
1605 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1606 copy_links |= copy_unsafe_links;
1608 while ((slash = strchr(slash+1, '/')) != 0) {
1610 send_file_name(f, flist, fbuf, NULL,
1611 dir_flags, ALL_FILTERS);
1614 copy_links = save_copy_links;
1615 xfer_dirs = save_xfer_dirs;
1617 strlcpy(lastpath, fbuf, sizeof lastpath);
1622 if (one_file_system)
1623 filesystem_dev = st.st_dev;
1625 if (recurse || (xfer_dirs && is_dot_dir)) {
1626 struct file_struct *file;
1627 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR
1628 | (is_dot_dir ? 0 : flags)
1629 | (inc_recurse ? FLAG_DIVERT_DIRS : 0);
1630 file = send_file_name(f, flist, fbuf, &st,
1631 top_flags, ALL_FILTERS);
1632 if (file && !inc_recurse)
1633 send_if_directory(f, flist, file, fbuf, len, flags);
1635 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1638 gettimeofday(&end_tv, NULL);
1639 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1640 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1641 if (stats.flist_buildtime == 0)
1642 stats.flist_buildtime = 1;
1645 write_byte(f, 0); /* Indicate end of file list */
1647 #ifdef SUPPORT_HARD_LINKS
1648 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1652 if (show_filelist_p())
1653 finish_filelist_progress(flist);
1655 gettimeofday(&end_tv, NULL);
1656 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1657 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1659 /* Sort the list without removing any duplicates in non-incremental
1660 * mode. This allows the receiving side to ask for whatever name it
1661 * kept. For incremental mode, the sender also removes duplicates
1662 * in this initial file-list so that it avoids re-sending duplicated
1664 clean_flist(flist, 0, inc_recurse);
1665 file_total += flist->count;
1667 if (!numeric_ids && !inc_recurse)
1670 /* send the io_error flag */
1671 if (protocol_version < 30)
1672 write_int(f, ignore_errors ? 0 : io_error);
1673 else if (io_error && !ignore_errors)
1674 send_msg_int(MSG_IO_ERROR, io_error);
1676 if (disable_buffering)
1677 io_end_buffering_out();
1679 stats.flist_size = stats.total_written - start_write;
1680 stats.num_files = flist->count;
1683 output_flist(flist);
1686 rprintf(FINFO, "send_file_list done\n");
1689 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
1690 if (file_total == 1) {
1691 /* If we're creating incremental file-lists and there
1692 * was just 1 item in the first file-list, send 1 more
1693 * file-list to check if this is a 1-file xfer. */
1694 if (send_dir_ndx < 0)
1695 write_ndx(f, NDX_DONE);
1697 send_extra_file_list(f, 1);
1704 struct file_list *recv_file_list(int f)
1706 struct file_list *flist;
1711 rprintf(FLOG, "receiving file list\n");
1712 if (show_filelist_p())
1713 start_filelist_progress("receiving file list");
1714 else if (inc_recurse && verbose && !am_server && !first_flist)
1715 rprintf(FCLIENT, "receiving incremental file list\n");
1717 start_read = stats.total_read;
1719 flist = flist_new(0, "recv_file_list");
1721 #ifdef SUPPORT_HARD_LINKS
1722 if (preserve_hard_links && protocol_version < 30)
1727 if (flist->ndx_start == 0)
1728 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1729 dstart = dir_flist->count;
1735 while ((flags = read_byte(f)) != 0) {
1736 struct file_struct *file;
1738 flist_expand(flist);
1740 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1741 flags |= read_byte(f) << 8;
1742 file = recv_file_entry(flist, flags, f);
1744 if (inc_recurse && S_ISDIR(file->mode)) {
1745 flist_expand(dir_flist);
1746 dir_flist->files[dir_flist->count++] = file;
1749 flist->files[flist->count++] = file;
1751 maybe_emit_filelist_progress(flist->count);
1754 rprintf(FINFO, "recv_file_name(%s)\n",
1755 f_name(file, NULL));
1758 file_total += flist->count;
1761 rprintf(FINFO, "received %d names\n", flist->count);
1763 if (show_filelist_p())
1764 finish_filelist_progress(flist);
1766 clean_flist(flist, relative_paths, 1);
1769 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
1770 sizeof dir_flist->files[0], (int (*)())file_compare);
1772 recv_uid_list(f, flist);
1774 if (protocol_version < 30) {
1775 /* Recv the io_error flag */
1779 io_error |= read_int(f);
1783 output_flist(flist);
1787 for (i = 0; i < flist->count; i++)
1788 list_file_entry(flist->files[i]);
1792 rprintf(FINFO, "recv_file_list done\n");
1794 stats.flist_size += stats.total_read - start_read;
1795 stats.num_files += flist->count;
1800 /* This is only used once by the receiver if the very first file-list
1801 * has exactly one item in it. */
1802 void recv_additional_file_list(int f)
1804 struct file_list *flist;
1805 int ndx = read_ndx(f);
1806 if (ndx == NDX_DONE) {
1808 change_local_filter_dir(NULL, 0, 0);
1810 ndx = NDX_FLIST_OFFSET - ndx;
1811 if (ndx < 0 || ndx >= dir_flist->count) {
1812 ndx = NDX_FLIST_OFFSET - ndx;
1814 "Invalid dir index: %d (%d - %d)\n",
1815 ndx, NDX_FLIST_OFFSET,
1816 NDX_FLIST_OFFSET - dir_flist->count);
1817 exit_cleanup(RERR_PROTOCOL);
1819 flist = recv_file_list(f);
1820 flist->parent_ndx = ndx;
1824 /* Search for an identically-named item in the file list. Note that the
1825 * items must agree in their directory-ness, or no match is returned. */
1826 int flist_find(struct file_list *flist, struct file_struct *f)
1828 int low = flist->low, high = flist->high;
1829 int diff, mid, mid_up;
1831 while (low <= high) {
1832 mid = (low + high) / 2;
1833 if (F_IS_ACTIVE(flist->files[mid]))
1836 /* Scan for the next non-empty entry using the cached
1837 * distance values. If the value isn't fully up-to-
1838 * date, update it. */
1839 mid_up = mid + F_DEPTH(flist->files[mid]);
1840 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1842 mid_up += F_DEPTH(flist->files[mid_up]);
1843 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1844 F_DEPTH(flist->files[mid]) = mid_up - mid;
1846 if (mid_up > high) {
1847 /* If there's nothing left above us, set high to
1848 * a non-empty entry below us and continue. */
1849 high = mid - (int)flist->files[mid]->len32;
1850 if (!F_IS_ACTIVE(flist->files[high])) {
1852 high -= (int)flist->files[high]->len32;
1853 } while (!F_IS_ACTIVE(flist->files[high]));
1854 flist->files[mid]->len32 = mid - high;
1859 diff = f_name_cmp(flist->files[mid_up], f);
1861 if (protocol_version < 29
1862 && S_ISDIR(flist->files[mid_up]->mode)
1863 != S_ISDIR(f->mode))
1876 * Free up any resources a file_struct has allocated
1877 * and clear the file.
1879 void clear_file(struct file_struct *file)
1881 /* The +1 zeros out the first char of the basename. */
1882 memset(file, 0, FILE_STRUCT_LEN + 1);
1883 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1884 * entry. Likewise for len32 in the opposite direction. We assume
1885 * that we're alone for now since flist_find() will adjust the counts
1886 * it runs into that aren't up-to-date. */
1887 file->len32 = F_DEPTH(file) = 1;
1890 /* Allocate a new file list. */
1891 struct file_list *flist_new(int flags, char *msg)
1893 struct file_list *flist;
1895 flist = new(struct file_list);
1899 memset(flist, 0, sizeof flist[0]);
1901 if (!(flags & FLIST_TEMP)) {
1903 flist->ndx_start = first_flist->prev->ndx_start
1904 + first_flist->prev->count;
1906 /* This is a doubly linked list with prev looping back to
1907 * the end of the list, but the last next pointer is NULL. */
1909 first_flist = cur_flist = flist->prev = flist;
1911 flist->prev = first_flist->prev;
1912 flist->prev->next = first_flist->prev = flist;
1917 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1923 /* Free up all elements in a flist. */
1924 void flist_free(struct file_list *flist)
1927 ; /* Was FLIST_TEMP dir-list. */
1928 else if (flist == flist->prev) {
1929 first_flist = cur_flist = NULL;
1933 if (flist == cur_flist)
1934 cur_flist = flist->next;
1935 if (flist == first_flist)
1936 first_flist = first_flist->next;
1938 flist->prev->next = flist->next;
1940 flist->next = first_flist;
1942 flist->next->prev = flist->prev;
1943 file_total -= flist->count;
1947 pool_destroy(flist->file_pool);
1953 * This routine ensures we don't have any duplicate names in our file list.
1954 * duplicate names can cause corruption because of the pipelining
1956 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1958 char fbuf[MAXPATHLEN];
1963 if (flist->count == 0) {
1968 qsort(flist->files, flist->count,
1969 sizeof flist->files[0], (int (*)())file_compare);
1971 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1972 if (F_IS_ACTIVE(flist->files[i])) {
1977 flist->low = prev_i;
1978 while (++i < flist->count) {
1980 struct file_struct *file = flist->files[i];
1982 if (!F_IS_ACTIVE(file))
1984 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1986 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1987 int save_mode = file->mode;
1988 /* Make sure that this directory doesn't duplicate a
1989 * non-directory earlier in the list. */
1990 flist->high = prev_i;
1991 file->mode = S_IFREG;
1992 j = flist_find(flist, file);
1993 file->mode = save_mode;
1997 struct file_struct *fp = flist->files[j];
1999 /* If one is a dir and the other is not, we want to
2000 * keep the dir because it might have contents in the
2002 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2003 if (S_ISDIR(file->mode))
2007 } else if (protocol_version < 27)
2011 if (verbose > 1 && !am_server) {
2013 "removing duplicate name %s from file list (%d)\n",
2014 f_name(file, fbuf), drop);
2016 /* Make sure we don't lose track of a user-specified
2018 flist->files[keep]->flags |= flist->files[drop]->flags
2019 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2021 clear_file(flist->files[drop]);
2024 if (flist->low == drop) {
2026 j < i && !F_IS_ACTIVE(flist->files[j]);
2035 flist->high = no_dups ? prev_i : flist->count - 1;
2038 /* We need to strip off the leading slashes for relative
2039 * paths, but this must be done _after_ the sorting phase. */
2040 for (i = flist->low; i <= flist->high; i++) {
2041 struct file_struct *file = flist->files[i];
2045 while (*file->dirname == '/')
2047 if (!*file->dirname)
2048 file->dirname = NULL;
2052 if (prune_empty_dirs && no_dups) {
2053 int j, prev_depth = 0;
2055 prev_i = 0; /* It's OK that this isn't really true. */
2057 for (i = flist->low; i <= flist->high; i++) {
2058 struct file_struct *fp, *file = flist->files[i];
2060 /* This temporarily abuses the F_DEPTH() value for a
2061 * directory that is in a chain that might get pruned.
2062 * We restore the old value if it gets a reprieve. */
2063 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2064 /* Dump empty dirs when coming back down. */
2065 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2066 fp = flist->files[prev_i];
2067 if (F_DEPTH(fp) >= 0)
2069 prev_i = -F_DEPTH(fp)-1;
2072 prev_depth = F_DEPTH(file);
2073 if (is_excluded(f_name(file, fbuf), 1,
2075 /* Keep dirs through this dir. */
2076 for (j = prev_depth-1; ; j--) {
2077 fp = flist->files[prev_i];
2078 if (F_DEPTH(fp) >= 0)
2080 prev_i = -F_DEPTH(fp)-1;
2084 F_DEPTH(file) = -prev_i-1;
2087 /* Keep dirs through this non-dir. */
2088 for (j = prev_depth; ; j--) {
2089 fp = flist->files[prev_i];
2090 if (F_DEPTH(fp) >= 0)
2092 prev_i = -F_DEPTH(fp)-1;
2097 /* Dump all remaining empty dirs. */
2099 struct file_struct *fp = flist->files[prev_i];
2100 if (F_DEPTH(fp) >= 0)
2102 prev_i = -F_DEPTH(fp)-1;
2106 for (i = flist->low; i <= flist->high; i++) {
2107 if (F_IS_ACTIVE(flist->files[i]))
2111 for (i = flist->high; i >= flist->low; i--) {
2112 if (F_IS_ACTIVE(flist->files[i]))
2119 static void output_flist(struct file_list *flist)
2121 char uidbuf[16], gidbuf[16], depthbuf[16];
2122 struct file_struct *file;
2123 const char *root, *dir, *slash, *name, *trail;
2124 const char *who = who_am_i();
2127 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2128 who, flist->ndx_start, flist->count, flist->low, flist->high);
2129 for (i = 0; i < flist->count; i++) {
2130 file = flist->files[i];
2131 if ((am_root || am_sender) && preserve_uid) {
2132 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
2136 if (preserve_gid && F_GID(file) != GID_NONE) {
2137 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
2142 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2143 if (F_IS_ACTIVE(file)) {
2144 root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
2145 if ((dir = file->dirname) == NULL)
2149 name = file->basename;
2150 trail = S_ISDIR(file->mode) ? "/" : "";
2152 root = dir = slash = name = trail = "";
2153 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2154 who, i, root, dir, slash, name, trail, (int)file->mode,
2155 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
2159 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2160 enum fnc_type { t_PATH, t_ITEM };
2162 /* Compare the names of two file_struct entities, similar to how strcmp()
2163 * would do if it were operating on the joined strings.
2165 * Some differences beginning with protocol_version 29: (1) directory names
2166 * are compared with an assumed trailing slash so that they compare in a
2167 * way that would cause them to sort immediately prior to any content they
2168 * may have; (2) a directory of any name compares after a non-directory of
2169 * any name at the same depth; (3) a directory with name "." compares prior
2170 * to anything else. These changes mean that a directory and a non-dir
2171 * with the same name will not compare as equal (protocol_version >= 29).
2173 * The dirname component can be an empty string, but the basename component
2174 * cannot (and never is in the current codebase). The basename component
2175 * may be NULL (for a removed item), in which case it is considered to be
2176 * after any existing item. */
2177 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
2180 const uchar *c1, *c2;
2181 enum fnc_state state1, state2;
2182 enum fnc_type type1, type2;
2183 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2185 if (!f1 || !F_IS_ACTIVE(f1)) {
2186 if (!f2 || !F_IS_ACTIVE(f2))
2190 if (!f2 || !F_IS_ACTIVE(f2))
2193 c1 = (uchar*)f1->dirname;
2194 c2 = (uchar*)f2->dirname;
2198 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2199 c1 = (const uchar*)f1->basename;
2200 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2202 state1 = s_TRAILING;
2211 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2212 c2 = (const uchar*)f2->basename;
2213 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2215 state2 = s_TRAILING;
2225 return type1 == t_PATH ? 1 : -1;
2235 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2236 c1 = (const uchar*)f1->basename;
2237 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2239 state1 = s_TRAILING;
2245 state1 = s_TRAILING;
2246 if (type1 == t_PATH) {
2255 if (*c2 && type1 != type2)
2256 return type1 == t_PATH ? 1 : -1;
2265 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2266 c2 = (const uchar*)f2->basename;
2267 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2269 state2 = s_TRAILING;
2275 state2 = s_TRAILING;
2276 if (type2 == t_PATH) {
2288 return type1 == t_PATH ? 1 : -1;
2290 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2295 char *f_name_buf(void)
2297 static char names[5][MAXPATHLEN];
2298 static unsigned int n;
2300 n = (n + 1) % (sizeof names / sizeof names[0]);
2305 /* Return a copy of the full filename of a flist entry, using the indicated
2306 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2307 * done because we checked the size when creating the file_struct entry.
2309 char *f_name(struct file_struct *f, char *fbuf)
2311 if (!f || !F_IS_ACTIVE(f))
2315 fbuf = f_name_buf();
2318 int len = strlen(f->dirname);
2319 memcpy(fbuf, f->dirname, len);
2321 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2323 strlcpy(fbuf, f->basename, MAXPATHLEN);
2328 /* Do a non-recursive scan of the named directory, possibly ignoring all
2329 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2330 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2331 * buffer (the functions we call will append names onto the end, but the old
2332 * dir value will be restored on exit). */
2333 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2335 struct file_list *dirlist;
2336 char dirbuf[MAXPATHLEN];
2337 int save_recurse = recurse;
2338 int save_xfer_dirs = xfer_dirs;
2341 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2342 if (dlen >= MAXPATHLEN)
2347 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2351 send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0);
2352 xfer_dirs = save_xfer_dirs;
2353 recurse = save_recurse;
2355 flist_count_offset += dirlist->count;
2357 clean_flist(dirlist, 0, 0);
2360 output_flist(dirlist);