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 /* Stat either a symlink or its referent, depending on the settings of
175 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
177 * If path is the name of a symlink, then the linkbuf buffer (which must hold
178 * MAXPATHLEN chars) will be set to the symlink's target string.
180 * The stat structure pointed to by stp will contain information about the
181 * link or the referent as appropriate, if they exist. */
182 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
185 if (link_stat(path, stp, copy_dirlinks) < 0)
187 if (S_ISLNK(stp->st_mode)) {
188 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
191 linkbuf[llen] = '\0';
192 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
194 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
197 return x_stat(path, stp, NULL);
202 return x_stat(path, stp, NULL);
206 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
210 return x_stat(path, stp, NULL);
211 if (x_lstat(path, stp, NULL) < 0)
213 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
215 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
220 return x_stat(path, stp, NULL);
224 /* This function is used to check if a file should be included/excluded
225 * from the list of files based on its name and type etc. The value of
226 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
227 static int is_excluded(char *fname, int is_dir, int filter_level)
229 #if 0 /* This currently never happens, so avoid a useless compare. */
230 if (filter_level == NO_FILTERS)
234 /* never exclude '.', even if somebody does --exclude '*' */
235 if (fname[0] == '.' && !fname[1])
237 /* Handle the -R version of the '.' dir. */
238 if (fname[0] == '/') {
239 int len = strlen(fname);
240 if (fname[len-1] == '.' && fname[len-2] == '/')
244 if (server_filter_list.head
245 && check_filter(&server_filter_list, fname, is_dir) < 0)
247 if (filter_level != ALL_FILTERS)
250 && check_filter(&filter_list, fname, is_dir) < 0)
255 static void send_directory(int f, struct file_list *flist,
256 char *fbuf, int len, int flags);
258 static const char *pathname, *orig_dir;
259 static int pathname_len;
262 /* Make sure flist can hold at least flist->used + extra entries. */
263 static void flist_expand(struct file_list *flist, int extra)
265 struct file_struct **new_ptr;
267 if (flist->used + extra <= flist->malloced)
270 if (flist->malloced < FLIST_START)
271 flist->malloced = FLIST_START;
272 else if (flist->malloced >= FLIST_LINEAR)
273 flist->malloced += FLIST_LINEAR;
275 flist->malloced *= 2;
277 /* In case count jumped or we are starting the list
278 * with a known size just set it. */
279 if (flist->malloced < flist->used + extra)
280 flist->malloced = flist->used + extra;
282 new_ptr = realloc_array(flist->files, struct file_struct *,
285 if (verbose >= 2 && flist->malloced != FLIST_START) {
286 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
288 (double)sizeof flist->files[0] * flist->malloced,
289 (new_ptr == flist->files) ? " not" : "");
292 flist->files = new_ptr;
295 out_of_memory("flist_expand");
298 static void flist_done_allocating(struct file_list *flist)
300 void *ptr = pool_boundary(flist->file_pool, 8*1024);
301 if (flist->pool_boundary == ptr)
302 flist->pool_boundary = NULL; /* list didn't use any pool memory */
304 flist->pool_boundary = ptr;
307 int push_pathname(const char *dir, int len)
313 orig_dir = strdup(curr_dir);
315 if (pathname && !pop_dir(orig_dir)) {
316 rsyserr(FERROR, errno, "pop_dir %s failed",
317 full_fname(orig_dir));
318 exit_cleanup(RERR_FILESELECT);
321 if (dir && !push_dir(dir, 0)) {
322 io_error |= IOERR_GENERAL;
323 rsyserr(FERROR, errno, "push_dir %s failed in %s",
324 full_fname(dir), curr_dir);
329 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
334 static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
336 static time_t modtime;
338 #ifdef SUPPORT_HARD_LINKS
342 static uint32 rdev_major;
345 static char *user_name, *group_name;
346 static char lastname[MAXPATHLEN];
347 char fname[MAXPATHLEN];
348 int first_hlink_ndx = -1;
353 if (ic_send != (iconv_t)-1) {
356 INIT_CONST_XBUF(outbuf, fname);
359 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
360 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
361 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
364 outbuf.buf[outbuf.len++] = '/';
367 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
368 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
370 io_error |= IOERR_GENERAL;
372 "[%s] cannot convert filename: %s (%s)\n",
373 who_am_i(), f_name(file, fname), strerror(errno));
376 outbuf.buf[outbuf.len] = '\0';
381 /* Initialize starting value of xflags. */
382 if (protocol_version >= 30 && S_ISDIR(file->mode)) {
383 if (file->flags & FLAG_CONTENT_DIR)
384 xflags = file->flags & FLAG_TOP_DIR;
385 else if (file->flags & FLAG_IMPLIED_DIR)
386 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
388 xflags = XMIT_NO_CONTENT_DIR;
390 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
392 if (file->mode == mode)
393 xflags |= XMIT_SAME_MODE;
397 if ((preserve_devices && IS_DEVICE(mode))
398 || (preserve_specials && IS_SPECIAL(mode))) {
399 if (protocol_version < 28) {
400 if (tmp_rdev == rdev)
401 xflags |= XMIT_SAME_RDEV_pre28;
406 if ((uint32)major(rdev) == rdev_major)
407 xflags |= XMIT_SAME_RDEV_MAJOR;
409 rdev_major = major(rdev);
410 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
411 xflags |= XMIT_RDEV_MINOR_8_pre30;
413 } else if (protocol_version < 28)
414 rdev = MAKEDEV(0, 0);
416 if ((uid_t)F_OWNER(file) == uid && *lastname)
417 xflags |= XMIT_SAME_UID;
420 if (uid_ndx && !numeric_ids) {
421 user_name = add_uid(uid);
422 if (inc_recurse && user_name)
423 xflags |= XMIT_USER_NAME_FOLLOWS;
428 if ((gid_t)F_GROUP(file) == gid && *lastname)
429 xflags |= XMIT_SAME_GID;
432 if (gid_ndx && !numeric_ids) {
433 group_name = add_gid(gid);
434 if (inc_recurse && group_name)
435 xflags |= XMIT_GROUP_NAME_FOLLOWS;
439 if (file->modtime == modtime)
440 xflags |= XMIT_SAME_TIME;
442 modtime = file->modtime;
444 #ifdef SUPPORT_HARD_LINKS
446 if (protocol_version >= 30) {
447 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
448 first_hlink_ndx = (int32)(long)np->data - 1;
449 if (first_hlink_ndx < 0) {
450 high_hlink_ndx = ndx + first_ndx;
451 np->data = (void*)(long)(high_hlink_ndx + 1);
452 xflags |= XMIT_HLINK_FIRST;
454 xflags |= XMIT_HLINKED;
456 if (tmp_dev == dev) {
457 if (protocol_version >= 28)
458 xflags |= XMIT_SAME_DEV_pre30;
461 xflags |= XMIT_HLINKED;
467 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
469 l2 = strlen(fname+l1);
472 xflags |= XMIT_SAME_NAME;
474 xflags |= XMIT_LONG_NAME;
476 /* We must make sure we don't send a zero flag byte or the
477 * other end will terminate the flist transfer. Note that
478 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
479 * it's harmless way to add a bit to the first flag byte. */
480 if (protocol_version >= 28) {
481 if (!xflags && !S_ISDIR(mode))
482 xflags |= XMIT_TOP_DIR;
483 if ((xflags & 0xFF00) || !xflags) {
484 xflags |= XMIT_EXTENDED_FLAGS;
485 write_shortint(f, xflags);
487 write_byte(f, xflags);
489 if (!(xflags & 0xFF))
490 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
491 write_byte(f, xflags);
493 if (xflags & XMIT_SAME_NAME)
495 if (xflags & XMIT_LONG_NAME)
496 write_varint30(f, l2);
499 write_buf(f, fname + l1, l2);
501 if (first_hlink_ndx >= 0) {
502 write_varint(f, first_hlink_ndx);
503 if (first_hlink_ndx >= first_ndx)
507 write_varlong30(f, F_LENGTH(file), 3);
508 if (!(xflags & XMIT_SAME_TIME)) {
509 if (protocol_version >= 30)
510 write_varlong(f, modtime, 4);
512 write_int(f, modtime);
514 if (!(xflags & XMIT_SAME_MODE))
515 write_int(f, to_wire_mode(mode));
516 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
517 if (protocol_version < 30)
520 write_varint(f, uid);
521 if (xflags & XMIT_USER_NAME_FOLLOWS) {
522 int len = strlen(user_name);
524 write_buf(f, user_name, len);
528 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
529 if (protocol_version < 30)
532 write_varint(f, gid);
533 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
534 int len = strlen(group_name);
536 write_buf(f, group_name, len);
540 if ((preserve_devices && IS_DEVICE(mode))
541 || (preserve_specials && IS_SPECIAL(mode))) {
542 if (protocol_version < 28) {
543 if (!(xflags & XMIT_SAME_RDEV_pre28))
544 write_int(f, (int)rdev);
546 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
547 write_varint30(f, major(rdev));
548 if (protocol_version >= 30)
549 write_varint(f, minor(rdev));
550 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
551 write_byte(f, minor(rdev));
553 write_int(f, minor(rdev));
558 if (preserve_links && S_ISLNK(mode)) {
559 const char *sl = F_SYMLINK(file);
560 int len = strlen(sl);
561 write_varint30(f, len);
562 write_buf(f, sl, len);
566 #ifdef SUPPORT_HARD_LINKS
567 if (tmp_dev != 0 && protocol_version < 30) {
568 if (protocol_version < 26) {
569 /* 32-bit dev_t and ino_t */
570 write_int(f, (int32)dev);
571 write_int(f, (int32)tmp_ino);
573 /* 64-bit dev_t and ino_t */
574 if (!(xflags & XMIT_SAME_DEV_pre30))
575 write_longint(f, dev);
576 write_longint(f, tmp_ino);
581 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
586 /* Prior to 28, we sent a useless set of nulls. */
589 write_buf(f, sum, checksum_len);
593 strlcpy(lastname, fname, MAXPATHLEN);
595 if (S_ISREG(mode) || S_ISLNK(mode))
596 stats.total_size += F_LENGTH(file);
599 static struct file_struct *recv_file_entry(struct file_list *flist,
602 static int64 modtime;
604 #ifdef SUPPORT_HARD_LINKS
608 static uint32 rdev_major;
611 static uint16 gid_flags;
612 static char lastname[MAXPATHLEN], *lastdir;
613 static int lastdir_depth, lastdir_len = -1;
614 static unsigned int del_hier_name_len = 0;
615 static int in_del_hier = 0;
616 char thisname[MAXPATHLEN];
617 unsigned int l1 = 0, l2 = 0;
618 int alloc_len, basename_len, linkname_len;
619 int extra_len = file_extra_cnt * EXTRA_LEN;
620 int first_hlink_ndx = -1;
622 const char *basename;
623 struct file_struct *file;
627 if (xflags & XMIT_SAME_NAME)
630 if (xflags & XMIT_LONG_NAME)
631 l2 = read_varint30(f);
635 if (l2 >= MAXPATHLEN - l1) {
637 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
638 xflags, l1, l2, lastname, who_am_i());
639 overflow_exit("recv_file_entry");
642 strlcpy(thisname, lastname, l1 + 1);
643 read_sbuf(f, &thisname[l1], l2);
644 thisname[l1 + l2] = 0;
646 /* Abuse basename_len for a moment... */
647 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
650 if (ic_recv != (iconv_t)-1) {
653 INIT_CONST_XBUF(outbuf, thisname);
654 INIT_XBUF(inbuf, lastname, basename_len, -1);
656 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
657 io_error |= IOERR_GENERAL;
659 "[%s] cannot convert filename: %s (%s)\n",
660 who_am_i(), lastname, strerror(errno));
663 outbuf.buf[outbuf.len] = '\0';
667 clean_fname(thisname, 0);
670 sanitize_path(thisname, thisname, "", 0, NULL);
672 if ((basename = strrchr(thisname, '/')) != NULL) {
673 int len = basename++ - thisname;
674 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
675 lastdir = new_array(char, len + 1);
676 memcpy(lastdir, thisname, len);
679 lastdir_depth = count_dir_elements(lastdir);
683 basename_len = strlen(basename) + 1; /* count the '\0' */
685 #ifdef SUPPORT_HARD_LINKS
686 if (protocol_version >= 30
687 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
688 first_hlink_ndx = read_varint(f);
689 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
691 "hard-link reference out of range: %d (%d)\n",
692 first_hlink_ndx, flist->ndx_start + flist->used);
693 exit_cleanup(RERR_PROTOCOL);
695 if (first_hlink_ndx >= flist->ndx_start) {
696 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
697 file_length = F_LENGTH(first);
698 modtime = first->modtime;
701 uid = F_OWNER(first);
703 gid = F_GROUP(first);
704 if ((preserve_devices && IS_DEVICE(mode))
705 || (preserve_specials && IS_SPECIAL(mode))) {
706 uint32 *devp = F_RDEV_P(first);
707 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
708 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
710 if (preserve_links && S_ISLNK(mode))
711 linkname_len = strlen(F_SYMLINK(first)) + 1;
719 file_length = read_varlong30(f, 3);
720 if (!(xflags & XMIT_SAME_TIME)) {
721 if (protocol_version >= 30) {
722 modtime = read_varlong(f, 4);
723 #if SIZEOF_TIME_T < SIZEOF_INT64
724 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
726 "Time value of %s truncated on receiver.\n",
731 modtime = read_int(f);
733 if (!(xflags & XMIT_SAME_MODE))
734 mode = from_wire_mode(read_int(f));
736 if (chmod_modes && !S_ISLNK(mode))
737 mode = tweak_mode(mode, chmod_modes);
739 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
740 if (protocol_version < 30)
741 uid = (uid_t)read_int(f);
743 uid = (uid_t)read_varint(f);
744 if (xflags & XMIT_USER_NAME_FOLLOWS)
745 uid = recv_user_name(f, uid);
746 else if (inc_recurse && am_root && !numeric_ids)
747 uid = match_uid(uid);
750 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
751 if (protocol_version < 30)
752 gid = (gid_t)read_int(f);
754 gid = (gid_t)read_varint(f);
756 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
757 gid = recv_group_name(f, gid, &gid_flags);
758 else if (inc_recurse && (!am_root || !numeric_ids))
759 gid = match_gid(gid, &gid_flags);
763 if ((preserve_devices && IS_DEVICE(mode))
764 || (preserve_specials && IS_SPECIAL(mode))) {
765 if (protocol_version < 28) {
766 if (!(xflags & XMIT_SAME_RDEV_pre28))
767 rdev = (dev_t)read_int(f);
770 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
771 rdev_major = read_varint30(f);
772 if (protocol_version >= 30)
773 rdev_minor = read_varint(f);
774 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
775 rdev_minor = read_byte(f);
777 rdev_minor = read_int(f);
778 rdev = MAKEDEV(rdev_major, rdev_minor);
780 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
782 } else if (protocol_version < 28)
783 rdev = MAKEDEV(0, 0);
786 if (preserve_links && S_ISLNK(mode)) {
787 linkname_len = read_varint30(f) + 1; /* count the '\0' */
788 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
789 rprintf(FERROR, "overflow: linkname_len=%d\n",
791 overflow_exit("recv_file_entry");
798 #ifdef SUPPORT_HARD_LINKS
800 if (preserve_hard_links) {
801 if (protocol_version < 28 && S_ISREG(mode))
802 xflags |= XMIT_HLINKED;
803 if (xflags & XMIT_HLINKED)
804 extra_len += (inc_recurse+1) * EXTRA_LEN;
809 /* We need one or two index int32s when we're preserving ACLs. */
811 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
814 if (always_checksum && S_ISREG(mode))
815 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
817 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
818 extra_len += EXTRA_LEN;
820 #if EXTRA_ROUNDING > 0
821 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
822 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
825 if (inc_recurse && S_ISDIR(mode)) {
826 if (one_file_system) {
827 /* Room to save the dir's device for -x */
828 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
830 pool = dir_flist->file_pool;
832 pool = flist->file_pool;
834 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
836 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
838 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
840 file = (struct file_struct *)bp;
841 bp += FILE_STRUCT_LEN;
843 memcpy(bp, basename, basename_len);
844 bp += basename_len + linkname_len; /* skip space for symlink too */
846 #ifdef SUPPORT_HARD_LINKS
847 if (xflags & XMIT_HLINKED)
848 file->flags |= FLAG_HLINKED;
850 file->modtime = (time_t)modtime;
851 file->len32 = (uint32)file_length;
852 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
853 file->flags |= FLAG_LENGTH64;
854 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
861 file->flags |= gid_flags;
865 F_NDX(file) = flist->used + flist->ndx_start;
868 if (basename != thisname) {
869 file->dirname = lastdir;
870 F_DEPTH(file) = lastdir_depth + 1;
875 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
877 if (protocol_version >= 30) {
878 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
879 if (xflags & XMIT_TOP_DIR)
880 file->flags |= FLAG_TOP_DIR;
881 file->flags |= FLAG_CONTENT_DIR;
882 } else if (xflags & XMIT_TOP_DIR)
883 file->flags |= FLAG_IMPLIED_DIR;
884 } else if (xflags & XMIT_TOP_DIR) {
885 in_del_hier = recurse;
886 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
887 if (relative_paths && del_hier_name_len > 2
888 && lastname[del_hier_name_len-1] == '.'
889 && lastname[del_hier_name_len-2] == '/')
890 del_hier_name_len -= 2;
891 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
892 } else if (in_del_hier) {
893 if (!relative_paths || !del_hier_name_len
894 || (l1 >= del_hier_name_len
895 && lastname[del_hier_name_len] == '/'))
896 file->flags |= FLAG_CONTENT_DIR;
902 if ((preserve_devices && IS_DEVICE(mode))
903 || (preserve_specials && IS_SPECIAL(mode))) {
904 uint32 *devp = F_RDEV_P(file);
905 DEV_MAJOR(devp) = major(rdev);
906 DEV_MINOR(devp) = minor(rdev);
911 bp = (char*)file->basename + basename_len;
912 if (first_hlink_ndx >= flist->ndx_start) {
913 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
914 memcpy(bp, F_SYMLINK(first), linkname_len);
916 read_sbuf(f, bp, linkname_len - 1);
918 sanitize_path(bp, bp, "", lastdir_depth, NULL);
922 #ifdef SUPPORT_HARD_LINKS
923 if (preserve_hard_links && xflags & XMIT_HLINKED) {
924 if (protocol_version >= 30) {
925 if (xflags & XMIT_HLINK_FIRST) {
926 high_hlink_ndx = flist->ndx_start + flist->used;
927 F_HL_GNUM(file) = high_hlink_ndx;
929 F_HL_GNUM(file) = first_hlink_ndx;
931 static int32 cnt = 0;
932 struct ht_int64_node *np;
935 if (protocol_version < 26) {
939 if (!(xflags & XMIT_SAME_DEV_pre30))
940 dev = read_longint(f);
941 ino = read_longint(f);
943 np = idev_find(dev, ino);
944 ndx = (int32)(long)np->data - 1;
947 np->data = (void*)(long)cnt;
949 F_HL_GNUM(file) = ndx;
954 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
958 /* Prior to 28, we get a useless set of nulls. */
961 if (first_hlink_ndx >= flist->ndx_start) {
962 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
963 memcpy(bp, F_SUM(first), checksum_len);
965 read_buf(f, bp, checksum_len);
969 if (preserve_acls && !S_ISLNK(mode))
970 receive_acl(file, f);
972 #ifdef SUPPORT_XATTRS
974 receive_xattr(file, f );
977 if (S_ISREG(mode) || S_ISLNK(mode))
978 stats.total_size += file_length;
983 /* Create a file_struct for a named file by reading its stat() information
984 * and performing extensive checks against global options.
986 * Returns a pointer to the new file struct, or NULL if there was an error
987 * or this file should be excluded. */
988 struct file_struct *make_file(const char *fname, struct file_list *flist,
989 STRUCT_STAT *stp, int flags, int filter_level)
991 static char *lastdir;
992 static int lastdir_len = -1;
993 struct file_struct *file;
994 char thisname[MAXPATHLEN];
995 char linkname[MAXPATHLEN];
996 int alloc_len, basename_len, linkname_len;
997 int extra_len = file_extra_cnt * EXTRA_LEN;
998 const char *basename;
1003 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1004 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1007 clean_fname(thisname, 0);
1009 sanitize_path(thisname, thisname, "", 0, NULL);
1011 if (stp && S_ISDIR(stp->st_mode)) {
1012 st = *stp; /* Needed for "symlink/." with --relative. */
1013 *linkname = '\0'; /* make IBM code checker happy */
1014 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1015 int save_errno = errno;
1016 /* See if file is excluded before reporting an error. */
1017 if (filter_level != NO_FILTERS
1018 && (is_excluded(thisname, 0, filter_level)
1019 || is_excluded(thisname, 1, filter_level))) {
1020 if (ignore_perishable && save_errno != ENOENT)
1021 non_perishable_cnt++;
1024 if (save_errno == ENOENT) {
1025 #ifdef SUPPORT_LINKS
1026 /* Avoid "vanished" error if symlink points nowhere. */
1027 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1028 && S_ISLNK(st.st_mode)) {
1029 io_error |= IOERR_GENERAL;
1030 rprintf(FERROR, "symlink has no referent: %s\n",
1031 full_fname(thisname));
1035 enum logcode c = am_daemon && protocol_version < 28
1037 io_error |= IOERR_VANISHED;
1038 rprintf(c, "file has vanished: %s\n",
1039 full_fname(thisname));
1042 io_error |= IOERR_GENERAL;
1043 rsyserr(FERROR, save_errno, "readlink %s failed",
1044 full_fname(thisname));
1049 /* backup.c calls us with filter_level set to NO_FILTERS. */
1050 if (filter_level == NO_FILTERS)
1053 if (S_ISDIR(st.st_mode)) {
1055 rprintf(FINFO, "skipping directory %s\n", thisname);
1058 /* -x only affects dirs because we need to avoid recursing
1059 * into a mount-point directory, not to avoid copying a
1060 * symlinked file if -L (or similar) was specified. */
1061 if (one_file_system && st.st_dev != filesystem_dev
1062 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1063 if (one_file_system > 1) {
1066 "[%s] skipping mount-point dir %s\n",
1067 who_am_i(), thisname);
1071 flags |= FLAG_MOUNT_DIR;
1072 flags &= ~FLAG_CONTENT_DIR;
1075 flags &= ~FLAG_CONTENT_DIR;
1077 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1078 if (ignore_perishable)
1079 non_perishable_cnt++;
1083 if (lp_ignore_nonreadable(module_id)) {
1084 #ifdef SUPPORT_LINKS
1085 if (!S_ISLNK(st.st_mode))
1087 if (access(thisname, R_OK) != 0)
1093 /* Only divert a directory in the main transfer. */
1095 if (flist->prev && S_ISDIR(st.st_mode)
1096 && flags & FLAG_DIVERT_DIRS) {
1097 /* Room for parent/sibling/next-child info. */
1098 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1100 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1102 pool = dir_flist->file_pool;
1104 pool = flist->file_pool;
1109 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1110 who_am_i(), thisname, filter_level);
1113 if ((basename = strrchr(thisname, '/')) != NULL) {
1114 int len = basename++ - thisname;
1115 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1116 lastdir = new_array(char, len + 1);
1117 memcpy(lastdir, thisname, len);
1118 lastdir[len] = '\0';
1122 basename = thisname;
1123 basename_len = strlen(basename) + 1; /* count the '\0' */
1125 #ifdef SUPPORT_LINKS
1126 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1131 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1132 extra_len += EXTRA_LEN;
1134 #if EXTRA_ROUNDING > 0
1135 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1136 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1139 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1142 bp = pool_alloc(pool, alloc_len, "make_file");
1144 if (!(bp = new_array(char, alloc_len)))
1145 out_of_memory("make_file");
1148 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1150 file = (struct file_struct *)bp;
1151 bp += FILE_STRUCT_LEN;
1153 memcpy(bp, basename, basename_len);
1154 bp += basename_len + linkname_len; /* skip space for symlink too */
1156 #ifdef SUPPORT_HARD_LINKS
1157 if (preserve_hard_links && flist && flist->prev) {
1158 if (protocol_version >= 28
1159 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1160 : S_ISREG(st.st_mode)) {
1161 tmp_dev = st.st_dev;
1162 tmp_ino = st.st_ino;
1168 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1169 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1170 tmp_rdev = st.st_rdev;
1175 file->flags = flags;
1176 file->modtime = st.st_mtime;
1177 file->len32 = (uint32)st.st_size;
1178 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1179 file->flags |= FLAG_LENGTH64;
1180 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1182 file->mode = st.st_mode;
1184 F_OWNER(file) = st.st_uid;
1186 F_GROUP(file) = st.st_gid;
1188 if (basename != thisname)
1189 file->dirname = lastdir;
1191 #ifdef SUPPORT_LINKS
1193 bp = (char*)file->basename + basename_len;
1194 memcpy(bp, linkname, linkname_len);
1198 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1199 file_checksum(thisname, tmp_sum, st.st_size);
1201 F_PATHNAME(file) = pathname;
1203 /* This code is only used by the receiver when it is building
1204 * a list of files for a delete pass. */
1205 if (keep_dirlinks && linkname_len && flist) {
1207 int save_mode = file->mode;
1208 file->mode = S_IFDIR; /* Find a directory with our name. */
1209 if (flist_find(dir_flist, file) >= 0
1210 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1211 file->modtime = st2.st_mtime;
1213 file->mode = st2.st_mode;
1215 F_OWNER(file) = st2.st_uid;
1217 F_GROUP(file) = st2.st_gid;
1219 file->mode = save_mode;
1222 if (basename_len == 0+1)
1227 F_NDX(file) = dir_count - 1;
1233 /* Only called for temporary file_struct entries created by make_file(). */
1234 void unmake_file(struct file_struct *file)
1236 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1237 #if EXTRA_ROUNDING > 0
1238 if (extra_cnt & EXTRA_ROUNDING)
1239 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1241 free(REQ_EXTRA(file, extra_cnt));
1244 static struct file_struct *send_file_name(int f, struct file_list *flist,
1245 char *fname, STRUCT_STAT *stp,
1246 int flags, int filter_level)
1248 struct file_struct *file;
1249 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1253 file = make_file(fname, flist, stp, flags, filter_level);
1257 if (chmod_modes && !S_ISLNK(file->mode))
1258 file->mode = tweak_mode(file->mode, chmod_modes);
1261 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1262 sx.st.st_mode = file->mode;
1263 sx.acc_acl = sx.def_acl = NULL;
1264 if (get_acl(fname, &sx) < 0)
1268 #ifdef SUPPORT_XATTRS
1269 if (preserve_xattrs && f >= 0) {
1271 if (get_xattr(fname, &sx) < 0)
1276 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1278 flist_expand(flist, 1);
1279 flist->files[flist->used++] = file;
1281 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1283 if (preserve_acls && !S_ISLNK(file->mode)) {
1288 #ifdef SUPPORT_XATTRS
1289 if (preserve_xattrs) {
1290 F_XATTR(file) = send_xattr(&sx, f);
1298 static void send_if_directory(int f, struct file_list *flist,
1299 struct file_struct *file,
1300 char *fbuf, unsigned int ol,
1303 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1305 if (S_ISDIR(file->mode)
1306 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1308 unsigned int len = strlen(fbuf);
1309 if (len > 1 && fbuf[len-1] == '/')
1311 if (len >= MAXPATHLEN - 1) {
1312 io_error |= IOERR_GENERAL;
1313 rprintf(FERROR, "skipping long-named directory: %s\n",
1317 save_filters = push_local_filters(fbuf, len);
1318 send_directory(f, flist, fbuf, len, flags);
1319 pop_local_filters(save_filters);
1326 static int file_compare(const void *file1, const void *file2)
1328 return f_name_cmp(*(struct file_struct **)file1,
1329 *(struct file_struct **)file2);
1332 /* The guts of a merge-sort algorithm. This was derived from the glibc
1333 * version, but I (Wayne) changed the merge code to do less copying and
1334 * to require only half the amount of temporary memory. */
1335 static void fsort_tmp(struct file_struct **fp, size_t num,
1336 struct file_struct **tmp)
1338 struct file_struct **f1, **f2, **t;
1347 fsort_tmp(f1, n1, tmp);
1349 fsort_tmp(f2, n2, tmp);
1351 while (f_name_cmp(*f1, *f2) <= 0) {
1358 memcpy(t, f1, n1 * PTR_SIZE);
1360 *f1++ = *f2++, n2--;
1362 while (n1 > 0 && n2 > 0) {
1363 if (f_name_cmp(*t, *f2) <= 0)
1366 *f1++ = *f2++, n2--;
1370 memcpy(f1, t, n1 * PTR_SIZE);
1373 /* This file-struct sorting routine makes sure that any identical names in
1374 * the file list stay in the same order as they were in the original list.
1375 * This is particularly vital in inc_recurse mode where we expect a sort
1376 * on the flist to match the exact order of a sort on the dir_flist. */
1377 static void fsort(struct file_struct **fp, size_t num)
1383 qsort(fp, num, PTR_SIZE, file_compare);
1385 struct file_struct **tmp = new_array(struct file_struct *,
1387 fsort_tmp(fp, num, tmp);
1392 /* We take an entire set of sibling dirs from the sorted flist and link them
1393 * into the tree, setting the appropriate parent/child/sibling pointers. */
1394 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1399 int32 *parent_dp = parent_ndx < 0 ? NULL
1400 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1402 flist_expand(dir_flist, dir_cnt);
1403 dir_flist->sorted = dir_flist->files;
1405 for (i = 0; dir_cnt; i++) {
1406 struct file_struct *file = from_flist->sorted[i];
1408 if (!S_ISDIR(file->mode))
1411 dir_flist->files[dir_flist->used++] = file;
1415 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1417 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1419 send_dir_ndx = dir_flist->used - 1;
1421 dp = F_DIR_NODE_P(file);
1422 DIR_PARENT(dp) = parent_ndx;
1423 DIR_FIRST_CHILD(dp) = -1;
1426 DIR_NEXT_SIBLING(dp) = -1;
1429 /* This function is normally called by the sender, but the receiving side also
1430 * calls it from get_dirlist() with f set to -1 so that we just construct the
1431 * file list in memory without sending it over the wire. Also, get_dirlist()
1432 * might call this with f set to -2, which also indicates that local filter
1433 * rules should be ignored. */
1434 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1441 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1442 int start = flist->used;
1443 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1445 assert(flist != NULL);
1447 if (!(d = opendir(fbuf))) {
1448 io_error |= IOERR_GENERAL;
1449 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1454 if (len != 1 || *fbuf != '/')
1457 remainder = MAXPATHLEN - (p - fbuf);
1459 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1460 char *dname = d_name(di);
1461 if (dname[0] == '.' && (dname[1] == '\0'
1462 || (dname[1] == '.' && dname[2] == '\0')))
1464 if (strlcpy(p, dname, remainder) >= remainder) {
1465 io_error |= IOERR_GENERAL;
1467 "cannot send long-named file %s\n",
1471 if (dname[0] == '\0') {
1472 io_error |= IOERR_GENERAL;
1474 "cannot send file with empty name in %s\n",
1479 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1485 io_error |= IOERR_GENERAL;
1486 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1491 if (f >= 0 && recurse && !divert_dirs) {
1492 int i, end = flist->used - 1;
1493 /* send_if_directory() bumps flist->used, so use "end". */
1494 for (i = start; i <= end; i++)
1495 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1499 static char lastpath[MAXPATHLEN] = "";
1500 static int lastpath_len = 0;
1501 static struct file_struct *lastpath_struct;
1503 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1504 char *start, char *limit, int flags, int is_dot_dir)
1506 struct file_struct *file;
1507 item_list *relname_list;
1508 relnamecache **rnpp;
1510 int len, need_new_dir;
1512 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1515 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1516 && lastpath_len == limit - fname
1517 && strncmp(lastpath, fname, lastpath_len) == 0)
1525 int save_copy_links = copy_links;
1526 int save_xfer_dirs = xfer_dirs;
1528 copy_links = xfer_dirs = 1;
1532 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1534 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1538 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1540 if (file && !S_ISDIR(file->mode))
1542 lastpath_struct = file;
1545 strlcpy(lastpath, fname, sizeof lastpath);
1546 lastpath_len = limit - fname;
1550 copy_links = save_copy_links;
1551 xfer_dirs = save_xfer_dirs;
1557 if (!lastpath_struct)
1558 return; /* dir must have vanished */
1560 len = strlen(limit+1);
1561 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1562 if (!relname_list) {
1563 if (!(relname_list = new0(item_list)))
1564 out_of_memory("send_implied_dirs");
1565 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1567 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1568 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1569 out_of_memory("send_implied_dirs");
1570 (*rnpp)->is_dot_dir = is_dot_dir;
1571 strlcpy((*rnpp)->fname, limit+1, len + 1);
1574 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1576 char fbuf[MAXPATHLEN];
1577 item_list *relname_list;
1578 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1582 dlen = strlen(fbuf);
1584 if (F_PATHNAME(file) != pathname) {
1585 if (!push_pathname(F_PATHNAME(file), -1))
1586 exit_cleanup(RERR_FILESELECT);
1589 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1591 if (file->flags & FLAG_CONTENT_DIR) {
1592 if (one_file_system) {
1594 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1595 io_error |= IOERR_GENERAL;
1596 rsyserr(FERROR, errno, "link_stat %s failed",
1600 filesystem_dev = st.st_dev;
1602 send_directory(f, flist, fbuf, dlen, flags);
1605 if (!relative_paths)
1608 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1612 for (j = 0; j < relname_list->count; j++) {
1614 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1615 int is_dot_dir = rnp->is_dot_dir;
1618 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1620 if (len >= (int)sizeof fbuf)
1621 continue; /* Impossible... */
1623 slash = strchr(fbuf+dlen+1, '/');
1625 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
1631 if (link_stat(fbuf, &st, 1) != 0) {
1632 io_error |= IOERR_GENERAL;
1633 rsyserr(FERROR, errno, "link_stat %s failed",
1637 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1639 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1645 void send_extra_file_list(int f, int at_least)
1647 struct file_list *flist;
1650 int future_cnt, save_io_error = io_error;
1655 /* Keep sending data until we have the requested number of
1656 * files in the upcoming file-lists. */
1657 if (cur_flist->next) {
1658 flist = first_flist->prev; /* the newest flist */
1659 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1662 while (future_cnt < at_least) {
1663 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1664 int dir_ndx, dstart = dir_count;
1665 const char *pathname = F_PATHNAME(file);
1668 flist = flist_new(0, "send_extra_file_list");
1669 start_write = stats.total_written;
1673 dir_ndx = F_NDX(file);
1676 dir_ndx = send_dir_ndx;
1677 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1678 flist->parent_ndx = dir_ndx;
1680 send1extra(f, file, flist);
1681 prev_flags = file->flags;
1682 dp = F_DIR_NODE_P(file);
1684 /* If there are any duplicate directory names that follow, we
1685 * send all the dirs together in one file-list. The dir_flist
1686 * tree links all the child subdirs onto the last dup dir. */
1687 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1688 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1689 send_dir_ndx = dir_ndx;
1690 file = dir_flist->sorted[dir_ndx];
1691 /* Try to avoid some duplicate scanning of identical dirs. */
1692 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1693 file->flags &= ~FLAG_CONTENT_DIR;
1694 send1extra(f, file, flist);
1695 prev_flags = file->flags;
1696 dp = F_DIR_NODE_P(file);
1702 if (need_unsorted_flist) {
1703 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1704 out_of_memory("send_extra_file_list");
1705 memcpy(flist->sorted, flist->files,
1706 flist->used * sizeof (struct file_struct*));
1709 flist->sorted = flist->files;
1711 clean_flist(flist, 0);
1713 flist->ndx_end = flist->ndx_start + flist->used - 1
1714 - (dir_count - dstart);
1716 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1717 flist_done_allocating(flist);
1719 file_total += flist->used;
1720 future_cnt += flist->used;
1721 stats.flist_size += stats.total_written - start_write;
1722 stats.num_files += flist->used;
1724 output_flist(flist);
1726 if (DIR_FIRST_CHILD(dp) >= 0) {
1727 send_dir_ndx = DIR_FIRST_CHILD(dp);
1730 while (DIR_NEXT_SIBLING(dp) < 0) {
1731 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1732 write_ndx(f, NDX_FLIST_EOF);
1734 change_local_filter_dir(NULL, 0, 0);
1738 file = dir_flist->sorted[send_dir_ndx];
1739 dp = F_DIR_NODE_P(file);
1741 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1746 if (io_error != save_io_error && !ignore_errors)
1747 send_msg_int(MSG_IO_ERROR, io_error);
1750 struct file_list *send_file_list(int f, int argc, char *argv[])
1752 static const char *lastdir;
1753 static int lastdir_len = -1;
1757 struct file_list *flist;
1758 struct timeval start_tv, end_tv;
1761 int disable_buffering;
1762 int flags = recurse ? FLAG_CONTENT_DIR : 0;
1763 int reading_remotely = filesfrom_host != NULL;
1764 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1766 | (filesfrom_convert ? RL_CONVERT : 0)
1768 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1770 rprintf(FLOG, "building file list\n");
1771 if (show_filelist_p())
1772 start_filelist_progress("building file list");
1773 else if (inc_recurse && verbose && !am_server)
1774 rprintf(FCLIENT, "sending incremental file list\n");
1776 start_write = stats.total_written;
1777 gettimeofday(&start_tv, NULL);
1779 if (relative_paths && protocol_version >= 30)
1780 implied_dirs = 1; /* We send flagged implied dirs */
1782 #ifdef SUPPORT_HARD_LINKS
1783 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1787 flist = cur_flist = flist_new(0, "send_file_list");
1789 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1790 flags |= FLAG_DIVERT_DIRS;
1792 dir_flist = cur_flist;
1794 disable_buffering = io_start_buffering_out(f);
1795 if (filesfrom_fd >= 0) {
1796 if (argv[0] && !push_dir(argv[0], 0)) {
1797 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1798 full_fname(argv[0]), curr_dir);
1799 exit_cleanup(RERR_FILESELECT);
1805 char fbuf[MAXPATHLEN];
1810 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1812 sanitize_path(fbuf, fbuf, "", 0, NULL);
1816 strlcpy(fbuf, *argv++, MAXPATHLEN);
1818 sanitize_path(fbuf, fbuf, "", 0, NULL);
1822 if (relative_paths) {
1823 /* We clean up fbuf below. */
1825 } else if (!len || fbuf[len - 1] == '/') {
1826 if (len == 2 && fbuf[0] == '.') {
1827 /* Turn "./" into just "." rather than "./." */
1830 if (len + 1 >= MAXPATHLEN)
1831 overflow_exit("send_file_list");
1836 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1837 && (len == 2 || fbuf[len-3] == '/')) {
1838 if (len + 2 >= MAXPATHLEN)
1839 overflow_exit("send_file_list");
1845 is_dot_dir = fbuf[len-1] == '.'
1846 && (len == 1 || fbuf[len-2] == '/');
1851 if (!relative_paths) {
1852 p = strrchr(fbuf, '/');
1859 len -= p - fbuf + 1;
1864 if ((p = strstr(fbuf, "/./")) != NULL) {
1870 len -= p - fbuf + 3;
1874 /* Get rid of trailing "/" and "/.". */
1876 if (fn[len - 1] == '/') {
1878 if (!--len && !dir) {
1883 else if (len >= 2 && fn[len - 1] == '.'
1884 && fn[len - 2] == '/') {
1886 if (!(len -= 2) && !dir) {
1898 } else if (fn[0] == '.')
1901 /* Reject a ".." dir in the active part of the path. */
1902 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1903 if ((p[2] == '/' || p[2] == '\0')
1904 && (p == fn || p[-1] == '/')) {
1906 "found \"..\" dir in relative path: %s\n",
1908 exit_cleanup(RERR_SYNTAX);
1919 dirlen = dir ? strlen(dir) : 0;
1920 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1921 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1924 lastdir_len = pathname_len;
1925 } else if (!push_pathname(lastdir, lastdir_len))
1929 memmove(fbuf, fn, len + 1);
1931 if (link_stat(fbuf, &st, copy_dirlinks || is_dot_dir) != 0) {
1932 io_error |= IOERR_GENERAL;
1933 rsyserr(FERROR, errno, "link_stat %s failed",
1938 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1939 rprintf(FINFO, "skipping directory %s\n", fbuf);
1943 if (inc_recurse && relative_paths && *fbuf) {
1944 if ((p = strchr(fbuf+1, '/')) != NULL) {
1945 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, is_dot_dir);
1948 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1949 /* Send the implied directories at the start of the
1950 * source spec, so we get their permissions right. */
1951 char *lp = lastpath, *slash = fbuf;
1953 /* Skip any initial directories in our path that we
1954 * have in common with lastpath. */
1955 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1960 if (fn != p || (*lp && *lp != '/'))
1961 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1964 if (one_file_system)
1965 filesystem_dev = st.st_dev;
1967 if (recurse || (xfer_dirs && is_dot_dir)) {
1968 struct file_struct *file;
1969 int top_flags = FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags;
1970 file = send_file_name(f, flist, fbuf, &st,
1971 top_flags, ALL_FILTERS);
1972 if (file && !inc_recurse)
1973 send_if_directory(f, flist, file, fbuf, len, flags);
1975 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1978 gettimeofday(&end_tv, NULL);
1979 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1980 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1981 if (stats.flist_buildtime == 0)
1982 stats.flist_buildtime = 1;
1985 write_byte(f, 0); /* Indicate end of file list */
1987 #ifdef SUPPORT_HARD_LINKS
1988 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1992 if (show_filelist_p())
1993 finish_filelist_progress(flist);
1995 gettimeofday(&end_tv, NULL);
1996 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1997 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1999 /* When converting names, both sides keep an unsorted file-list array
2000 * because the names will differ on the sending and receiving sides
2001 * (both sides will use the unsorted index number for each item). */
2003 /* Sort the list without removing any duplicates. This allows the
2004 * receiving side to ask for whatever name it kept. For incremental
2005 * recursion mode, the sender marks duplicate dirs so that it can
2006 * send them together in a single file-list. */
2008 if (need_unsorted_flist) {
2010 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2011 out_of_memory("send_file_list");
2012 memcpy(flist->sorted, flist->files,
2013 flist->used * sizeof (struct file_struct*));
2014 clean_flist(flist, 0);
2016 flist->sorted = flist->files;
2018 flist->high = flist->used - 1;
2023 flist->sorted = flist->files;
2024 clean_flist(flist, 0);
2026 file_total += flist->used;
2028 /* We don't subtract dir_count for the first send since we
2029 * might have one or more dot dirs which need to get sent. */
2030 flist->ndx_end = flist->ndx_start + flist->used - 1;
2032 if (!numeric_ids && !inc_recurse)
2035 /* send the io_error flag */
2036 if (protocol_version < 30)
2037 write_int(f, ignore_errors ? 0 : io_error);
2038 else if (io_error && !ignore_errors)
2039 send_msg_int(MSG_IO_ERROR, io_error);
2041 if (disable_buffering)
2042 io_end_buffering_out();
2044 stats.flist_size = stats.total_written - start_write;
2045 stats.num_files = flist->used;
2048 output_flist(flist);
2051 rprintf(FINFO, "send_file_list done\n");
2054 add_dirs_to_tree(-1, flist, dir_count);
2055 flist_done_allocating(flist);
2056 if (send_dir_ndx < 0) {
2057 write_ndx(f, NDX_FLIST_EOF);
2060 else if (file_total == 1) {
2061 /* If we're creating incremental file-lists and there
2062 * was just 1 item in the first file-list, send 1 more
2063 * file-list to check if this is a 1-file xfer. */
2064 send_extra_file_list(f, 1);
2071 struct file_list *recv_file_list(int f)
2073 struct file_list *flist;
2078 rprintf(FLOG, "receiving file list\n");
2079 if (show_filelist_p())
2080 start_filelist_progress("receiving file list");
2081 else if (inc_recurse && verbose && !am_server && !first_flist)
2082 rprintf(FCLIENT, "receiving incremental file list\n");
2084 start_read = stats.total_read;
2086 #ifdef SUPPORT_HARD_LINKS
2087 if (preserve_hard_links && !first_flist)
2091 flist = flist_new(0, "recv_file_list");
2094 if (flist->ndx_start == 0)
2095 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2096 dstart = dir_flist->used;
2102 while ((flags = read_byte(f)) != 0) {
2103 struct file_struct *file;
2105 flist_expand(flist, 1);
2107 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2108 flags |= read_byte(f) << 8;
2109 file = recv_file_entry(flist, flags, f);
2111 if (inc_recurse && S_ISDIR(file->mode)) {
2112 flist_expand(dir_flist, 1);
2113 dir_flist->files[dir_flist->used++] = file;
2116 flist->files[flist->used++] = file;
2118 maybe_emit_filelist_progress(flist->used);
2121 rprintf(FINFO, "recv_file_name(%s)\n",
2122 f_name(file, NULL));
2125 file_total += flist->used;
2127 flist->ndx_end = flist->ndx_start + flist->used - 1;
2128 if (inc_recurse && flist->ndx_start)
2129 flist->ndx_end -= dir_flist->used - dstart;
2132 rprintf(FINFO, "received %d names\n", flist->used);
2134 if (show_filelist_p())
2135 finish_filelist_progress(flist);
2138 if (need_unsorted_flist) {
2139 /* Create an extra array of index pointers that we can sort for
2140 * the generator's use (for wading through the files in sorted
2141 * order and for calling flist_find()). We keep the "files"
2142 * list unsorted for our exchange of index numbers with the
2143 * other side (since their names may not sort the same). */
2144 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2145 out_of_memory("recv_file_list");
2146 memcpy(flist->sorted, flist->files,
2147 flist->used * sizeof (struct file_struct*));
2148 if (inc_recurse && dir_flist->used > dstart) {
2149 dir_flist->sorted = realloc_array(dir_flist->sorted,
2150 struct file_struct *,
2152 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2153 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2154 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2159 flist->sorted = flist->files;
2160 if (inc_recurse && dir_flist->used > dstart) {
2161 dir_flist->sorted = dir_flist->files;
2162 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2167 flist_done_allocating(flist);
2169 recv_id_list(f, flist);
2171 clean_flist(flist, relative_paths);
2173 if (protocol_version < 30) {
2174 /* Recv the io_error flag */
2178 io_error |= read_int(f);
2182 output_flist(flist);
2185 rprintf(FINFO, "recv_file_list done\n");
2187 stats.flist_size += stats.total_read - start_read;
2188 stats.num_files += flist->used;
2193 /* This is only used once by the receiver if the very first file-list
2194 * has exactly one item in it. */
2195 void recv_additional_file_list(int f)
2197 struct file_list *flist;
2198 int ndx = read_ndx(f);
2199 if (ndx == NDX_FLIST_EOF) {
2201 change_local_filter_dir(NULL, 0, 0);
2203 ndx = NDX_FLIST_OFFSET - ndx;
2204 if (ndx < 0 || ndx >= dir_flist->used) {
2205 ndx = NDX_FLIST_OFFSET - ndx;
2207 "[%s] Invalid dir index: %d (%d - %d)\n",
2208 who_am_i(), ndx, NDX_FLIST_OFFSET,
2209 NDX_FLIST_OFFSET - dir_flist->used + 1);
2210 exit_cleanup(RERR_PROTOCOL);
2213 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2216 flist = recv_file_list(f);
2217 flist->parent_ndx = ndx;
2221 /* Search for an identically-named item in the file list. Note that the
2222 * items must agree in their directory-ness, or no match is returned. */
2223 int flist_find(struct file_list *flist, struct file_struct *f)
2225 int low = flist->low, high = flist->high;
2226 int diff, mid, mid_up;
2228 while (low <= high) {
2229 mid = (low + high) / 2;
2230 if (F_IS_ACTIVE(flist->sorted[mid]))
2233 /* Scan for the next non-empty entry using the cached
2234 * distance values. If the value isn't fully up-to-
2235 * date, update it. */
2236 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2237 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2239 mid_up += F_DEPTH(flist->sorted[mid_up]);
2240 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2241 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2243 if (mid_up > high) {
2244 /* If there's nothing left above us, set high to
2245 * a non-empty entry below us and continue. */
2246 high = mid - (int)flist->sorted[mid]->len32;
2247 if (!F_IS_ACTIVE(flist->sorted[high])) {
2249 high -= (int)flist->sorted[high]->len32;
2250 } while (!F_IS_ACTIVE(flist->sorted[high]));
2251 flist->sorted[mid]->len32 = mid - high;
2256 diff = f_name_cmp(flist->sorted[mid_up], f);
2258 if (protocol_version < 29
2259 && S_ISDIR(flist->sorted[mid_up]->mode)
2260 != S_ISDIR(f->mode))
2273 * Free up any resources a file_struct has allocated
2274 * and clear the file.
2276 void clear_file(struct file_struct *file)
2278 /* The +1 zeros out the first char of the basename. */
2279 memset(file, 0, FILE_STRUCT_LEN + 1);
2280 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2281 * entry. Likewise for len32 in the opposite direction. We assume
2282 * that we're alone for now since flist_find() will adjust the counts
2283 * it runs into that aren't up-to-date. */
2284 file->len32 = F_DEPTH(file) = 1;
2287 /* Allocate a new file list. */
2288 struct file_list *flist_new(int flags, char *msg)
2290 struct file_list *flist;
2292 if (!(flist = new0(struct file_list)))
2295 if (flags & FLIST_TEMP) {
2296 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2297 out_of_memory, POOL_INTERN)))
2300 /* This is a doubly linked list with prev looping back to
2301 * the end of the list, but the last next pointer is NULL. */
2303 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2304 out_of_memory, POOL_INTERN);
2305 if (!flist->file_pool)
2308 first_flist = cur_flist = flist->prev = flist;
2310 flist->file_pool = first_flist->file_pool;
2312 flist->ndx_start = first_flist->prev->ndx_end + 2;
2313 if (flist->ndx_start <= high_hlink_ndx)
2314 flist->ndx_start = high_hlink_ndx + 1;
2316 flist->prev = first_flist->prev;
2317 flist->prev->next = first_flist->prev = flist;
2319 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2326 /* Free up all elements in a flist. */
2327 void flist_free(struct file_list *flist)
2330 /* Was FLIST_TEMP dir-list. */
2331 } else if (flist == flist->prev) {
2332 first_flist = cur_flist = NULL;
2336 if (flist == cur_flist)
2337 cur_flist = flist->next;
2338 if (flist == first_flist)
2339 first_flist = first_flist->next;
2341 flist->prev->next = flist->next;
2343 flist->next = first_flist;
2345 flist->next->prev = flist->prev;
2346 file_total -= flist->used;
2350 if (!flist->prev || !flist_cnt)
2351 pool_destroy(flist->file_pool);
2353 pool_free_old(flist->file_pool, flist->pool_boundary);
2355 if (flist->sorted && flist->sorted != flist->files)
2356 free(flist->sorted);
2361 /* This routine ensures we don't have any duplicate names in our file list.
2362 * duplicate names can cause corruption because of the pipelining. */
2363 static void clean_flist(struct file_list *flist, int strip_root)
2365 char fbuf[MAXPATHLEN];
2370 if (flist->used == 0) {
2376 fsort(flist->sorted, flist->used);
2378 if (!am_sender || inc_recurse) {
2379 for (i = prev_i = 0; i < flist->used; i++) {
2380 if (F_IS_ACTIVE(flist->sorted[i])) {
2385 flist->low = prev_i;
2387 i = prev_i = flist->used - 1;
2391 while (++i < flist->used) {
2393 struct file_struct *file = flist->sorted[i];
2395 if (!F_IS_ACTIVE(file))
2397 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2399 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2400 int save_mode = file->mode;
2401 /* Make sure that this directory doesn't duplicate a
2402 * non-directory earlier in the list. */
2403 flist->high = prev_i;
2404 file->mode = S_IFREG;
2405 j = flist_find(flist, file);
2406 file->mode = save_mode;
2411 /* If one is a dir and the other is not, we want to
2412 * keep the dir because it might have contents in the
2413 * list. Otherwise keep the first one. */
2414 if (S_ISDIR(file->mode)) {
2415 struct file_struct *fp = flist->sorted[j];
2416 if (!S_ISDIR(fp->mode))
2420 file->flags |= FLAG_DUPLICATE;
2421 else { /* Make sure we merge our vital flags. */
2422 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2423 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2433 "removing duplicate name %s from file list (%d)\n",
2434 f_name(file, fbuf), drop + flist->ndx_start);
2436 clear_file(flist->sorted[drop]);
2440 if (flist->low == drop) {
2442 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2451 flist->high = prev_i;
2454 /* We need to strip off the leading slashes for relative
2455 * paths, but this must be done _after_ the sorting phase. */
2456 for (i = flist->low; i <= flist->high; i++) {
2457 struct file_struct *file = flist->sorted[i];
2461 while (*file->dirname == '/')
2463 if (!*file->dirname)
2464 file->dirname = NULL;
2468 if (prune_empty_dirs && !am_sender) {
2469 int j, prev_depth = 0;
2471 prev_i = 0; /* It's OK that this isn't really true. */
2473 for (i = flist->low; i <= flist->high; i++) {
2474 struct file_struct *fp, *file = flist->sorted[i];
2476 /* This temporarily abuses the F_DEPTH() value for a
2477 * directory that is in a chain that might get pruned.
2478 * We restore the old value if it gets a reprieve. */
2479 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2480 /* Dump empty dirs when coming back down. */
2481 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2482 fp = flist->sorted[prev_i];
2483 if (F_DEPTH(fp) >= 0)
2485 prev_i = -F_DEPTH(fp)-1;
2488 prev_depth = F_DEPTH(file);
2489 if (is_excluded(f_name(file, fbuf), 1,
2491 /* Keep dirs through this dir. */
2492 for (j = prev_depth-1; ; j--) {
2493 fp = flist->sorted[prev_i];
2494 if (F_DEPTH(fp) >= 0)
2496 prev_i = -F_DEPTH(fp)-1;
2500 F_DEPTH(file) = -prev_i-1;
2503 /* Keep dirs through this non-dir. */
2504 for (j = prev_depth; ; j--) {
2505 fp = flist->sorted[prev_i];
2506 if (F_DEPTH(fp) >= 0)
2508 prev_i = -F_DEPTH(fp)-1;
2513 /* Dump all remaining empty dirs. */
2515 struct file_struct *fp = flist->sorted[prev_i];
2516 if (F_DEPTH(fp) >= 0)
2518 prev_i = -F_DEPTH(fp)-1;
2522 for (i = flist->low; i <= flist->high; i++) {
2523 if (F_IS_ACTIVE(flist->sorted[i]))
2527 for (i = flist->high; i >= flist->low; i--) {
2528 if (F_IS_ACTIVE(flist->sorted[i]))
2535 static void output_flist(struct file_list *flist)
2537 char uidbuf[16], gidbuf[16], depthbuf[16];
2538 struct file_struct *file;
2539 const char *root, *dir, *slash, *name, *trail;
2540 const char *who = who_am_i();
2543 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2544 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2545 for (i = 0; i < flist->used; i++) {
2546 file = flist->sorted[i];
2547 if ((am_root || am_sender) && uid_ndx) {
2548 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2553 static char parens[] = "(\0)\0\0\0";
2554 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2555 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2556 pp, F_GROUP(file), pp + 2);
2560 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2561 if (F_IS_ACTIVE(file)) {
2562 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2563 if ((dir = file->dirname) == NULL)
2567 name = file->basename;
2568 trail = S_ISDIR(file->mode) ? "/" : "";
2570 root = dir = slash = name = trail = "";
2572 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2573 who, i + flist->ndx_start,
2574 root, dir, slash, name, trail,
2575 (int)file->mode, (double)F_LENGTH(file),
2576 uidbuf, gidbuf, file->flags);
2580 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2581 enum fnc_type { t_PATH, t_ITEM };
2583 static int found_prefix;
2585 /* Compare the names of two file_struct entities, similar to how strcmp()
2586 * would do if it were operating on the joined strings.
2588 * Some differences beginning with protocol_version 29: (1) directory names
2589 * are compared with an assumed trailing slash so that they compare in a
2590 * way that would cause them to sort immediately prior to any content they
2591 * may have; (2) a directory of any name compares after a non-directory of
2592 * any name at the same depth; (3) a directory with name "." compares prior
2593 * to anything else. These changes mean that a directory and a non-dir
2594 * with the same name will not compare as equal (protocol_version >= 29).
2596 * The dirname component can be an empty string, but the basename component
2597 * cannot (and never is in the current codebase). The basename component
2598 * may be NULL (for a removed item), in which case it is considered to be
2599 * after any existing item. */
2600 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2603 const uchar *c1, *c2;
2604 enum fnc_state state1, state2;
2605 enum fnc_type type1, type2;
2606 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2608 if (!f1 || !F_IS_ACTIVE(f1)) {
2609 if (!f2 || !F_IS_ACTIVE(f2))
2613 if (!f2 || !F_IS_ACTIVE(f2))
2616 c1 = (uchar*)f1->dirname;
2617 c2 = (uchar*)f2->dirname;
2621 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2622 c1 = (const uchar*)f1->basename;
2623 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2625 state1 = s_TRAILING;
2634 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2635 c2 = (const uchar*)f2->basename;
2636 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2638 state2 = s_TRAILING;
2648 return type1 == t_PATH ? 1 : -1;
2658 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2659 c1 = (const uchar*)f1->basename;
2660 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2662 state1 = s_TRAILING;
2668 state1 = s_TRAILING;
2669 if (type1 == t_PATH) {
2678 if (*c2 && type1 != type2)
2679 return type1 == t_PATH ? 1 : -1;
2688 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2689 c2 = (const uchar*)f2->basename;
2690 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2692 state2 = s_TRAILING;
2698 state2 = s_TRAILING;
2699 if (type2 == t_PATH) {
2712 return type1 == t_PATH ? 1 : -1;
2714 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2719 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2720 * not match if f2's basename is not an exact match of a path element in f1.
2721 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2722 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2726 return found_prefix;
2729 char *f_name_buf(void)
2731 static char names[5][MAXPATHLEN];
2732 static unsigned int n;
2734 n = (n + 1) % (sizeof names / sizeof names[0]);
2739 /* Return a copy of the full filename of a flist entry, using the indicated
2740 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2741 * done because we checked the size when creating the file_struct entry.
2743 char *f_name(const struct file_struct *f, char *fbuf)
2745 if (!f || !F_IS_ACTIVE(f))
2749 fbuf = f_name_buf();
2752 int len = strlen(f->dirname);
2753 memcpy(fbuf, f->dirname, len);
2755 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2757 strlcpy(fbuf, f->basename, MAXPATHLEN);
2762 /* Do a non-recursive scan of the named directory, possibly ignoring all
2763 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2764 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2765 * buffer (the functions we call will append names onto the end, but the old
2766 * dir value will be restored on exit). */
2767 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2769 struct file_list *dirlist;
2770 char dirbuf[MAXPATHLEN];
2771 int save_recurse = recurse;
2772 int save_xfer_dirs = xfer_dirs;
2773 int save_prune_empty_dirs = prune_empty_dirs;
2776 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2777 if (dlen >= MAXPATHLEN)
2782 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2786 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2787 xfer_dirs = save_xfer_dirs;
2788 recurse = save_recurse;
2790 flist_count_offset += dirlist->used;
2792 prune_empty_dirs = 0;
2793 dirlist->sorted = dirlist->files;
2794 clean_flist(dirlist, 0);
2795 prune_empty_dirs = save_prune_empty_dirs;
2798 output_flist(dirlist);