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 (xflags & XMIT_TOP_DIR) {
868 in_del_hier = recurse;
869 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
870 if (relative_paths && del_hier_name_len > 2
871 && lastname[del_hier_name_len-1] == '.'
872 && lastname[del_hier_name_len-2] == '/')
873 del_hier_name_len -= 2;
874 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
875 } else if (protocol_version >= 30) {
876 if (!(xflags & XMIT_NON_XFER_DIR))
877 file->flags |= FLAG_XFER_DIR;
878 } else if (in_del_hier) {
879 if (!relative_paths || !del_hier_name_len
880 || (l1 >= del_hier_name_len
881 && lastname[del_hier_name_len] == '/'))
882 file->flags |= FLAG_XFER_DIR;
888 if ((preserve_devices && IS_DEVICE(mode))
889 || (preserve_specials && IS_SPECIAL(mode))) {
890 uint32 *devp = F_RDEV_P(file);
891 DEV_MAJOR(devp) = major(rdev);
892 DEV_MINOR(devp) = minor(rdev);
897 bp = (char*)file->basename + basename_len;
898 if (first_hlink_ndx >= flist->ndx_start) {
899 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
900 memcpy(bp, F_SYMLINK(first), linkname_len);
902 read_sbuf(f, bp, linkname_len - 1);
904 sanitize_path(bp, bp, "", lastdir_depth, NULL);
908 #ifdef SUPPORT_HARD_LINKS
909 if (preserve_hard_links && xflags & XMIT_HLINKED) {
910 if (protocol_version >= 30) {
911 if (xflags & XMIT_HLINK_FIRST) {
912 high_hlink_ndx = flist->ndx_start + flist->used;
913 F_HL_GNUM(file) = high_hlink_ndx;
915 F_HL_GNUM(file) = first_hlink_ndx;
917 static int32 cnt = 0;
918 struct ht_int64_node *np;
921 if (protocol_version < 26) {
925 if (!(xflags & XMIT_SAME_DEV_pre30))
926 dev = read_longint(f);
927 ino = read_longint(f);
929 np = idev_find(dev, ino);
930 ndx = (int32)(long)np->data - 1;
933 np->data = (void*)(long)cnt;
935 F_HL_GNUM(file) = ndx;
940 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
944 /* Prior to 28, we get a useless set of nulls. */
947 if (first_hlink_ndx >= flist->ndx_start) {
948 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
949 memcpy(bp, F_SUM(first), checksum_len);
951 read_buf(f, bp, checksum_len);
955 if (preserve_acls && !S_ISLNK(mode))
956 receive_acl(file, f);
958 #ifdef SUPPORT_XATTRS
960 receive_xattr(file, f );
963 if (S_ISREG(mode) || S_ISLNK(mode))
964 stats.total_size += file_length;
969 /* Create a file_struct for a named file by reading its stat() information
970 * and performing extensive checks against global options.
972 * Returns a pointer to the new file struct, or NULL if there was an error
973 * or this file should be excluded. */
974 struct file_struct *make_file(const char *fname, struct file_list *flist,
975 STRUCT_STAT *stp, int flags, int filter_level)
977 static char *lastdir;
978 static int lastdir_len = -1;
979 struct file_struct *file;
980 char thisname[MAXPATHLEN];
981 char linkname[MAXPATHLEN];
982 int alloc_len, basename_len, linkname_len;
983 int extra_len = file_extra_cnt * EXTRA_LEN;
984 const char *basename;
989 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
990 rprintf(FINFO, "skipping overly long name: %s\n", fname);
993 clean_fname(thisname, 0);
995 sanitize_path(thisname, thisname, "", 0, NULL);
997 if (stp && S_ISDIR(stp->st_mode)) {
998 st = *stp; /* Needed for "symlink/." with --relative. */
999 *linkname = '\0'; /* make IBM code checker happy */
1000 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1001 int save_errno = errno;
1002 /* See if file is excluded before reporting an error. */
1003 if (filter_level != NO_FILTERS
1004 && (is_excluded(thisname, 0, filter_level)
1005 || is_excluded(thisname, 1, filter_level))) {
1006 if (ignore_perishable && save_errno != ENOENT)
1007 non_perishable_cnt++;
1010 if (save_errno == ENOENT) {
1011 #ifdef SUPPORT_LINKS
1012 /* Avoid "vanished" error if symlink points nowhere. */
1013 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1014 && S_ISLNK(st.st_mode)) {
1015 io_error |= IOERR_GENERAL;
1016 rprintf(FERROR, "symlink has no referent: %s\n",
1017 full_fname(thisname));
1021 enum logcode c = am_daemon && protocol_version < 28
1023 io_error |= IOERR_VANISHED;
1024 rprintf(c, "file has vanished: %s\n",
1025 full_fname(thisname));
1028 io_error |= IOERR_GENERAL;
1029 rsyserr(FERROR, save_errno, "readlink %s failed",
1030 full_fname(thisname));
1035 /* backup.c calls us with filter_level set to NO_FILTERS. */
1036 if (filter_level == NO_FILTERS)
1039 if (S_ISDIR(st.st_mode)) {
1041 rprintf(FINFO, "skipping directory %s\n", thisname);
1045 flags &= ~FLAG_XFER_DIR;
1047 /* -x only affects directories because we need to avoid recursing
1048 * into a mount-point directory, not to avoid copying a symlinked
1049 * file if -L (or similar) was specified. */
1050 if (one_file_system && S_ISDIR(st.st_mode)) {
1051 if (flags & FLAG_TOP_DIR)
1052 filesystem_dev = st.st_dev;
1053 else if (st.st_dev != filesystem_dev) {
1054 if (one_file_system > 1) {
1056 rprintf(FINFO, "skipping mount-point dir %s\n",
1061 flags |= FLAG_MOUNT_DIR;
1065 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1066 if (ignore_perishable)
1067 non_perishable_cnt++;
1071 if (lp_ignore_nonreadable(module_id)) {
1072 #ifdef SUPPORT_LINKS
1073 if (!S_ISLNK(st.st_mode))
1075 if (access(thisname, R_OK) != 0)
1081 /* Only divert a directory in the main transfer. */
1083 if (flist->prev && S_ISDIR(st.st_mode)
1084 && flags & FLAG_DIVERT_DIRS) {
1085 /* Room for parent/sibling/next-child info. */
1086 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1088 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1090 pool = dir_flist->file_pool;
1092 pool = flist->file_pool;
1097 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1098 who_am_i(), thisname, filter_level);
1101 if ((basename = strrchr(thisname, '/')) != NULL) {
1102 int len = basename++ - thisname;
1103 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1104 lastdir = new_array(char, len + 1);
1105 memcpy(lastdir, thisname, len);
1106 lastdir[len] = '\0';
1110 basename = thisname;
1111 basename_len = strlen(basename) + 1; /* count the '\0' */
1113 #ifdef SUPPORT_LINKS
1114 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1119 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1120 extra_len += EXTRA_LEN;
1122 #if EXTRA_ROUNDING > 0
1123 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1124 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1127 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1130 bp = pool_alloc(pool, alloc_len, "make_file");
1132 if (!(bp = new_array(char, alloc_len)))
1133 out_of_memory("make_file");
1136 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1138 file = (struct file_struct *)bp;
1139 bp += FILE_STRUCT_LEN;
1141 memcpy(bp, basename, basename_len);
1142 bp += basename_len + linkname_len; /* skip space for symlink too */
1144 #ifdef SUPPORT_HARD_LINKS
1145 if (preserve_hard_links && flist && flist->prev) {
1146 if (protocol_version >= 28
1147 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1148 : S_ISREG(st.st_mode)) {
1149 tmp_dev = st.st_dev;
1150 tmp_ino = st.st_ino;
1156 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1157 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1158 tmp_rdev = st.st_rdev;
1163 file->flags = flags;
1164 file->modtime = st.st_mtime;
1165 file->len32 = (uint32)st.st_size;
1166 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1167 file->flags |= FLAG_LENGTH64;
1168 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1170 file->mode = st.st_mode;
1172 F_OWNER(file) = st.st_uid;
1174 F_GROUP(file) = st.st_gid;
1176 if (basename != thisname)
1177 file->dirname = lastdir;
1179 #ifdef SUPPORT_LINKS
1181 bp = (char*)file->basename + basename_len;
1182 memcpy(bp, linkname, linkname_len);
1186 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1187 file_checksum(thisname, tmp_sum, st.st_size);
1189 F_PATHNAME(file) = pathname;
1191 /* This code is only used by the receiver when it is building
1192 * a list of files for a delete pass. */
1193 if (keep_dirlinks && linkname_len && flist) {
1195 int save_mode = file->mode;
1196 file->mode = S_IFDIR; /* Find a directory with our name. */
1197 if (flist_find(dir_flist, file) >= 0
1198 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1199 file->modtime = st2.st_mtime;
1201 file->mode = st2.st_mode;
1203 F_OWNER(file) = st2.st_uid;
1205 F_GROUP(file) = st2.st_gid;
1207 file->mode = save_mode;
1210 if (basename_len == 0+1)
1215 F_NDX(file) = dir_count - 1;
1221 /* Only called for temporary file_struct entries created by make_file(). */
1222 void unmake_file(struct file_struct *file)
1224 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1225 #if EXTRA_ROUNDING > 0
1226 if (extra_cnt & EXTRA_ROUNDING)
1227 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1229 free(REQ_EXTRA(file, extra_cnt));
1232 static struct file_struct *send_file_name(int f, struct file_list *flist,
1233 char *fname, STRUCT_STAT *stp,
1234 int flags, int filter_level)
1236 struct file_struct *file;
1237 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1241 file = make_file(fname, flist, stp, flags, filter_level);
1245 if (chmod_modes && !S_ISLNK(file->mode))
1246 file->mode = tweak_mode(file->mode, chmod_modes);
1249 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1250 sx.st.st_mode = file->mode;
1251 sx.acc_acl = sx.def_acl = NULL;
1252 if (get_acl(fname, &sx) < 0)
1256 #ifdef SUPPORT_XATTRS
1257 if (preserve_xattrs && f >= 0) {
1259 if (get_xattr(fname, &sx) < 0)
1264 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1266 flist_expand(flist, 1);
1267 flist->files[flist->used++] = file;
1269 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1271 if (preserve_acls && !S_ISLNK(file->mode)) {
1276 #ifdef SUPPORT_XATTRS
1277 if (preserve_xattrs) {
1278 F_XATTR(file) = send_xattr(&sx, f);
1286 static void send_if_directory(int f, struct file_list *flist,
1287 struct file_struct *file,
1288 char *fbuf, unsigned int ol,
1291 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1293 if (S_ISDIR(file->mode)
1294 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1296 unsigned int len = strlen(fbuf);
1297 if (len > 1 && fbuf[len-1] == '/')
1299 if (len >= MAXPATHLEN - 1) {
1300 io_error |= IOERR_GENERAL;
1301 rprintf(FERROR, "skipping long-named directory: %s\n",
1305 save_filters = push_local_filters(fbuf, len);
1306 send_directory(f, flist, fbuf, len, flags);
1307 pop_local_filters(save_filters);
1314 static int file_compare(const void *file1, const void *file2)
1316 return f_name_cmp(*(struct file_struct **)file1,
1317 *(struct file_struct **)file2);
1320 /* The guts of a merge-sort algorithm. This was derived from the glibc
1321 * version, but I (Wayne) changed the merge code to do less copying and
1322 * to require only half the amount of temporary memory. */
1323 static void fsort_tmp(struct file_struct **fp, size_t num,
1324 struct file_struct **tmp)
1326 struct file_struct **f1, **f2, **t;
1335 fsort_tmp(f1, n1, tmp);
1337 fsort_tmp(f2, n2, tmp);
1339 while (f_name_cmp(*f1, *f2) <= 0) {
1346 memcpy(t, f1, n1 * PTR_SIZE);
1348 *f1++ = *f2++, n2--;
1350 while (n1 > 0 && n2 > 0) {
1351 if (f_name_cmp(*t, *f2) <= 0)
1354 *f1++ = *f2++, n2--;
1358 memcpy(f1, t, n1 * PTR_SIZE);
1361 /* This file-struct sorting routine makes sure that any identical names in
1362 * the file list stay in the same order as they were in the original list.
1363 * This is particularly vital in inc_recurse mode where we expect a sort
1364 * on the flist to match the exact order of a sort on the dir_flist. */
1365 static void fsort(struct file_struct **fp, size_t num)
1371 qsort(fp, num, PTR_SIZE, file_compare);
1373 struct file_struct **tmp = new_array(struct file_struct *,
1375 fsort_tmp(fp, num, tmp);
1380 /* We take an entire set of sibling dirs from the sorted flist and link them
1381 * into the tree, setting the appropriate parent/child/sibling pointers. */
1382 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1387 int32 *parent_dp = parent_ndx < 0 ? NULL
1388 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1390 flist_expand(dir_flist, dir_cnt);
1391 dir_flist->sorted = dir_flist->files;
1393 for (i = 0; dir_cnt; i++) {
1394 struct file_struct *file = from_flist->sorted[i];
1396 if (!S_ISDIR(file->mode))
1399 dir_flist->files[dir_flist->used++] = file;
1402 if (file->flags & FLAG_MOUNT_DIR)
1406 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1408 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1410 send_dir_ndx = dir_flist->used - 1;
1412 dp = F_DIR_NODE_P(file);
1413 DIR_PARENT(dp) = parent_ndx;
1414 DIR_FIRST_CHILD(dp) = -1;
1417 DIR_NEXT_SIBLING(dp) = -1;
1420 /* This function is normally called by the sender, but the receiving side also
1421 * calls it from get_dirlist() with f set to -1 so that we just construct the
1422 * file list in memory without sending it over the wire. Also, get_dirlist()
1423 * might call this with f set to -2, which also indicates that local filter
1424 * rules should be ignored. */
1425 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1432 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1433 int start = flist->used;
1434 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1436 assert(flist != NULL);
1438 if (!(d = opendir(fbuf))) {
1439 io_error |= IOERR_GENERAL;
1440 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1445 if (len != 1 || *fbuf != '/')
1448 remainder = MAXPATHLEN - (p - fbuf);
1450 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1451 char *dname = d_name(di);
1452 if (dname[0] == '.' && (dname[1] == '\0'
1453 || (dname[1] == '.' && dname[2] == '\0')))
1455 if (strlcpy(p, dname, remainder) >= remainder) {
1456 io_error |= IOERR_GENERAL;
1458 "cannot send long-named file %s\n",
1462 if (dname[0] == '\0') {
1463 io_error |= IOERR_GENERAL;
1465 "cannot send file with empty name in %s\n",
1470 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1476 io_error |= IOERR_GENERAL;
1477 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1482 if (f >= 0 && recurse && !divert_dirs) {
1483 int i, end = flist->used - 1;
1484 /* send_if_directory() bumps flist->used, so use "end". */
1485 for (i = start; i <= end; i++)
1486 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1490 static char lastpath[MAXPATHLEN] = "";
1491 static int lastpath_len = 0;
1492 static struct file_struct *lastpath_struct;
1494 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1495 char *start, char *limit, int flags, int is_dot_dir)
1497 struct file_struct *file;
1498 item_list *rel_list;
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(&rel_list, F_DIR_RELS_P(lastpath_struct), sizeof rel_list);
1553 if (!(rel_list = new0(item_list)))
1554 out_of_memory("send_implied_dirs");
1555 memcpy(F_DIR_RELS_P(lastpath_struct), &rel_list, sizeof rel_list);
1557 ep = EXPAND_ITEM_LIST(rel_list, char *, 32);
1558 if (!(*ep = new_array(char, 1 + len + 1)))
1559 out_of_memory("send_implied_dirs");
1561 strlcpy(*ep + 1, 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 *rel_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 (file->flags & FLAG_XFER_DIR)
1582 send_directory(f, flist, fbuf, dlen, flags);
1584 if (!relative_paths)
1587 memcpy(&rel_list, F_DIR_RELS_P(file), sizeof rel_list);
1591 for (j = 0; j < rel_list->count; j++) {
1592 char *slash, *ep = ((char**)rel_list->items)[j];
1593 int is_dot_dir = *ep;
1596 len = strlcpy(fbuf + dlen + 1, ep+1, sizeof fbuf - dlen - 1);
1598 if (len >= (int)sizeof fbuf)
1599 continue; /* Impossible... */
1601 slash = strchr(fbuf+dlen+1, '/');
1603 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
1609 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1610 io_error |= IOERR_GENERAL;
1611 rsyserr(FERROR, errno, "link_stat %s failed",
1615 send_file_name(f, flist, fbuf, &st,
1616 recurse || xfer_dirs ? FLAG_TOP_DIR | flags : flags,
1619 send_file_name(f, flist, fbuf, NULL,
1620 recurse ? FLAG_TOP_DIR | flags : flags,
1628 void send_extra_file_list(int f, int at_least)
1630 struct file_list *flist;
1632 int future_cnt, save_io_error = io_error;
1637 /* Keep sending data until we have the requested number of
1638 * files in the upcoming file-lists. */
1639 if (cur_flist->next) {
1640 flist = first_flist->prev; /* the newest flist */
1641 future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
1644 while (future_cnt < at_least) {
1645 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1646 int dir_ndx, dstart = dir_count;
1647 const char *pathname = F_PATHNAME(file);
1650 flist = flist_new(0, "send_extra_file_list");
1651 start_write = stats.total_written;
1655 dir_ndx = F_NDX(file);
1658 dir_ndx = send_dir_ndx;
1659 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1660 flist->parent_ndx = dir_ndx;
1662 send1extra(f, file, flist);
1663 dp = F_DIR_NODE_P(file);
1665 /* If there are any duplicate directory names that follow, we
1666 * send all the dirs together in one file-list. The dir_flist
1667 * tree links all the child subdirs onto the last dup dir. */
1668 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1669 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1670 send_dir_ndx = dir_ndx;
1671 file = dir_flist->sorted[dir_ndx];
1672 if (F_PATHNAME(file) != pathname)
1673 send1extra(f, file, flist);
1674 dp = F_DIR_NODE_P(file);
1680 if (need_unsorted_flist) {
1681 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1682 out_of_memory("send_extra_file_list");
1683 memcpy(flist->sorted, flist->files,
1684 flist->used * sizeof (struct file_struct*));
1687 flist->sorted = flist->files;
1689 clean_flist(flist, 0);
1691 flist->ndx_end = flist->ndx_start + flist->used - 1
1692 - (dir_count - dstart);
1694 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1695 flist_done_allocating(flist);
1697 file_total += flist->used;
1698 future_cnt += flist->used;
1699 stats.flist_size += stats.total_written - start_write;
1700 stats.num_files += flist->used;
1702 output_flist(flist);
1704 if (DIR_FIRST_CHILD(dp) >= 0) {
1705 send_dir_ndx = DIR_FIRST_CHILD(dp);
1708 while (DIR_NEXT_SIBLING(dp) < 0) {
1709 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1710 write_ndx(f, NDX_FLIST_EOF);
1712 change_local_filter_dir(NULL, 0, 0);
1716 file = dir_flist->sorted[send_dir_ndx];
1717 dp = F_DIR_NODE_P(file);
1719 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1724 if (io_error != save_io_error && !ignore_errors)
1725 send_msg_int(MSG_IO_ERROR, io_error);
1728 struct file_list *send_file_list(int f, int argc, char *argv[])
1730 static const char *lastdir;
1731 static int lastdir_len = -1;
1735 struct file_list *flist;
1736 struct timeval start_tv, end_tv;
1739 int flags, disable_buffering;
1740 int reading_remotely = filesfrom_host != NULL;
1741 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1743 | (filesfrom_convert ? RL_CONVERT : 0)
1745 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1747 rprintf(FLOG, "building file list\n");
1748 if (show_filelist_p())
1749 start_filelist_progress("building file list");
1750 else if (inc_recurse && verbose && !am_server)
1751 rprintf(FCLIENT, "sending incremental file list\n");
1753 start_write = stats.total_written;
1754 gettimeofday(&start_tv, NULL);
1756 #ifdef SUPPORT_HARD_LINKS
1757 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1761 flist = cur_flist = flist_new(0, "send_file_list");
1763 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1764 flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
1766 dir_flist = cur_flist;
1767 flags = FLAG_XFER_DIR;
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) {
1869 if (len == 1 && fn[0] == '/')
1872 /* Reject a ".." dir in the active part of the path. */
1873 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1874 if ((p[2] == '/' || p[2] == '\0')
1875 && (p == fn || p[-1] == '/')) {
1877 "found \"..\" dir in relative path: %s\n",
1879 exit_cleanup(RERR_SYNTAX);
1889 dirlen = dir ? strlen(dir) : 0;
1890 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1891 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1894 lastdir_len = pathname_len;
1895 } else if (!push_pathname(lastdir, lastdir_len))
1899 memmove(fbuf, fn, len + 1);
1901 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1902 io_error |= IOERR_GENERAL;
1903 rsyserr(FERROR, errno, "link_stat %s failed",
1908 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1909 rprintf(FINFO, "skipping directory %s\n", fbuf);
1913 if (inc_recurse && relative_paths && *fbuf) {
1914 if ((p = strchr(fbuf+1, '/')) != NULL) {
1915 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, is_dot_dir);
1918 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1919 /* Send the implied directories at the start of the
1920 * source spec, so we get their permissions right. */
1921 char *lp = lastpath, *slash = fbuf;
1923 /* Skip any initial directories in our path that we
1924 * have in common with lastpath. */
1925 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1930 if (fn != p || (*lp && *lp != '/'))
1931 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1934 if (recurse || (xfer_dirs && is_dot_dir)) {
1935 struct file_struct *file;
1936 int top_flags = FLAG_TOP_DIR | flags;
1937 file = send_file_name(f, flist, fbuf, &st,
1938 top_flags, ALL_FILTERS);
1939 if (file && !inc_recurse)
1940 send_if_directory(f, flist, file, fbuf, len, flags);
1942 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1945 gettimeofday(&end_tv, NULL);
1946 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1947 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1948 if (stats.flist_buildtime == 0)
1949 stats.flist_buildtime = 1;
1952 write_byte(f, 0); /* Indicate end of file list */
1954 #ifdef SUPPORT_HARD_LINKS
1955 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1959 if (show_filelist_p())
1960 finish_filelist_progress(flist);
1962 gettimeofday(&end_tv, NULL);
1963 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1964 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1966 /* When converting names, both sides keep an unsorted file-list array
1967 * because the names will differ on the sending and receiving sides
1968 * (both sides will use the unsorted index number for each item). */
1970 /* Sort the list without removing any duplicates. This allows the
1971 * receiving side to ask for whatever name it kept. For incremental
1972 * recursion mode, the sender marks duplicate dirs so that it can
1973 * send them together in a single file-list. */
1975 if (need_unsorted_flist) {
1977 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1978 out_of_memory("send_file_list");
1979 memcpy(flist->sorted, flist->files,
1980 flist->used * sizeof (struct file_struct*));
1981 clean_flist(flist, 0);
1983 flist->sorted = flist->files;
1985 flist->high = flist->used - 1;
1990 flist->sorted = flist->files;
1991 clean_flist(flist, 0);
1993 file_total += flist->used;
1995 /* We don't subtract dir_count for the first send since we
1996 * might have one or more dot dirs which need to get sent. */
1997 flist->ndx_end = flist->ndx_start + flist->used - 1;
1999 if (!numeric_ids && !inc_recurse)
2002 /* send the io_error flag */
2003 if (protocol_version < 30)
2004 write_int(f, ignore_errors ? 0 : io_error);
2005 else if (io_error && !ignore_errors)
2006 send_msg_int(MSG_IO_ERROR, io_error);
2008 if (disable_buffering)
2009 io_end_buffering_out();
2011 stats.flist_size = stats.total_written - start_write;
2012 stats.num_files = flist->used;
2015 output_flist(flist);
2018 rprintf(FINFO, "send_file_list done\n");
2021 add_dirs_to_tree(-1, flist, dir_count);
2022 flist_done_allocating(flist);
2023 if (send_dir_ndx < 0) {
2024 write_ndx(f, NDX_FLIST_EOF);
2027 else if (file_total == 1) {
2028 /* If we're creating incremental file-lists and there
2029 * was just 1 item in the first file-list, send 1 more
2030 * file-list to check if this is a 1-file xfer. */
2031 send_extra_file_list(f, 1);
2038 struct file_list *recv_file_list(int f)
2040 struct file_list *flist;
2045 rprintf(FLOG, "receiving file list\n");
2046 if (show_filelist_p())
2047 start_filelist_progress("receiving file list");
2048 else if (inc_recurse && verbose && !am_server && !first_flist)
2049 rprintf(FCLIENT, "receiving incremental file list\n");
2051 start_read = stats.total_read;
2053 #ifdef SUPPORT_HARD_LINKS
2054 if (preserve_hard_links && !first_flist)
2058 flist = flist_new(0, "recv_file_list");
2061 if (flist->ndx_start == 0)
2062 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2063 dstart = dir_flist->used;
2069 while ((flags = read_byte(f)) != 0) {
2070 struct file_struct *file;
2072 flist_expand(flist, 1);
2074 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2075 flags |= read_byte(f) << 8;
2076 file = recv_file_entry(flist, flags, f);
2078 if (inc_recurse && S_ISDIR(file->mode)) {
2079 flist_expand(dir_flist, 1);
2080 dir_flist->files[dir_flist->used++] = file;
2083 flist->files[flist->used++] = file;
2085 maybe_emit_filelist_progress(flist->used);
2088 rprintf(FINFO, "recv_file_name(%s)\n",
2089 f_name(file, NULL));
2092 file_total += flist->used;
2094 flist->ndx_end = flist->ndx_start + flist->used - 1;
2095 if (inc_recurse && flist->ndx_start)
2096 flist->ndx_end -= dir_flist->used - dstart;
2099 rprintf(FINFO, "received %d names\n", flist->used);
2101 if (show_filelist_p())
2102 finish_filelist_progress(flist);
2105 if (need_unsorted_flist) {
2106 /* Create an extra array of index pointers that we can sort for
2107 * the generator's use (for wading through the files in sorted
2108 * order and for calling flist_find()). We keep the "files"
2109 * list unsorted for our exchange of index numbers with the
2110 * other side (since their names may not sort the same). */
2111 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2112 out_of_memory("recv_file_list");
2113 memcpy(flist->sorted, flist->files,
2114 flist->used * sizeof (struct file_struct*));
2115 if (inc_recurse && dir_flist->used > dstart) {
2116 dir_flist->sorted = realloc_array(dir_flist->sorted,
2117 struct file_struct *,
2119 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2120 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2121 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2126 flist->sorted = flist->files;
2127 if (inc_recurse && dir_flist->used > dstart) {
2128 dir_flist->sorted = dir_flist->files;
2129 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2134 flist_done_allocating(flist);
2136 recv_id_list(f, flist);
2138 clean_flist(flist, relative_paths);
2140 if (protocol_version < 30) {
2141 /* Recv the io_error flag */
2145 io_error |= read_int(f);
2149 output_flist(flist);
2152 rprintf(FINFO, "recv_file_list done\n");
2154 stats.flist_size += stats.total_read - start_read;
2155 stats.num_files += flist->used;
2160 /* This is only used once by the receiver if the very first file-list
2161 * has exactly one item in it. */
2162 void recv_additional_file_list(int f)
2164 struct file_list *flist;
2165 int ndx = read_ndx(f);
2166 if (ndx == NDX_FLIST_EOF) {
2168 change_local_filter_dir(NULL, 0, 0);
2170 ndx = NDX_FLIST_OFFSET - ndx;
2171 if (ndx < 0 || ndx >= dir_flist->used) {
2172 ndx = NDX_FLIST_OFFSET - ndx;
2174 "[%s] Invalid dir index: %d (%d - %d)\n",
2175 who_am_i(), ndx, NDX_FLIST_OFFSET,
2176 NDX_FLIST_OFFSET - dir_flist->used + 1);
2177 exit_cleanup(RERR_PROTOCOL);
2180 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2183 flist = recv_file_list(f);
2184 flist->parent_ndx = ndx;
2188 /* Search for an identically-named item in the file list. Note that the
2189 * items must agree in their directory-ness, or no match is returned. */
2190 int flist_find(struct file_list *flist, struct file_struct *f)
2192 int low = flist->low, high = flist->high;
2193 int diff, mid, mid_up;
2195 while (low <= high) {
2196 mid = (low + high) / 2;
2197 if (F_IS_ACTIVE(flist->sorted[mid]))
2200 /* Scan for the next non-empty entry using the cached
2201 * distance values. If the value isn't fully up-to-
2202 * date, update it. */
2203 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2204 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2206 mid_up += F_DEPTH(flist->sorted[mid_up]);
2207 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2208 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2210 if (mid_up > high) {
2211 /* If there's nothing left above us, set high to
2212 * a non-empty entry below us and continue. */
2213 high = mid - (int)flist->sorted[mid]->len32;
2214 if (!F_IS_ACTIVE(flist->sorted[high])) {
2216 high -= (int)flist->sorted[high]->len32;
2217 } while (!F_IS_ACTIVE(flist->sorted[high]));
2218 flist->sorted[mid]->len32 = mid - high;
2223 diff = f_name_cmp(flist->sorted[mid_up], f);
2225 if (protocol_version < 29
2226 && S_ISDIR(flist->sorted[mid_up]->mode)
2227 != S_ISDIR(f->mode))
2240 * Free up any resources a file_struct has allocated
2241 * and clear the file.
2243 void clear_file(struct file_struct *file)
2245 /* The +1 zeros out the first char of the basename. */
2246 memset(file, 0, FILE_STRUCT_LEN + 1);
2247 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2248 * entry. Likewise for len32 in the opposite direction. We assume
2249 * that we're alone for now since flist_find() will adjust the counts
2250 * it runs into that aren't up-to-date. */
2251 file->len32 = F_DEPTH(file) = 1;
2254 /* Allocate a new file list. */
2255 struct file_list *flist_new(int flags, char *msg)
2257 struct file_list *flist;
2259 if (!(flist = new0(struct file_list)))
2262 if (flags & FLIST_TEMP) {
2263 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2264 out_of_memory, POOL_INTERN)))
2267 /* This is a doubly linked list with prev looping back to
2268 * the end of the list, but the last next pointer is NULL. */
2270 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2271 out_of_memory, POOL_INTERN);
2272 if (!flist->file_pool)
2275 first_flist = cur_flist = flist->prev = flist;
2277 flist->file_pool = first_flist->file_pool;
2279 flist->ndx_start = first_flist->prev->ndx_end + 2;
2280 if (flist->ndx_start <= high_hlink_ndx)
2281 flist->ndx_start = high_hlink_ndx + 1;
2283 flist->prev = first_flist->prev;
2284 flist->prev->next = first_flist->prev = flist;
2286 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2293 /* Free up all elements in a flist. */
2294 void flist_free(struct file_list *flist)
2297 /* Was FLIST_TEMP dir-list. */
2298 } else if (flist == flist->prev) {
2299 first_flist = cur_flist = NULL;
2303 if (flist == cur_flist)
2304 cur_flist = flist->next;
2305 if (flist == first_flist)
2306 first_flist = first_flist->next;
2308 flist->prev->next = flist->next;
2310 flist->next = first_flist;
2312 flist->next->prev = flist->prev;
2313 file_total -= flist->used;
2317 if (!flist->prev || !flist_cnt)
2318 pool_destroy(flist->file_pool);
2320 pool_free_old(flist->file_pool, flist->pool_boundary);
2322 if (flist->sorted && flist->sorted != flist->files)
2323 free(flist->sorted);
2328 /* This routine ensures we don't have any duplicate names in our file list.
2329 * duplicate names can cause corruption because of the pipelining. */
2330 static void clean_flist(struct file_list *flist, int strip_root)
2332 char fbuf[MAXPATHLEN];
2337 if (flist->used == 0) {
2343 fsort(flist->sorted, flist->used);
2345 if (!am_sender || inc_recurse) {
2346 for (i = prev_i = 0; i < flist->used; i++) {
2347 if (F_IS_ACTIVE(flist->sorted[i])) {
2352 flist->low = prev_i;
2354 i = prev_i = flist->used - 1;
2358 while (++i < flist->used) {
2360 struct file_struct *file = flist->sorted[i];
2362 if (!F_IS_ACTIVE(file))
2364 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2366 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2367 int save_mode = file->mode;
2368 /* Make sure that this directory doesn't duplicate a
2369 * non-directory earlier in the list. */
2370 flist->high = prev_i;
2371 file->mode = S_IFREG;
2372 j = flist_find(flist, file);
2373 file->mode = save_mode;
2378 /* If one is a dir and the other is not, we want to
2379 * keep the dir because it might have contents in the
2380 * list. Otherwise keep the first one. */
2381 if (S_ISDIR(file->mode)) {
2382 struct file_struct *fp = flist->sorted[j];
2383 if (!S_ISDIR(fp->mode))
2387 file->flags |= FLAG_DUPLICATE;
2396 "removing duplicate name %s from file list (%d)\n",
2397 f_name(file, fbuf), drop + flist->ndx_start);
2399 /* Make sure we don't lose track of a user-specified
2401 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2402 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2404 clear_file(flist->sorted[drop]);
2408 if (flist->low == drop) {
2410 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2419 flist->high = prev_i;
2422 /* We need to strip off the leading slashes for relative
2423 * paths, but this must be done _after_ the sorting phase. */
2424 for (i = flist->low; i <= flist->high; i++) {
2425 struct file_struct *file = flist->sorted[i];
2429 while (*file->dirname == '/')
2431 if (!*file->dirname)
2432 file->dirname = NULL;
2436 if (prune_empty_dirs && !am_sender) {
2437 int j, prev_depth = 0;
2439 prev_i = 0; /* It's OK that this isn't really true. */
2441 for (i = flist->low; i <= flist->high; i++) {
2442 struct file_struct *fp, *file = flist->sorted[i];
2444 /* This temporarily abuses the F_DEPTH() value for a
2445 * directory that is in a chain that might get pruned.
2446 * We restore the old value if it gets a reprieve. */
2447 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2448 /* Dump empty dirs when coming back down. */
2449 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2450 fp = flist->sorted[prev_i];
2451 if (F_DEPTH(fp) >= 0)
2453 prev_i = -F_DEPTH(fp)-1;
2456 prev_depth = F_DEPTH(file);
2457 if (is_excluded(f_name(file, fbuf), 1,
2459 /* Keep dirs through this dir. */
2460 for (j = prev_depth-1; ; j--) {
2461 fp = flist->sorted[prev_i];
2462 if (F_DEPTH(fp) >= 0)
2464 prev_i = -F_DEPTH(fp)-1;
2468 F_DEPTH(file) = -prev_i-1;
2471 /* Keep dirs through this non-dir. */
2472 for (j = prev_depth; ; j--) {
2473 fp = flist->sorted[prev_i];
2474 if (F_DEPTH(fp) >= 0)
2476 prev_i = -F_DEPTH(fp)-1;
2481 /* Dump all remaining empty dirs. */
2483 struct file_struct *fp = flist->sorted[prev_i];
2484 if (F_DEPTH(fp) >= 0)
2486 prev_i = -F_DEPTH(fp)-1;
2490 for (i = flist->low; i <= flist->high; i++) {
2491 if (F_IS_ACTIVE(flist->sorted[i]))
2495 for (i = flist->high; i >= flist->low; i--) {
2496 if (F_IS_ACTIVE(flist->sorted[i]))
2503 static void output_flist(struct file_list *flist)
2505 char uidbuf[16], gidbuf[16], depthbuf[16];
2506 struct file_struct *file;
2507 const char *root, *dir, *slash, *name, *trail;
2508 const char *who = who_am_i();
2511 rprintf(FINFO, "[%s] flist start=%d, end=%d, used=%d, low=%d, high=%d\n",
2512 who, flist->ndx_start, flist->ndx_end, flist->used, flist->low, flist->high);
2513 for (i = 0; i < flist->used; i++) {
2514 file = flist->sorted[i];
2515 if ((am_root || am_sender) && uid_ndx) {
2516 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2521 static char parens[] = "(\0)\0\0\0";
2522 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2523 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2524 pp, F_GROUP(file), pp + 2);
2528 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2529 if (F_IS_ACTIVE(file)) {
2530 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2531 if ((dir = file->dirname) == NULL)
2535 name = file->basename;
2536 trail = S_ISDIR(file->mode) ? "/" : "";
2538 root = dir = slash = name = trail = "";
2540 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2541 who, i + flist->ndx_start,
2542 root, dir, slash, name, trail,
2543 (int)file->mode, (double)F_LENGTH(file),
2544 uidbuf, gidbuf, file->flags);
2548 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2549 enum fnc_type { t_PATH, t_ITEM };
2551 static int found_prefix;
2553 /* Compare the names of two file_struct entities, similar to how strcmp()
2554 * would do if it were operating on the joined strings.
2556 * Some differences beginning with protocol_version 29: (1) directory names
2557 * are compared with an assumed trailing slash so that they compare in a
2558 * way that would cause them to sort immediately prior to any content they
2559 * may have; (2) a directory of any name compares after a non-directory of
2560 * any name at the same depth; (3) a directory with name "." compares prior
2561 * to anything else. These changes mean that a directory and a non-dir
2562 * with the same name will not compare as equal (protocol_version >= 29).
2564 * The dirname component can be an empty string, but the basename component
2565 * cannot (and never is in the current codebase). The basename component
2566 * may be NULL (for a removed item), in which case it is considered to be
2567 * after any existing item. */
2568 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2571 const uchar *c1, *c2;
2572 enum fnc_state state1, state2;
2573 enum fnc_type type1, type2;
2574 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2576 if (!f1 || !F_IS_ACTIVE(f1)) {
2577 if (!f2 || !F_IS_ACTIVE(f2))
2581 if (!f2 || !F_IS_ACTIVE(f2))
2584 c1 = (uchar*)f1->dirname;
2585 c2 = (uchar*)f2->dirname;
2589 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2590 c1 = (const uchar*)f1->basename;
2591 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2593 state1 = s_TRAILING;
2602 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2603 c2 = (const uchar*)f2->basename;
2604 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2606 state2 = s_TRAILING;
2616 return type1 == t_PATH ? 1 : -1;
2626 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2627 c1 = (const uchar*)f1->basename;
2628 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2630 state1 = s_TRAILING;
2636 state1 = s_TRAILING;
2637 if (type1 == t_PATH) {
2646 if (*c2 && type1 != type2)
2647 return type1 == t_PATH ? 1 : -1;
2656 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2657 c2 = (const uchar*)f2->basename;
2658 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2660 state2 = s_TRAILING;
2666 state2 = s_TRAILING;
2667 if (type2 == t_PATH) {
2680 return type1 == t_PATH ? 1 : -1;
2682 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2687 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2688 * not match if f2's basename is not an exact match of a path element in f1.
2689 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2690 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2694 return found_prefix;
2697 char *f_name_buf(void)
2699 static char names[5][MAXPATHLEN];
2700 static unsigned int n;
2702 n = (n + 1) % (sizeof names / sizeof names[0]);
2707 /* Return a copy of the full filename of a flist entry, using the indicated
2708 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2709 * done because we checked the size when creating the file_struct entry.
2711 char *f_name(const struct file_struct *f, char *fbuf)
2713 if (!f || !F_IS_ACTIVE(f))
2717 fbuf = f_name_buf();
2720 int len = strlen(f->dirname);
2721 memcpy(fbuf, f->dirname, len);
2723 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2725 strlcpy(fbuf, f->basename, MAXPATHLEN);
2730 /* Do a non-recursive scan of the named directory, possibly ignoring all
2731 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2732 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2733 * buffer (the functions we call will append names onto the end, but the old
2734 * dir value will be restored on exit). */
2735 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2737 struct file_list *dirlist;
2738 char dirbuf[MAXPATHLEN];
2739 int save_recurse = recurse;
2740 int save_xfer_dirs = xfer_dirs;
2741 int save_prune_empty_dirs = prune_empty_dirs;
2744 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2745 if (dlen >= MAXPATHLEN)
2750 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2754 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2755 xfer_dirs = save_xfer_dirs;
2756 recurse = save_recurse;
2758 flist_count_offset += dirlist->used;
2760 prune_empty_dirs = 0;
2761 dirlist->sorted = dirlist->files;
2762 clean_flist(dirlist, 0);
2763 prune_empty_dirs = save_prune_empty_dirs;
2766 output_flist(dirlist);