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, 2003, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
33 extern int do_progress;
34 extern int always_checksum;
36 extern int ignore_errors;
37 extern int numeric_ids;
40 extern int filesfrom_fd;
41 extern int one_file_system;
42 extern int copy_dirlinks;
43 extern int keep_dirlinks;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_devices;
47 extern int preserve_specials;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int relative_paths;
51 extern int implied_dirs;
52 extern int flist_extra_cnt;
53 extern int ignore_perishable;
54 extern int non_perishable_cnt;
55 extern int prune_empty_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
62 extern alloc_pool_t hlink_pool;
64 extern char curr_dir[MAXPATHLEN];
66 extern struct chmod_mode_struct *chmod_modes;
68 extern struct filter_list_struct filter_list;
69 extern struct filter_list_struct server_filter_list;
73 dev_t filesystem_dev; /* used to implement -x */
75 /* The tmp_* vars are used as a cache area by make_file() to store data
76 * that the sender doesn't need to remember in its file list. The data
77 * will survive just long enough to be used by send_file_entry(). */
78 static dev_t tmp_rdev;
79 #ifdef SUPPORT_HARD_LINKS
80 static struct idev tmp_idev;
82 static char tmp_sum[MD4_SUM_LENGTH];
84 static char empty_sum[MD4_SUM_LENGTH];
85 static int flist_count_offset; /* for --delete --progress */
87 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
88 static void output_flist(struct file_list *flist);
93 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
94 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
96 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
99 static int show_filelist_p(void)
101 return verbose && xfer_dirs && !am_server;
104 static void start_filelist_progress(char *kind)
106 rprintf(FCLIENT, "%s ... ", kind);
107 if (verbose > 1 || do_progress)
108 rprintf(FCLIENT, "\n");
112 static void emit_filelist_progress(int count)
114 rprintf(FCLIENT, " %d files...\r", count);
117 static void maybe_emit_filelist_progress(int count)
119 if (do_progress && show_filelist_p() && (count % 100) == 0)
120 emit_filelist_progress(count);
123 static void finish_filelist_progress(const struct file_list *flist)
126 /* This overwrites the progress line */
127 rprintf(FINFO, "%d file%sto consider\n",
128 flist->count, flist->count == 1 ? " " : "s ");
130 rprintf(FINFO, "done\n");
133 void show_flist_stats(void)
138 static void list_file_entry(struct file_struct *f)
140 char permbuf[PERMSTRING_SIZE];
143 if (!F_IS_ACTIVE(f)) {
144 /* this can happen if duplicate names were removed */
148 permstring(permbuf, f->mode);
152 if (preserve_links && S_ISLNK(f->mode)) {
153 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
154 permbuf, len, timestring(f->modtime),
155 f_name(f, NULL), F_SYMLINK(f));
159 rprintf(FINFO, "%s %11.0f %s %s\n",
160 permbuf, len, timestring(f->modtime),
165 /* Stat either a symlink or its referent, depending on the settings of
166 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
168 * If path is the name of a symlink, then the linkbuf buffer (which must hold
169 * MAXPATHLEN chars) will be set to the symlink's target string.
171 * The stat structure pointed to by stp will contain information about the
172 * link or the referent as appropriate, if they exist. */
173 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
176 if (link_stat(path, stp, copy_dirlinks) < 0)
178 if (S_ISLNK(stp->st_mode)) {
179 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
182 linkbuf[llen] = '\0';
183 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
185 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
188 return do_stat(path, stp);
193 return do_stat(path, stp);
197 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
201 return do_stat(path, stp);
202 if (do_lstat(path, stp) < 0)
204 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
206 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
211 return do_stat(path, stp);
215 /* This function is used to check if a file should be included/excluded
216 * from the list of files based on its name and type etc. The value of
217 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
218 static int is_excluded(char *fname, int is_dir, int filter_level)
220 #if 0 /* This currently never happens, so avoid a useless compare. */
221 if (filter_level == NO_FILTERS)
225 /* never exclude '.', even if somebody does --exclude '*' */
226 if (fname[0] == '.' && !fname[1])
228 /* Handle the -R version of the '.' dir. */
229 if (fname[0] == '/') {
230 int len = strlen(fname);
231 if (fname[len-1] == '.' && fname[len-2] == '/')
235 if (server_filter_list.head
236 && check_filter(&server_filter_list, fname, is_dir) < 0)
238 if (filter_level != ALL_FILTERS)
241 && check_filter(&filter_list, fname, is_dir) < 0)
246 static int to_wire_mode(mode_t mode)
249 #if _S_IFLNK != 0120000
251 return (mode & ~(_S_IFMT)) | 0120000;
257 static mode_t from_wire_mode(int mode)
259 #if _S_IFLNK != 0120000
260 if ((mode & (_S_IFMT)) == 0120000)
261 return (mode & ~(_S_IFMT)) | _S_IFLNK;
266 static void send_directory(int f, struct file_list *flist,
267 char *fbuf, int len);
269 static const char *flist_dir;
270 static int flist_dir_len;
274 * Make sure @p flist is big enough to hold at least @p flist->count
277 void flist_expand(struct file_list *flist)
279 struct file_struct **new_ptr;
281 if (flist->count < flist->malloced)
284 if (flist->malloced < FLIST_START)
285 flist->malloced = FLIST_START;
286 else if (flist->malloced >= FLIST_LINEAR)
287 flist->malloced += FLIST_LINEAR;
289 flist->malloced *= 2;
292 * In case count jumped or we are starting the list
293 * with a known size just set it.
295 if (flist->malloced < flist->count)
296 flist->malloced = flist->count;
298 new_ptr = realloc_array(flist->files, struct file_struct *,
301 if (verbose >= 2 && flist->malloced != FLIST_START) {
302 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
304 (double)sizeof flist->files[0] * flist->malloced,
305 (new_ptr == flist->files) ? " not" : "");
308 flist->files = new_ptr;
311 out_of_memory("flist_expand");
314 static void send_file_entry(struct file_struct *file, int f, int ndx)
316 unsigned short flags;
317 static time_t modtime;
321 static uint32 rdev_major;
324 static char lastname[MAXPATHLEN];
325 char fname[MAXPATHLEN];
326 int first_hlink_ndx = -1;
331 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
333 if (file->mode == mode)
334 flags |= XMIT_SAME_MODE;
337 if ((preserve_devices && IS_DEVICE(mode))
338 || (preserve_specials && IS_SPECIAL(mode))) {
339 if (protocol_version < 28) {
340 if (tmp_rdev == rdev)
341 flags |= XMIT_SAME_RDEV_pre28;
346 if ((uint32)major(rdev) == rdev_major)
347 flags |= XMIT_SAME_RDEV_MAJOR;
349 rdev_major = major(rdev);
350 if ((uint32)minor(rdev) <= 0xFFu)
351 flags |= XMIT_RDEV_MINOR_IS_SMALL;
353 } else if (protocol_version < 28)
354 rdev = MAKEDEV(0, 0);
356 if (F_UID(file) == uid)
357 flags |= XMIT_SAME_UID;
362 if (F_GID(file) == gid)
363 flags |= XMIT_SAME_GID;
367 if (file->modtime == modtime)
368 flags |= XMIT_SAME_TIME;
370 modtime = file->modtime;
372 #ifdef SUPPORT_HARD_LINKS
373 if (tmp_idev.dev != 0) {
374 if (protocol_version >= 30) {
375 struct idev_node *np = idev_node(tmp_idev.dev, tmp_idev.ino);
376 first_hlink_ndx = (int32)np->data - 1;
377 if (first_hlink_ndx < 0) {
378 np->data = (void*)(ndx + 1);
379 flags |= XMIT_HLINK_FIRST;
381 flags |= XMIT_HLINKED;
383 if (tmp_idev.dev == dev) {
384 if (protocol_version >= 28)
385 flags |= XMIT_SAME_DEV_pre30;
388 flags |= XMIT_HLINKED;
394 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
396 l2 = strlen(fname+l1);
399 flags |= XMIT_SAME_NAME;
401 flags |= XMIT_LONG_NAME;
403 /* We must make sure we don't send a zero flag byte or the
404 * other end will terminate the flist transfer. Note that
405 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
406 * it's harmless way to add a bit to the first flag byte. */
407 if (protocol_version >= 28) {
408 if (!flags && !S_ISDIR(mode))
409 flags |= XMIT_TOP_DIR;
410 if ((flags & 0xFF00) || !flags) {
411 flags |= XMIT_EXTENDED_FLAGS;
412 write_shortint(f, flags);
414 write_byte(f, flags);
417 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
418 write_byte(f, flags);
420 if (flags & XMIT_SAME_NAME)
422 if (flags & XMIT_LONG_NAME)
426 write_buf(f, fname + l1, l2);
428 if (first_hlink_ndx >= 0) {
429 write_int(f, first_hlink_ndx);
433 write_longint(f, F_LENGTH(file));
434 if (!(flags & XMIT_SAME_TIME))
435 write_int(f, modtime);
436 if (!(flags & XMIT_SAME_MODE))
437 write_int(f, to_wire_mode(mode));
438 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
443 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
448 if ((preserve_devices && IS_DEVICE(mode))
449 || (preserve_specials && IS_SPECIAL(mode))) {
450 if (protocol_version < 28) {
451 if (!(flags & XMIT_SAME_RDEV_pre28))
452 write_int(f, (int)rdev);
454 if (!(flags & XMIT_SAME_RDEV_MAJOR))
455 write_int(f, major(rdev));
456 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
457 write_byte(f, minor(rdev));
459 write_int(f, minor(rdev));
464 if (preserve_links && S_ISLNK(mode)) {
465 const char *sl = F_SYMLINK(file);
466 int len = strlen(sl);
468 write_buf(f, sl, len);
472 #ifdef SUPPORT_HARD_LINKS
473 if (tmp_idev.dev != 0 && protocol_version < 30) {
474 if (protocol_version < 26) {
475 /* 32-bit dev_t and ino_t */
476 write_int(f, (int32)dev);
477 write_int(f, (int32)tmp_idev.ino);
479 /* 64-bit dev_t and ino_t */
480 if (!(flags & XMIT_SAME_DEV_pre30))
481 write_longint(f, dev);
482 write_longint(f, tmp_idev.ino);
487 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
492 /* Prior to 28, we sent a useless set of nulls. */
495 write_buf(f, sum, checksum_len);
499 strlcpy(lastname, fname, MAXPATHLEN);
502 static struct file_struct *recv_file_entry(struct file_list *flist,
503 unsigned short flags, int f)
505 static time_t modtime;
509 static uint32 rdev_major;
512 static char lastname[MAXPATHLEN], *lastdir;
513 static int lastdir_depth, lastdir_len = -1;
514 static unsigned int del_hier_name_len = 0;
515 static int in_del_hier = 0;
516 char thisname[MAXPATHLEN];
517 unsigned int l1 = 0, l2 = 0;
518 int alloc_len, basename_len, dirname_len, linkname_len;
519 int extra_len = flist_extra_cnt * EXTRA_LEN;
520 int first_hlink_ndx = -1;
522 char *basename, *dirname, *bp;
523 struct file_struct *file;
526 modtime = 0, mode = 0;
527 dev = 0, rdev = MAKEDEV(0, 0);
536 if (flags & XMIT_SAME_NAME)
539 if (flags & XMIT_LONG_NAME)
544 if (l2 >= MAXPATHLEN - l1) {
546 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
547 flags, l1, l2, lastname, who_am_i());
548 overflow_exit("recv_file_entry");
551 strlcpy(thisname, lastname, l1 + 1);
552 read_sbuf(f, &thisname[l1], l2);
553 thisname[l1 + l2] = 0;
555 strlcpy(lastname, thisname, MAXPATHLEN);
557 clean_fname(thisname, 0);
560 sanitize_path(thisname, thisname, "", 0, NULL);
562 if ((basename = strrchr(thisname, '/')) != NULL) {
563 dirname_len = ++basename - thisname; /* counts future '\0' */
564 if (lastdir_len == dirname_len - 1
565 && strncmp(thisname, lastdir, lastdir_len) == 0) {
567 dirname_len = 0; /* indicates no copy is needed */
575 basename_len = strlen(basename) + 1; /* count the '\0' */
577 #ifdef SUPPORT_HARD_LINKS
578 if (protocol_version >= 30
579 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
580 struct file_struct *first;
581 first_hlink_ndx = read_int(f);
582 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
584 "hard-link reference out of range: %d (%d)\n",
585 first_hlink_ndx, flist->count);
586 exit_cleanup(RERR_PROTOCOL);
588 first = flist->files[first_hlink_ndx];
589 file_length = F_LENGTH(first);
590 modtime = first->modtime;
596 if ((preserve_devices && IS_DEVICE(mode))
597 || (preserve_specials && IS_SPECIAL(mode))) {
598 uint32 *devp = F_RDEV_P(first);
599 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
601 if (preserve_links && S_ISLNK(mode))
602 linkname_len = strlen(F_SYMLINK(first)) + 1;
609 file_length = read_longint(f);
610 if (!(flags & XMIT_SAME_TIME))
611 modtime = (time_t)read_int(f);
612 if (!(flags & XMIT_SAME_MODE))
613 mode = from_wire_mode(read_int(f));
615 if (chmod_modes && !S_ISLNK(mode))
616 mode = tweak_mode(mode, chmod_modes);
618 if (preserve_uid && !(flags & XMIT_SAME_UID))
619 uid = (uid_t)read_int(f);
620 if (preserve_gid && !(flags & XMIT_SAME_GID))
621 gid = (gid_t)read_int(f);
623 if ((preserve_devices && IS_DEVICE(mode))
624 || (preserve_specials && IS_SPECIAL(mode))) {
625 if (protocol_version < 28) {
626 if (!(flags & XMIT_SAME_RDEV_pre28))
627 rdev = (dev_t)read_int(f);
630 if (!(flags & XMIT_SAME_RDEV_MAJOR))
631 rdev_major = read_int(f);
632 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
633 rdev_minor = read_byte(f);
635 rdev_minor = read_int(f);
636 rdev = MAKEDEV(rdev_major, rdev_minor);
638 extra_len += 2 * EXTRA_LEN;
640 } else if (protocol_version < 28)
641 rdev = MAKEDEV(0, 0);
644 if (preserve_links && S_ISLNK(mode)) {
645 linkname_len = read_int(f) + 1; /* count the '\0' */
646 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
647 rprintf(FERROR, "overflow: linkname_len=%d\n",
649 overflow_exit("recv_file_entry");
656 #ifdef SUPPORT_HARD_LINKS
658 if (preserve_hard_links) {
659 if (protocol_version < 28 && S_ISREG(mode))
660 flags |= XMIT_HLINKED;
661 if (flags & XMIT_HLINKED)
662 extra_len += EXTRA_LEN;
666 if (always_checksum && S_ISREG(mode))
667 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
669 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
670 extra_len += EXTRA_LEN;
672 #if EXTRA_ROUNDING > 0
673 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
674 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
677 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
679 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
681 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
683 file = (struct file_struct *)bp;
684 bp += FILE_STRUCT_LEN;
686 memcpy(bp, basename, basename_len);
687 bp += basename_len + linkname_len; /* skip space for symlink too */
689 #ifdef SUPPORT_HARD_LINKS
690 if (flags & XMIT_HLINKED)
691 file->flags |= FLAG_HLINKED;
693 file->modtime = modtime;
694 file->len32 = (uint32)file_length;
695 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
696 file->flags |= FLAG_LENGTH64;
697 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
706 file->dirname = lastdir = bp;
707 lastdir_len = dirname_len - 1;
708 memcpy(bp, dirname, dirname_len - 1);
711 lastdir_depth = count_dir_elements(lastdir);
712 file->dir.depth = lastdir_depth + 1;
713 } else if (dirname) {
714 file->dirname = dirname; /* we're reusing lastname */
715 file->dir.depth = lastdir_depth + 1;
720 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
722 if (flags & XMIT_TOP_DIR) {
723 in_del_hier = recurse;
724 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
725 if (relative_paths && del_hier_name_len > 2
726 && lastname[del_hier_name_len-1] == '.'
727 && lastname[del_hier_name_len-2] == '/')
728 del_hier_name_len -= 2;
729 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
730 } else if (in_del_hier) {
731 if (!relative_paths || !del_hier_name_len
732 || (l1 >= del_hier_name_len
733 && lastname[del_hier_name_len] == '/'))
734 file->flags |= FLAG_XFER_DIR;
740 if ((preserve_devices && IS_DEVICE(mode))
741 || (preserve_specials && IS_SPECIAL(mode))) {
742 uint32 *devp = F_RDEV_P(file);
743 DEV_MAJOR(devp) = major(rdev);
744 DEV_MINOR(devp) = minor(rdev);
749 bp = (char*)file->basename + basename_len;
750 if (first_hlink_ndx >= 0) {
751 struct file_struct *first = flist->files[first_hlink_ndx];
752 memcpy(bp, F_SYMLINK(first), linkname_len);
754 read_sbuf(f, bp, linkname_len - 1);
756 sanitize_path(bp, bp, "", lastdir_depth, NULL);
760 #ifdef SUPPORT_HARD_LINKS
761 if (preserve_hard_links && flags & XMIT_HLINKED) {
762 if (protocol_version >= 30) {
763 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
764 ? flist->count : first_hlink_ndx;
766 struct idev *idevp = pool_talloc(hlink_pool, struct idev,
768 F_HL_IDEV(file) = idevp;
769 if (protocol_version < 26) {
770 idevp->dev = read_int(f);
771 idevp->ino = read_int(f);
773 if (!(flags & XMIT_SAME_DEV_pre30))
774 dev = read_longint(f);
776 idevp->ino = read_longint(f);
782 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
784 bp = (char*)F_SUM(file);
786 /* Prior to 28, we get a useless set of nulls. */
789 if (first_hlink_ndx >= 0) {
790 struct file_struct *first = flist->files[first_hlink_ndx];
791 memcpy(bp, F_SUM(first), checksum_len);
793 read_buf(f, bp, checksum_len);
800 * Create a file_struct for a named file by reading its stat()
801 * information and performing extensive checks against global
804 * @return the new file, or NULL if there was an error or this file
805 * should be excluded.
807 * @todo There is a small optimization opportunity here to avoid
808 * stat()ing the file in some circumstances, which has a certain cost.
809 * We are called immediately after doing readdir(), and so we may
810 * already know the d_type of the file. We could for example avoid
811 * statting directories if we're not recursing, but this is not a very
812 * important case. Some systems may not have d_type.
814 struct file_struct *make_file(const char *fname, struct file_list *flist,
815 STRUCT_STAT *stp, int flags, int filter_level)
817 static char *lastdir;
818 static int lastdir_len = -1;
819 struct file_struct *file;
821 char thisname[MAXPATHLEN];
822 char linkname[MAXPATHLEN];
823 int alloc_len, basename_len, dirname_len, linkname_len;
824 int extra_len = flist_extra_cnt * EXTRA_LEN;
825 char *basename, *dirname, *bp;
827 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
830 if (strlcpy(thisname, fname, sizeof thisname)
831 >= sizeof thisname - flist_dir_len) {
832 rprintf(FINFO, "skipping overly long name: %s\n", fname);
835 clean_fname(thisname, 0);
837 sanitize_path(thisname, thisname, "", 0, NULL);
839 if (stp && S_ISDIR(stp->st_mode)) {
840 st = *stp; /* Needed for "symlink/." with --relative. */
841 *linkname = '\0'; /* make IBM code checker happy */
842 } else if (readlink_stat(thisname, &st, linkname) != 0) {
843 int save_errno = errno;
844 /* See if file is excluded before reporting an error. */
845 if (filter_level != NO_FILTERS
846 && (is_excluded(thisname, 0, filter_level)
847 || is_excluded(thisname, 1, filter_level))) {
848 if (ignore_perishable && save_errno != ENOENT)
849 non_perishable_cnt++;
852 if (save_errno == ENOENT) {
854 /* Avoid "vanished" error if symlink points nowhere. */
855 if (copy_links && do_lstat(thisname, &st) == 0
856 && S_ISLNK(st.st_mode)) {
857 io_error |= IOERR_GENERAL;
858 rprintf(FERROR, "symlink has no referent: %s\n",
859 full_fname(thisname));
863 enum logcode c = am_daemon && protocol_version < 28
865 io_error |= IOERR_VANISHED;
866 rprintf(c, "file has vanished: %s\n",
867 full_fname(thisname));
870 io_error |= IOERR_GENERAL;
871 rsyserr(FERROR, save_errno, "readlink %s failed",
872 full_fname(thisname));
877 /* backup.c calls us with filter_level set to NO_FILTERS. */
878 if (filter_level == NO_FILTERS)
881 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
882 rprintf(FINFO, "skipping directory %s\n", thisname);
886 /* -x only affects directories because we need to avoid recursing
887 * into a mount-point directory, not to avoid copying a symlinked
888 * file if -L (or similar) was specified. */
889 if (one_file_system && st.st_dev != filesystem_dev
890 && S_ISDIR(st.st_mode)) {
891 if (one_file_system > 1) {
893 rprintf(FINFO, "skipping mount-point dir %s\n",
898 flags |= FLAG_MOUNT_DIR;
901 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
902 if (ignore_perishable)
903 non_perishable_cnt++;
907 if (lp_ignore_nonreadable(module_id)) {
909 if (!S_ISLNK(st.st_mode))
911 if (access(thisname, R_OK) != 0)
918 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
919 who_am_i(), thisname, filter_level);
922 if ((basename = strrchr(thisname, '/')) != NULL) {
923 dirname_len = ++basename - thisname; /* counts future '\0' */
924 if (lastdir_len == dirname_len - 1
925 && strncmp(thisname, lastdir, lastdir_len) == 0) {
927 dirname_len = 0; /* indicates no copy is needed */
935 basename_len = strlen(basename) + 1; /* count the '\0' */
938 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
943 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
944 extra_len += EXTRA_LEN;
946 #if EXTRA_ROUNDING > 0
947 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
948 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
951 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
954 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
956 if (!(bp = new_array(char, alloc_len)))
957 out_of_memory("make_file");
960 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
962 file = (struct file_struct *)bp;
963 bp += FILE_STRUCT_LEN;
965 memcpy(bp, basename, basename_len);
966 bp += basename_len + linkname_len; /* skip space for symlink too */
968 #ifdef SUPPORT_HARD_LINKS
969 if (preserve_hard_links && flist) {
970 if (protocol_version >= 28
971 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
972 : S_ISREG(st.st_mode)) {
973 tmp_idev.dev = st.st_dev;
974 tmp_idev.ino = st.st_ino;
976 tmp_idev.dev = tmp_idev.ino = 0;
980 #ifdef HAVE_STRUCT_STAT_ST_RDEV
981 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
982 tmp_rdev = st.st_rdev;
988 file->modtime = st.st_mtime;
989 file->len32 = (uint32)st.st_size;
990 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
991 file->flags |= FLAG_LENGTH64;
992 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
994 file->mode = st.st_mode;
996 F_UID(file) = st.st_uid;
998 F_GID(file) = st.st_gid;
1001 file->dirname = lastdir = bp;
1002 lastdir_len = dirname_len - 1;
1003 memcpy(bp, dirname, dirname_len - 1);
1007 file->dirname = dirname;
1009 #ifdef SUPPORT_LINKS
1011 bp = (char*)file->basename + basename_len;
1012 memcpy(bp, linkname, linkname_len);
1016 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1017 file_checksum(thisname, tmp_sum, st.st_size);
1019 file->dir.root = flist_dir;
1021 /* This code is only used by the receiver when it is building
1022 * a list of files for a delete pass. */
1023 if (keep_dirlinks && linkname_len && flist) {
1025 int save_mode = file->mode;
1026 file->mode = S_IFDIR; /* Find a directory with our name. */
1027 if (flist_find(the_file_list, file) >= 0
1028 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1029 file->modtime = st2.st_mtime;
1031 file->mode = st2.st_mode;
1033 F_UID(file) = st2.st_uid;
1035 F_GID(file) = st2.st_gid;
1037 file->mode = save_mode;
1040 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
1041 stats.total_size += st.st_size;
1043 if (basename_len == 0+1)
1049 /* Only called for temporary file_struct entries created by make_file(). */
1050 void unmake_file(struct file_struct *file)
1052 int extra_cnt = flist_extra_cnt + LEN64_BUMP(file);
1053 #if EXTRA_ROUNDING > 0
1054 if (extra_cnt & EXTRA_ROUNDING)
1055 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1057 free(REQ_EXTRA(file, extra_cnt));
1060 static struct file_struct *send_file_name(int f, struct file_list *flist,
1061 char *fname, STRUCT_STAT *stp,
1062 unsigned short flags)
1064 struct file_struct *file;
1066 file = make_file(fname, flist, stp, flags,
1067 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1071 if (chmod_modes && !S_ISLNK(file->mode))
1072 file->mode = tweak_mode(file->mode, chmod_modes);
1074 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1076 flist_expand(flist);
1077 flist->files[flist->count++] = file;
1079 send_file_entry(file, f, flist->count - 1);
1083 static void send_if_directory(int f, struct file_list *flist,
1084 struct file_struct *file,
1085 char *fbuf, unsigned int ol)
1087 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1089 if (S_ISDIR(file->mode)
1090 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1092 unsigned int len = strlen(fbuf);
1093 if (len > 1 && fbuf[len-1] == '/')
1095 if (len >= MAXPATHLEN - 1) {
1096 io_error |= IOERR_GENERAL;
1097 rprintf(FERROR, "skipping long-named directory: %s\n",
1101 save_filters = push_local_filters(fbuf, len);
1102 send_directory(f, flist, fbuf, len);
1103 pop_local_filters(save_filters);
1110 /* This function is normally called by the sender, but the receiving side also
1111 * calls it from get_dirlist() with f set to -1 so that we just construct the
1112 * file list in memory without sending it over the wire. Also, get_dirlist()
1113 * might call this with f set to -2, which also indicates that local filter
1114 * rules should be ignored. */
1115 static void send_directory(int f, struct file_list *flist, char *fbuf, int len)
1121 int start = flist->count;
1123 if (!(d = opendir(fbuf))) {
1124 io_error |= IOERR_GENERAL;
1125 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1130 if (len != 1 || *fbuf != '/')
1133 remainder = MAXPATHLEN - (p - fbuf);
1135 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1136 char *dname = d_name(di);
1137 if (dname[0] == '.' && (dname[1] == '\0'
1138 || (dname[1] == '.' && dname[2] == '\0')))
1140 if (strlcpy(p, dname, remainder) >= remainder) {
1141 io_error |= IOERR_GENERAL;
1143 "cannot send long-named file %s\n",
1148 send_file_name(f, flist, fbuf, NULL, 0);
1154 io_error |= IOERR_GENERAL;
1155 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1161 int i, end = flist->count - 1;
1162 /* send_if_directory() bumps flist->count, so use "end". */
1163 for (i = start; i <= end; i++)
1164 send_if_directory(f, flist, flist->files[i], fbuf, len);
1168 struct file_list *send_file_list(int f, int argc, char *argv[])
1172 char *p, *dir, olddir[sizeof curr_dir];
1173 char lastpath[MAXPATHLEN] = "";
1174 struct file_list *flist;
1175 struct timeval start_tv, end_tv;
1179 rprintf(FLOG, "building file list\n");
1180 if (show_filelist_p())
1181 start_filelist_progress("building file list");
1183 start_write = stats.total_written;
1184 gettimeofday(&start_tv, NULL);
1186 #ifdef SUPPORT_HARD_LINKS
1187 if (preserve_hard_links && protocol_version >= 30)
1191 flist = flist_new("send_file_list");
1193 io_start_buffering_out();
1194 if (filesfrom_fd >= 0) {
1195 if (argv[0] && !push_dir(argv[0], 0)) {
1196 rsyserr(FERROR, errno, "push_dir %s failed",
1197 full_fname(argv[0]));
1198 exit_cleanup(RERR_FILESELECT);
1204 char fbuf[MAXPATHLEN];
1209 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1211 sanitize_path(fbuf, fbuf, "", 0, NULL);
1215 strlcpy(fbuf, *argv++, MAXPATHLEN);
1217 sanitize_path(fbuf, fbuf, "", 0, NULL);
1221 if (relative_paths) {
1222 /* We clean up fbuf below. */
1224 } else if (!len || fbuf[len - 1] == '/') {
1225 if (len == 2 && fbuf[0] == '.') {
1226 /* Turn "./" into just "." rather than "./." */
1229 if (len + 1 >= MAXPATHLEN)
1230 overflow_exit("send_file_list");
1235 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1236 && (len == 2 || fbuf[len-3] == '/')) {
1237 if (len + 2 >= MAXPATHLEN)
1238 overflow_exit("send_file_list");
1244 is_dot_dir = fbuf[len-1] == '.'
1245 && (len == 1 || fbuf[len-2] == '/');
1248 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1249 io_error |= IOERR_GENERAL;
1250 rsyserr(FERROR, errno, "link_stat %s failed",
1255 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1256 rprintf(FINFO, "skipping directory %s\n", fbuf);
1263 if (!relative_paths) {
1264 p = strrchr(fbuf, '/');
1271 len -= p - fbuf + 1;
1276 if ((p = strstr(fbuf, "/./")) != NULL) {
1282 len -= p - fbuf + 3;
1286 /* Get rid of trailing "/" and "/.". */
1288 if (fn[len - 1] == '/') {
1290 if (!--len && !dir) {
1295 else if (len >= 2 && fn[len - 1] == '.'
1296 && fn[len - 2] == '/') {
1298 if (!(len -= 2) && !dir) {
1305 if (len == 1 && fn[0] == '/')
1308 /* Reject a ".." dir in the active part of the path. */
1309 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1310 if ((p[2] == '/' || p[2] == '\0')
1311 && (p == fn || p[-1] == '/')) {
1313 "found \"..\" dir in relative path: %s\n",
1315 exit_cleanup(RERR_SYNTAX);
1326 static const char *lastdir;
1327 static int lastdir_len;
1329 strlcpy(olddir, curr_dir, sizeof olddir);
1331 if (!push_dir(dir, 0)) {
1332 io_error |= IOERR_GENERAL;
1333 rsyserr(FERROR, errno, "push_dir %s failed",
1338 if (lastdir && strcmp(lastdir, dir) == 0) {
1339 flist_dir = lastdir;
1340 flist_dir_len = lastdir_len;
1342 flist_dir = lastdir = strdup(dir);
1343 flist_dir_len = lastdir_len = strlen(dir);
1348 memmove(fbuf, fn, len + 1);
1350 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1351 /* Send the implied directories at the start of the
1352 * source spec, so we get their permissions right. */
1353 char *lp = lastpath, *slash = fbuf;
1355 /* Skip any initial directories in our path that we
1356 * have in common with lastpath. */
1357 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1362 if (fn != p || (*lp && *lp != '/')) {
1363 int save_copy_links = copy_links;
1364 int save_xfer_dirs = xfer_dirs;
1365 copy_links |= copy_unsafe_links;
1367 while ((slash = strchr(slash+1, '/')) != 0) {
1369 send_file_name(f, flist, fbuf, NULL, 0);
1372 copy_links = save_copy_links;
1373 xfer_dirs = save_xfer_dirs;
1375 strlcpy(lastpath, fbuf, sizeof lastpath);
1380 if (one_file_system)
1381 filesystem_dev = st.st_dev;
1383 if (recurse || (xfer_dirs && is_dot_dir)) {
1384 struct file_struct *file;
1385 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1387 send_if_directory(f, flist, file, fbuf, len);
1389 send_file_name(f, flist, fbuf, &st, 0);
1394 if (!pop_dir(olddir)) {
1395 rsyserr(FERROR, errno, "pop_dir %s failed",
1396 full_fname(olddir));
1397 exit_cleanup(RERR_FILESELECT);
1402 gettimeofday(&end_tv, NULL);
1403 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1404 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1405 if (stats.flist_buildtime == 0)
1406 stats.flist_buildtime = 1;
1409 write_byte(f, 0); /* Indicate end of file list */
1411 #ifdef SUPPORT_HARD_LINKS
1412 if (preserve_hard_links && protocol_version >= 30)
1416 if (show_filelist_p())
1417 finish_filelist_progress(flist);
1419 gettimeofday(&end_tv, NULL);
1420 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1421 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1423 /* Sort the list without removing any duplicates. This allows the
1424 * receiving side to ask for any name they like, which gives us the
1425 * flexibility to change the way we unduplicate names in the future
1426 * without causing a compatibility problem with older versions. */
1427 clean_flist(flist, 0, 0);
1432 /* send the io_error flag */
1433 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1436 stats.flist_size = stats.total_written - start_write;
1437 stats.num_files = flist->count;
1440 output_flist(flist);
1443 rprintf(FINFO, "send_file_list done\n");
1448 struct file_list *recv_file_list(int f)
1450 struct file_list *flist;
1451 unsigned short flags;
1454 rprintf(FLOG, "receiving file list\n");
1455 if (show_filelist_p())
1456 start_filelist_progress("receiving file list");
1458 start_read = stats.total_read;
1460 flist = flist_new("recv_file_list");
1462 #ifdef SUPPORT_HARD_LINKS
1463 if (preserve_hard_links && protocol_version < 30)
1467 while ((flags = read_byte(f)) != 0) {
1468 struct file_struct *file;
1470 flist_expand(flist);
1472 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1473 flags |= read_byte(f) << 8;
1474 file = recv_file_entry(flist, flags, f);
1476 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1477 stats.total_size += F_LENGTH(file);
1479 flist->files[flist->count++] = file;
1481 maybe_emit_filelist_progress(flist->count);
1484 rprintf(FINFO, "recv_file_name(%s)\n",
1485 f_name(file, NULL));
1488 recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1491 rprintf(FINFO, "received %d names\n", flist->count);
1493 if (show_filelist_p())
1494 finish_filelist_progress(flist);
1496 clean_flist(flist, relative_paths, 1);
1499 recv_uid_list(f, flist);
1501 /* Recv the io_error flag */
1502 if (lp_ignore_errors(module_id) || ignore_errors)
1505 io_error |= read_int(f);
1509 output_flist(flist);
1513 for (i = 0; i < flist->count; i++)
1514 list_file_entry(flist->files[i]);
1518 rprintf(FINFO, "recv_file_list done\n");
1520 stats.flist_size = stats.total_read - start_read;
1521 stats.num_files = flist->count;
1526 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1528 return f_name_cmp(*file1, *file2);
1531 /* Search for an identically-named item in the file list. Note that the
1532 * items must agree in their directory-ness, or no match is returned. */
1533 int flist_find(struct file_list *flist, struct file_struct *f)
1535 int low = flist->low, high = flist->high;
1536 int diff, mid, mid_up;
1538 while (low <= high) {
1539 mid = (low + high) / 2;
1540 if (F_IS_ACTIVE(flist->files[mid]))
1543 /* Scan for the next non-empty entry using the cached
1544 * distance values. If the value isn't fully up-to-
1545 * date, update it. */
1546 mid_up = mid + flist->files[mid]->dir.depth;
1547 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1549 mid_up += flist->files[mid_up]->dir.depth;
1550 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1551 flist->files[mid]->dir.depth = mid_up - mid;
1553 if (mid_up > high) {
1554 /* If there's nothing left above us, set high to
1555 * a non-empty entry below us and continue. */
1556 high = mid - (int)flist->files[mid]->len32;
1557 if (!F_IS_ACTIVE(flist->files[high])) {
1559 high -= (int)flist->files[high]->len32;
1560 } while (!F_IS_ACTIVE(flist->files[high]));
1561 flist->files[mid]->len32 = mid - high;
1566 diff = f_name_cmp(flist->files[mid_up], f);
1568 if (protocol_version < 29
1569 && S_ISDIR(flist->files[mid_up]->mode)
1570 != S_ISDIR(f->mode))
1583 * Free up any resources a file_struct has allocated
1584 * and clear the file.
1586 void clear_file(struct file_struct *file)
1588 /* The +1 zeros out the first char of the basename. */
1589 memset(file, 0, FILE_STRUCT_LEN + 1);
1590 /* In an empty entry, dir.depth is an offset to the next non-empty
1591 * entry. Likewise for len32 in the opposite direction. We assume
1592 * that we're alone for now since flist_find() will adjust the counts
1593 * it runs into that aren't up-to-date. */
1594 file->len32 = file->dir.depth = 1;
1597 /* Allocate a new file list. */
1598 struct file_list *flist_new(char *msg)
1600 struct file_list *flist;
1602 flist = new(struct file_list);
1606 memset(flist, 0, sizeof flist[0]);
1608 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1614 /* Free up all elements in a flist. */
1615 void flist_free(struct file_list *flist)
1617 pool_destroy(flist->file_pool);
1623 * This routine ensures we don't have any duplicate names in our file list.
1624 * duplicate names can cause corruption because of the pipelining
1626 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1628 char fbuf[MAXPATHLEN];
1633 if (flist->count == 0) {
1638 qsort(flist->files, flist->count,
1639 sizeof flist->files[0], (int (*)())file_compare);
1641 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1642 if (F_IS_ACTIVE(flist->files[i])) {
1647 flist->low = prev_i;
1648 while (++i < flist->count) {
1650 struct file_struct *file = flist->files[i];
1652 if (!F_IS_ACTIVE(file))
1654 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1656 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1657 int save_mode = file->mode;
1658 /* Make sure that this directory doesn't duplicate a
1659 * non-directory earlier in the list. */
1660 flist->high = prev_i;
1661 file->mode = S_IFREG;
1662 j = flist_find(flist, file);
1663 file->mode = save_mode;
1667 struct file_struct *fp = flist->files[j];
1669 /* If one is a dir and the other is not, we want to
1670 * keep the dir because it might have contents in the
1672 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1673 if (S_ISDIR(file->mode))
1679 if (verbose > 1 && !am_server) {
1681 "removing duplicate name %s from file list (%d)\n",
1682 f_name(file, fbuf), drop);
1684 /* Make sure we don't lose track of a user-specified
1686 flist->files[keep]->flags |= flist->files[drop]->flags
1687 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1689 clear_file(flist->files[drop]);
1692 if (flist->low == drop) {
1694 j < i && !F_IS_ACTIVE(flist->files[j]);
1703 flist->high = no_dups ? prev_i : flist->count - 1;
1706 /* We need to strip off the leading slashes for relative
1707 * paths, but this must be done _after_ the sorting phase. */
1708 for (i = flist->low; i <= flist->high; i++) {
1709 struct file_struct *file = flist->files[i];
1713 while (*file->dirname == '/')
1715 if (!*file->dirname)
1716 file->dirname = NULL;
1720 if (prune_empty_dirs && no_dups) {
1721 int j, prev_depth = 0;
1723 prev_i = 0; /* It's OK that this isn't really true. */
1725 for (i = flist->low; i <= flist->high; i++) {
1726 struct file_struct *fp, *file = flist->files[i];
1728 /* This temporarily abuses the dir.depth value for a
1729 * directory that is in a chain that might get pruned.
1730 * We restore the old value if it gets a reprieve. */
1731 if (S_ISDIR(file->mode) && file->dir.depth) {
1732 /* Dump empty dirs when coming back down. */
1733 for (j = prev_depth; j >= file->dir.depth; j--) {
1734 fp = flist->files[prev_i];
1735 if (fp->dir.depth >= 0)
1737 prev_i = -fp->dir.depth-1;
1740 prev_depth = file->dir.depth;
1741 if (is_excluded(f_name(file, fbuf), 1,
1743 /* Keep dirs through this dir. */
1744 for (j = prev_depth-1; ; j--) {
1745 fp = flist->files[prev_i];
1746 if (fp->dir.depth >= 0)
1748 prev_i = -fp->dir.depth-1;
1752 file->dir.depth = -prev_i-1;
1755 /* Keep dirs through this non-dir. */
1756 for (j = prev_depth; ; j--) {
1757 fp = flist->files[prev_i];
1758 if (fp->dir.depth >= 0)
1760 prev_i = -fp->dir.depth-1;
1765 /* Dump empty all remaining empty dirs. */
1767 struct file_struct *fp = flist->files[prev_i];
1768 if (fp->dir.depth >= 0)
1770 prev_i = -fp->dir.depth-1;
1774 for (i = flist->low; i <= flist->high; i++) {
1775 if (F_IS_ACTIVE(flist->files[i]))
1779 for (i = flist->high; i >= flist->low; i--) {
1780 if (F_IS_ACTIVE(flist->files[i]))
1787 static void output_flist(struct file_list *flist)
1789 char uidbuf[16], gidbuf[16], depthbuf[16];
1790 struct file_struct *file;
1791 const char *dir, *slash, *name, *trail;
1792 const char *who = who_am_i();
1795 for (i = 0; i < flist->count; i++) {
1796 file = flist->files[i];
1797 if ((am_root || am_sender) && preserve_uid) {
1798 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1802 if (preserve_gid && F_GID(file) != GID_NONE) {
1803 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1808 snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
1809 if (F_IS_ACTIVE(file)) {
1810 if ((dir = file->dirname) == NULL)
1814 name = file->basename;
1815 trail = S_ISDIR(file->mode) ? "/" : "";
1817 dir = slash = name = trail = "";
1818 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1819 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1820 dir, slash, name, trail, (int)file->mode,
1821 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1825 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1826 enum fnc_type { t_PATH, t_ITEM };
1828 /* Compare the names of two file_struct entities, similar to how strcmp()
1829 * would do if it were operating on the joined strings.
1831 * Some differences beginning with protocol_version 29: (1) directory names
1832 * are compared with an assumed trailing slash so that they compare in a
1833 * way that would cause them to sort immediately prior to any content they
1834 * may have; (2) a directory of any name compares after a non-directory of
1835 * any name at the same depth; (3) a directory with name "." compares prior
1836 * to anything else. These changes mean that a directory and a non-dir
1837 * with the same name will not compare as equal (protocol_version >= 29).
1839 * The dirname component can be an empty string, but the basename component
1840 * cannot (and never is in the current codebase). The basename component
1841 * may be NULL (for a removed item), in which case it is considered to be
1842 * after any existing item. */
1843 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1846 const uchar *c1, *c2;
1847 enum fnc_state state1, state2;
1848 enum fnc_type type1, type2;
1849 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1851 if (!f1 || !F_IS_ACTIVE(f1)) {
1852 if (!f2 || !F_IS_ACTIVE(f2))
1856 if (!f2 || !F_IS_ACTIVE(f2))
1859 c1 = (uchar*)f1->dirname;
1860 c2 = (uchar*)f2->dirname;
1864 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1865 c1 = (const uchar*)f1->basename;
1866 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1868 state1 = s_TRAILING;
1877 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1878 c2 = (const uchar*)f2->basename;
1879 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1881 state2 = s_TRAILING;
1891 return type1 == t_PATH ? 1 : -1;
1901 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1902 c1 = (const uchar*)f1->basename;
1903 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1905 state1 = s_TRAILING;
1911 state1 = s_TRAILING;
1912 if (type1 == t_PATH) {
1921 if (*c2 && type1 != type2)
1922 return type1 == t_PATH ? 1 : -1;
1931 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1932 c2 = (const uchar*)f2->basename;
1933 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1935 state2 = s_TRAILING;
1941 state2 = s_TRAILING;
1942 if (type2 == t_PATH) {
1954 return type1 == t_PATH ? 1 : -1;
1956 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1961 char *f_name_buf(void)
1963 static char names[5][MAXPATHLEN];
1964 static unsigned int n;
1966 n = (n + 1) % (sizeof names / sizeof names[0]);
1971 /* Return a copy of the full filename of a flist entry, using the indicated
1972 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1973 * done because we checked the size when creating the file_struct entry.
1975 char *f_name(struct file_struct *f, char *fbuf)
1977 if (!f || !F_IS_ACTIVE(f))
1981 fbuf = f_name_buf();
1984 int len = strlen(f->dirname);
1985 memcpy(fbuf, f->dirname, len);
1987 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1989 strlcpy(fbuf, f->basename, MAXPATHLEN);
1994 /* Do a non-recursive scan of the named directory, possibly ignoring all
1995 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1996 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1997 * buffer (the functions we call will append names onto the end, but the old
1998 * dir value will be restored on exit). */
1999 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2001 struct file_list *dirlist;
2002 char dirbuf[MAXPATHLEN];
2003 int save_recurse = recurse;
2004 int save_xfer_dirs = xfer_dirs;
2007 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2008 if (dlen >= MAXPATHLEN)
2013 dirlist = flist_new("get_dirlist");
2017 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
2018 xfer_dirs = save_xfer_dirs;
2019 recurse = save_recurse;
2021 flist_count_offset += dirlist->count;
2023 clean_flist(dirlist, 0, 0);
2026 output_flist(dirlist);