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.
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int do_progress;
36 extern int always_checksum;
38 extern int ignore_errors;
39 extern int numeric_ids;
43 extern int filesfrom_fd;
44 extern int one_file_system;
45 extern int copy_dirlinks;
46 extern int keep_dirlinks;
47 extern int preserve_acls;
48 extern int preserve_xattrs;
49 extern int preserve_links;
50 extern int preserve_hard_links;
51 extern int preserve_devices;
52 extern int preserve_specials;
56 extern int relative_paths;
57 extern int implied_dirs;
58 extern int file_extra_cnt;
59 extern int ignore_perishable;
60 extern int non_perishable_cnt;
61 extern int prune_empty_dirs;
62 extern int copy_links;
63 extern int copy_unsafe_links;
64 extern int protocol_version;
65 extern int sanitize_paths;
66 extern struct stats stats;
67 extern char *filesfrom_host;
69 extern char curr_dir[MAXPATHLEN];
71 extern struct chmod_mode_struct *chmod_modes;
73 extern struct filter_list_struct filter_list;
74 extern struct filter_list_struct server_filter_list;
78 extern int filesfrom_convert;
79 extern int need_unsorted_flist;
80 extern iconv_t ic_send, ic_recv;
83 #define PTR_SIZE (sizeof (struct file_struct *))
87 dev_t filesystem_dev; /* used to implement -x */
89 struct file_list *cur_flist, *first_flist, *dir_flist;
90 int send_dir_ndx = -1, send_dir_depth = 0;
91 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
92 int file_total = 0; /* total of all active items over all file-lists */
93 int flist_eof = 0; /* all the file-lists are now known */
95 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
96 * internally, even if this platform does not allow files to have 64-bit inums.
97 * The only exception is if we're on a platform with no 64-bit type at all.
99 * Because we use read_longint() to get these off the wire, if you transfer
100 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
101 * machine with no 64-bit types then you will get an overflow error.
103 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
104 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
105 * be truncated. But it's a kind of silly thing to do anyhow. */
107 /* The tmp_* vars are used as a cache area by make_file() to store data
108 * that the sender doesn't need to remember in its file list. The data
109 * will survive just long enough to be used by send_file_entry(). */
110 static dev_t tmp_rdev;
111 #ifdef SUPPORT_HARD_LINKS
112 static int64 tmp_dev, tmp_ino;
114 static char tmp_sum[MAX_DIGEST_LEN];
116 static char empty_sum[MAX_DIGEST_LEN];
117 static int flist_count_offset; /* for --delete --progress */
118 static int dir_count = 0;
119 static int high_hlink_ndx;
121 static void clean_flist(struct file_list *flist, int strip_root);
122 static void output_flist(struct file_list *flist);
124 void init_flist(void)
127 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
128 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
130 checksum_len = protocol_version < 21 ? 2
131 : protocol_version < 30 ? MD4_DIGEST_LEN
135 static int show_filelist_p(void)
137 return verbose && xfer_dirs && !am_server && !inc_recurse;
140 static void start_filelist_progress(char *kind)
142 rprintf(FCLIENT, "%s ... ", kind);
143 if (verbose > 1 || do_progress)
144 rprintf(FCLIENT, "\n");
148 static void emit_filelist_progress(int count)
150 rprintf(FCLIENT, " %d files...\r", count);
153 static void maybe_emit_filelist_progress(int count)
155 if (do_progress && show_filelist_p() && (count % 100) == 0)
156 emit_filelist_progress(count);
159 static void finish_filelist_progress(const struct file_list *flist)
162 /* This overwrites the progress line */
163 rprintf(FINFO, "%d file%sto consider\n",
164 flist->used, flist->used == 1 ? " " : "s ");
166 rprintf(FINFO, "done\n");
169 void show_flist_stats(void)
174 /* Stat either a symlink or its referent, depending on the settings of
175 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
177 * If path is the name of a symlink, then the linkbuf buffer (which must hold
178 * MAXPATHLEN chars) will be set to the symlink's target string.
180 * The stat structure pointed to by stp will contain information about the
181 * link or the referent as appropriate, if they exist. */
182 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
185 if (link_stat(path, stp, copy_dirlinks) < 0)
187 if (S_ISLNK(stp->st_mode)) {
188 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
191 linkbuf[llen] = '\0';
192 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
194 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
197 return x_stat(path, stp, NULL);
202 return x_stat(path, stp, NULL);
206 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
210 return x_stat(path, stp, NULL);
211 if (x_lstat(path, stp, NULL) < 0)
213 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
215 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
220 return x_stat(path, stp, NULL);
224 /* This function is used to check if a file should be included/excluded
225 * from the list of files based on its name and type etc. The value of
226 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
227 static int is_excluded(char *fname, int is_dir, int filter_level)
229 #if 0 /* This currently never happens, so avoid a useless compare. */
230 if (filter_level == NO_FILTERS)
234 /* never exclude '.', even if somebody does --exclude '*' */
235 if (fname[0] == '.' && !fname[1])
237 /* Handle the -R version of the '.' dir. */
238 if (fname[0] == '/') {
239 int len = strlen(fname);
240 if (fname[len-1] == '.' && fname[len-2] == '/')
244 if (server_filter_list.head
245 && check_filter(&server_filter_list, fname, is_dir) < 0)
247 if (filter_level != ALL_FILTERS)
250 && check_filter(&filter_list, fname, is_dir) < 0)
255 static void send_directory(int f, struct file_list *flist,
256 char *fbuf, int len, int flags);
258 static const char *pathname, *orig_dir;
259 static int pathname_len;
262 /* Make sure flist can hold at least flist->used + extra entries. */
263 static void flist_expand(struct file_list *flist, int extra)
265 struct file_struct **new_ptr;
267 if (flist->used + extra <= flist->malloced)
270 if (flist->malloced < FLIST_START)
271 flist->malloced = FLIST_START;
272 else if (flist->malloced >= FLIST_LINEAR)
273 flist->malloced += FLIST_LINEAR;
275 flist->malloced *= 2;
277 /* In case count jumped or we are starting the list
278 * with a known size just set it. */
279 if (flist->malloced < flist->used + extra)
280 flist->malloced = flist->used + extra;
282 new_ptr = realloc_array(flist->files, struct file_struct *,
285 if (verbose >= 2 && flist->malloced != FLIST_START) {
286 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
288 (double)sizeof flist->files[0] * flist->malloced,
289 (new_ptr == flist->files) ? " not" : "");
292 flist->files = new_ptr;
295 out_of_memory("flist_expand");
298 static void flist_done_allocating(struct file_list *flist)
300 void *ptr = pool_boundary(flist->file_pool, 8*1024);
301 if (flist->pool_boundary == ptr)
302 flist->pool_boundary = NULL; /* list didn't use any pool memory */
304 flist->pool_boundary = ptr;
307 int push_pathname(const char *dir, int len)
313 orig_dir = strdup(curr_dir);
315 if (pathname && !pop_dir(orig_dir)) {
316 rsyserr(FERROR, errno, "pop_dir %s failed",
317 full_fname(orig_dir));
318 exit_cleanup(RERR_FILESELECT);
321 if (dir && !push_dir(dir, 0)) {
322 io_error |= IOERR_GENERAL;
323 rsyserr(FERROR, errno, "push_dir %s failed in %s",
324 full_fname(dir), curr_dir);
329 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
334 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
336 static time_t modtime;
340 static uint32 rdev_major;
343 static char *user_name, *group_name;
344 static char lastname[MAXPATHLEN];
345 char fname[MAXPATHLEN];
346 int first_hlink_ndx = -1;
351 if (ic_send != (iconv_t)-1) {
354 INIT_CONST_XBUF(outbuf, fname);
357 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
358 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
359 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
362 outbuf.buf[outbuf.len++] = '/';
365 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
366 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
368 io_error |= IOERR_GENERAL;
370 "[%s] cannot convert filename: %s (%s)\n",
371 who_am_i(), f_name(file, fname), strerror(errno));
374 outbuf.buf[outbuf.len] = '\0';
379 /* Initialize starting value of xflags. */
380 if (protocol_version >= 30 && S_ISDIR(file->mode)) {
381 if (file->flags & FLAG_CONTENT_DIR)
382 xflags = file->flags & FLAG_TOP_DIR;
383 else if (file->flags & FLAG_IMPLIED_DIR)
384 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
386 xflags = XMIT_NO_CONTENT_DIR;
388 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
390 if (file->mode == mode)
391 xflags |= XMIT_SAME_MODE;
395 if ((preserve_devices && IS_DEVICE(mode))
396 || (preserve_specials && IS_SPECIAL(mode))) {
397 if (protocol_version < 28) {
398 if (tmp_rdev == rdev)
399 xflags |= XMIT_SAME_RDEV_pre28;
404 if ((uint32)major(rdev) == rdev_major)
405 xflags |= XMIT_SAME_RDEV_MAJOR;
407 rdev_major = major(rdev);
408 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
409 xflags |= XMIT_RDEV_MINOR_8_pre30;
411 } else if (protocol_version < 28)
412 rdev = MAKEDEV(0, 0);
414 if ((uid_t)F_OWNER(file) == uid && *lastname)
415 xflags |= XMIT_SAME_UID;
418 if (uid_ndx && !numeric_ids) {
419 user_name = add_uid(uid);
420 if (inc_recurse && user_name)
421 xflags |= XMIT_USER_NAME_FOLLOWS;
426 if ((gid_t)F_GROUP(file) == gid && *lastname)
427 xflags |= XMIT_SAME_GID;
430 if (gid_ndx && !numeric_ids) {
431 group_name = add_gid(gid);
432 if (inc_recurse && group_name)
433 xflags |= XMIT_GROUP_NAME_FOLLOWS;
437 if (file->modtime == modtime)
438 xflags |= XMIT_SAME_TIME;
440 modtime = file->modtime;
442 #ifdef SUPPORT_HARD_LINKS
444 if (protocol_version >= 30) {
445 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
446 first_hlink_ndx = (int32)(long)np->data - 1;
447 if (first_hlink_ndx < 0) {
448 high_hlink_ndx = ndx + first_ndx;
449 np->data = (void*)(long)(high_hlink_ndx + 1);
450 xflags |= XMIT_HLINK_FIRST;
452 xflags |= XMIT_HLINKED;
454 if (tmp_dev == dev) {
455 if (protocol_version >= 28)
456 xflags |= XMIT_SAME_DEV_pre30;
459 xflags |= XMIT_HLINKED;
465 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
467 l2 = strlen(fname+l1);
470 xflags |= XMIT_SAME_NAME;
472 xflags |= XMIT_LONG_NAME;
474 /* We must make sure we don't send a zero flag byte or the
475 * other end will terminate the flist transfer. Note that
476 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
477 * it's harmless way to add a bit to the first flag byte. */
478 if (protocol_version >= 28) {
479 if (!xflags && !S_ISDIR(mode))
480 xflags |= XMIT_TOP_DIR;
481 if ((xflags & 0xFF00) || !xflags) {
482 xflags |= XMIT_EXTENDED_FLAGS;
483 write_shortint(f, xflags);
485 write_byte(f, xflags);
487 if (!(xflags & 0xFF))
488 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
489 write_byte(f, xflags);
491 if (xflags & XMIT_SAME_NAME)
493 if (xflags & XMIT_LONG_NAME)
494 write_varint30(f, l2);
497 write_buf(f, fname + l1, l2);
499 if (first_hlink_ndx >= 0) {
500 write_varint(f, first_hlink_ndx);
501 if (first_hlink_ndx >= first_ndx)
505 write_varlong30(f, F_LENGTH(file), 3);
506 if (!(xflags & XMIT_SAME_TIME)) {
507 if (protocol_version >= 30)
508 write_varlong(f, modtime, 4);
510 write_int(f, modtime);
512 if (!(xflags & XMIT_SAME_MODE))
513 write_int(f, to_wire_mode(mode));
514 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
515 if (protocol_version < 30)
518 write_varint(f, uid);
519 if (xflags & XMIT_USER_NAME_FOLLOWS) {
520 int len = strlen(user_name);
522 write_buf(f, user_name, len);
526 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
527 if (protocol_version < 30)
530 write_varint(f, gid);
531 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
532 int len = strlen(group_name);
534 write_buf(f, group_name, len);
538 if ((preserve_devices && IS_DEVICE(mode))
539 || (preserve_specials && IS_SPECIAL(mode))) {
540 if (protocol_version < 28) {
541 if (!(xflags & XMIT_SAME_RDEV_pre28))
542 write_int(f, (int)rdev);
544 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
545 write_varint30(f, major(rdev));
546 if (protocol_version >= 30)
547 write_varint(f, minor(rdev));
548 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
549 write_byte(f, minor(rdev));
551 write_int(f, minor(rdev));
556 if (preserve_links && S_ISLNK(mode)) {
557 const char *sl = F_SYMLINK(file);
558 int len = strlen(sl);
559 write_varint30(f, len);
560 write_buf(f, sl, len);
564 #ifdef SUPPORT_HARD_LINKS
565 if (tmp_dev != 0 && protocol_version < 30) {
566 if (protocol_version < 26) {
567 /* 32-bit dev_t and ino_t */
568 write_int(f, (int32)dev);
569 write_int(f, (int32)tmp_ino);
571 /* 64-bit dev_t and ino_t */
572 if (!(xflags & XMIT_SAME_DEV_pre30))
573 write_longint(f, dev);
574 write_longint(f, tmp_ino);
579 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
584 /* Prior to 28, we sent a useless set of nulls. */
587 write_buf(f, sum, checksum_len);
591 strlcpy(lastname, fname, MAXPATHLEN);
593 if (S_ISREG(mode) || S_ISLNK(mode))
594 stats.total_size += F_LENGTH(file);
597 static struct file_struct *recv_file_entry(struct file_list *flist,
600 static int64 modtime;
604 static uint32 rdev_major;
607 static uint16 gid_flags;
608 static char lastname[MAXPATHLEN], *lastdir;
609 static int lastdir_depth, lastdir_len = -1;
610 static unsigned int del_hier_name_len = 0;
611 static int in_del_hier = 0;
612 char thisname[MAXPATHLEN];
613 unsigned int l1 = 0, l2 = 0;
614 int alloc_len, basename_len, linkname_len;
615 int extra_len = file_extra_cnt * EXTRA_LEN;
616 int first_hlink_ndx = -1;
618 const char *basename;
619 struct file_struct *file;
623 if (xflags & XMIT_SAME_NAME)
626 if (xflags & XMIT_LONG_NAME)
627 l2 = read_varint30(f);
631 if (l2 >= MAXPATHLEN - l1) {
633 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
634 xflags, l1, l2, lastname, who_am_i());
635 overflow_exit("recv_file_entry");
638 strlcpy(thisname, lastname, l1 + 1);
639 read_sbuf(f, &thisname[l1], l2);
640 thisname[l1 + l2] = 0;
642 /* Abuse basename_len for a moment... */
643 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
646 if (ic_recv != (iconv_t)-1) {
649 INIT_CONST_XBUF(outbuf, thisname);
650 INIT_XBUF(inbuf, lastname, basename_len, -1);
652 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
653 io_error |= IOERR_GENERAL;
655 "[%s] cannot convert filename: %s (%s)\n",
656 who_am_i(), lastname, strerror(errno));
659 outbuf.buf[outbuf.len] = '\0';
663 clean_fname(thisname, 0);
666 sanitize_path(thisname, thisname, "", 0, NULL);
668 if ((basename = strrchr(thisname, '/')) != NULL) {
669 int len = basename++ - thisname;
670 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
671 lastdir = new_array(char, len + 1);
672 memcpy(lastdir, thisname, len);
675 lastdir_depth = count_dir_elements(lastdir);
679 basename_len = strlen(basename) + 1; /* count the '\0' */
681 #ifdef SUPPORT_HARD_LINKS
682 if (protocol_version >= 30
683 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
684 first_hlink_ndx = read_varint(f);
685 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
687 "hard-link reference out of range: %d (%d)\n",
688 first_hlink_ndx, flist->ndx_start + flist->used);
689 exit_cleanup(RERR_PROTOCOL);
691 if (first_hlink_ndx >= flist->ndx_start) {
692 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
693 file_length = F_LENGTH(first);
694 modtime = first->modtime;
697 uid = F_OWNER(first);
699 gid = F_GROUP(first);
700 if ((preserve_devices && IS_DEVICE(mode))
701 || (preserve_specials && IS_SPECIAL(mode))) {
702 uint32 *devp = F_RDEV_P(first);
703 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
704 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
706 if (preserve_links && S_ISLNK(mode))
707 linkname_len = strlen(F_SYMLINK(first)) + 1;
715 file_length = read_varlong30(f, 3);
716 if (!(xflags & XMIT_SAME_TIME)) {
717 if (protocol_version >= 30) {
718 modtime = read_varlong(f, 4);
719 #if SIZEOF_TIME_T < SIZEOF_INT64
720 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
722 "Time value of %s truncated on receiver.\n",
727 modtime = read_int(f);
729 if (!(xflags & XMIT_SAME_MODE))
730 mode = from_wire_mode(read_int(f));
732 if (chmod_modes && !S_ISLNK(mode))
733 mode = tweak_mode(mode, chmod_modes);
735 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
736 if (protocol_version < 30)
737 uid = (uid_t)read_int(f);
739 uid = (uid_t)read_varint(f);
740 if (xflags & XMIT_USER_NAME_FOLLOWS)
741 uid = recv_user_name(f, uid);
742 else if (inc_recurse && am_root && !numeric_ids)
743 uid = match_uid(uid);
746 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
747 if (protocol_version < 30)
748 gid = (gid_t)read_int(f);
750 gid = (gid_t)read_varint(f);
752 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
753 gid = recv_group_name(f, gid, &gid_flags);
754 else if (inc_recurse && (!am_root || !numeric_ids))
755 gid = match_gid(gid, &gid_flags);
759 if ((preserve_devices && IS_DEVICE(mode))
760 || (preserve_specials && IS_SPECIAL(mode))) {
761 if (protocol_version < 28) {
762 if (!(xflags & XMIT_SAME_RDEV_pre28))
763 rdev = (dev_t)read_int(f);
766 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
767 rdev_major = read_varint30(f);
768 if (protocol_version >= 30)
769 rdev_minor = read_varint(f);
770 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
771 rdev_minor = read_byte(f);
773 rdev_minor = read_int(f);
774 rdev = MAKEDEV(rdev_major, rdev_minor);
776 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
778 } else if (protocol_version < 28)
779 rdev = MAKEDEV(0, 0);
782 if (preserve_links && S_ISLNK(mode)) {
783 linkname_len = read_varint30(f) + 1; /* count the '\0' */
784 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
785 rprintf(FERROR, "overflow: linkname_len=%d\n",
787 overflow_exit("recv_file_entry");
794 #ifdef SUPPORT_HARD_LINKS
796 if (preserve_hard_links) {
797 if (protocol_version < 28 && S_ISREG(mode))
798 xflags |= XMIT_HLINKED;
799 if (xflags & XMIT_HLINKED)
800 extra_len += (inc_recurse+1) * EXTRA_LEN;
805 /* We need one or two index int32s when we're preserving ACLs. */
807 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
810 if (always_checksum && S_ISREG(mode))
811 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
813 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
814 extra_len += EXTRA_LEN;
816 #if EXTRA_ROUNDING > 0
817 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
818 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
821 if (inc_recurse && S_ISDIR(mode)) {
822 if (one_file_system) {
823 /* Room to save the dir's device for -x */
824 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
826 pool = dir_flist->file_pool;
828 pool = flist->file_pool;
830 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
832 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
834 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
836 file = (struct file_struct *)bp;
837 bp += FILE_STRUCT_LEN;
839 memcpy(bp, basename, basename_len);
840 bp += basename_len + linkname_len; /* skip space for symlink too */
842 #ifdef SUPPORT_HARD_LINKS
843 if (xflags & XMIT_HLINKED)
844 file->flags |= FLAG_HLINKED;
846 file->modtime = (time_t)modtime;
847 file->len32 = (uint32)file_length;
848 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
849 file->flags |= FLAG_LENGTH64;
850 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
857 file->flags |= gid_flags;
861 F_NDX(file) = flist->used + flist->ndx_start;
864 if (basename != thisname) {
865 file->dirname = lastdir;
866 F_DEPTH(file) = lastdir_depth + 1;
871 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
873 if (protocol_version >= 30) {
874 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
875 if (xflags & XMIT_TOP_DIR)
876 file->flags |= FLAG_TOP_DIR;
877 file->flags |= FLAG_CONTENT_DIR;
878 } else if (xflags & XMIT_TOP_DIR)
879 file->flags |= FLAG_IMPLIED_DIR;
880 } else if (xflags & XMIT_TOP_DIR) {
881 in_del_hier = recurse;
882 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
883 if (relative_paths && del_hier_name_len > 2
884 && lastname[del_hier_name_len-1] == '.'
885 && lastname[del_hier_name_len-2] == '/')
886 del_hier_name_len -= 2;
887 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
888 } else if (in_del_hier) {
889 if (!relative_paths || !del_hier_name_len
890 || (l1 >= del_hier_name_len
891 && lastname[del_hier_name_len] == '/'))
892 file->flags |= FLAG_CONTENT_DIR;
898 if ((preserve_devices && IS_DEVICE(mode))
899 || (preserve_specials && IS_SPECIAL(mode))) {
900 uint32 *devp = F_RDEV_P(file);
901 DEV_MAJOR(devp) = major(rdev);
902 DEV_MINOR(devp) = minor(rdev);
907 bp = (char*)file->basename + basename_len;
908 if (first_hlink_ndx >= flist->ndx_start) {
909 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
910 memcpy(bp, F_SYMLINK(first), linkname_len);
912 read_sbuf(f, bp, linkname_len - 1);
914 sanitize_path(bp, bp, "", lastdir_depth, NULL);
918 #ifdef SUPPORT_HARD_LINKS
919 if (preserve_hard_links && xflags & XMIT_HLINKED) {
920 if (protocol_version >= 30) {
921 if (xflags & XMIT_HLINK_FIRST) {
922 high_hlink_ndx = flist->ndx_start + flist->used;
923 F_HL_GNUM(file) = high_hlink_ndx;
925 F_HL_GNUM(file) = first_hlink_ndx;
927 static int32 cnt = 0;
928 struct ht_int64_node *np;
931 if (protocol_version < 26) {
935 if (!(xflags & XMIT_SAME_DEV_pre30))
936 dev = read_longint(f);
937 ino = read_longint(f);
939 np = idev_find(dev, ino);
940 ndx = (int32)(long)np->data - 1;
943 np->data = (void*)(long)cnt;
945 F_HL_GNUM(file) = ndx;
950 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
954 /* Prior to 28, we get a useless set of nulls. */
957 if (first_hlink_ndx >= flist->ndx_start) {
958 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
959 memcpy(bp, F_SUM(first), checksum_len);
961 read_buf(f, bp, checksum_len);
965 if (preserve_acls && !S_ISLNK(mode))
966 receive_acl(file, f);
968 #ifdef SUPPORT_XATTRS
970 receive_xattr(file, f );
973 if (S_ISREG(mode) || S_ISLNK(mode))
974 stats.total_size += file_length;
979 /* Create a file_struct for a named file by reading its stat() information
980 * and performing extensive checks against global options.
982 * Returns a pointer to the new file struct, or NULL if there was an error
983 * or this file should be excluded. */
984 struct file_struct *make_file(const char *fname, struct file_list *flist,
985 STRUCT_STAT *stp, int flags, int filter_level)
987 static char *lastdir;
988 static int lastdir_len = -1;
989 struct file_struct *file;
990 char thisname[MAXPATHLEN];
991 char linkname[MAXPATHLEN];
992 int alloc_len, basename_len, linkname_len;
993 int extra_len = file_extra_cnt * EXTRA_LEN;
994 const char *basename;
999 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1000 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1003 clean_fname(thisname, 0);
1005 sanitize_path(thisname, thisname, "", 0, NULL);
1007 if (stp && S_ISDIR(stp->st_mode)) {
1008 st = *stp; /* Needed for "symlink/." with --relative. */
1009 *linkname = '\0'; /* make IBM code checker happy */
1010 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1011 int save_errno = errno;
1012 /* See if file is excluded before reporting an error. */
1013 if (filter_level != NO_FILTERS
1014 && (is_excluded(thisname, 0, filter_level)
1015 || is_excluded(thisname, 1, filter_level))) {
1016 if (ignore_perishable && save_errno != ENOENT)
1017 non_perishable_cnt++;
1020 if (save_errno == ENOENT) {
1021 #ifdef SUPPORT_LINKS
1022 /* Avoid "vanished" error if symlink points nowhere. */
1023 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1024 && S_ISLNK(st.st_mode)) {
1025 io_error |= IOERR_GENERAL;
1026 rprintf(FERROR, "symlink has no referent: %s\n",
1027 full_fname(thisname));
1031 enum logcode c = am_daemon && protocol_version < 28
1033 io_error |= IOERR_VANISHED;
1034 rprintf(c, "file has vanished: %s\n",
1035 full_fname(thisname));
1038 io_error |= IOERR_GENERAL;
1039 rsyserr(FERROR, save_errno, "readlink %s failed",
1040 full_fname(thisname));
1045 /* backup.c calls us with filter_level set to NO_FILTERS. */
1046 if (filter_level == NO_FILTERS)
1049 if (S_ISDIR(st.st_mode)) {
1051 rprintf(FINFO, "skipping directory %s\n", thisname);
1054 /* -x only affects dirs because we need to avoid recursing
1055 * into a mount-point directory, not to avoid copying a
1056 * symlinked file if -L (or similar) was specified. */
1057 if (one_file_system && flags & FLAG_CONTENT_DIR) {
1058 if (flags & FLAG_TOP_DIR)
1059 filesystem_dev = st.st_dev;
1060 else if (st.st_dev != filesystem_dev) {
1061 if (one_file_system > 1) {
1064 "[%s] skipping mount-point dir %s\n",
1065 who_am_i(), thisname);
1069 flags |= FLAG_MOUNT_DIR;
1073 flags &= ~FLAG_CONTENT_DIR;
1075 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1076 if (ignore_perishable)
1077 non_perishable_cnt++;
1081 if (lp_ignore_nonreadable(module_id)) {
1082 #ifdef SUPPORT_LINKS
1083 if (!S_ISLNK(st.st_mode))
1085 if (access(thisname, R_OK) != 0)
1091 /* Only divert a directory in the main transfer. */
1093 if (flist->prev && S_ISDIR(st.st_mode)
1094 && flags & FLAG_DIVERT_DIRS) {
1095 /* Room for parent/sibling/next-child info. */
1096 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1098 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1100 pool = dir_flist->file_pool;
1102 pool = flist->file_pool;
1107 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1108 who_am_i(), thisname, filter_level);
1111 if ((basename = strrchr(thisname, '/')) != NULL) {
1112 int len = basename++ - thisname;
1113 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1114 lastdir = new_array(char, len + 1);
1115 memcpy(lastdir, thisname, len);
1116 lastdir[len] = '\0';
1120 basename = thisname;
1121 basename_len = strlen(basename) + 1; /* count the '\0' */
1123 #ifdef SUPPORT_LINKS
1124 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1129 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1130 extra_len += EXTRA_LEN;
1132 #if EXTRA_ROUNDING > 0
1133 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1134 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1137 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1140 bp = pool_alloc(pool, alloc_len, "make_file");
1142 if (!(bp = new_array(char, alloc_len)))
1143 out_of_memory("make_file");
1146 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1148 file = (struct file_struct *)bp;
1149 bp += FILE_STRUCT_LEN;
1151 memcpy(bp, basename, basename_len);
1152 bp += basename_len + linkname_len; /* skip space for symlink too */
1154 #ifdef SUPPORT_HARD_LINKS
1155 if (preserve_hard_links && flist && flist->prev) {
1156 if (protocol_version >= 28
1157 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1158 : S_ISREG(st.st_mode)) {
1159 tmp_dev = st.st_dev;
1160 tmp_ino = st.st_ino;
1166 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1167 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1168 tmp_rdev = st.st_rdev;
1173 file->flags = flags;
1174 file->modtime = st.st_mtime;
1175 file->len32 = (uint32)st.st_size;
1176 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1177 file->flags |= FLAG_LENGTH64;
1178 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1180 file->mode = st.st_mode;
1182 F_OWNER(file) = st.st_uid;
1184 F_GROUP(file) = st.st_gid;
1186 if (basename != thisname)
1187 file->dirname = lastdir;
1189 #ifdef SUPPORT_LINKS
1191 bp = (char*)file->basename + basename_len;
1192 memcpy(bp, linkname, linkname_len);
1196 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1197 file_checksum(thisname, tmp_sum, st.st_size);
1199 F_PATHNAME(file) = pathname;
1201 /* This code is only used by the receiver when it is building
1202 * a list of files for a delete pass. */
1203 if (keep_dirlinks && linkname_len && flist) {
1205 int save_mode = file->mode;
1206 file->mode = S_IFDIR; /* Find a directory with our name. */
1207 if (flist_find(dir_flist, file) >= 0
1208 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1209 file->modtime = st2.st_mtime;
1211 file->mode = st2.st_mode;
1213 F_OWNER(file) = st2.st_uid;
1215 F_GROUP(file) = st2.st_gid;
1217 file->mode = save_mode;
1220 if (basename_len == 0+1)
1225 F_NDX(file) = dir_count - 1;
1231 /* Only called for temporary file_struct entries created by make_file(). */
1232 void unmake_file(struct file_struct *file)
1234 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1235 #if EXTRA_ROUNDING > 0
1236 if (extra_cnt & EXTRA_ROUNDING)
1237 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1239 free(REQ_EXTRA(file, extra_cnt));
1242 static struct file_struct *send_file_name(int f, struct file_list *flist,
1243 char *fname, STRUCT_STAT *stp,
1244 int flags, int filter_level)
1246 struct file_struct *file;
1247 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1251 file = make_file(fname, flist, stp, flags, filter_level);
1255 if (chmod_modes && !S_ISLNK(file->mode))
1256 file->mode = tweak_mode(file->mode, chmod_modes);
1259 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1260 sx.st.st_mode = file->mode;
1261 sx.acc_acl = sx.def_acl = NULL;
1262 if (get_acl(fname, &sx) < 0)
1266 #ifdef SUPPORT_XATTRS
1267 if (preserve_xattrs && f >= 0) {
1269 if (get_xattr(fname, &sx) < 0)
1274 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1276 flist_expand(flist, 1);
1277 flist->files[flist->used++] = file;
1279 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1281 if (preserve_acls && !S_ISLNK(file->mode)) {
1286 #ifdef SUPPORT_XATTRS
1287 if (preserve_xattrs) {
1288 F_XATTR(file) = send_xattr(&sx, f);
1296 static void send_if_directory(int f, struct file_list *flist,
1297 struct file_struct *file,
1298 char *fbuf, unsigned int ol,
1301 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1303 if (S_ISDIR(file->mode)
1304 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1306 unsigned int len = strlen(fbuf);
1307 if (len > 1 && fbuf[len-1] == '/')
1309 if (len >= MAXPATHLEN - 1) {
1310 io_error |= IOERR_GENERAL;
1311 rprintf(FERROR, "skipping long-named directory: %s\n",
1315 save_filters = push_local_filters(fbuf, len);
1316 send_directory(f, flist, fbuf, len, flags);
1317 pop_local_filters(save_filters);
1324 static int file_compare(const void *file1, const void *file2)
1326 return f_name_cmp(*(struct file_struct **)file1,
1327 *(struct file_struct **)file2);
1330 /* The guts of a merge-sort algorithm. This was derived from the glibc
1331 * version, but I (Wayne) changed the merge code to do less copying and
1332 * to require only half the amount of temporary memory. */
1333 static void fsort_tmp(struct file_struct **fp, size_t num,
1334 struct file_struct **tmp)
1336 struct file_struct **f1, **f2, **t;
1345 fsort_tmp(f1, n1, tmp);
1347 fsort_tmp(f2, n2, tmp);
1349 while (f_name_cmp(*f1, *f2) <= 0) {
1356 memcpy(t, f1, n1 * PTR_SIZE);
1358 *f1++ = *f2++, n2--;
1360 while (n1 > 0 && n2 > 0) {
1361 if (f_name_cmp(*t, *f2) <= 0)
1364 *f1++ = *f2++, n2--;
1368 memcpy(f1, t, n1 * PTR_SIZE);
1371 /* This file-struct sorting routine makes sure that any identical names in
1372 * the file list stay in the same order as they were in the original list.
1373 * This is particularly vital in inc_recurse mode where we expect a sort
1374 * on the flist to match the exact order of a sort on the dir_flist. */
1375 static void fsort(struct file_struct **fp, size_t num)
1381 qsort(fp, num, PTR_SIZE, file_compare);
1383 struct file_struct **tmp = new_array(struct file_struct *,
1385 fsort_tmp(fp, num, tmp);
1390 /* We take an entire set of sibling dirs from the sorted flist and link them
1391 * into the tree, setting the appropriate parent/child/sibling pointers. */
1392 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1397 int32 *parent_dp = parent_ndx < 0 ? NULL
1398 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1400 flist_expand(dir_flist, dir_cnt);
1401 dir_flist->sorted = dir_flist->files;
1403 for (i = 0; dir_cnt; i++) {
1404 struct file_struct *file = from_flist->sorted[i];
1406 if (!S_ISDIR(file->mode))
1409 dir_flist->files[dir_flist->used++] = file;
1413 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1415 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1417 send_dir_ndx = dir_flist->used - 1;
1419 dp = F_DIR_NODE_P(file);
1420 DIR_PARENT(dp) = parent_ndx;
1421 DIR_FIRST_CHILD(dp) = -1;
1424 DIR_NEXT_SIBLING(dp) = -1;
1427 /* This function is normally called by the sender, but the receiving side also
1428 * calls it from get_dirlist() with f set to -1 so that we just construct the
1429 * file list in memory without sending it over the wire. Also, get_dirlist()
1430 * might call this with f set to -2, which also indicates that local filter
1431 * rules should be ignored. */
1432 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1439 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1440 int start = flist->used;
1441 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1443 assert(flist != NULL);
1445 if (!(d = opendir(fbuf))) {
1446 io_error |= IOERR_GENERAL;
1447 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1452 if (len != 1 || *fbuf != '/')
1455 remainder = MAXPATHLEN - (p - fbuf);
1457 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1458 char *dname = d_name(di);
1459 if (dname[0] == '.' && (dname[1] == '\0'
1460 || (dname[1] == '.' && dname[2] == '\0')))
1462 if (strlcpy(p, dname, remainder) >= remainder) {
1463 io_error |= IOERR_GENERAL;
1465 "cannot send long-named file %s\n",
1469 if (dname[0] == '\0') {
1470 io_error |= IOERR_GENERAL;
1472 "cannot send file with empty name in %s\n",
1477 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1483 io_error |= IOERR_GENERAL;
1484 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1489 if (f >= 0 && recurse && !divert_dirs) {
1490 int i, end = flist->used - 1;
1491 /* send_if_directory() bumps flist->used, so use "end". */
1492 for (i = start; i <= end; i++)
1493 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1497 static char lastpath[MAXPATHLEN] = "";
1498 static int lastpath_len = 0;
1499 static struct file_struct *lastpath_struct;
1501 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1502 char *start, char *limit, int flags, int is_dot_dir)
1504 struct file_struct *file;
1505 item_list *relname_list;
1506 relnamecache **rnpp;
1508 int len, need_new_dir;
1510 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1513 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1514 && lastpath_len == limit - fname
1515 && strncmp(lastpath, fname, lastpath_len) == 0)
1523 int save_copy_links = copy_links;
1524 int save_xfer_dirs = xfer_dirs;
1526 copy_links = xfer_dirs = 1;
1530 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1532 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1536 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1538 if (file && !S_ISDIR(file->mode))
1540 lastpath_struct = file;
1543 strlcpy(lastpath, fname, sizeof lastpath);
1544 lastpath_len = limit - fname;
1548 copy_links = save_copy_links;
1549 xfer_dirs = save_xfer_dirs;
1555 if (!lastpath_struct)
1556 return; /* dir must have vanished */
1558 len = strlen(limit+1);
1559 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1560 if (!relname_list) {
1561 if (!(relname_list = new0(item_list)))
1562 out_of_memory("send_implied_dirs");
1563 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1565 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1566 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1567 out_of_memory("send_implied_dirs");
1568 (*rnpp)->is_dot_dir = is_dot_dir;
1569 strlcpy((*rnpp)->fname, limit+1, len + 1);
1572 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1574 char fbuf[MAXPATHLEN];
1575 item_list *relname_list;
1576 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1580 dlen = strlen(fbuf);
1582 if (F_PATHNAME(file) != pathname) {
1583 if (!push_pathname(F_PATHNAME(file), -1))
1584 exit_cleanup(RERR_FILESELECT);
1587 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1589 if (BITS_SETnUNSET(file->flags, FLAG_CONTENT_DIR, FLAG_MOUNT_DIR))
1590 send_directory(f, flist, fbuf, dlen, flags);
1592 if (!relative_paths)
1595 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1599 for (j = 0; j < relname_list->count; j++) {
1601 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1602 int is_dot_dir = rnp->is_dot_dir;
1605 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1607 if (len >= (int)sizeof fbuf)
1608 continue; /* Impossible... */
1610 slash = strchr(fbuf+dlen+1, '/');
1612 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
1618 if (link_stat(fbuf, &st, 1) != 0) {
1619 io_error |= IOERR_GENERAL;
1620 rsyserr(FERROR, errno, "link_stat %s failed",
1624 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1626 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1632 void send_extra_file_list(int f, int at_least)
1634 struct file_list *flist;
1637 int future_cnt, save_io_error = io_error;
1642 /* Keep sending data until we have the requested number of
1643 * files in the upcoming file-lists. */
1644 if (cur_flist->next) {
1645 flist = first_flist->prev; /* the newest flist */
1646 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1649 while (future_cnt < at_least) {
1650 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1651 int dir_ndx, dstart = dir_count;
1652 const char *pathname = F_PATHNAME(file);
1655 flist = flist_new(0, "send_extra_file_list");
1656 start_write = stats.total_written;
1660 dir_ndx = F_NDX(file);
1663 dir_ndx = send_dir_ndx;
1664 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1665 flist->parent_ndx = dir_ndx;
1667 send1extra(f, file, flist);
1668 prev_flags = file->flags;
1669 dp = F_DIR_NODE_P(file);
1671 /* If there are any duplicate directory names that follow, we
1672 * send all the dirs together in one file-list. The dir_flist
1673 * tree links all the child subdirs onto the last dup dir. */
1674 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1675 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1676 send_dir_ndx = dir_ndx;
1677 file = dir_flist->sorted[dir_ndx];
1678 /* Try to avoid some duplicate scanning of identical dirs. */
1679 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1680 file->flags &= ~FLAG_CONTENT_DIR;
1681 send1extra(f, file, flist);
1682 prev_flags = file->flags;
1683 dp = F_DIR_NODE_P(file);
1689 if (need_unsorted_flist) {
1690 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1691 out_of_memory("send_extra_file_list");
1692 memcpy(flist->sorted, flist->files,
1693 flist->used * sizeof (struct file_struct*));
1696 flist->sorted = flist->files;
1698 clean_flist(flist, 0);
1700 flist->ndx_end = flist->ndx_start + flist->used - 1
1701 - (dir_count - dstart);
1703 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1704 flist_done_allocating(flist);
1706 file_total += flist->used;
1707 future_cnt += flist->used;
1708 stats.flist_size += stats.total_written - start_write;
1709 stats.num_files += flist->used;
1711 output_flist(flist);
1713 if (DIR_FIRST_CHILD(dp) >= 0) {
1714 send_dir_ndx = DIR_FIRST_CHILD(dp);
1717 while (DIR_NEXT_SIBLING(dp) < 0) {
1718 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1719 write_ndx(f, NDX_FLIST_EOF);
1721 change_local_filter_dir(NULL, 0, 0);
1725 file = dir_flist->sorted[send_dir_ndx];
1726 dp = F_DIR_NODE_P(file);
1728 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1733 if (io_error != save_io_error && !ignore_errors)
1734 send_msg_int(MSG_IO_ERROR, io_error);
1737 struct file_list *send_file_list(int f, int argc, char *argv[])
1739 static const char *lastdir;
1740 static int lastdir_len = -1;
1744 struct file_list *flist;
1745 struct timeval start_tv, end_tv;
1748 int disable_buffering;
1749 int flags = recurse ? FLAG_CONTENT_DIR : 0;
1750 int reading_remotely = filesfrom_host != NULL;
1751 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1753 | (filesfrom_convert ? RL_CONVERT : 0)
1755 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1757 rprintf(FLOG, "building file list\n");
1758 if (show_filelist_p())
1759 start_filelist_progress("building file list");
1760 else if (inc_recurse && verbose && !am_server)
1761 rprintf(FCLIENT, "sending incremental file list\n");
1763 start_write = stats.total_written;
1764 gettimeofday(&start_tv, NULL);
1766 if (relative_paths && protocol_version >= 30)
1767 implied_dirs = 1; /* We send flagged implied dirs */
1769 #ifdef SUPPORT_HARD_LINKS
1770 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1774 flist = cur_flist = flist_new(0, "send_file_list");
1776 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1777 flags |= FLAG_DIVERT_DIRS;
1779 dir_flist = cur_flist;
1781 disable_buffering = io_start_buffering_out(f);
1782 if (filesfrom_fd >= 0) {
1783 if (argv[0] && !push_dir(argv[0], 0)) {
1784 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1785 full_fname(argv[0]), curr_dir);
1786 exit_cleanup(RERR_FILESELECT);
1792 char fbuf[MAXPATHLEN];
1797 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1799 sanitize_path(fbuf, fbuf, "", 0, NULL);
1803 strlcpy(fbuf, *argv++, MAXPATHLEN);
1805 sanitize_path(fbuf, fbuf, "", 0, NULL);
1809 if (relative_paths) {
1810 /* We clean up fbuf below. */
1812 } else if (!len || fbuf[len - 1] == '/') {
1813 if (len == 2 && fbuf[0] == '.') {
1814 /* Turn "./" into just "." rather than "./." */
1817 if (len + 1 >= MAXPATHLEN)
1818 overflow_exit("send_file_list");
1823 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1824 && (len == 2 || fbuf[len-3] == '/')) {
1825 if (len + 2 >= MAXPATHLEN)
1826 overflow_exit("send_file_list");
1832 is_dot_dir = fbuf[len-1] == '.'
1833 && (len == 1 || fbuf[len-2] == '/');
1838 if (!relative_paths) {
1839 p = strrchr(fbuf, '/');
1846 len -= p - fbuf + 1;
1851 if ((p = strstr(fbuf, "/./")) != NULL) {
1857 len -= p - fbuf + 3;
1861 /* Get rid of trailing "/" and "/.". */
1863 if (fn[len - 1] == '/') {
1865 if (!--len && !dir) {
1870 else if (len >= 2 && fn[len - 1] == '.'
1871 && fn[len - 2] == '/') {
1873 if (!(len -= 2) && !dir) {
1885 } else if (fn[0] == '.')
1888 /* Reject a ".." dir in the active part of the path. */
1889 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1890 if ((p[2] == '/' || p[2] == '\0')
1891 && (p == fn || p[-1] == '/')) {
1893 "found \"..\" dir in relative path: %s\n",
1895 exit_cleanup(RERR_SYNTAX);
1906 dirlen = dir ? strlen(dir) : 0;
1907 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1908 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1911 lastdir_len = pathname_len;
1912 } else if (!push_pathname(lastdir, lastdir_len))
1916 memmove(fbuf, fn, len + 1);
1918 if (link_stat(fbuf, &st, copy_dirlinks || is_dot_dir) != 0) {
1919 io_error |= IOERR_GENERAL;
1920 rsyserr(FERROR, errno, "link_stat %s failed",
1925 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1926 rprintf(FINFO, "skipping directory %s\n", fbuf);
1930 if (inc_recurse && relative_paths && *fbuf) {
1931 if ((p = strchr(fbuf+1, '/')) != NULL) {
1932 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, is_dot_dir);
1935 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1936 /* Send the implied directories at the start of the
1937 * source spec, so we get their permissions right. */
1938 char *lp = lastpath, *slash = fbuf;
1940 /* Skip any initial directories in our path that we
1941 * have in common with lastpath. */
1942 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1947 if (fn != p || (*lp && *lp != '/'))
1948 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1951 if (recurse || (xfer_dirs && is_dot_dir)) {
1952 struct file_struct *file;
1953 int top_flags = FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags;
1954 file = send_file_name(f, flist, fbuf, &st,
1955 top_flags, ALL_FILTERS);
1956 if (file && !inc_recurse)
1957 send_if_directory(f, flist, file, fbuf, len, flags);
1959 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1962 gettimeofday(&end_tv, NULL);
1963 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1964 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1965 if (stats.flist_buildtime == 0)
1966 stats.flist_buildtime = 1;
1969 write_byte(f, 0); /* Indicate end of file list */
1971 #ifdef SUPPORT_HARD_LINKS
1972 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1976 if (show_filelist_p())
1977 finish_filelist_progress(flist);
1979 gettimeofday(&end_tv, NULL);
1980 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1981 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1983 /* When converting names, both sides keep an unsorted file-list array
1984 * because the names will differ on the sending and receiving sides
1985 * (both sides will use the unsorted index number for each item). */
1987 /* Sort the list without removing any duplicates. This allows the
1988 * receiving side to ask for whatever name it kept. For incremental
1989 * recursion mode, the sender marks duplicate dirs so that it can
1990 * send them together in a single file-list. */
1992 if (need_unsorted_flist) {
1994 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1995 out_of_memory("send_file_list");
1996 memcpy(flist->sorted, flist->files,
1997 flist->used * sizeof (struct file_struct*));
1998 clean_flist(flist, 0);
2000 flist->sorted = flist->files;
2002 flist->high = flist->used - 1;
2007 flist->sorted = flist->files;
2008 clean_flist(flist, 0);
2010 file_total += flist->used;
2012 /* We don't subtract dir_count for the first send since we
2013 * might have one or more dot dirs which need to get sent. */
2014 flist->ndx_end = flist->ndx_start + flist->used - 1;
2016 if (!numeric_ids && !inc_recurse)
2019 /* send the io_error flag */
2020 if (protocol_version < 30)
2021 write_int(f, ignore_errors ? 0 : io_error);
2022 else if (io_error && !ignore_errors)
2023 send_msg_int(MSG_IO_ERROR, io_error);
2025 if (disable_buffering)
2026 io_end_buffering_out();
2028 stats.flist_size = stats.total_written - start_write;
2029 stats.num_files = flist->used;
2032 output_flist(flist);
2035 rprintf(FINFO, "send_file_list done\n");
2038 add_dirs_to_tree(-1, flist, dir_count);
2039 flist_done_allocating(flist);
2040 if (send_dir_ndx < 0) {
2041 write_ndx(f, NDX_FLIST_EOF);
2044 else if (file_total == 1) {
2045 /* If we're creating incremental file-lists and there
2046 * was just 1 item in the first file-list, send 1 more
2047 * file-list to check if this is a 1-file xfer. */
2048 send_extra_file_list(f, 1);
2055 struct file_list *recv_file_list(int f)
2057 struct file_list *flist;
2062 rprintf(FLOG, "receiving file list\n");
2063 if (show_filelist_p())
2064 start_filelist_progress("receiving file list");
2065 else if (inc_recurse && verbose && !am_server && !first_flist)
2066 rprintf(FCLIENT, "receiving incremental file list\n");
2068 start_read = stats.total_read;
2070 #ifdef SUPPORT_HARD_LINKS
2071 if (preserve_hard_links && !first_flist)
2075 flist = flist_new(0, "recv_file_list");
2078 if (flist->ndx_start == 0)
2079 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2080 dstart = dir_flist->used;
2086 while ((flags = read_byte(f)) != 0) {
2087 struct file_struct *file;
2089 flist_expand(flist, 1);
2091 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2092 flags |= read_byte(f) << 8;
2093 file = recv_file_entry(flist, flags, f);
2095 if (inc_recurse && S_ISDIR(file->mode)) {
2096 flist_expand(dir_flist, 1);
2097 dir_flist->files[dir_flist->used++] = file;
2100 flist->files[flist->used++] = file;
2102 maybe_emit_filelist_progress(flist->used);
2105 rprintf(FINFO, "recv_file_name(%s)\n",
2106 f_name(file, NULL));
2109 file_total += flist->used;
2111 flist->ndx_end = flist->ndx_start + flist->used - 1;
2112 if (inc_recurse && flist->ndx_start)
2113 flist->ndx_end -= dir_flist->used - dstart;
2116 rprintf(FINFO, "received %d names\n", flist->used);
2118 if (show_filelist_p())
2119 finish_filelist_progress(flist);
2122 if (need_unsorted_flist) {
2123 /* Create an extra array of index pointers that we can sort for
2124 * the generator's use (for wading through the files in sorted
2125 * order and for calling flist_find()). We keep the "files"
2126 * list unsorted for our exchange of index numbers with the
2127 * other side (since their names may not sort the same). */
2128 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2129 out_of_memory("recv_file_list");
2130 memcpy(flist->sorted, flist->files,
2131 flist->used * sizeof (struct file_struct*));
2132 if (inc_recurse && dir_flist->used > dstart) {
2133 dir_flist->sorted = realloc_array(dir_flist->sorted,
2134 struct file_struct *,
2136 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2137 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2138 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2143 flist->sorted = flist->files;
2144 if (inc_recurse && dir_flist->used > dstart) {
2145 dir_flist->sorted = dir_flist->files;
2146 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2151 flist_done_allocating(flist);
2153 recv_id_list(f, flist);
2155 clean_flist(flist, relative_paths);
2157 if (protocol_version < 30) {
2158 /* Recv the io_error flag */
2162 io_error |= read_int(f);
2166 output_flist(flist);
2169 rprintf(FINFO, "recv_file_list done\n");
2171 stats.flist_size += stats.total_read - start_read;
2172 stats.num_files += flist->used;
2177 /* This is only used once by the receiver if the very first file-list
2178 * has exactly one item in it. */
2179 void recv_additional_file_list(int f)
2181 struct file_list *flist;
2182 int ndx = read_ndx(f);
2183 if (ndx == NDX_FLIST_EOF) {
2185 change_local_filter_dir(NULL, 0, 0);
2187 ndx = NDX_FLIST_OFFSET - ndx;
2188 if (ndx < 0 || ndx >= dir_flist->used) {
2189 ndx = NDX_FLIST_OFFSET - ndx;
2191 "[%s] Invalid dir index: %d (%d - %d)\n",
2192 who_am_i(), ndx, NDX_FLIST_OFFSET,
2193 NDX_FLIST_OFFSET - dir_flist->used + 1);
2194 exit_cleanup(RERR_PROTOCOL);
2197 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2200 flist = recv_file_list(f);
2201 flist->parent_ndx = ndx;
2205 /* Search for an identically-named item in the file list. Note that the
2206 * items must agree in their directory-ness, or no match is returned. */
2207 int flist_find(struct file_list *flist, struct file_struct *f)
2209 int low = flist->low, high = flist->high;
2210 int diff, mid, mid_up;
2212 while (low <= high) {
2213 mid = (low + high) / 2;
2214 if (F_IS_ACTIVE(flist->sorted[mid]))
2217 /* Scan for the next non-empty entry using the cached
2218 * distance values. If the value isn't fully up-to-
2219 * date, update it. */
2220 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2221 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2223 mid_up += F_DEPTH(flist->sorted[mid_up]);
2224 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2225 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2227 if (mid_up > high) {
2228 /* If there's nothing left above us, set high to
2229 * a non-empty entry below us and continue. */
2230 high = mid - (int)flist->sorted[mid]->len32;
2231 if (!F_IS_ACTIVE(flist->sorted[high])) {
2233 high -= (int)flist->sorted[high]->len32;
2234 } while (!F_IS_ACTIVE(flist->sorted[high]));
2235 flist->sorted[mid]->len32 = mid - high;
2240 diff = f_name_cmp(flist->sorted[mid_up], f);
2242 if (protocol_version < 29
2243 && S_ISDIR(flist->sorted[mid_up]->mode)
2244 != S_ISDIR(f->mode))
2257 * Free up any resources a file_struct has allocated
2258 * and clear the file.
2260 void clear_file(struct file_struct *file)
2262 /* The +1 zeros out the first char of the basename. */
2263 memset(file, 0, FILE_STRUCT_LEN + 1);
2264 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2265 * entry. Likewise for len32 in the opposite direction. We assume
2266 * that we're alone for now since flist_find() will adjust the counts
2267 * it runs into that aren't up-to-date. */
2268 file->len32 = F_DEPTH(file) = 1;
2271 /* Allocate a new file list. */
2272 struct file_list *flist_new(int flags, char *msg)
2274 struct file_list *flist;
2276 if (!(flist = new0(struct file_list)))
2279 if (flags & FLIST_TEMP) {
2280 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2281 out_of_memory, POOL_INTERN)))
2284 /* This is a doubly linked list with prev looping back to
2285 * the end of the list, but the last next pointer is NULL. */
2287 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2288 out_of_memory, POOL_INTERN);
2289 if (!flist->file_pool)
2292 first_flist = cur_flist = flist->prev = flist;
2294 flist->file_pool = first_flist->file_pool;
2296 flist->ndx_start = first_flist->prev->ndx_end + 2;
2297 if (flist->ndx_start <= high_hlink_ndx)
2298 flist->ndx_start = high_hlink_ndx + 1;
2300 flist->prev = first_flist->prev;
2301 flist->prev->next = first_flist->prev = flist;
2303 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2310 /* Free up all elements in a flist. */
2311 void flist_free(struct file_list *flist)
2314 /* Was FLIST_TEMP dir-list. */
2315 } else if (flist == flist->prev) {
2316 first_flist = cur_flist = NULL;
2320 if (flist == cur_flist)
2321 cur_flist = flist->next;
2322 if (flist == first_flist)
2323 first_flist = first_flist->next;
2325 flist->prev->next = flist->next;
2327 flist->next = first_flist;
2329 flist->next->prev = flist->prev;
2330 file_total -= flist->used;
2334 if (!flist->prev || !flist_cnt)
2335 pool_destroy(flist->file_pool);
2337 pool_free_old(flist->file_pool, flist->pool_boundary);
2339 if (flist->sorted && flist->sorted != flist->files)
2340 free(flist->sorted);
2345 /* This routine ensures we don't have any duplicate names in our file list.
2346 * duplicate names can cause corruption because of the pipelining. */
2347 static void clean_flist(struct file_list *flist, int strip_root)
2349 char fbuf[MAXPATHLEN];
2354 if (flist->used == 0) {
2360 fsort(flist->sorted, flist->used);
2362 if (!am_sender || inc_recurse) {
2363 for (i = prev_i = 0; i < flist->used; i++) {
2364 if (F_IS_ACTIVE(flist->sorted[i])) {
2369 flist->low = prev_i;
2371 i = prev_i = flist->used - 1;
2375 while (++i < flist->used) {
2377 struct file_struct *file = flist->sorted[i];
2379 if (!F_IS_ACTIVE(file))
2381 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2383 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2384 int save_mode = file->mode;
2385 /* Make sure that this directory doesn't duplicate a
2386 * non-directory earlier in the list. */
2387 flist->high = prev_i;
2388 file->mode = S_IFREG;
2389 j = flist_find(flist, file);
2390 file->mode = save_mode;
2395 /* If one is a dir and the other is not, we want to
2396 * keep the dir because it might have contents in the
2397 * list. Otherwise keep the first one. */
2398 if (S_ISDIR(file->mode)) {
2399 struct file_struct *fp = flist->sorted[j];
2400 if (!S_ISDIR(fp->mode))
2404 file->flags |= FLAG_DUPLICATE;
2405 else { /* Make sure we merge our vital flags. */
2406 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2407 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2417 "removing duplicate name %s from file list (%d)\n",
2418 f_name(file, fbuf), drop + flist->ndx_start);
2420 clear_file(flist->sorted[drop]);
2424 if (flist->low == drop) {
2426 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2435 flist->high = prev_i;
2438 /* We need to strip off the leading slashes for relative
2439 * paths, but this must be done _after_ the sorting phase. */
2440 for (i = flist->low; i <= flist->high; i++) {
2441 struct file_struct *file = flist->sorted[i];
2445 while (*file->dirname == '/')
2447 if (!*file->dirname)
2448 file->dirname = NULL;
2452 if (prune_empty_dirs && !am_sender) {
2453 int j, prev_depth = 0;
2455 prev_i = 0; /* It's OK that this isn't really true. */
2457 for (i = flist->low; i <= flist->high; i++) {
2458 struct file_struct *fp, *file = flist->sorted[i];
2460 /* This temporarily abuses the F_DEPTH() value for a
2461 * directory that is in a chain that might get pruned.
2462 * We restore the old value if it gets a reprieve. */
2463 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2464 /* Dump empty dirs when coming back down. */
2465 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2466 fp = flist->sorted[prev_i];
2467 if (F_DEPTH(fp) >= 0)
2469 prev_i = -F_DEPTH(fp)-1;
2472 prev_depth = F_DEPTH(file);
2473 if (is_excluded(f_name(file, fbuf), 1,
2475 /* Keep dirs through this dir. */
2476 for (j = prev_depth-1; ; j--) {
2477 fp = flist->sorted[prev_i];
2478 if (F_DEPTH(fp) >= 0)
2480 prev_i = -F_DEPTH(fp)-1;
2484 F_DEPTH(file) = -prev_i-1;
2487 /* Keep dirs through this non-dir. */
2488 for (j = prev_depth; ; j--) {
2489 fp = flist->sorted[prev_i];
2490 if (F_DEPTH(fp) >= 0)
2492 prev_i = -F_DEPTH(fp)-1;
2497 /* Dump all remaining empty dirs. */
2499 struct file_struct *fp = flist->sorted[prev_i];
2500 if (F_DEPTH(fp) >= 0)
2502 prev_i = -F_DEPTH(fp)-1;
2506 for (i = flist->low; i <= flist->high; i++) {
2507 if (F_IS_ACTIVE(flist->sorted[i]))
2511 for (i = flist->high; i >= flist->low; i--) {
2512 if (F_IS_ACTIVE(flist->sorted[i]))
2519 static void output_flist(struct file_list *flist)
2521 char uidbuf[16], gidbuf[16], depthbuf[16];
2522 struct file_struct *file;
2523 const char *root, *dir, *slash, *name, *trail;
2524 const char *who = who_am_i();
2527 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2528 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2529 for (i = 0; i < flist->used; i++) {
2530 file = flist->sorted[i];
2531 if ((am_root || am_sender) && uid_ndx) {
2532 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2537 static char parens[] = "(\0)\0\0\0";
2538 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2539 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2540 pp, F_GROUP(file), pp + 2);
2544 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2545 if (F_IS_ACTIVE(file)) {
2546 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2547 if ((dir = file->dirname) == NULL)
2551 name = file->basename;
2552 trail = S_ISDIR(file->mode) ? "/" : "";
2554 root = dir = slash = name = trail = "";
2556 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2557 who, i + flist->ndx_start,
2558 root, dir, slash, name, trail,
2559 (int)file->mode, (double)F_LENGTH(file),
2560 uidbuf, gidbuf, file->flags);
2564 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2565 enum fnc_type { t_PATH, t_ITEM };
2567 static int found_prefix;
2569 /* Compare the names of two file_struct entities, similar to how strcmp()
2570 * would do if it were operating on the joined strings.
2572 * Some differences beginning with protocol_version 29: (1) directory names
2573 * are compared with an assumed trailing slash so that they compare in a
2574 * way that would cause them to sort immediately prior to any content they
2575 * may have; (2) a directory of any name compares after a non-directory of
2576 * any name at the same depth; (3) a directory with name "." compares prior
2577 * to anything else. These changes mean that a directory and a non-dir
2578 * with the same name will not compare as equal (protocol_version >= 29).
2580 * The dirname component can be an empty string, but the basename component
2581 * cannot (and never is in the current codebase). The basename component
2582 * may be NULL (for a removed item), in which case it is considered to be
2583 * after any existing item. */
2584 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2587 const uchar *c1, *c2;
2588 enum fnc_state state1, state2;
2589 enum fnc_type type1, type2;
2590 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2592 if (!f1 || !F_IS_ACTIVE(f1)) {
2593 if (!f2 || !F_IS_ACTIVE(f2))
2597 if (!f2 || !F_IS_ACTIVE(f2))
2600 c1 = (uchar*)f1->dirname;
2601 c2 = (uchar*)f2->dirname;
2605 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2606 c1 = (const uchar*)f1->basename;
2607 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2609 state1 = s_TRAILING;
2618 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2619 c2 = (const uchar*)f2->basename;
2620 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2622 state2 = s_TRAILING;
2632 return type1 == t_PATH ? 1 : -1;
2642 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2643 c1 = (const uchar*)f1->basename;
2644 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2646 state1 = s_TRAILING;
2652 state1 = s_TRAILING;
2653 if (type1 == t_PATH) {
2662 if (*c2 && type1 != type2)
2663 return type1 == t_PATH ? 1 : -1;
2672 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2673 c2 = (const uchar*)f2->basename;
2674 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2676 state2 = s_TRAILING;
2682 state2 = s_TRAILING;
2683 if (type2 == t_PATH) {
2696 return type1 == t_PATH ? 1 : -1;
2698 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2703 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2704 * not match if f2's basename is not an exact match of a path element in f1.
2705 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2706 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2710 return found_prefix;
2713 char *f_name_buf(void)
2715 static char names[5][MAXPATHLEN];
2716 static unsigned int n;
2718 n = (n + 1) % (sizeof names / sizeof names[0]);
2723 /* Return a copy of the full filename of a flist entry, using the indicated
2724 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2725 * done because we checked the size when creating the file_struct entry.
2727 char *f_name(const struct file_struct *f, char *fbuf)
2729 if (!f || !F_IS_ACTIVE(f))
2733 fbuf = f_name_buf();
2736 int len = strlen(f->dirname);
2737 memcpy(fbuf, f->dirname, len);
2739 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2741 strlcpy(fbuf, f->basename, MAXPATHLEN);
2746 /* Do a non-recursive scan of the named directory, possibly ignoring all
2747 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2748 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2749 * buffer (the functions we call will append names onto the end, but the old
2750 * dir value will be restored on exit). */
2751 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2753 struct file_list *dirlist;
2754 char dirbuf[MAXPATHLEN];
2755 int save_recurse = recurse;
2756 int save_xfer_dirs = xfer_dirs;
2757 int save_prune_empty_dirs = prune_empty_dirs;
2760 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2761 if (dlen >= MAXPATHLEN)
2766 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2770 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2771 xfer_dirs = save_xfer_dirs;
2772 recurse = save_recurse;
2774 flist_count_offset += dirlist->used;
2776 prune_empty_dirs = 0;
2777 dirlist->sorted = dirlist->files;
2778 clean_flist(dirlist, 0);
2779 prune_empty_dirs = save_prune_empty_dirs;
2782 output_flist(dirlist);