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.
32 extern int do_progress;
33 extern int always_checksum;
35 extern int ignore_errors;
36 extern int numeric_ids;
39 extern int filesfrom_fd;
40 extern int one_file_system;
41 extern int copy_dirlinks;
42 extern int keep_dirlinks;
43 extern int preserve_links;
44 extern int preserve_hard_links;
45 extern int preserve_devices;
46 extern int preserve_specials;
47 extern int preserve_uid;
48 extern int preserve_gid;
49 extern int relative_paths;
50 extern int implied_dirs;
51 extern int flist_extra_cnt;
52 extern int ignore_perishable;
53 extern int non_perishable_cnt;
54 extern int prune_empty_dirs;
55 extern int copy_links;
56 extern int copy_unsafe_links;
57 extern int protocol_version;
58 extern int sanitize_paths;
59 extern struct stats stats;
60 extern struct file_list *the_file_list;
61 extern alloc_pool_t hlink_pool;
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 struct idev tmp_idev;
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 unsigned short flags;
316 static time_t modtime;
320 static uint32 rdev_major;
323 static char lastname[MAXPATHLEN];
324 char fname[MAXPATHLEN];
325 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
372 if (tmp_idev.dev != 0) {
373 if (protocol_version >= 30) {
374 struct idev_node *np = idev_node(tmp_idev.dev, tmp_idev.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_idev.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_idev.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_idev.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_idev.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,
502 unsigned short flags, int f)
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 = (flist_extra_cnt - 1) * 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 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
673 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
675 memset(bp, 0, FILE_STRUCT_LEN + extra_len);
677 file = (struct file_struct *)bp;
678 bp += FILE_STRUCT_LEN;
680 memcpy(bp, basename, basename_len);
681 bp += basename_len + linkname_len; /* skip space for symlink too */
683 #ifdef SUPPORT_HARD_LINKS
684 if (flags & XMIT_HLINKED)
685 file->flags |= FLAG_HLINKED;
687 file->modtime = modtime;
688 file->len32 = (uint32)file_length;
689 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
690 file->flags |= FLAG_LENGTH64;
691 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
700 file->dirname = lastdir = bp;
701 lastdir_len = dirname_len - 1;
702 memcpy(bp, dirname, dirname_len - 1);
705 lastdir_depth = count_dir_elements(lastdir);
706 file->dir.depth = lastdir_depth + 1;
707 } else if (dirname) {
708 file->dirname = dirname; /* we're reusing lastname */
709 file->dir.depth = lastdir_depth + 1;
714 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
716 if (flags & XMIT_TOP_DIR) {
717 in_del_hier = recurse;
718 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
719 if (relative_paths && del_hier_name_len > 2
720 && lastname[del_hier_name_len-1] == '.'
721 && lastname[del_hier_name_len-2] == '/')
722 del_hier_name_len -= 2;
723 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
724 } else if (in_del_hier) {
725 if (!relative_paths || !del_hier_name_len
726 || (l1 >= del_hier_name_len
727 && lastname[del_hier_name_len] == '/'))
728 file->flags |= FLAG_XFER_DIR;
734 if ((preserve_devices && IS_DEVICE(mode))
735 || (preserve_specials && IS_SPECIAL(mode))) {
736 uint32 *devp = F_RDEV_P(file);
737 DEV_MAJOR(devp) = major(rdev);
738 DEV_MINOR(devp) = minor(rdev);
743 bp = (char*)file->basename + basename_len;
744 if (first_hlink_ndx >= 0) {
745 struct file_struct *first = flist->files[first_hlink_ndx];
746 memcpy(bp, F_SYMLINK(first), linkname_len);
748 read_sbuf(f, bp, linkname_len - 1);
750 sanitize_path(bp, bp, "", lastdir_depth, NULL);
754 #ifdef SUPPORT_HARD_LINKS
755 if (preserve_hard_links && flags & XMIT_HLINKED) {
756 if (protocol_version >= 30) {
757 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
758 ? flist->count : first_hlink_ndx;
760 struct idev *idevp = pool_talloc(hlink_pool, struct idev,
762 F_HL_IDEV(file) = idevp;
763 if (protocol_version < 26) {
764 idevp->dev = read_int(f);
765 idevp->ino = read_int(f);
767 if (!(flags & XMIT_SAME_DEV_pre30))
768 dev = read_longint(f);
770 idevp->ino = read_longint(f);
776 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
778 bp = (char*)F_SUM(file);
780 /* Prior to 28, we get a useless set of nulls. */
783 if (first_hlink_ndx >= 0) {
784 struct file_struct *first = flist->files[first_hlink_ndx];
785 memcpy(bp, F_SUM(first), checksum_len);
787 read_buf(f, bp, checksum_len);
794 * Create a file_struct for a named file by reading its stat()
795 * information and performing extensive checks against global
798 * @return the new file, or NULL if there was an error or this file
799 * should be excluded.
801 * @todo There is a small optimization opportunity here to avoid
802 * stat()ing the file in some circumstances, which has a certain cost.
803 * We are called immediately after doing readdir(), and so we may
804 * already know the d_type of the file. We could for example avoid
805 * statting directories if we're not recursing, but this is not a very
806 * important case. Some systems may not have d_type.
808 struct file_struct *make_file(const char *fname, struct file_list *flist,
809 STRUCT_STAT *stp, int flags, int filter_level)
811 static char *lastdir;
812 static int lastdir_len = -1;
813 struct file_struct *file;
815 char thisname[MAXPATHLEN];
816 char linkname[MAXPATHLEN];
817 int alloc_len, basename_len, dirname_len, linkname_len;
818 int extra_len = (flist_extra_cnt - 1) * EXTRA_LEN;
819 char *basename, *dirname, *bp;
821 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
824 if (strlcpy(thisname, fname, sizeof thisname)
825 >= sizeof thisname - flist_dir_len) {
826 rprintf(FINFO, "skipping overly long name: %s\n", fname);
829 clean_fname(thisname, 0);
831 sanitize_path(thisname, thisname, "", 0, NULL);
833 if (stp && S_ISDIR(stp->st_mode)) {
834 st = *stp; /* Needed for "symlink/." with --relative. */
835 *linkname = '\0'; /* make IBM code checker happy */
836 } else if (readlink_stat(thisname, &st, linkname) != 0) {
837 int save_errno = errno;
838 /* See if file is excluded before reporting an error. */
839 if (filter_level != NO_FILTERS
840 && (is_excluded(thisname, 0, filter_level)
841 || is_excluded(thisname, 1, filter_level))) {
842 if (ignore_perishable && save_errno != ENOENT)
843 non_perishable_cnt++;
846 if (save_errno == ENOENT) {
848 /* Avoid "vanished" error if symlink points nowhere. */
849 if (copy_links && do_lstat(thisname, &st) == 0
850 && S_ISLNK(st.st_mode)) {
851 io_error |= IOERR_GENERAL;
852 rprintf(FERROR, "symlink has no referent: %s\n",
853 full_fname(thisname));
857 enum logcode c = am_daemon && protocol_version < 28
859 io_error |= IOERR_VANISHED;
860 rprintf(c, "file has vanished: %s\n",
861 full_fname(thisname));
864 io_error |= IOERR_GENERAL;
865 rsyserr(FERROR, save_errno, "readlink %s failed",
866 full_fname(thisname));
871 /* backup.c calls us with filter_level set to NO_FILTERS. */
872 if (filter_level == NO_FILTERS)
875 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
876 rprintf(FINFO, "skipping directory %s\n", thisname);
880 /* -x only affects directories because we need to avoid recursing
881 * into a mount-point directory, not to avoid copying a symlinked
882 * file if -L (or similar) was specified. */
883 if (one_file_system && st.st_dev != filesystem_dev
884 && S_ISDIR(st.st_mode)) {
885 if (one_file_system > 1) {
887 rprintf(FINFO, "skipping mount-point dir %s\n",
892 flags |= FLAG_MOUNT_DIR;
895 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
896 if (ignore_perishable)
897 non_perishable_cnt++;
901 if (lp_ignore_nonreadable(module_id)) {
903 if (!S_ISLNK(st.st_mode))
905 if (access(thisname, R_OK) != 0)
912 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
913 who_am_i(), thisname, filter_level);
916 if ((basename = strrchr(thisname, '/')) != NULL) {
917 dirname_len = ++basename - thisname; /* counts future '\0' */
918 if (lastdir_len == dirname_len - 1
919 && strncmp(thisname, lastdir, lastdir_len) == 0) {
921 dirname_len = 0; /* indicates no copy is needed */
929 basename_len = strlen(basename) + 1; /* count the '\0' */
932 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
937 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
938 extra_len += EXTRA_LEN;
940 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
943 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
945 if (!(bp = new_array(char, alloc_len)))
946 out_of_memory("make_file");
949 memset(bp, 0, FILE_STRUCT_LEN + extra_len);
951 file = (struct file_struct *)bp;
952 bp += FILE_STRUCT_LEN;
954 memcpy(bp, basename, basename_len);
955 bp += basename_len + linkname_len; /* skip space for symlink too */
957 #ifdef SUPPORT_HARD_LINKS
958 if (preserve_hard_links && flist) {
959 if (protocol_version >= 28
960 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
961 : S_ISREG(st.st_mode)) {
962 tmp_idev.dev = st.st_dev;
963 tmp_idev.ino = st.st_ino;
965 tmp_idev.dev = tmp_idev.ino = 0;
969 #ifdef HAVE_STRUCT_STAT_ST_RDEV
970 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
971 tmp_rdev = st.st_rdev;
977 file->modtime = st.st_mtime;
978 file->len32 = (uint32)st.st_size;
979 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
980 file->flags |= FLAG_LENGTH64;
981 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
983 file->mode = st.st_mode;
985 F_UID(file) = st.st_uid;
987 F_GID(file) = st.st_gid;
990 file->dirname = lastdir = bp;
991 lastdir_len = dirname_len - 1;
992 memcpy(bp, dirname, dirname_len - 1);
996 file->dirname = dirname;
1000 bp = (char*)file->basename + basename_len;
1001 memcpy(bp, linkname, linkname_len);
1005 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1006 file_checksum(thisname, tmp_sum, st.st_size);
1008 file->dir.root = flist_dir;
1010 /* This code is only used by the receiver when it is building
1011 * a list of files for a delete pass. */
1012 if (keep_dirlinks && linkname_len && flist) {
1014 int save_mode = file->mode;
1015 file->mode = S_IFDIR; /* Find a directory with our name. */
1016 if (flist_find(the_file_list, file) >= 0
1017 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1018 file->modtime = st2.st_mtime;
1020 file->mode = st2.st_mode;
1022 F_UID(file) = st2.st_uid;
1024 F_GID(file) = st2.st_gid;
1026 file->mode = save_mode;
1029 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
1030 stats.total_size += st.st_size;
1032 if (basename_len == 0+1)
1038 /* Only called for temporary file_struct entries created by make_file(). */
1039 void unmake_file(struct file_struct *file)
1041 int extra_cnt = flist_extra_cnt - 1 + LEN64_BUMP(file);
1042 free(file->extras - extra_cnt);
1045 static struct file_struct *send_file_name(int f, struct file_list *flist,
1046 char *fname, STRUCT_STAT *stp,
1047 unsigned short flags)
1049 struct file_struct *file;
1051 file = make_file(fname, flist, stp, flags,
1052 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1056 if (chmod_modes && !S_ISLNK(file->mode))
1057 file->mode = tweak_mode(file->mode, chmod_modes);
1059 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1061 flist_expand(flist);
1062 flist->files[flist->count++] = file;
1064 send_file_entry(file, f, flist->count - 1);
1068 static void send_if_directory(int f, struct file_list *flist,
1069 struct file_struct *file,
1070 char *fbuf, unsigned int ol)
1072 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1074 if (S_ISDIR(file->mode)
1075 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1077 unsigned int len = strlen(fbuf);
1078 if (len > 1 && fbuf[len-1] == '/')
1080 if (len >= MAXPATHLEN - 1) {
1081 io_error |= IOERR_GENERAL;
1082 rprintf(FERROR, "skipping long-named directory: %s\n",
1086 save_filters = push_local_filters(fbuf, len);
1087 send_directory(f, flist, fbuf, len);
1088 pop_local_filters(save_filters);
1095 /* This function is normally called by the sender, but the receiving side also
1096 * calls it from get_dirlist() with f set to -1 so that we just construct the
1097 * file list in memory without sending it over the wire. Also, get_dirlist()
1098 * might call this with f set to -2, which also indicates that local filter
1099 * rules should be ignored. */
1100 static void send_directory(int f, struct file_list *flist, char *fbuf, int len)
1106 int start = flist->count;
1108 if (!(d = opendir(fbuf))) {
1109 io_error |= IOERR_GENERAL;
1110 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1115 if (len != 1 || *fbuf != '/')
1118 remainder = MAXPATHLEN - (p - fbuf);
1120 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1121 char *dname = d_name(di);
1122 if (dname[0] == '.' && (dname[1] == '\0'
1123 || (dname[1] == '.' && dname[2] == '\0')))
1125 if (strlcpy(p, dname, remainder) >= remainder) {
1126 io_error |= IOERR_GENERAL;
1128 "cannot send long-named file %s\n",
1133 send_file_name(f, flist, fbuf, NULL, 0);
1139 io_error |= IOERR_GENERAL;
1140 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1146 int i, end = flist->count - 1;
1147 /* send_if_directory() bumps flist->count, so use "end". */
1148 for (i = start; i <= end; i++)
1149 send_if_directory(f, flist, flist->files[i], fbuf, len);
1153 struct file_list *send_file_list(int f, int argc, char *argv[])
1157 char *p, *dir, olddir[sizeof curr_dir];
1158 char lastpath[MAXPATHLEN] = "";
1159 struct file_list *flist;
1160 struct timeval start_tv, end_tv;
1164 rprintf(FLOG, "building file list\n");
1165 if (show_filelist_p())
1166 start_filelist_progress("building file list");
1168 start_write = stats.total_written;
1169 gettimeofday(&start_tv, NULL);
1171 #ifdef SUPPORT_HARD_LINKS
1172 if (preserve_hard_links && protocol_version >= 30)
1176 flist = flist_new("send_file_list");
1178 io_start_buffering_out();
1179 if (filesfrom_fd >= 0) {
1180 if (argv[0] && !push_dir(argv[0], 0)) {
1181 rsyserr(FERROR, errno, "push_dir %s failed",
1182 full_fname(argv[0]));
1183 exit_cleanup(RERR_FILESELECT);
1189 char fbuf[MAXPATHLEN];
1194 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1196 sanitize_path(fbuf, fbuf, "", 0, NULL);
1200 strlcpy(fbuf, *argv++, MAXPATHLEN);
1202 sanitize_path(fbuf, fbuf, "", 0, NULL);
1206 if (relative_paths) {
1207 /* We clean up fbuf below. */
1209 } else if (!len || fbuf[len - 1] == '/') {
1210 if (len == 2 && fbuf[0] == '.') {
1211 /* Turn "./" into just "." rather than "./." */
1214 if (len + 1 >= MAXPATHLEN)
1215 overflow_exit("send_file_list");
1220 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1221 && (len == 2 || fbuf[len-3] == '/')) {
1222 if (len + 2 >= MAXPATHLEN)
1223 overflow_exit("send_file_list");
1229 is_dot_dir = fbuf[len-1] == '.'
1230 && (len == 1 || fbuf[len-2] == '/');
1233 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1234 io_error |= IOERR_GENERAL;
1235 rsyserr(FERROR, errno, "link_stat %s failed",
1240 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1241 rprintf(FINFO, "skipping directory %s\n", fbuf);
1248 if (!relative_paths) {
1249 p = strrchr(fbuf, '/');
1256 len -= p - fbuf + 1;
1261 if ((p = strstr(fbuf, "/./")) != NULL) {
1267 len -= p - fbuf + 3;
1271 /* Get rid of trailing "/" and "/.". */
1273 if (fn[len - 1] == '/') {
1275 if (!--len && !dir) {
1280 else if (len >= 2 && fn[len - 1] == '.'
1281 && fn[len - 2] == '/') {
1283 if (!(len -= 2) && !dir) {
1290 if (len == 1 && fn[0] == '/')
1293 /* Reject a ".." dir in the active part of the path. */
1294 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1295 if ((p[2] == '/' || p[2] == '\0')
1296 && (p == fn || p[-1] == '/')) {
1298 "found \"..\" dir in relative path: %s\n",
1300 exit_cleanup(RERR_SYNTAX);
1311 static const char *lastdir;
1312 static int lastdir_len;
1314 strlcpy(olddir, curr_dir, sizeof olddir);
1316 if (!push_dir(dir, 0)) {
1317 io_error |= IOERR_GENERAL;
1318 rsyserr(FERROR, errno, "push_dir %s failed",
1323 if (lastdir && strcmp(lastdir, dir) == 0) {
1324 flist_dir = lastdir;
1325 flist_dir_len = lastdir_len;
1327 flist_dir = lastdir = strdup(dir);
1328 flist_dir_len = lastdir_len = strlen(dir);
1333 memmove(fbuf, fn, len + 1);
1335 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1336 /* Send the implied directories at the start of the
1337 * source spec, so we get their permissions right. */
1338 char *lp = lastpath, *slash = fbuf;
1340 /* Skip any initial directories in our path that we
1341 * have in common with lastpath. */
1342 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1347 if (fn != p || (*lp && *lp != '/')) {
1348 int save_copy_links = copy_links;
1349 int save_xfer_dirs = xfer_dirs;
1350 copy_links |= copy_unsafe_links;
1352 while ((slash = strchr(slash+1, '/')) != 0) {
1354 send_file_name(f, flist, fbuf, NULL, 0);
1357 copy_links = save_copy_links;
1358 xfer_dirs = save_xfer_dirs;
1360 strlcpy(lastpath, fbuf, sizeof lastpath);
1365 if (one_file_system)
1366 filesystem_dev = st.st_dev;
1368 if (recurse || (xfer_dirs && is_dot_dir)) {
1369 struct file_struct *file;
1370 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1372 send_if_directory(f, flist, file, fbuf, len);
1374 send_file_name(f, flist, fbuf, &st, 0);
1379 if (!pop_dir(olddir)) {
1380 rsyserr(FERROR, errno, "pop_dir %s failed",
1381 full_fname(olddir));
1382 exit_cleanup(RERR_FILESELECT);
1387 gettimeofday(&end_tv, NULL);
1388 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1389 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1390 if (stats.flist_buildtime == 0)
1391 stats.flist_buildtime = 1;
1394 write_byte(f, 0); /* Indicate end of file list */
1396 #ifdef SUPPORT_HARD_LINKS
1397 if (preserve_hard_links && protocol_version >= 30)
1401 if (show_filelist_p())
1402 finish_filelist_progress(flist);
1404 gettimeofday(&end_tv, NULL);
1405 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1406 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1408 /* Sort the list without removing any duplicates. This allows the
1409 * receiving side to ask for any name they like, which gives us the
1410 * flexibility to change the way we unduplicate names in the future
1411 * without causing a compatibility problem with older versions. */
1412 clean_flist(flist, 0, 0);
1417 /* send the io_error flag */
1418 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1421 stats.flist_size = stats.total_written - start_write;
1422 stats.num_files = flist->count;
1425 output_flist(flist);
1428 rprintf(FINFO, "send_file_list done\n");
1433 struct file_list *recv_file_list(int f)
1435 struct file_list *flist;
1436 unsigned short flags;
1439 rprintf(FLOG, "receiving file list\n");
1440 if (show_filelist_p())
1441 start_filelist_progress("receiving file list");
1443 start_read = stats.total_read;
1445 flist = flist_new("recv_file_list");
1447 #ifdef SUPPORT_HARD_LINKS
1448 if (preserve_hard_links && protocol_version < 30)
1452 while ((flags = read_byte(f)) != 0) {
1453 struct file_struct *file;
1455 flist_expand(flist);
1457 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1458 flags |= read_byte(f) << 8;
1459 file = recv_file_entry(flist, flags, f);
1461 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1462 stats.total_size += F_LENGTH(file);
1464 flist->files[flist->count++] = file;
1466 maybe_emit_filelist_progress(flist->count);
1469 rprintf(FINFO, "recv_file_name(%s)\n",
1470 f_name(file, NULL));
1473 recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1476 rprintf(FINFO, "received %d names\n", flist->count);
1478 if (show_filelist_p())
1479 finish_filelist_progress(flist);
1481 clean_flist(flist, relative_paths, 1);
1484 recv_uid_list(f, flist);
1486 /* Recv the io_error flag */
1487 if (lp_ignore_errors(module_id) || ignore_errors)
1490 io_error |= read_int(f);
1494 output_flist(flist);
1498 for (i = 0; i < flist->count; i++)
1499 list_file_entry(flist->files[i]);
1503 rprintf(FINFO, "recv_file_list done\n");
1505 stats.flist_size = stats.total_read - start_read;
1506 stats.num_files = flist->count;
1511 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1513 return f_name_cmp(*file1, *file2);
1516 /* Search for an identically-named item in the file list. Note that the
1517 * items must agree in their directory-ness, or no match is returned. */
1518 int flist_find(struct file_list *flist, struct file_struct *f)
1520 int low = flist->low, high = flist->high;
1521 int diff, mid, mid_up;
1523 while (low <= high) {
1524 mid = (low + high) / 2;
1525 if (F_IS_ACTIVE(flist->files[mid]))
1528 /* Scan for the next non-empty entry using the cached
1529 * distance values. If the value isn't fully up-to-
1530 * date, update it. */
1531 mid_up = mid + flist->files[mid]->dir.depth;
1532 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1534 mid_up += flist->files[mid_up]->dir.depth;
1535 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1536 flist->files[mid]->dir.depth = mid_up - mid;
1538 if (mid_up > high) {
1539 /* If there's nothing left above us, set high to
1540 * a non-empty entry below us and continue. */
1541 high = mid - (int)flist->files[mid]->len32;
1542 if (!F_IS_ACTIVE(flist->files[high])) {
1544 high -= (int)flist->files[high]->len32;
1545 } while (!F_IS_ACTIVE(flist->files[high]));
1546 flist->files[mid]->len32 = mid - high;
1551 diff = f_name_cmp(flist->files[mid_up], f);
1553 if (protocol_version < 29
1554 && S_ISDIR(flist->files[mid_up]->mode)
1555 != S_ISDIR(f->mode))
1568 * Free up any resources a file_struct has allocated
1569 * and clear the file.
1571 void clear_file(struct file_struct *file)
1573 memset((char*)file + EXTRA_LEN, 0, FILE_STRUCT_LEN - EXTRA_LEN + 1);
1574 /* In an empty entry, dir.depth is an offset to the next non-empty
1575 * entry. Likewise for len32 in the opposite direction. We assume
1576 * that we're alone for now since flist_find() will adjust the counts
1577 * it runs into that aren't up-to-date. */
1578 file->len32 = file->dir.depth = 1;
1581 /* Allocate a new file list. */
1582 struct file_list *flist_new(char *msg)
1584 struct file_list *flist;
1586 flist = new(struct file_list);
1590 memset(flist, 0, sizeof flist[0]);
1592 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1598 /* Free up all elements in a flist. */
1599 void flist_free(struct file_list *flist)
1601 pool_destroy(flist->file_pool);
1607 * This routine ensures we don't have any duplicate names in our file list.
1608 * duplicate names can cause corruption because of the pipelining
1610 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1612 char fbuf[MAXPATHLEN];
1617 if (flist->count == 0) {
1622 qsort(flist->files, flist->count,
1623 sizeof flist->files[0], (int (*)())file_compare);
1625 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1626 if (F_IS_ACTIVE(flist->files[i])) {
1631 flist->low = prev_i;
1632 while (++i < flist->count) {
1634 struct file_struct *file = flist->files[i];
1636 if (!F_IS_ACTIVE(file))
1638 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1640 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1641 int save_mode = file->mode;
1642 /* Make sure that this directory doesn't duplicate a
1643 * non-directory earlier in the list. */
1644 flist->high = prev_i;
1645 file->mode = S_IFREG;
1646 j = flist_find(flist, file);
1647 file->mode = save_mode;
1651 struct file_struct *fp = flist->files[j];
1653 /* If one is a dir and the other is not, we want to
1654 * keep the dir because it might have contents in the
1656 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1657 if (S_ISDIR(file->mode))
1663 if (verbose > 1 && !am_server) {
1665 "removing duplicate name %s from file list (%d)\n",
1666 f_name(file, fbuf), drop);
1668 /* Make sure we don't lose track of a user-specified
1670 flist->files[keep]->flags |= flist->files[drop]->flags
1671 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1673 clear_file(flist->files[drop]);
1676 if (flist->low == drop) {
1678 j < i && !F_IS_ACTIVE(flist->files[j]);
1687 flist->high = no_dups ? prev_i : flist->count - 1;
1690 /* We need to strip off the leading slashes for relative
1691 * paths, but this must be done _after_ the sorting phase. */
1692 for (i = flist->low; i <= flist->high; i++) {
1693 struct file_struct *file = flist->files[i];
1697 while (*file->dirname == '/')
1699 if (!*file->dirname)
1700 file->dirname = NULL;
1704 if (prune_empty_dirs && no_dups) {
1705 int j, prev_depth = 0;
1707 prev_i = 0; /* It's OK that this isn't really true. */
1709 for (i = flist->low; i <= flist->high; i++) {
1710 struct file_struct *fp, *file = flist->files[i];
1712 /* This temporarily abuses the dir.depth value for a
1713 * directory that is in a chain that might get pruned.
1714 * We restore the old value if it gets a reprieve. */
1715 if (S_ISDIR(file->mode) && file->dir.depth) {
1716 /* Dump empty dirs when coming back down. */
1717 for (j = prev_depth; j >= file->dir.depth; j--) {
1718 fp = flist->files[prev_i];
1719 if (fp->dir.depth >= 0)
1721 prev_i = -fp->dir.depth-1;
1724 prev_depth = file->dir.depth;
1725 if (is_excluded(f_name(file, fbuf), 1,
1727 /* Keep dirs through this dir. */
1728 for (j = prev_depth-1; ; j--) {
1729 fp = flist->files[prev_i];
1730 if (fp->dir.depth >= 0)
1732 prev_i = -fp->dir.depth-1;
1736 file->dir.depth = -prev_i-1;
1739 /* Keep dirs through this non-dir. */
1740 for (j = prev_depth; ; j--) {
1741 fp = flist->files[prev_i];
1742 if (fp->dir.depth >= 0)
1744 prev_i = -fp->dir.depth-1;
1749 /* Dump empty all remaining empty dirs. */
1751 struct file_struct *fp = flist->files[prev_i];
1752 if (fp->dir.depth >= 0)
1754 prev_i = -fp->dir.depth-1;
1758 for (i = flist->low; i <= flist->high; i++) {
1759 if (F_IS_ACTIVE(flist->files[i]))
1763 for (i = flist->high; i >= flist->low; i--) {
1764 if (F_IS_ACTIVE(flist->files[i]))
1771 static void output_flist(struct file_list *flist)
1773 char uidbuf[16], gidbuf[16], depthbuf[16];
1774 struct file_struct *file;
1775 const char *dir, *slash, *name, *trail;
1776 const char *who = who_am_i();
1779 for (i = 0; i < flist->count; i++) {
1780 file = flist->files[i];
1781 if ((am_root || am_sender) && preserve_uid) {
1782 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1786 if (preserve_gid && F_GID(file) != GID_NONE) {
1787 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1792 snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
1793 if (F_IS_ACTIVE(file)) {
1794 if ((dir = file->dirname) == NULL)
1798 name = file->basename;
1799 trail = S_ISDIR(file->mode) ? "/" : "";
1801 dir = slash = name = trail = "";
1802 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1803 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1804 dir, slash, name, trail, (int)file->mode,
1805 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1809 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1810 enum fnc_type { t_PATH, t_ITEM };
1812 /* Compare the names of two file_struct entities, similar to how strcmp()
1813 * would do if it were operating on the joined strings.
1815 * Some differences beginning with protocol_version 29: (1) directory names
1816 * are compared with an assumed trailing slash so that they compare in a
1817 * way that would cause them to sort immediately prior to any content they
1818 * may have; (2) a directory of any name compares after a non-directory of
1819 * any name at the same depth; (3) a directory with name "." compares prior
1820 * to anything else. These changes mean that a directory and a non-dir
1821 * with the same name will not compare as equal (protocol_version >= 29).
1823 * The dirname component can be an empty string, but the basename component
1824 * cannot (and never is in the current codebase). The basename component
1825 * may be NULL (for a removed item), in which case it is considered to be
1826 * after any existing item. */
1827 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1830 const uchar *c1, *c2;
1831 enum fnc_state state1, state2;
1832 enum fnc_type type1, type2;
1833 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1835 if (!f1 || !F_IS_ACTIVE(f1)) {
1836 if (!f2 || !F_IS_ACTIVE(f2))
1840 if (!f2 || !F_IS_ACTIVE(f2))
1843 c1 = (uchar*)f1->dirname;
1844 c2 = (uchar*)f2->dirname;
1848 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1849 c1 = (const uchar*)f1->basename;
1850 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1852 state1 = s_TRAILING;
1861 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1862 c2 = (const uchar*)f2->basename;
1863 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1865 state2 = s_TRAILING;
1875 return type1 == t_PATH ? 1 : -1;
1885 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1886 c1 = (const uchar*)f1->basename;
1887 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1889 state1 = s_TRAILING;
1895 state1 = s_TRAILING;
1896 if (type1 == t_PATH) {
1905 if (*c2 && type1 != type2)
1906 return type1 == t_PATH ? 1 : -1;
1915 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1916 c2 = (const uchar*)f2->basename;
1917 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1919 state2 = s_TRAILING;
1925 state2 = s_TRAILING;
1926 if (type2 == t_PATH) {
1938 return type1 == t_PATH ? 1 : -1;
1940 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1945 char *f_name_buf(void)
1947 static char names[5][MAXPATHLEN];
1948 static unsigned int n;
1950 n = (n + 1) % (sizeof names / sizeof names[0]);
1955 /* Return a copy of the full filename of a flist entry, using the indicated
1956 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1957 * done because we checked the size when creating the file_struct entry.
1959 char *f_name(struct file_struct *f, char *fbuf)
1961 if (!f || !F_IS_ACTIVE(f))
1965 fbuf = f_name_buf();
1968 int len = strlen(f->dirname);
1969 memcpy(fbuf, f->dirname, len);
1971 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1973 strlcpy(fbuf, f->basename, MAXPATHLEN);
1978 /* Do a non-recursive scan of the named directory, possibly ignoring all
1979 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1980 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1981 * buffer (the functions we call will append names onto the end, but the old
1982 * dir value will be restored on exit). */
1983 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
1985 struct file_list *dirlist;
1986 char dirbuf[MAXPATHLEN];
1987 int save_recurse = recurse;
1988 int save_xfer_dirs = xfer_dirs;
1991 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1992 if (dlen >= MAXPATHLEN)
1997 dirlist = flist_new("get_dirlist");
2001 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
2002 xfer_dirs = save_xfer_dirs;
2003 recurse = save_recurse;
2005 flist_count_offset += dirlist->count;
2007 clean_flist(dirlist, 0, 0);
2010 output_flist(dirlist);