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;
340 static uint32 rdev_major;
343 static char *user_name, *group_name;
344 static char lastname[MAXPATHLEN];
345 char fname[MAXPATHLEN];
346 int first_hlink_ndx = -1;
351 if (ic_send != (iconv_t)-1) {
354 INIT_CONST_XBUF(outbuf, fname);
357 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
358 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
359 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
362 outbuf.buf[outbuf.len++] = '/';
365 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
366 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
368 io_error |= IOERR_GENERAL;
370 "[%s] cannot convert filename: %s (%s)\n",
371 who_am_i(), f_name(file, fname), strerror(errno));
374 outbuf.buf[outbuf.len] = '\0';
379 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
381 if (file->mode == mode)
382 xflags |= XMIT_SAME_MODE;
386 if (protocol_version >= 30 && S_ISDIR(mode) && !(file->flags & FLAG_XFER_DIR))
387 xflags |= XMIT_NON_XFER_DIR;
389 if ((preserve_devices && IS_DEVICE(mode))
390 || (preserve_specials && IS_SPECIAL(mode))) {
391 if (protocol_version < 28) {
392 if (tmp_rdev == rdev)
393 xflags |= XMIT_SAME_RDEV_pre28;
398 if ((uint32)major(rdev) == rdev_major)
399 xflags |= XMIT_SAME_RDEV_MAJOR;
401 rdev_major = major(rdev);
402 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
403 xflags |= XMIT_RDEV_MINOR_8_pre30;
405 } else if (protocol_version < 28)
406 rdev = MAKEDEV(0, 0);
408 if ((uid_t)F_OWNER(file) == uid && *lastname)
409 xflags |= XMIT_SAME_UID;
412 if (uid_ndx && !numeric_ids) {
413 user_name = add_uid(uid);
414 if (inc_recurse && user_name)
415 xflags |= XMIT_USER_NAME_FOLLOWS;
420 if ((gid_t)F_GROUP(file) == gid && *lastname)
421 xflags |= XMIT_SAME_GID;
424 if (gid_ndx && !numeric_ids) {
425 group_name = add_gid(gid);
426 if (inc_recurse && group_name)
427 xflags |= XMIT_GROUP_NAME_FOLLOWS;
431 if (file->modtime == modtime)
432 xflags |= XMIT_SAME_TIME;
434 modtime = file->modtime;
436 #ifdef SUPPORT_HARD_LINKS
438 if (protocol_version >= 30) {
439 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
440 first_hlink_ndx = (int32)(long)np->data - 1;
441 if (first_hlink_ndx < 0) {
442 high_hlink_ndx = ndx + first_ndx;
443 np->data = (void*)(long)(high_hlink_ndx + 1);
444 xflags |= XMIT_HLINK_FIRST;
446 xflags |= XMIT_HLINKED;
448 if (tmp_dev == dev) {
449 if (protocol_version >= 28)
450 xflags |= XMIT_SAME_DEV_pre30;
453 xflags |= XMIT_HLINKED;
459 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
461 l2 = strlen(fname+l1);
464 xflags |= XMIT_SAME_NAME;
466 xflags |= XMIT_LONG_NAME;
468 /* We must make sure we don't send a zero flag byte or the
469 * other end will terminate the flist transfer. Note that
470 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
471 * it's harmless way to add a bit to the first flag byte. */
472 if (protocol_version >= 28) {
473 if (!xflags && !S_ISDIR(mode))
474 xflags |= XMIT_TOP_DIR;
475 if ((xflags & 0xFF00) || !xflags) {
476 xflags |= XMIT_EXTENDED_FLAGS;
477 write_shortint(f, xflags);
479 write_byte(f, xflags);
481 if (!(xflags & 0xFF))
482 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
483 write_byte(f, xflags);
485 if (xflags & XMIT_SAME_NAME)
487 if (xflags & XMIT_LONG_NAME)
488 write_varint30(f, l2);
491 write_buf(f, fname + l1, l2);
493 if (first_hlink_ndx >= 0) {
494 write_varint(f, first_hlink_ndx);
495 if (first_hlink_ndx >= first_ndx)
499 write_varlong30(f, F_LENGTH(file), 3);
500 if (!(xflags & XMIT_SAME_TIME)) {
501 if (protocol_version >= 30)
502 write_varlong(f, modtime, 4);
504 write_int(f, modtime);
506 if (!(xflags & XMIT_SAME_MODE))
507 write_int(f, to_wire_mode(mode));
508 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
509 if (protocol_version < 30)
512 write_varint(f, uid);
513 if (xflags & XMIT_USER_NAME_FOLLOWS) {
514 int len = strlen(user_name);
516 write_buf(f, user_name, len);
520 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
521 if (protocol_version < 30)
524 write_varint(f, gid);
525 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
526 int len = strlen(group_name);
528 write_buf(f, group_name, len);
532 if ((preserve_devices && IS_DEVICE(mode))
533 || (preserve_specials && IS_SPECIAL(mode))) {
534 if (protocol_version < 28) {
535 if (!(xflags & XMIT_SAME_RDEV_pre28))
536 write_int(f, (int)rdev);
538 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
539 write_varint30(f, major(rdev));
540 if (protocol_version >= 30)
541 write_varint(f, minor(rdev));
542 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
543 write_byte(f, minor(rdev));
545 write_int(f, minor(rdev));
550 if (preserve_links && S_ISLNK(mode)) {
551 const char *sl = F_SYMLINK(file);
552 int len = strlen(sl);
553 write_varint30(f, len);
554 write_buf(f, sl, len);
558 #ifdef SUPPORT_HARD_LINKS
559 if (tmp_dev != 0 && protocol_version < 30) {
560 if (protocol_version < 26) {
561 /* 32-bit dev_t and ino_t */
562 write_int(f, (int32)dev);
563 write_int(f, (int32)tmp_ino);
565 /* 64-bit dev_t and ino_t */
566 if (!(xflags & XMIT_SAME_DEV_pre30))
567 write_longint(f, dev);
568 write_longint(f, tmp_ino);
573 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
578 /* Prior to 28, we sent a useless set of nulls. */
581 write_buf(f, sum, checksum_len);
585 strlcpy(lastname, fname, MAXPATHLEN);
587 if (S_ISREG(mode) || S_ISLNK(mode))
588 stats.total_size += F_LENGTH(file);
591 static struct file_struct *recv_file_entry(struct file_list *flist,
594 static int64 modtime;
598 static uint32 rdev_major;
601 static uint16 gid_flags;
602 static char lastname[MAXPATHLEN], *lastdir;
603 static int lastdir_depth, lastdir_len = -1;
604 static unsigned int del_hier_name_len = 0;
605 static int in_del_hier = 0;
606 char thisname[MAXPATHLEN];
607 unsigned int l1 = 0, l2 = 0;
608 int alloc_len, basename_len, linkname_len;
609 int extra_len = file_extra_cnt * EXTRA_LEN;
610 int first_hlink_ndx = -1;
612 const char *basename;
613 struct file_struct *file;
617 if (xflags & XMIT_SAME_NAME)
620 if (xflags & XMIT_LONG_NAME)
621 l2 = read_varint30(f);
625 if (l2 >= MAXPATHLEN - l1) {
627 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
628 xflags, l1, l2, lastname, who_am_i());
629 overflow_exit("recv_file_entry");
632 strlcpy(thisname, lastname, l1 + 1);
633 read_sbuf(f, &thisname[l1], l2);
634 thisname[l1 + l2] = 0;
636 /* Abuse basename_len for a moment... */
637 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
640 if (ic_recv != (iconv_t)-1) {
643 INIT_CONST_XBUF(outbuf, thisname);
644 INIT_XBUF(inbuf, lastname, basename_len, -1);
646 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
647 io_error |= IOERR_GENERAL;
649 "[%s] cannot convert filename: %s (%s)\n",
650 who_am_i(), lastname, strerror(errno));
653 outbuf.buf[outbuf.len] = '\0';
657 clean_fname(thisname, 0);
660 sanitize_path(thisname, thisname, "", 0, NULL);
662 if ((basename = strrchr(thisname, '/')) != NULL) {
663 int len = basename++ - thisname;
664 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
665 lastdir = new_array(char, len + 1);
666 memcpy(lastdir, thisname, len);
669 lastdir_depth = count_dir_elements(lastdir);
673 basename_len = strlen(basename) + 1; /* count the '\0' */
675 #ifdef SUPPORT_HARD_LINKS
676 if (protocol_version >= 30
677 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
678 first_hlink_ndx = read_varint(f);
679 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
681 "hard-link reference out of range: %d (%d)\n",
682 first_hlink_ndx, flist->ndx_start + flist->used);
683 exit_cleanup(RERR_PROTOCOL);
685 if (first_hlink_ndx >= flist->ndx_start) {
686 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
687 file_length = F_LENGTH(first);
688 modtime = first->modtime;
691 uid = F_OWNER(first);
693 gid = F_GROUP(first);
694 if ((preserve_devices && IS_DEVICE(mode))
695 || (preserve_specials && IS_SPECIAL(mode))) {
696 uint32 *devp = F_RDEV_P(first);
697 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
698 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
700 if (preserve_links && S_ISLNK(mode))
701 linkname_len = strlen(F_SYMLINK(first)) + 1;
709 file_length = read_varlong30(f, 3);
710 if (!(xflags & XMIT_SAME_TIME)) {
711 if (protocol_version >= 30) {
712 modtime = read_varlong(f, 4);
713 #if SIZEOF_TIME_T < SIZEOF_INT64
714 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
716 "Time value of %s truncated on receiver.\n",
721 modtime = read_int(f);
723 if (!(xflags & XMIT_SAME_MODE))
724 mode = from_wire_mode(read_int(f));
726 if (chmod_modes && !S_ISLNK(mode))
727 mode = tweak_mode(mode, chmod_modes);
729 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
730 if (protocol_version < 30)
731 uid = (uid_t)read_int(f);
733 uid = (uid_t)read_varint(f);
734 if (xflags & XMIT_USER_NAME_FOLLOWS)
735 uid = recv_user_name(f, uid);
736 else if (inc_recurse && am_root && !numeric_ids)
737 uid = match_uid(uid);
740 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
741 if (protocol_version < 30)
742 gid = (gid_t)read_int(f);
744 gid = (gid_t)read_varint(f);
746 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
747 gid = recv_group_name(f, gid, &gid_flags);
748 else if (inc_recurse && (!am_root || !numeric_ids))
749 gid = match_gid(gid, &gid_flags);
753 if ((preserve_devices && IS_DEVICE(mode))
754 || (preserve_specials && IS_SPECIAL(mode))) {
755 if (protocol_version < 28) {
756 if (!(xflags & XMIT_SAME_RDEV_pre28))
757 rdev = (dev_t)read_int(f);
760 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
761 rdev_major = read_varint30(f);
762 if (protocol_version >= 30)
763 rdev_minor = read_varint(f);
764 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
765 rdev_minor = read_byte(f);
767 rdev_minor = read_int(f);
768 rdev = MAKEDEV(rdev_major, rdev_minor);
770 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
772 } else if (protocol_version < 28)
773 rdev = MAKEDEV(0, 0);
776 if (preserve_links && S_ISLNK(mode)) {
777 linkname_len = read_varint30(f) + 1; /* count the '\0' */
778 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
779 rprintf(FERROR, "overflow: linkname_len=%d\n",
781 overflow_exit("recv_file_entry");
788 #ifdef SUPPORT_HARD_LINKS
790 if (preserve_hard_links) {
791 if (protocol_version < 28 && S_ISREG(mode))
792 xflags |= XMIT_HLINKED;
793 if (xflags & XMIT_HLINKED)
794 extra_len += (inc_recurse+1) * EXTRA_LEN;
799 /* We need one or two index int32s when we're preserving ACLs. */
801 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
804 if (always_checksum && S_ISREG(mode))
805 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
807 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
808 extra_len += EXTRA_LEN;
810 #if EXTRA_ROUNDING > 0
811 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
812 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
815 if (inc_recurse && S_ISDIR(mode)) {
816 if (one_file_system) {
817 /* Room to save the dir's device for -x */
818 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
820 pool = dir_flist->file_pool;
822 pool = flist->file_pool;
824 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
826 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
828 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
830 file = (struct file_struct *)bp;
831 bp += FILE_STRUCT_LEN;
833 memcpy(bp, basename, basename_len);
834 bp += basename_len + linkname_len; /* skip space for symlink too */
836 #ifdef SUPPORT_HARD_LINKS
837 if (xflags & XMIT_HLINKED)
838 file->flags |= FLAG_HLINKED;
840 file->modtime = (time_t)modtime;
841 file->len32 = (uint32)file_length;
842 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
843 file->flags |= FLAG_LENGTH64;
844 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
851 file->flags |= gid_flags;
855 F_NDX(file) = flist->used + flist->ndx_start;
858 if (basename != thisname) {
859 file->dirname = lastdir;
860 F_DEPTH(file) = lastdir_depth + 1;
865 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
867 if (protocol_version >= 30) {
868 if (!(xflags & XMIT_NON_XFER_DIR))
869 file->flags |= FLAG_XFER_DIR;
870 if (xflags & XMIT_TOP_DIR)
871 file->flags |= FLAG_TOP_DIR;
872 } else if (xflags & XMIT_TOP_DIR) {
873 in_del_hier = recurse;
874 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
875 if (relative_paths && del_hier_name_len > 2
876 && lastname[del_hier_name_len-1] == '.'
877 && lastname[del_hier_name_len-2] == '/')
878 del_hier_name_len -= 2;
879 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
880 } else if (in_del_hier) {
881 if (!relative_paths || !del_hier_name_len
882 || (l1 >= del_hier_name_len
883 && lastname[del_hier_name_len] == '/'))
884 file->flags |= FLAG_XFER_DIR;
890 if ((preserve_devices && IS_DEVICE(mode))
891 || (preserve_specials && IS_SPECIAL(mode))) {
892 uint32 *devp = F_RDEV_P(file);
893 DEV_MAJOR(devp) = major(rdev);
894 DEV_MINOR(devp) = minor(rdev);
899 bp = (char*)file->basename + basename_len;
900 if (first_hlink_ndx >= flist->ndx_start) {
901 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
902 memcpy(bp, F_SYMLINK(first), linkname_len);
904 read_sbuf(f, bp, linkname_len - 1);
906 sanitize_path(bp, bp, "", lastdir_depth, NULL);
910 #ifdef SUPPORT_HARD_LINKS
911 if (preserve_hard_links && xflags & XMIT_HLINKED) {
912 if (protocol_version >= 30) {
913 if (xflags & XMIT_HLINK_FIRST) {
914 high_hlink_ndx = flist->ndx_start + flist->used;
915 F_HL_GNUM(file) = high_hlink_ndx;
917 F_HL_GNUM(file) = first_hlink_ndx;
919 static int32 cnt = 0;
920 struct ht_int64_node *np;
923 if (protocol_version < 26) {
927 if (!(xflags & XMIT_SAME_DEV_pre30))
928 dev = read_longint(f);
929 ino = read_longint(f);
931 np = idev_find(dev, ino);
932 ndx = (int32)(long)np->data - 1;
935 np->data = (void*)(long)cnt;
937 F_HL_GNUM(file) = ndx;
942 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
946 /* Prior to 28, we get a useless set of nulls. */
949 if (first_hlink_ndx >= flist->ndx_start) {
950 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
951 memcpy(bp, F_SUM(first), checksum_len);
953 read_buf(f, bp, checksum_len);
957 if (preserve_acls && !S_ISLNK(mode))
958 receive_acl(file, f);
960 #ifdef SUPPORT_XATTRS
962 receive_xattr(file, f );
965 if (S_ISREG(mode) || S_ISLNK(mode))
966 stats.total_size += file_length;
971 /* Create a file_struct for a named file by reading its stat() information
972 * and performing extensive checks against global options.
974 * Returns a pointer to the new file struct, or NULL if there was an error
975 * or this file should be excluded. */
976 struct file_struct *make_file(const char *fname, struct file_list *flist,
977 STRUCT_STAT *stp, int flags, int filter_level)
979 static char *lastdir;
980 static int lastdir_len = -1;
981 struct file_struct *file;
982 char thisname[MAXPATHLEN];
983 char linkname[MAXPATHLEN];
984 int alloc_len, basename_len, linkname_len;
985 int extra_len = file_extra_cnt * EXTRA_LEN;
986 const char *basename;
991 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
992 rprintf(FINFO, "skipping overly long name: %s\n", fname);
995 clean_fname(thisname, 0);
997 sanitize_path(thisname, thisname, "", 0, NULL);
999 if (stp && S_ISDIR(stp->st_mode)) {
1000 st = *stp; /* Needed for "symlink/." with --relative. */
1001 *linkname = '\0'; /* make IBM code checker happy */
1002 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1003 int save_errno = errno;
1004 /* See if file is excluded before reporting an error. */
1005 if (filter_level != NO_FILTERS
1006 && (is_excluded(thisname, 0, filter_level)
1007 || is_excluded(thisname, 1, filter_level))) {
1008 if (ignore_perishable && save_errno != ENOENT)
1009 non_perishable_cnt++;
1012 if (save_errno == ENOENT) {
1013 #ifdef SUPPORT_LINKS
1014 /* Avoid "vanished" error if symlink points nowhere. */
1015 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1016 && S_ISLNK(st.st_mode)) {
1017 io_error |= IOERR_GENERAL;
1018 rprintf(FERROR, "symlink has no referent: %s\n",
1019 full_fname(thisname));
1023 enum logcode c = am_daemon && protocol_version < 28
1025 io_error |= IOERR_VANISHED;
1026 rprintf(c, "file has vanished: %s\n",
1027 full_fname(thisname));
1030 io_error |= IOERR_GENERAL;
1031 rsyserr(FERROR, save_errno, "readlink %s failed",
1032 full_fname(thisname));
1037 /* backup.c calls us with filter_level set to NO_FILTERS. */
1038 if (filter_level == NO_FILTERS)
1041 if (S_ISDIR(st.st_mode)) {
1043 rprintf(FINFO, "skipping directory %s\n", thisname);
1046 /* -x only affects dirs because we need to avoid recursing
1047 * into a mount-point directory, not to avoid copying a
1048 * symlinked file if -L (or similar) was specified. */
1049 if (one_file_system && flags & FLAG_XFER_DIR) {
1050 if (flags & FLAG_TOP_DIR)
1051 filesystem_dev = st.st_dev;
1052 else if (st.st_dev != filesystem_dev) {
1053 if (one_file_system > 1) {
1056 "[%s] skipping mount-point dir %s\n",
1057 who_am_i(), thisname);
1061 flags |= FLAG_MOUNT_DIR;
1065 flags &= ~FLAG_XFER_DIR;
1067 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1068 if (ignore_perishable)
1069 non_perishable_cnt++;
1073 if (lp_ignore_nonreadable(module_id)) {
1074 #ifdef SUPPORT_LINKS
1075 if (!S_ISLNK(st.st_mode))
1077 if (access(thisname, R_OK) != 0)
1083 /* Only divert a directory in the main transfer. */
1085 if (flist->prev && S_ISDIR(st.st_mode)
1086 && flags & FLAG_DIVERT_DIRS) {
1087 /* Room for parent/sibling/next-child info. */
1088 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1090 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1092 pool = dir_flist->file_pool;
1094 pool = flist->file_pool;
1099 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1100 who_am_i(), thisname, filter_level);
1103 if ((basename = strrchr(thisname, '/')) != NULL) {
1104 int len = basename++ - thisname;
1105 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1106 lastdir = new_array(char, len + 1);
1107 memcpy(lastdir, thisname, len);
1108 lastdir[len] = '\0';
1112 basename = thisname;
1113 basename_len = strlen(basename) + 1; /* count the '\0' */
1115 #ifdef SUPPORT_LINKS
1116 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1121 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1122 extra_len += EXTRA_LEN;
1124 #if EXTRA_ROUNDING > 0
1125 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1126 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1129 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1132 bp = pool_alloc(pool, alloc_len, "make_file");
1134 if (!(bp = new_array(char, alloc_len)))
1135 out_of_memory("make_file");
1138 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1140 file = (struct file_struct *)bp;
1141 bp += FILE_STRUCT_LEN;
1143 memcpy(bp, basename, basename_len);
1144 bp += basename_len + linkname_len; /* skip space for symlink too */
1146 #ifdef SUPPORT_HARD_LINKS
1147 if (preserve_hard_links && flist && flist->prev) {
1148 if (protocol_version >= 28
1149 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1150 : S_ISREG(st.st_mode)) {
1151 tmp_dev = st.st_dev;
1152 tmp_ino = st.st_ino;
1158 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1159 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1160 tmp_rdev = st.st_rdev;
1165 file->flags = flags;
1166 file->modtime = st.st_mtime;
1167 file->len32 = (uint32)st.st_size;
1168 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1169 file->flags |= FLAG_LENGTH64;
1170 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1172 file->mode = st.st_mode;
1174 F_OWNER(file) = st.st_uid;
1176 F_GROUP(file) = st.st_gid;
1178 if (basename != thisname)
1179 file->dirname = lastdir;
1181 #ifdef SUPPORT_LINKS
1183 bp = (char*)file->basename + basename_len;
1184 memcpy(bp, linkname, linkname_len);
1188 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1189 file_checksum(thisname, tmp_sum, st.st_size);
1191 F_PATHNAME(file) = pathname;
1193 /* This code is only used by the receiver when it is building
1194 * a list of files for a delete pass. */
1195 if (keep_dirlinks && linkname_len && flist) {
1197 int save_mode = file->mode;
1198 file->mode = S_IFDIR; /* Find a directory with our name. */
1199 if (flist_find(dir_flist, file) >= 0
1200 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1201 file->modtime = st2.st_mtime;
1203 file->mode = st2.st_mode;
1205 F_OWNER(file) = st2.st_uid;
1207 F_GROUP(file) = st2.st_gid;
1209 file->mode = save_mode;
1212 if (basename_len == 0+1)
1217 F_NDX(file) = dir_count - 1;
1223 /* Only called for temporary file_struct entries created by make_file(). */
1224 void unmake_file(struct file_struct *file)
1226 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1227 #if EXTRA_ROUNDING > 0
1228 if (extra_cnt & EXTRA_ROUNDING)
1229 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1231 free(REQ_EXTRA(file, extra_cnt));
1234 static struct file_struct *send_file_name(int f, struct file_list *flist,
1235 char *fname, STRUCT_STAT *stp,
1236 int flags, int filter_level)
1238 struct file_struct *file;
1239 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1243 file = make_file(fname, flist, stp, flags, filter_level);
1247 if (chmod_modes && !S_ISLNK(file->mode))
1248 file->mode = tweak_mode(file->mode, chmod_modes);
1251 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1252 sx.st.st_mode = file->mode;
1253 sx.acc_acl = sx.def_acl = NULL;
1254 if (get_acl(fname, &sx) < 0)
1258 #ifdef SUPPORT_XATTRS
1259 if (preserve_xattrs && f >= 0) {
1261 if (get_xattr(fname, &sx) < 0)
1266 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1268 flist_expand(flist, 1);
1269 flist->files[flist->used++] = file;
1271 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1273 if (preserve_acls && !S_ISLNK(file->mode)) {
1278 #ifdef SUPPORT_XATTRS
1279 if (preserve_xattrs) {
1280 F_XATTR(file) = send_xattr(&sx, f);
1288 static void send_if_directory(int f, struct file_list *flist,
1289 struct file_struct *file,
1290 char *fbuf, unsigned int ol,
1293 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1295 if (S_ISDIR(file->mode)
1296 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1298 unsigned int len = strlen(fbuf);
1299 if (len > 1 && fbuf[len-1] == '/')
1301 if (len >= MAXPATHLEN - 1) {
1302 io_error |= IOERR_GENERAL;
1303 rprintf(FERROR, "skipping long-named directory: %s\n",
1307 save_filters = push_local_filters(fbuf, len);
1308 send_directory(f, flist, fbuf, len, flags);
1309 pop_local_filters(save_filters);
1316 static int file_compare(const void *file1, const void *file2)
1318 return f_name_cmp(*(struct file_struct **)file1,
1319 *(struct file_struct **)file2);
1322 /* The guts of a merge-sort algorithm. This was derived from the glibc
1323 * version, but I (Wayne) changed the merge code to do less copying and
1324 * to require only half the amount of temporary memory. */
1325 static void fsort_tmp(struct file_struct **fp, size_t num,
1326 struct file_struct **tmp)
1328 struct file_struct **f1, **f2, **t;
1337 fsort_tmp(f1, n1, tmp);
1339 fsort_tmp(f2, n2, tmp);
1341 while (f_name_cmp(*f1, *f2) <= 0) {
1348 memcpy(t, f1, n1 * PTR_SIZE);
1350 *f1++ = *f2++, n2--;
1352 while (n1 > 0 && n2 > 0) {
1353 if (f_name_cmp(*t, *f2) <= 0)
1356 *f1++ = *f2++, n2--;
1360 memcpy(f1, t, n1 * PTR_SIZE);
1363 /* This file-struct sorting routine makes sure that any identical names in
1364 * the file list stay in the same order as they were in the original list.
1365 * This is particularly vital in inc_recurse mode where we expect a sort
1366 * on the flist to match the exact order of a sort on the dir_flist. */
1367 static void fsort(struct file_struct **fp, size_t num)
1373 qsort(fp, num, PTR_SIZE, file_compare);
1375 struct file_struct **tmp = new_array(struct file_struct *,
1377 fsort_tmp(fp, num, tmp);
1382 /* We take an entire set of sibling dirs from the sorted flist and link them
1383 * into the tree, setting the appropriate parent/child/sibling pointers. */
1384 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1389 int32 *parent_dp = parent_ndx < 0 ? NULL
1390 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1392 flist_expand(dir_flist, dir_cnt);
1393 dir_flist->sorted = dir_flist->files;
1395 for (i = 0; dir_cnt; i++) {
1396 struct file_struct *file = from_flist->sorted[i];
1398 if (!S_ISDIR(file->mode))
1401 dir_flist->files[dir_flist->used++] = file;
1405 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1407 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1409 send_dir_ndx = dir_flist->used - 1;
1411 dp = F_DIR_NODE_P(file);
1412 DIR_PARENT(dp) = parent_ndx;
1413 DIR_FIRST_CHILD(dp) = -1;
1416 DIR_NEXT_SIBLING(dp) = -1;
1419 /* This function is normally called by the sender, but the receiving side also
1420 * calls it from get_dirlist() with f set to -1 so that we just construct the
1421 * file list in memory without sending it over the wire. Also, get_dirlist()
1422 * might call this with f set to -2, which also indicates that local filter
1423 * rules should be ignored. */
1424 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1431 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1432 int start = flist->used;
1433 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1435 assert(flist != NULL);
1437 if (!(d = opendir(fbuf))) {
1438 io_error |= IOERR_GENERAL;
1439 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1444 if (len != 1 || *fbuf != '/')
1447 remainder = MAXPATHLEN - (p - fbuf);
1449 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1450 char *dname = d_name(di);
1451 if (dname[0] == '.' && (dname[1] == '\0'
1452 || (dname[1] == '.' && dname[2] == '\0')))
1454 if (strlcpy(p, dname, remainder) >= remainder) {
1455 io_error |= IOERR_GENERAL;
1457 "cannot send long-named file %s\n",
1461 if (dname[0] == '\0') {
1462 io_error |= IOERR_GENERAL;
1464 "cannot send file with empty name in %s\n",
1469 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1475 io_error |= IOERR_GENERAL;
1476 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1481 if (f >= 0 && recurse && !divert_dirs) {
1482 int i, end = flist->used - 1;
1483 /* send_if_directory() bumps flist->used, so use "end". */
1484 for (i = start; i <= end; i++)
1485 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1489 static char lastpath[MAXPATHLEN] = "";
1490 static int lastpath_len = 0;
1491 static struct file_struct *lastpath_struct;
1493 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1494 char *start, char *limit, int flags, int is_dot_dir)
1496 struct file_struct *file;
1497 item_list *relname_list;
1498 relnamecache **rnpp;
1500 int len, need_new_dir;
1502 flags &= ~FLAG_XFER_DIR;
1505 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1506 && lastpath_len == limit - fname
1507 && strncmp(lastpath, fname, lastpath_len) == 0)
1515 int save_copy_links = copy_links;
1516 int save_xfer_dirs = xfer_dirs;
1518 copy_links = xfer_dirs = 1;
1522 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1524 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1528 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1530 if (file && !S_ISDIR(file->mode))
1532 lastpath_struct = file;
1535 strlcpy(lastpath, fname, sizeof lastpath);
1536 lastpath_len = limit - fname;
1540 copy_links = save_copy_links;
1541 xfer_dirs = save_xfer_dirs;
1547 if (!lastpath_struct)
1548 return; /* dir must have vanished */
1550 len = strlen(limit+1);
1551 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1552 if (!relname_list) {
1553 if (!(relname_list = new0(item_list)))
1554 out_of_memory("send_implied_dirs");
1555 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1557 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1558 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1559 out_of_memory("send_implied_dirs");
1560 (*rnpp)->is_dot_dir = is_dot_dir;
1561 strlcpy((*rnpp)->fname, limit+1, len + 1);
1564 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1566 char fbuf[MAXPATHLEN];
1567 item_list *relname_list;
1568 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1572 dlen = strlen(fbuf);
1574 if (F_PATHNAME(file) != pathname) {
1575 if (!push_pathname(F_PATHNAME(file), -1))
1576 exit_cleanup(RERR_FILESELECT);
1579 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1581 if (BITS_SETnUNSET(file->flags, FLAG_XFER_DIR, FLAG_MOUNT_DIR))
1582 send_directory(f, flist, fbuf, dlen, flags);
1584 if (!relative_paths)
1587 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1591 for (j = 0; j < relname_list->count; j++) {
1593 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1594 int is_dot_dir = rnp->is_dot_dir;
1597 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1599 if (len >= (int)sizeof fbuf)
1600 continue; /* Impossible... */
1602 slash = strchr(fbuf+dlen+1, '/');
1604 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
1610 if (link_stat(fbuf, &st, 1) != 0) {
1611 io_error |= IOERR_GENERAL;
1612 rsyserr(FERROR, errno, "link_stat %s failed",
1616 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1618 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1624 void send_extra_file_list(int f, int at_least)
1626 struct file_list *flist;
1629 int future_cnt, save_io_error = io_error;
1634 /* Keep sending data until we have the requested number of
1635 * files in the upcoming file-lists. */
1636 if (cur_flist->next) {
1637 flist = first_flist->prev; /* the newest flist */
1638 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1641 while (future_cnt < at_least) {
1642 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1643 int dir_ndx, dstart = dir_count;
1644 const char *pathname = F_PATHNAME(file);
1647 flist = flist_new(0, "send_extra_file_list");
1648 start_write = stats.total_written;
1652 dir_ndx = F_NDX(file);
1655 dir_ndx = send_dir_ndx;
1656 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1657 flist->parent_ndx = dir_ndx;
1659 send1extra(f, file, flist);
1660 prev_flags = file->flags;
1661 dp = F_DIR_NODE_P(file);
1663 /* If there are any duplicate directory names that follow, we
1664 * send all the dirs together in one file-list. The dir_flist
1665 * tree links all the child subdirs onto the last dup dir. */
1666 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1667 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1668 send_dir_ndx = dir_ndx;
1669 file = dir_flist->sorted[dir_ndx];
1670 /* Try to avoid some duplicate scanning of identical dirs. */
1671 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_XFER_DIR)
1672 file->flags &= ~FLAG_XFER_DIR;
1673 send1extra(f, file, flist);
1674 prev_flags = file->flags;
1675 dp = F_DIR_NODE_P(file);
1681 if (need_unsorted_flist) {
1682 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1683 out_of_memory("send_extra_file_list");
1684 memcpy(flist->sorted, flist->files,
1685 flist->used * sizeof (struct file_struct*));
1688 flist->sorted = flist->files;
1690 clean_flist(flist, 0);
1692 flist->ndx_end = flist->ndx_start + flist->used - 1
1693 - (dir_count - dstart);
1695 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1696 flist_done_allocating(flist);
1698 file_total += flist->used;
1699 future_cnt += flist->used;
1700 stats.flist_size += stats.total_written - start_write;
1701 stats.num_files += flist->used;
1703 output_flist(flist);
1705 if (DIR_FIRST_CHILD(dp) >= 0) {
1706 send_dir_ndx = DIR_FIRST_CHILD(dp);
1709 while (DIR_NEXT_SIBLING(dp) < 0) {
1710 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1711 write_ndx(f, NDX_FLIST_EOF);
1713 change_local_filter_dir(NULL, 0, 0);
1717 file = dir_flist->sorted[send_dir_ndx];
1718 dp = F_DIR_NODE_P(file);
1720 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1725 if (io_error != save_io_error && !ignore_errors)
1726 send_msg_int(MSG_IO_ERROR, io_error);
1729 struct file_list *send_file_list(int f, int argc, char *argv[])
1731 static const char *lastdir;
1732 static int lastdir_len = -1;
1736 struct file_list *flist;
1737 struct timeval start_tv, end_tv;
1740 int disable_buffering;
1741 int flags = recurse ? FLAG_XFER_DIR : 0;
1742 int reading_remotely = filesfrom_host != NULL;
1743 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1745 | (filesfrom_convert ? RL_CONVERT : 0)
1747 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1749 rprintf(FLOG, "building file list\n");
1750 if (show_filelist_p())
1751 start_filelist_progress("building file list");
1752 else if (inc_recurse && verbose && !am_server)
1753 rprintf(FCLIENT, "sending incremental file list\n");
1755 start_write = stats.total_written;
1756 gettimeofday(&start_tv, NULL);
1758 #ifdef SUPPORT_HARD_LINKS
1759 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1763 flist = cur_flist = flist_new(0, "send_file_list");
1765 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1766 flags |= FLAG_DIVERT_DIRS;
1768 dir_flist = cur_flist;
1770 disable_buffering = io_start_buffering_out(f);
1771 if (filesfrom_fd >= 0) {
1772 if (argv[0] && !push_dir(argv[0], 0)) {
1773 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1774 full_fname(argv[0]), curr_dir);
1775 exit_cleanup(RERR_FILESELECT);
1781 char fbuf[MAXPATHLEN];
1786 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1788 sanitize_path(fbuf, fbuf, "", 0, NULL);
1792 strlcpy(fbuf, *argv++, MAXPATHLEN);
1794 sanitize_path(fbuf, fbuf, "", 0, NULL);
1798 if (relative_paths) {
1799 /* We clean up fbuf below. */
1801 } else if (!len || fbuf[len - 1] == '/') {
1802 if (len == 2 && fbuf[0] == '.') {
1803 /* Turn "./" into just "." rather than "./." */
1806 if (len + 1 >= MAXPATHLEN)
1807 overflow_exit("send_file_list");
1812 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1813 && (len == 2 || fbuf[len-3] == '/')) {
1814 if (len + 2 >= MAXPATHLEN)
1815 overflow_exit("send_file_list");
1821 is_dot_dir = fbuf[len-1] == '.'
1822 && (len == 1 || fbuf[len-2] == '/');
1827 if (!relative_paths) {
1828 p = strrchr(fbuf, '/');
1835 len -= p - fbuf + 1;
1840 if ((p = strstr(fbuf, "/./")) != NULL) {
1846 len -= p - fbuf + 3;
1850 /* Get rid of trailing "/" and "/.". */
1852 if (fn[len - 1] == '/') {
1854 if (!--len && !dir) {
1859 else if (len >= 2 && fn[len - 1] == '.'
1860 && fn[len - 2] == '/') {
1862 if (!(len -= 2) && !dir) {
1874 } else if (fn[0] == '.')
1877 /* Reject a ".." dir in the active part of the path. */
1878 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1879 if ((p[2] == '/' || p[2] == '\0')
1880 && (p == fn || p[-1] == '/')) {
1882 "found \"..\" dir in relative path: %s\n",
1884 exit_cleanup(RERR_SYNTAX);
1895 dirlen = dir ? strlen(dir) : 0;
1896 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1897 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1900 lastdir_len = pathname_len;
1901 } else if (!push_pathname(lastdir, lastdir_len))
1905 memmove(fbuf, fn, len + 1);
1907 if (link_stat(fbuf, &st, copy_dirlinks || is_dot_dir) != 0) {
1908 io_error |= IOERR_GENERAL;
1909 rsyserr(FERROR, errno, "link_stat %s failed",
1914 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1915 rprintf(FINFO, "skipping directory %s\n", fbuf);
1919 if (inc_recurse && relative_paths && *fbuf) {
1920 if ((p = strchr(fbuf+1, '/')) != NULL) {
1921 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, is_dot_dir);
1924 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1925 /* Send the implied directories at the start of the
1926 * source spec, so we get their permissions right. */
1927 char *lp = lastpath, *slash = fbuf;
1929 /* Skip any initial directories in our path that we
1930 * have in common with lastpath. */
1931 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1936 if (fn != p || (*lp && *lp != '/'))
1937 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1940 if (recurse || (xfer_dirs && is_dot_dir)) {
1941 struct file_struct *file;
1942 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags;
1943 file = send_file_name(f, flist, fbuf, &st,
1944 top_flags, ALL_FILTERS);
1945 if (file && !inc_recurse)
1946 send_if_directory(f, flist, file, fbuf, len, flags);
1948 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1951 gettimeofday(&end_tv, NULL);
1952 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1953 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1954 if (stats.flist_buildtime == 0)
1955 stats.flist_buildtime = 1;
1958 write_byte(f, 0); /* Indicate end of file list */
1960 #ifdef SUPPORT_HARD_LINKS
1961 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1965 if (show_filelist_p())
1966 finish_filelist_progress(flist);
1968 gettimeofday(&end_tv, NULL);
1969 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1970 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1972 /* When converting names, both sides keep an unsorted file-list array
1973 * because the names will differ on the sending and receiving sides
1974 * (both sides will use the unsorted index number for each item). */
1976 /* Sort the list without removing any duplicates. This allows the
1977 * receiving side to ask for whatever name it kept. For incremental
1978 * recursion mode, the sender marks duplicate dirs so that it can
1979 * send them together in a single file-list. */
1981 if (need_unsorted_flist) {
1983 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1984 out_of_memory("send_file_list");
1985 memcpy(flist->sorted, flist->files,
1986 flist->used * sizeof (struct file_struct*));
1987 clean_flist(flist, 0);
1989 flist->sorted = flist->files;
1991 flist->high = flist->used - 1;
1996 flist->sorted = flist->files;
1997 clean_flist(flist, 0);
1999 file_total += flist->used;
2001 /* We don't subtract dir_count for the first send since we
2002 * might have one or more dot dirs which need to get sent. */
2003 flist->ndx_end = flist->ndx_start + flist->used - 1;
2005 if (!numeric_ids && !inc_recurse)
2008 /* send the io_error flag */
2009 if (protocol_version < 30)
2010 write_int(f, ignore_errors ? 0 : io_error);
2011 else if (io_error && !ignore_errors)
2012 send_msg_int(MSG_IO_ERROR, io_error);
2014 if (disable_buffering)
2015 io_end_buffering_out();
2017 stats.flist_size = stats.total_written - start_write;
2018 stats.num_files = flist->used;
2021 output_flist(flist);
2024 rprintf(FINFO, "send_file_list done\n");
2027 add_dirs_to_tree(-1, flist, dir_count);
2028 flist_done_allocating(flist);
2029 if (send_dir_ndx < 0) {
2030 write_ndx(f, NDX_FLIST_EOF);
2033 else if (file_total == 1) {
2034 /* If we're creating incremental file-lists and there
2035 * was just 1 item in the first file-list, send 1 more
2036 * file-list to check if this is a 1-file xfer. */
2037 send_extra_file_list(f, 1);
2044 struct file_list *recv_file_list(int f)
2046 struct file_list *flist;
2051 rprintf(FLOG, "receiving file list\n");
2052 if (show_filelist_p())
2053 start_filelist_progress("receiving file list");
2054 else if (inc_recurse && verbose && !am_server && !first_flist)
2055 rprintf(FCLIENT, "receiving incremental file list\n");
2057 start_read = stats.total_read;
2059 #ifdef SUPPORT_HARD_LINKS
2060 if (preserve_hard_links && !first_flist)
2064 flist = flist_new(0, "recv_file_list");
2067 if (flist->ndx_start == 0)
2068 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2069 dstart = dir_flist->used;
2075 while ((flags = read_byte(f)) != 0) {
2076 struct file_struct *file;
2078 flist_expand(flist, 1);
2080 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2081 flags |= read_byte(f) << 8;
2082 file = recv_file_entry(flist, flags, f);
2084 if (inc_recurse && S_ISDIR(file->mode)) {
2085 flist_expand(dir_flist, 1);
2086 dir_flist->files[dir_flist->used++] = file;
2089 flist->files[flist->used++] = file;
2091 maybe_emit_filelist_progress(flist->used);
2094 rprintf(FINFO, "recv_file_name(%s)\n",
2095 f_name(file, NULL));
2098 file_total += flist->used;
2100 flist->ndx_end = flist->ndx_start + flist->used - 1;
2101 if (inc_recurse && flist->ndx_start)
2102 flist->ndx_end -= dir_flist->used - dstart;
2105 rprintf(FINFO, "received %d names\n", flist->used);
2107 if (show_filelist_p())
2108 finish_filelist_progress(flist);
2111 if (need_unsorted_flist) {
2112 /* Create an extra array of index pointers that we can sort for
2113 * the generator's use (for wading through the files in sorted
2114 * order and for calling flist_find()). We keep the "files"
2115 * list unsorted for our exchange of index numbers with the
2116 * other side (since their names may not sort the same). */
2117 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2118 out_of_memory("recv_file_list");
2119 memcpy(flist->sorted, flist->files,
2120 flist->used * sizeof (struct file_struct*));
2121 if (inc_recurse && dir_flist->used > dstart) {
2122 dir_flist->sorted = realloc_array(dir_flist->sorted,
2123 struct file_struct *,
2125 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2126 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2127 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2132 flist->sorted = flist->files;
2133 if (inc_recurse && dir_flist->used > dstart) {
2134 dir_flist->sorted = dir_flist->files;
2135 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2140 flist_done_allocating(flist);
2142 recv_id_list(f, flist);
2144 clean_flist(flist, relative_paths);
2146 if (protocol_version < 30) {
2147 /* Recv the io_error flag */
2151 io_error |= read_int(f);
2155 output_flist(flist);
2158 rprintf(FINFO, "recv_file_list done\n");
2160 stats.flist_size += stats.total_read - start_read;
2161 stats.num_files += flist->used;
2166 /* This is only used once by the receiver if the very first file-list
2167 * has exactly one item in it. */
2168 void recv_additional_file_list(int f)
2170 struct file_list *flist;
2171 int ndx = read_ndx(f);
2172 if (ndx == NDX_FLIST_EOF) {
2174 change_local_filter_dir(NULL, 0, 0);
2176 ndx = NDX_FLIST_OFFSET - ndx;
2177 if (ndx < 0 || ndx >= dir_flist->used) {
2178 ndx = NDX_FLIST_OFFSET - ndx;
2180 "[%s] Invalid dir index: %d (%d - %d)\n",
2181 who_am_i(), ndx, NDX_FLIST_OFFSET,
2182 NDX_FLIST_OFFSET - dir_flist->used + 1);
2183 exit_cleanup(RERR_PROTOCOL);
2186 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2189 flist = recv_file_list(f);
2190 flist->parent_ndx = ndx;
2194 /* Search for an identically-named item in the file list. Note that the
2195 * items must agree in their directory-ness, or no match is returned. */
2196 int flist_find(struct file_list *flist, struct file_struct *f)
2198 int low = flist->low, high = flist->high;
2199 int diff, mid, mid_up;
2201 while (low <= high) {
2202 mid = (low + high) / 2;
2203 if (F_IS_ACTIVE(flist->sorted[mid]))
2206 /* Scan for the next non-empty entry using the cached
2207 * distance values. If the value isn't fully up-to-
2208 * date, update it. */
2209 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2210 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2212 mid_up += F_DEPTH(flist->sorted[mid_up]);
2213 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2214 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2216 if (mid_up > high) {
2217 /* If there's nothing left above us, set high to
2218 * a non-empty entry below us and continue. */
2219 high = mid - (int)flist->sorted[mid]->len32;
2220 if (!F_IS_ACTIVE(flist->sorted[high])) {
2222 high -= (int)flist->sorted[high]->len32;
2223 } while (!F_IS_ACTIVE(flist->sorted[high]));
2224 flist->sorted[mid]->len32 = mid - high;
2229 diff = f_name_cmp(flist->sorted[mid_up], f);
2231 if (protocol_version < 29
2232 && S_ISDIR(flist->sorted[mid_up]->mode)
2233 != S_ISDIR(f->mode))
2246 * Free up any resources a file_struct has allocated
2247 * and clear the file.
2249 void clear_file(struct file_struct *file)
2251 /* The +1 zeros out the first char of the basename. */
2252 memset(file, 0, FILE_STRUCT_LEN + 1);
2253 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2254 * entry. Likewise for len32 in the opposite direction. We assume
2255 * that we're alone for now since flist_find() will adjust the counts
2256 * it runs into that aren't up-to-date. */
2257 file->len32 = F_DEPTH(file) = 1;
2260 /* Allocate a new file list. */
2261 struct file_list *flist_new(int flags, char *msg)
2263 struct file_list *flist;
2265 if (!(flist = new0(struct file_list)))
2268 if (flags & FLIST_TEMP) {
2269 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2270 out_of_memory, POOL_INTERN)))
2273 /* This is a doubly linked list with prev looping back to
2274 * the end of the list, but the last next pointer is NULL. */
2276 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2277 out_of_memory, POOL_INTERN);
2278 if (!flist->file_pool)
2281 first_flist = cur_flist = flist->prev = flist;
2283 flist->file_pool = first_flist->file_pool;
2285 flist->ndx_start = first_flist->prev->ndx_end + 2;
2286 if (flist->ndx_start <= high_hlink_ndx)
2287 flist->ndx_start = high_hlink_ndx + 1;
2289 flist->prev = first_flist->prev;
2290 flist->prev->next = first_flist->prev = flist;
2292 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2299 /* Free up all elements in a flist. */
2300 void flist_free(struct file_list *flist)
2303 /* Was FLIST_TEMP dir-list. */
2304 } else if (flist == flist->prev) {
2305 first_flist = cur_flist = NULL;
2309 if (flist == cur_flist)
2310 cur_flist = flist->next;
2311 if (flist == first_flist)
2312 first_flist = first_flist->next;
2314 flist->prev->next = flist->next;
2316 flist->next = first_flist;
2318 flist->next->prev = flist->prev;
2319 file_total -= flist->used;
2323 if (!flist->prev || !flist_cnt)
2324 pool_destroy(flist->file_pool);
2326 pool_free_old(flist->file_pool, flist->pool_boundary);
2328 if (flist->sorted && flist->sorted != flist->files)
2329 free(flist->sorted);
2334 /* This routine ensures we don't have any duplicate names in our file list.
2335 * duplicate names can cause corruption because of the pipelining. */
2336 static void clean_flist(struct file_list *flist, int strip_root)
2338 char fbuf[MAXPATHLEN];
2343 if (flist->used == 0) {
2349 fsort(flist->sorted, flist->used);
2351 if (!am_sender || inc_recurse) {
2352 for (i = prev_i = 0; i < flist->used; i++) {
2353 if (F_IS_ACTIVE(flist->sorted[i])) {
2358 flist->low = prev_i;
2360 i = prev_i = flist->used - 1;
2364 while (++i < flist->used) {
2366 struct file_struct *file = flist->sorted[i];
2368 if (!F_IS_ACTIVE(file))
2370 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2372 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2373 int save_mode = file->mode;
2374 /* Make sure that this directory doesn't duplicate a
2375 * non-directory earlier in the list. */
2376 flist->high = prev_i;
2377 file->mode = S_IFREG;
2378 j = flist_find(flist, file);
2379 file->mode = save_mode;
2384 /* If one is a dir and the other is not, we want to
2385 * keep the dir because it might have contents in the
2386 * list. Otherwise keep the first one. */
2387 if (S_ISDIR(file->mode)) {
2388 struct file_struct *fp = flist->sorted[j];
2389 if (!S_ISDIR(fp->mode))
2393 file->flags |= FLAG_DUPLICATE;
2394 else /* Make sure we don't lose vital flags. */
2395 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2404 "removing duplicate name %s from file list (%d)\n",
2405 f_name(file, fbuf), drop + flist->ndx_start);
2407 clear_file(flist->sorted[drop]);
2411 if (flist->low == drop) {
2413 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2422 flist->high = prev_i;
2425 /* We need to strip off the leading slashes for relative
2426 * paths, but this must be done _after_ the sorting phase. */
2427 for (i = flist->low; i <= flist->high; i++) {
2428 struct file_struct *file = flist->sorted[i];
2432 while (*file->dirname == '/')
2434 if (!*file->dirname)
2435 file->dirname = NULL;
2439 if (prune_empty_dirs && !am_sender) {
2440 int j, prev_depth = 0;
2442 prev_i = 0; /* It's OK that this isn't really true. */
2444 for (i = flist->low; i <= flist->high; i++) {
2445 struct file_struct *fp, *file = flist->sorted[i];
2447 /* This temporarily abuses the F_DEPTH() value for a
2448 * directory that is in a chain that might get pruned.
2449 * We restore the old value if it gets a reprieve. */
2450 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2451 /* Dump empty dirs when coming back down. */
2452 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2453 fp = flist->sorted[prev_i];
2454 if (F_DEPTH(fp) >= 0)
2456 prev_i = -F_DEPTH(fp)-1;
2459 prev_depth = F_DEPTH(file);
2460 if (is_excluded(f_name(file, fbuf), 1,
2462 /* Keep dirs through this dir. */
2463 for (j = prev_depth-1; ; j--) {
2464 fp = flist->sorted[prev_i];
2465 if (F_DEPTH(fp) >= 0)
2467 prev_i = -F_DEPTH(fp)-1;
2471 F_DEPTH(file) = -prev_i-1;
2474 /* Keep dirs through this non-dir. */
2475 for (j = prev_depth; ; j--) {
2476 fp = flist->sorted[prev_i];
2477 if (F_DEPTH(fp) >= 0)
2479 prev_i = -F_DEPTH(fp)-1;
2484 /* Dump all remaining empty dirs. */
2486 struct file_struct *fp = flist->sorted[prev_i];
2487 if (F_DEPTH(fp) >= 0)
2489 prev_i = -F_DEPTH(fp)-1;
2493 for (i = flist->low; i <= flist->high; i++) {
2494 if (F_IS_ACTIVE(flist->sorted[i]))
2498 for (i = flist->high; i >= flist->low; i--) {
2499 if (F_IS_ACTIVE(flist->sorted[i]))
2506 static void output_flist(struct file_list *flist)
2508 char uidbuf[16], gidbuf[16], depthbuf[16];
2509 struct file_struct *file;
2510 const char *root, *dir, *slash, *name, *trail;
2511 const char *who = who_am_i();
2514 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2515 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2516 for (i = 0; i < flist->used; i++) {
2517 file = flist->sorted[i];
2518 if ((am_root || am_sender) && uid_ndx) {
2519 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2524 static char parens[] = "(\0)\0\0\0";
2525 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2526 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2527 pp, F_GROUP(file), pp + 2);
2531 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2532 if (F_IS_ACTIVE(file)) {
2533 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2534 if ((dir = file->dirname) == NULL)
2538 name = file->basename;
2539 trail = S_ISDIR(file->mode) ? "/" : "";
2541 root = dir = slash = name = trail = "";
2543 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2544 who, i + flist->ndx_start,
2545 root, dir, slash, name, trail,
2546 (int)file->mode, (double)F_LENGTH(file),
2547 uidbuf, gidbuf, file->flags);
2551 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2552 enum fnc_type { t_PATH, t_ITEM };
2554 static int found_prefix;
2556 /* Compare the names of two file_struct entities, similar to how strcmp()
2557 * would do if it were operating on the joined strings.
2559 * Some differences beginning with protocol_version 29: (1) directory names
2560 * are compared with an assumed trailing slash so that they compare in a
2561 * way that would cause them to sort immediately prior to any content they
2562 * may have; (2) a directory of any name compares after a non-directory of
2563 * any name at the same depth; (3) a directory with name "." compares prior
2564 * to anything else. These changes mean that a directory and a non-dir
2565 * with the same name will not compare as equal (protocol_version >= 29).
2567 * The dirname component can be an empty string, but the basename component
2568 * cannot (and never is in the current codebase). The basename component
2569 * may be NULL (for a removed item), in which case it is considered to be
2570 * after any existing item. */
2571 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2574 const uchar *c1, *c2;
2575 enum fnc_state state1, state2;
2576 enum fnc_type type1, type2;
2577 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2579 if (!f1 || !F_IS_ACTIVE(f1)) {
2580 if (!f2 || !F_IS_ACTIVE(f2))
2584 if (!f2 || !F_IS_ACTIVE(f2))
2587 c1 = (uchar*)f1->dirname;
2588 c2 = (uchar*)f2->dirname;
2592 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2593 c1 = (const uchar*)f1->basename;
2594 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2596 state1 = s_TRAILING;
2605 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2606 c2 = (const uchar*)f2->basename;
2607 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2609 state2 = s_TRAILING;
2619 return type1 == t_PATH ? 1 : -1;
2629 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2630 c1 = (const uchar*)f1->basename;
2631 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2633 state1 = s_TRAILING;
2639 state1 = s_TRAILING;
2640 if (type1 == t_PATH) {
2649 if (*c2 && type1 != type2)
2650 return type1 == t_PATH ? 1 : -1;
2659 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2660 c2 = (const uchar*)f2->basename;
2661 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2663 state2 = s_TRAILING;
2669 state2 = s_TRAILING;
2670 if (type2 == t_PATH) {
2683 return type1 == t_PATH ? 1 : -1;
2685 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2690 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2691 * not match if f2's basename is not an exact match of a path element in f1.
2692 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2693 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2697 return found_prefix;
2700 char *f_name_buf(void)
2702 static char names[5][MAXPATHLEN];
2703 static unsigned int n;
2705 n = (n + 1) % (sizeof names / sizeof names[0]);
2710 /* Return a copy of the full filename of a flist entry, using the indicated
2711 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2712 * done because we checked the size when creating the file_struct entry.
2714 char *f_name(const struct file_struct *f, char *fbuf)
2716 if (!f || !F_IS_ACTIVE(f))
2720 fbuf = f_name_buf();
2723 int len = strlen(f->dirname);
2724 memcpy(fbuf, f->dirname, len);
2726 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2728 strlcpy(fbuf, f->basename, MAXPATHLEN);
2733 /* Do a non-recursive scan of the named directory, possibly ignoring all
2734 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2735 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2736 * buffer (the functions we call will append names onto the end, but the old
2737 * dir value will be restored on exit). */
2738 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2740 struct file_list *dirlist;
2741 char dirbuf[MAXPATHLEN];
2742 int save_recurse = recurse;
2743 int save_xfer_dirs = xfer_dirs;
2744 int save_prune_empty_dirs = prune_empty_dirs;
2747 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2748 if (dlen >= MAXPATHLEN)
2753 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2757 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2758 xfer_dirs = save_xfer_dirs;
2759 recurse = save_recurse;
2761 flist_count_offset += dirlist->used;
2763 prune_empty_dirs = 0;
2764 dirlist->sorted = dirlist->files;
2765 clean_flist(dirlist, 0);
2766 prune_empty_dirs = save_prune_empty_dirs;
2769 output_flist(dirlist);