2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
34 extern int am_generator;
35 extern int inc_recurse;
36 extern int do_progress;
37 extern int always_checksum;
39 extern int ignore_errors;
40 extern int numeric_ids;
44 extern int filesfrom_fd;
45 extern int one_file_system;
46 extern int copy_dirlinks;
47 extern int keep_dirlinks;
48 extern int preserve_acls;
49 extern int preserve_xattrs;
50 extern int preserve_links;
51 extern int preserve_hard_links;
52 extern int preserve_devices;
53 extern int preserve_specials;
57 extern int relative_paths;
58 extern int implied_dirs;
59 extern int file_extra_cnt;
60 extern int ignore_perishable;
61 extern int non_perishable_cnt;
62 extern int prune_empty_dirs;
63 extern int copy_links;
64 extern int copy_unsafe_links;
65 extern int protocol_version;
66 extern int sanitize_paths;
67 extern struct stats stats;
68 extern char *filesfrom_host;
70 extern char curr_dir[MAXPATHLEN];
72 extern struct chmod_mode_struct *chmod_modes;
74 extern struct filter_list_struct filter_list;
75 extern struct filter_list_struct server_filter_list;
79 extern int filesfrom_convert;
80 extern int need_unsorted_flist;
81 extern iconv_t ic_send, ic_recv;
84 #define PTR_SIZE (sizeof (struct file_struct *))
88 dev_t filesystem_dev; /* used to implement -x */
90 struct file_list *cur_flist, *first_flist, *dir_flist;
91 int send_dir_ndx = -1, send_dir_depth = 0;
92 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
93 int file_total = 0; /* total of all active items over all file-lists */
94 int flist_eof = 0; /* all the file-lists are now known */
96 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
97 * internally, even if this platform does not allow files to have 64-bit inums.
98 * The only exception is if we're on a platform with no 64-bit type at all.
100 * Because we use read_longint() to get these off the wire, if you transfer
101 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
102 * machine with no 64-bit types then you will get an overflow error.
104 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
105 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
106 * be truncated. But it's a kind of silly thing to do anyhow. */
108 /* The tmp_* vars are used as a cache area by make_file() to store data
109 * that the sender doesn't need to remember in its file list. The data
110 * will survive just long enough to be used by send_file_entry(). */
111 static dev_t tmp_rdev;
112 #ifdef SUPPORT_HARD_LINKS
113 static int64 tmp_dev, tmp_ino;
115 static char tmp_sum[MAX_DIGEST_LEN];
117 static char empty_sum[MAX_DIGEST_LEN];
118 static int flist_count_offset; /* for --delete --progress */
119 static int dir_count = 0;
120 static int high_hlink_ndx;
122 static void clean_flist(struct file_list *flist, int strip_root);
123 static void output_flist(struct file_list *flist);
125 void init_flist(void)
128 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
129 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
131 checksum_len = protocol_version < 21 ? 2
132 : protocol_version < 30 ? MD4_DIGEST_LEN
136 static int show_filelist_p(void)
138 return verbose && xfer_dirs && !am_server && !inc_recurse;
141 static void start_filelist_progress(char *kind)
143 rprintf(FCLIENT, "%s ... ", kind);
144 if (verbose > 1 || do_progress)
145 rprintf(FCLIENT, "\n");
149 static void emit_filelist_progress(int count)
151 rprintf(FCLIENT, " %d files...\r", count);
154 static void maybe_emit_filelist_progress(int count)
156 if (do_progress && show_filelist_p() && (count % 100) == 0)
157 emit_filelist_progress(count);
160 static void finish_filelist_progress(const struct file_list *flist)
163 /* This overwrites the progress line */
164 rprintf(FINFO, "%d file%sto consider\n",
165 flist->used, flist->used == 1 ? " " : "s ");
167 rprintf(FINFO, "done\n");
170 void show_flist_stats(void)
175 static void list_file_entry(struct file_struct *f)
177 char permbuf[PERMSTRING_SIZE];
180 if (!F_IS_ACTIVE(f)) {
181 /* this can happen if duplicate names were removed */
185 permstring(permbuf, f->mode);
188 /* TODO: indicate '+' if the entry has an ACL. */
191 if (preserve_links && S_ISLNK(f->mode)) {
192 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
193 permbuf, len, timestring(f->modtime),
194 f_name(f, NULL), F_SYMLINK(f));
198 rprintf(FINFO, "%s %11.0f %s %s\n",
199 permbuf, len, timestring(f->modtime),
204 /* Stat either a symlink or its referent, depending on the settings of
205 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
207 * If path is the name of a symlink, then the linkbuf buffer (which must hold
208 * MAXPATHLEN chars) will be set to the symlink's target string.
210 * The stat structure pointed to by stp will contain information about the
211 * link or the referent as appropriate, if they exist. */
212 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
215 if (link_stat(path, stp, copy_dirlinks) < 0)
217 if (S_ISLNK(stp->st_mode)) {
218 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
221 linkbuf[llen] = '\0';
222 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
224 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
227 return x_stat(path, stp, NULL);
232 return x_stat(path, stp, NULL);
236 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
240 return x_stat(path, stp, NULL);
241 if (x_lstat(path, stp, NULL) < 0)
243 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
245 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
250 return x_stat(path, stp, NULL);
254 /* This function is used to check if a file should be included/excluded
255 * from the list of files based on its name and type etc. The value of
256 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
257 static int is_excluded(char *fname, int is_dir, int filter_level)
259 #if 0 /* This currently never happens, so avoid a useless compare. */
260 if (filter_level == NO_FILTERS)
264 /* never exclude '.', even if somebody does --exclude '*' */
265 if (fname[0] == '.' && !fname[1])
267 /* Handle the -R version of the '.' dir. */
268 if (fname[0] == '/') {
269 int len = strlen(fname);
270 if (fname[len-1] == '.' && fname[len-2] == '/')
274 if (server_filter_list.head
275 && check_filter(&server_filter_list, fname, is_dir) < 0)
277 if (filter_level != ALL_FILTERS)
280 && check_filter(&filter_list, fname, is_dir) < 0)
285 static void send_directory(int f, struct file_list *flist,
286 char *fbuf, int len, int flags);
288 static const char *pathname, *orig_dir;
289 static int pathname_len;
292 /* Make sure flist can hold at least flist->used + extra entries. */
293 static void flist_expand(struct file_list *flist, int extra)
295 struct file_struct **new_ptr;
297 if (flist->used + extra <= flist->malloced)
300 if (flist->malloced < FLIST_START)
301 flist->malloced = FLIST_START;
302 else if (flist->malloced >= FLIST_LINEAR)
303 flist->malloced += FLIST_LINEAR;
305 flist->malloced *= 2;
307 /* In case count jumped or we are starting the list
308 * with a known size just set it. */
309 if (flist->malloced < flist->used + extra)
310 flist->malloced = flist->used + extra;
312 new_ptr = realloc_array(flist->files, struct file_struct *,
315 if (verbose >= 2 && flist->malloced != FLIST_START) {
316 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
318 (double)sizeof flist->files[0] * flist->malloced,
319 (new_ptr == flist->files) ? " not" : "");
322 flist->files = new_ptr;
325 out_of_memory("flist_expand");
328 static void flist_done_allocating(struct file_list *flist)
330 void *ptr = pool_boundary(flist->file_pool, 8*1024);
331 if (flist->pool_boundary == ptr)
332 flist->pool_boundary = NULL; /* list didn't use any pool memory */
334 flist->pool_boundary = ptr;
337 int push_pathname(const char *dir, int len)
343 orig_dir = strdup(curr_dir);
345 if (pathname && !pop_dir(orig_dir)) {
346 rsyserr(FERROR, errno, "pop_dir %s failed",
347 full_fname(orig_dir));
348 exit_cleanup(RERR_FILESELECT);
351 if (dir && !push_dir(dir, 0)) {
352 io_error |= IOERR_GENERAL;
353 rsyserr(FERROR, errno, "push_dir %s failed in %s",
354 full_fname(dir), curr_dir);
359 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
364 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
366 static time_t modtime;
370 static uint32 rdev_major;
373 static char *user_name, *group_name;
374 static char lastname[MAXPATHLEN];
375 char fname[MAXPATHLEN];
376 int first_hlink_ndx = -1;
381 if (ic_send != (iconv_t)-1) {
384 INIT_CONST_XBUF(outbuf, fname);
387 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
388 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
389 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
392 outbuf.buf[outbuf.len++] = '/';
395 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
396 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
398 io_error |= IOERR_GENERAL;
400 "[%s] cannot convert filename: %s (%s)\n",
401 who_am_i(), f_name(file, fname), strerror(errno));
404 outbuf.buf[outbuf.len] = '\0';
409 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
411 if (file->mode == mode)
412 xflags |= XMIT_SAME_MODE;
416 if (protocol_version >= 30 && S_ISDIR(mode) && !(file->flags & FLAG_XFER_DIR))
417 xflags |= XMIT_NON_XFER_DIR;
419 if ((preserve_devices && IS_DEVICE(mode))
420 || (preserve_specials && IS_SPECIAL(mode))) {
421 if (protocol_version < 28) {
422 if (tmp_rdev == rdev)
423 xflags |= XMIT_SAME_RDEV_pre28;
428 if ((uint32)major(rdev) == rdev_major)
429 xflags |= XMIT_SAME_RDEV_MAJOR;
431 rdev_major = major(rdev);
432 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
433 xflags |= XMIT_RDEV_MINOR_8_pre30;
435 } else if (protocol_version < 28)
436 rdev = MAKEDEV(0, 0);
438 if ((uid_t)F_OWNER(file) == uid && *lastname)
439 xflags |= XMIT_SAME_UID;
442 if (uid_ndx && !numeric_ids) {
443 user_name = add_uid(uid);
444 if (inc_recurse && user_name)
445 xflags |= XMIT_USER_NAME_FOLLOWS;
450 if ((gid_t)F_GROUP(file) == gid && *lastname)
451 xflags |= XMIT_SAME_GID;
454 if (gid_ndx && !numeric_ids) {
455 group_name = add_gid(gid);
456 if (inc_recurse && group_name)
457 xflags |= XMIT_GROUP_NAME_FOLLOWS;
461 if (file->modtime == modtime)
462 xflags |= XMIT_SAME_TIME;
464 modtime = file->modtime;
466 #ifdef SUPPORT_HARD_LINKS
468 if (protocol_version >= 30) {
469 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
470 first_hlink_ndx = (int32)(long)np->data - 1;
471 if (first_hlink_ndx < 0) {
472 high_hlink_ndx = ndx + first_ndx;
473 np->data = (void*)(long)(high_hlink_ndx + 1);
474 xflags |= XMIT_HLINK_FIRST;
476 xflags |= XMIT_HLINKED;
478 if (tmp_dev == dev) {
479 if (protocol_version >= 28)
480 xflags |= XMIT_SAME_DEV_pre30;
483 xflags |= XMIT_HLINKED;
489 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
491 l2 = strlen(fname+l1);
494 xflags |= XMIT_SAME_NAME;
496 xflags |= XMIT_LONG_NAME;
498 /* We must make sure we don't send a zero flag byte or the
499 * other end will terminate the flist transfer. Note that
500 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
501 * it's harmless way to add a bit to the first flag byte. */
502 if (protocol_version >= 28) {
503 if (!xflags && !S_ISDIR(mode))
504 xflags |= XMIT_TOP_DIR;
505 if ((xflags & 0xFF00) || !xflags) {
506 xflags |= XMIT_EXTENDED_FLAGS;
507 write_shortint(f, xflags);
509 write_byte(f, xflags);
511 if (!(xflags & 0xFF))
512 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
513 write_byte(f, xflags);
515 if (xflags & XMIT_SAME_NAME)
517 if (xflags & XMIT_LONG_NAME)
518 write_varint30(f, l2);
521 write_buf(f, fname + l1, l2);
523 if (first_hlink_ndx >= 0) {
524 write_varint(f, first_hlink_ndx);
525 if (first_hlink_ndx >= first_ndx)
529 write_varlong30(f, F_LENGTH(file), 3);
530 if (!(xflags & XMIT_SAME_TIME)) {
531 if (protocol_version >= 30)
532 write_varlong(f, modtime, 4);
534 write_int(f, modtime);
536 if (!(xflags & XMIT_SAME_MODE))
537 write_int(f, to_wire_mode(mode));
538 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
539 if (protocol_version < 30)
542 write_varint(f, uid);
543 if (xflags & XMIT_USER_NAME_FOLLOWS) {
544 int len = strlen(user_name);
546 write_buf(f, user_name, len);
550 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
551 if (protocol_version < 30)
554 write_varint(f, gid);
555 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
556 int len = strlen(group_name);
558 write_buf(f, group_name, len);
562 if ((preserve_devices && IS_DEVICE(mode))
563 || (preserve_specials && IS_SPECIAL(mode))) {
564 if (protocol_version < 28) {
565 if (!(xflags & XMIT_SAME_RDEV_pre28))
566 write_int(f, (int)rdev);
568 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
569 write_varint30(f, major(rdev));
570 if (protocol_version >= 30)
571 write_varint(f, minor(rdev));
572 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
573 write_byte(f, minor(rdev));
575 write_int(f, minor(rdev));
580 if (preserve_links && S_ISLNK(mode)) {
581 const char *sl = F_SYMLINK(file);
582 int len = strlen(sl);
583 write_varint30(f, len);
584 write_buf(f, sl, len);
588 #ifdef SUPPORT_HARD_LINKS
589 if (tmp_dev != 0 && protocol_version < 30) {
590 if (protocol_version < 26) {
591 /* 32-bit dev_t and ino_t */
592 write_int(f, (int32)dev);
593 write_int(f, (int32)tmp_ino);
595 /* 64-bit dev_t and ino_t */
596 if (!(xflags & XMIT_SAME_DEV_pre30))
597 write_longint(f, dev);
598 write_longint(f, tmp_ino);
603 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
608 /* Prior to 28, we sent a useless set of nulls. */
611 write_buf(f, sum, checksum_len);
615 strlcpy(lastname, fname, MAXPATHLEN);
617 if (S_ISREG(mode) || S_ISLNK(mode))
618 stats.total_size += F_LENGTH(file);
621 static struct file_struct *recv_file_entry(struct file_list *flist,
624 static int64 modtime;
628 static uint32 rdev_major;
631 static uint16 gid_flags;
632 static char lastname[MAXPATHLEN], *lastdir;
633 static int lastdir_depth, lastdir_len = -1;
634 static unsigned int del_hier_name_len = 0;
635 static int in_del_hier = 0;
636 char thisname[MAXPATHLEN];
637 unsigned int l1 = 0, l2 = 0;
638 int alloc_len, basename_len, linkname_len;
639 int extra_len = file_extra_cnt * EXTRA_LEN;
640 int first_hlink_ndx = -1;
642 const char *basename;
643 struct file_struct *file;
647 if (xflags & XMIT_SAME_NAME)
650 if (xflags & XMIT_LONG_NAME)
651 l2 = read_varint30(f);
655 if (l2 >= MAXPATHLEN - l1) {
657 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
658 xflags, l1, l2, lastname, who_am_i());
659 overflow_exit("recv_file_entry");
662 strlcpy(thisname, lastname, l1 + 1);
663 read_sbuf(f, &thisname[l1], l2);
664 thisname[l1 + l2] = 0;
666 /* Abuse basename_len for a moment... */
667 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
670 if (ic_recv != (iconv_t)-1) {
673 INIT_CONST_XBUF(outbuf, thisname);
674 INIT_XBUF(inbuf, lastname, basename_len, -1);
676 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
677 io_error |= IOERR_GENERAL;
679 "[%s] cannot convert filename: %s (%s)\n",
680 who_am_i(), lastname, strerror(errno));
683 outbuf.buf[outbuf.len] = '\0';
687 clean_fname(thisname, 0);
690 sanitize_path(thisname, thisname, "", 0, NULL);
692 if ((basename = strrchr(thisname, '/')) != NULL) {
693 int len = basename++ - thisname;
694 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
695 lastdir = new_array(char, len + 1);
696 memcpy(lastdir, thisname, len);
699 lastdir_depth = count_dir_elements(lastdir);
703 basename_len = strlen(basename) + 1; /* count the '\0' */
705 #ifdef SUPPORT_HARD_LINKS
706 if (protocol_version >= 30
707 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
708 first_hlink_ndx = read_varint(f);
709 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
711 "hard-link reference out of range: %d (%d)\n",
712 first_hlink_ndx, flist->ndx_start + flist->used);
713 exit_cleanup(RERR_PROTOCOL);
715 if (first_hlink_ndx >= flist->ndx_start) {
716 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
717 file_length = F_LENGTH(first);
718 modtime = first->modtime;
721 uid = F_OWNER(first);
723 gid = F_GROUP(first);
724 if ((preserve_devices && IS_DEVICE(mode))
725 || (preserve_specials && IS_SPECIAL(mode))) {
726 uint32 *devp = F_RDEV_P(first);
727 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
728 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
730 if (preserve_links && S_ISLNK(mode))
731 linkname_len = strlen(F_SYMLINK(first)) + 1;
739 file_length = read_varlong30(f, 3);
740 if (!(xflags & XMIT_SAME_TIME)) {
741 if (protocol_version >= 30) {
742 modtime = read_varlong(f, 4);
743 #if SIZEOF_TIME_T < SIZEOF_INT64
744 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
746 "Time value of %s truncated on receiver.\n",
751 modtime = read_int(f);
753 if (!(xflags & XMIT_SAME_MODE))
754 mode = from_wire_mode(read_int(f));
756 if (chmod_modes && !S_ISLNK(mode))
757 mode = tweak_mode(mode, chmod_modes);
759 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
760 if (protocol_version < 30)
761 uid = (uid_t)read_int(f);
763 uid = (uid_t)read_varint(f);
764 if (xflags & XMIT_USER_NAME_FOLLOWS)
765 uid = recv_user_name(f, uid);
766 else if (inc_recurse && am_root && !numeric_ids)
767 uid = match_uid(uid);
770 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
771 if (protocol_version < 30)
772 gid = (gid_t)read_int(f);
774 gid = (gid_t)read_varint(f);
776 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
777 gid = recv_group_name(f, gid, &gid_flags);
778 else if (inc_recurse && (!am_root || !numeric_ids))
779 gid = match_gid(gid, &gid_flags);
783 if ((preserve_devices && IS_DEVICE(mode))
784 || (preserve_specials && IS_SPECIAL(mode))) {
785 if (protocol_version < 28) {
786 if (!(xflags & XMIT_SAME_RDEV_pre28))
787 rdev = (dev_t)read_int(f);
790 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
791 rdev_major = read_varint30(f);
792 if (protocol_version >= 30)
793 rdev_minor = read_varint(f);
794 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
795 rdev_minor = read_byte(f);
797 rdev_minor = read_int(f);
798 rdev = MAKEDEV(rdev_major, rdev_minor);
800 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
802 } else if (protocol_version < 28)
803 rdev = MAKEDEV(0, 0);
806 if (preserve_links && S_ISLNK(mode)) {
807 linkname_len = read_varint30(f) + 1; /* count the '\0' */
808 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
809 rprintf(FERROR, "overflow: linkname_len=%d\n",
811 overflow_exit("recv_file_entry");
818 #ifdef SUPPORT_HARD_LINKS
820 if (preserve_hard_links) {
821 if (protocol_version < 28 && S_ISREG(mode))
822 xflags |= XMIT_HLINKED;
823 if (xflags & XMIT_HLINKED)
824 extra_len += (inc_recurse+1) * EXTRA_LEN;
829 /* We need one or two index int32s when we're preserving ACLs. */
831 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
834 if (always_checksum && S_ISREG(mode))
835 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
837 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
838 extra_len += EXTRA_LEN;
840 #if EXTRA_ROUNDING > 0
841 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
842 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
845 if (inc_recurse && S_ISDIR(mode)) {
846 if (one_file_system) {
847 /* Room to save the dir's device for -x */
848 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
850 pool = dir_flist->file_pool;
852 pool = flist->file_pool;
854 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
856 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
858 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
860 file = (struct file_struct *)bp;
861 bp += FILE_STRUCT_LEN;
863 memcpy(bp, basename, basename_len);
864 bp += basename_len + linkname_len; /* skip space for symlink too */
866 #ifdef SUPPORT_HARD_LINKS
867 if (xflags & XMIT_HLINKED)
868 file->flags |= FLAG_HLINKED;
870 file->modtime = (time_t)modtime;
871 file->len32 = (uint32)file_length;
872 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
873 file->flags |= FLAG_LENGTH64;
874 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
881 file->flags |= gid_flags;
885 F_NDX(file) = flist->used + flist->ndx_start;
888 if (basename != thisname) {
889 file->dirname = lastdir;
890 F_DEPTH(file) = lastdir_depth + 1;
895 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
897 if (xflags & XMIT_TOP_DIR) {
898 in_del_hier = recurse;
899 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
900 if (relative_paths && del_hier_name_len > 2
901 && lastname[del_hier_name_len-1] == '.'
902 && lastname[del_hier_name_len-2] == '/')
903 del_hier_name_len -= 2;
904 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
905 } else if (protocol_version >= 30) {
906 if (!(xflags & XMIT_NON_XFER_DIR))
907 file->flags |= FLAG_XFER_DIR;
908 } else if (in_del_hier) {
909 if (!relative_paths || !del_hier_name_len
910 || (l1 >= del_hier_name_len
911 && lastname[del_hier_name_len] == '/'))
912 file->flags |= FLAG_XFER_DIR;
918 if ((preserve_devices && IS_DEVICE(mode))
919 || (preserve_specials && IS_SPECIAL(mode))) {
920 uint32 *devp = F_RDEV_P(file);
921 DEV_MAJOR(devp) = major(rdev);
922 DEV_MINOR(devp) = minor(rdev);
927 bp = (char*)file->basename + basename_len;
928 if (first_hlink_ndx >= flist->ndx_start) {
929 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
930 memcpy(bp, F_SYMLINK(first), linkname_len);
932 read_sbuf(f, bp, linkname_len - 1);
934 sanitize_path(bp, bp, "", lastdir_depth, NULL);
938 #ifdef SUPPORT_HARD_LINKS
939 if (preserve_hard_links && xflags & XMIT_HLINKED) {
940 if (protocol_version >= 30) {
941 if (xflags & XMIT_HLINK_FIRST) {
942 high_hlink_ndx = flist->ndx_start + flist->used;
943 F_HL_GNUM(file) = high_hlink_ndx;
945 F_HL_GNUM(file) = first_hlink_ndx;
947 static int32 cnt = 0;
948 struct ht_int64_node *np;
951 if (protocol_version < 26) {
955 if (!(xflags & XMIT_SAME_DEV_pre30))
956 dev = read_longint(f);
957 ino = read_longint(f);
959 np = idev_find(dev, ino);
960 ndx = (int32)(long)np->data - 1;
963 np->data = (void*)(long)cnt;
965 F_HL_GNUM(file) = ndx;
970 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
974 /* Prior to 28, we get a useless set of nulls. */
977 if (first_hlink_ndx >= flist->ndx_start) {
978 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
979 memcpy(bp, F_SUM(first), checksum_len);
981 read_buf(f, bp, checksum_len);
985 if (preserve_acls && !S_ISLNK(mode))
986 receive_acl(file, f);
988 #ifdef SUPPORT_XATTRS
990 receive_xattr(file, f );
993 if (S_ISREG(mode) || S_ISLNK(mode))
994 stats.total_size += file_length;
999 /* Create a file_struct for a named file by reading its stat() information
1000 * and performing extensive checks against global options.
1002 * Returns a pointer to the new file struct, or NULL if there was an error
1003 * or this file should be excluded. */
1004 struct file_struct *make_file(const char *fname, struct file_list *flist,
1005 STRUCT_STAT *stp, int flags, int filter_level)
1007 static char *lastdir;
1008 static int lastdir_len = -1;
1009 struct file_struct *file;
1010 char thisname[MAXPATHLEN];
1011 char linkname[MAXPATHLEN];
1012 int alloc_len, basename_len, linkname_len;
1013 int extra_len = file_extra_cnt * EXTRA_LEN;
1014 const char *basename;
1019 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1020 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1023 clean_fname(thisname, 0);
1025 sanitize_path(thisname, thisname, "", 0, NULL);
1027 if (stp && S_ISDIR(stp->st_mode)) {
1028 st = *stp; /* Needed for "symlink/." with --relative. */
1029 *linkname = '\0'; /* make IBM code checker happy */
1030 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1031 int save_errno = errno;
1032 /* See if file is excluded before reporting an error. */
1033 if (filter_level != NO_FILTERS
1034 && (is_excluded(thisname, 0, filter_level)
1035 || is_excluded(thisname, 1, filter_level))) {
1036 if (ignore_perishable && save_errno != ENOENT)
1037 non_perishable_cnt++;
1040 if (save_errno == ENOENT) {
1041 #ifdef SUPPORT_LINKS
1042 /* Avoid "vanished" error if symlink points nowhere. */
1043 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1044 && S_ISLNK(st.st_mode)) {
1045 io_error |= IOERR_GENERAL;
1046 rprintf(FERROR, "symlink has no referent: %s\n",
1047 full_fname(thisname));
1051 enum logcode c = am_daemon && protocol_version < 28
1053 io_error |= IOERR_VANISHED;
1054 rprintf(c, "file has vanished: %s\n",
1055 full_fname(thisname));
1058 io_error |= IOERR_GENERAL;
1059 rsyserr(FERROR, save_errno, "readlink %s failed",
1060 full_fname(thisname));
1065 /* backup.c calls us with filter_level set to NO_FILTERS. */
1066 if (filter_level == NO_FILTERS)
1069 if (S_ISDIR(st.st_mode)) {
1071 rprintf(FINFO, "skipping directory %s\n", thisname);
1075 flags &= ~FLAG_XFER_DIR;
1077 /* -x only affects directories because we need to avoid recursing
1078 * into a mount-point directory, not to avoid copying a symlinked
1079 * file if -L (or similar) was specified. */
1080 if (one_file_system && S_ISDIR(st.st_mode)) {
1081 if (flags & FLAG_TOP_DIR)
1082 filesystem_dev = st.st_dev;
1083 else if (st.st_dev != filesystem_dev) {
1084 if (one_file_system > 1) {
1086 rprintf(FINFO, "skipping mount-point dir %s\n",
1091 flags |= FLAG_MOUNT_DIR;
1095 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1096 if (ignore_perishable)
1097 non_perishable_cnt++;
1101 if (lp_ignore_nonreadable(module_id)) {
1102 #ifdef SUPPORT_LINKS
1103 if (!S_ISLNK(st.st_mode))
1105 if (access(thisname, R_OK) != 0)
1111 /* Only divert a directory in the main transfer. */
1113 if (flist->prev && S_ISDIR(st.st_mode)
1114 && flags & FLAG_DIVERT_DIRS) {
1115 /* Room for parent/sibling/next-child info. */
1116 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1118 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1120 pool = dir_flist->file_pool;
1122 pool = flist->file_pool;
1127 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1128 who_am_i(), thisname, filter_level);
1131 if ((basename = strrchr(thisname, '/')) != NULL) {
1132 int len = basename++ - thisname;
1133 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1134 lastdir = new_array(char, len + 1);
1135 memcpy(lastdir, thisname, len);
1136 lastdir[len] = '\0';
1140 basename = thisname;
1141 basename_len = strlen(basename) + 1; /* count the '\0' */
1143 #ifdef SUPPORT_LINKS
1144 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1149 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1150 extra_len += EXTRA_LEN;
1152 #if EXTRA_ROUNDING > 0
1153 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1154 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1157 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1160 bp = pool_alloc(pool, alloc_len, "make_file");
1162 if (!(bp = new_array(char, alloc_len)))
1163 out_of_memory("make_file");
1166 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1168 file = (struct file_struct *)bp;
1169 bp += FILE_STRUCT_LEN;
1171 memcpy(bp, basename, basename_len);
1172 bp += basename_len + linkname_len; /* skip space for symlink too */
1174 #ifdef SUPPORT_HARD_LINKS
1175 if (preserve_hard_links && flist && flist->prev) {
1176 if (protocol_version >= 28
1177 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1178 : S_ISREG(st.st_mode)) {
1179 tmp_dev = st.st_dev;
1180 tmp_ino = st.st_ino;
1186 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1187 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1188 tmp_rdev = st.st_rdev;
1193 file->flags = flags;
1194 file->modtime = st.st_mtime;
1195 file->len32 = (uint32)st.st_size;
1196 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1197 file->flags |= FLAG_LENGTH64;
1198 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1200 file->mode = st.st_mode;
1202 F_OWNER(file) = st.st_uid;
1204 F_GROUP(file) = st.st_gid;
1206 if (basename != thisname)
1207 file->dirname = lastdir;
1209 #ifdef SUPPORT_LINKS
1211 bp = (char*)file->basename + basename_len;
1212 memcpy(bp, linkname, linkname_len);
1216 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1217 file_checksum(thisname, tmp_sum, st.st_size);
1219 F_PATHNAME(file) = pathname;
1221 /* This code is only used by the receiver when it is building
1222 * a list of files for a delete pass. */
1223 if (keep_dirlinks && linkname_len && flist) {
1225 int save_mode = file->mode;
1226 file->mode = S_IFDIR; /* Find a directory with our name. */
1227 if (flist_find(dir_flist, file) >= 0
1228 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1229 file->modtime = st2.st_mtime;
1231 file->mode = st2.st_mode;
1233 F_OWNER(file) = st2.st_uid;
1235 F_GROUP(file) = st2.st_gid;
1237 file->mode = save_mode;
1240 if (basename_len == 0+1)
1245 F_NDX(file) = dir_count - 1;
1251 /* Only called for temporary file_struct entries created by make_file(). */
1252 void unmake_file(struct file_struct *file)
1254 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1255 #if EXTRA_ROUNDING > 0
1256 if (extra_cnt & EXTRA_ROUNDING)
1257 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1259 free(REQ_EXTRA(file, extra_cnt));
1262 static struct file_struct *send_file_name(int f, struct file_list *flist,
1263 char *fname, STRUCT_STAT *stp,
1264 int flags, int filter_level)
1266 struct file_struct *file;
1267 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1271 file = make_file(fname, flist, stp, flags, filter_level);
1275 if (chmod_modes && !S_ISLNK(file->mode))
1276 file->mode = tweak_mode(file->mode, chmod_modes);
1279 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1280 sx.st.st_mode = file->mode;
1281 sx.acc_acl = sx.def_acl = NULL;
1282 if (get_acl(fname, &sx) < 0)
1286 #ifdef SUPPORT_XATTRS
1287 if (preserve_xattrs && f >= 0) {
1289 if (get_xattr(fname, &sx) < 0)
1294 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1296 flist_expand(flist, 1);
1297 flist->files[flist->used++] = file;
1299 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1301 if (preserve_acls && !S_ISLNK(file->mode)) {
1306 #ifdef SUPPORT_XATTRS
1307 if (preserve_xattrs) {
1308 F_XATTR(file) = send_xattr(&sx, f);
1316 static void send_if_directory(int f, struct file_list *flist,
1317 struct file_struct *file,
1318 char *fbuf, unsigned int ol,
1321 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1323 if (S_ISDIR(file->mode)
1324 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1326 unsigned int len = strlen(fbuf);
1327 if (len > 1 && fbuf[len-1] == '/')
1329 if (len >= MAXPATHLEN - 1) {
1330 io_error |= IOERR_GENERAL;
1331 rprintf(FERROR, "skipping long-named directory: %s\n",
1335 save_filters = push_local_filters(fbuf, len);
1336 send_directory(f, flist, fbuf, len, flags);
1337 pop_local_filters(save_filters);
1344 static int file_compare(const void *file1, const void *file2)
1346 return f_name_cmp(*(struct file_struct **)file1,
1347 *(struct file_struct **)file2);
1350 /* The guts of a merge-sort algorithm. This was derived from the glibc
1351 * version, but I (Wayne) changed the merge code to do less copying and
1352 * to require only half the amount of temporary memory. */
1353 static void fsort_tmp(struct file_struct **fp, size_t num,
1354 struct file_struct **tmp)
1356 struct file_struct **f1, **f2, **t;
1365 fsort_tmp(f1, n1, tmp);
1367 fsort_tmp(f2, n2, tmp);
1369 while (f_name_cmp(*f1, *f2) <= 0) {
1376 memcpy(t, f1, n1 * PTR_SIZE);
1378 *f1++ = *f2++, n2--;
1380 while (n1 > 0 && n2 > 0) {
1381 if (f_name_cmp(*t, *f2) <= 0)
1384 *f1++ = *f2++, n2--;
1388 memcpy(f1, t, n1 * PTR_SIZE);
1391 /* This file-struct sorting routine makes sure that any identical names in
1392 * the file list stay in the same order as they were in the original list.
1393 * This is particularly vital in inc_recurse mode where we expect a sort
1394 * on the flist to match the exact order of a sort on the dir_flist. */
1395 static void fsort(struct file_struct **fp, size_t num)
1401 qsort(fp, num, PTR_SIZE, file_compare);
1403 struct file_struct **tmp = new_array(struct file_struct *,
1405 fsort_tmp(fp, num, tmp);
1410 /* We take an entire set of sibling dirs from the sorted flist and link them
1411 * into the tree, setting the appropriate parent/child/sibling pointers. */
1412 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1417 int32 *parent_dp = parent_ndx < 0 ? NULL
1418 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1420 flist_expand(dir_flist, dir_cnt);
1421 dir_flist->sorted = dir_flist->files;
1423 for (i = 0; dir_cnt; i++) {
1424 struct file_struct *file = from_flist->sorted[i];
1426 if (!S_ISDIR(file->mode))
1429 dir_flist->files[dir_flist->used++] = file;
1432 if (file->flags & FLAG_MOUNT_DIR)
1436 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1438 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1440 send_dir_ndx = dir_flist->used - 1;
1442 dp = F_DIR_NODE_P(file);
1443 DIR_PARENT(dp) = parent_ndx;
1444 DIR_FIRST_CHILD(dp) = -1;
1447 DIR_NEXT_SIBLING(dp) = -1;
1450 /* This function is normally called by the sender, but the receiving side also
1451 * calls it from get_dirlist() with f set to -1 so that we just construct the
1452 * file list in memory without sending it over the wire. Also, get_dirlist()
1453 * might call this with f set to -2, which also indicates that local filter
1454 * rules should be ignored. */
1455 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1462 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1463 int start = flist->used;
1464 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1466 assert(flist != NULL);
1468 if (!(d = opendir(fbuf))) {
1469 io_error |= IOERR_GENERAL;
1470 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1475 if (len != 1 || *fbuf != '/')
1478 remainder = MAXPATHLEN - (p - fbuf);
1480 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1481 char *dname = d_name(di);
1482 if (dname[0] == '.' && (dname[1] == '\0'
1483 || (dname[1] == '.' && dname[2] == '\0')))
1485 if (strlcpy(p, dname, remainder) >= remainder) {
1486 io_error |= IOERR_GENERAL;
1488 "cannot send long-named file %s\n",
1492 if (dname[0] == '\0') {
1493 io_error |= IOERR_GENERAL;
1495 "cannot send file with empty name in %s\n",
1500 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1506 io_error |= IOERR_GENERAL;
1507 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1512 if (f >= 0 && recurse && !divert_dirs) {
1513 int i, end = flist->used - 1;
1514 /* send_if_directory() bumps flist->used, so use "end". */
1515 for (i = start; i <= end; i++)
1516 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1520 static char lastpath[MAXPATHLEN] = "";
1521 static int lastpath_len = 0;
1522 static struct file_struct *lastpath_struct;
1524 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1525 char *start, char *limit, int flags, int is_dot_dir)
1527 struct file_struct *file;
1528 item_list *rel_list;
1530 int len, need_new_dir;
1532 flags &= ~FLAG_XFER_DIR;
1535 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1536 && lastpath_len == limit - fname
1537 && strncmp(lastpath, fname, lastpath_len) == 0)
1545 int save_copy_links = copy_links;
1546 int save_xfer_dirs = xfer_dirs;
1548 copy_links |= copy_unsafe_links;
1553 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1555 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1559 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1561 if (file && !S_ISDIR(file->mode))
1564 memset(F_DIR_RELS_P(file), 0, sizeof (item_list*));
1565 lastpath_struct = file;
1568 strlcpy(lastpath, fname, sizeof lastpath);
1569 lastpath_len = limit - fname;
1573 copy_links = save_copy_links;
1574 xfer_dirs = save_xfer_dirs;
1580 if (!lastpath_struct)
1581 return; /* dir must have vanished */
1583 len = strlen(limit+1);
1584 memcpy(&rel_list, F_DIR_RELS_P(lastpath_struct), sizeof rel_list);
1586 if (!(rel_list = new0(item_list)))
1587 out_of_memory("send_implied_dirs");
1588 memcpy(F_DIR_RELS_P(lastpath_struct), &rel_list, sizeof rel_list);
1590 ep = EXPAND_ITEM_LIST(rel_list, char *, 32);
1591 if (!(*ep = new_array(char, 1 + len + 1)))
1592 out_of_memory("send_implied_dirs");
1594 strlcpy(*ep + 1, limit+1, len + 1);
1597 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1599 char fbuf[MAXPATHLEN];
1600 item_list *rel_list;
1601 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1605 dlen = strlen(fbuf);
1607 if (F_PATHNAME(file) != pathname) {
1608 if (!push_pathname(F_PATHNAME(file), -1))
1609 exit_cleanup(RERR_FILESELECT);
1612 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1614 if (file->flags & FLAG_XFER_DIR)
1615 send_directory(f, flist, fbuf, dlen, flags);
1617 if (!relative_paths)
1620 memcpy(&rel_list, F_DIR_RELS_P(file), sizeof rel_list);
1624 for (j = 0; j < rel_list->count; j++) {
1625 char *slash, *ep = ((char**)rel_list->items)[j];
1626 int is_dot_dir = *ep;
1629 len = strlcpy(fbuf + dlen + 1, ep+1, sizeof fbuf - dlen - 1);
1631 if (len >= (int)sizeof fbuf)
1632 continue; /* Impossible... */
1634 slash = strchr(fbuf+dlen+1, '/');
1636 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
1642 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1643 io_error |= IOERR_GENERAL;
1644 rsyserr(FERROR, errno, "link_stat %s failed",
1648 send_file_name(f, flist, fbuf, &st,
1649 recurse || xfer_dirs ? FLAG_TOP_DIR | flags : flags,
1652 send_file_name(f, flist, fbuf, NULL,
1653 recurse ? FLAG_TOP_DIR | flags : flags,
1661 void send_extra_file_list(int f, int at_least)
1663 struct file_list *flist;
1665 int future_cnt, save_io_error = io_error;
1670 /* Keep sending data until we have the requested number of
1671 * files in the upcoming file-lists. */
1672 if (cur_flist->next) {
1673 flist = first_flist->prev; /* the newest flist */
1674 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1677 while (future_cnt < at_least) {
1678 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1679 int dir_ndx, dstart = dir_count;
1680 const char *pathname = F_PATHNAME(file);
1683 flist = flist_new(0, "send_extra_file_list");
1684 start_write = stats.total_written;
1688 dir_ndx = F_NDX(file);
1691 dir_ndx = send_dir_ndx;
1692 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1693 flist->parent_ndx = dir_ndx;
1695 send1extra(f, file, flist);
1696 dp = F_DIR_NODE_P(file);
1698 /* If there are any duplicate directory names that follow, we
1699 * send all the dirs together in one file-list. The dir_flist
1700 * tree links all the child subdirs onto the last dup dir. */
1701 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1702 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1703 send_dir_ndx = dir_ndx;
1704 file = dir_flist->sorted[dir_ndx];
1705 if (F_PATHNAME(file) != pathname)
1706 send1extra(f, file, flist);
1707 dp = F_DIR_NODE_P(file);
1713 if (need_unsorted_flist) {
1714 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1715 out_of_memory("send_extra_file_list");
1716 memcpy(flist->sorted, flist->files,
1717 flist->used * sizeof (struct file_struct*));
1720 flist->sorted = flist->files;
1722 clean_flist(flist, 0);
1724 flist->ndx_end = flist->ndx_start + flist->used - 1
1725 - (dir_count - dstart);
1727 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1728 flist_done_allocating(flist);
1730 file_total += flist->used;
1731 future_cnt += flist->used;
1732 stats.flist_size += stats.total_written - start_write;
1733 stats.num_files += flist->used;
1735 output_flist(flist);
1737 if (DIR_FIRST_CHILD(dp) >= 0) {
1738 send_dir_ndx = DIR_FIRST_CHILD(dp);
1741 while (DIR_NEXT_SIBLING(dp) < 0) {
1742 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1743 write_ndx(f, NDX_FLIST_EOF);
1745 change_local_filter_dir(NULL, 0, 0);
1749 file = dir_flist->sorted[send_dir_ndx];
1750 dp = F_DIR_NODE_P(file);
1752 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1757 if (io_error != save_io_error && !ignore_errors)
1758 send_msg_int(MSG_IO_ERROR, io_error);
1761 struct file_list *send_file_list(int f, int argc, char *argv[])
1763 static const char *lastdir;
1764 static int lastdir_len = -1;
1768 struct file_list *flist;
1769 struct timeval start_tv, end_tv;
1772 int flags, disable_buffering;
1773 int reading_remotely = filesfrom_host != NULL;
1774 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1776 | (filesfrom_convert ? RL_CONVERT : 0)
1778 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1780 rprintf(FLOG, "building file list\n");
1781 if (show_filelist_p())
1782 start_filelist_progress("building file list");
1783 else if (inc_recurse && verbose && !am_server)
1784 rprintf(FCLIENT, "sending incremental file list\n");
1786 start_write = stats.total_written;
1787 gettimeofday(&start_tv, NULL);
1789 #ifdef SUPPORT_HARD_LINKS
1790 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1794 flist = cur_flist = flist_new(0, "send_file_list");
1796 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1797 flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1799 dir_flist = cur_flist;
1800 flags = FLAG_XFER_DIR;
1803 disable_buffering = io_start_buffering_out(f);
1804 if (filesfrom_fd >= 0) {
1805 if (argv[0] && !push_dir(argv[0], 0)) {
1806 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1807 full_fname(argv[0]), curr_dir);
1808 exit_cleanup(RERR_FILESELECT);
1814 char fbuf[MAXPATHLEN];
1819 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1821 sanitize_path(fbuf, fbuf, "", 0, NULL);
1825 strlcpy(fbuf, *argv++, MAXPATHLEN);
1827 sanitize_path(fbuf, fbuf, "", 0, NULL);
1831 if (relative_paths) {
1832 /* We clean up fbuf below. */
1834 } else if (!len || fbuf[len - 1] == '/') {
1835 if (len == 2 && fbuf[0] == '.') {
1836 /* Turn "./" into just "." rather than "./." */
1839 if (len + 1 >= MAXPATHLEN)
1840 overflow_exit("send_file_list");
1845 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1846 && (len == 2 || fbuf[len-3] == '/')) {
1847 if (len + 2 >= MAXPATHLEN)
1848 overflow_exit("send_file_list");
1854 is_dot_dir = fbuf[len-1] == '.'
1855 && (len == 1 || fbuf[len-2] == '/');
1860 if (!relative_paths) {
1861 p = strrchr(fbuf, '/');
1868 len -= p - fbuf + 1;
1873 if ((p = strstr(fbuf, "/./")) != NULL) {
1879 len -= p - fbuf + 3;
1883 /* Get rid of trailing "/" and "/.". */
1885 if (fn[len - 1] == '/') {
1887 if (!--len && !dir) {
1892 else if (len >= 2 && fn[len - 1] == '.'
1893 && fn[len - 2] == '/') {
1895 if (!(len -= 2) && !dir) {
1902 if (len == 1 && fn[0] == '/')
1905 /* Reject a ".." dir in the active part of the path. */
1906 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1907 if ((p[2] == '/' || p[2] == '\0')
1908 && (p == fn || p[-1] == '/')) {
1910 "found \"..\" dir in relative path: %s\n",
1912 exit_cleanup(RERR_SYNTAX);
1922 dirlen = dir ? strlen(dir) : 0;
1923 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1924 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1927 lastdir_len = pathname_len;
1928 } else if (!push_pathname(lastdir, lastdir_len))
1932 memmove(fbuf, fn, len + 1);
1934 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1935 io_error |= IOERR_GENERAL;
1936 rsyserr(FERROR, errno, "link_stat %s failed",
1941 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1942 rprintf(FINFO, "skipping directory %s\n", fbuf);
1946 if (inc_recurse && relative_paths && *fbuf) {
1947 if ((p = strchr(fbuf+1, '/')) != NULL) {
1948 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, is_dot_dir);
1951 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1952 /* Send the implied directories at the start of the
1953 * source spec, so we get their permissions right. */
1954 char *lp = lastpath, *slash = fbuf;
1956 /* Skip any initial directories in our path that we
1957 * have in common with lastpath. */
1958 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1963 if (fn != p || (*lp && *lp != '/'))
1964 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1967 if (recurse || (xfer_dirs && is_dot_dir)) {
1968 struct file_struct *file;
1969 int top_flags = FLAG_TOP_DIR | flags;
1970 file = send_file_name(f, flist, fbuf, &st,
1971 top_flags, ALL_FILTERS);
1972 if (file && !inc_recurse)
1973 send_if_directory(f, flist, file, fbuf, len, flags);
1975 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1978 gettimeofday(&end_tv, NULL);
1979 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1980 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1981 if (stats.flist_buildtime == 0)
1982 stats.flist_buildtime = 1;
1985 write_byte(f, 0); /* Indicate end of file list */
1987 #ifdef SUPPORT_HARD_LINKS
1988 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1992 if (show_filelist_p())
1993 finish_filelist_progress(flist);
1995 gettimeofday(&end_tv, NULL);
1996 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1997 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1999 /* When converting names, both sides keep an unsorted file-list array
2000 * because the names will differ on the sending and receiving sides
2001 * (both sides will use the unsorted index number for each item). */
2003 /* Sort the list without removing any duplicates. This allows the
2004 * receiving side to ask for whatever name it kept. For incremental
2005 * recursion mode, the sender marks duplicate dirs so that it can
2006 * send them together in a single file-list. */
2008 if (need_unsorted_flist) {
2010 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2011 out_of_memory("send_file_list");
2012 memcpy(flist->sorted, flist->files,
2013 flist->used * sizeof (struct file_struct*));
2014 clean_flist(flist, 0);
2016 flist->sorted = flist->files;
2018 flist->high = flist->used - 1;
2023 flist->sorted = flist->files;
2024 clean_flist(flist, 0);
2026 file_total += flist->used;
2028 /* We don't subtract dir_count for the first send since we
2029 * might have one or more dot dirs which need to get sent. */
2030 flist->ndx_end = flist->ndx_start + flist->used - 1;
2032 if (!numeric_ids && !inc_recurse)
2035 /* send the io_error flag */
2036 if (protocol_version < 30)
2037 write_int(f, ignore_errors ? 0 : io_error);
2038 else if (io_error && !ignore_errors)
2039 send_msg_int(MSG_IO_ERROR, io_error);
2041 if (disable_buffering)
2042 io_end_buffering_out();
2044 stats.flist_size = stats.total_written - start_write;
2045 stats.num_files = flist->used;
2048 output_flist(flist);
2051 rprintf(FINFO, "send_file_list done\n");
2054 add_dirs_to_tree(-1, flist, dir_count);
2055 flist_done_allocating(flist);
2056 if (send_dir_ndx < 0) {
2057 write_ndx(f, NDX_FLIST_EOF);
2060 else if (file_total == 1) {
2061 /* If we're creating incremental file-lists and there
2062 * was just 1 item in the first file-list, send 1 more
2063 * file-list to check if this is a 1-file xfer. */
2064 send_extra_file_list(f, 1);
2071 struct file_list *recv_file_list(int f)
2073 struct file_list *flist;
2078 rprintf(FLOG, "receiving file list\n");
2079 if (show_filelist_p())
2080 start_filelist_progress("receiving file list");
2081 else if (inc_recurse && verbose && !am_server && !first_flist)
2082 rprintf(FCLIENT, "receiving incremental file list\n");
2084 start_read = stats.total_read;
2086 #ifdef SUPPORT_HARD_LINKS
2087 if (preserve_hard_links && !first_flist)
2091 flist = flist_new(0, "recv_file_list");
2094 if (flist->ndx_start == 0)
2095 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2096 dstart = dir_flist->used;
2102 while ((flags = read_byte(f)) != 0) {
2103 struct file_struct *file;
2105 flist_expand(flist, 1);
2107 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2108 flags |= read_byte(f) << 8;
2109 file = recv_file_entry(flist, flags, f);
2111 if (inc_recurse && S_ISDIR(file->mode)) {
2112 flist_expand(dir_flist, 1);
2113 dir_flist->files[dir_flist->used++] = file;
2116 flist->files[flist->used++] = file;
2118 maybe_emit_filelist_progress(flist->used);
2121 rprintf(FINFO, "recv_file_name(%s)\n",
2122 f_name(file, NULL));
2125 file_total += flist->used;
2127 flist->ndx_end = flist->ndx_start + flist->used - 1;
2128 if (inc_recurse && flist->ndx_start)
2129 flist->ndx_end -= dir_flist->used - dstart;
2132 rprintf(FINFO, "received %d names\n", flist->used);
2134 if (show_filelist_p())
2135 finish_filelist_progress(flist);
2138 if (need_unsorted_flist) {
2139 /* Create an extra array of index pointers that we can sort for
2140 * the generator's use (for wading through the files in sorted
2141 * order and for calling flist_find()). We keep the "files"
2142 * list unsorted for our exchange of index numbers with the
2143 * other side (since their names may not sort the same). */
2144 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2145 out_of_memory("recv_file_list");
2146 memcpy(flist->sorted, flist->files,
2147 flist->used * sizeof (struct file_struct*));
2148 if (inc_recurse && dir_flist->used > dstart) {
2149 dir_flist->sorted = realloc_array(dir_flist->sorted,
2150 struct file_struct *,
2152 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2153 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2154 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2159 flist->sorted = flist->files;
2160 if (inc_recurse && dir_flist->used > dstart) {
2161 dir_flist->sorted = dir_flist->files;
2162 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2167 flist_done_allocating(flist);
2169 recv_id_list(f, flist);
2171 clean_flist(flist, relative_paths);
2173 if (protocol_version < 30) {
2174 /* Recv the io_error flag */
2178 io_error |= read_int(f);
2182 output_flist(flist);
2186 for (i = flist->low; i <= flist->high; i++)
2187 list_file_entry(flist->files[i]);
2191 rprintf(FINFO, "recv_file_list done\n");
2193 stats.flist_size += stats.total_read - start_read;
2194 stats.num_files += flist->used;
2199 /* This is only used once by the receiver if the very first file-list
2200 * has exactly one item in it. */
2201 void recv_additional_file_list(int f)
2203 struct file_list *flist;
2204 int ndx = read_ndx(f);
2205 if (ndx == NDX_FLIST_EOF) {
2207 change_local_filter_dir(NULL, 0, 0);
2209 ndx = NDX_FLIST_OFFSET - ndx;
2210 if (ndx < 0 || ndx >= dir_flist->used) {
2211 ndx = NDX_FLIST_OFFSET - ndx;
2213 "[%s] Invalid dir index: %d (%d - %d)\n",
2214 who_am_i(), ndx, NDX_FLIST_OFFSET,
2215 NDX_FLIST_OFFSET - dir_flist->used + 1);
2216 exit_cleanup(RERR_PROTOCOL);
2219 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2222 flist = recv_file_list(f);
2223 flist->parent_ndx = ndx;
2227 /* Search for an identically-named item in the file list. Note that the
2228 * items must agree in their directory-ness, or no match is returned. */
2229 int flist_find(struct file_list *flist, struct file_struct *f)
2231 int low = flist->low, high = flist->high;
2232 int diff, mid, mid_up;
2234 while (low <= high) {
2235 mid = (low + high) / 2;
2236 if (F_IS_ACTIVE(flist->sorted[mid]))
2239 /* Scan for the next non-empty entry using the cached
2240 * distance values. If the value isn't fully up-to-
2241 * date, update it. */
2242 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2243 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2245 mid_up += F_DEPTH(flist->sorted[mid_up]);
2246 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2247 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2249 if (mid_up > high) {
2250 /* If there's nothing left above us, set high to
2251 * a non-empty entry below us and continue. */
2252 high = mid - (int)flist->sorted[mid]->len32;
2253 if (!F_IS_ACTIVE(flist->sorted[high])) {
2255 high -= (int)flist->sorted[high]->len32;
2256 } while (!F_IS_ACTIVE(flist->sorted[high]));
2257 flist->sorted[mid]->len32 = mid - high;
2262 diff = f_name_cmp(flist->sorted[mid_up], f);
2264 if (protocol_version < 29
2265 && S_ISDIR(flist->sorted[mid_up]->mode)
2266 != S_ISDIR(f->mode))
2279 * Free up any resources a file_struct has allocated
2280 * and clear the file.
2282 void clear_file(struct file_struct *file)
2284 /* The +1 zeros out the first char of the basename. */
2285 memset(file, 0, FILE_STRUCT_LEN + 1);
2286 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2287 * entry. Likewise for len32 in the opposite direction. We assume
2288 * that we're alone for now since flist_find() will adjust the counts
2289 * it runs into that aren't up-to-date. */
2290 file->len32 = F_DEPTH(file) = 1;
2293 /* Allocate a new file list. */
2294 struct file_list *flist_new(int flags, char *msg)
2296 struct file_list *flist;
2298 if (!(flist = new0(struct file_list)))
2301 if (flags & FLIST_TEMP) {
2302 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2303 out_of_memory, POOL_INTERN)))
2306 /* This is a doubly linked list with prev looping back to
2307 * the end of the list, but the last next pointer is NULL. */
2309 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2310 out_of_memory, POOL_INTERN);
2311 if (!flist->file_pool)
2314 first_flist = cur_flist = flist->prev = flist;
2316 flist->file_pool = first_flist->file_pool;
2318 flist->ndx_start = first_flist->prev->ndx_end + 2;
2319 if (flist->ndx_start <= high_hlink_ndx)
2320 flist->ndx_start = high_hlink_ndx + 1;
2322 flist->prev = first_flist->prev;
2323 flist->prev->next = first_flist->prev = flist;
2325 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2332 /* Free up all elements in a flist. */
2333 void flist_free(struct file_list *flist)
2336 /* Was FLIST_TEMP dir-list. */
2337 } else if (flist == flist->prev) {
2338 first_flist = cur_flist = NULL;
2342 if (flist == cur_flist)
2343 cur_flist = flist->next;
2344 if (flist == first_flist)
2345 first_flist = first_flist->next;
2347 flist->prev->next = flist->next;
2349 flist->next = first_flist;
2351 flist->next->prev = flist->prev;
2352 file_total -= flist->used;
2356 if (!flist->prev || !flist_cnt)
2357 pool_destroy(flist->file_pool);
2359 pool_free_old(flist->file_pool, flist->pool_boundary);
2361 if (flist->sorted && flist->sorted != flist->files)
2362 free(flist->sorted);
2367 /* This routine ensures we don't have any duplicate names in our file list.
2368 * duplicate names can cause corruption because of the pipelining. */
2369 static void clean_flist(struct file_list *flist, int strip_root)
2371 char fbuf[MAXPATHLEN];
2376 if (flist->used == 0) {
2382 fsort(flist->sorted, flist->used);
2384 if (!am_sender || inc_recurse) {
2385 for (i = prev_i = 0; i < flist->used; i++) {
2386 if (F_IS_ACTIVE(flist->sorted[i])) {
2391 flist->low = prev_i;
2393 i = prev_i = flist->used - 1;
2397 while (++i < flist->used) {
2399 struct file_struct *file = flist->sorted[i];
2401 if (!F_IS_ACTIVE(file))
2403 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2405 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2406 int save_mode = file->mode;
2407 /* Make sure that this directory doesn't duplicate a
2408 * non-directory earlier in the list. */
2409 flist->high = prev_i;
2410 file->mode = S_IFREG;
2411 j = flist_find(flist, file);
2412 file->mode = save_mode;
2417 /* If one is a dir and the other is not, we want to
2418 * keep the dir because it might have contents in the
2419 * list. Otherwise keep the first one. */
2420 if (S_ISDIR(file->mode)) {
2421 struct file_struct *fp = flist->sorted[j];
2422 if (!S_ISDIR(fp->mode))
2426 file->flags |= FLAG_DUPLICATE;
2435 "removing duplicate name %s from file list (%d)\n",
2436 f_name(file, fbuf), drop + flist->ndx_start);
2438 /* Make sure we don't lose track of a user-specified
2440 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2441 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2443 clear_file(flist->sorted[drop]);
2447 if (flist->low == drop) {
2449 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2458 flist->high = prev_i;
2461 /* We need to strip off the leading slashes for relative
2462 * paths, but this must be done _after_ the sorting phase. */
2463 for (i = flist->low; i <= flist->high; i++) {
2464 struct file_struct *file = flist->sorted[i];
2468 while (*file->dirname == '/')
2470 if (!*file->dirname)
2471 file->dirname = NULL;
2475 if (prune_empty_dirs && !am_sender) {
2476 int j, prev_depth = 0;
2478 prev_i = 0; /* It's OK that this isn't really true. */
2480 for (i = flist->low; i <= flist->high; i++) {
2481 struct file_struct *fp, *file = flist->sorted[i];
2483 /* This temporarily abuses the F_DEPTH() value for a
2484 * directory that is in a chain that might get pruned.
2485 * We restore the old value if it gets a reprieve. */
2486 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2487 /* Dump empty dirs when coming back down. */
2488 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2489 fp = flist->sorted[prev_i];
2490 if (F_DEPTH(fp) >= 0)
2492 prev_i = -F_DEPTH(fp)-1;
2495 prev_depth = F_DEPTH(file);
2496 if (is_excluded(f_name(file, fbuf), 1,
2498 /* Keep dirs through this dir. */
2499 for (j = prev_depth-1; ; j--) {
2500 fp = flist->sorted[prev_i];
2501 if (F_DEPTH(fp) >= 0)
2503 prev_i = -F_DEPTH(fp)-1;
2507 F_DEPTH(file) = -prev_i-1;
2510 /* Keep dirs through this non-dir. */
2511 for (j = prev_depth; ; j--) {
2512 fp = flist->sorted[prev_i];
2513 if (F_DEPTH(fp) >= 0)
2515 prev_i = -F_DEPTH(fp)-1;
2520 /* Dump all remaining empty dirs. */
2522 struct file_struct *fp = flist->sorted[prev_i];
2523 if (F_DEPTH(fp) >= 0)
2525 prev_i = -F_DEPTH(fp)-1;
2529 for (i = flist->low; i <= flist->high; i++) {
2530 if (F_IS_ACTIVE(flist->sorted[i]))
2534 for (i = flist->high; i >= flist->low; i--) {
2535 if (F_IS_ACTIVE(flist->sorted[i]))
2542 static void output_flist(struct file_list *flist)
2544 char uidbuf[16], gidbuf[16], depthbuf[16];
2545 struct file_struct *file;
2546 const char *root, *dir, *slash, *name, *trail;
2547 const char *who = who_am_i();
2550 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2551 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2552 for (i = 0; i < flist->used; i++) {
2553 file = flist->sorted[i];
2554 if ((am_root || am_sender) && uid_ndx) {
2555 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2560 static char parens[] = "(\0)\0\0\0";
2561 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2562 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2563 pp, F_GROUP(file), pp + 2);
2567 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2568 if (F_IS_ACTIVE(file)) {
2569 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2570 if ((dir = file->dirname) == NULL)
2574 name = file->basename;
2575 trail = S_ISDIR(file->mode) ? "/" : "";
2577 root = dir = slash = name = trail = "";
2579 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2580 who, i + flist->ndx_start,
2581 root, dir, slash, name, trail,
2582 (int)file->mode, (double)F_LENGTH(file),
2583 uidbuf, gidbuf, file->flags);
2587 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2588 enum fnc_type { t_PATH, t_ITEM };
2590 static int found_prefix;
2592 /* Compare the names of two file_struct entities, similar to how strcmp()
2593 * would do if it were operating on the joined strings.
2595 * Some differences beginning with protocol_version 29: (1) directory names
2596 * are compared with an assumed trailing slash so that they compare in a
2597 * way that would cause them to sort immediately prior to any content they
2598 * may have; (2) a directory of any name compares after a non-directory of
2599 * any name at the same depth; (3) a directory with name "." compares prior
2600 * to anything else. These changes mean that a directory and a non-dir
2601 * with the same name will not compare as equal (protocol_version >= 29).
2603 * The dirname component can be an empty string, but the basename component
2604 * cannot (and never is in the current codebase). The basename component
2605 * may be NULL (for a removed item), in which case it is considered to be
2606 * after any existing item. */
2607 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2610 const uchar *c1, *c2;
2611 enum fnc_state state1, state2;
2612 enum fnc_type type1, type2;
2613 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2615 if (!f1 || !F_IS_ACTIVE(f1)) {
2616 if (!f2 || !F_IS_ACTIVE(f2))
2620 if (!f2 || !F_IS_ACTIVE(f2))
2623 c1 = (uchar*)f1->dirname;
2624 c2 = (uchar*)f2->dirname;
2628 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2629 c1 = (const uchar*)f1->basename;
2630 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2632 state1 = s_TRAILING;
2641 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2642 c2 = (const uchar*)f2->basename;
2643 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2645 state2 = s_TRAILING;
2655 return type1 == t_PATH ? 1 : -1;
2665 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2666 c1 = (const uchar*)f1->basename;
2667 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2669 state1 = s_TRAILING;
2675 state1 = s_TRAILING;
2676 if (type1 == t_PATH) {
2685 if (*c2 && type1 != type2)
2686 return type1 == t_PATH ? 1 : -1;
2695 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2696 c2 = (const uchar*)f2->basename;
2697 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2699 state2 = s_TRAILING;
2705 state2 = s_TRAILING;
2706 if (type2 == t_PATH) {
2719 return type1 == t_PATH ? 1 : -1;
2721 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2726 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2727 * not match if f2's basename is not an exact match of a path element in f1.
2728 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2729 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2733 return found_prefix;
2736 char *f_name_buf(void)
2738 static char names[5][MAXPATHLEN];
2739 static unsigned int n;
2741 n = (n + 1) % (sizeof names / sizeof names[0]);
2746 /* Return a copy of the full filename of a flist entry, using the indicated
2747 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2748 * done because we checked the size when creating the file_struct entry.
2750 char *f_name(const struct file_struct *f, char *fbuf)
2752 if (!f || !F_IS_ACTIVE(f))
2756 fbuf = f_name_buf();
2759 int len = strlen(f->dirname);
2760 memcpy(fbuf, f->dirname, len);
2762 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2764 strlcpy(fbuf, f->basename, MAXPATHLEN);
2769 /* Do a non-recursive scan of the named directory, possibly ignoring all
2770 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2771 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2772 * buffer (the functions we call will append names onto the end, but the old
2773 * dir value will be restored on exit). */
2774 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2776 struct file_list *dirlist;
2777 char dirbuf[MAXPATHLEN];
2778 int save_recurse = recurse;
2779 int save_xfer_dirs = xfer_dirs;
2780 int save_prune_empty_dirs = prune_empty_dirs;
2783 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2784 if (dlen >= MAXPATHLEN)
2789 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2793 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2794 xfer_dirs = save_xfer_dirs;
2795 recurse = save_recurse;
2797 flist_count_offset += dirlist->used;
2799 prune_empty_dirs = 0;
2800 dirlist->sorted = dirlist->files;
2801 clean_flist(dirlist, 0);
2802 prune_empty_dirs = save_prune_empty_dirs;
2805 output_flist(dirlist);