2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int do_progress;
36 extern int always_checksum;
38 extern int ignore_errors;
39 extern int numeric_ids;
43 extern int filesfrom_fd;
44 extern int one_file_system;
45 extern int copy_dirlinks;
46 extern int keep_dirlinks;
47 extern int preserve_acls;
48 extern int preserve_xattrs;
49 extern int preserve_links;
50 extern int preserve_hard_links;
51 extern int preserve_devices;
52 extern int preserve_specials;
55 extern int relative_paths;
56 extern int implied_dirs;
57 extern int file_extra_cnt;
58 extern int ignore_perishable;
59 extern int non_perishable_cnt;
60 extern int prune_empty_dirs;
61 extern int copy_links;
62 extern int copy_unsafe_links;
63 extern int protocol_version;
64 extern int sanitize_paths;
65 extern struct stats stats;
67 extern char curr_dir[MAXPATHLEN];
69 extern struct chmod_mode_struct *chmod_modes;
71 extern struct filter_list_struct filter_list;
72 extern struct filter_list_struct server_filter_list;
76 extern int need_unsorted_flist;
77 extern iconv_t ic_send, ic_recv;
80 #define PTR_SIZE (sizeof (struct file_struct *))
84 dev_t filesystem_dev; /* used to implement -x */
86 struct file_list *cur_flist, *first_flist, *dir_flist;
87 int send_dir_ndx = -1, send_dir_depth = 0;
88 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
89 int file_total = 0; /* total of all active items over all file-lists */
90 int flist_eof = 0; /* all the file-lists are now known */
92 /* The tmp_* vars are used as a cache area by make_file() to store data
93 * that the sender doesn't need to remember in its file list. The data
94 * will survive just long enough to be used by send_file_entry(). */
95 static dev_t tmp_rdev;
96 #ifdef SUPPORT_HARD_LINKS
97 static int64 tmp_dev, tmp_ino;
99 static char tmp_sum[MAX_DIGEST_LEN];
101 static char empty_sum[MAX_DIGEST_LEN];
102 static int flist_count_offset; /* for --delete --progress */
103 static int dir_count = 0;
105 static void clean_flist(struct file_list *flist, int strip_root);
106 static void output_flist(struct file_list *flist);
108 void init_flist(void)
111 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
112 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
114 checksum_len = protocol_version < 21 ? 2
115 : protocol_version < 30 ? MD4_DIGEST_LEN
119 static int show_filelist_p(void)
121 return verbose && xfer_dirs && !am_server && !inc_recurse;
124 static void start_filelist_progress(char *kind)
126 rprintf(FCLIENT, "%s ... ", kind);
127 if (verbose > 1 || do_progress)
128 rprintf(FCLIENT, "\n");
132 static void emit_filelist_progress(int count)
134 rprintf(FCLIENT, " %d files...\r", count);
137 static void maybe_emit_filelist_progress(int count)
139 if (do_progress && show_filelist_p() && (count % 100) == 0)
140 emit_filelist_progress(count);
143 static void finish_filelist_progress(const struct file_list *flist)
146 /* This overwrites the progress line */
147 rprintf(FINFO, "%d file%sto consider\n",
148 flist->used, flist->used == 1 ? " " : "s ");
150 rprintf(FINFO, "done\n");
153 void show_flist_stats(void)
158 static void list_file_entry(struct file_struct *f)
160 char permbuf[PERMSTRING_SIZE];
163 if (!F_IS_ACTIVE(f)) {
164 /* this can happen if duplicate names were removed */
168 permstring(permbuf, f->mode);
171 /* TODO: indicate '+' if the entry has an ACL. */
174 if (preserve_links && S_ISLNK(f->mode)) {
175 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
176 permbuf, len, timestring(f->modtime),
177 f_name(f, NULL), F_SYMLINK(f));
181 rprintf(FINFO, "%s %11.0f %s %s\n",
182 permbuf, len, timestring(f->modtime),
187 /* Stat either a symlink or its referent, depending on the settings of
188 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
190 * If path is the name of a symlink, then the linkbuf buffer (which must hold
191 * MAXPATHLEN chars) will be set to the symlink's target string.
193 * The stat structure pointed to by stp will contain information about the
194 * link or the referent as appropriate, if they exist. */
195 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
198 if (link_stat(path, stp, copy_dirlinks) < 0)
200 if (S_ISLNK(stp->st_mode)) {
201 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
204 linkbuf[llen] = '\0';
205 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
207 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
210 return x_stat(path, stp, NULL);
215 return x_stat(path, stp, NULL);
219 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
223 return x_stat(path, stp, NULL);
224 if (x_lstat(path, stp, NULL) < 0)
226 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
228 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
233 return x_stat(path, stp, NULL);
237 /* This function is used to check if a file should be included/excluded
238 * from the list of files based on its name and type etc. The value of
239 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
240 static int is_excluded(char *fname, int is_dir, int filter_level)
242 #if 0 /* This currently never happens, so avoid a useless compare. */
243 if (filter_level == NO_FILTERS)
247 /* never exclude '.', even if somebody does --exclude '*' */
248 if (fname[0] == '.' && !fname[1])
250 /* Handle the -R version of the '.' dir. */
251 if (fname[0] == '/') {
252 int len = strlen(fname);
253 if (fname[len-1] == '.' && fname[len-2] == '/')
257 if (server_filter_list.head
258 && check_filter(&server_filter_list, fname, is_dir) < 0)
260 if (filter_level != ALL_FILTERS)
263 && check_filter(&filter_list, fname, is_dir) < 0)
268 static void send_directory(int f, struct file_list *flist,
269 char *fbuf, int len, int flags);
271 static const char *pathname, *orig_dir;
272 static int pathname_len;
275 /* Make sure flist can hold at least flist->used + extra entries. */
276 static void flist_expand(struct file_list *flist, int extra)
278 struct file_struct **new_ptr;
280 if (flist->used + extra <= 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;
290 /* In case count jumped or we are starting the list
291 * with a known size just set it. */
292 if (flist->malloced < flist->used + extra)
293 flist->malloced = flist->used + extra;
295 new_ptr = realloc_array(flist->files, struct file_struct *,
298 if (verbose >= 2 && flist->malloced != FLIST_START) {
299 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
301 (double)sizeof flist->files[0] * flist->malloced,
302 (new_ptr == flist->files) ? " not" : "");
305 flist->files = new_ptr;
308 out_of_memory("flist_expand");
311 static void flist_done_allocating(struct file_list *flist)
313 void *ptr = pool_boundary(flist->file_pool, 8*1024);
314 if (flist->pool_boundary == ptr)
315 flist->pool_boundary = NULL; /* list didn't use any pool memory */
317 flist->pool_boundary = ptr;
320 int push_pathname(const char *dir, int len)
326 orig_dir = strdup(curr_dir);
328 if (pathname && !pop_dir(orig_dir)) {
329 rsyserr(FERROR, errno, "pop_dir %s failed",
330 full_fname(orig_dir));
331 exit_cleanup(RERR_FILESELECT);
334 if (dir && !push_dir(dir, 0)) {
335 io_error |= IOERR_GENERAL;
336 rsyserr(FERROR, errno, "push_dir %s failed in %s",
337 full_fname(dir), curr_dir);
342 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
347 static void send_file_entry(int f, struct file_struct *file, int ndx)
349 static time_t modtime;
353 static uint32 rdev_major;
356 static char *user_name, *group_name;
357 static char lastname[MAXPATHLEN];
358 char fname[MAXPATHLEN];
359 int first_hlink_ndx = -1;
364 if (ic_send != (iconv_t)-1) {
365 ICONV_CONST char *ibuf;
367 size_t ocnt = MAXPATHLEN, icnt;
369 iconv(ic_send, NULL,0, NULL,0);
370 if ((ibuf = (ICONV_CONST char *)file->dirname) != NULL) {
372 ocnt--; /* pre-subtract the space for the '/' */
373 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1)
378 ibuf = (ICONV_CONST char *)file->basename;
380 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
382 io_error |= IOERR_GENERAL;
384 "[%s] cannot convert filename: %s (%s)\n",
385 who_am_i(), f_name(file, fname), strerror(errno));
393 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
395 if (file->mode == mode)
396 flags |= XMIT_SAME_MODE;
399 if ((preserve_devices && IS_DEVICE(mode))
400 || (preserve_specials && IS_SPECIAL(mode))) {
401 if (protocol_version < 28) {
402 if (tmp_rdev == rdev)
403 flags |= XMIT_SAME_RDEV_pre28;
408 if ((uint32)major(rdev) == rdev_major)
409 flags |= XMIT_SAME_RDEV_MAJOR;
411 rdev_major = major(rdev);
412 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
413 flags |= XMIT_RDEV_MINOR_8_pre30;
415 } else if (protocol_version < 28)
416 rdev = MAKEDEV(0, 0);
418 if ((uid_t)F_OWNER(file) == uid && *lastname)
419 flags |= XMIT_SAME_UID;
422 if (uid_ndx && !numeric_ids) {
423 user_name = add_uid(uid);
424 if (inc_recurse && user_name)
425 flags |= XMIT_USER_NAME_FOLLOWS;
430 if ((gid_t)F_GROUP(file) == gid && *lastname)
431 flags |= XMIT_SAME_GID;
434 if (gid_ndx && !numeric_ids) {
435 group_name = add_gid(gid);
436 if (inc_recurse && group_name)
437 flags |= XMIT_GROUP_NAME_FOLLOWS;
441 if (file->modtime == modtime)
442 flags |= XMIT_SAME_TIME;
444 modtime = file->modtime;
446 #ifdef SUPPORT_HARD_LINKS
448 if (protocol_version >= 30) {
449 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
450 first_hlink_ndx = (int32)(long)np->data - 1;
451 if (first_hlink_ndx < 0) {
452 np->data = (void*)(long)(ndx + 1);
453 flags |= XMIT_HLINK_FIRST;
455 flags |= XMIT_HLINKED;
457 if (tmp_dev == dev) {
458 if (protocol_version >= 28)
459 flags |= XMIT_SAME_DEV_pre30;
462 flags |= XMIT_HLINKED;
468 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
470 l2 = strlen(fname+l1);
473 flags |= XMIT_SAME_NAME;
475 flags |= XMIT_LONG_NAME;
477 /* We must make sure we don't send a zero flag byte or the
478 * other end will terminate the flist transfer. Note that
479 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
480 * it's harmless way to add a bit to the first flag byte. */
481 if (protocol_version >= 28) {
482 if (!flags && !S_ISDIR(mode))
483 flags |= XMIT_TOP_DIR;
484 if ((flags & 0xFF00) || !flags) {
485 flags |= XMIT_EXTENDED_FLAGS;
486 write_shortint(f, flags);
488 write_byte(f, flags);
491 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
492 write_byte(f, flags);
494 if (flags & XMIT_SAME_NAME)
496 if (flags & XMIT_LONG_NAME)
497 write_varint30(f, l2);
500 write_buf(f, fname + l1, l2);
502 if (first_hlink_ndx >= 0) {
503 write_varint30(f, first_hlink_ndx);
507 write_varlong30(f, F_LENGTH(file), 3);
508 if (!(flags & XMIT_SAME_TIME)) {
509 if (protocol_version >= 30)
510 write_varlong(f, modtime, 4);
512 write_int(f, modtime);
514 if (!(flags & XMIT_SAME_MODE))
515 write_int(f, to_wire_mode(mode));
516 if (uid_ndx && !(flags & XMIT_SAME_UID)) {
517 if (protocol_version < 30)
520 write_varint(f, uid);
521 if (flags & XMIT_USER_NAME_FOLLOWS) {
522 int len = strlen(user_name);
524 write_buf(f, user_name, len);
528 if (gid_ndx && !(flags & XMIT_SAME_GID)) {
529 if (protocol_version < 30)
532 write_varint(f, gid);
533 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
534 int len = strlen(group_name);
536 write_buf(f, group_name, len);
540 if ((preserve_devices && IS_DEVICE(mode))
541 || (preserve_specials && IS_SPECIAL(mode))) {
542 if (protocol_version < 28) {
543 if (!(flags & XMIT_SAME_RDEV_pre28))
544 write_int(f, (int)rdev);
546 if (!(flags & XMIT_SAME_RDEV_MAJOR))
547 write_varint30(f, major(rdev));
548 if (protocol_version >= 30)
549 write_varint(f, minor(rdev));
550 else if (flags & XMIT_RDEV_MINOR_8_pre30)
551 write_byte(f, minor(rdev));
553 write_int(f, minor(rdev));
558 if (preserve_links && S_ISLNK(mode)) {
559 const char *sl = F_SYMLINK(file);
560 int len = strlen(sl);
561 write_varint30(f, len);
562 write_buf(f, sl, len);
566 #ifdef SUPPORT_HARD_LINKS
567 if (tmp_dev != 0 && protocol_version < 30) {
568 if (protocol_version < 26) {
569 /* 32-bit dev_t and ino_t */
570 write_int(f, (int32)dev);
571 write_int(f, (int32)tmp_ino);
573 /* 64-bit dev_t and ino_t */
574 if (!(flags & XMIT_SAME_DEV_pre30))
575 write_longint(f, dev);
576 write_longint(f, tmp_ino);
581 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
586 /* Prior to 28, we sent a useless set of nulls. */
589 write_buf(f, sum, checksum_len);
593 strlcpy(lastname, fname, MAXPATHLEN);
595 if (S_ISREG(mode) || S_ISLNK(mode))
596 stats.total_size += F_LENGTH(file);
599 static struct file_struct *recv_file_entry(struct file_list *flist,
602 static int64 modtime;
606 static uint32 rdev_major;
609 static uint16 gid_flags;
610 static char lastname[MAXPATHLEN], *lastdir;
611 static int lastdir_depth, lastdir_len = -1;
612 static unsigned int del_hier_name_len = 0;
613 static int in_del_hier = 0;
614 char thisname[MAXPATHLEN];
615 unsigned int l1 = 0, l2 = 0;
616 int alloc_len, basename_len, linkname_len;
617 int extra_len = file_extra_cnt * EXTRA_LEN;
618 int first_hlink_ndx = -1;
620 const char *basename;
621 struct file_struct *file;
625 if (xflags & XMIT_SAME_NAME)
628 if (xflags & XMIT_LONG_NAME)
629 l2 = read_varint30(f);
633 if (l2 >= MAXPATHLEN - l1) {
635 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
636 xflags, l1, l2, lastname, who_am_i());
637 overflow_exit("recv_file_entry");
640 strlcpy(thisname, lastname, l1 + 1);
641 read_sbuf(f, &thisname[l1], l2);
642 thisname[l1 + l2] = 0;
644 /* Abuse basename_len for a moment... */
645 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
648 if (ic_recv != (iconv_t)-1) {
649 char *obuf = thisname;
650 ICONV_CONST char *ibuf = (ICONV_CONST char *)lastname;
651 size_t ocnt = MAXPATHLEN, icnt = basename_len;
653 if (icnt >= MAXPATHLEN) {
658 iconv(ic_recv, NULL,0, NULL,0);
659 if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
661 io_error |= IOERR_GENERAL;
663 "[%s] cannot convert filename: %s (%s)\n",
664 who_am_i(), lastname, strerror(errno));
671 clean_fname(thisname, 0);
674 sanitize_path(thisname, thisname, "", 0, NULL);
676 if ((basename = strrchr(thisname, '/')) != NULL) {
677 int len = basename++ - thisname;
678 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
679 lastdir = new_array(char, len + 1);
680 memcpy(lastdir, thisname, len);
683 lastdir_depth = count_dir_elements(lastdir);
687 basename_len = strlen(basename) + 1; /* count the '\0' */
689 #ifdef SUPPORT_HARD_LINKS
690 if (protocol_version >= 30
691 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
692 struct file_struct *first;
693 first_hlink_ndx = read_varint30(f);
694 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->used) {
696 "hard-link reference out of range: %d (%d)\n",
697 first_hlink_ndx, flist->used);
698 exit_cleanup(RERR_PROTOCOL);
700 first = flist->files[first_hlink_ndx];
701 file_length = F_LENGTH(first);
702 modtime = first->modtime;
705 uid = F_OWNER(first);
707 gid = F_GROUP(first);
708 if ((preserve_devices && IS_DEVICE(mode))
709 || (preserve_specials && IS_SPECIAL(mode))) {
710 uint32 *devp = F_RDEV_P(first);
711 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
712 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
714 if (preserve_links && S_ISLNK(mode))
715 linkname_len = strlen(F_SYMLINK(first)) + 1;
722 file_length = read_varlong30(f, 3);
723 if (!(xflags & XMIT_SAME_TIME)) {
724 if (protocol_version >= 30) {
725 modtime = read_varlong(f, 4);
726 #if SIZEOF_TIME_T < SIZEOF_INT64
727 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
729 "Time value of %s truncated on receiver.\n",
734 modtime = read_int(f);
736 if (!(xflags & XMIT_SAME_MODE))
737 mode = from_wire_mode(read_int(f));
739 if (chmod_modes && !S_ISLNK(mode))
740 mode = tweak_mode(mode, chmod_modes);
742 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
743 if (protocol_version < 30)
744 uid = (uid_t)read_int(f);
746 uid = (uid_t)read_varint(f);
747 if (xflags & XMIT_USER_NAME_FOLLOWS)
748 uid = recv_user_name(f, uid);
749 else if (inc_recurse && am_root && !numeric_ids)
750 uid = match_uid(uid);
753 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
754 if (protocol_version < 30)
755 gid = (gid_t)read_int(f);
757 gid = (gid_t)read_varint(f);
759 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
760 gid = recv_group_name(f, gid, &gid_flags);
761 else if (inc_recurse && (!am_root || !numeric_ids))
762 gid = match_gid(gid, &gid_flags);
766 if ((preserve_devices && IS_DEVICE(mode))
767 || (preserve_specials && IS_SPECIAL(mode))) {
768 if (protocol_version < 28) {
769 if (!(xflags & XMIT_SAME_RDEV_pre28))
770 rdev = (dev_t)read_int(f);
773 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
774 rdev_major = read_varint30(f);
775 if (protocol_version >= 30)
776 rdev_minor = read_varint(f);
777 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
778 rdev_minor = read_byte(f);
780 rdev_minor = read_int(f);
781 rdev = MAKEDEV(rdev_major, rdev_minor);
783 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
785 } else if (protocol_version < 28)
786 rdev = MAKEDEV(0, 0);
789 if (preserve_links && S_ISLNK(mode)) {
790 linkname_len = read_varint30(f) + 1; /* count the '\0' */
791 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
792 rprintf(FERROR, "overflow: linkname_len=%d\n",
794 overflow_exit("recv_file_entry");
801 #ifdef SUPPORT_HARD_LINKS
803 if (preserve_hard_links) {
804 if (protocol_version < 28 && S_ISREG(mode))
805 xflags |= XMIT_HLINKED;
806 if (xflags & XMIT_HLINKED)
807 extra_len += EXTRA_LEN;
812 /* We need one or two index int32s when we're preserving ACLs. */
814 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
817 if (always_checksum && S_ISREG(mode))
818 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
820 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
821 extra_len += EXTRA_LEN;
823 #if EXTRA_ROUNDING > 0
824 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
825 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
828 if (inc_recurse && S_ISDIR(mode)) {
829 if (one_file_system) {
830 /* Room to save the dir's device for -x */
831 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
833 pool = dir_flist->file_pool;
835 pool = flist->file_pool;
837 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
839 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
841 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
843 file = (struct file_struct *)bp;
844 bp += FILE_STRUCT_LEN;
846 memcpy(bp, basename, basename_len);
847 bp += basename_len + linkname_len; /* skip space for symlink too */
849 #ifdef SUPPORT_HARD_LINKS
850 if (xflags & XMIT_HLINKED)
851 file->flags |= FLAG_HLINKED;
853 file->modtime = (time_t)modtime;
854 file->len32 = (uint32)file_length;
855 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
856 file->flags |= FLAG_LENGTH64;
857 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
864 file->flags |= gid_flags;
868 F_NDX(file) = flist->used + flist->ndx_start;
871 if (basename != thisname) {
872 file->dirname = lastdir;
873 F_DEPTH(file) = lastdir_depth + 1;
878 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
880 if (xflags & XMIT_TOP_DIR) {
881 in_del_hier = recurse;
882 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
883 if (relative_paths && del_hier_name_len > 2
884 && lastname[del_hier_name_len-1] == '.'
885 && lastname[del_hier_name_len-2] == '/')
886 del_hier_name_len -= 2;
887 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
888 } else if (in_del_hier) {
889 if (!relative_paths || !del_hier_name_len
890 || (l1 >= del_hier_name_len
891 && lastname[del_hier_name_len] == '/'))
892 file->flags |= FLAG_XFER_DIR;
898 if ((preserve_devices && IS_DEVICE(mode))
899 || (preserve_specials && IS_SPECIAL(mode))) {
900 uint32 *devp = F_RDEV_P(file);
901 DEV_MAJOR(devp) = major(rdev);
902 DEV_MINOR(devp) = minor(rdev);
907 bp = (char*)file->basename + basename_len;
908 if (first_hlink_ndx >= 0) {
909 struct file_struct *first = flist->files[first_hlink_ndx];
910 memcpy(bp, F_SYMLINK(first), linkname_len);
912 read_sbuf(f, bp, linkname_len - 1);
914 sanitize_path(bp, bp, "", lastdir_depth, NULL);
918 #ifdef SUPPORT_HARD_LINKS
919 if (preserve_hard_links && xflags & XMIT_HLINKED) {
920 if (protocol_version >= 30) {
921 F_HL_GNUM(file) = xflags & XMIT_HLINK_FIRST
922 ? flist->used : first_hlink_ndx;
924 static int32 cnt = 0;
925 struct idev_node *np;
928 if (protocol_version < 26) {
932 if (!(xflags & XMIT_SAME_DEV_pre30))
933 dev = read_longint(f);
934 ino = read_longint(f);
936 np = idev_node(dev, ino);
937 ndx = (int32)(long)np->data - 1;
940 np->data = (void*)(long)cnt;
942 F_HL_GNUM(file) = ndx;
947 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
951 /* Prior to 28, we get a useless set of nulls. */
954 if (first_hlink_ndx >= 0) {
955 struct file_struct *first = flist->files[first_hlink_ndx];
956 memcpy(bp, F_SUM(first), checksum_len);
958 read_buf(f, bp, checksum_len);
962 if (preserve_acls && !S_ISLNK(mode))
963 receive_acl(file, f);
965 #ifdef SUPPORT_XATTRS
967 receive_xattr(file, f );
970 if (S_ISREG(mode) || S_ISLNK(mode))
971 stats.total_size += file_length;
977 * Create a file_struct for a named file by reading its stat()
978 * information and performing extensive checks against global
981 * @return the new file, or NULL if there was an error or this file
982 * should be excluded.
984 * @todo There is a small optimization opportunity here to avoid
985 * stat()ing the file in some circumstances, which has a certain cost.
986 * We are called immediately after doing readdir(), and so we may
987 * already know the d_type of the file. We could for example avoid
988 * statting directories if we're not recursing, but this is not a very
989 * important case. Some systems may not have d_type.
991 struct file_struct *make_file(const char *fname, struct file_list *flist,
992 STRUCT_STAT *stp, int flags, int filter_level)
994 static char *lastdir;
995 static int lastdir_len = -1;
996 struct file_struct *file;
998 char thisname[MAXPATHLEN];
999 char linkname[MAXPATHLEN];
1000 int alloc_len, basename_len, linkname_len;
1001 int extra_len = file_extra_cnt * EXTRA_LEN;
1002 const char *basename;
1006 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1007 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1010 clean_fname(thisname, 0);
1012 sanitize_path(thisname, thisname, "", 0, NULL);
1014 if (stp && S_ISDIR(stp->st_mode)) {
1015 st = *stp; /* Needed for "symlink/." with --relative. */
1016 *linkname = '\0'; /* make IBM code checker happy */
1017 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1018 int save_errno = errno;
1019 /* See if file is excluded before reporting an error. */
1020 if (filter_level != NO_FILTERS
1021 && (is_excluded(thisname, 0, filter_level)
1022 || is_excluded(thisname, 1, filter_level))) {
1023 if (ignore_perishable && save_errno != ENOENT)
1024 non_perishable_cnt++;
1027 if (save_errno == ENOENT) {
1028 #ifdef SUPPORT_LINKS
1029 /* Avoid "vanished" error if symlink points nowhere. */
1030 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1031 && S_ISLNK(st.st_mode)) {
1032 io_error |= IOERR_GENERAL;
1033 rprintf(FERROR, "symlink has no referent: %s\n",
1034 full_fname(thisname));
1038 enum logcode c = am_daemon && protocol_version < 28
1040 io_error |= IOERR_VANISHED;
1041 rprintf(c, "file has vanished: %s\n",
1042 full_fname(thisname));
1045 io_error |= IOERR_GENERAL;
1046 rsyserr(FERROR, save_errno, "readlink %s failed",
1047 full_fname(thisname));
1052 /* backup.c calls us with filter_level set to NO_FILTERS. */
1053 if (filter_level == NO_FILTERS)
1056 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1057 rprintf(FINFO, "skipping directory %s\n", thisname);
1061 /* -x only affects directories because we need to avoid recursing
1062 * into a mount-point directory, not to avoid copying a symlinked
1063 * file if -L (or similar) was specified. */
1064 if (one_file_system && st.st_dev != filesystem_dev
1065 && S_ISDIR(st.st_mode)) {
1066 if (one_file_system > 1) {
1068 rprintf(FINFO, "skipping mount-point dir %s\n",
1073 flags |= FLAG_MOUNT_DIR;
1076 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1077 if (ignore_perishable)
1078 non_perishable_cnt++;
1082 if (lp_ignore_nonreadable(module_id)) {
1083 #ifdef SUPPORT_LINKS
1084 if (!S_ISLNK(st.st_mode))
1086 if (access(thisname, R_OK) != 0)
1092 /* Only divert a directory in the main transfer. */
1094 if (flist->prev && S_ISDIR(st.st_mode)
1095 && flags & FLAG_DIVERT_DIRS) {
1096 /* Room for parent/sibling/next-child info. */
1097 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1099 pool = dir_flist->file_pool;
1101 pool = flist->file_pool;
1106 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1107 who_am_i(), thisname, filter_level);
1110 if ((basename = strrchr(thisname, '/')) != NULL) {
1111 int len = basename++ - thisname;
1112 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1113 lastdir = new_array(char, len + 1);
1114 memcpy(lastdir, thisname, len);
1115 lastdir[len] = '\0';
1119 basename = thisname;
1120 basename_len = strlen(basename) + 1; /* count the '\0' */
1122 #ifdef SUPPORT_LINKS
1123 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1128 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1129 extra_len += EXTRA_LEN;
1131 #if EXTRA_ROUNDING > 0
1132 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1133 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1136 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1139 bp = pool_alloc(pool, alloc_len, "make_file");
1141 if (!(bp = new_array(char, alloc_len)))
1142 out_of_memory("make_file");
1145 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1147 file = (struct file_struct *)bp;
1148 bp += FILE_STRUCT_LEN;
1150 memcpy(bp, basename, basename_len);
1151 bp += basename_len + linkname_len; /* skip space for symlink too */
1153 #ifdef SUPPORT_HARD_LINKS
1154 if (preserve_hard_links && flist && flist->prev) {
1155 if (protocol_version >= 28
1156 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1157 : S_ISREG(st.st_mode)) {
1158 tmp_dev = st.st_dev;
1159 tmp_ino = st.st_ino;
1165 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1166 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1167 tmp_rdev = st.st_rdev;
1172 file->flags = flags;
1173 file->modtime = st.st_mtime;
1174 file->len32 = (uint32)st.st_size;
1175 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1176 file->flags |= FLAG_LENGTH64;
1177 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1179 file->mode = st.st_mode;
1181 F_OWNER(file) = st.st_uid;
1183 F_GROUP(file) = st.st_gid;
1185 if (basename != thisname)
1186 file->dirname = lastdir;
1188 #ifdef SUPPORT_LINKS
1190 bp = (char*)file->basename + basename_len;
1191 memcpy(bp, linkname, linkname_len);
1195 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1196 file_checksum(thisname, tmp_sum, st.st_size);
1198 F_PATHNAME(file) = pathname;
1200 /* This code is only used by the receiver when it is building
1201 * a list of files for a delete pass. */
1202 if (keep_dirlinks && linkname_len && flist) {
1204 int save_mode = file->mode;
1205 file->mode = S_IFDIR; /* Find a directory with our name. */
1206 if (flist_find(dir_flist, file) >= 0
1207 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1208 file->modtime = st2.st_mtime;
1210 file->mode = st2.st_mode;
1212 F_OWNER(file) = st2.st_uid;
1214 F_GROUP(file) = st2.st_gid;
1216 file->mode = save_mode;
1219 if (basename_len == 0+1)
1224 F_NDX(file) = dir_count - 1;
1230 /* Only called for temporary file_struct entries created by make_file(). */
1231 void unmake_file(struct file_struct *file)
1233 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1234 #if EXTRA_ROUNDING > 0
1235 if (extra_cnt & EXTRA_ROUNDING)
1236 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1238 free(REQ_EXTRA(file, extra_cnt));
1241 static struct file_struct *send_file_name(int f, struct file_list *flist,
1242 char *fname, STRUCT_STAT *stp,
1243 int flags, int filter_level)
1245 struct file_struct *file;
1246 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1250 file = make_file(fname, flist, stp, flags, filter_level);
1254 if (chmod_modes && !S_ISLNK(file->mode))
1255 file->mode = tweak_mode(file->mode, chmod_modes);
1258 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1259 sx.st.st_mode = file->mode;
1260 sx.acc_acl = sx.def_acl = NULL;
1261 if (get_acl(fname, &sx) < 0)
1265 #ifdef SUPPORT_XATTRS
1266 if (preserve_xattrs && f >= 0) {
1268 if (get_xattr(fname, &sx) < 0)
1273 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1275 flist_expand(flist, 1);
1276 flist->files[flist->used++] = file;
1278 send_file_entry(f, file, flist->used - 1);
1280 if (preserve_acls && !S_ISLNK(file->mode)) {
1285 #ifdef SUPPORT_XATTRS
1286 if (preserve_xattrs) {
1287 F_XATTR(file) = send_xattr(&sx, f);
1295 static void send_if_directory(int f, struct file_list *flist,
1296 struct file_struct *file,
1297 char *fbuf, unsigned int ol,
1300 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1302 if (S_ISDIR(file->mode)
1303 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1305 unsigned int len = strlen(fbuf);
1306 if (len > 1 && fbuf[len-1] == '/')
1308 if (len >= MAXPATHLEN - 1) {
1309 io_error |= IOERR_GENERAL;
1310 rprintf(FERROR, "skipping long-named directory: %s\n",
1314 save_filters = push_local_filters(fbuf, len);
1315 send_directory(f, flist, fbuf, len, flags);
1316 pop_local_filters(save_filters);
1323 static int file_compare(const void *file1, const void *file2)
1325 return f_name_cmp(*(struct file_struct **)file1,
1326 *(struct file_struct **)file2);
1329 /* The guts of a merge-sort algorithm. This was derived from the glibc
1330 * version, but I (Wayne) changed the merge code to do less copying and
1331 * to require only half the amount of temporary memory. */
1332 static void fsort_tmp(struct file_struct **fp, size_t num,
1333 struct file_struct **tmp)
1335 struct file_struct **f1, **f2, **t;
1344 fsort_tmp(f1, n1, tmp);
1346 fsort_tmp(f2, n2, tmp);
1348 while (f_name_cmp(*f1, *f2) <= 0) {
1355 memcpy(t, f1, n1 * PTR_SIZE);
1357 *f1++ = *f2++, n2--;
1359 while (n1 > 0 && n2 > 0) {
1360 if (f_name_cmp(*t, *f2) <= 0)
1363 *f1++ = *f2++, n2--;
1367 memcpy(f1, t, n1 * PTR_SIZE);
1370 /* This file-struct sorting routine makes sure that any identical names in
1371 * the file list stay in the same order as they were in the original list.
1372 * This is particularly vital in inc_recurse mode where we expect a sort
1373 * on the flist to match the exact order of a sort on the dir_flist. */
1374 static void fsort(struct file_struct **fp, size_t num)
1380 qsort(fp, num, PTR_SIZE, file_compare);
1382 struct file_struct **tmp = new_array(struct file_struct *,
1384 fsort_tmp(fp, num, tmp);
1389 /* We take an entire set of sibling dirs from the sorted flist and link them
1390 * into the tree, setting the appropriate parent/child/sibling pointers. */
1391 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1396 int32 *parent_dp = parent_ndx < 0 ? NULL
1397 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1399 flist_expand(dir_flist, dir_cnt);
1400 dir_flist->sorted = dir_flist->files;
1402 for (i = 0; dir_cnt; i++) {
1403 struct file_struct *file = from_flist->sorted[i];
1405 if (!S_ISDIR(file->mode))
1408 dir_flist->files[dir_flist->used++] = file;
1411 if (!(file->flags & FLAG_XFER_DIR)
1412 || file->flags & FLAG_MOUNT_DIR)
1416 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1418 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1420 send_dir_ndx = dir_flist->used - 1;
1422 dp = F_DIR_NODE_P(file);
1423 DIR_PARENT(dp) = parent_ndx;
1424 DIR_FIRST_CHILD(dp) = -1;
1427 DIR_NEXT_SIBLING(dp) = -1;
1430 /* This function is normally called by the sender, but the receiving side also
1431 * calls it from get_dirlist() with f set to -1 so that we just construct the
1432 * file list in memory without sending it over the wire. Also, get_dirlist()
1433 * might call this with f set to -2, which also indicates that local filter
1434 * rules should be ignored. */
1435 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1442 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1443 int start = flist->used;
1444 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1446 assert(flist != NULL);
1448 if (!(d = opendir(fbuf))) {
1449 io_error |= IOERR_GENERAL;
1450 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1455 if (len != 1 || *fbuf != '/')
1458 remainder = MAXPATHLEN - (p - fbuf);
1460 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1461 char *dname = d_name(di);
1462 if (dname[0] == '.' && (dname[1] == '\0'
1463 || (dname[1] == '.' && dname[2] == '\0')))
1465 if (strlcpy(p, dname, remainder) >= remainder) {
1466 io_error |= IOERR_GENERAL;
1468 "cannot send long-named file %s\n",
1472 if (dname[0] == '\0') {
1473 io_error |= IOERR_GENERAL;
1475 "cannot send file with empty name in %s\n",
1480 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1486 io_error |= IOERR_GENERAL;
1487 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1492 if (f >= 0 && recurse && !divert_dirs) {
1493 int i, end = flist->used - 1;
1494 /* send_if_directory() bumps flist->used, so use "end". */
1495 for (i = start; i <= end; i++)
1496 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1500 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1502 char fbuf[MAXPATHLEN];
1506 dlen = strlen(fbuf);
1508 if (F_PATHNAME(file) != pathname) {
1509 if (!push_pathname(F_PATHNAME(file), -1))
1510 exit_cleanup(RERR_FILESELECT);
1513 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1515 send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1518 void send_extra_file_list(int f, int at_least)
1520 struct file_list *flist;
1522 int future_cnt, save_io_error = io_error;
1527 /* Keep sending data until we have the requested number of
1528 * files in the upcoming file-lists. */
1529 if (cur_flist->next) {
1530 flist = first_flist->prev; /* the newest flist */
1531 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1534 while (future_cnt < at_least) {
1535 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1536 int dir_ndx, dstart = dir_count;
1539 flist = flist_new(0, "send_extra_file_list");
1540 start_write = stats.total_written;
1544 dir_ndx = F_NDX(file);
1547 dir_ndx = send_dir_ndx;
1548 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1549 flist->parent_ndx = dir_ndx;
1551 send1extra(f, file, flist);
1552 dp = F_DIR_NODE_P(file);
1554 /* If there are any duplicate directory names that follow, we
1555 * send all the dirs together in one file-list. The dir_flist
1556 * tree links all the child subdirs onto the last dup dir. */
1557 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1558 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1559 send_dir_ndx = dir_ndx;
1560 file = dir_flist->sorted[dir_ndx];
1561 send1extra(f, file, flist);
1562 dp = F_DIR_NODE_P(file);
1568 if (need_unsorted_flist) {
1569 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1570 out_of_memory("send_extra_file_list");
1571 memcpy(flist->sorted, flist->files,
1572 flist->used * sizeof (struct file_struct*));
1575 flist->sorted = flist->files;
1577 clean_flist(flist, 0);
1579 flist->ndx_end = flist->ndx_start + flist->used - 1
1580 - (dir_count - dstart);
1582 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1583 flist_done_allocating(flist);
1585 file_total += flist->used;
1586 future_cnt += flist->used;
1587 stats.flist_size += stats.total_written - start_write;
1588 stats.num_files += flist->used;
1590 output_flist(flist);
1592 if (DIR_FIRST_CHILD(dp) >= 0) {
1593 send_dir_ndx = DIR_FIRST_CHILD(dp);
1596 while (DIR_NEXT_SIBLING(dp) < 0) {
1597 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1598 write_ndx(f, NDX_FLIST_EOF);
1600 change_local_filter_dir(NULL, 0, 0);
1604 file = dir_flist->sorted[send_dir_ndx];
1605 dp = F_DIR_NODE_P(file);
1607 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1612 if (io_error != save_io_error && !ignore_errors)
1613 send_msg_int(MSG_IO_ERROR, io_error);
1616 struct file_list *send_file_list(int f, int argc, char *argv[])
1618 static const char *lastdir;
1619 static int lastdir_len = -1;
1623 char lastpath[MAXPATHLEN] = "";
1624 struct file_list *flist;
1625 struct timeval start_tv, end_tv;
1628 int flags, disable_buffering;
1630 rprintf(FLOG, "building file list\n");
1631 if (show_filelist_p())
1632 start_filelist_progress("building file list");
1633 else if (inc_recurse && verbose && !am_server)
1634 rprintf(FCLIENT, "sending incremental file list\n");
1636 start_write = stats.total_written;
1637 gettimeofday(&start_tv, NULL);
1639 #ifdef SUPPORT_HARD_LINKS
1640 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1644 flist = cur_flist = flist_new(0, "send_file_list");
1646 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1647 flags = FLAG_DIVERT_DIRS;
1649 dir_flist = cur_flist;
1653 disable_buffering = io_start_buffering_out(f);
1654 if (filesfrom_fd >= 0) {
1655 if (argv[0] && !push_dir(argv[0], 0)) {
1656 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1657 full_fname(argv[0]), curr_dir);
1658 exit_cleanup(RERR_FILESELECT);
1664 char fbuf[MAXPATHLEN];
1669 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1671 sanitize_path(fbuf, fbuf, "", 0, NULL);
1675 strlcpy(fbuf, *argv++, MAXPATHLEN);
1677 sanitize_path(fbuf, fbuf, "", 0, NULL);
1681 if (relative_paths) {
1682 /* We clean up fbuf below. */
1684 } else if (!len || fbuf[len - 1] == '/') {
1685 if (len == 2 && fbuf[0] == '.') {
1686 /* Turn "./" into just "." rather than "./." */
1689 if (len + 1 >= MAXPATHLEN)
1690 overflow_exit("send_file_list");
1695 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1696 && (len == 2 || fbuf[len-3] == '/')) {
1697 if (len + 2 >= MAXPATHLEN)
1698 overflow_exit("send_file_list");
1704 is_dot_dir = fbuf[len-1] == '.'
1705 && (len == 1 || fbuf[len-2] == '/');
1710 if (!relative_paths) {
1711 p = strrchr(fbuf, '/');
1718 len -= p - fbuf + 1;
1723 if ((p = strstr(fbuf, "/./")) != NULL) {
1729 len -= p - fbuf + 3;
1733 /* Get rid of trailing "/" and "/.". */
1735 if (fn[len - 1] == '/') {
1737 if (!--len && !dir) {
1742 else if (len >= 2 && fn[len - 1] == '.'
1743 && fn[len - 2] == '/') {
1745 if (!(len -= 2) && !dir) {
1752 if (len == 1 && fn[0] == '/')
1755 /* Reject a ".." dir in the active part of the path. */
1756 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1757 if ((p[2] == '/' || p[2] == '\0')
1758 && (p == fn || p[-1] == '/')) {
1760 "found \"..\" dir in relative path: %s\n",
1762 exit_cleanup(RERR_SYNTAX);
1772 dirlen = dir ? strlen(dir) : 0;
1773 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1774 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1777 lastdir_len = pathname_len;
1778 } else if (!push_pathname(lastdir, lastdir_len)) {
1780 io_error |= IOERR_GENERAL;
1781 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1782 full_fname(dir), curr_dir);
1787 memmove(fbuf, fn, len + 1);
1789 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1790 io_error |= IOERR_GENERAL;
1791 rsyserr(FERROR, errno, "link_stat %s failed",
1796 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1797 rprintf(FINFO, "skipping directory %s\n", fbuf);
1801 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1802 /* Send the implied directories at the start of the
1803 * source spec, so we get their permissions right. */
1804 char *lp = lastpath, *slash = fbuf;
1806 /* Skip any initial directories in our path that we
1807 * have in common with lastpath. */
1808 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1813 if (fn != p || (*lp && *lp != '/')) {
1814 int save_copy_links = copy_links;
1815 int save_xfer_dirs = xfer_dirs;
1816 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
1817 copy_links |= copy_unsafe_links;
1819 while ((slash = strchr(slash+1, '/')) != 0) {
1821 send_file_name(f, flist, fbuf, NULL,
1822 dir_flags, ALL_FILTERS);
1825 copy_links = save_copy_links;
1826 xfer_dirs = save_xfer_dirs;
1828 strlcpy(lastpath, fbuf, sizeof lastpath);
1833 if (one_file_system)
1834 filesystem_dev = st.st_dev;
1836 if (recurse || (xfer_dirs && is_dot_dir)) {
1837 struct file_struct *file;
1838 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags;
1839 file = send_file_name(f, flist, fbuf, &st,
1840 top_flags, ALL_FILTERS);
1841 if (file && !inc_recurse)
1842 send_if_directory(f, flist, file, fbuf, len, flags);
1844 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1847 gettimeofday(&end_tv, NULL);
1848 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1849 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1850 if (stats.flist_buildtime == 0)
1851 stats.flist_buildtime = 1;
1854 write_byte(f, 0); /* Indicate end of file list */
1856 #ifdef SUPPORT_HARD_LINKS
1857 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1861 if (show_filelist_p())
1862 finish_filelist_progress(flist);
1864 gettimeofday(&end_tv, NULL);
1865 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1866 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1868 /* When converting names, both sides keep an unsorted file-list array
1869 * because the names will differ on the sending and receiving sides
1870 * (both sides will use the unsorted index number for each item). */
1872 /* Sort the list without removing any duplicates. This allows the
1873 * receiving side to ask for whatever name it kept. For incremental
1874 * recursion mode, the sender marks duplicate dirs so that it can
1875 * send them together in a single file-list. */
1877 if (need_unsorted_flist) {
1879 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1880 out_of_memory("send_file_list");
1881 memcpy(flist->sorted, flist->files,
1882 flist->used * sizeof (struct file_struct*));
1883 clean_flist(flist, 0);
1885 flist->sorted = flist->files;
1887 flist->high = flist->used - 1;
1892 flist->sorted = flist->files;
1893 clean_flist(flist, 0);
1895 file_total += flist->used;
1897 /* We don't subtract dir_count for the first send since we
1898 * might have one or more dot dirs which need to get sent. */
1899 flist->ndx_end = flist->ndx_start + flist->used - 1;
1901 if (!numeric_ids && !inc_recurse)
1904 /* send the io_error flag */
1905 if (protocol_version < 30)
1906 write_int(f, ignore_errors ? 0 : io_error);
1907 else if (io_error && !ignore_errors)
1908 send_msg_int(MSG_IO_ERROR, io_error);
1910 if (disable_buffering)
1911 io_end_buffering_out();
1913 stats.flist_size = stats.total_written - start_write;
1914 stats.num_files = flist->used;
1917 output_flist(flist);
1920 rprintf(FINFO, "send_file_list done\n");
1923 add_dirs_to_tree(-1, flist, dir_count);
1924 flist_done_allocating(flist);
1925 if (send_dir_ndx < 0) {
1926 write_ndx(f, NDX_FLIST_EOF);
1929 else if (file_total == 1) {
1930 /* If we're creating incremental file-lists and there
1931 * was just 1 item in the first file-list, send 1 more
1932 * file-list to check if this is a 1-file xfer. */
1933 send_extra_file_list(f, 1);
1940 struct file_list *recv_file_list(int f)
1942 struct file_list *flist;
1947 rprintf(FLOG, "receiving file list\n");
1948 if (show_filelist_p())
1949 start_filelist_progress("receiving file list");
1950 else if (inc_recurse && verbose && !am_server && !first_flist)
1951 rprintf(FCLIENT, "receiving incremental file list\n");
1953 start_read = stats.total_read;
1955 flist = flist_new(0, "recv_file_list");
1957 #ifdef SUPPORT_HARD_LINKS
1958 if (preserve_hard_links && protocol_version < 30)
1963 if (flist->ndx_start == 0)
1964 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1965 dstart = dir_flist->used;
1971 while ((flags = read_byte(f)) != 0) {
1972 struct file_struct *file;
1974 flist_expand(flist, 1);
1976 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1977 flags |= read_byte(f) << 8;
1978 file = recv_file_entry(flist, flags, f);
1980 if (inc_recurse && S_ISDIR(file->mode)) {
1981 flist_expand(dir_flist, 1);
1982 dir_flist->files[dir_flist->used++] = file;
1985 flist->files[flist->used++] = file;
1987 maybe_emit_filelist_progress(flist->used);
1990 rprintf(FINFO, "recv_file_name(%s)\n",
1991 f_name(file, NULL));
1994 file_total += flist->used;
1996 flist->ndx_end = flist->ndx_start + flist->used - 1;
1997 if (inc_recurse && flist->ndx_start)
1998 flist->ndx_end -= dir_flist->used - dstart;
2001 rprintf(FINFO, "received %d names\n", flist->used);
2003 if (show_filelist_p())
2004 finish_filelist_progress(flist);
2007 if (need_unsorted_flist) {
2008 /* Create an extra array of index pointers that we can sort for
2009 * the generator's use (for wading through the files in sorted
2010 * order and for calling flist_find()). We keep the "files"
2011 * list unsorted for our exchange of index numbers with the
2012 * other side (since their names may not sort the same). */
2013 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2014 out_of_memory("recv_file_list");
2015 memcpy(flist->sorted, flist->files,
2016 flist->used * sizeof (struct file_struct*));
2017 if (inc_recurse && dir_flist->used > dstart) {
2018 dir_flist->sorted = realloc_array(dir_flist->sorted,
2019 struct file_struct *,
2021 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2022 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2023 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2028 flist->sorted = flist->files;
2029 if (inc_recurse && dir_flist->used > dstart) {
2030 dir_flist->sorted = dir_flist->files;
2031 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2036 flist_done_allocating(flist);
2038 recv_id_list(f, flist);
2040 clean_flist(flist, relative_paths);
2042 if (protocol_version < 30) {
2043 /* Recv the io_error flag */
2047 io_error |= read_int(f);
2051 output_flist(flist);
2055 for (i = flist->low; i <= flist->high; i++)
2056 list_file_entry(flist->files[i]);
2060 rprintf(FINFO, "recv_file_list done\n");
2062 stats.flist_size += stats.total_read - start_read;
2063 stats.num_files += flist->used;
2068 /* This is only used once by the receiver if the very first file-list
2069 * has exactly one item in it. */
2070 void recv_additional_file_list(int f)
2072 struct file_list *flist;
2073 int ndx = read_ndx(f);
2074 if (ndx == NDX_FLIST_EOF) {
2076 change_local_filter_dir(NULL, 0, 0);
2078 ndx = NDX_FLIST_OFFSET - ndx;
2079 if (ndx < 0 || ndx >= dir_flist->used) {
2080 ndx = NDX_FLIST_OFFSET - ndx;
2082 "[%s] Invalid dir index: %d (%d - %d)\n",
2083 who_am_i(), ndx, NDX_FLIST_OFFSET,
2084 NDX_FLIST_OFFSET - dir_flist->used + 1);
2085 exit_cleanup(RERR_PROTOCOL);
2088 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2091 flist = recv_file_list(f);
2092 flist->parent_ndx = ndx;
2096 /* Search for an identically-named item in the file list. Note that the
2097 * items must agree in their directory-ness, or no match is returned. */
2098 int flist_find(struct file_list *flist, struct file_struct *f)
2100 int low = flist->low, high = flist->high;
2101 int diff, mid, mid_up;
2103 while (low <= high) {
2104 mid = (low + high) / 2;
2105 if (F_IS_ACTIVE(flist->sorted[mid]))
2108 /* Scan for the next non-empty entry using the cached
2109 * distance values. If the value isn't fully up-to-
2110 * date, update it. */
2111 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2112 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2114 mid_up += F_DEPTH(flist->sorted[mid_up]);
2115 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2116 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2118 if (mid_up > high) {
2119 /* If there's nothing left above us, set high to
2120 * a non-empty entry below us and continue. */
2121 high = mid - (int)flist->sorted[mid]->len32;
2122 if (!F_IS_ACTIVE(flist->sorted[high])) {
2124 high -= (int)flist->sorted[high]->len32;
2125 } while (!F_IS_ACTIVE(flist->sorted[high]));
2126 flist->sorted[mid]->len32 = mid - high;
2131 diff = f_name_cmp(flist->sorted[mid_up], f);
2133 if (protocol_version < 29
2134 && S_ISDIR(flist->sorted[mid_up]->mode)
2135 != S_ISDIR(f->mode))
2148 * Free up any resources a file_struct has allocated
2149 * and clear the file.
2151 void clear_file(struct file_struct *file)
2153 /* The +1 zeros out the first char of the basename. */
2154 memset(file, 0, FILE_STRUCT_LEN + 1);
2155 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2156 * entry. Likewise for len32 in the opposite direction. We assume
2157 * that we're alone for now since flist_find() will adjust the counts
2158 * it runs into that aren't up-to-date. */
2159 file->len32 = F_DEPTH(file) = 1;
2162 /* Allocate a new file list. */
2163 struct file_list *flist_new(int flags, char *msg)
2165 struct file_list *flist;
2167 flist = new(struct file_list);
2171 memset(flist, 0, sizeof flist[0]);
2173 if (flags & FLIST_TEMP) {
2174 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2175 out_of_memory, POOL_INTERN)))
2178 /* This is a doubly linked list with prev looping back to
2179 * the end of the list, but the last next pointer is NULL. */
2181 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2182 out_of_memory, POOL_INTERN);
2183 if (!flist->file_pool)
2186 first_flist = cur_flist = flist->prev = flist;
2188 flist->file_pool = first_flist->file_pool;
2190 flist->ndx_start = first_flist->prev->ndx_end + 2;
2192 flist->prev = first_flist->prev;
2193 flist->prev->next = first_flist->prev = flist;
2195 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2202 /* Free up all elements in a flist. */
2203 void flist_free(struct file_list *flist)
2206 /* Was FLIST_TEMP dir-list. */
2207 } else if (flist == flist->prev) {
2208 first_flist = cur_flist = NULL;
2212 if (flist == cur_flist)
2213 cur_flist = flist->next;
2214 if (flist == first_flist)
2215 first_flist = first_flist->next;
2217 flist->prev->next = flist->next;
2219 flist->next = first_flist;
2221 flist->next->prev = flist->prev;
2222 file_total -= flist->used;
2226 if (!flist->prev || !flist_cnt)
2227 pool_destroy(flist->file_pool);
2229 pool_free_old(flist->file_pool, flist->pool_boundary);
2231 if (flist->sorted && flist->sorted != flist->files)
2232 free(flist->sorted);
2237 /* This routine ensures we don't have any duplicate names in our file list.
2238 * duplicate names can cause corruption because of the pipelining. */
2239 static void clean_flist(struct file_list *flist, int strip_root)
2241 char fbuf[MAXPATHLEN];
2246 if (flist->used == 0) {
2252 fsort(flist->sorted, flist->used);
2254 if (!am_sender || inc_recurse) {
2255 for (i = prev_i = 0; i < flist->used; i++) {
2256 if (F_IS_ACTIVE(flist->sorted[i])) {
2261 flist->low = prev_i;
2263 i = prev_i = flist->used - 1;
2267 while (++i < flist->used) {
2269 struct file_struct *file = flist->sorted[i];
2271 if (!F_IS_ACTIVE(file))
2273 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2275 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2276 int save_mode = file->mode;
2277 /* Make sure that this directory doesn't duplicate a
2278 * non-directory earlier in the list. */
2279 flist->high = prev_i;
2280 file->mode = S_IFREG;
2281 j = flist_find(flist, file);
2282 file->mode = save_mode;
2287 /* If one is a dir and the other is not, we want to
2288 * keep the dir because it might have contents in the
2290 if (S_ISDIR(file->mode)) {
2291 struct file_struct *fp = flist->sorted[j];
2292 if (!S_ISDIR(fp->mode))
2300 flist->sorted[drop]->flags |= FLAG_DUPLICATE;
2304 "removing duplicate name %s from file list (%d)\n",
2305 f_name(file, fbuf), drop + flist->ndx_start);
2307 /* Make sure we don't lose track of a user-specified
2309 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2310 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2312 clear_file(flist->sorted[drop]);
2316 if (flist->low == drop) {
2318 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2327 flist->high = prev_i;
2330 /* We need to strip off the leading slashes for relative
2331 * paths, but this must be done _after_ the sorting phase. */
2332 for (i = flist->low; i <= flist->high; i++) {
2333 struct file_struct *file = flist->sorted[i];
2337 while (*file->dirname == '/')
2339 if (!*file->dirname)
2340 file->dirname = NULL;
2344 if (prune_empty_dirs && !am_sender) {
2345 int j, prev_depth = 0;
2347 prev_i = 0; /* It's OK that this isn't really true. */
2349 for (i = flist->low; i <= flist->high; i++) {
2350 struct file_struct *fp, *file = flist->sorted[i];
2352 /* This temporarily abuses the F_DEPTH() value for a
2353 * directory that is in a chain that might get pruned.
2354 * We restore the old value if it gets a reprieve. */
2355 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2356 /* Dump empty dirs when coming back down. */
2357 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2358 fp = flist->sorted[prev_i];
2359 if (F_DEPTH(fp) >= 0)
2361 prev_i = -F_DEPTH(fp)-1;
2364 prev_depth = F_DEPTH(file);
2365 if (is_excluded(f_name(file, fbuf), 1,
2367 /* Keep dirs through this dir. */
2368 for (j = prev_depth-1; ; j--) {
2369 fp = flist->sorted[prev_i];
2370 if (F_DEPTH(fp) >= 0)
2372 prev_i = -F_DEPTH(fp)-1;
2376 F_DEPTH(file) = -prev_i-1;
2379 /* Keep dirs through this non-dir. */
2380 for (j = prev_depth; ; j--) {
2381 fp = flist->sorted[prev_i];
2382 if (F_DEPTH(fp) >= 0)
2384 prev_i = -F_DEPTH(fp)-1;
2389 /* Dump all remaining empty dirs. */
2391 struct file_struct *fp = flist->sorted[prev_i];
2392 if (F_DEPTH(fp) >= 0)
2394 prev_i = -F_DEPTH(fp)-1;
2398 for (i = flist->low; i <= flist->high; i++) {
2399 if (F_IS_ACTIVE(flist->sorted[i]))
2403 for (i = flist->high; i >= flist->low; i--) {
2404 if (F_IS_ACTIVE(flist->sorted[i]))
2411 static void output_flist(struct file_list *flist)
2413 char uidbuf[16], gidbuf[16], depthbuf[16];
2414 struct file_struct *file;
2415 const char *root, *dir, *slash, *name, *trail;
2416 const char *who = who_am_i();
2419 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2420 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2421 for (i = 0; i < flist->used; i++) {
2422 file = flist->sorted[i];
2423 if ((am_root || am_sender) && uid_ndx) {
2424 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2429 static char parens[] = "(\0)\0\0\0";
2430 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2431 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2432 pp, F_GROUP(file), pp + 2);
2436 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2437 if (F_IS_ACTIVE(file)) {
2438 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2439 if ((dir = file->dirname) == NULL)
2443 name = file->basename;
2444 trail = S_ISDIR(file->mode) ? "/" : "";
2446 root = dir = slash = name = trail = "";
2448 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2449 who, i + flist->ndx_start,
2450 root, dir, slash, name, trail,
2451 (int)file->mode, (double)F_LENGTH(file),
2452 uidbuf, gidbuf, file->flags);
2456 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2457 enum fnc_type { t_PATH, t_ITEM };
2459 static int found_prefix;
2461 /* Compare the names of two file_struct entities, similar to how strcmp()
2462 * would do if it were operating on the joined strings.
2464 * Some differences beginning with protocol_version 29: (1) directory names
2465 * are compared with an assumed trailing slash so that they compare in a
2466 * way that would cause them to sort immediately prior to any content they
2467 * may have; (2) a directory of any name compares after a non-directory of
2468 * any name at the same depth; (3) a directory with name "." compares prior
2469 * to anything else. These changes mean that a directory and a non-dir
2470 * with the same name will not compare as equal (protocol_version >= 29).
2472 * The dirname component can be an empty string, but the basename component
2473 * cannot (and never is in the current codebase). The basename component
2474 * may be NULL (for a removed item), in which case it is considered to be
2475 * after any existing item. */
2476 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2479 const uchar *c1, *c2;
2480 enum fnc_state state1, state2;
2481 enum fnc_type type1, type2;
2482 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2484 if (!f1 || !F_IS_ACTIVE(f1)) {
2485 if (!f2 || !F_IS_ACTIVE(f2))
2489 if (!f2 || !F_IS_ACTIVE(f2))
2492 c1 = (uchar*)f1->dirname;
2493 c2 = (uchar*)f2->dirname;
2497 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2498 c1 = (const uchar*)f1->basename;
2499 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2501 state1 = s_TRAILING;
2510 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2511 c2 = (const uchar*)f2->basename;
2512 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2514 state2 = s_TRAILING;
2524 return type1 == t_PATH ? 1 : -1;
2534 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2535 c1 = (const uchar*)f1->basename;
2536 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2538 state1 = s_TRAILING;
2544 state1 = s_TRAILING;
2545 if (type1 == t_PATH) {
2554 if (*c2 && type1 != type2)
2555 return type1 == t_PATH ? 1 : -1;
2564 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2565 c2 = (const uchar*)f2->basename;
2566 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2568 state2 = s_TRAILING;
2574 state2 = s_TRAILING;
2575 if (type2 == t_PATH) {
2588 return type1 == t_PATH ? 1 : -1;
2590 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2595 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2596 * not match if f2's basename is not an exact match of a path element in f1.
2597 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2598 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2602 return found_prefix;
2605 char *f_name_buf(void)
2607 static char names[5][MAXPATHLEN];
2608 static unsigned int n;
2610 n = (n + 1) % (sizeof names / sizeof names[0]);
2615 /* Return a copy of the full filename of a flist entry, using the indicated
2616 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2617 * done because we checked the size when creating the file_struct entry.
2619 char *f_name(const struct file_struct *f, char *fbuf)
2621 if (!f || !F_IS_ACTIVE(f))
2625 fbuf = f_name_buf();
2628 int len = strlen(f->dirname);
2629 memcpy(fbuf, f->dirname, len);
2631 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2633 strlcpy(fbuf, f->basename, MAXPATHLEN);
2638 /* Do a non-recursive scan of the named directory, possibly ignoring all
2639 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2640 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2641 * buffer (the functions we call will append names onto the end, but the old
2642 * dir value will be restored on exit). */
2643 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2645 struct file_list *dirlist;
2646 char dirbuf[MAXPATHLEN];
2647 int save_recurse = recurse;
2648 int save_xfer_dirs = xfer_dirs;
2649 int save_prune_empty_dirs = prune_empty_dirs;
2652 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2653 if (dlen >= MAXPATHLEN)
2658 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2662 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2663 xfer_dirs = save_xfer_dirs;
2664 recurse = save_recurse;
2666 flist_count_offset += dirlist->used;
2668 prune_empty_dirs = 0;
2669 dirlist->sorted = dirlist->files;
2670 clean_flist(dirlist, 0);
2671 prune_empty_dirs = save_prune_empty_dirs;
2674 output_flist(dirlist);