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 file_extra_cnt;
53 extern int ignore_perishable;
54 extern int non_perishable_cnt;
55 extern int prune_empty_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
63 extern char curr_dir[MAXPATHLEN];
65 extern struct chmod_mode_struct *chmod_modes;
67 extern struct filter_list_struct filter_list;
68 extern struct filter_list_struct server_filter_list;
72 dev_t filesystem_dev; /* used to implement -x */
74 /* The tmp_* vars are used as a cache area by make_file() to store data
75 * that the sender doesn't need to remember in its file list. The data
76 * will survive just long enough to be used by send_file_entry(). */
77 static dev_t tmp_rdev;
78 #ifdef SUPPORT_HARD_LINKS
79 static int64 tmp_dev, tmp_ino;
81 static char tmp_sum[MD4_SUM_LENGTH];
83 static char empty_sum[MD4_SUM_LENGTH];
84 static int flist_count_offset; /* for --delete --progress */
86 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
87 static void output_flist(struct file_list *flist);
92 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
93 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
95 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
98 static int show_filelist_p(void)
100 return verbose && xfer_dirs && !am_server;
103 static void start_filelist_progress(char *kind)
105 rprintf(FCLIENT, "%s ... ", kind);
106 if (verbose > 1 || do_progress)
107 rprintf(FCLIENT, "\n");
111 static void emit_filelist_progress(int count)
113 rprintf(FCLIENT, " %d files...\r", count);
116 static void maybe_emit_filelist_progress(int count)
118 if (do_progress && show_filelist_p() && (count % 100) == 0)
119 emit_filelist_progress(count);
122 static void finish_filelist_progress(const struct file_list *flist)
125 /* This overwrites the progress line */
126 rprintf(FINFO, "%d file%sto consider\n",
127 flist->count, flist->count == 1 ? " " : "s ");
129 rprintf(FINFO, "done\n");
132 void show_flist_stats(void)
137 static void list_file_entry(struct file_struct *f)
139 char permbuf[PERMSTRING_SIZE];
142 if (!F_IS_ACTIVE(f)) {
143 /* this can happen if duplicate names were removed */
147 permstring(permbuf, f->mode);
151 if (preserve_links && S_ISLNK(f->mode)) {
152 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
153 permbuf, len, timestring(f->modtime),
154 f_name(f, NULL), F_SYMLINK(f));
158 rprintf(FINFO, "%s %11.0f %s %s\n",
159 permbuf, len, timestring(f->modtime),
164 /* Stat either a symlink or its referent, depending on the settings of
165 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
167 * If path is the name of a symlink, then the linkbuf buffer (which must hold
168 * MAXPATHLEN chars) will be set to the symlink's target string.
170 * The stat structure pointed to by stp will contain information about the
171 * link or the referent as appropriate, if they exist. */
172 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
175 if (link_stat(path, stp, copy_dirlinks) < 0)
177 if (S_ISLNK(stp->st_mode)) {
178 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
181 linkbuf[llen] = '\0';
182 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
184 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
187 return do_stat(path, stp);
192 return do_stat(path, stp);
196 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
200 return do_stat(path, stp);
201 if (do_lstat(path, stp) < 0)
203 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
205 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
210 return do_stat(path, stp);
214 /* This function is used to check if a file should be included/excluded
215 * from the list of files based on its name and type etc. The value of
216 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
217 static int is_excluded(char *fname, int is_dir, int filter_level)
219 #if 0 /* This currently never happens, so avoid a useless compare. */
220 if (filter_level == NO_FILTERS)
224 /* never exclude '.', even if somebody does --exclude '*' */
225 if (fname[0] == '.' && !fname[1])
227 /* Handle the -R version of the '.' dir. */
228 if (fname[0] == '/') {
229 int len = strlen(fname);
230 if (fname[len-1] == '.' && fname[len-2] == '/')
234 if (server_filter_list.head
235 && check_filter(&server_filter_list, fname, is_dir) < 0)
237 if (filter_level != ALL_FILTERS)
240 && check_filter(&filter_list, fname, is_dir) < 0)
245 static int to_wire_mode(mode_t mode)
248 #if _S_IFLNK != 0120000
250 return (mode & ~(_S_IFMT)) | 0120000;
256 static mode_t from_wire_mode(int mode)
258 #if _S_IFLNK != 0120000
259 if ((mode & (_S_IFMT)) == 0120000)
260 return (mode & ~(_S_IFMT)) | _S_IFLNK;
265 static void send_directory(int f, struct file_list *flist,
266 char *fbuf, int len);
268 static const char *flist_dir;
269 static int flist_dir_len;
273 * Make sure @p flist is big enough to hold at least @p flist->count
276 void flist_expand(struct file_list *flist)
278 struct file_struct **new_ptr;
280 if (flist->count < flist->malloced)
283 if (flist->malloced < FLIST_START)
284 flist->malloced = FLIST_START;
285 else if (flist->malloced >= FLIST_LINEAR)
286 flist->malloced += FLIST_LINEAR;
288 flist->malloced *= 2;
291 * In case count jumped or we are starting the list
292 * with a known size just set it.
294 if (flist->malloced < flist->count)
295 flist->malloced = flist->count;
297 new_ptr = realloc_array(flist->files, struct file_struct *,
300 if (verbose >= 2 && flist->malloced != FLIST_START) {
301 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
303 (double)sizeof flist->files[0] * flist->malloced,
304 (new_ptr == flist->files) ? " not" : "");
307 flist->files = new_ptr;
310 out_of_memory("flist_expand");
313 static void send_file_entry(struct file_struct *file, int f, int ndx)
315 static time_t modtime;
319 static uint32 rdev_major;
322 static char lastname[MAXPATHLEN];
323 char fname[MAXPATHLEN];
324 int first_hlink_ndx = -1;
330 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
332 if (file->mode == mode)
333 flags |= XMIT_SAME_MODE;
336 if ((preserve_devices && IS_DEVICE(mode))
337 || (preserve_specials && IS_SPECIAL(mode))) {
338 if (protocol_version < 28) {
339 if (tmp_rdev == rdev)
340 flags |= XMIT_SAME_RDEV_pre28;
345 if ((uint32)major(rdev) == rdev_major)
346 flags |= XMIT_SAME_RDEV_MAJOR;
348 rdev_major = major(rdev);
349 if ((uint32)minor(rdev) <= 0xFFu)
350 flags |= XMIT_RDEV_MINOR_IS_SMALL;
352 } else if (protocol_version < 28)
353 rdev = MAKEDEV(0, 0);
355 if (F_UID(file) == uid)
356 flags |= XMIT_SAME_UID;
361 if (F_GID(file) == gid)
362 flags |= XMIT_SAME_GID;
366 if (file->modtime == modtime)
367 flags |= XMIT_SAME_TIME;
369 modtime = file->modtime;
371 #ifdef SUPPORT_HARD_LINKS
373 if (protocol_version >= 30) {
374 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
375 first_hlink_ndx = (int32)np->data - 1;
376 if (first_hlink_ndx < 0) {
377 np->data = (void*)(ndx + 1);
378 flags |= XMIT_HLINK_FIRST;
380 flags |= XMIT_HLINKED;
382 if (tmp_dev == dev) {
383 if (protocol_version >= 28)
384 flags |= XMIT_SAME_DEV_pre30;
387 flags |= XMIT_HLINKED;
393 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
395 l2 = strlen(fname+l1);
398 flags |= XMIT_SAME_NAME;
400 flags |= XMIT_LONG_NAME;
402 /* We must make sure we don't send a zero flag byte or the
403 * other end will terminate the flist transfer. Note that
404 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
405 * it's harmless way to add a bit to the first flag byte. */
406 if (protocol_version >= 28) {
407 if (!flags && !S_ISDIR(mode))
408 flags |= XMIT_TOP_DIR;
409 if ((flags & 0xFF00) || !flags) {
410 flags |= XMIT_EXTENDED_FLAGS;
411 write_shortint(f, flags);
413 write_byte(f, flags);
416 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
417 write_byte(f, flags);
419 if (flags & XMIT_SAME_NAME)
421 if (flags & XMIT_LONG_NAME)
425 write_buf(f, fname + l1, l2);
427 if (first_hlink_ndx >= 0) {
428 write_int(f, first_hlink_ndx);
432 write_longint(f, F_LENGTH(file));
433 if (!(flags & XMIT_SAME_TIME))
434 write_int(f, modtime);
435 if (!(flags & XMIT_SAME_MODE))
436 write_int(f, to_wire_mode(mode));
437 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
442 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
447 if ((preserve_devices && IS_DEVICE(mode))
448 || (preserve_specials && IS_SPECIAL(mode))) {
449 if (protocol_version < 28) {
450 if (!(flags & XMIT_SAME_RDEV_pre28))
451 write_int(f, (int)rdev);
453 if (!(flags & XMIT_SAME_RDEV_MAJOR))
454 write_int(f, major(rdev));
455 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
456 write_byte(f, minor(rdev));
458 write_int(f, minor(rdev));
463 if (preserve_links && S_ISLNK(mode)) {
464 const char *sl = F_SYMLINK(file);
465 int len = strlen(sl);
467 write_buf(f, sl, len);
471 #ifdef SUPPORT_HARD_LINKS
472 if (tmp_dev != 0 && protocol_version < 30) {
473 if (protocol_version < 26) {
474 /* 32-bit dev_t and ino_t */
475 write_int(f, (int32)dev);
476 write_int(f, (int32)tmp_ino);
478 /* 64-bit dev_t and ino_t */
479 if (!(flags & XMIT_SAME_DEV_pre30))
480 write_longint(f, dev);
481 write_longint(f, tmp_ino);
486 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
491 /* Prior to 28, we sent a useless set of nulls. */
494 write_buf(f, sum, checksum_len);
498 strlcpy(lastname, fname, MAXPATHLEN);
501 static struct file_struct *recv_file_entry(struct file_list *flist,
504 static time_t modtime;
508 static uint32 rdev_major;
511 static char lastname[MAXPATHLEN], *lastdir;
512 static int lastdir_depth, lastdir_len = -1;
513 static unsigned int del_hier_name_len = 0;
514 static int in_del_hier = 0;
515 char thisname[MAXPATHLEN];
516 unsigned int l1 = 0, l2 = 0;
517 int alloc_len, basename_len, dirname_len, linkname_len;
518 int extra_len = file_extra_cnt * EXTRA_LEN;
519 int first_hlink_ndx = -1;
521 char *basename, *dirname, *bp;
522 struct file_struct *file;
525 modtime = 0, mode = 0;
526 dev = 0, rdev = MAKEDEV(0, 0);
535 if (flags & XMIT_SAME_NAME)
538 if (flags & XMIT_LONG_NAME)
543 if (l2 >= MAXPATHLEN - l1) {
545 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
546 flags, l1, l2, lastname, who_am_i());
547 overflow_exit("recv_file_entry");
550 strlcpy(thisname, lastname, l1 + 1);
551 read_sbuf(f, &thisname[l1], l2);
552 thisname[l1 + l2] = 0;
554 strlcpy(lastname, thisname, MAXPATHLEN);
556 clean_fname(thisname, 0);
559 sanitize_path(thisname, thisname, "", 0, NULL);
561 if ((basename = strrchr(thisname, '/')) != NULL) {
562 dirname_len = ++basename - thisname; /* counts future '\0' */
563 if (lastdir_len == dirname_len - 1
564 && strncmp(thisname, lastdir, lastdir_len) == 0) {
566 dirname_len = 0; /* indicates no copy is needed */
574 basename_len = strlen(basename) + 1; /* count the '\0' */
576 #ifdef SUPPORT_HARD_LINKS
577 if (protocol_version >= 30
578 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
579 struct file_struct *first;
580 first_hlink_ndx = read_int(f);
581 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
583 "hard-link reference out of range: %d (%d)\n",
584 first_hlink_ndx, flist->count);
585 exit_cleanup(RERR_PROTOCOL);
587 first = flist->files[first_hlink_ndx];
588 file_length = F_LENGTH(first);
589 modtime = first->modtime;
595 if ((preserve_devices && IS_DEVICE(mode))
596 || (preserve_specials && IS_SPECIAL(mode))) {
597 uint32 *devp = F_RDEV_P(first);
598 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
600 if (preserve_links && S_ISLNK(mode))
601 linkname_len = strlen(F_SYMLINK(first)) + 1;
608 file_length = read_longint(f);
609 if (!(flags & XMIT_SAME_TIME))
610 modtime = (time_t)read_int(f);
611 if (!(flags & XMIT_SAME_MODE))
612 mode = from_wire_mode(read_int(f));
614 if (chmod_modes && !S_ISLNK(mode))
615 mode = tweak_mode(mode, chmod_modes);
617 if (preserve_uid && !(flags & XMIT_SAME_UID))
618 uid = (uid_t)read_int(f);
619 if (preserve_gid && !(flags & XMIT_SAME_GID))
620 gid = (gid_t)read_int(f);
622 if ((preserve_devices && IS_DEVICE(mode))
623 || (preserve_specials && IS_SPECIAL(mode))) {
624 if (protocol_version < 28) {
625 if (!(flags & XMIT_SAME_RDEV_pre28))
626 rdev = (dev_t)read_int(f);
629 if (!(flags & XMIT_SAME_RDEV_MAJOR))
630 rdev_major = read_int(f);
631 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
632 rdev_minor = read_byte(f);
634 rdev_minor = read_int(f);
635 rdev = MAKEDEV(rdev_major, rdev_minor);
637 extra_len += 2 * EXTRA_LEN;
639 } else if (protocol_version < 28)
640 rdev = MAKEDEV(0, 0);
643 if (preserve_links && S_ISLNK(mode)) {
644 linkname_len = read_int(f) + 1; /* count the '\0' */
645 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
646 rprintf(FERROR, "overflow: linkname_len=%d\n",
648 overflow_exit("recv_file_entry");
655 #ifdef SUPPORT_HARD_LINKS
657 if (preserve_hard_links) {
658 if (protocol_version < 28 && S_ISREG(mode))
659 flags |= XMIT_HLINKED;
660 if (flags & XMIT_HLINKED)
661 extra_len += EXTRA_LEN;
665 if (always_checksum && S_ISREG(mode))
666 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
668 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
669 extra_len += EXTRA_LEN;
671 #if EXTRA_ROUNDING > 0
672 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
673 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
676 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
678 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
680 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
682 file = (struct file_struct *)bp;
683 bp += FILE_STRUCT_LEN;
685 memcpy(bp, basename, basename_len);
686 bp += basename_len + linkname_len; /* skip space for symlink too */
688 #ifdef SUPPORT_HARD_LINKS
689 if (flags & XMIT_HLINKED)
690 file->flags |= FLAG_HLINKED;
692 file->modtime = modtime;
693 file->len32 = (uint32)file_length;
694 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
695 file->flags |= FLAG_LENGTH64;
696 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
705 file->dirname = lastdir = bp;
706 lastdir_len = dirname_len - 1;
707 memcpy(bp, dirname, dirname_len - 1);
710 lastdir_depth = count_dir_elements(lastdir);
711 F_DEPTH(file) = lastdir_depth + 1;
712 } else if (dirname) {
713 file->dirname = dirname; /* we're reusing lastname */
714 F_DEPTH(file) = lastdir_depth + 1;
719 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
721 if (flags & XMIT_TOP_DIR) {
722 in_del_hier = recurse;
723 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
724 if (relative_paths && del_hier_name_len > 2
725 && lastname[del_hier_name_len-1] == '.'
726 && lastname[del_hier_name_len-2] == '/')
727 del_hier_name_len -= 2;
728 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
729 } else if (in_del_hier) {
730 if (!relative_paths || !del_hier_name_len
731 || (l1 >= del_hier_name_len
732 && lastname[del_hier_name_len] == '/'))
733 file->flags |= FLAG_XFER_DIR;
739 if ((preserve_devices && IS_DEVICE(mode))
740 || (preserve_specials && IS_SPECIAL(mode))) {
741 uint32 *devp = F_RDEV_P(file);
742 DEV_MAJOR(devp) = major(rdev);
743 DEV_MINOR(devp) = minor(rdev);
748 bp = (char*)file->basename + basename_len;
749 if (first_hlink_ndx >= 0) {
750 struct file_struct *first = flist->files[first_hlink_ndx];
751 memcpy(bp, F_SYMLINK(first), linkname_len);
753 read_sbuf(f, bp, linkname_len - 1);
755 sanitize_path(bp, bp, "", lastdir_depth, NULL);
759 #ifdef SUPPORT_HARD_LINKS
760 if (preserve_hard_links && flags & XMIT_HLINKED) {
761 if (protocol_version >= 30) {
762 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
763 ? flist->count : first_hlink_ndx;
765 static int32 cnt = 0;
766 struct idev_node *np;
769 if (protocol_version < 26) {
773 if (!(flags & XMIT_SAME_DEV_pre30))
774 dev = read_longint(f);
775 ino = read_longint(f);
777 np = idev_node(dev, ino);
778 ndx = (int32)np->data - 1;
781 np->data = (void*)cnt;
783 F_HL_GNUM(file) = ndx;
788 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
790 bp = (char*)F_SUM(file);
792 /* Prior to 28, we get a useless set of nulls. */
795 if (first_hlink_ndx >= 0) {
796 struct file_struct *first = flist->files[first_hlink_ndx];
797 memcpy(bp, F_SUM(first), checksum_len);
799 read_buf(f, bp, checksum_len);
806 * Create a file_struct for a named file by reading its stat()
807 * information and performing extensive checks against global
810 * @return the new file, or NULL if there was an error or this file
811 * should be excluded.
813 * @todo There is a small optimization opportunity here to avoid
814 * stat()ing the file in some circumstances, which has a certain cost.
815 * We are called immediately after doing readdir(), and so we may
816 * already know the d_type of the file. We could for example avoid
817 * statting directories if we're not recursing, but this is not a very
818 * important case. Some systems may not have d_type.
820 struct file_struct *make_file(const char *fname, struct file_list *flist,
821 STRUCT_STAT *stp, int flags, int filter_level)
823 static char *lastdir;
824 static int lastdir_len = -1;
825 struct file_struct *file;
827 char thisname[MAXPATHLEN];
828 char linkname[MAXPATHLEN];
829 int alloc_len, basename_len, dirname_len, linkname_len;
830 int extra_len = file_extra_cnt * EXTRA_LEN;
831 char *basename, *dirname, *bp;
833 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
836 if (strlcpy(thisname, fname, sizeof thisname)
837 >= sizeof thisname - flist_dir_len) {
838 rprintf(FINFO, "skipping overly long name: %s\n", fname);
841 clean_fname(thisname, 0);
843 sanitize_path(thisname, thisname, "", 0, NULL);
845 if (stp && S_ISDIR(stp->st_mode)) {
846 st = *stp; /* Needed for "symlink/." with --relative. */
847 *linkname = '\0'; /* make IBM code checker happy */
848 } else if (readlink_stat(thisname, &st, linkname) != 0) {
849 int save_errno = errno;
850 /* See if file is excluded before reporting an error. */
851 if (filter_level != NO_FILTERS
852 && (is_excluded(thisname, 0, filter_level)
853 || is_excluded(thisname, 1, filter_level))) {
854 if (ignore_perishable && save_errno != ENOENT)
855 non_perishable_cnt++;
858 if (save_errno == ENOENT) {
860 /* Avoid "vanished" error if symlink points nowhere. */
861 if (copy_links && do_lstat(thisname, &st) == 0
862 && S_ISLNK(st.st_mode)) {
863 io_error |= IOERR_GENERAL;
864 rprintf(FERROR, "symlink has no referent: %s\n",
865 full_fname(thisname));
869 enum logcode c = am_daemon && protocol_version < 28
871 io_error |= IOERR_VANISHED;
872 rprintf(c, "file has vanished: %s\n",
873 full_fname(thisname));
876 io_error |= IOERR_GENERAL;
877 rsyserr(FERROR, save_errno, "readlink %s failed",
878 full_fname(thisname));
883 /* backup.c calls us with filter_level set to NO_FILTERS. */
884 if (filter_level == NO_FILTERS)
887 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
888 rprintf(FINFO, "skipping directory %s\n", thisname);
892 /* -x only affects directories because we need to avoid recursing
893 * into a mount-point directory, not to avoid copying a symlinked
894 * file if -L (or similar) was specified. */
895 if (one_file_system && st.st_dev != filesystem_dev
896 && S_ISDIR(st.st_mode)) {
897 if (one_file_system > 1) {
899 rprintf(FINFO, "skipping mount-point dir %s\n",
904 flags |= FLAG_MOUNT_DIR;
907 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
908 if (ignore_perishable)
909 non_perishable_cnt++;
913 if (lp_ignore_nonreadable(module_id)) {
915 if (!S_ISLNK(st.st_mode))
917 if (access(thisname, R_OK) != 0)
924 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
925 who_am_i(), thisname, filter_level);
928 if ((basename = strrchr(thisname, '/')) != NULL) {
929 dirname_len = ++basename - thisname; /* counts future '\0' */
930 if (lastdir_len == dirname_len - 1
931 && strncmp(thisname, lastdir, lastdir_len) == 0) {
933 dirname_len = 0; /* indicates no copy is needed */
941 basename_len = strlen(basename) + 1; /* count the '\0' */
944 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
949 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
950 extra_len += EXTRA_LEN;
952 #if EXTRA_ROUNDING > 0
953 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
954 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
957 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
960 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
962 if (!(bp = new_array(char, alloc_len)))
963 out_of_memory("make_file");
966 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
968 file = (struct file_struct *)bp;
969 bp += FILE_STRUCT_LEN;
971 memcpy(bp, basename, basename_len);
972 bp += basename_len + linkname_len; /* skip space for symlink too */
974 #ifdef SUPPORT_HARD_LINKS
975 if (preserve_hard_links && flist) {
976 if (protocol_version >= 28
977 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
978 : S_ISREG(st.st_mode)) {
986 #ifdef HAVE_STRUCT_STAT_ST_RDEV
987 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
988 tmp_rdev = st.st_rdev;
994 file->modtime = st.st_mtime;
995 file->len32 = (uint32)st.st_size;
996 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
997 file->flags |= FLAG_LENGTH64;
998 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1000 file->mode = st.st_mode;
1002 F_UID(file) = st.st_uid;
1004 F_GID(file) = st.st_gid;
1007 file->dirname = lastdir = bp;
1008 lastdir_len = dirname_len - 1;
1009 memcpy(bp, dirname, dirname_len - 1);
1013 file->dirname = dirname;
1015 #ifdef SUPPORT_LINKS
1017 bp = (char*)file->basename + basename_len;
1018 memcpy(bp, linkname, linkname_len);
1022 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1023 file_checksum(thisname, tmp_sum, st.st_size);
1025 F_ROOTDIR(file) = flist_dir;
1027 /* This code is only used by the receiver when it is building
1028 * a list of files for a delete pass. */
1029 if (keep_dirlinks && linkname_len && flist) {
1031 int save_mode = file->mode;
1032 file->mode = S_IFDIR; /* Find a directory with our name. */
1033 if (flist_find(the_file_list, file) >= 0
1034 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1035 file->modtime = st2.st_mtime;
1037 file->mode = st2.st_mode;
1039 F_UID(file) = st2.st_uid;
1041 F_GID(file) = st2.st_gid;
1043 file->mode = save_mode;
1046 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
1047 stats.total_size += st.st_size;
1049 if (basename_len == 0+1)
1055 /* Only called for temporary file_struct entries created by make_file(). */
1056 void unmake_file(struct file_struct *file)
1058 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1059 #if EXTRA_ROUNDING > 0
1060 if (extra_cnt & EXTRA_ROUNDING)
1061 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1063 free(REQ_EXTRA(file, extra_cnt));
1066 static struct file_struct *send_file_name(int f, struct file_list *flist,
1067 char *fname, STRUCT_STAT *stp,
1070 struct file_struct *file;
1072 file = make_file(fname, flist, stp, flags,
1073 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1077 if (chmod_modes && !S_ISLNK(file->mode))
1078 file->mode = tweak_mode(file->mode, chmod_modes);
1080 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1082 flist_expand(flist);
1083 flist->files[flist->count++] = file;
1085 send_file_entry(file, f, flist->count - 1);
1089 static void send_if_directory(int f, struct file_list *flist,
1090 struct file_struct *file,
1091 char *fbuf, unsigned int ol)
1093 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1095 if (S_ISDIR(file->mode)
1096 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1098 unsigned int len = strlen(fbuf);
1099 if (len > 1 && fbuf[len-1] == '/')
1101 if (len >= MAXPATHLEN - 1) {
1102 io_error |= IOERR_GENERAL;
1103 rprintf(FERROR, "skipping long-named directory: %s\n",
1107 save_filters = push_local_filters(fbuf, len);
1108 send_directory(f, flist, fbuf, len);
1109 pop_local_filters(save_filters);
1116 /* This function is normally called by the sender, but the receiving side also
1117 * calls it from get_dirlist() with f set to -1 so that we just construct the
1118 * file list in memory without sending it over the wire. Also, get_dirlist()
1119 * might call this with f set to -2, which also indicates that local filter
1120 * rules should be ignored. */
1121 static void send_directory(int f, struct file_list *flist, char *fbuf, int len)
1127 int start = flist->count;
1129 if (!(d = opendir(fbuf))) {
1130 io_error |= IOERR_GENERAL;
1131 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1136 if (len != 1 || *fbuf != '/')
1139 remainder = MAXPATHLEN - (p - fbuf);
1141 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1142 char *dname = d_name(di);
1143 if (dname[0] == '.' && (dname[1] == '\0'
1144 || (dname[1] == '.' && dname[2] == '\0')))
1146 if (strlcpy(p, dname, remainder) >= remainder) {
1147 io_error |= IOERR_GENERAL;
1149 "cannot send long-named file %s\n",
1154 send_file_name(f, flist, fbuf, NULL, 0);
1160 io_error |= IOERR_GENERAL;
1161 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1167 int i, end = flist->count - 1;
1168 /* send_if_directory() bumps flist->count, so use "end". */
1169 for (i = start; i <= end; i++)
1170 send_if_directory(f, flist, flist->files[i], fbuf, len);
1174 struct file_list *send_file_list(int f, int argc, char *argv[])
1178 char *p, *dir, olddir[sizeof curr_dir];
1179 char lastpath[MAXPATHLEN] = "";
1180 struct file_list *flist;
1181 struct timeval start_tv, end_tv;
1185 rprintf(FLOG, "building file list\n");
1186 if (show_filelist_p())
1187 start_filelist_progress("building file list");
1189 start_write = stats.total_written;
1190 gettimeofday(&start_tv, NULL);
1192 #ifdef SUPPORT_HARD_LINKS
1193 if (preserve_hard_links && protocol_version >= 30)
1197 flist = flist_new("send_file_list");
1199 io_start_buffering_out();
1200 if (filesfrom_fd >= 0) {
1201 if (argv[0] && !push_dir(argv[0], 0)) {
1202 rsyserr(FERROR, errno, "push_dir %s failed",
1203 full_fname(argv[0]));
1204 exit_cleanup(RERR_FILESELECT);
1210 char fbuf[MAXPATHLEN];
1215 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1217 sanitize_path(fbuf, fbuf, "", 0, NULL);
1221 strlcpy(fbuf, *argv++, MAXPATHLEN);
1223 sanitize_path(fbuf, fbuf, "", 0, NULL);
1227 if (relative_paths) {
1228 /* We clean up fbuf below. */
1230 } else if (!len || fbuf[len - 1] == '/') {
1231 if (len == 2 && fbuf[0] == '.') {
1232 /* Turn "./" into just "." rather than "./." */
1235 if (len + 1 >= MAXPATHLEN)
1236 overflow_exit("send_file_list");
1241 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1242 && (len == 2 || fbuf[len-3] == '/')) {
1243 if (len + 2 >= MAXPATHLEN)
1244 overflow_exit("send_file_list");
1250 is_dot_dir = fbuf[len-1] == '.'
1251 && (len == 1 || fbuf[len-2] == '/');
1254 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1255 io_error |= IOERR_GENERAL;
1256 rsyserr(FERROR, errno, "link_stat %s failed",
1261 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1262 rprintf(FINFO, "skipping directory %s\n", fbuf);
1269 if (!relative_paths) {
1270 p = strrchr(fbuf, '/');
1277 len -= p - fbuf + 1;
1282 if ((p = strstr(fbuf, "/./")) != NULL) {
1288 len -= p - fbuf + 3;
1292 /* Get rid of trailing "/" and "/.". */
1294 if (fn[len - 1] == '/') {
1296 if (!--len && !dir) {
1301 else if (len >= 2 && fn[len - 1] == '.'
1302 && fn[len - 2] == '/') {
1304 if (!(len -= 2) && !dir) {
1311 if (len == 1 && fn[0] == '/')
1314 /* Reject a ".." dir in the active part of the path. */
1315 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1316 if ((p[2] == '/' || p[2] == '\0')
1317 && (p == fn || p[-1] == '/')) {
1319 "found \"..\" dir in relative path: %s\n",
1321 exit_cleanup(RERR_SYNTAX);
1332 static const char *lastdir;
1333 static int lastdir_len;
1335 strlcpy(olddir, curr_dir, sizeof olddir);
1337 if (!push_dir(dir, 0)) {
1338 io_error |= IOERR_GENERAL;
1339 rsyserr(FERROR, errno, "push_dir %s failed",
1344 if (lastdir && strcmp(lastdir, dir) == 0) {
1345 flist_dir = lastdir;
1346 flist_dir_len = lastdir_len;
1348 flist_dir = lastdir = strdup(dir);
1349 flist_dir_len = lastdir_len = strlen(dir);
1354 memmove(fbuf, fn, len + 1);
1356 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1357 /* Send the implied directories at the start of the
1358 * source spec, so we get their permissions right. */
1359 char *lp = lastpath, *slash = fbuf;
1361 /* Skip any initial directories in our path that we
1362 * have in common with lastpath. */
1363 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1368 if (fn != p || (*lp && *lp != '/')) {
1369 int save_copy_links = copy_links;
1370 int save_xfer_dirs = xfer_dirs;
1371 copy_links |= copy_unsafe_links;
1373 while ((slash = strchr(slash+1, '/')) != 0) {
1375 send_file_name(f, flist, fbuf, NULL, 0);
1378 copy_links = save_copy_links;
1379 xfer_dirs = save_xfer_dirs;
1381 strlcpy(lastpath, fbuf, sizeof lastpath);
1386 if (one_file_system)
1387 filesystem_dev = st.st_dev;
1389 if (recurse || (xfer_dirs && is_dot_dir)) {
1390 struct file_struct *file;
1391 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1393 send_if_directory(f, flist, file, fbuf, len);
1395 send_file_name(f, flist, fbuf, &st, 0);
1400 if (!pop_dir(olddir)) {
1401 rsyserr(FERROR, errno, "pop_dir %s failed",
1402 full_fname(olddir));
1403 exit_cleanup(RERR_FILESELECT);
1408 gettimeofday(&end_tv, NULL);
1409 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1410 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1411 if (stats.flist_buildtime == 0)
1412 stats.flist_buildtime = 1;
1415 write_byte(f, 0); /* Indicate end of file list */
1417 #ifdef SUPPORT_HARD_LINKS
1418 if (preserve_hard_links && protocol_version >= 30)
1422 if (show_filelist_p())
1423 finish_filelist_progress(flist);
1425 gettimeofday(&end_tv, NULL);
1426 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1427 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1429 /* Sort the list without removing any duplicates. This allows the
1430 * receiving side to ask for any name they like, which gives us the
1431 * flexibility to change the way we unduplicate names in the future
1432 * without causing a compatibility problem with older versions. */
1433 clean_flist(flist, 0, 0);
1438 /* send the io_error flag */
1439 write_int(f, ignore_errors ? 0 : io_error);
1442 stats.flist_size = stats.total_written - start_write;
1443 stats.num_files = flist->count;
1446 output_flist(flist);
1449 rprintf(FINFO, "send_file_list done\n");
1454 struct file_list *recv_file_list(int f)
1456 struct file_list *flist;
1460 rprintf(FLOG, "receiving file list\n");
1461 if (show_filelist_p())
1462 start_filelist_progress("receiving file list");
1464 start_read = stats.total_read;
1466 flist = flist_new("recv_file_list");
1468 #ifdef SUPPORT_HARD_LINKS
1469 if (preserve_hard_links && protocol_version < 30)
1473 while ((flags = read_byte(f)) != 0) {
1474 struct file_struct *file;
1476 flist_expand(flist);
1478 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1479 flags |= read_byte(f) << 8;
1480 file = recv_file_entry(flist, flags, f);
1482 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1483 stats.total_size += F_LENGTH(file);
1485 flist->files[flist->count++] = file;
1487 maybe_emit_filelist_progress(flist->count);
1490 rprintf(FINFO, "recv_file_name(%s)\n",
1491 f_name(file, NULL));
1494 recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1497 rprintf(FINFO, "received %d names\n", flist->count);
1499 if (show_filelist_p())
1500 finish_filelist_progress(flist);
1502 clean_flist(flist, relative_paths, 1);
1505 recv_uid_list(f, flist);
1507 /* Recv the io_error flag */
1511 io_error |= read_int(f);
1515 output_flist(flist);
1519 for (i = 0; i < flist->count; i++)
1520 list_file_entry(flist->files[i]);
1524 rprintf(FINFO, "recv_file_list done\n");
1526 stats.flist_size = stats.total_read - start_read;
1527 stats.num_files = flist->count;
1532 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1534 return f_name_cmp(*file1, *file2);
1537 /* Search for an identically-named item in the file list. Note that the
1538 * items must agree in their directory-ness, or no match is returned. */
1539 int flist_find(struct file_list *flist, struct file_struct *f)
1541 int low = flist->low, high = flist->high;
1542 int diff, mid, mid_up;
1544 while (low <= high) {
1545 mid = (low + high) / 2;
1546 if (F_IS_ACTIVE(flist->files[mid]))
1549 /* Scan for the next non-empty entry using the cached
1550 * distance values. If the value isn't fully up-to-
1551 * date, update it. */
1552 mid_up = mid + F_DEPTH(flist->files[mid]);
1553 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1555 mid_up += F_DEPTH(flist->files[mid_up]);
1556 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1557 F_DEPTH(flist->files[mid]) = mid_up - mid;
1559 if (mid_up > high) {
1560 /* If there's nothing left above us, set high to
1561 * a non-empty entry below us and continue. */
1562 high = mid - (int)flist->files[mid]->len32;
1563 if (!F_IS_ACTIVE(flist->files[high])) {
1565 high -= (int)flist->files[high]->len32;
1566 } while (!F_IS_ACTIVE(flist->files[high]));
1567 flist->files[mid]->len32 = mid - high;
1572 diff = f_name_cmp(flist->files[mid_up], f);
1574 if (protocol_version < 29
1575 && S_ISDIR(flist->files[mid_up]->mode)
1576 != S_ISDIR(f->mode))
1589 * Free up any resources a file_struct has allocated
1590 * and clear the file.
1592 void clear_file(struct file_struct *file)
1594 /* The +1 zeros out the first char of the basename. */
1595 memset(file, 0, FILE_STRUCT_LEN + 1);
1596 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1597 * entry. Likewise for len32 in the opposite direction. We assume
1598 * that we're alone for now since flist_find() will adjust the counts
1599 * it runs into that aren't up-to-date. */
1600 file->len32 = F_DEPTH(file) = 1;
1603 /* Allocate a new file list. */
1604 struct file_list *flist_new(char *msg)
1606 struct file_list *flist;
1608 flist = new(struct file_list);
1612 memset(flist, 0, sizeof flist[0]);
1614 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1620 /* Free up all elements in a flist. */
1621 void flist_free(struct file_list *flist)
1623 pool_destroy(flist->file_pool);
1629 * This routine ensures we don't have any duplicate names in our file list.
1630 * duplicate names can cause corruption because of the pipelining
1632 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1634 char fbuf[MAXPATHLEN];
1639 if (flist->count == 0) {
1644 qsort(flist->files, flist->count,
1645 sizeof flist->files[0], (int (*)())file_compare);
1647 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1648 if (F_IS_ACTIVE(flist->files[i])) {
1653 flist->low = prev_i;
1654 while (++i < flist->count) {
1656 struct file_struct *file = flist->files[i];
1658 if (!F_IS_ACTIVE(file))
1660 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1662 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1663 int save_mode = file->mode;
1664 /* Make sure that this directory doesn't duplicate a
1665 * non-directory earlier in the list. */
1666 flist->high = prev_i;
1667 file->mode = S_IFREG;
1668 j = flist_find(flist, file);
1669 file->mode = save_mode;
1673 struct file_struct *fp = flist->files[j];
1675 /* If one is a dir and the other is not, we want to
1676 * keep the dir because it might have contents in the
1678 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1679 if (S_ISDIR(file->mode))
1685 if (verbose > 1 && !am_server) {
1687 "removing duplicate name %s from file list (%d)\n",
1688 f_name(file, fbuf), drop);
1690 /* Make sure we don't lose track of a user-specified
1692 flist->files[keep]->flags |= flist->files[drop]->flags
1693 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1695 clear_file(flist->files[drop]);
1698 if (flist->low == drop) {
1700 j < i && !F_IS_ACTIVE(flist->files[j]);
1709 flist->high = no_dups ? prev_i : flist->count - 1;
1712 /* We need to strip off the leading slashes for relative
1713 * paths, but this must be done _after_ the sorting phase. */
1714 for (i = flist->low; i <= flist->high; i++) {
1715 struct file_struct *file = flist->files[i];
1719 while (*file->dirname == '/')
1721 if (!*file->dirname)
1722 file->dirname = NULL;
1726 if (prune_empty_dirs && no_dups) {
1727 int j, prev_depth = 0;
1729 prev_i = 0; /* It's OK that this isn't really true. */
1731 for (i = flist->low; i <= flist->high; i++) {
1732 struct file_struct *fp, *file = flist->files[i];
1734 /* This temporarily abuses the F_DEPTH() value for a
1735 * directory that is in a chain that might get pruned.
1736 * We restore the old value if it gets a reprieve. */
1737 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
1738 /* Dump empty dirs when coming back down. */
1739 for (j = prev_depth; j >= F_DEPTH(file); j--) {
1740 fp = flist->files[prev_i];
1741 if (F_DEPTH(fp) >= 0)
1743 prev_i = -F_DEPTH(fp)-1;
1746 prev_depth = F_DEPTH(file);
1747 if (is_excluded(f_name(file, fbuf), 1,
1749 /* Keep dirs through this dir. */
1750 for (j = prev_depth-1; ; j--) {
1751 fp = flist->files[prev_i];
1752 if (F_DEPTH(fp) >= 0)
1754 prev_i = -F_DEPTH(fp)-1;
1758 F_DEPTH(file) = -prev_i-1;
1761 /* Keep dirs through this non-dir. */
1762 for (j = prev_depth; ; j--) {
1763 fp = flist->files[prev_i];
1764 if (F_DEPTH(fp) >= 0)
1766 prev_i = -F_DEPTH(fp)-1;
1771 /* Dump empty all remaining empty dirs. */
1773 struct file_struct *fp = flist->files[prev_i];
1774 if (F_DEPTH(fp) >= 0)
1776 prev_i = -F_DEPTH(fp)-1;
1780 for (i = flist->low; i <= flist->high; i++) {
1781 if (F_IS_ACTIVE(flist->files[i]))
1785 for (i = flist->high; i >= flist->low; i--) {
1786 if (F_IS_ACTIVE(flist->files[i]))
1793 static void output_flist(struct file_list *flist)
1795 char uidbuf[16], gidbuf[16], depthbuf[16];
1796 struct file_struct *file;
1797 const char *root, *dir, *slash, *name, *trail;
1798 const char *who = who_am_i();
1801 for (i = 0; i < flist->count; i++) {
1802 file = flist->files[i];
1803 if ((am_root || am_sender) && preserve_uid) {
1804 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1808 if (preserve_gid && F_GID(file) != GID_NONE) {
1809 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1814 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
1815 if (F_IS_ACTIVE(file)) {
1816 root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
1817 if ((dir = file->dirname) == NULL)
1821 name = file->basename;
1822 trail = S_ISDIR(file->mode) ? "/" : "";
1824 root = dir = slash = name = trail = "";
1825 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1826 who, i, root, dir, slash, name, trail, (int)file->mode,
1827 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1831 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1832 enum fnc_type { t_PATH, t_ITEM };
1834 /* Compare the names of two file_struct entities, similar to how strcmp()
1835 * would do if it were operating on the joined strings.
1837 * Some differences beginning with protocol_version 29: (1) directory names
1838 * are compared with an assumed trailing slash so that they compare in a
1839 * way that would cause them to sort immediately prior to any content they
1840 * may have; (2) a directory of any name compares after a non-directory of
1841 * any name at the same depth; (3) a directory with name "." compares prior
1842 * to anything else. These changes mean that a directory and a non-dir
1843 * with the same name will not compare as equal (protocol_version >= 29).
1845 * The dirname component can be an empty string, but the basename component
1846 * cannot (and never is in the current codebase). The basename component
1847 * may be NULL (for a removed item), in which case it is considered to be
1848 * after any existing item. */
1849 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1852 const uchar *c1, *c2;
1853 enum fnc_state state1, state2;
1854 enum fnc_type type1, type2;
1855 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1857 if (!f1 || !F_IS_ACTIVE(f1)) {
1858 if (!f2 || !F_IS_ACTIVE(f2))
1862 if (!f2 || !F_IS_ACTIVE(f2))
1865 c1 = (uchar*)f1->dirname;
1866 c2 = (uchar*)f2->dirname;
1870 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1871 c1 = (const uchar*)f1->basename;
1872 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1874 state1 = s_TRAILING;
1883 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1884 c2 = (const uchar*)f2->basename;
1885 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1887 state2 = s_TRAILING;
1897 return type1 == t_PATH ? 1 : -1;
1907 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1908 c1 = (const uchar*)f1->basename;
1909 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1911 state1 = s_TRAILING;
1917 state1 = s_TRAILING;
1918 if (type1 == t_PATH) {
1927 if (*c2 && type1 != type2)
1928 return type1 == t_PATH ? 1 : -1;
1937 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1938 c2 = (const uchar*)f2->basename;
1939 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1941 state2 = s_TRAILING;
1947 state2 = s_TRAILING;
1948 if (type2 == t_PATH) {
1960 return type1 == t_PATH ? 1 : -1;
1962 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1967 char *f_name_buf(void)
1969 static char names[5][MAXPATHLEN];
1970 static unsigned int n;
1972 n = (n + 1) % (sizeof names / sizeof names[0]);
1977 /* Return a copy of the full filename of a flist entry, using the indicated
1978 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1979 * done because we checked the size when creating the file_struct entry.
1981 char *f_name(struct file_struct *f, char *fbuf)
1983 if (!f || !F_IS_ACTIVE(f))
1987 fbuf = f_name_buf();
1990 int len = strlen(f->dirname);
1991 memcpy(fbuf, f->dirname, len);
1993 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1995 strlcpy(fbuf, f->basename, MAXPATHLEN);
2000 /* Do a non-recursive scan of the named directory, possibly ignoring all
2001 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2002 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2003 * buffer (the functions we call will append names onto the end, but the old
2004 * dir value will be restored on exit). */
2005 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2007 struct file_list *dirlist;
2008 char dirbuf[MAXPATHLEN];
2009 int save_recurse = recurse;
2010 int save_xfer_dirs = xfer_dirs;
2013 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2014 if (dlen >= MAXPATHLEN)
2019 dirlist = flist_new("get_dirlist");
2023 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
2024 xfer_dirs = save_xfer_dirs;
2025 recurse = save_recurse;
2027 flist_count_offset += dirlist->count;
2029 clean_flist(dirlist, 0, 0);
2032 output_flist(dirlist);