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;
62 extern char curr_dir[MAXPATHLEN];
64 extern struct chmod_mode_struct *chmod_modes;
66 extern struct filter_list_struct filter_list;
67 extern struct filter_list_struct server_filter_list;
71 dev_t filesystem_dev; /* used to implement -x */
73 /* The tmp_* vars are used as a cache area by make_file() to store data
74 * that the sender doesn't need to remember in its file list. The data
75 * will survive just long enough to be used by send_file_entry(). */
76 static dev_t tmp_rdev;
77 static struct idev tmp_idev;
78 static char tmp_sum[MD4_SUM_LENGTH];
80 static char empty_sum[MD4_SUM_LENGTH];
81 static int flist_count_offset;
83 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
84 static void output_flist(struct file_list *flist);
89 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
90 FILE_STRUCT_LEN, EXTRA_LEN);
92 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
95 static int show_filelist_p(void)
97 return verbose && xfer_dirs && !am_server;
100 static void start_filelist_progress(char *kind)
102 rprintf(FCLIENT, "%s ... ", kind);
103 if (verbose > 1 || do_progress)
104 rprintf(FCLIENT, "\n");
108 static void emit_filelist_progress(int count)
110 rprintf(FCLIENT, " %d files...\r", count);
113 static void maybe_emit_filelist_progress(int count)
115 if (do_progress && show_filelist_p() && (count % 100) == 0)
116 emit_filelist_progress(count);
119 static void finish_filelist_progress(const struct file_list *flist)
122 /* This overwrites the progress line */
123 rprintf(FINFO, "%d file%sto consider\n",
124 flist->count, flist->count == 1 ? " " : "s ");
126 rprintf(FINFO, "done\n");
129 void show_flist_stats(void)
134 static void list_file_entry(struct file_struct *f)
136 char permbuf[PERMSTRING_SIZE];
139 if (!F_IS_ACTIVE(f)) {
140 /* this can happen if duplicate names were removed */
144 permstring(permbuf, f->mode);
148 if (preserve_links && S_ISLNK(f->mode)) {
149 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
150 permbuf, len, timestring(f->modtime),
151 f_name(f, NULL), F_SYMLINK(f));
155 rprintf(FINFO, "%s %11.0f %s %s\n",
156 permbuf, len, timestring(f->modtime),
161 /* Stat either a symlink or its referent, depending on the settings of
162 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
164 * If path is the name of a symlink, then the linkbuf buffer (which must hold
165 * MAXPATHLEN chars) will be set to the symlink's target string.
167 * The stat structure pointed to by stp will contain information about the
168 * link or the referent as appropriate, if they exist. */
169 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
172 if (link_stat(path, stp, copy_dirlinks) < 0)
174 if (S_ISLNK(stp->st_mode)) {
175 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
178 linkbuf[llen] = '\0';
179 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
181 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
184 return do_stat(path, stp);
189 return do_stat(path, stp);
193 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
197 return do_stat(path, stp);
198 if (do_lstat(path, stp) < 0)
200 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
202 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
207 return do_stat(path, stp);
211 /* This function is used to check if a file should be included/excluded
212 * from the list of files based on its name and type etc. The value of
213 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
214 static int is_excluded(char *fname, int is_dir, int filter_level)
216 #if 0 /* This currently never happens, so avoid a useless compare. */
217 if (filter_level == NO_FILTERS)
221 /* never exclude '.', even if somebody does --exclude '*' */
222 if (fname[0] == '.' && !fname[1])
224 /* Handle the -R version of the '.' dir. */
225 if (fname[0] == '/') {
226 int len = strlen(fname);
227 if (fname[len-1] == '.' && fname[len-2] == '/')
231 if (server_filter_list.head
232 && check_filter(&server_filter_list, fname, is_dir) < 0)
234 if (filter_level != ALL_FILTERS)
237 && check_filter(&filter_list, fname, is_dir) < 0)
242 static int to_wire_mode(mode_t mode)
245 #if _S_IFLNK != 0120000
247 return (mode & ~(_S_IFMT)) | 0120000;
253 static mode_t from_wire_mode(int mode)
255 #if _S_IFLNK != 0120000
256 if ((mode & (_S_IFMT)) == 0120000)
257 return (mode & ~(_S_IFMT)) | _S_IFLNK;
262 static void send_directory(int f, struct file_list *flist,
263 char *fbuf, int len);
265 static const char *flist_dir;
266 static int flist_dir_len;
270 * Make sure @p flist is big enough to hold at least @p flist->count
273 void flist_expand(struct file_list *flist)
275 struct file_struct **new_ptr;
277 if (flist->count < flist->malloced)
280 if (flist->malloced < FLIST_START)
281 flist->malloced = FLIST_START;
282 else if (flist->malloced >= FLIST_LINEAR)
283 flist->malloced += FLIST_LINEAR;
285 flist->malloced *= 2;
288 * In case count jumped or we are starting the list
289 * with a known size just set it.
291 if (flist->malloced < flist->count)
292 flist->malloced = flist->count;
294 new_ptr = realloc_array(flist->files, struct file_struct *,
297 if (verbose >= 2 && flist->malloced != FLIST_START) {
298 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
300 (double)sizeof flist->files[0] * flist->malloced,
301 (new_ptr == flist->files) ? " not" : "");
304 flist->files = new_ptr;
307 out_of_memory("flist_expand");
310 static void send_file_entry(struct file_struct *file, int f)
312 unsigned short flags;
313 static time_t modtime;
317 static uint32 rdev_major;
320 static char lastname[MAXPATHLEN];
321 char fname[MAXPATHLEN];
329 modtime = 0, mode = 0;
330 dev = 0, rdev = MAKEDEV(0, 0);
339 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
341 if (file->mode == mode)
342 flags |= XMIT_SAME_MODE;
345 if ((preserve_devices && IS_DEVICE(mode))
346 || (preserve_specials && IS_SPECIAL(mode))) {
347 if (protocol_version < 28) {
348 if (tmp_rdev == rdev)
349 flags |= XMIT_SAME_RDEV_pre28;
354 if ((uint32)major(rdev) == rdev_major)
355 flags |= XMIT_SAME_RDEV_MAJOR;
357 rdev_major = major(rdev);
358 if ((uint32)minor(rdev) <= 0xFFu)
359 flags |= XMIT_RDEV_MINOR_IS_SMALL;
361 } else if (protocol_version < 28)
362 rdev = MAKEDEV(0, 0);
364 if (F_UID(file) == uid)
365 flags |= XMIT_SAME_UID;
370 if (F_GID(file) == gid)
371 flags |= XMIT_SAME_GID;
375 if (file->modtime == modtime)
376 flags |= XMIT_SAME_TIME;
378 modtime = file->modtime;
380 #ifdef SUPPORT_HARD_LINKS
381 if (tmp_idev.dev != 0) {
382 if (tmp_idev.dev == dev) {
383 if (protocol_version >= 28)
384 flags |= XMIT_SAME_DEV;
387 flags |= XMIT_HAS_IDEV_DATA;
392 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
394 l2 = strlen(fname+l1);
397 flags |= XMIT_SAME_NAME;
399 flags |= XMIT_LONG_NAME;
401 /* We must make sure we don't send a zero flag byte or the
402 * other end will terminate the flist transfer. Note that
403 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
404 * it's harmless way to add a bit to the first flag byte. */
405 if (protocol_version >= 28) {
406 if (!flags && !S_ISDIR(mode))
407 flags |= XMIT_TOP_DIR;
408 if ((flags & 0xFF00) || !flags) {
409 flags |= XMIT_EXTENDED_FLAGS;
410 write_shortint(f, flags);
412 write_byte(f, flags);
415 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
416 write_byte(f, flags);
418 if (flags & XMIT_SAME_NAME)
420 if (flags & XMIT_LONG_NAME)
424 write_buf(f, fname + l1, l2);
426 write_longint(f, F_LENGTH(file));
427 if (!(flags & XMIT_SAME_TIME))
428 write_int(f, modtime);
429 if (!(flags & XMIT_SAME_MODE))
430 write_int(f, to_wire_mode(mode));
431 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
436 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
441 if ((preserve_devices && IS_DEVICE(mode))
442 || (preserve_specials && IS_SPECIAL(mode))) {
443 if (protocol_version < 28) {
444 if (!(flags & XMIT_SAME_RDEV_pre28))
445 write_int(f, (int)rdev);
447 if (!(flags & XMIT_SAME_RDEV_MAJOR))
448 write_int(f, major(rdev));
449 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
450 write_byte(f, minor(rdev));
452 write_int(f, minor(rdev));
457 if (preserve_links && S_ISLNK(mode)) {
458 const char *sl = F_SYMLINK(file);
459 int len = strlen(sl);
461 write_buf(f, sl, len);
465 #ifdef SUPPORT_HARD_LINKS
466 if (tmp_idev.dev != 0) {
467 if (protocol_version < 26) {
468 /* 32-bit dev_t and ino_t */
469 write_int(f, (int32)dev);
470 write_int(f, (int32)tmp_idev.ino);
472 /* 64-bit dev_t and ino_t */
473 if (!(flags & XMIT_SAME_DEV))
474 write_longint(f, dev);
475 write_longint(f, tmp_idev.ino);
480 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
485 /* Prior to 28, we sent a useless set of nulls. */
488 write_buf(f, sum, checksum_len);
491 strlcpy(lastname, fname, MAXPATHLEN);
494 static struct file_struct *recv_file_entry(struct file_list *flist,
495 unsigned short flags, int f)
497 static time_t modtime;
501 static uint32 rdev_major;
504 static char lastname[MAXPATHLEN], *lastdir;
505 static int lastdir_depth, lastdir_len = -1;
506 static unsigned int del_hier_name_len = 0;
507 static int in_del_hier = 0;
508 char thisname[MAXPATHLEN];
509 unsigned int l1 = 0, l2 = 0;
510 int alloc_len, basename_len, dirname_len, linkname_len;
511 int extra_len = (flist_extra_cnt - 1) * EXTRA_LEN;
513 char *basename, *dirname, *bp;
514 struct file_struct *file;
517 modtime = 0, mode = 0;
518 dev = 0, rdev = MAKEDEV(0, 0);
527 if (flags & XMIT_SAME_NAME)
530 if (flags & XMIT_LONG_NAME)
535 if (l2 >= MAXPATHLEN - l1) {
537 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
538 flags, l1, l2, lastname, who_am_i());
539 overflow_exit("recv_file_entry");
542 strlcpy(thisname, lastname, l1 + 1);
543 read_sbuf(f, &thisname[l1], l2);
544 thisname[l1 + l2] = 0;
546 strlcpy(lastname, thisname, MAXPATHLEN);
548 clean_fname(thisname, 0);
551 sanitize_path(thisname, thisname, "", 0, NULL);
553 if ((basename = strrchr(thisname, '/')) != NULL) {
554 dirname_len = ++basename - thisname; /* counts future '\0' */
555 if (lastdir_len == dirname_len - 1
556 && strncmp(thisname, lastdir, lastdir_len) == 0) {
558 dirname_len = 0; /* indicates no copy is needed */
566 basename_len = strlen(basename) + 1; /* count the '\0' */
568 file_length = read_longint(f);
569 if (!(flags & XMIT_SAME_TIME))
570 modtime = (time_t)read_int(f);
571 if (!(flags & XMIT_SAME_MODE))
572 mode = from_wire_mode(read_int(f));
574 if (chmod_modes && !S_ISLNK(mode))
575 mode = tweak_mode(mode, chmod_modes);
577 if (preserve_uid && !(flags & XMIT_SAME_UID))
578 uid = (uid_t)read_int(f);
579 if (preserve_gid && !(flags & XMIT_SAME_GID))
580 gid = (gid_t)read_int(f);
582 if ((preserve_devices && IS_DEVICE(mode))
583 || (preserve_specials && IS_SPECIAL(mode))) {
584 if (protocol_version < 28) {
585 if (!(flags & XMIT_SAME_RDEV_pre28))
586 rdev = (dev_t)read_int(f);
589 if (!(flags & XMIT_SAME_RDEV_MAJOR))
590 rdev_major = read_int(f);
591 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
592 rdev_minor = read_byte(f);
594 rdev_minor = read_int(f);
595 rdev = MAKEDEV(rdev_major, rdev_minor);
597 extra_len += 2 * EXTRA_LEN;
599 } else if (protocol_version < 28)
600 rdev = MAKEDEV(0, 0);
603 if (preserve_links && S_ISLNK(mode)) {
604 linkname_len = read_int(f) + 1; /* count the '\0' */
605 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
606 rprintf(FERROR, "overflow: linkname_len=%d\n",
608 overflow_exit("recv_file_entry");
615 #ifdef SUPPORT_HARD_LINKS
616 if (preserve_hard_links) {
617 if (protocol_version < 28 && S_ISREG(mode))
618 flags |= XMIT_HAS_IDEV_DATA;
619 if (flags & XMIT_HAS_IDEV_DATA)
620 extra_len += EXTRA_LEN;
624 if (always_checksum && S_ISREG(mode))
625 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
627 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
628 extra_len += EXTRA_LEN;
630 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
632 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
634 memset(bp, 0, FILE_STRUCT_LEN + extra_len);
636 file = (struct file_struct *)bp;
637 bp += FILE_STRUCT_LEN;
639 memcpy(bp, basename, basename_len);
640 bp += basename_len + linkname_len; /* skip space for symlink too */
642 #ifdef SUPPORT_HARD_LINKS
643 if (flags & XMIT_HAS_IDEV_DATA)
644 file->flags |= FLAG_HLINKED;
646 file->modtime = modtime;
647 file->len32 = file_length;
648 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
649 file->flags |= FLAG_LENGTH64;
650 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
659 file->dirname = lastdir = bp;
660 lastdir_len = dirname_len - 1;
661 memcpy(bp, dirname, dirname_len - 1);
664 lastdir_depth = count_dir_elements(lastdir);
665 file->dir.depth = lastdir_depth + 1;
666 } else if (dirname) {
667 file->dirname = dirname; /* we're reusing lastname */
668 file->dir.depth = lastdir_depth + 1;
673 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
675 if (flags & XMIT_TOP_DIR) {
676 in_del_hier = recurse;
677 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
678 if (relative_paths && del_hier_name_len > 2
679 && lastname[del_hier_name_len-1] == '.'
680 && lastname[del_hier_name_len-2] == '/')
681 del_hier_name_len -= 2;
682 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
683 } else if (in_del_hier) {
684 if (!relative_paths || !del_hier_name_len
685 || (l1 >= del_hier_name_len
686 && lastname[del_hier_name_len] == '/'))
687 file->flags |= FLAG_XFER_DIR;
693 if ((preserve_devices && IS_DEVICE(mode))
694 || (preserve_specials && IS_SPECIAL(mode))) {
695 uint32 *devp = F_RDEV_P(file);
696 DEV_MAJOR(devp) = major(rdev);
697 DEV_MINOR(devp) = minor(rdev);
702 bp = (char*)F_BASENAME(file) + basename_len;
703 read_sbuf(f, bp, linkname_len - 1);
705 sanitize_path(bp, bp, "", lastdir_depth, NULL);
709 #ifdef SUPPORT_HARD_LINKS
710 if (preserve_hard_links && flags & XMIT_HAS_IDEV_DATA) {
711 struct idev *idevp = pool_talloc(flist->hlink_pool, struct idev,
713 F_HL_IDEV(file) = idevp;
714 if (protocol_version < 26) {
715 idevp->dev = read_int(f);
716 idevp->ino = read_int(f);
718 if (!(flags & XMIT_SAME_DEV))
719 dev = read_longint(f);
721 idevp->ino = read_longint(f);
726 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
728 bp = (char*)F_SUM(file);
730 /* Prior to 28, we get a useless set of nulls. */
733 read_buf(f, bp, checksum_len);
740 * Create a file_struct for a named file by reading its stat()
741 * information and performing extensive checks against global
744 * @return the new file, or NULL if there was an error or this file
745 * should be excluded.
747 * @todo There is a small optimization opportunity here to avoid
748 * stat()ing the file in some circumstances, which has a certain cost.
749 * We are called immediately after doing readdir(), and so we may
750 * already know the d_type of the file. We could for example avoid
751 * statting directories if we're not recursing, but this is not a very
752 * important case. Some systems may not have d_type.
754 struct file_struct *make_file(const char *fname, struct file_list *flist,
755 STRUCT_STAT *stp, int flags, int filter_level)
757 static char *lastdir;
758 static int lastdir_len = -1;
759 struct file_struct *file;
761 char thisname[MAXPATHLEN];
762 char linkname[MAXPATHLEN];
763 int alloc_len, basename_len, dirname_len, linkname_len;
764 int extra_len = (flist_extra_cnt - 1) * EXTRA_LEN;
765 char *basename, *dirname, *bp;
767 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
770 if (strlcpy(thisname, fname, sizeof thisname)
771 >= sizeof thisname - flist_dir_len) {
772 rprintf(FINFO, "skipping overly long name: %s\n", fname);
775 clean_fname(thisname, 0);
777 sanitize_path(thisname, thisname, "", 0, NULL);
779 if (stp && S_ISDIR(stp->st_mode)) {
780 st = *stp; /* Needed for "symlink/." with --relative. */
781 *linkname = '\0'; /* make IBM code checker happy */
782 } else if (readlink_stat(thisname, &st, linkname) != 0) {
783 int save_errno = errno;
784 /* See if file is excluded before reporting an error. */
785 if (filter_level != NO_FILTERS
786 && (is_excluded(thisname, 0, filter_level)
787 || is_excluded(thisname, 1, filter_level))) {
788 if (ignore_perishable && save_errno != ENOENT)
789 non_perishable_cnt++;
792 if (save_errno == ENOENT) {
794 /* Avoid "vanished" error if symlink points nowhere. */
795 if (copy_links && do_lstat(thisname, &st) == 0
796 && S_ISLNK(st.st_mode)) {
797 io_error |= IOERR_GENERAL;
798 rprintf(FERROR, "symlink has no referent: %s\n",
799 full_fname(thisname));
803 enum logcode c = am_daemon && protocol_version < 28
805 io_error |= IOERR_VANISHED;
806 rprintf(c, "file has vanished: %s\n",
807 full_fname(thisname));
810 io_error |= IOERR_GENERAL;
811 rsyserr(FERROR, save_errno, "readlink %s failed",
812 full_fname(thisname));
817 /* backup.c calls us with filter_level set to NO_FILTERS. */
818 if (filter_level == NO_FILTERS)
821 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
822 rprintf(FINFO, "skipping directory %s\n", thisname);
826 /* -x only affects directories because we need to avoid recursing
827 * into a mount-point directory, not to avoid copying a symlinked
828 * file if -L (or similar) was specified. */
829 if (one_file_system && st.st_dev != filesystem_dev
830 && S_ISDIR(st.st_mode)) {
831 if (one_file_system > 1) {
833 rprintf(FINFO, "skipping mount-point dir %s\n",
838 flags |= FLAG_MOUNT_DIR;
841 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
842 if (ignore_perishable)
843 non_perishable_cnt++;
847 if (lp_ignore_nonreadable(module_id)) {
849 if (!S_ISLNK(st.st_mode))
851 if (access(thisname, R_OK) != 0)
858 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
859 who_am_i(), thisname, filter_level);
862 if ((basename = strrchr(thisname, '/')) != NULL) {
863 dirname_len = ++basename - thisname; /* counts future '\0' */
864 if (lastdir_len == dirname_len - 1
865 && strncmp(thisname, lastdir, lastdir_len) == 0) {
867 dirname_len = 0; /* indicates no copy is needed */
875 basename_len = strlen(basename) + 1; /* count the '\0' */
878 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
883 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
884 extra_len += EXTRA_LEN;
886 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
889 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
891 if (!(bp = new_array(char, alloc_len)))
892 out_of_memory("make_file");
895 memset(bp, 0, FILE_STRUCT_LEN + extra_len);
897 file = (struct file_struct *)bp;
898 bp += FILE_STRUCT_LEN;
900 memcpy(bp, basename, basename_len);
901 bp += basename_len + linkname_len; /* skip space for symlink too */
903 #ifdef SUPPORT_HARD_LINKS
904 if (preserve_hard_links && flist) {
905 if (protocol_version >= 28
906 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
907 : S_ISREG(st.st_mode)) {
908 tmp_idev.dev = st.st_dev;
909 tmp_idev.ino = st.st_ino;
911 tmp_idev.dev = tmp_idev.ino = 0;
915 #ifdef HAVE_STRUCT_STAT_ST_RDEV
916 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
917 tmp_rdev = st.st_rdev;
923 file->modtime = st.st_mtime;
924 file->len32 = st.st_size;
925 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
926 file->flags |= FLAG_LENGTH64;
927 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
929 file->mode = st.st_mode;
931 F_UID(file) = st.st_uid;
933 F_GID(file) = st.st_gid;
936 file->dirname = lastdir = bp;
937 lastdir_len = dirname_len - 1;
938 memcpy(bp, dirname, dirname_len - 1);
942 file->dirname = dirname;
946 bp = (char*)F_BASENAME(file) + basename_len;
947 memcpy(bp, linkname, linkname_len);
951 if (always_checksum && am_sender && S_ISREG(st.st_mode))
952 file_checksum(thisname, tmp_sum, st.st_size);
954 file->dir.root = flist_dir;
956 /* This code is only used by the receiver when it is building
957 * a list of files for a delete pass. */
958 if (keep_dirlinks && linkname_len && flist) {
960 int save_mode = file->mode;
961 file->mode = S_IFDIR; /* Find a directory with our name. */
962 if (flist_find(the_file_list, file) >= 0
963 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
964 file->modtime = st2.st_mtime;
966 file->mode = st2.st_mode;
968 F_UID(file) = st2.st_uid;
970 F_GID(file) = st2.st_gid;
972 file->mode = save_mode;
975 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
976 stats.total_size += st.st_size;
978 if (basename_len == 0+1)
984 /* Only called for temporary file_struct entries created by make_file(). */
985 void unmake_file(struct file_struct *file)
987 int extra_cnt = flist_extra_cnt - 1 + LEN64_BUMP(file);
988 free(file->extras - extra_cnt);
991 static struct file_struct *send_file_name(int f, struct file_list *flist,
992 char *fname, STRUCT_STAT *stp,
993 unsigned short flags)
995 struct file_struct *file;
997 file = make_file(fname, flist, stp, flags,
998 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1002 if (chmod_modes && !S_ISLNK(file->mode))
1003 file->mode = tweak_mode(file->mode, chmod_modes);
1005 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1007 flist_expand(flist);
1008 flist->files[flist->count++] = file;
1009 send_file_entry(file, f);
1013 static void send_if_directory(int f, struct file_list *flist,
1014 struct file_struct *file,
1015 char *fbuf, unsigned int ol)
1017 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1019 if (S_ISDIR(file->mode)
1020 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1022 unsigned int len = strlen(fbuf);
1023 if (len > 1 && fbuf[len-1] == '/')
1025 if (len >= MAXPATHLEN - 1) {
1026 io_error |= IOERR_GENERAL;
1027 rprintf(FERROR, "skipping long-named directory: %s\n",
1031 save_filters = push_local_filters(fbuf, len);
1032 send_directory(f, flist, fbuf, len);
1033 pop_local_filters(save_filters);
1040 /* This function is normally called by the sender, but the receiving side also
1041 * calls it from get_dirlist() with f set to -1 so that we just construct the
1042 * file list in memory without sending it over the wire. Also, get_dirlist()
1043 * might call this with f set to -2, which also indicates that local filter
1044 * rules should be ignored. */
1045 static void send_directory(int f, struct file_list *flist,
1046 char *fbuf, int len)
1052 int start = flist->count;
1054 if (!(d = opendir(fbuf))) {
1055 io_error |= IOERR_GENERAL;
1056 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1061 if (len != 1 || *fbuf != '/')
1064 remainder = MAXPATHLEN - (p - fbuf);
1066 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1067 char *dname = d_name(di);
1068 if (dname[0] == '.' && (dname[1] == '\0'
1069 || (dname[1] == '.' && dname[2] == '\0')))
1071 if (strlcpy(p, dname, remainder) >= remainder) {
1072 io_error |= IOERR_GENERAL;
1074 "cannot send long-named file %s\n",
1079 send_file_name(f, flist, fbuf, NULL, 0);
1085 io_error |= IOERR_GENERAL;
1086 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1092 int i, end = flist->count - 1;
1093 for (i = start; i <= end; i++)
1094 send_if_directory(f, flist, flist->files[i], fbuf, len);
1098 struct file_list *send_file_list(int f, int argc, char *argv[])
1102 char *p, *dir, olddir[sizeof curr_dir];
1103 char lastpath[MAXPATHLEN] = "";
1104 struct file_list *flist;
1105 struct timeval start_tv, end_tv;
1109 rprintf(FLOG, "building file list\n");
1110 if (show_filelist_p())
1111 start_filelist_progress("building file list");
1113 start_write = stats.total_written;
1114 gettimeofday(&start_tv, NULL);
1116 flist = flist_new(0, "send_file_list");
1118 io_start_buffering_out();
1119 if (filesfrom_fd >= 0) {
1120 if (argv[0] && !push_dir(argv[0], 0)) {
1121 rsyserr(FERROR, errno, "push_dir %s failed",
1122 full_fname(argv[0]));
1123 exit_cleanup(RERR_FILESELECT);
1129 char fbuf[MAXPATHLEN];
1134 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1136 sanitize_path(fbuf, fbuf, "", 0, NULL);
1140 strlcpy(fbuf, *argv++, MAXPATHLEN);
1142 sanitize_path(fbuf, fbuf, "", 0, NULL);
1146 if (relative_paths) {
1147 /* We clean up fbuf below. */
1149 } else if (!len || fbuf[len - 1] == '/') {
1150 if (len == 2 && fbuf[0] == '.') {
1151 /* Turn "./" into just "." rather than "./." */
1154 if (len + 1 >= MAXPATHLEN)
1155 overflow_exit("send_file_list");
1160 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1161 && (len == 2 || fbuf[len-3] == '/')) {
1162 if (len + 2 >= MAXPATHLEN)
1163 overflow_exit("send_file_list");
1169 is_dot_dir = fbuf[len-1] == '.'
1170 && (len == 1 || fbuf[len-2] == '/');
1173 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1174 io_error |= IOERR_GENERAL;
1175 rsyserr(FERROR, errno, "link_stat %s failed",
1180 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1181 rprintf(FINFO, "skipping directory %s\n", fbuf);
1188 if (!relative_paths) {
1189 p = strrchr(fbuf, '/');
1196 len -= p - fbuf + 1;
1201 if ((p = strstr(fbuf, "/./")) != NULL) {
1207 len -= p - fbuf + 3;
1211 /* Get rid of trailing "/" and "/.". */
1213 if (fn[len - 1] == '/') {
1215 if (!--len && !dir) {
1220 else if (len >= 2 && fn[len - 1] == '.'
1221 && fn[len - 2] == '/') {
1223 if (!(len -= 2) && !dir) {
1230 if (len == 1 && fn[0] == '/')
1233 /* Reject a ".." dir in the active part of the path. */
1234 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1235 if ((p[2] == '/' || p[2] == '\0')
1236 && (p == fn || p[-1] == '/')) {
1238 "found \"..\" dir in relative path: %s\n",
1240 exit_cleanup(RERR_SYNTAX);
1251 static const char *lastdir;
1252 static int lastdir_len;
1254 strlcpy(olddir, curr_dir, sizeof olddir);
1256 if (!push_dir(dir, 0)) {
1257 io_error |= IOERR_GENERAL;
1258 rsyserr(FERROR, errno, "push_dir %s failed",
1263 if (lastdir && strcmp(lastdir, dir) == 0) {
1264 flist_dir = lastdir;
1265 flist_dir_len = lastdir_len;
1267 flist_dir = lastdir = strdup(dir);
1268 flist_dir_len = lastdir_len = strlen(dir);
1273 memmove(fbuf, fn, len + 1);
1275 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1276 /* Send the implied directories at the start of the
1277 * source spec, so we get their permissions right. */
1278 char *lp = lastpath, *slash = fbuf;
1280 /* Skip any initial directories in our path that we
1281 * have in common with lastpath. */
1282 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1287 if (fn != p || (*lp && *lp != '/')) {
1288 int save_copy_links = copy_links;
1289 int save_xfer_dirs = xfer_dirs;
1290 copy_links |= copy_unsafe_links;
1292 while ((slash = strchr(slash+1, '/')) != 0) {
1294 send_file_name(f, flist, fbuf, NULL, 0);
1297 copy_links = save_copy_links;
1298 xfer_dirs = save_xfer_dirs;
1300 strlcpy(lastpath, fbuf, sizeof lastpath);
1305 if (one_file_system)
1306 filesystem_dev = st.st_dev;
1308 if (recurse || (xfer_dirs && is_dot_dir)) {
1309 struct file_struct *file;
1310 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1312 send_if_directory(f, flist, file, fbuf, len);
1314 send_file_name(f, flist, fbuf, &st, 0);
1319 if (!pop_dir(olddir)) {
1320 rsyserr(FERROR, errno, "pop_dir %s failed",
1321 full_fname(olddir));
1322 exit_cleanup(RERR_FILESELECT);
1327 gettimeofday(&end_tv, NULL);
1328 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1329 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1330 if (stats.flist_buildtime == 0)
1331 stats.flist_buildtime = 1;
1334 send_file_entry(NULL, f);
1336 if (show_filelist_p())
1337 finish_filelist_progress(flist);
1339 gettimeofday(&end_tv, NULL);
1340 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1341 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1343 /* Sort the list without removing any duplicates. This allows the
1344 * receiving side to ask for any name they like, which gives us the
1345 * flexibility to change the way we unduplicate names in the future
1346 * without causing a compatibility problem with older versions. */
1347 clean_flist(flist, 0, 0);
1352 /* send the io_error flag */
1353 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1356 stats.flist_size = stats.total_written - start_write;
1357 stats.num_files = flist->count;
1360 output_flist(flist);
1363 rprintf(FINFO, "send_file_list done\n");
1368 struct file_list *recv_file_list(int f)
1370 struct file_list *flist;
1371 unsigned short flags;
1374 rprintf(FLOG, "receiving file list\n");
1375 if (show_filelist_p())
1376 start_filelist_progress("receiving file list");
1378 start_read = stats.total_read;
1380 flist = flist_new(WITH_HLINK, "recv_file_list");
1383 while ((flags = read_byte(f)) != 0) {
1384 struct file_struct *file;
1386 flist_expand(flist);
1388 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1389 flags |= read_byte(f) << 8;
1390 file = recv_file_entry(flist, flags, f);
1392 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1393 stats.total_size += F_LENGTH(file);
1395 flist->files[flist->count++] = file;
1397 maybe_emit_filelist_progress(flist->count);
1400 rprintf(FINFO, "recv_file_name(%s)\n",
1401 f_name(file, NULL));
1404 recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1407 rprintf(FINFO, "received %d names\n", flist->count);
1409 if (show_filelist_p())
1410 finish_filelist_progress(flist);
1412 clean_flist(flist, relative_paths, 1);
1415 recv_uid_list(f, flist);
1417 /* Recv the io_error flag */
1418 if (lp_ignore_errors(module_id) || ignore_errors)
1421 io_error |= read_int(f);
1425 output_flist(flist);
1429 for (i = 0; i < flist->count; i++)
1430 list_file_entry(flist->files[i]);
1434 rprintf(FINFO, "recv_file_list done\n");
1436 stats.flist_size = stats.total_read - start_read;
1437 stats.num_files = flist->count;
1442 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1444 return f_name_cmp(*file1, *file2);
1447 /* Search for an identically-named item in the file list. Note that the
1448 * items must agree in their directory-ness, or no match is returned. */
1449 int flist_find(struct file_list *flist, struct file_struct *f)
1451 int low = flist->low, high = flist->high;
1452 int diff, mid, mid_up;
1454 while (low <= high) {
1455 mid = (low + high) / 2;
1456 if (F_IS_ACTIVE(flist->files[mid]))
1459 /* Scan for the next non-empty entry using the cached
1460 * distance values. If the value isn't fully up-to-
1461 * date, update it. */
1462 mid_up = mid + flist->files[mid]->dir.depth;
1463 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1465 mid_up += flist->files[mid_up]->dir.depth;
1466 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1467 flist->files[mid]->dir.depth = mid_up - mid;
1469 if (mid_up > high) {
1470 /* If there's nothing left above us, set high to
1471 * a non-empty entry below us and continue. */
1472 high = mid - (int)flist->files[mid]->len32;
1473 if (!F_IS_ACTIVE(flist->files[high])) {
1475 high -= (int)flist->files[high]->len32;
1476 } while (!F_IS_ACTIVE(flist->files[high]));
1477 flist->files[mid]->len32 = mid - high;
1482 diff = f_name_cmp(flist->files[mid_up], f);
1484 if (protocol_version < 29
1485 && S_ISDIR(flist->files[mid_up]->mode)
1486 != S_ISDIR(f->mode))
1499 * Free up any resources a file_struct has allocated
1500 * and clear the file.
1502 void clear_file(struct file_struct *file)
1504 memset((char*)file + EXTRA_LEN, 0, FILE_STRUCT_LEN - EXTRA_LEN + 1);
1505 /* In an empty entry, dir.depth is an offset to the next non-empty
1506 * entry. Likewise for len32 in the opposite direction. We assume
1507 * that we're alone for now since flist_find() will adjust the counts
1508 * it runs into that aren't up-to-date. */
1509 file->len32 = file->dir.depth = 1;
1512 /* Allocate a new file list. */
1513 struct file_list *flist_new(int with_hlink, char *msg)
1515 struct file_list *flist;
1517 flist = new(struct file_list);
1521 memset(flist, 0, sizeof flist[0]);
1523 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1526 #ifdef SUPPORT_HARD_LINKS
1527 if (with_hlink && preserve_hard_links) {
1528 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1529 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1537 /* Free up all elements in a flist. */
1538 void flist_free(struct file_list *flist)
1540 pool_destroy(flist->file_pool);
1541 pool_destroy(flist->hlink_pool);
1547 * This routine ensures we don't have any duplicate names in our file list.
1548 * duplicate names can cause corruption because of the pipelining
1550 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1552 char fbuf[MAXPATHLEN];
1557 if (flist->count == 0) {
1562 qsort(flist->files, flist->count,
1563 sizeof flist->files[0], (int (*)())file_compare);
1565 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1566 if (F_IS_ACTIVE(flist->files[i])) {
1571 flist->low = prev_i;
1572 while (++i < flist->count) {
1574 struct file_struct *file = flist->files[i];
1576 if (!F_IS_ACTIVE(file))
1578 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1580 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1581 int save_mode = file->mode;
1582 /* Make sure that this directory doesn't duplicate a
1583 * non-directory earlier in the list. */
1584 flist->high = prev_i;
1585 file->mode = S_IFREG;
1586 j = flist_find(flist, file);
1587 file->mode = save_mode;
1591 struct file_struct *fp = flist->files[j];
1593 /* If one is a dir and the other is not, we want to
1594 * keep the dir because it might have contents in the
1596 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1597 if (S_ISDIR(file->mode))
1603 if (verbose > 1 && !am_server) {
1605 "removing duplicate name %s from file list (%d)\n",
1606 f_name(file, fbuf), drop);
1608 /* Make sure we don't lose track of a user-specified
1610 flist->files[keep]->flags |= flist->files[drop]->flags
1611 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1613 clear_file(flist->files[drop]);
1616 if (flist->low == drop) {
1618 j < i && !F_IS_ACTIVE(flist->files[j]);
1627 flist->high = no_dups ? prev_i : flist->count - 1;
1630 /* We need to strip off the leading slashes for relative
1631 * paths, but this must be done _after_ the sorting phase. */
1632 for (i = flist->low; i <= flist->high; i++) {
1633 struct file_struct *file = flist->files[i];
1637 while (*file->dirname == '/')
1639 if (!*file->dirname)
1640 file->dirname = NULL;
1644 if (prune_empty_dirs && no_dups) {
1645 int j, prev_depth = 0;
1647 prev_i = 0; /* It's OK that this isn't really true. */
1649 for (i = flist->low; i <= flist->high; i++) {
1650 struct file_struct *fp, *file = flist->files[i];
1652 /* This temporarily abuses the dir.depth value for a
1653 * directory that is in a chain that might get pruned.
1654 * We restore the old value if it gets a reprieve. */
1655 if (S_ISDIR(file->mode) && file->dir.depth) {
1656 /* Dump empty dirs when coming back down. */
1657 for (j = prev_depth; j >= file->dir.depth; j--) {
1658 fp = flist->files[prev_i];
1659 if (fp->dir.depth >= 0)
1661 prev_i = -fp->dir.depth-1;
1664 prev_depth = file->dir.depth;
1665 if (is_excluded(f_name(file, fbuf), 1,
1667 /* Keep dirs through this dir. */
1668 for (j = prev_depth-1; ; j--) {
1669 fp = flist->files[prev_i];
1670 if (fp->dir.depth >= 0)
1672 prev_i = -fp->dir.depth-1;
1676 file->dir.depth = -prev_i-1;
1679 /* Keep dirs through this non-dir. */
1680 for (j = prev_depth; ; j--) {
1681 fp = flist->files[prev_i];
1682 if (fp->dir.depth >= 0)
1684 prev_i = -fp->dir.depth-1;
1689 /* Dump empty all remaining empty dirs. */
1691 struct file_struct *fp = flist->files[prev_i];
1692 if (fp->dir.depth >= 0)
1694 prev_i = -fp->dir.depth-1;
1698 for (i = flist->low; i <= flist->high; i++) {
1699 if (F_IS_ACTIVE(flist->files[i]))
1703 for (i = flist->high; i >= flist->low; i--) {
1704 if (F_IS_ACTIVE(flist->files[i]))
1711 static void output_flist(struct file_list *flist)
1713 char uidbuf[16], gidbuf[16], depthbuf[16];
1714 struct file_struct *file;
1715 const char *dir, *slash, *name, *trail;
1716 const char *who = who_am_i();
1719 for (i = 0; i < flist->count; i++) {
1720 file = flist->files[i];
1721 if ((am_root || am_sender) && preserve_uid) {
1722 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1726 if (preserve_gid && F_GID(file) != GID_NONE) {
1727 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1732 snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
1733 if (F_IS_ACTIVE(file)) {
1734 if ((dir = file->dirname) == NULL)
1738 name = F_BASENAME(file);
1739 trail = S_ISDIR(file->mode) ? "/" : "";
1741 dir = slash = name = trail = "";
1742 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1743 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1744 dir, slash, name, trail, (int)file->mode,
1745 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1749 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1750 enum fnc_type { t_PATH, t_ITEM };
1752 /* Compare the names of two file_struct entities, similar to how strcmp()
1753 * would do if it were operating on the joined strings.
1755 * Some differences beginning with protocol_version 29: (1) directory names
1756 * are compared with an assumed trailing slash so that they compare in a
1757 * way that would cause them to sort immediately prior to any content they
1758 * may have; (2) a directory of any name compares after a non-directory of
1759 * any name at the same depth; (3) a directory with name "." compares prior
1760 * to anything else. These changes mean that a directory and a non-dir
1761 * with the same name will not compare as equal (protocol_version >= 29).
1763 * The dirname component can be an empty string, but the basename component
1764 * cannot (and never is in the current codebase). The basename component
1765 * may be NULL (for a removed item), in which case it is considered to be
1766 * after any existing item. */
1767 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1770 const uchar *c1, *c2;
1771 enum fnc_state state1, state2;
1772 enum fnc_type type1, type2;
1773 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1775 if (!f1 || !F_IS_ACTIVE(f1)) {
1776 if (!f2 || !F_IS_ACTIVE(f2))
1780 if (!f2 || !F_IS_ACTIVE(f2))
1783 c1 = (uchar*)f1->dirname;
1784 c2 = (uchar*)f2->dirname;
1788 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1789 c1 = (uchar*)F_BASENAME(f1);
1790 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1792 state1 = s_TRAILING;
1801 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1802 c2 = (uchar*)F_BASENAME(f2);
1803 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1805 state2 = s_TRAILING;
1815 return type1 == t_PATH ? 1 : -1;
1825 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1826 c1 = (uchar*)F_BASENAME(f1);
1827 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1829 state1 = s_TRAILING;
1835 state1 = s_TRAILING;
1836 if (type1 == t_PATH) {
1845 if (*c2 && type1 != type2)
1846 return type1 == t_PATH ? 1 : -1;
1855 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1856 c2 = (uchar*)F_BASENAME(f2);
1857 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1859 state2 = s_TRAILING;
1865 state2 = s_TRAILING;
1866 if (type2 == t_PATH) {
1878 return type1 == t_PATH ? 1 : -1;
1880 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1885 char *f_name_buf(void)
1887 static char names[5][MAXPATHLEN];
1888 static unsigned int n;
1890 n = (n + 1) % (sizeof names / sizeof names[0]);
1895 /* Return a copy of the full filename of a flist entry, using the indicated
1896 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1897 * done because we checked the size when creating the file_struct entry.
1899 char *f_name(struct file_struct *f, char *fbuf)
1901 if (!f || !F_IS_ACTIVE(f))
1905 fbuf = f_name_buf();
1908 int len = strlen(f->dirname);
1909 memcpy(fbuf, f->dirname, len);
1911 strlcpy(fbuf + len + 1, F_BASENAME(f), MAXPATHLEN - (len + 1));
1913 strlcpy(fbuf, F_BASENAME(f), MAXPATHLEN);
1918 /* Do a non-recursive scan of the named directory, possibly ignoring all
1919 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1920 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1921 * buffer (the functions we call will append names onto the end, but the old
1922 * dir value will be restored on exit). */
1923 struct file_list *get_dirlist(char *dirname, int dlen,
1924 int ignore_filter_rules)
1926 struct file_list *dirlist;
1927 char dirbuf[MAXPATHLEN];
1928 int save_recurse = recurse;
1929 int save_xfer_dirs = xfer_dirs;
1932 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1933 if (dlen >= MAXPATHLEN)
1938 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1942 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1943 xfer_dirs = save_xfer_dirs;
1944 recurse = save_recurse;
1946 flist_count_offset += dirlist->count;
1948 clean_flist(dirlist, 0, 0);
1951 output_flist(dirlist);