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 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
93 * internally, even if this platform does not allow files to have 64-bit inums.
94 * The only exception is if we're on a platform with no 64-bit type at all.
96 * Because we use read_longint() to get these off the wire, if you transfer
97 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
98 * machine with no 64-bit types then you will get an overflow error.
100 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
101 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
102 * be truncated. But it's a kind of silly thing to do anyhow. */
104 /* The tmp_* vars are used as a cache area by make_file() to store data
105 * that the sender doesn't need to remember in its file list. The data
106 * will survive just long enough to be used by send_file_entry(). */
107 static dev_t tmp_rdev;
108 #ifdef SUPPORT_HARD_LINKS
109 static int64 tmp_dev, tmp_ino;
111 static char tmp_sum[MAX_DIGEST_LEN];
113 static char empty_sum[MAX_DIGEST_LEN];
114 static int flist_count_offset; /* for --delete --progress */
115 static int dir_count = 0;
117 static void clean_flist(struct file_list *flist, int strip_root);
118 static void output_flist(struct file_list *flist);
120 void init_flist(void)
123 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
124 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
126 checksum_len = protocol_version < 21 ? 2
127 : protocol_version < 30 ? MD4_DIGEST_LEN
131 static int show_filelist_p(void)
133 return verbose && xfer_dirs && !am_server && !inc_recurse;
136 static void start_filelist_progress(char *kind)
138 rprintf(FCLIENT, "%s ... ", kind);
139 if (verbose > 1 || do_progress)
140 rprintf(FCLIENT, "\n");
144 static void emit_filelist_progress(int count)
146 rprintf(FCLIENT, " %d files...\r", count);
149 static void maybe_emit_filelist_progress(int count)
151 if (do_progress && show_filelist_p() && (count % 100) == 0)
152 emit_filelist_progress(count);
155 static void finish_filelist_progress(const struct file_list *flist)
158 /* This overwrites the progress line */
159 rprintf(FINFO, "%d file%sto consider\n",
160 flist->used, flist->used == 1 ? " " : "s ");
162 rprintf(FINFO, "done\n");
165 void show_flist_stats(void)
170 static void list_file_entry(struct file_struct *f)
172 char permbuf[PERMSTRING_SIZE];
175 if (!F_IS_ACTIVE(f)) {
176 /* this can happen if duplicate names were removed */
180 permstring(permbuf, f->mode);
183 /* TODO: indicate '+' if the entry has an ACL. */
186 if (preserve_links && S_ISLNK(f->mode)) {
187 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
188 permbuf, len, timestring(f->modtime),
189 f_name(f, NULL), F_SYMLINK(f));
193 rprintf(FINFO, "%s %11.0f %s %s\n",
194 permbuf, len, timestring(f->modtime),
199 /* Stat either a symlink or its referent, depending on the settings of
200 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
202 * If path is the name of a symlink, then the linkbuf buffer (which must hold
203 * MAXPATHLEN chars) will be set to the symlink's target string.
205 * The stat structure pointed to by stp will contain information about the
206 * link or the referent as appropriate, if they exist. */
207 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
210 if (link_stat(path, stp, copy_dirlinks) < 0)
212 if (S_ISLNK(stp->st_mode)) {
213 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
216 linkbuf[llen] = '\0';
217 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
219 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
222 return x_stat(path, stp, NULL);
227 return x_stat(path, stp, NULL);
231 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
235 return x_stat(path, stp, NULL);
236 if (x_lstat(path, stp, NULL) < 0)
238 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
240 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
245 return x_stat(path, stp, NULL);
249 /* This function is used to check if a file should be included/excluded
250 * from the list of files based on its name and type etc. The value of
251 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
252 static int is_excluded(char *fname, int is_dir, int filter_level)
254 #if 0 /* This currently never happens, so avoid a useless compare. */
255 if (filter_level == NO_FILTERS)
259 /* never exclude '.', even if somebody does --exclude '*' */
260 if (fname[0] == '.' && !fname[1])
262 /* Handle the -R version of the '.' dir. */
263 if (fname[0] == '/') {
264 int len = strlen(fname);
265 if (fname[len-1] == '.' && fname[len-2] == '/')
269 if (server_filter_list.head
270 && check_filter(&server_filter_list, fname, is_dir) < 0)
272 if (filter_level != ALL_FILTERS)
275 && check_filter(&filter_list, fname, is_dir) < 0)
280 static void send_directory(int f, struct file_list *flist,
281 char *fbuf, int len, int flags);
283 static const char *pathname, *orig_dir;
284 static int pathname_len;
287 /* Make sure flist can hold at least flist->used + extra entries. */
288 static void flist_expand(struct file_list *flist, int extra)
290 struct file_struct **new_ptr;
292 if (flist->used + extra <= flist->malloced)
295 if (flist->malloced < FLIST_START)
296 flist->malloced = FLIST_START;
297 else if (flist->malloced >= FLIST_LINEAR)
298 flist->malloced += FLIST_LINEAR;
300 flist->malloced *= 2;
302 /* In case count jumped or we are starting the list
303 * with a known size just set it. */
304 if (flist->malloced < flist->used + extra)
305 flist->malloced = flist->used + extra;
307 new_ptr = realloc_array(flist->files, struct file_struct *,
310 if (verbose >= 2 && flist->malloced != FLIST_START) {
311 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
313 (double)sizeof flist->files[0] * flist->malloced,
314 (new_ptr == flist->files) ? " not" : "");
317 flist->files = new_ptr;
320 out_of_memory("flist_expand");
323 static void flist_done_allocating(struct file_list *flist)
325 void *ptr = pool_boundary(flist->file_pool, 8*1024);
326 if (flist->pool_boundary == ptr)
327 flist->pool_boundary = NULL; /* list didn't use any pool memory */
329 flist->pool_boundary = ptr;
332 int push_pathname(const char *dir, int len)
338 orig_dir = strdup(curr_dir);
340 if (pathname && !pop_dir(orig_dir)) {
341 rsyserr(FERROR, errno, "pop_dir %s failed",
342 full_fname(orig_dir));
343 exit_cleanup(RERR_FILESELECT);
346 if (dir && !push_dir(dir, 0)) {
347 io_error |= IOERR_GENERAL;
348 rsyserr(FERROR, errno, "push_dir %s failed in %s",
349 full_fname(dir), curr_dir);
354 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
359 static void send_file_entry(int f, struct file_struct *file, int ndx)
361 static time_t modtime;
365 static uint32 rdev_major;
368 static char *user_name, *group_name;
369 static char lastname[MAXPATHLEN];
370 char fname[MAXPATHLEN];
371 int first_hlink_ndx = -1;
376 if (ic_send != (iconv_t)-1) {
377 ICONV_CONST char *ibuf;
379 size_t ocnt = MAXPATHLEN, icnt;
381 iconv(ic_send, NULL,0, NULL,0);
382 if ((ibuf = (ICONV_CONST char *)file->dirname) != NULL) {
384 ocnt--; /* pre-subtract the space for the '/' */
385 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1)
390 ibuf = (ICONV_CONST char *)file->basename;
392 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
394 io_error |= IOERR_GENERAL;
396 "[%s] cannot convert filename: %s (%s)\n",
397 who_am_i(), f_name(file, fname), strerror(errno));
405 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
407 if (file->mode == mode)
408 xflags |= XMIT_SAME_MODE;
412 if (protocol_version >= 30 && S_ISDIR(mode) && !(file->flags & FLAG_XFER_DIR))
413 xflags |= XMIT_NON_XFER_DIR;
415 if ((preserve_devices && IS_DEVICE(mode))
416 || (preserve_specials && IS_SPECIAL(mode))) {
417 if (protocol_version < 28) {
418 if (tmp_rdev == rdev)
419 xflags |= XMIT_SAME_RDEV_pre28;
424 if ((uint32)major(rdev) == rdev_major)
425 xflags |= XMIT_SAME_RDEV_MAJOR;
427 rdev_major = major(rdev);
428 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
429 xflags |= XMIT_RDEV_MINOR_8_pre30;
431 } else if (protocol_version < 28)
432 rdev = MAKEDEV(0, 0);
434 if ((uid_t)F_OWNER(file) == uid && *lastname)
435 xflags |= XMIT_SAME_UID;
438 if (uid_ndx && !numeric_ids) {
439 user_name = add_uid(uid);
440 if (inc_recurse && user_name)
441 xflags |= XMIT_USER_NAME_FOLLOWS;
446 if ((gid_t)F_GROUP(file) == gid && *lastname)
447 xflags |= XMIT_SAME_GID;
450 if (gid_ndx && !numeric_ids) {
451 group_name = add_gid(gid);
452 if (inc_recurse && group_name)
453 xflags |= XMIT_GROUP_NAME_FOLLOWS;
457 if (file->modtime == modtime)
458 xflags |= XMIT_SAME_TIME;
460 modtime = file->modtime;
462 #ifdef SUPPORT_HARD_LINKS
464 if (protocol_version >= 30) {
465 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
466 first_hlink_ndx = (int32)(long)np->data - 1;
467 if (first_hlink_ndx < 0) {
468 np->data = (void*)(long)(ndx + 1);
469 xflags |= XMIT_HLINK_FIRST;
471 xflags |= XMIT_HLINKED;
473 if (tmp_dev == dev) {
474 if (protocol_version >= 28)
475 xflags |= XMIT_SAME_DEV_pre30;
478 xflags |= XMIT_HLINKED;
484 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
486 l2 = strlen(fname+l1);
489 xflags |= XMIT_SAME_NAME;
491 xflags |= XMIT_LONG_NAME;
493 /* We must make sure we don't send a zero flag byte or the
494 * other end will terminate the flist transfer. Note that
495 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
496 * it's harmless way to add a bit to the first flag byte. */
497 if (protocol_version >= 28) {
498 if (!xflags && !S_ISDIR(mode))
499 xflags |= XMIT_TOP_DIR;
500 if ((xflags & 0xFF00) || !xflags) {
501 xflags |= XMIT_EXTENDED_FLAGS;
502 write_shortint(f, xflags);
504 write_byte(f, xflags);
506 if (!(xflags & 0xFF))
507 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
508 write_byte(f, xflags);
510 if (xflags & XMIT_SAME_NAME)
512 if (xflags & XMIT_LONG_NAME)
513 write_varint30(f, l2);
516 write_buf(f, fname + l1, l2);
518 if (first_hlink_ndx >= 0) {
519 write_varint(f, first_hlink_ndx);
523 write_varlong30(f, F_LENGTH(file), 3);
524 if (!(xflags & XMIT_SAME_TIME)) {
525 if (protocol_version >= 30)
526 write_varlong(f, modtime, 4);
528 write_int(f, modtime);
530 if (!(xflags & XMIT_SAME_MODE))
531 write_int(f, to_wire_mode(mode));
532 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
533 if (protocol_version < 30)
536 write_varint(f, uid);
537 if (xflags & XMIT_USER_NAME_FOLLOWS) {
538 int len = strlen(user_name);
540 write_buf(f, user_name, len);
544 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
545 if (protocol_version < 30)
548 write_varint(f, gid);
549 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
550 int len = strlen(group_name);
552 write_buf(f, group_name, len);
556 if ((preserve_devices && IS_DEVICE(mode))
557 || (preserve_specials && IS_SPECIAL(mode))) {
558 if (protocol_version < 28) {
559 if (!(xflags & XMIT_SAME_RDEV_pre28))
560 write_int(f, (int)rdev);
562 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
563 write_varint30(f, major(rdev));
564 if (protocol_version >= 30)
565 write_varint(f, minor(rdev));
566 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
567 write_byte(f, minor(rdev));
569 write_int(f, minor(rdev));
574 if (preserve_links && S_ISLNK(mode)) {
575 const char *sl = F_SYMLINK(file);
576 int len = strlen(sl);
577 write_varint30(f, len);
578 write_buf(f, sl, len);
582 #ifdef SUPPORT_HARD_LINKS
583 if (tmp_dev != 0 && protocol_version < 30) {
584 if (protocol_version < 26) {
585 /* 32-bit dev_t and ino_t */
586 write_int(f, (int32)dev);
587 write_int(f, (int32)tmp_ino);
589 /* 64-bit dev_t and ino_t */
590 if (!(xflags & XMIT_SAME_DEV_pre30))
591 write_longint(f, dev);
592 write_longint(f, tmp_ino);
597 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
602 /* Prior to 28, we sent a useless set of nulls. */
605 write_buf(f, sum, checksum_len);
609 strlcpy(lastname, fname, MAXPATHLEN);
611 if (S_ISREG(mode) || S_ISLNK(mode))
612 stats.total_size += F_LENGTH(file);
615 static struct file_struct *recv_file_entry(struct file_list *flist,
618 static int64 modtime;
622 static uint32 rdev_major;
625 static uint16 gid_flags;
626 static char lastname[MAXPATHLEN], *lastdir;
627 static int lastdir_depth, lastdir_len = -1;
628 static unsigned int del_hier_name_len = 0;
629 static int in_del_hier = 0;
630 char thisname[MAXPATHLEN];
631 unsigned int l1 = 0, l2 = 0;
632 int alloc_len, basename_len, linkname_len;
633 int extra_len = file_extra_cnt * EXTRA_LEN;
634 int first_hlink_ndx = -1;
636 const char *basename;
637 struct file_struct *file;
641 if (xflags & XMIT_SAME_NAME)
644 if (xflags & XMIT_LONG_NAME)
645 l2 = read_varint30(f);
649 if (l2 >= MAXPATHLEN - l1) {
651 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
652 xflags, l1, l2, lastname, who_am_i());
653 overflow_exit("recv_file_entry");
656 strlcpy(thisname, lastname, l1 + 1);
657 read_sbuf(f, &thisname[l1], l2);
658 thisname[l1 + l2] = 0;
660 /* Abuse basename_len for a moment... */
661 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
664 if (ic_recv != (iconv_t)-1) {
665 char *obuf = thisname;
666 ICONV_CONST char *ibuf = (ICONV_CONST char *)lastname;
667 size_t ocnt = MAXPATHLEN, icnt = basename_len;
669 if (icnt >= MAXPATHLEN) {
674 iconv(ic_recv, NULL,0, NULL,0);
675 if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
677 io_error |= IOERR_GENERAL;
679 "[%s] cannot convert filename: %s (%s)\n",
680 who_am_i(), lastname, strerror(errno));
687 clean_fname(thisname, 0);
690 sanitize_path(thisname, thisname, "", 0, NULL);
692 if ((basename = strrchr(thisname, '/')) != NULL) {
693 int len = basename++ - thisname;
694 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
695 lastdir = new_array(char, len + 1);
696 memcpy(lastdir, thisname, len);
699 lastdir_depth = count_dir_elements(lastdir);
703 basename_len = strlen(basename) + 1; /* count the '\0' */
705 #ifdef SUPPORT_HARD_LINKS
706 if (protocol_version >= 30
707 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
708 struct file_struct *first;
709 first_hlink_ndx = read_varint(f);
710 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->used) {
712 "hard-link reference out of range: %d (%d)\n",
713 first_hlink_ndx, flist->used);
714 exit_cleanup(RERR_PROTOCOL);
716 first = flist->files[first_hlink_ndx];
717 file_length = F_LENGTH(first);
718 modtime = first->modtime;
721 uid = F_OWNER(first);
723 gid = F_GROUP(first);
724 if ((preserve_devices && IS_DEVICE(mode))
725 || (preserve_specials && IS_SPECIAL(mode))) {
726 uint32 *devp = F_RDEV_P(first);
727 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
728 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
730 if (preserve_links && S_ISLNK(mode))
731 linkname_len = strlen(F_SYMLINK(first)) + 1;
738 file_length = read_varlong30(f, 3);
739 if (!(xflags & XMIT_SAME_TIME)) {
740 if (protocol_version >= 30) {
741 modtime = read_varlong(f, 4);
742 #if SIZEOF_TIME_T < SIZEOF_INT64
743 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
745 "Time value of %s truncated on receiver.\n",
750 modtime = read_int(f);
752 if (!(xflags & XMIT_SAME_MODE))
753 mode = from_wire_mode(read_int(f));
755 if (chmod_modes && !S_ISLNK(mode))
756 mode = tweak_mode(mode, chmod_modes);
758 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
759 if (protocol_version < 30)
760 uid = (uid_t)read_int(f);
762 uid = (uid_t)read_varint(f);
763 if (xflags & XMIT_USER_NAME_FOLLOWS)
764 uid = recv_user_name(f, uid);
765 else if (inc_recurse && am_root && !numeric_ids)
766 uid = match_uid(uid);
769 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
770 if (protocol_version < 30)
771 gid = (gid_t)read_int(f);
773 gid = (gid_t)read_varint(f);
775 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
776 gid = recv_group_name(f, gid, &gid_flags);
777 else if (inc_recurse && (!am_root || !numeric_ids))
778 gid = match_gid(gid, &gid_flags);
782 if ((preserve_devices && IS_DEVICE(mode))
783 || (preserve_specials && IS_SPECIAL(mode))) {
784 if (protocol_version < 28) {
785 if (!(xflags & XMIT_SAME_RDEV_pre28))
786 rdev = (dev_t)read_int(f);
789 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
790 rdev_major = read_varint30(f);
791 if (protocol_version >= 30)
792 rdev_minor = read_varint(f);
793 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
794 rdev_minor = read_byte(f);
796 rdev_minor = read_int(f);
797 rdev = MAKEDEV(rdev_major, rdev_minor);
799 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
801 } else if (protocol_version < 28)
802 rdev = MAKEDEV(0, 0);
805 if (preserve_links && S_ISLNK(mode)) {
806 linkname_len = read_varint30(f) + 1; /* count the '\0' */
807 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
808 rprintf(FERROR, "overflow: linkname_len=%d\n",
810 overflow_exit("recv_file_entry");
817 #ifdef SUPPORT_HARD_LINKS
819 if (preserve_hard_links) {
820 if (protocol_version < 28 && S_ISREG(mode))
821 xflags |= XMIT_HLINKED;
822 if (xflags & XMIT_HLINKED)
823 extra_len += EXTRA_LEN;
828 /* We need one or two index int32s when we're preserving ACLs. */
830 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
833 if (always_checksum && S_ISREG(mode))
834 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
836 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
837 extra_len += EXTRA_LEN;
839 #if EXTRA_ROUNDING > 0
840 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
841 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
844 if (inc_recurse && S_ISDIR(mode)) {
845 if (one_file_system) {
846 /* Room to save the dir's device for -x */
847 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
849 pool = dir_flist->file_pool;
851 pool = flist->file_pool;
853 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
855 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
857 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
859 file = (struct file_struct *)bp;
860 bp += FILE_STRUCT_LEN;
862 memcpy(bp, basename, basename_len);
863 bp += basename_len + linkname_len; /* skip space for symlink too */
865 #ifdef SUPPORT_HARD_LINKS
866 if (xflags & XMIT_HLINKED)
867 file->flags |= FLAG_HLINKED;
869 file->modtime = (time_t)modtime;
870 file->len32 = (uint32)file_length;
871 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
872 file->flags |= FLAG_LENGTH64;
873 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
880 file->flags |= gid_flags;
884 F_NDX(file) = flist->used + flist->ndx_start;
887 if (basename != thisname) {
888 file->dirname = lastdir;
889 F_DEPTH(file) = lastdir_depth + 1;
894 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
896 if (xflags & XMIT_TOP_DIR) {
897 in_del_hier = recurse;
898 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
899 if (relative_paths && del_hier_name_len > 2
900 && lastname[del_hier_name_len-1] == '.'
901 && lastname[del_hier_name_len-2] == '/')
902 del_hier_name_len -= 2;
903 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
904 } else if (protocol_version >= 30) {
905 if (!(xflags & XMIT_NON_XFER_DIR))
906 file->flags |= FLAG_XFER_DIR;
907 } else if (in_del_hier) {
908 if (!relative_paths || !del_hier_name_len
909 || (l1 >= del_hier_name_len
910 && lastname[del_hier_name_len] == '/'))
911 file->flags |= FLAG_XFER_DIR;
917 if ((preserve_devices && IS_DEVICE(mode))
918 || (preserve_specials && IS_SPECIAL(mode))) {
919 uint32 *devp = F_RDEV_P(file);
920 DEV_MAJOR(devp) = major(rdev);
921 DEV_MINOR(devp) = minor(rdev);
926 bp = (char*)file->basename + basename_len;
927 if (first_hlink_ndx >= 0) {
928 struct file_struct *first = flist->files[first_hlink_ndx];
929 memcpy(bp, F_SYMLINK(first), linkname_len);
931 read_sbuf(f, bp, linkname_len - 1);
933 sanitize_path(bp, bp, "", lastdir_depth, NULL);
937 #ifdef SUPPORT_HARD_LINKS
938 if (preserve_hard_links && xflags & XMIT_HLINKED) {
939 if (protocol_version >= 30) {
940 F_HL_GNUM(file) = xflags & XMIT_HLINK_FIRST
941 ? flist->used : first_hlink_ndx;
943 static int32 cnt = 0;
944 struct ht_int64_node *np;
947 if (protocol_version < 26) {
951 if (!(xflags & XMIT_SAME_DEV_pre30))
952 dev = read_longint(f);
953 ino = read_longint(f);
955 np = idev_find(dev, ino);
956 ndx = (int32)(long)np->data - 1;
959 np->data = (void*)(long)cnt;
961 F_HL_GNUM(file) = ndx;
966 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
970 /* Prior to 28, we get a useless set of nulls. */
973 if (first_hlink_ndx >= 0) {
974 struct file_struct *first = flist->files[first_hlink_ndx];
975 memcpy(bp, F_SUM(first), checksum_len);
977 read_buf(f, bp, checksum_len);
981 if (preserve_acls && !S_ISLNK(mode))
982 receive_acl(file, f);
984 #ifdef SUPPORT_XATTRS
986 receive_xattr(file, f );
989 if (S_ISREG(mode) || S_ISLNK(mode))
990 stats.total_size += file_length;
995 /* Create a file_struct for a named file by reading its stat() information
996 * and performing extensive checks against global options.
998 * Returns a pointer to the new file struct, or NULL if there was an error
999 * or this file should be excluded. */
1000 struct file_struct *make_file(const char *fname, struct file_list *flist,
1001 STRUCT_STAT *stp, int flags, int filter_level)
1003 static char *lastdir;
1004 static int lastdir_len = -1;
1005 struct file_struct *file;
1006 char thisname[MAXPATHLEN];
1007 char linkname[MAXPATHLEN];
1008 int alloc_len, basename_len, linkname_len;
1009 int extra_len = file_extra_cnt * EXTRA_LEN;
1010 const char *basename;
1015 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1016 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1019 clean_fname(thisname, 0);
1021 sanitize_path(thisname, thisname, "", 0, NULL);
1023 if (stp && S_ISDIR(stp->st_mode)) {
1024 st = *stp; /* Needed for "symlink/." with --relative. */
1025 *linkname = '\0'; /* make IBM code checker happy */
1026 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1027 int save_errno = errno;
1028 /* See if file is excluded before reporting an error. */
1029 if (filter_level != NO_FILTERS
1030 && (is_excluded(thisname, 0, filter_level)
1031 || is_excluded(thisname, 1, filter_level))) {
1032 if (ignore_perishable && save_errno != ENOENT)
1033 non_perishable_cnt++;
1036 if (save_errno == ENOENT) {
1037 #ifdef SUPPORT_LINKS
1038 /* Avoid "vanished" error if symlink points nowhere. */
1039 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1040 && S_ISLNK(st.st_mode)) {
1041 io_error |= IOERR_GENERAL;
1042 rprintf(FERROR, "symlink has no referent: %s\n",
1043 full_fname(thisname));
1047 enum logcode c = am_daemon && protocol_version < 28
1049 io_error |= IOERR_VANISHED;
1050 rprintf(c, "file has vanished: %s\n",
1051 full_fname(thisname));
1054 io_error |= IOERR_GENERAL;
1055 rsyserr(FERROR, save_errno, "readlink %s failed",
1056 full_fname(thisname));
1061 /* backup.c calls us with filter_level set to NO_FILTERS. */
1062 if (filter_level == NO_FILTERS)
1065 if (S_ISDIR(st.st_mode)) {
1067 rprintf(FINFO, "skipping directory %s\n", thisname);
1071 flags &= ~FLAG_XFER_DIR;
1073 /* -x only affects directories because we need to avoid recursing
1074 * into a mount-point directory, not to avoid copying a symlinked
1075 * file if -L (or similar) was specified. */
1076 if (one_file_system && st.st_dev != filesystem_dev
1077 && S_ISDIR(st.st_mode)) {
1078 if (one_file_system > 1) {
1080 rprintf(FINFO, "skipping mount-point dir %s\n",
1085 flags |= FLAG_MOUNT_DIR;
1088 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1089 if (ignore_perishable)
1090 non_perishable_cnt++;
1094 if (lp_ignore_nonreadable(module_id)) {
1095 #ifdef SUPPORT_LINKS
1096 if (!S_ISLNK(st.st_mode))
1098 if (access(thisname, R_OK) != 0)
1104 /* Only divert a directory in the main transfer. */
1106 if (flist->prev && S_ISDIR(st.st_mode)
1107 && flags & FLAG_DIVERT_DIRS) {
1108 /* Room for parent/sibling/next-child info. */
1109 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1111 pool = dir_flist->file_pool;
1113 pool = flist->file_pool;
1118 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1119 who_am_i(), thisname, filter_level);
1122 if ((basename = strrchr(thisname, '/')) != NULL) {
1123 int len = basename++ - thisname;
1124 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1125 lastdir = new_array(char, len + 1);
1126 memcpy(lastdir, thisname, len);
1127 lastdir[len] = '\0';
1131 basename = thisname;
1132 basename_len = strlen(basename) + 1; /* count the '\0' */
1134 #ifdef SUPPORT_LINKS
1135 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1140 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1141 extra_len += EXTRA_LEN;
1143 #if EXTRA_ROUNDING > 0
1144 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1145 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1148 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1151 bp = pool_alloc(pool, alloc_len, "make_file");
1153 if (!(bp = new_array(char, alloc_len)))
1154 out_of_memory("make_file");
1157 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1159 file = (struct file_struct *)bp;
1160 bp += FILE_STRUCT_LEN;
1162 memcpy(bp, basename, basename_len);
1163 bp += basename_len + linkname_len; /* skip space for symlink too */
1165 #ifdef SUPPORT_HARD_LINKS
1166 if (preserve_hard_links && flist && flist->prev) {
1167 if (protocol_version >= 28
1168 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1169 : S_ISREG(st.st_mode)) {
1170 tmp_dev = st.st_dev;
1171 tmp_ino = st.st_ino;
1177 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1178 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1179 tmp_rdev = st.st_rdev;
1184 file->flags = flags;
1185 file->modtime = st.st_mtime;
1186 file->len32 = (uint32)st.st_size;
1187 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1188 file->flags |= FLAG_LENGTH64;
1189 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1191 file->mode = st.st_mode;
1193 F_OWNER(file) = st.st_uid;
1195 F_GROUP(file) = st.st_gid;
1197 if (basename != thisname)
1198 file->dirname = lastdir;
1200 #ifdef SUPPORT_LINKS
1202 bp = (char*)file->basename + basename_len;
1203 memcpy(bp, linkname, linkname_len);
1207 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1208 file_checksum(thisname, tmp_sum, st.st_size);
1210 F_PATHNAME(file) = pathname;
1212 /* This code is only used by the receiver when it is building
1213 * a list of files for a delete pass. */
1214 if (keep_dirlinks && linkname_len && flist) {
1216 int save_mode = file->mode;
1217 file->mode = S_IFDIR; /* Find a directory with our name. */
1218 if (flist_find(dir_flist, file) >= 0
1219 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1220 file->modtime = st2.st_mtime;
1222 file->mode = st2.st_mode;
1224 F_OWNER(file) = st2.st_uid;
1226 F_GROUP(file) = st2.st_gid;
1228 file->mode = save_mode;
1231 if (basename_len == 0+1)
1236 F_NDX(file) = dir_count - 1;
1242 /* Only called for temporary file_struct entries created by make_file(). */
1243 void unmake_file(struct file_struct *file)
1245 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1246 #if EXTRA_ROUNDING > 0
1247 if (extra_cnt & EXTRA_ROUNDING)
1248 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1250 free(REQ_EXTRA(file, extra_cnt));
1253 static struct file_struct *send_file_name(int f, struct file_list *flist,
1254 char *fname, STRUCT_STAT *stp,
1255 int flags, int filter_level)
1257 struct file_struct *file;
1258 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1262 file = make_file(fname, flist, stp, flags, filter_level);
1266 if (chmod_modes && !S_ISLNK(file->mode))
1267 file->mode = tweak_mode(file->mode, chmod_modes);
1270 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1271 sx.st.st_mode = file->mode;
1272 sx.acc_acl = sx.def_acl = NULL;
1273 if (get_acl(fname, &sx) < 0)
1277 #ifdef SUPPORT_XATTRS
1278 if (preserve_xattrs && f >= 0) {
1280 if (get_xattr(fname, &sx) < 0)
1285 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1287 flist_expand(flist, 1);
1288 flist->files[flist->used++] = file;
1290 send_file_entry(f, file, flist->used - 1);
1292 if (preserve_acls && !S_ISLNK(file->mode)) {
1297 #ifdef SUPPORT_XATTRS
1298 if (preserve_xattrs) {
1299 F_XATTR(file) = send_xattr(&sx, f);
1307 static void send_if_directory(int f, struct file_list *flist,
1308 struct file_struct *file,
1309 char *fbuf, unsigned int ol,
1312 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1314 if (S_ISDIR(file->mode)
1315 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1317 unsigned int len = strlen(fbuf);
1318 if (len > 1 && fbuf[len-1] == '/')
1320 if (len >= MAXPATHLEN - 1) {
1321 io_error |= IOERR_GENERAL;
1322 rprintf(FERROR, "skipping long-named directory: %s\n",
1326 save_filters = push_local_filters(fbuf, len);
1327 send_directory(f, flist, fbuf, len, flags);
1328 pop_local_filters(save_filters);
1335 static int file_compare(const void *file1, const void *file2)
1337 return f_name_cmp(*(struct file_struct **)file1,
1338 *(struct file_struct **)file2);
1341 /* The guts of a merge-sort algorithm. This was derived from the glibc
1342 * version, but I (Wayne) changed the merge code to do less copying and
1343 * to require only half the amount of temporary memory. */
1344 static void fsort_tmp(struct file_struct **fp, size_t num,
1345 struct file_struct **tmp)
1347 struct file_struct **f1, **f2, **t;
1356 fsort_tmp(f1, n1, tmp);
1358 fsort_tmp(f2, n2, tmp);
1360 while (f_name_cmp(*f1, *f2) <= 0) {
1367 memcpy(t, f1, n1 * PTR_SIZE);
1369 *f1++ = *f2++, n2--;
1371 while (n1 > 0 && n2 > 0) {
1372 if (f_name_cmp(*t, *f2) <= 0)
1375 *f1++ = *f2++, n2--;
1379 memcpy(f1, t, n1 * PTR_SIZE);
1382 /* This file-struct sorting routine makes sure that any identical names in
1383 * the file list stay in the same order as they were in the original list.
1384 * This is particularly vital in inc_recurse mode where we expect a sort
1385 * on the flist to match the exact order of a sort on the dir_flist. */
1386 static void fsort(struct file_struct **fp, size_t num)
1392 qsort(fp, num, PTR_SIZE, file_compare);
1394 struct file_struct **tmp = new_array(struct file_struct *,
1396 fsort_tmp(fp, num, tmp);
1401 /* We take an entire set of sibling dirs from the sorted flist and link them
1402 * into the tree, setting the appropriate parent/child/sibling pointers. */
1403 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1408 int32 *parent_dp = parent_ndx < 0 ? NULL
1409 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1411 flist_expand(dir_flist, dir_cnt);
1412 dir_flist->sorted = dir_flist->files;
1414 for (i = 0; dir_cnt; i++) {
1415 struct file_struct *file = from_flist->sorted[i];
1417 if (!S_ISDIR(file->mode))
1420 dir_flist->files[dir_flist->used++] = file;
1423 if (!(file->flags & FLAG_XFER_DIR)
1424 || file->flags & FLAG_MOUNT_DIR)
1428 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1430 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1432 send_dir_ndx = dir_flist->used - 1;
1434 dp = F_DIR_NODE_P(file);
1435 DIR_PARENT(dp) = parent_ndx;
1436 DIR_FIRST_CHILD(dp) = -1;
1439 DIR_NEXT_SIBLING(dp) = -1;
1442 /* This function is normally called by the sender, but the receiving side also
1443 * calls it from get_dirlist() with f set to -1 so that we just construct the
1444 * file list in memory without sending it over the wire. Also, get_dirlist()
1445 * might call this with f set to -2, which also indicates that local filter
1446 * rules should be ignored. */
1447 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1454 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1455 int start = flist->used;
1456 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1458 assert(flist != NULL);
1460 if (!(d = opendir(fbuf))) {
1461 io_error |= IOERR_GENERAL;
1462 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1467 if (len != 1 || *fbuf != '/')
1470 remainder = MAXPATHLEN - (p - fbuf);
1472 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1473 char *dname = d_name(di);
1474 if (dname[0] == '.' && (dname[1] == '\0'
1475 || (dname[1] == '.' && dname[2] == '\0')))
1477 if (strlcpy(p, dname, remainder) >= remainder) {
1478 io_error |= IOERR_GENERAL;
1480 "cannot send long-named file %s\n",
1484 if (dname[0] == '\0') {
1485 io_error |= IOERR_GENERAL;
1487 "cannot send file with empty name in %s\n",
1492 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1498 io_error |= IOERR_GENERAL;
1499 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1504 if (f >= 0 && recurse && !divert_dirs) {
1505 int i, end = flist->used - 1;
1506 /* send_if_directory() bumps flist->used, so use "end". */
1507 for (i = start; i <= end; i++)
1508 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1512 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1514 char fbuf[MAXPATHLEN];
1518 dlen = strlen(fbuf);
1520 if (F_PATHNAME(file) != pathname) {
1521 if (!push_pathname(F_PATHNAME(file), -1))
1522 exit_cleanup(RERR_FILESELECT);
1525 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1527 send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1530 void send_extra_file_list(int f, int at_least)
1532 struct file_list *flist;
1534 int future_cnt, save_io_error = io_error;
1539 /* Keep sending data until we have the requested number of
1540 * files in the upcoming file-lists. */
1541 if (cur_flist->next) {
1542 flist = first_flist->prev; /* the newest flist */
1543 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1546 while (future_cnt < at_least) {
1547 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1548 int dir_ndx, dstart = dir_count;
1551 flist = flist_new(0, "send_extra_file_list");
1552 start_write = stats.total_written;
1556 dir_ndx = F_NDX(file);
1559 dir_ndx = send_dir_ndx;
1560 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1561 flist->parent_ndx = dir_ndx;
1563 send1extra(f, file, flist);
1564 dp = F_DIR_NODE_P(file);
1566 /* If there are any duplicate directory names that follow, we
1567 * send all the dirs together in one file-list. The dir_flist
1568 * tree links all the child subdirs onto the last dup dir. */
1569 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1570 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1571 send_dir_ndx = dir_ndx;
1572 file = dir_flist->sorted[dir_ndx];
1573 send1extra(f, file, flist);
1574 dp = F_DIR_NODE_P(file);
1580 if (need_unsorted_flist) {
1581 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1582 out_of_memory("send_extra_file_list");
1583 memcpy(flist->sorted, flist->files,
1584 flist->used * sizeof (struct file_struct*));
1587 flist->sorted = flist->files;
1589 clean_flist(flist, 0);
1591 flist->ndx_end = flist->ndx_start + flist->used - 1
1592 - (dir_count - dstart);
1594 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1595 flist_done_allocating(flist);
1597 file_total += flist->used;
1598 future_cnt += flist->used;
1599 stats.flist_size += stats.total_written - start_write;
1600 stats.num_files += flist->used;
1602 output_flist(flist);
1604 if (DIR_FIRST_CHILD(dp) >= 0) {
1605 send_dir_ndx = DIR_FIRST_CHILD(dp);
1608 while (DIR_NEXT_SIBLING(dp) < 0) {
1609 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1610 write_ndx(f, NDX_FLIST_EOF);
1612 change_local_filter_dir(NULL, 0, 0);
1616 file = dir_flist->sorted[send_dir_ndx];
1617 dp = F_DIR_NODE_P(file);
1619 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1624 if (io_error != save_io_error && !ignore_errors)
1625 send_msg_int(MSG_IO_ERROR, io_error);
1628 struct file_list *send_file_list(int f, int argc, char *argv[])
1630 static const char *lastdir;
1631 static int lastdir_len = -1;
1635 char lastpath[MAXPATHLEN] = "";
1636 struct file_list *flist;
1637 struct timeval start_tv, end_tv;
1640 int flags, disable_buffering;
1642 rprintf(FLOG, "building file list\n");
1643 if (show_filelist_p())
1644 start_filelist_progress("building file list");
1645 else if (inc_recurse && verbose && !am_server)
1646 rprintf(FCLIENT, "sending incremental file list\n");
1648 start_write = stats.total_written;
1649 gettimeofday(&start_tv, NULL);
1651 #ifdef SUPPORT_HARD_LINKS
1652 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1656 flist = cur_flist = flist_new(0, "send_file_list");
1658 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1659 flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1661 dir_flist = cur_flist;
1662 flags = FLAG_XFER_DIR;
1665 disable_buffering = io_start_buffering_out(f);
1666 if (filesfrom_fd >= 0) {
1667 if (argv[0] && !push_dir(argv[0], 0)) {
1668 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1669 full_fname(argv[0]), curr_dir);
1670 exit_cleanup(RERR_FILESELECT);
1676 char fbuf[MAXPATHLEN];
1681 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1683 sanitize_path(fbuf, fbuf, "", 0, NULL);
1687 strlcpy(fbuf, *argv++, MAXPATHLEN);
1689 sanitize_path(fbuf, fbuf, "", 0, NULL);
1693 if (relative_paths) {
1694 /* We clean up fbuf below. */
1696 } else if (!len || fbuf[len - 1] == '/') {
1697 if (len == 2 && fbuf[0] == '.') {
1698 /* Turn "./" into just "." rather than "./." */
1701 if (len + 1 >= MAXPATHLEN)
1702 overflow_exit("send_file_list");
1707 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1708 && (len == 2 || fbuf[len-3] == '/')) {
1709 if (len + 2 >= MAXPATHLEN)
1710 overflow_exit("send_file_list");
1716 is_dot_dir = fbuf[len-1] == '.'
1717 && (len == 1 || fbuf[len-2] == '/');
1722 if (!relative_paths) {
1723 p = strrchr(fbuf, '/');
1730 len -= p - fbuf + 1;
1735 if ((p = strstr(fbuf, "/./")) != NULL) {
1741 len -= p - fbuf + 3;
1745 /* Get rid of trailing "/" and "/.". */
1747 if (fn[len - 1] == '/') {
1749 if (!--len && !dir) {
1754 else if (len >= 2 && fn[len - 1] == '.'
1755 && fn[len - 2] == '/') {
1757 if (!(len -= 2) && !dir) {
1764 if (len == 1 && fn[0] == '/')
1767 /* Reject a ".." dir in the active part of the path. */
1768 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1769 if ((p[2] == '/' || p[2] == '\0')
1770 && (p == fn || p[-1] == '/')) {
1772 "found \"..\" dir in relative path: %s\n",
1774 exit_cleanup(RERR_SYNTAX);
1784 dirlen = dir ? strlen(dir) : 0;
1785 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1786 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1789 lastdir_len = pathname_len;
1790 } else if (!push_pathname(lastdir, lastdir_len)) {
1792 io_error |= IOERR_GENERAL;
1793 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1794 full_fname(dir), curr_dir);
1799 memmove(fbuf, fn, len + 1);
1801 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1802 io_error |= IOERR_GENERAL;
1803 rsyserr(FERROR, errno, "link_stat %s failed",
1808 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1809 rprintf(FINFO, "skipping directory %s\n", fbuf);
1813 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1814 /* Send the implied directories at the start of the
1815 * source spec, so we get their permissions right. */
1816 char *lp = lastpath, *slash = fbuf;
1818 /* Skip any initial directories in our path that we
1819 * have in common with lastpath. */
1820 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1825 if (fn != p || (*lp && *lp != '/')) {
1826 int save_copy_links = copy_links;
1827 int save_xfer_dirs = xfer_dirs;
1828 int dir_flags = flags & ~FLAG_XFER_DIR;
1829 copy_links |= copy_unsafe_links;
1831 while ((slash = strchr(slash+1, '/')) != 0) {
1833 send_file_name(f, flist, fbuf, NULL,
1834 dir_flags, ALL_FILTERS);
1837 copy_links = save_copy_links;
1838 xfer_dirs = save_xfer_dirs;
1840 strlcpy(lastpath, fbuf, sizeof lastpath);
1845 if (one_file_system)
1846 filesystem_dev = st.st_dev;
1848 if (recurse || (xfer_dirs && is_dot_dir)) {
1849 struct file_struct *file;
1850 int top_flags = FLAG_TOP_DIR | flags;
1851 file = send_file_name(f, flist, fbuf, &st,
1852 top_flags, ALL_FILTERS);
1853 if (file && !inc_recurse)
1854 send_if_directory(f, flist, file, fbuf, len, flags);
1856 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1859 gettimeofday(&end_tv, NULL);
1860 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1861 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1862 if (stats.flist_buildtime == 0)
1863 stats.flist_buildtime = 1;
1866 write_byte(f, 0); /* Indicate end of file list */
1868 #ifdef SUPPORT_HARD_LINKS
1869 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1873 if (show_filelist_p())
1874 finish_filelist_progress(flist);
1876 gettimeofday(&end_tv, NULL);
1877 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1878 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1880 /* When converting names, both sides keep an unsorted file-list array
1881 * because the names will differ on the sending and receiving sides
1882 * (both sides will use the unsorted index number for each item). */
1884 /* Sort the list without removing any duplicates. This allows the
1885 * receiving side to ask for whatever name it kept. For incremental
1886 * recursion mode, the sender marks duplicate dirs so that it can
1887 * send them together in a single file-list. */
1889 if (need_unsorted_flist) {
1891 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1892 out_of_memory("send_file_list");
1893 memcpy(flist->sorted, flist->files,
1894 flist->used * sizeof (struct file_struct*));
1895 clean_flist(flist, 0);
1897 flist->sorted = flist->files;
1899 flist->high = flist->used - 1;
1904 flist->sorted = flist->files;
1905 clean_flist(flist, 0);
1907 file_total += flist->used;
1909 /* We don't subtract dir_count for the first send since we
1910 * might have one or more dot dirs which need to get sent. */
1911 flist->ndx_end = flist->ndx_start + flist->used - 1;
1913 if (!numeric_ids && !inc_recurse)
1916 /* send the io_error flag */
1917 if (protocol_version < 30)
1918 write_int(f, ignore_errors ? 0 : io_error);
1919 else if (io_error && !ignore_errors)
1920 send_msg_int(MSG_IO_ERROR, io_error);
1922 if (disable_buffering)
1923 io_end_buffering_out();
1925 stats.flist_size = stats.total_written - start_write;
1926 stats.num_files = flist->used;
1929 output_flist(flist);
1932 rprintf(FINFO, "send_file_list done\n");
1935 add_dirs_to_tree(-1, flist, dir_count);
1936 flist_done_allocating(flist);
1937 if (send_dir_ndx < 0) {
1938 write_ndx(f, NDX_FLIST_EOF);
1941 else if (file_total == 1) {
1942 /* If we're creating incremental file-lists and there
1943 * was just 1 item in the first file-list, send 1 more
1944 * file-list to check if this is a 1-file xfer. */
1945 send_extra_file_list(f, 1);
1952 struct file_list *recv_file_list(int f)
1954 struct file_list *flist;
1959 rprintf(FLOG, "receiving file list\n");
1960 if (show_filelist_p())
1961 start_filelist_progress("receiving file list");
1962 else if (inc_recurse && verbose && !am_server && !first_flist)
1963 rprintf(FCLIENT, "receiving incremental file list\n");
1965 start_read = stats.total_read;
1967 #ifdef SUPPORT_HARD_LINKS
1968 if (preserve_hard_links && protocol_version < 30)
1972 flist = flist_new(0, "recv_file_list");
1975 if (flist->ndx_start == 0)
1976 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1977 dstart = dir_flist->used;
1983 while ((flags = read_byte(f)) != 0) {
1984 struct file_struct *file;
1986 flist_expand(flist, 1);
1988 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1989 flags |= read_byte(f) << 8;
1990 file = recv_file_entry(flist, flags, f);
1992 if (inc_recurse && S_ISDIR(file->mode)) {
1993 flist_expand(dir_flist, 1);
1994 dir_flist->files[dir_flist->used++] = file;
1997 flist->files[flist->used++] = file;
1999 maybe_emit_filelist_progress(flist->used);
2002 rprintf(FINFO, "recv_file_name(%s)\n",
2003 f_name(file, NULL));
2006 file_total += flist->used;
2008 flist->ndx_end = flist->ndx_start + flist->used - 1;
2009 if (inc_recurse && flist->ndx_start)
2010 flist->ndx_end -= dir_flist->used - dstart;
2013 rprintf(FINFO, "received %d names\n", flist->used);
2015 if (show_filelist_p())
2016 finish_filelist_progress(flist);
2019 if (need_unsorted_flist) {
2020 /* Create an extra array of index pointers that we can sort for
2021 * the generator's use (for wading through the files in sorted
2022 * order and for calling flist_find()). We keep the "files"
2023 * list unsorted for our exchange of index numbers with the
2024 * other side (since their names may not sort the same). */
2025 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2026 out_of_memory("recv_file_list");
2027 memcpy(flist->sorted, flist->files,
2028 flist->used * sizeof (struct file_struct*));
2029 if (inc_recurse && dir_flist->used > dstart) {
2030 dir_flist->sorted = realloc_array(dir_flist->sorted,
2031 struct file_struct *,
2033 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2034 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2035 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2040 flist->sorted = flist->files;
2041 if (inc_recurse && dir_flist->used > dstart) {
2042 dir_flist->sorted = dir_flist->files;
2043 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2048 flist_done_allocating(flist);
2050 recv_id_list(f, flist);
2052 clean_flist(flist, relative_paths);
2054 if (protocol_version < 30) {
2055 /* Recv the io_error flag */
2059 io_error |= read_int(f);
2063 output_flist(flist);
2067 for (i = flist->low; i <= flist->high; i++)
2068 list_file_entry(flist->files[i]);
2072 rprintf(FINFO, "recv_file_list done\n");
2074 stats.flist_size += stats.total_read - start_read;
2075 stats.num_files += flist->used;
2080 /* This is only used once by the receiver if the very first file-list
2081 * has exactly one item in it. */
2082 void recv_additional_file_list(int f)
2084 struct file_list *flist;
2085 int ndx = read_ndx(f);
2086 if (ndx == NDX_FLIST_EOF) {
2088 change_local_filter_dir(NULL, 0, 0);
2090 ndx = NDX_FLIST_OFFSET - ndx;
2091 if (ndx < 0 || ndx >= dir_flist->used) {
2092 ndx = NDX_FLIST_OFFSET - ndx;
2094 "[%s] Invalid dir index: %d (%d - %d)\n",
2095 who_am_i(), ndx, NDX_FLIST_OFFSET,
2096 NDX_FLIST_OFFSET - dir_flist->used + 1);
2097 exit_cleanup(RERR_PROTOCOL);
2100 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2103 flist = recv_file_list(f);
2104 flist->parent_ndx = ndx;
2108 /* Search for an identically-named item in the file list. Note that the
2109 * items must agree in their directory-ness, or no match is returned. */
2110 int flist_find(struct file_list *flist, struct file_struct *f)
2112 int low = flist->low, high = flist->high;
2113 int diff, mid, mid_up;
2115 while (low <= high) {
2116 mid = (low + high) / 2;
2117 if (F_IS_ACTIVE(flist->sorted[mid]))
2120 /* Scan for the next non-empty entry using the cached
2121 * distance values. If the value isn't fully up-to-
2122 * date, update it. */
2123 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2124 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2126 mid_up += F_DEPTH(flist->sorted[mid_up]);
2127 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2128 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2130 if (mid_up > high) {
2131 /* If there's nothing left above us, set high to
2132 * a non-empty entry below us and continue. */
2133 high = mid - (int)flist->sorted[mid]->len32;
2134 if (!F_IS_ACTIVE(flist->sorted[high])) {
2136 high -= (int)flist->sorted[high]->len32;
2137 } while (!F_IS_ACTIVE(flist->sorted[high]));
2138 flist->sorted[mid]->len32 = mid - high;
2143 diff = f_name_cmp(flist->sorted[mid_up], f);
2145 if (protocol_version < 29
2146 && S_ISDIR(flist->sorted[mid_up]->mode)
2147 != S_ISDIR(f->mode))
2160 * Free up any resources a file_struct has allocated
2161 * and clear the file.
2163 void clear_file(struct file_struct *file)
2165 /* The +1 zeros out the first char of the basename. */
2166 memset(file, 0, FILE_STRUCT_LEN + 1);
2167 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2168 * entry. Likewise for len32 in the opposite direction. We assume
2169 * that we're alone for now since flist_find() will adjust the counts
2170 * it runs into that aren't up-to-date. */
2171 file->len32 = F_DEPTH(file) = 1;
2174 /* Allocate a new file list. */
2175 struct file_list *flist_new(int flags, char *msg)
2177 struct file_list *flist;
2179 flist = new0(struct file_list);
2183 if (flags & FLIST_TEMP) {
2184 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2185 out_of_memory, POOL_INTERN)))
2188 /* This is a doubly linked list with prev looping back to
2189 * the end of the list, but the last next pointer is NULL. */
2191 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2192 out_of_memory, POOL_INTERN);
2193 if (!flist->file_pool)
2196 first_flist = cur_flist = flist->prev = flist;
2198 flist->file_pool = first_flist->file_pool;
2200 flist->ndx_start = first_flist->prev->ndx_end + 2;
2202 flist->prev = first_flist->prev;
2203 flist->prev->next = first_flist->prev = flist;
2205 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2212 /* Free up all elements in a flist. */
2213 void flist_free(struct file_list *flist)
2216 /* Was FLIST_TEMP dir-list. */
2217 } else if (flist == flist->prev) {
2218 first_flist = cur_flist = NULL;
2222 if (flist == cur_flist)
2223 cur_flist = flist->next;
2224 if (flist == first_flist)
2225 first_flist = first_flist->next;
2227 flist->prev->next = flist->next;
2229 flist->next = first_flist;
2231 flist->next->prev = flist->prev;
2232 file_total -= flist->used;
2236 if (!flist->prev || !flist_cnt)
2237 pool_destroy(flist->file_pool);
2239 pool_free_old(flist->file_pool, flist->pool_boundary);
2241 if (flist->sorted && flist->sorted != flist->files)
2242 free(flist->sorted);
2247 /* This routine ensures we don't have any duplicate names in our file list.
2248 * duplicate names can cause corruption because of the pipelining. */
2249 static void clean_flist(struct file_list *flist, int strip_root)
2251 char fbuf[MAXPATHLEN];
2256 if (flist->used == 0) {
2262 fsort(flist->sorted, flist->used);
2264 if (!am_sender || inc_recurse) {
2265 for (i = prev_i = 0; i < flist->used; i++) {
2266 if (F_IS_ACTIVE(flist->sorted[i])) {
2271 flist->low = prev_i;
2273 i = prev_i = flist->used - 1;
2277 while (++i < flist->used) {
2279 struct file_struct *file = flist->sorted[i];
2281 if (!F_IS_ACTIVE(file))
2283 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2285 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2286 int save_mode = file->mode;
2287 /* Make sure that this directory doesn't duplicate a
2288 * non-directory earlier in the list. */
2289 flist->high = prev_i;
2290 file->mode = S_IFREG;
2291 j = flist_find(flist, file);
2292 file->mode = save_mode;
2297 /* If one is a dir and the other is not, we want to
2298 * keep the dir because it might have contents in the
2300 if (S_ISDIR(file->mode)) {
2301 struct file_struct *fp = flist->sorted[j];
2302 if (!S_ISDIR(fp->mode))
2310 flist->sorted[drop]->flags |= FLAG_DUPLICATE;
2314 "removing duplicate name %s from file list (%d)\n",
2315 f_name(file, fbuf), drop + flist->ndx_start);
2317 /* Make sure we don't lose track of a user-specified
2319 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2320 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2322 clear_file(flist->sorted[drop]);
2326 if (flist->low == drop) {
2328 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2337 flist->high = prev_i;
2340 /* We need to strip off the leading slashes for relative
2341 * paths, but this must be done _after_ the sorting phase. */
2342 for (i = flist->low; i <= flist->high; i++) {
2343 struct file_struct *file = flist->sorted[i];
2347 while (*file->dirname == '/')
2349 if (!*file->dirname)
2350 file->dirname = NULL;
2354 if (prune_empty_dirs && !am_sender) {
2355 int j, prev_depth = 0;
2357 prev_i = 0; /* It's OK that this isn't really true. */
2359 for (i = flist->low; i <= flist->high; i++) {
2360 struct file_struct *fp, *file = flist->sorted[i];
2362 /* This temporarily abuses the F_DEPTH() value for a
2363 * directory that is in a chain that might get pruned.
2364 * We restore the old value if it gets a reprieve. */
2365 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2366 /* Dump empty dirs when coming back down. */
2367 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2368 fp = flist->sorted[prev_i];
2369 if (F_DEPTH(fp) >= 0)
2371 prev_i = -F_DEPTH(fp)-1;
2374 prev_depth = F_DEPTH(file);
2375 if (is_excluded(f_name(file, fbuf), 1,
2377 /* Keep dirs through this dir. */
2378 for (j = prev_depth-1; ; j--) {
2379 fp = flist->sorted[prev_i];
2380 if (F_DEPTH(fp) >= 0)
2382 prev_i = -F_DEPTH(fp)-1;
2386 F_DEPTH(file) = -prev_i-1;
2389 /* Keep dirs through this non-dir. */
2390 for (j = prev_depth; ; j--) {
2391 fp = flist->sorted[prev_i];
2392 if (F_DEPTH(fp) >= 0)
2394 prev_i = -F_DEPTH(fp)-1;
2399 /* Dump all remaining empty dirs. */
2401 struct file_struct *fp = flist->sorted[prev_i];
2402 if (F_DEPTH(fp) >= 0)
2404 prev_i = -F_DEPTH(fp)-1;
2408 for (i = flist->low; i <= flist->high; i++) {
2409 if (F_IS_ACTIVE(flist->sorted[i]))
2413 for (i = flist->high; i >= flist->low; i--) {
2414 if (F_IS_ACTIVE(flist->sorted[i]))
2421 static void output_flist(struct file_list *flist)
2423 char uidbuf[16], gidbuf[16], depthbuf[16];
2424 struct file_struct *file;
2425 const char *root, *dir, *slash, *name, *trail;
2426 const char *who = who_am_i();
2429 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2430 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2431 for (i = 0; i < flist->used; i++) {
2432 file = flist->sorted[i];
2433 if ((am_root || am_sender) && uid_ndx) {
2434 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2439 static char parens[] = "(\0)\0\0\0";
2440 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2441 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2442 pp, F_GROUP(file), pp + 2);
2446 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2447 if (F_IS_ACTIVE(file)) {
2448 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2449 if ((dir = file->dirname) == NULL)
2453 name = file->basename;
2454 trail = S_ISDIR(file->mode) ? "/" : "";
2456 root = dir = slash = name = trail = "";
2458 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2459 who, i + flist->ndx_start,
2460 root, dir, slash, name, trail,
2461 (int)file->mode, (double)F_LENGTH(file),
2462 uidbuf, gidbuf, file->flags);
2466 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2467 enum fnc_type { t_PATH, t_ITEM };
2469 static int found_prefix;
2471 /* Compare the names of two file_struct entities, similar to how strcmp()
2472 * would do if it were operating on the joined strings.
2474 * Some differences beginning with protocol_version 29: (1) directory names
2475 * are compared with an assumed trailing slash so that they compare in a
2476 * way that would cause them to sort immediately prior to any content they
2477 * may have; (2) a directory of any name compares after a non-directory of
2478 * any name at the same depth; (3) a directory with name "." compares prior
2479 * to anything else. These changes mean that a directory and a non-dir
2480 * with the same name will not compare as equal (protocol_version >= 29).
2482 * The dirname component can be an empty string, but the basename component
2483 * cannot (and never is in the current codebase). The basename component
2484 * may be NULL (for a removed item), in which case it is considered to be
2485 * after any existing item. */
2486 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2489 const uchar *c1, *c2;
2490 enum fnc_state state1, state2;
2491 enum fnc_type type1, type2;
2492 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2494 if (!f1 || !F_IS_ACTIVE(f1)) {
2495 if (!f2 || !F_IS_ACTIVE(f2))
2499 if (!f2 || !F_IS_ACTIVE(f2))
2502 c1 = (uchar*)f1->dirname;
2503 c2 = (uchar*)f2->dirname;
2507 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2508 c1 = (const uchar*)f1->basename;
2509 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2511 state1 = s_TRAILING;
2520 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2521 c2 = (const uchar*)f2->basename;
2522 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2524 state2 = s_TRAILING;
2534 return type1 == t_PATH ? 1 : -1;
2544 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2545 c1 = (const uchar*)f1->basename;
2546 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2548 state1 = s_TRAILING;
2554 state1 = s_TRAILING;
2555 if (type1 == t_PATH) {
2564 if (*c2 && type1 != type2)
2565 return type1 == t_PATH ? 1 : -1;
2574 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2575 c2 = (const uchar*)f2->basename;
2576 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2578 state2 = s_TRAILING;
2584 state2 = s_TRAILING;
2585 if (type2 == t_PATH) {
2598 return type1 == t_PATH ? 1 : -1;
2600 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2605 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2606 * not match if f2's basename is not an exact match of a path element in f1.
2607 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2608 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2612 return found_prefix;
2615 char *f_name_buf(void)
2617 static char names[5][MAXPATHLEN];
2618 static unsigned int n;
2620 n = (n + 1) % (sizeof names / sizeof names[0]);
2625 /* Return a copy of the full filename of a flist entry, using the indicated
2626 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2627 * done because we checked the size when creating the file_struct entry.
2629 char *f_name(const struct file_struct *f, char *fbuf)
2631 if (!f || !F_IS_ACTIVE(f))
2635 fbuf = f_name_buf();
2638 int len = strlen(f->dirname);
2639 memcpy(fbuf, f->dirname, len);
2641 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2643 strlcpy(fbuf, f->basename, MAXPATHLEN);
2648 /* Do a non-recursive scan of the named directory, possibly ignoring all
2649 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2650 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2651 * buffer (the functions we call will append names onto the end, but the old
2652 * dir value will be restored on exit). */
2653 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2655 struct file_list *dirlist;
2656 char dirbuf[MAXPATHLEN];
2657 int save_recurse = recurse;
2658 int save_xfer_dirs = xfer_dirs;
2659 int save_prune_empty_dirs = prune_empty_dirs;
2662 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2663 if (dlen >= MAXPATHLEN)
2668 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2672 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2673 xfer_dirs = save_xfer_dirs;
2674 recurse = save_recurse;
2676 flist_count_offset += dirlist->used;
2678 prune_empty_dirs = 0;
2679 dirlist->sorted = dirlist->files;
2680 clean_flist(dirlist, 0);
2681 prune_empty_dirs = save_prune_empty_dirs;
2684 output_flist(dirlist);