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;
56 extern int relative_paths;
57 extern int implied_dirs;
58 extern int file_extra_cnt;
59 extern int ignore_perishable;
60 extern int non_perishable_cnt;
61 extern int prune_empty_dirs;
62 extern int copy_links;
63 extern int copy_unsafe_links;
64 extern int protocol_version;
65 extern int sanitize_paths;
66 extern struct stats stats;
67 extern char *filesfrom_host;
69 extern char curr_dir[MAXPATHLEN];
71 extern struct chmod_mode_struct *chmod_modes;
73 extern struct filter_list_struct filter_list;
74 extern struct filter_list_struct server_filter_list;
78 extern int filesfrom_convert;
79 extern int need_unsorted_flist;
80 extern iconv_t ic_send, ic_recv;
83 #define PTR_SIZE (sizeof (struct file_struct *))
87 dev_t filesystem_dev; /* used to implement -x */
89 struct file_list *cur_flist, *first_flist, *dir_flist;
90 int send_dir_ndx = -1, send_dir_depth = 0;
91 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
92 int file_total = 0; /* total of all active items over all file-lists */
93 int flist_eof = 0; /* all the file-lists are now known */
95 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
96 * internally, even if this platform does not allow files to have 64-bit inums.
97 * The only exception is if we're on a platform with no 64-bit type at all.
99 * Because we use read_longint() to get these off the wire, if you transfer
100 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
101 * machine with no 64-bit types then you will get an overflow error.
103 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
104 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
105 * be truncated. But it's a kind of silly thing to do anyhow. */
107 /* The tmp_* vars are used as a cache area by make_file() to store data
108 * that the sender doesn't need to remember in its file list. The data
109 * will survive just long enough to be used by send_file_entry(). */
110 static dev_t tmp_rdev;
111 #ifdef SUPPORT_HARD_LINKS
112 static int64 tmp_dev, tmp_ino;
114 static char tmp_sum[MAX_DIGEST_LEN];
116 static char empty_sum[MAX_DIGEST_LEN];
117 static int flist_count_offset; /* for --delete --progress */
118 static int dir_count = 0;
119 static int high_hlink_ndx;
121 static void clean_flist(struct file_list *flist, int strip_root);
122 static void output_flist(struct file_list *flist);
124 void init_flist(void)
127 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
128 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
130 checksum_len = protocol_version < 21 ? 2
131 : protocol_version < 30 ? MD4_DIGEST_LEN
135 static int show_filelist_p(void)
137 return verbose && xfer_dirs && !am_server && !inc_recurse;
140 static void start_filelist_progress(char *kind)
142 rprintf(FCLIENT, "%s ... ", kind);
143 if (verbose > 1 || do_progress)
144 rprintf(FCLIENT, "\n");
148 static void emit_filelist_progress(int count)
150 rprintf(FCLIENT, " %d files...\r", count);
153 static void maybe_emit_filelist_progress(int count)
155 if (do_progress && show_filelist_p() && (count % 100) == 0)
156 emit_filelist_progress(count);
159 static void finish_filelist_progress(const struct file_list *flist)
162 /* This overwrites the progress line */
163 rprintf(FINFO, "%d file%sto consider\n",
164 flist->used, flist->used == 1 ? " " : "s ");
166 rprintf(FINFO, "done\n");
169 void show_flist_stats(void)
174 static void list_file_entry(struct file_struct *f)
176 char permbuf[PERMSTRING_SIZE];
179 if (!F_IS_ACTIVE(f)) {
180 /* this can happen if duplicate names were removed */
184 permstring(permbuf, f->mode);
187 /* TODO: indicate '+' if the entry has an ACL. */
190 if (preserve_links && S_ISLNK(f->mode)) {
191 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
192 permbuf, len, timestring(f->modtime),
193 f_name(f, NULL), F_SYMLINK(f));
197 rprintf(FINFO, "%s %11.0f %s %s\n",
198 permbuf, len, timestring(f->modtime),
203 /* Stat either a symlink or its referent, depending on the settings of
204 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
206 * If path is the name of a symlink, then the linkbuf buffer (which must hold
207 * MAXPATHLEN chars) will be set to the symlink's target string.
209 * The stat structure pointed to by stp will contain information about the
210 * link or the referent as appropriate, if they exist. */
211 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
214 if (link_stat(path, stp, copy_dirlinks) < 0)
216 if (S_ISLNK(stp->st_mode)) {
217 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
220 linkbuf[llen] = '\0';
221 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
223 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
226 return x_stat(path, stp, NULL);
231 return x_stat(path, stp, NULL);
235 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
239 return x_stat(path, stp, NULL);
240 if (x_lstat(path, stp, NULL) < 0)
242 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
244 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
249 return x_stat(path, stp, NULL);
253 /* This function is used to check if a file should be included/excluded
254 * from the list of files based on its name and type etc. The value of
255 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
256 static int is_excluded(char *fname, int is_dir, int filter_level)
258 #if 0 /* This currently never happens, so avoid a useless compare. */
259 if (filter_level == NO_FILTERS)
263 /* never exclude '.', even if somebody does --exclude '*' */
264 if (fname[0] == '.' && !fname[1])
266 /* Handle the -R version of the '.' dir. */
267 if (fname[0] == '/') {
268 int len = strlen(fname);
269 if (fname[len-1] == '.' && fname[len-2] == '/')
273 if (server_filter_list.head
274 && check_filter(&server_filter_list, fname, is_dir) < 0)
276 if (filter_level != ALL_FILTERS)
279 && check_filter(&filter_list, fname, is_dir) < 0)
284 static void send_directory(int f, struct file_list *flist,
285 char *fbuf, int len, int flags);
287 static const char *pathname, *orig_dir;
288 static int pathname_len;
291 /* Make sure flist can hold at least flist->used + extra entries. */
292 static void flist_expand(struct file_list *flist, int extra)
294 struct file_struct **new_ptr;
296 if (flist->used + extra <= flist->malloced)
299 if (flist->malloced < FLIST_START)
300 flist->malloced = FLIST_START;
301 else if (flist->malloced >= FLIST_LINEAR)
302 flist->malloced += FLIST_LINEAR;
304 flist->malloced *= 2;
306 /* In case count jumped or we are starting the list
307 * with a known size just set it. */
308 if (flist->malloced < flist->used + extra)
309 flist->malloced = flist->used + extra;
311 new_ptr = realloc_array(flist->files, struct file_struct *,
314 if (verbose >= 2 && flist->malloced != FLIST_START) {
315 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
317 (double)sizeof flist->files[0] * flist->malloced,
318 (new_ptr == flist->files) ? " not" : "");
321 flist->files = new_ptr;
324 out_of_memory("flist_expand");
327 static void flist_done_allocating(struct file_list *flist)
329 void *ptr = pool_boundary(flist->file_pool, 8*1024);
330 if (flist->pool_boundary == ptr)
331 flist->pool_boundary = NULL; /* list didn't use any pool memory */
333 flist->pool_boundary = ptr;
336 int push_pathname(const char *dir, int len)
342 orig_dir = strdup(curr_dir);
344 if (pathname && !pop_dir(orig_dir)) {
345 rsyserr(FERROR, errno, "pop_dir %s failed",
346 full_fname(orig_dir));
347 exit_cleanup(RERR_FILESELECT);
350 if (dir && !push_dir(dir, 0)) {
351 io_error |= IOERR_GENERAL;
352 rsyserr(FERROR, errno, "push_dir %s failed in %s",
353 full_fname(dir), curr_dir);
358 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
363 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
365 static time_t modtime;
369 static uint32 rdev_major;
372 static char *user_name, *group_name;
373 static char lastname[MAXPATHLEN];
374 char fname[MAXPATHLEN];
375 int first_hlink_ndx = -1;
380 if (ic_send != (iconv_t)-1) {
383 INIT_CONST_XBUF(outbuf, fname);
386 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
387 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
388 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
391 outbuf.buf[outbuf.len++] = '/';
394 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
395 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
397 io_error |= IOERR_GENERAL;
399 "[%s] cannot convert filename: %s (%s)\n",
400 who_am_i(), f_name(file, fname), strerror(errno));
403 outbuf.buf[outbuf.len] = '\0';
408 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
410 if (file->mode == mode)
411 xflags |= XMIT_SAME_MODE;
415 if (protocol_version >= 30 && S_ISDIR(mode) && !(file->flags & FLAG_XFER_DIR))
416 xflags |= XMIT_NON_XFER_DIR;
418 if ((preserve_devices && IS_DEVICE(mode))
419 || (preserve_specials && IS_SPECIAL(mode))) {
420 if (protocol_version < 28) {
421 if (tmp_rdev == rdev)
422 xflags |= XMIT_SAME_RDEV_pre28;
427 if ((uint32)major(rdev) == rdev_major)
428 xflags |= XMIT_SAME_RDEV_MAJOR;
430 rdev_major = major(rdev);
431 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
432 xflags |= XMIT_RDEV_MINOR_8_pre30;
434 } else if (protocol_version < 28)
435 rdev = MAKEDEV(0, 0);
437 if ((uid_t)F_OWNER(file) == uid && *lastname)
438 xflags |= XMIT_SAME_UID;
441 if (uid_ndx && !numeric_ids) {
442 user_name = add_uid(uid);
443 if (inc_recurse && user_name)
444 xflags |= XMIT_USER_NAME_FOLLOWS;
449 if ((gid_t)F_GROUP(file) == gid && *lastname)
450 xflags |= XMIT_SAME_GID;
453 if (gid_ndx && !numeric_ids) {
454 group_name = add_gid(gid);
455 if (inc_recurse && group_name)
456 xflags |= XMIT_GROUP_NAME_FOLLOWS;
460 if (file->modtime == modtime)
461 xflags |= XMIT_SAME_TIME;
463 modtime = file->modtime;
465 #ifdef SUPPORT_HARD_LINKS
467 if (protocol_version >= 30) {
468 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
469 first_hlink_ndx = (int32)(long)np->data - 1;
470 if (first_hlink_ndx < 0) {
471 high_hlink_ndx = ndx + first_ndx;
472 np->data = (void*)(long)(high_hlink_ndx + 1);
473 xflags |= XMIT_HLINK_FIRST;
475 xflags |= XMIT_HLINKED;
477 if (tmp_dev == dev) {
478 if (protocol_version >= 28)
479 xflags |= XMIT_SAME_DEV_pre30;
482 xflags |= XMIT_HLINKED;
488 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
490 l2 = strlen(fname+l1);
493 xflags |= XMIT_SAME_NAME;
495 xflags |= XMIT_LONG_NAME;
497 /* We must make sure we don't send a zero flag byte or the
498 * other end will terminate the flist transfer. Note that
499 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
500 * it's harmless way to add a bit to the first flag byte. */
501 if (protocol_version >= 28) {
502 if (!xflags && !S_ISDIR(mode))
503 xflags |= XMIT_TOP_DIR;
504 if ((xflags & 0xFF00) || !xflags) {
505 xflags |= XMIT_EXTENDED_FLAGS;
506 write_shortint(f, xflags);
508 write_byte(f, xflags);
510 if (!(xflags & 0xFF))
511 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
512 write_byte(f, xflags);
514 if (xflags & XMIT_SAME_NAME)
516 if (xflags & XMIT_LONG_NAME)
517 write_varint30(f, l2);
520 write_buf(f, fname + l1, l2);
522 if (first_hlink_ndx >= 0) {
523 write_varint(f, first_hlink_ndx);
524 if (first_hlink_ndx >= first_ndx)
528 write_varlong30(f, F_LENGTH(file), 3);
529 if (!(xflags & XMIT_SAME_TIME)) {
530 if (protocol_version >= 30)
531 write_varlong(f, modtime, 4);
533 write_int(f, modtime);
535 if (!(xflags & XMIT_SAME_MODE))
536 write_int(f, to_wire_mode(mode));
537 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
538 if (protocol_version < 30)
541 write_varint(f, uid);
542 if (xflags & XMIT_USER_NAME_FOLLOWS) {
543 int len = strlen(user_name);
545 write_buf(f, user_name, len);
549 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
550 if (protocol_version < 30)
553 write_varint(f, gid);
554 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
555 int len = strlen(group_name);
557 write_buf(f, group_name, len);
561 if ((preserve_devices && IS_DEVICE(mode))
562 || (preserve_specials && IS_SPECIAL(mode))) {
563 if (protocol_version < 28) {
564 if (!(xflags & XMIT_SAME_RDEV_pre28))
565 write_int(f, (int)rdev);
567 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
568 write_varint30(f, major(rdev));
569 if (protocol_version >= 30)
570 write_varint(f, minor(rdev));
571 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
572 write_byte(f, minor(rdev));
574 write_int(f, minor(rdev));
579 if (preserve_links && S_ISLNK(mode)) {
580 const char *sl = F_SYMLINK(file);
581 int len = strlen(sl);
582 write_varint30(f, len);
583 write_buf(f, sl, len);
587 #ifdef SUPPORT_HARD_LINKS
588 if (tmp_dev != 0 && protocol_version < 30) {
589 if (protocol_version < 26) {
590 /* 32-bit dev_t and ino_t */
591 write_int(f, (int32)dev);
592 write_int(f, (int32)tmp_ino);
594 /* 64-bit dev_t and ino_t */
595 if (!(xflags & XMIT_SAME_DEV_pre30))
596 write_longint(f, dev);
597 write_longint(f, tmp_ino);
602 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
607 /* Prior to 28, we sent a useless set of nulls. */
610 write_buf(f, sum, checksum_len);
614 strlcpy(lastname, fname, MAXPATHLEN);
616 if (S_ISREG(mode) || S_ISLNK(mode))
617 stats.total_size += F_LENGTH(file);
620 static struct file_struct *recv_file_entry(struct file_list *flist,
623 static int64 modtime;
627 static uint32 rdev_major;
630 static uint16 gid_flags;
631 static char lastname[MAXPATHLEN], *lastdir;
632 static int lastdir_depth, lastdir_len = -1;
633 static unsigned int del_hier_name_len = 0;
634 static int in_del_hier = 0;
635 char thisname[MAXPATHLEN];
636 unsigned int l1 = 0, l2 = 0;
637 int alloc_len, basename_len, linkname_len;
638 int extra_len = file_extra_cnt * EXTRA_LEN;
639 int first_hlink_ndx = -1;
641 const char *basename;
642 struct file_struct *file;
646 if (xflags & XMIT_SAME_NAME)
649 if (xflags & XMIT_LONG_NAME)
650 l2 = read_varint30(f);
654 if (l2 >= MAXPATHLEN - l1) {
656 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
657 xflags, l1, l2, lastname, who_am_i());
658 overflow_exit("recv_file_entry");
661 strlcpy(thisname, lastname, l1 + 1);
662 read_sbuf(f, &thisname[l1], l2);
663 thisname[l1 + l2] = 0;
665 /* Abuse basename_len for a moment... */
666 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
669 if (ic_recv != (iconv_t)-1) {
672 INIT_CONST_XBUF(outbuf, thisname);
673 INIT_XBUF(inbuf, lastname, basename_len, -1);
675 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
676 io_error |= IOERR_GENERAL;
678 "[%s] cannot convert filename: %s (%s)\n",
679 who_am_i(), lastname, strerror(errno));
682 outbuf.buf[outbuf.len] = '\0';
686 clean_fname(thisname, 0);
689 sanitize_path(thisname, thisname, "", 0, NULL);
691 if ((basename = strrchr(thisname, '/')) != NULL) {
692 int len = basename++ - thisname;
693 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
694 lastdir = new_array(char, len + 1);
695 memcpy(lastdir, thisname, len);
698 lastdir_depth = count_dir_elements(lastdir);
702 basename_len = strlen(basename) + 1; /* count the '\0' */
704 #ifdef SUPPORT_HARD_LINKS
705 if (protocol_version >= 30
706 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
707 first_hlink_ndx = read_varint(f);
708 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
710 "hard-link reference out of range: %d (%d)\n",
711 first_hlink_ndx, flist->ndx_start + flist->used);
712 exit_cleanup(RERR_PROTOCOL);
714 if (first_hlink_ndx >= flist->ndx_start) {
715 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
716 file_length = F_LENGTH(first);
717 modtime = first->modtime;
720 uid = F_OWNER(first);
722 gid = F_GROUP(first);
723 if ((preserve_devices && IS_DEVICE(mode))
724 || (preserve_specials && IS_SPECIAL(mode))) {
725 uint32 *devp = F_RDEV_P(first);
726 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
727 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
729 if (preserve_links && S_ISLNK(mode))
730 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 += (inc_recurse+1) * 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 >= flist->ndx_start) {
928 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
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 if (xflags & XMIT_HLINK_FIRST) {
941 high_hlink_ndx = flist->ndx_start + flist->used;
942 F_HL_GNUM(file) = high_hlink_ndx;
944 F_HL_GNUM(file) = first_hlink_ndx;
946 static int32 cnt = 0;
947 struct ht_int64_node *np;
950 if (protocol_version < 26) {
954 if (!(xflags & XMIT_SAME_DEV_pre30))
955 dev = read_longint(f);
956 ino = read_longint(f);
958 np = idev_find(dev, ino);
959 ndx = (int32)(long)np->data - 1;
962 np->data = (void*)(long)cnt;
964 F_HL_GNUM(file) = ndx;
969 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
973 /* Prior to 28, we get a useless set of nulls. */
976 if (first_hlink_ndx >= flist->ndx_start) {
977 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
978 memcpy(bp, F_SUM(first), checksum_len);
980 read_buf(f, bp, checksum_len);
984 if (preserve_acls && !S_ISLNK(mode))
985 receive_acl(file, f);
987 #ifdef SUPPORT_XATTRS
989 receive_xattr(file, f );
992 if (S_ISREG(mode) || S_ISLNK(mode))
993 stats.total_size += file_length;
998 /* Create a file_struct for a named file by reading its stat() information
999 * and performing extensive checks against global options.
1001 * Returns a pointer to the new file struct, or NULL if there was an error
1002 * or this file should be excluded. */
1003 struct file_struct *make_file(const char *fname, struct file_list *flist,
1004 STRUCT_STAT *stp, int flags, int filter_level)
1006 static char *lastdir;
1007 static int lastdir_len = -1;
1008 struct file_struct *file;
1009 char thisname[MAXPATHLEN];
1010 char linkname[MAXPATHLEN];
1011 int alloc_len, basename_len, linkname_len;
1012 int extra_len = file_extra_cnt * EXTRA_LEN;
1013 const char *basename;
1018 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1019 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1022 clean_fname(thisname, 0);
1024 sanitize_path(thisname, thisname, "", 0, NULL);
1026 if (stp && S_ISDIR(stp->st_mode)) {
1027 st = *stp; /* Needed for "symlink/." with --relative. */
1028 *linkname = '\0'; /* make IBM code checker happy */
1029 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1030 int save_errno = errno;
1031 /* See if file is excluded before reporting an error. */
1032 if (filter_level != NO_FILTERS
1033 && (is_excluded(thisname, 0, filter_level)
1034 || is_excluded(thisname, 1, filter_level))) {
1035 if (ignore_perishable && save_errno != ENOENT)
1036 non_perishable_cnt++;
1039 if (save_errno == ENOENT) {
1040 #ifdef SUPPORT_LINKS
1041 /* Avoid "vanished" error if symlink points nowhere. */
1042 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1043 && S_ISLNK(st.st_mode)) {
1044 io_error |= IOERR_GENERAL;
1045 rprintf(FERROR, "symlink has no referent: %s\n",
1046 full_fname(thisname));
1050 enum logcode c = am_daemon && protocol_version < 28
1052 io_error |= IOERR_VANISHED;
1053 rprintf(c, "file has vanished: %s\n",
1054 full_fname(thisname));
1057 io_error |= IOERR_GENERAL;
1058 rsyserr(FERROR, save_errno, "readlink %s failed",
1059 full_fname(thisname));
1064 /* backup.c calls us with filter_level set to NO_FILTERS. */
1065 if (filter_level == NO_FILTERS)
1068 if (S_ISDIR(st.st_mode)) {
1070 rprintf(FINFO, "skipping directory %s\n", thisname);
1074 flags &= ~FLAG_XFER_DIR;
1076 /* -x only affects directories because we need to avoid recursing
1077 * into a mount-point directory, not to avoid copying a symlinked
1078 * file if -L (or similar) was specified. */
1079 if (one_file_system && st.st_dev != filesystem_dev
1080 && S_ISDIR(st.st_mode)) {
1081 if (one_file_system > 1) {
1083 rprintf(FINFO, "skipping mount-point dir %s\n",
1088 flags |= FLAG_MOUNT_DIR;
1091 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1092 if (ignore_perishable)
1093 non_perishable_cnt++;
1097 if (lp_ignore_nonreadable(module_id)) {
1098 #ifdef SUPPORT_LINKS
1099 if (!S_ISLNK(st.st_mode))
1101 if (access(thisname, R_OK) != 0)
1107 /* Only divert a directory in the main transfer. */
1109 if (flist->prev && S_ISDIR(st.st_mode)
1110 && flags & FLAG_DIVERT_DIRS) {
1111 /* Room for parent/sibling/next-child info. */
1112 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1114 pool = dir_flist->file_pool;
1116 pool = flist->file_pool;
1121 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1122 who_am_i(), thisname, filter_level);
1125 if ((basename = strrchr(thisname, '/')) != NULL) {
1126 int len = basename++ - thisname;
1127 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1128 lastdir = new_array(char, len + 1);
1129 memcpy(lastdir, thisname, len);
1130 lastdir[len] = '\0';
1134 basename = thisname;
1135 basename_len = strlen(basename) + 1; /* count the '\0' */
1137 #ifdef SUPPORT_LINKS
1138 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1143 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1144 extra_len += EXTRA_LEN;
1146 #if EXTRA_ROUNDING > 0
1147 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1148 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1151 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1154 bp = pool_alloc(pool, alloc_len, "make_file");
1156 if (!(bp = new_array(char, alloc_len)))
1157 out_of_memory("make_file");
1160 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1162 file = (struct file_struct *)bp;
1163 bp += FILE_STRUCT_LEN;
1165 memcpy(bp, basename, basename_len);
1166 bp += basename_len + linkname_len; /* skip space for symlink too */
1168 #ifdef SUPPORT_HARD_LINKS
1169 if (preserve_hard_links && flist && flist->prev) {
1170 if (protocol_version >= 28
1171 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1172 : S_ISREG(st.st_mode)) {
1173 tmp_dev = st.st_dev;
1174 tmp_ino = st.st_ino;
1180 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1181 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1182 tmp_rdev = st.st_rdev;
1187 file->flags = flags;
1188 file->modtime = st.st_mtime;
1189 file->len32 = (uint32)st.st_size;
1190 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1191 file->flags |= FLAG_LENGTH64;
1192 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1194 file->mode = st.st_mode;
1196 F_OWNER(file) = st.st_uid;
1198 F_GROUP(file) = st.st_gid;
1200 if (basename != thisname)
1201 file->dirname = lastdir;
1203 #ifdef SUPPORT_LINKS
1205 bp = (char*)file->basename + basename_len;
1206 memcpy(bp, linkname, linkname_len);
1210 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1211 file_checksum(thisname, tmp_sum, st.st_size);
1213 F_PATHNAME(file) = pathname;
1215 /* This code is only used by the receiver when it is building
1216 * a list of files for a delete pass. */
1217 if (keep_dirlinks && linkname_len && flist) {
1219 int save_mode = file->mode;
1220 file->mode = S_IFDIR; /* Find a directory with our name. */
1221 if (flist_find(dir_flist, file) >= 0
1222 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1223 file->modtime = st2.st_mtime;
1225 file->mode = st2.st_mode;
1227 F_OWNER(file) = st2.st_uid;
1229 F_GROUP(file) = st2.st_gid;
1231 file->mode = save_mode;
1234 if (basename_len == 0+1)
1239 F_NDX(file) = dir_count - 1;
1245 /* Only called for temporary file_struct entries created by make_file(). */
1246 void unmake_file(struct file_struct *file)
1248 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1249 #if EXTRA_ROUNDING > 0
1250 if (extra_cnt & EXTRA_ROUNDING)
1251 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1253 free(REQ_EXTRA(file, extra_cnt));
1256 static struct file_struct *send_file_name(int f, struct file_list *flist,
1257 char *fname, STRUCT_STAT *stp,
1258 int flags, int filter_level)
1260 struct file_struct *file;
1261 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1265 file = make_file(fname, flist, stp, flags, filter_level);
1269 if (chmod_modes && !S_ISLNK(file->mode))
1270 file->mode = tweak_mode(file->mode, chmod_modes);
1273 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1274 sx.st.st_mode = file->mode;
1275 sx.acc_acl = sx.def_acl = NULL;
1276 if (get_acl(fname, &sx) < 0)
1280 #ifdef SUPPORT_XATTRS
1281 if (preserve_xattrs && f >= 0) {
1283 if (get_xattr(fname, &sx) < 0)
1288 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1290 flist_expand(flist, 1);
1291 flist->files[flist->used++] = file;
1293 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1295 if (preserve_acls && !S_ISLNK(file->mode)) {
1300 #ifdef SUPPORT_XATTRS
1301 if (preserve_xattrs) {
1302 F_XATTR(file) = send_xattr(&sx, f);
1310 static void send_if_directory(int f, struct file_list *flist,
1311 struct file_struct *file,
1312 char *fbuf, unsigned int ol,
1315 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1317 if (S_ISDIR(file->mode)
1318 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1320 unsigned int len = strlen(fbuf);
1321 if (len > 1 && fbuf[len-1] == '/')
1323 if (len >= MAXPATHLEN - 1) {
1324 io_error |= IOERR_GENERAL;
1325 rprintf(FERROR, "skipping long-named directory: %s\n",
1329 save_filters = push_local_filters(fbuf, len);
1330 send_directory(f, flist, fbuf, len, flags);
1331 pop_local_filters(save_filters);
1338 static int file_compare(const void *file1, const void *file2)
1340 return f_name_cmp(*(struct file_struct **)file1,
1341 *(struct file_struct **)file2);
1344 /* The guts of a merge-sort algorithm. This was derived from the glibc
1345 * version, but I (Wayne) changed the merge code to do less copying and
1346 * to require only half the amount of temporary memory. */
1347 static void fsort_tmp(struct file_struct **fp, size_t num,
1348 struct file_struct **tmp)
1350 struct file_struct **f1, **f2, **t;
1359 fsort_tmp(f1, n1, tmp);
1361 fsort_tmp(f2, n2, tmp);
1363 while (f_name_cmp(*f1, *f2) <= 0) {
1370 memcpy(t, f1, n1 * PTR_SIZE);
1372 *f1++ = *f2++, n2--;
1374 while (n1 > 0 && n2 > 0) {
1375 if (f_name_cmp(*t, *f2) <= 0)
1378 *f1++ = *f2++, n2--;
1382 memcpy(f1, t, n1 * PTR_SIZE);
1385 /* This file-struct sorting routine makes sure that any identical names in
1386 * the file list stay in the same order as they were in the original list.
1387 * This is particularly vital in inc_recurse mode where we expect a sort
1388 * on the flist to match the exact order of a sort on the dir_flist. */
1389 static void fsort(struct file_struct **fp, size_t num)
1395 qsort(fp, num, PTR_SIZE, file_compare);
1397 struct file_struct **tmp = new_array(struct file_struct *,
1399 fsort_tmp(fp, num, tmp);
1404 /* We take an entire set of sibling dirs from the sorted flist and link them
1405 * into the tree, setting the appropriate parent/child/sibling pointers. */
1406 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1411 int32 *parent_dp = parent_ndx < 0 ? NULL
1412 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1414 flist_expand(dir_flist, dir_cnt);
1415 dir_flist->sorted = dir_flist->files;
1417 for (i = 0; dir_cnt; i++) {
1418 struct file_struct *file = from_flist->sorted[i];
1420 if (!S_ISDIR(file->mode))
1423 dir_flist->files[dir_flist->used++] = file;
1426 if (!(file->flags & FLAG_XFER_DIR)
1427 || file->flags & FLAG_MOUNT_DIR)
1431 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1433 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1435 send_dir_ndx = dir_flist->used - 1;
1437 dp = F_DIR_NODE_P(file);
1438 DIR_PARENT(dp) = parent_ndx;
1439 DIR_FIRST_CHILD(dp) = -1;
1442 DIR_NEXT_SIBLING(dp) = -1;
1445 /* This function is normally called by the sender, but the receiving side also
1446 * calls it from get_dirlist() with f set to -1 so that we just construct the
1447 * file list in memory without sending it over the wire. Also, get_dirlist()
1448 * might call this with f set to -2, which also indicates that local filter
1449 * rules should be ignored. */
1450 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1457 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1458 int start = flist->used;
1459 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1461 assert(flist != NULL);
1463 if (!(d = opendir(fbuf))) {
1464 io_error |= IOERR_GENERAL;
1465 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1470 if (len != 1 || *fbuf != '/')
1473 remainder = MAXPATHLEN - (p - fbuf);
1475 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1476 char *dname = d_name(di);
1477 if (dname[0] == '.' && (dname[1] == '\0'
1478 || (dname[1] == '.' && dname[2] == '\0')))
1480 if (strlcpy(p, dname, remainder) >= remainder) {
1481 io_error |= IOERR_GENERAL;
1483 "cannot send long-named file %s\n",
1487 if (dname[0] == '\0') {
1488 io_error |= IOERR_GENERAL;
1490 "cannot send file with empty name in %s\n",
1495 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1501 io_error |= IOERR_GENERAL;
1502 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1507 if (f >= 0 && recurse && !divert_dirs) {
1508 int i, end = flist->used - 1;
1509 /* send_if_directory() bumps flist->used, so use "end". */
1510 for (i = start; i <= end; i++)
1511 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1515 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1517 char fbuf[MAXPATHLEN];
1521 dlen = strlen(fbuf);
1523 if (F_PATHNAME(file) != pathname) {
1524 if (!push_pathname(F_PATHNAME(file), -1))
1525 exit_cleanup(RERR_FILESELECT);
1528 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1530 send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1533 void send_extra_file_list(int f, int at_least)
1535 struct file_list *flist;
1537 int future_cnt, save_io_error = io_error;
1542 /* Keep sending data until we have the requested number of
1543 * files in the upcoming file-lists. */
1544 if (cur_flist->next) {
1545 flist = first_flist->prev; /* the newest flist */
1546 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1549 while (future_cnt < at_least) {
1550 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1551 int dir_ndx, dstart = dir_count;
1552 const char *pathname = F_PATHNAME(file);
1555 flist = flist_new(0, "send_extra_file_list");
1556 start_write = stats.total_written;
1560 dir_ndx = F_NDX(file);
1563 dir_ndx = send_dir_ndx;
1564 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1565 flist->parent_ndx = dir_ndx;
1567 send1extra(f, file, flist);
1568 dp = F_DIR_NODE_P(file);
1570 /* If there are any duplicate directory names that follow, we
1571 * send all the dirs together in one file-list. The dir_flist
1572 * tree links all the child subdirs onto the last dup dir. */
1573 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1574 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1575 send_dir_ndx = dir_ndx;
1576 file = dir_flist->sorted[dir_ndx];
1577 if (F_PATHNAME(file) != pathname)
1578 send1extra(f, file, flist);
1579 dp = F_DIR_NODE_P(file);
1585 if (need_unsorted_flist) {
1586 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1587 out_of_memory("send_extra_file_list");
1588 memcpy(flist->sorted, flist->files,
1589 flist->used * sizeof (struct file_struct*));
1592 flist->sorted = flist->files;
1594 clean_flist(flist, 0);
1596 flist->ndx_end = flist->ndx_start + flist->used - 1
1597 - (dir_count - dstart);
1599 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1600 flist_done_allocating(flist);
1602 file_total += flist->used;
1603 future_cnt += flist->used;
1604 stats.flist_size += stats.total_written - start_write;
1605 stats.num_files += flist->used;
1607 output_flist(flist);
1609 if (DIR_FIRST_CHILD(dp) >= 0) {
1610 send_dir_ndx = DIR_FIRST_CHILD(dp);
1613 while (DIR_NEXT_SIBLING(dp) < 0) {
1614 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1615 write_ndx(f, NDX_FLIST_EOF);
1617 change_local_filter_dir(NULL, 0, 0);
1621 file = dir_flist->sorted[send_dir_ndx];
1622 dp = F_DIR_NODE_P(file);
1624 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1629 if (io_error != save_io_error && !ignore_errors)
1630 send_msg_int(MSG_IO_ERROR, io_error);
1633 struct file_list *send_file_list(int f, int argc, char *argv[])
1635 static const char *lastdir;
1636 static int lastdir_len = -1;
1640 char lastpath[MAXPATHLEN] = "";
1641 struct file_list *flist;
1642 struct timeval start_tv, end_tv;
1645 int flags, disable_buffering;
1646 int reading_remotely = filesfrom_host != NULL;
1647 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1648 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0)
1649 | (filesfrom_convert ? RL_CONVERT : 0);
1651 rprintf(FLOG, "building file list\n");
1652 if (show_filelist_p())
1653 start_filelist_progress("building file list");
1654 else if (inc_recurse && verbose && !am_server)
1655 rprintf(FCLIENT, "sending incremental file list\n");
1657 start_write = stats.total_written;
1658 gettimeofday(&start_tv, NULL);
1660 #ifdef SUPPORT_HARD_LINKS
1661 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1665 flist = cur_flist = flist_new(0, "send_file_list");
1667 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1668 flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1670 dir_flist = cur_flist;
1671 flags = FLAG_XFER_DIR;
1674 disable_buffering = io_start_buffering_out(f);
1675 if (filesfrom_fd >= 0) {
1676 if (argv[0] && !push_dir(argv[0], 0)) {
1677 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1678 full_fname(argv[0]), curr_dir);
1679 exit_cleanup(RERR_FILESELECT);
1685 char fbuf[MAXPATHLEN];
1690 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1692 sanitize_path(fbuf, fbuf, "", 0, NULL);
1696 strlcpy(fbuf, *argv++, MAXPATHLEN);
1698 sanitize_path(fbuf, fbuf, "", 0, NULL);
1702 if (relative_paths) {
1703 /* We clean up fbuf below. */
1705 } else if (!len || fbuf[len - 1] == '/') {
1706 if (len == 2 && fbuf[0] == '.') {
1707 /* Turn "./" into just "." rather than "./." */
1710 if (len + 1 >= MAXPATHLEN)
1711 overflow_exit("send_file_list");
1716 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1717 && (len == 2 || fbuf[len-3] == '/')) {
1718 if (len + 2 >= MAXPATHLEN)
1719 overflow_exit("send_file_list");
1725 is_dot_dir = fbuf[len-1] == '.'
1726 && (len == 1 || fbuf[len-2] == '/');
1731 if (!relative_paths) {
1732 p = strrchr(fbuf, '/');
1739 len -= p - fbuf + 1;
1744 if ((p = strstr(fbuf, "/./")) != NULL) {
1750 len -= p - fbuf + 3;
1754 /* Get rid of trailing "/" and "/.". */
1756 if (fn[len - 1] == '/') {
1758 if (!--len && !dir) {
1763 else if (len >= 2 && fn[len - 1] == '.'
1764 && fn[len - 2] == '/') {
1766 if (!(len -= 2) && !dir) {
1773 if (len == 1 && fn[0] == '/')
1776 /* Reject a ".." dir in the active part of the path. */
1777 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1778 if ((p[2] == '/' || p[2] == '\0')
1779 && (p == fn || p[-1] == '/')) {
1781 "found \"..\" dir in relative path: %s\n",
1783 exit_cleanup(RERR_SYNTAX);
1793 dirlen = dir ? strlen(dir) : 0;
1794 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1795 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1798 lastdir_len = pathname_len;
1799 } else if (!push_pathname(lastdir, lastdir_len))
1803 memmove(fbuf, fn, len + 1);
1805 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1806 io_error |= IOERR_GENERAL;
1807 rsyserr(FERROR, errno, "link_stat %s failed",
1812 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1813 rprintf(FINFO, "skipping directory %s\n", fbuf);
1817 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1818 /* Send the implied directories at the start of the
1819 * source spec, so we get their permissions right. */
1820 char *lp = lastpath, *slash = fbuf;
1822 /* Skip any initial directories in our path that we
1823 * have in common with lastpath. */
1824 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1829 if (fn != p || (*lp && *lp != '/')) {
1830 int save_copy_links = copy_links;
1831 int save_xfer_dirs = xfer_dirs;
1832 int dir_flags = flags & ~FLAG_XFER_DIR;
1833 copy_links |= copy_unsafe_links;
1835 while ((slash = strchr(slash+1, '/')) != 0) {
1837 send_file_name(f, flist, fbuf, NULL,
1838 dir_flags, ALL_FILTERS);
1841 copy_links = save_copy_links;
1842 xfer_dirs = save_xfer_dirs;
1844 strlcpy(lastpath, fbuf, sizeof lastpath);
1849 if (one_file_system)
1850 filesystem_dev = st.st_dev;
1852 if (recurse || (xfer_dirs && is_dot_dir)) {
1853 struct file_struct *file;
1854 int top_flags = FLAG_TOP_DIR | flags;
1855 file = send_file_name(f, flist, fbuf, &st,
1856 top_flags, ALL_FILTERS);
1857 if (file && !inc_recurse)
1858 send_if_directory(f, flist, file, fbuf, len, flags);
1860 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1863 gettimeofday(&end_tv, NULL);
1864 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1865 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1866 if (stats.flist_buildtime == 0)
1867 stats.flist_buildtime = 1;
1870 write_byte(f, 0); /* Indicate end of file list */
1872 #ifdef SUPPORT_HARD_LINKS
1873 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1877 if (show_filelist_p())
1878 finish_filelist_progress(flist);
1880 gettimeofday(&end_tv, NULL);
1881 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1882 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1884 /* When converting names, both sides keep an unsorted file-list array
1885 * because the names will differ on the sending and receiving sides
1886 * (both sides will use the unsorted index number for each item). */
1888 /* Sort the list without removing any duplicates. This allows the
1889 * receiving side to ask for whatever name it kept. For incremental
1890 * recursion mode, the sender marks duplicate dirs so that it can
1891 * send them together in a single file-list. */
1893 if (need_unsorted_flist) {
1895 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1896 out_of_memory("send_file_list");
1897 memcpy(flist->sorted, flist->files,
1898 flist->used * sizeof (struct file_struct*));
1899 clean_flist(flist, 0);
1901 flist->sorted = flist->files;
1903 flist->high = flist->used - 1;
1908 flist->sorted = flist->files;
1909 clean_flist(flist, 0);
1911 file_total += flist->used;
1913 /* We don't subtract dir_count for the first send since we
1914 * might have one or more dot dirs which need to get sent. */
1915 flist->ndx_end = flist->ndx_start + flist->used - 1;
1917 if (!numeric_ids && !inc_recurse)
1920 /* send the io_error flag */
1921 if (protocol_version < 30)
1922 write_int(f, ignore_errors ? 0 : io_error);
1923 else if (io_error && !ignore_errors)
1924 send_msg_int(MSG_IO_ERROR, io_error);
1926 if (disable_buffering)
1927 io_end_buffering_out();
1929 stats.flist_size = stats.total_written - start_write;
1930 stats.num_files = flist->used;
1933 output_flist(flist);
1936 rprintf(FINFO, "send_file_list done\n");
1939 add_dirs_to_tree(-1, flist, dir_count);
1940 flist_done_allocating(flist);
1941 if (send_dir_ndx < 0) {
1942 write_ndx(f, NDX_FLIST_EOF);
1945 else if (file_total == 1) {
1946 /* If we're creating incremental file-lists and there
1947 * was just 1 item in the first file-list, send 1 more
1948 * file-list to check if this is a 1-file xfer. */
1949 send_extra_file_list(f, 1);
1956 struct file_list *recv_file_list(int f)
1958 struct file_list *flist;
1963 rprintf(FLOG, "receiving file list\n");
1964 if (show_filelist_p())
1965 start_filelist_progress("receiving file list");
1966 else if (inc_recurse && verbose && !am_server && !first_flist)
1967 rprintf(FCLIENT, "receiving incremental file list\n");
1969 start_read = stats.total_read;
1971 #ifdef SUPPORT_HARD_LINKS
1972 if (preserve_hard_links && !first_flist)
1976 flist = flist_new(0, "recv_file_list");
1979 if (flist->ndx_start == 0)
1980 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1981 dstart = dir_flist->used;
1987 while ((flags = read_byte(f)) != 0) {
1988 struct file_struct *file;
1990 flist_expand(flist, 1);
1992 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1993 flags |= read_byte(f) << 8;
1994 file = recv_file_entry(flist, flags, f);
1996 if (inc_recurse && S_ISDIR(file->mode)) {
1997 flist_expand(dir_flist, 1);
1998 dir_flist->files[dir_flist->used++] = file;
2001 flist->files[flist->used++] = file;
2003 maybe_emit_filelist_progress(flist->used);
2006 rprintf(FINFO, "recv_file_name(%s)\n",
2007 f_name(file, NULL));
2010 file_total += flist->used;
2012 flist->ndx_end = flist->ndx_start + flist->used - 1;
2013 if (inc_recurse && flist->ndx_start)
2014 flist->ndx_end -= dir_flist->used - dstart;
2017 rprintf(FINFO, "received %d names\n", flist->used);
2019 if (show_filelist_p())
2020 finish_filelist_progress(flist);
2023 if (need_unsorted_flist) {
2024 /* Create an extra array of index pointers that we can sort for
2025 * the generator's use (for wading through the files in sorted
2026 * order and for calling flist_find()). We keep the "files"
2027 * list unsorted for our exchange of index numbers with the
2028 * other side (since their names may not sort the same). */
2029 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2030 out_of_memory("recv_file_list");
2031 memcpy(flist->sorted, flist->files,
2032 flist->used * sizeof (struct file_struct*));
2033 if (inc_recurse && dir_flist->used > dstart) {
2034 dir_flist->sorted = realloc_array(dir_flist->sorted,
2035 struct file_struct *,
2037 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2038 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2039 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2044 flist->sorted = flist->files;
2045 if (inc_recurse && dir_flist->used > dstart) {
2046 dir_flist->sorted = dir_flist->files;
2047 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2052 flist_done_allocating(flist);
2054 recv_id_list(f, flist);
2056 clean_flist(flist, relative_paths);
2058 if (protocol_version < 30) {
2059 /* Recv the io_error flag */
2063 io_error |= read_int(f);
2067 output_flist(flist);
2071 for (i = flist->low; i <= flist->high; i++)
2072 list_file_entry(flist->files[i]);
2076 rprintf(FINFO, "recv_file_list done\n");
2078 stats.flist_size += stats.total_read - start_read;
2079 stats.num_files += flist->used;
2084 /* This is only used once by the receiver if the very first file-list
2085 * has exactly one item in it. */
2086 void recv_additional_file_list(int f)
2088 struct file_list *flist;
2089 int ndx = read_ndx(f);
2090 if (ndx == NDX_FLIST_EOF) {
2092 change_local_filter_dir(NULL, 0, 0);
2094 ndx = NDX_FLIST_OFFSET - ndx;
2095 if (ndx < 0 || ndx >= dir_flist->used) {
2096 ndx = NDX_FLIST_OFFSET - ndx;
2098 "[%s] Invalid dir index: %d (%d - %d)\n",
2099 who_am_i(), ndx, NDX_FLIST_OFFSET,
2100 NDX_FLIST_OFFSET - dir_flist->used + 1);
2101 exit_cleanup(RERR_PROTOCOL);
2104 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2107 flist = recv_file_list(f);
2108 flist->parent_ndx = ndx;
2112 /* Search for an identically-named item in the file list. Note that the
2113 * items must agree in their directory-ness, or no match is returned. */
2114 int flist_find(struct file_list *flist, struct file_struct *f)
2116 int low = flist->low, high = flist->high;
2117 int diff, mid, mid_up;
2119 while (low <= high) {
2120 mid = (low + high) / 2;
2121 if (F_IS_ACTIVE(flist->sorted[mid]))
2124 /* Scan for the next non-empty entry using the cached
2125 * distance values. If the value isn't fully up-to-
2126 * date, update it. */
2127 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2128 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2130 mid_up += F_DEPTH(flist->sorted[mid_up]);
2131 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2132 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2134 if (mid_up > high) {
2135 /* If there's nothing left above us, set high to
2136 * a non-empty entry below us and continue. */
2137 high = mid - (int)flist->sorted[mid]->len32;
2138 if (!F_IS_ACTIVE(flist->sorted[high])) {
2140 high -= (int)flist->sorted[high]->len32;
2141 } while (!F_IS_ACTIVE(flist->sorted[high]));
2142 flist->sorted[mid]->len32 = mid - high;
2147 diff = f_name_cmp(flist->sorted[mid_up], f);
2149 if (protocol_version < 29
2150 && S_ISDIR(flist->sorted[mid_up]->mode)
2151 != S_ISDIR(f->mode))
2164 * Free up any resources a file_struct has allocated
2165 * and clear the file.
2167 void clear_file(struct file_struct *file)
2169 /* The +1 zeros out the first char of the basename. */
2170 memset(file, 0, FILE_STRUCT_LEN + 1);
2171 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2172 * entry. Likewise for len32 in the opposite direction. We assume
2173 * that we're alone for now since flist_find() will adjust the counts
2174 * it runs into that aren't up-to-date. */
2175 file->len32 = F_DEPTH(file) = 1;
2178 /* Allocate a new file list. */
2179 struct file_list *flist_new(int flags, char *msg)
2181 struct file_list *flist;
2183 flist = new0(struct file_list);
2187 if (flags & FLIST_TEMP) {
2188 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2189 out_of_memory, POOL_INTERN)))
2192 /* This is a doubly linked list with prev looping back to
2193 * the end of the list, but the last next pointer is NULL. */
2195 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2196 out_of_memory, POOL_INTERN);
2197 if (!flist->file_pool)
2200 first_flist = cur_flist = flist->prev = flist;
2202 flist->file_pool = first_flist->file_pool;
2204 flist->ndx_start = first_flist->prev->ndx_end + 2;
2205 if (flist->ndx_start <= high_hlink_ndx)
2206 flist->ndx_start = high_hlink_ndx + 1;
2208 flist->prev = first_flist->prev;
2209 flist->prev->next = first_flist->prev = flist;
2211 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2218 /* Free up all elements in a flist. */
2219 void flist_free(struct file_list *flist)
2222 /* Was FLIST_TEMP dir-list. */
2223 } else if (flist == flist->prev) {
2224 first_flist = cur_flist = NULL;
2228 if (flist == cur_flist)
2229 cur_flist = flist->next;
2230 if (flist == first_flist)
2231 first_flist = first_flist->next;
2233 flist->prev->next = flist->next;
2235 flist->next = first_flist;
2237 flist->next->prev = flist->prev;
2238 file_total -= flist->used;
2242 if (!flist->prev || !flist_cnt)
2243 pool_destroy(flist->file_pool);
2245 pool_free_old(flist->file_pool, flist->pool_boundary);
2247 if (flist->sorted && flist->sorted != flist->files)
2248 free(flist->sorted);
2253 /* This routine ensures we don't have any duplicate names in our file list.
2254 * duplicate names can cause corruption because of the pipelining. */
2255 static void clean_flist(struct file_list *flist, int strip_root)
2257 char fbuf[MAXPATHLEN];
2262 if (flist->used == 0) {
2268 fsort(flist->sorted, flist->used);
2270 if (!am_sender || inc_recurse) {
2271 for (i = prev_i = 0; i < flist->used; i++) {
2272 if (F_IS_ACTIVE(flist->sorted[i])) {
2277 flist->low = prev_i;
2279 i = prev_i = flist->used - 1;
2283 while (++i < flist->used) {
2285 struct file_struct *file = flist->sorted[i];
2287 if (!F_IS_ACTIVE(file))
2289 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2291 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2292 int save_mode = file->mode;
2293 /* Make sure that this directory doesn't duplicate a
2294 * non-directory earlier in the list. */
2295 flist->high = prev_i;
2296 file->mode = S_IFREG;
2297 j = flist_find(flist, file);
2298 file->mode = save_mode;
2303 /* If one is a dir and the other is not, we want to
2304 * keep the dir because it might have contents in the
2305 * list. Otherwise keep the first one. */
2306 if (S_ISDIR(file->mode)) {
2307 struct file_struct *fp = flist->sorted[j];
2308 if (!S_ISDIR(fp->mode) || !(fp->flags & FLAG_XFER_DIR))
2312 file->flags |= FLAG_DUPLICATE;
2321 "removing duplicate name %s from file list (%d)\n",
2322 f_name(file, fbuf), drop + flist->ndx_start);
2324 /* Make sure we don't lose track of a user-specified
2326 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2327 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2329 clear_file(flist->sorted[drop]);
2333 if (flist->low == drop) {
2335 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2344 flist->high = prev_i;
2347 /* We need to strip off the leading slashes for relative
2348 * paths, but this must be done _after_ the sorting phase. */
2349 for (i = flist->low; i <= flist->high; i++) {
2350 struct file_struct *file = flist->sorted[i];
2354 while (*file->dirname == '/')
2356 if (!*file->dirname)
2357 file->dirname = NULL;
2361 if (prune_empty_dirs && !am_sender) {
2362 int j, prev_depth = 0;
2364 prev_i = 0; /* It's OK that this isn't really true. */
2366 for (i = flist->low; i <= flist->high; i++) {
2367 struct file_struct *fp, *file = flist->sorted[i];
2369 /* This temporarily abuses the F_DEPTH() value for a
2370 * directory that is in a chain that might get pruned.
2371 * We restore the old value if it gets a reprieve. */
2372 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2373 /* Dump empty dirs when coming back down. */
2374 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2375 fp = flist->sorted[prev_i];
2376 if (F_DEPTH(fp) >= 0)
2378 prev_i = -F_DEPTH(fp)-1;
2381 prev_depth = F_DEPTH(file);
2382 if (is_excluded(f_name(file, fbuf), 1,
2384 /* Keep dirs through this dir. */
2385 for (j = prev_depth-1; ; j--) {
2386 fp = flist->sorted[prev_i];
2387 if (F_DEPTH(fp) >= 0)
2389 prev_i = -F_DEPTH(fp)-1;
2393 F_DEPTH(file) = -prev_i-1;
2396 /* Keep dirs through this non-dir. */
2397 for (j = prev_depth; ; j--) {
2398 fp = flist->sorted[prev_i];
2399 if (F_DEPTH(fp) >= 0)
2401 prev_i = -F_DEPTH(fp)-1;
2406 /* Dump all remaining empty dirs. */
2408 struct file_struct *fp = flist->sorted[prev_i];
2409 if (F_DEPTH(fp) >= 0)
2411 prev_i = -F_DEPTH(fp)-1;
2415 for (i = flist->low; i <= flist->high; i++) {
2416 if (F_IS_ACTIVE(flist->sorted[i]))
2420 for (i = flist->high; i >= flist->low; i--) {
2421 if (F_IS_ACTIVE(flist->sorted[i]))
2428 static void output_flist(struct file_list *flist)
2430 char uidbuf[16], gidbuf[16], depthbuf[16];
2431 struct file_struct *file;
2432 const char *root, *dir, *slash, *name, *trail;
2433 const char *who = who_am_i();
2436 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2437 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2438 for (i = 0; i < flist->used; i++) {
2439 file = flist->sorted[i];
2440 if ((am_root || am_sender) && uid_ndx) {
2441 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2446 static char parens[] = "(\0)\0\0\0";
2447 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2448 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2449 pp, F_GROUP(file), pp + 2);
2453 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2454 if (F_IS_ACTIVE(file)) {
2455 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2456 if ((dir = file->dirname) == NULL)
2460 name = file->basename;
2461 trail = S_ISDIR(file->mode) ? "/" : "";
2463 root = dir = slash = name = trail = "";
2465 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2466 who, i + flist->ndx_start,
2467 root, dir, slash, name, trail,
2468 (int)file->mode, (double)F_LENGTH(file),
2469 uidbuf, gidbuf, file->flags);
2473 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2474 enum fnc_type { t_PATH, t_ITEM };
2476 static int found_prefix;
2478 /* Compare the names of two file_struct entities, similar to how strcmp()
2479 * would do if it were operating on the joined strings.
2481 * Some differences beginning with protocol_version 29: (1) directory names
2482 * are compared with an assumed trailing slash so that they compare in a
2483 * way that would cause them to sort immediately prior to any content they
2484 * may have; (2) a directory of any name compares after a non-directory of
2485 * any name at the same depth; (3) a directory with name "." compares prior
2486 * to anything else. These changes mean that a directory and a non-dir
2487 * with the same name will not compare as equal (protocol_version >= 29).
2489 * The dirname component can be an empty string, but the basename component
2490 * cannot (and never is in the current codebase). The basename component
2491 * may be NULL (for a removed item), in which case it is considered to be
2492 * after any existing item. */
2493 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2496 const uchar *c1, *c2;
2497 enum fnc_state state1, state2;
2498 enum fnc_type type1, type2;
2499 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2501 if (!f1 || !F_IS_ACTIVE(f1)) {
2502 if (!f2 || !F_IS_ACTIVE(f2))
2506 if (!f2 || !F_IS_ACTIVE(f2))
2509 c1 = (uchar*)f1->dirname;
2510 c2 = (uchar*)f2->dirname;
2514 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2515 c1 = (const uchar*)f1->basename;
2516 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2518 state1 = s_TRAILING;
2527 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2528 c2 = (const uchar*)f2->basename;
2529 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2531 state2 = s_TRAILING;
2541 return type1 == t_PATH ? 1 : -1;
2551 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2552 c1 = (const uchar*)f1->basename;
2553 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2555 state1 = s_TRAILING;
2561 state1 = s_TRAILING;
2562 if (type1 == t_PATH) {
2571 if (*c2 && type1 != type2)
2572 return type1 == t_PATH ? 1 : -1;
2581 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2582 c2 = (const uchar*)f2->basename;
2583 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2585 state2 = s_TRAILING;
2591 state2 = s_TRAILING;
2592 if (type2 == t_PATH) {
2605 return type1 == t_PATH ? 1 : -1;
2607 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2612 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2613 * not match if f2's basename is not an exact match of a path element in f1.
2614 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2615 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2619 return found_prefix;
2622 char *f_name_buf(void)
2624 static char names[5][MAXPATHLEN];
2625 static unsigned int n;
2627 n = (n + 1) % (sizeof names / sizeof names[0]);
2632 /* Return a copy of the full filename of a flist entry, using the indicated
2633 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2634 * done because we checked the size when creating the file_struct entry.
2636 char *f_name(const struct file_struct *f, char *fbuf)
2638 if (!f || !F_IS_ACTIVE(f))
2642 fbuf = f_name_buf();
2645 int len = strlen(f->dirname);
2646 memcpy(fbuf, f->dirname, len);
2648 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2650 strlcpy(fbuf, f->basename, MAXPATHLEN);
2655 /* Do a non-recursive scan of the named directory, possibly ignoring all
2656 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2657 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2658 * buffer (the functions we call will append names onto the end, but the old
2659 * dir value will be restored on exit). */
2660 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2662 struct file_list *dirlist;
2663 char dirbuf[MAXPATHLEN];
2664 int save_recurse = recurse;
2665 int save_xfer_dirs = xfer_dirs;
2666 int save_prune_empty_dirs = prune_empty_dirs;
2669 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2670 if (dlen >= MAXPATHLEN)
2675 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2679 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2680 xfer_dirs = save_xfer_dirs;
2681 recurse = save_recurse;
2683 flist_count_offset += dirlist->used;
2685 prune_empty_dirs = 0;
2686 dirlist->sorted = dirlist->files;
2687 clean_flist(dirlist, 0);
2688 prune_empty_dirs = save_prune_empty_dirs;
2691 output_flist(dirlist);