added apple silicon path details
[rsync.git] / flist.c
blob464d556ec906a572d2c378ac79e6d0fa169dd003
1 /*
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-2023 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.
23 #include "rsync.h"
24 #include "ifuncs.h"
25 #include "rounding.h"
26 #include "inums.h"
27 #include "io.h"
29 extern int am_root;
30 extern int am_server;
31 extern int am_daemon;
32 extern int am_sender;
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int always_checksum;
36 extern int module_id;
37 extern int ignore_errors;
38 extern int numeric_ids;
39 extern int quiet;
40 extern int recurse;
41 extern int use_qsort;
42 extern int xfer_dirs;
43 extern int filesfrom_fd;
44 extern int one_file_system;
45 extern int copy_devices;
46 extern int copy_dirlinks;
47 extern int preserve_uid;
48 extern int preserve_gid;
49 extern int preserve_acls;
50 extern int preserve_xattrs;
51 extern int preserve_links;
52 extern int preserve_hard_links;
53 extern int preserve_devices;
54 extern int preserve_specials;
55 extern int delete_during;
56 extern int missing_args;
57 extern int eol_nulls;
58 extern int atimes_ndx;
59 extern int crtimes_ndx;
60 extern int relative_paths;
61 extern int implied_dirs;
62 extern int ignore_perishable;
63 extern int non_perishable_cnt;
64 extern int prune_empty_dirs;
65 extern int copy_links;
66 extern int copy_unsafe_links;
67 extern int protocol_version;
68 extern int sanitize_paths;
69 extern int munge_symlinks;
70 extern int use_safe_inc_flist;
71 extern int need_unsorted_flist;
72 extern int sender_symlink_iconv;
73 extern int output_needs_newline;
74 extern int sender_keeps_checksum;
75 extern int trust_sender_filter;
76 extern int unsort_ndx;
77 extern uid_t our_uid;
78 extern struct stats stats;
79 extern char *filesfrom_host;
80 extern char *usermap, *groupmap;
82 extern struct name_num_item *file_sum_nni;
84 extern char curr_dir[MAXPATHLEN];
86 extern struct chmod_mode_struct *chmod_modes;
88 extern filter_rule_list filter_list, implied_filter_list, daemon_filter_list;
90 #ifdef ICONV_OPTION
91 extern int filesfrom_convert;
92 extern iconv_t ic_send, ic_recv;
93 #endif
95 #define PTR_SIZE (sizeof (struct file_struct *))
97 int io_error;
98 int flist_csum_len;
99 dev_t filesystem_dev; /* used to implement -x */
101 struct file_list *cur_flist, *first_flist, *dir_flist;
102 int send_dir_ndx = -1, send_dir_depth = -1;
103 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
104 int file_total = 0; /* total of all active items over all file-lists */
105 int file_old_total = 0; /* total of active items that will soon be gone */
106 int flist_eof = 0; /* all the file-lists are now known */
107 int xfer_flags_as_varint = 0;
109 #define NORMAL_NAME 0
110 #define SLASH_ENDING_NAME 1
111 #define DOTDIR_NAME 2
112 #define MISSING_NAME 3
114 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
115 * internally, even if this platform does not allow files to have 64-bit inums.
116 * The only exception is if we're on a platform with no 64-bit type at all.
118 * Because we use read_longint() to get these off the wire, if you transfer
119 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
120 * machine with no 64-bit types then you will get an overflow error.
122 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
123 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
124 * be truncated. But it's a kind of silly thing to do anyhow. */
126 /* The tmp_* vars are used as a cache area by make_file() to store data
127 * that the sender doesn't need to remember in its file list. The data
128 * will survive just long enough to be used by send_file_entry(). */
129 static dev_t tmp_rdev;
130 #ifdef SUPPORT_HARD_LINKS
131 static int64 tmp_dev = -1, tmp_ino;
132 #endif
133 static char tmp_sum[MAX_DIGEST_LEN];
135 static char empty_sum[MAX_DIGEST_LEN];
136 static int flist_count_offset; /* for --delete --progress */
137 static int show_filelist_progress;
139 static struct file_list *flist_new(int flags, const char *msg);
140 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
141 static void output_flist(struct file_list *flist);
143 void init_flist(void)
145 if (DEBUG_GTE(FLIST, 4)) {
146 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
147 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
149 /* Note that this isn't identical to file_sum_len in the case of CSUM_MD4_ARCHAIC: */
150 flist_csum_len = csum_len_for_type(file_sum_nni->num, 1);
152 show_filelist_progress = INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
155 static void start_filelist_progress(char *kind)
157 if (quiet)
158 return;
159 rprintf(FCLIENT, "%s ... ", kind);
160 output_needs_newline = 1;
161 rflush(FINFO);
164 static void emit_filelist_progress(int count)
166 if (quiet)
167 return;
168 if (output_needs_newline == 2) /* avoid a newline in the middle of this filelist-progress output */
169 output_needs_newline = 0;
170 rprintf(FCLIENT, " %d files...\r", count);
171 output_needs_newline = 2;
174 static void maybe_emit_filelist_progress(int count)
176 if (INFO_GTE(FLIST, 2) && show_filelist_progress && (count % 100) == 0)
177 emit_filelist_progress(count);
180 static void finish_filelist_progress(const struct file_list *flist)
182 output_needs_newline = 0;
183 if (INFO_GTE(FLIST, 2)) {
184 /* This overwrites the progress line */
185 rprintf(FINFO, "%d file%sto consider\n",
186 flist->used, flist->used == 1 ? " " : "s ");
187 } else {
188 rprintf(FINFO, "done\n");
192 void show_flist_stats(void)
194 /* Nothing yet */
197 /* Stat either a symlink or its referent, depending on the settings of
198 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
200 * If path is the name of a symlink, then the linkbuf buffer (which must hold
201 * MAXPATHLEN chars) will be set to the symlink's target string.
203 * The stat structure pointed to by stp will contain information about the
204 * link or the referent as appropriate, if they exist. */
205 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
207 #ifdef SUPPORT_LINKS
208 if (link_stat(path, stp, copy_dirlinks) < 0)
209 return -1;
210 if (S_ISLNK(stp->st_mode)) {
211 int llen = do_readlink(path, linkbuf, MAXPATHLEN - 1);
212 if (llen < 0)
213 return -1;
214 linkbuf[llen] = '\0';
215 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
216 if (INFO_GTE(SYMSAFE, 1)) {
217 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
218 path, linkbuf);
220 return x_stat(path, stp, NULL);
222 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
223 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
224 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
225 llen - SYMLINK_PREFIX_LEN + 1);
228 return 0;
229 #else
230 return x_stat(path, stp, NULL);
231 #endif
234 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
236 #ifdef SUPPORT_LINKS
237 if (copy_links)
238 return x_stat(path, stp, NULL);
239 if (x_lstat(path, stp, NULL) < 0)
240 return -1;
241 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
242 STRUCT_STAT st;
243 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
244 *stp = st;
246 return 0;
247 #else
248 return x_stat(path, stp, NULL);
249 #endif
252 static inline int path_is_daemon_excluded(char *path, int ignore_filename)
254 if (daemon_filter_list.head) {
255 char *slash = path;
257 while ((slash = strchr(slash+1, '/')) != NULL) {
258 int ret;
259 *slash = '\0';
260 ret = check_filter(&daemon_filter_list, FLOG, path, 1);
261 *slash = '/';
262 if (ret < 0) {
263 errno = ENOENT;
264 return 1;
268 if (!ignore_filename
269 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
270 errno = ENOENT;
271 return 1;
275 return 0;
278 static inline int is_excluded(const char *fname, int is_dir, int filter_level)
280 return name_is_excluded(fname, is_dir ? NAME_IS_DIR : NAME_IS_FILE, filter_level);
283 static void send_directory(int f, struct file_list *flist,
284 char *fbuf, int len, int flags);
286 static const char *pathname, *orig_dir;
287 static int pathname_len;
289 /* Make sure flist can hold at least flist->used + extra entries. */
290 static void flist_expand(struct file_list *flist, int extra)
292 struct file_struct **new_ptr;
294 if (flist->used + extra <= flist->malloced)
295 return;
297 if (flist->malloced < FLIST_START)
298 flist->malloced = FLIST_START;
299 else if (flist->malloced >= FLIST_LINEAR)
300 flist->malloced += FLIST_LINEAR;
301 else if (flist->malloced < FLIST_START_LARGE/16)
302 flist->malloced *= 4;
303 else
304 flist->malloced *= 2;
306 /* In case count jumped or we are starting the list
307 * with a known size just set it. */
308 if (flist->malloced < flist->used + extra)
309 flist->malloced = flist->used + extra;
311 new_ptr = realloc_array(flist->files, struct file_struct *, flist->malloced);
313 if (DEBUG_GTE(FLIST, 1) && flist->files) {
314 rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
315 who_am_i(),
316 big_num(sizeof flist->files[0] * flist->malloced),
317 (new_ptr == flist->files) ? " not" : "");
320 flist->files = new_ptr;
323 static void flist_done_allocating(struct file_list *flist)
325 void *ptr = pool_boundary(flist->file_pool, 8*1024);
326 if (flist->pool_boundary == ptr)
327 flist->pool_boundary = NULL; /* list didn't use any pool memory */
328 else
329 flist->pool_boundary = ptr;
332 /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
333 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
334 * with dir == NULL taken to be the starting directory, and dirlen < 0
335 * indicating that strdup(dir) should be called and then the -dirlen length
336 * value checked to ensure that it is not daemon-excluded. */
337 int change_pathname(struct file_struct *file, const char *dir, int dirlen)
339 if (dirlen < 0) {
340 char *cpy = strdup(dir);
341 if (*cpy != '/')
342 change_dir(orig_dir, CD_SKIP_CHDIR);
343 if (path_is_daemon_excluded(cpy, 0))
344 goto chdir_error;
345 dir = cpy;
346 dirlen = -dirlen;
347 } else {
348 if (file) {
349 if (pathname == F_PATHNAME(file))
350 return 1;
351 dir = F_PATHNAME(file);
352 if (dir)
353 dirlen = strlen(dir);
354 } else if (pathname == dir)
355 return 1;
356 if (dir && *dir != '/')
357 change_dir(orig_dir, CD_SKIP_CHDIR);
360 pathname = dir;
361 pathname_len = dirlen;
363 if (!dir)
364 dir = orig_dir;
366 if (!change_dir(dir, CD_NORMAL)) {
367 chdir_error:
368 io_error |= IOERR_GENERAL;
369 rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
370 if (dir != orig_dir)
371 change_dir(orig_dir, CD_NORMAL);
372 pathname = NULL;
373 pathname_len = 0;
374 return 0;
377 return 1;
380 static void send_file_entry(int f, const char *fname, struct file_struct *file,
381 #ifdef SUPPORT_LINKS
382 const char *symlink_name, int symlink_len,
383 #endif
384 int ndx, int first_ndx)
386 static time_t modtime, atime;
387 #ifdef SUPPORT_CRTIMES
388 static time_t crtime;
389 #endif
390 static mode_t mode;
391 #ifdef SUPPORT_HARD_LINKS
392 static int64 dev;
393 #endif
394 static dev_t rdev;
395 static uint32 rdev_major;
396 static uid_t uid;
397 static gid_t gid;
398 static const char *user_name, *group_name;
399 static char lastname[MAXPATHLEN];
400 #ifdef SUPPORT_HARD_LINKS
401 int first_hlink_ndx = -1;
402 #endif
403 int l1, l2;
404 int xflags;
406 /* Initialize starting value of xflags and adjust counts. */
407 if (S_ISREG(file->mode))
408 xflags = 0;
409 else if (S_ISDIR(file->mode)) {
410 stats.num_dirs++;
411 if (protocol_version >= 30) {
412 if (file->flags & FLAG_CONTENT_DIR)
413 xflags = file->flags & FLAG_TOP_DIR;
414 else if (file->flags & FLAG_IMPLIED_DIR)
415 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
416 else
417 xflags = XMIT_NO_CONTENT_DIR;
418 } else
419 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
420 } else {
421 if (S_ISLNK(file->mode))
422 stats.num_symlinks++;
423 else if (IS_DEVICE(file->mode))
424 stats.num_devices++;
425 else if (IS_SPECIAL(file->mode))
426 stats.num_specials++;
427 xflags = 0;
430 if (file->mode == mode)
431 xflags |= XMIT_SAME_MODE;
432 else
433 mode = file->mode;
435 if (preserve_devices && IS_DEVICE(mode)) {
436 if (protocol_version < 28) {
437 if (tmp_rdev == rdev)
438 xflags |= XMIT_SAME_RDEV_pre28;
439 else
440 rdev = tmp_rdev;
441 } else {
442 rdev = tmp_rdev;
443 if ((uint32)major(rdev) == rdev_major)
444 xflags |= XMIT_SAME_RDEV_MAJOR;
445 else
446 rdev_major = major(rdev);
447 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
448 xflags |= XMIT_RDEV_MINOR_8_pre30;
450 } else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) {
451 /* Special files don't need an rdev number, so just make
452 * the historical transmission of the value efficient. */
453 if (protocol_version < 28)
454 xflags |= XMIT_SAME_RDEV_pre28;
455 else {
456 rdev = MAKEDEV(rdev_major, 0);
457 xflags |= XMIT_SAME_RDEV_MAJOR;
458 if (protocol_version < 30)
459 xflags |= XMIT_RDEV_MINOR_8_pre30;
461 } else if (protocol_version < 28)
462 rdev = MAKEDEV(0, 0);
463 if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
464 xflags |= XMIT_SAME_UID;
465 else {
466 uid = F_OWNER(file);
467 if (!numeric_ids) {
468 user_name = add_uid(uid);
469 if (inc_recurse && user_name)
470 xflags |= XMIT_USER_NAME_FOLLOWS;
473 if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
474 xflags |= XMIT_SAME_GID;
475 else {
476 gid = F_GROUP(file);
477 if (!numeric_ids) {
478 group_name = add_gid(gid);
479 if (inc_recurse && group_name)
480 xflags |= XMIT_GROUP_NAME_FOLLOWS;
483 if (file->modtime == modtime)
484 xflags |= XMIT_SAME_TIME;
485 else
486 modtime = file->modtime;
487 if (NSEC_BUMP(file) && protocol_version >= 31)
488 xflags |= XMIT_MOD_NSEC;
489 if (atimes_ndx && !S_ISDIR(mode)) {
490 if (F_ATIME(file) == atime)
491 xflags |= XMIT_SAME_ATIME;
492 else
493 atime = F_ATIME(file);
495 #ifdef SUPPORT_CRTIMES
496 if (crtimes_ndx) {
497 crtime = F_CRTIME(file);
498 if (crtime == modtime)
499 xflags |= XMIT_CRTIME_EQ_MTIME;
501 #endif
503 #ifdef SUPPORT_HARD_LINKS
504 if (tmp_dev != -1) {
505 if (protocol_version >= 30) {
506 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
507 first_hlink_ndx = (int32)(long)np->data; /* is -1 when new */
508 if (first_hlink_ndx < 0) {
509 np->data = (void*)(long)(first_ndx + ndx);
510 xflags |= XMIT_HLINK_FIRST;
512 if (DEBUG_GTE(HLINK, 1)) {
513 if (first_hlink_ndx >= 0) {
514 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
515 who_am_i(), first_ndx + ndx, first_hlink_ndx,
516 first_hlink_ndx >= first_ndx ? "" : "un");
517 } else if (DEBUG_GTE(HLINK, 3)) {
518 rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
519 who_am_i(), first_ndx + ndx,
520 big_num(tmp_dev), big_num(tmp_ino));
523 } else {
524 if (tmp_dev == dev) {
525 if (protocol_version >= 28)
526 xflags |= XMIT_SAME_DEV_pre30;
527 } else
528 dev = tmp_dev;
530 xflags |= XMIT_HLINKED;
532 #endif
534 for (l1 = 0;
535 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
536 l1++) {}
537 l2 = strlen(fname+l1);
539 if (l1 > 0)
540 xflags |= XMIT_SAME_NAME;
541 if (l2 > 255)
542 xflags |= XMIT_LONG_NAME;
544 /* We must avoid sending a flag value of 0 (or an initial byte of
545 * 0 for the older xflags protocol) or it will signal the end of
546 * the list. Note that the use of XMIT_TOP_DIR on a non-dir has
547 * no meaning, so it's a harmless way to add a bit to the first
548 * flag byte. */
549 if (xfer_flags_as_varint)
550 write_varint(f, xflags ? xflags : XMIT_EXTENDED_FLAGS);
551 else if (protocol_version >= 28) {
552 if (!xflags && !S_ISDIR(mode))
553 xflags |= XMIT_TOP_DIR;
554 if ((xflags & 0xFF00) || !xflags) {
555 xflags |= XMIT_EXTENDED_FLAGS;
556 write_shortint(f, xflags);
557 } else
558 write_byte(f, xflags);
559 } else {
560 if (!(xflags & 0xFF))
561 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
562 write_byte(f, xflags);
564 if (xflags & XMIT_SAME_NAME)
565 write_byte(f, l1);
566 if (xflags & XMIT_LONG_NAME)
567 write_varint30(f, l2);
568 else
569 write_byte(f, l2);
570 write_buf(f, fname + l1, l2);
572 #ifdef SUPPORT_HARD_LINKS
573 if (first_hlink_ndx >= 0) {
574 write_varint(f, first_hlink_ndx);
575 if (first_hlink_ndx >= first_ndx)
576 goto the_end;
578 #endif
580 write_varlong30(f, F_LENGTH(file), 3);
581 if (!(xflags & XMIT_SAME_TIME)) {
582 if (protocol_version >= 30)
583 write_varlong(f, modtime, 4);
584 else
585 write_int(f, modtime);
587 if (xflags & XMIT_MOD_NSEC)
588 write_varint(f, F_MOD_NSEC(file));
589 #ifdef SUPPORT_CRTIMES
590 if (crtimes_ndx && !(xflags & XMIT_CRTIME_EQ_MTIME))
591 write_varlong(f, crtime, 4);
592 #endif
593 if (!(xflags & XMIT_SAME_MODE))
594 write_int(f, to_wire_mode(mode));
595 if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME))
596 write_varlong(f, atime, 4);
597 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
598 if (protocol_version < 30)
599 write_int(f, uid);
600 else {
601 write_varint(f, uid);
602 if (xflags & XMIT_USER_NAME_FOLLOWS) {
603 int len = strlen(user_name);
604 write_byte(f, len);
605 write_buf(f, user_name, len);
609 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
610 if (protocol_version < 30)
611 write_int(f, gid);
612 else {
613 write_varint(f, gid);
614 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
615 int len = strlen(group_name);
616 write_byte(f, len);
617 write_buf(f, group_name, len);
621 if ((preserve_devices && IS_DEVICE(mode))
622 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
623 if (protocol_version < 28) {
624 if (!(xflags & XMIT_SAME_RDEV_pre28))
625 write_int(f, (int)rdev);
626 } else {
627 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
628 write_varint30(f, major(rdev));
629 if (protocol_version >= 30)
630 write_varint(f, minor(rdev));
631 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
632 write_byte(f, minor(rdev));
633 else
634 write_int(f, minor(rdev));
638 #ifdef SUPPORT_LINKS
639 if (symlink_len) {
640 write_varint30(f, symlink_len);
641 write_buf(f, symlink_name, symlink_len);
643 #endif
645 #ifdef SUPPORT_HARD_LINKS
646 if (tmp_dev != -1 && protocol_version < 30) {
647 /* Older protocols expect the dev number to be transmitted
648 * 1-incremented so that it is never zero. */
649 if (protocol_version < 26) {
650 /* 32-bit dev_t and ino_t */
651 write_int(f, (int32)(dev+1));
652 write_int(f, (int32)tmp_ino);
653 } else {
654 /* 64-bit dev_t and ino_t */
655 if (!(xflags & XMIT_SAME_DEV_pre30))
656 write_longint(f, dev+1);
657 write_longint(f, tmp_ino);
660 #endif
662 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
663 const char *sum;
664 if (S_ISREG(mode))
665 sum = tmp_sum;
666 else {
667 /* Prior to 28, we sent a useless set of nulls. */
668 sum = empty_sum;
670 write_buf(f, sum, flist_csum_len);
673 #ifdef SUPPORT_HARD_LINKS
674 the_end:
675 #endif
676 strlcpy(lastname, fname, MAXPATHLEN);
678 if (S_ISREG(mode) || S_ISLNK(mode))
679 stats.total_size += F_LENGTH(file);
682 static struct file_struct *recv_file_entry(int f, struct file_list *flist, int xflags)
684 static int64 modtime, atime;
685 #ifdef SUPPORT_CRTIMES
686 static time_t crtime;
687 #endif
688 static mode_t mode;
689 #ifdef SUPPORT_HARD_LINKS
690 static int64 dev;
691 #endif
692 static dev_t rdev;
693 static uint32 rdev_major;
694 static uid_t uid;
695 static gid_t gid;
696 static uint16 gid_flags;
697 static char lastname[MAXPATHLEN], *lastdir;
698 static int lastdir_depth, lastdir_len = -1;
699 static unsigned int del_hier_name_len = 0;
700 static int in_del_hier = 0;
701 char thisname[MAXPATHLEN];
702 unsigned int l1 = 0, l2 = 0;
703 int alloc_len, basename_len, linkname_len;
704 int extra_len = file_extra_cnt * EXTRA_LEN;
705 int first_hlink_ndx = -1;
706 char real_ISREG_entry;
707 int64 file_length;
708 #ifdef CAN_SET_NSEC
709 uint32 modtime_nsec;
710 #endif
711 const char *basename;
712 struct file_struct *file;
713 alloc_pool_t *pool;
714 char *bp;
716 if (xflags & XMIT_SAME_NAME)
717 l1 = read_byte(f);
719 if (xflags & XMIT_LONG_NAME)
720 l2 = read_varint30(f);
721 else
722 l2 = read_byte(f);
724 if (l2 >= MAXPATHLEN - l1) {
725 rprintf(FERROR,
726 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
727 xflags, l1, l2, lastname, who_am_i());
728 overflow_exit("recv_file_entry");
731 strlcpy(thisname, lastname, l1 + 1);
732 read_sbuf(f, &thisname[l1], l2);
733 thisname[l1 + l2] = 0;
735 /* Abuse basename_len for a moment... */
736 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
738 #ifdef ICONV_OPTION
739 if (ic_recv != (iconv_t)-1) {
740 xbuf outbuf, inbuf;
742 INIT_CONST_XBUF(outbuf, thisname);
743 INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
745 if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
746 io_error |= IOERR_GENERAL;
747 rprintf(FERROR_UTF8,
748 "[%s] cannot convert filename: %s (%s)\n",
749 who_am_i(), lastname, strerror(errno));
750 outbuf.len = 0;
752 thisname[outbuf.len] = '\0';
754 #endif
756 if (*thisname
757 && (clean_fname(thisname, CFN_REFUSE_DOT_DOT_DIRS) < 0 || (!relative_paths && *thisname == '/'))) {
758 rprintf(FERROR, "ABORTING due to unsafe pathname from sender: %s\n", thisname);
759 exit_cleanup(RERR_UNSUPPORTED);
762 if (sanitize_paths)
763 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
765 if ((basename = strrchr(thisname, '/')) != NULL) {
766 int len = basename++ - thisname;
767 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
768 lastdir = new_array(char, len + 1);
769 memcpy(lastdir, thisname, len);
770 lastdir[len] = '\0';
771 lastdir_len = len;
772 lastdir_depth = count_dir_elements(lastdir);
774 } else
775 basename = thisname;
776 basename_len = strlen(basename) + 1; /* count the '\0' */
778 #ifdef SUPPORT_HARD_LINKS
779 if (protocol_version >= 30
780 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
781 first_hlink_ndx = read_varint(f);
782 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
783 rprintf(FERROR,
784 "hard-link reference out of range: %d (%d)\n",
785 first_hlink_ndx, flist->ndx_start + flist->used);
786 exit_cleanup(RERR_PROTOCOL);
788 if (DEBUG_GTE(HLINK, 1)) {
789 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
790 who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
791 first_hlink_ndx >= flist->ndx_start ? "" : "un");
793 if (first_hlink_ndx >= flist->ndx_start) {
794 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
795 file_length = F_LENGTH(first);
796 modtime = first->modtime;
797 #ifdef CAN_SET_NSEC
798 modtime_nsec = F_MOD_NSEC_or_0(first);
799 #endif
800 mode = first->mode;
801 if (atimes_ndx && !S_ISDIR(mode))
802 atime = F_ATIME(first);
803 #ifdef SUPPORT_CRTIMES
804 if (crtimes_ndx)
805 crtime = F_CRTIME(first);
806 #endif
807 if (preserve_uid)
808 uid = F_OWNER(first);
809 if (preserve_gid)
810 gid = F_GROUP(first);
811 if (preserve_devices && IS_DEVICE(mode)) {
812 uint32 *devp = F_RDEV_P(first);
813 rdev_major = DEV_MAJOR(devp);
814 rdev = MAKEDEV(rdev_major, DEV_MINOR(devp));
815 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
817 if (preserve_links && S_ISLNK(mode))
818 linkname_len = strlen(F_SYMLINK(first)) + 1;
819 else
820 linkname_len = 0;
821 real_ISREG_entry = S_ISREG(mode) ? 1 : 0;
822 goto create_object;
825 #endif
827 file_length = read_varlong30(f, 3);
828 if (!(xflags & XMIT_SAME_TIME)) {
829 if (protocol_version >= 30) {
830 modtime = read_varlong(f, 4);
831 #if SIZEOF_TIME_T < SIZEOF_INT64
832 if (!am_generator && (int64)(time_t)modtime != modtime) {
833 rprintf(FERROR_XFER,
834 "Time value of %s truncated on receiver.\n",
835 lastname);
837 #endif
838 } else
839 modtime = read_uint(f);
841 if (xflags & XMIT_MOD_NSEC)
842 #ifndef CAN_SET_NSEC
843 (void)read_varint(f);
844 #else
845 modtime_nsec = read_varint(f);
846 else
847 modtime_nsec = 0;
848 #endif
849 #ifdef SUPPORT_CRTIMES
850 if (crtimes_ndx) {
851 if (xflags & XMIT_CRTIME_EQ_MTIME)
852 crtime = modtime;
853 else
854 crtime = read_varlong(f, 4);
855 #if SIZEOF_TIME_T < SIZEOF_INT64
856 if (!am_generator && (int64)(time_t)crtime != crtime) {
857 rprintf(FERROR_XFER,
858 "Create time value of %s truncated on receiver.\n",
859 lastname);
861 #endif
863 #endif
864 if (!(xflags & XMIT_SAME_MODE))
865 mode = from_wire_mode(read_int(f));
866 if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME)) {
867 atime = read_varlong(f, 4);
868 #if SIZEOF_TIME_T < SIZEOF_INT64
869 if (!am_generator && (int64)(time_t)atime != atime) {
870 rprintf(FERROR_XFER,
871 "Access time value of %s truncated on receiver.\n",
872 lastname);
874 #endif
877 if (chmod_modes && !S_ISLNK(mode) && mode)
878 mode = tweak_mode(mode, chmod_modes);
880 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
881 if (protocol_version < 30)
882 uid = (uid_t)read_int(f);
883 else {
884 uid = (uid_t)read_varint(f);
885 if (xflags & XMIT_USER_NAME_FOLLOWS)
886 uid = recv_user_name(f, uid);
887 else if (inc_recurse && am_root && (!numeric_ids || usermap))
888 uid = match_uid(uid);
891 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
892 if (protocol_version < 30)
893 gid = (gid_t)read_int(f);
894 else {
895 gid = (gid_t)read_varint(f);
896 gid_flags = 0;
897 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
898 gid = recv_group_name(f, gid, &gid_flags);
899 else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
900 gid = match_gid(gid, &gid_flags);
904 if ((preserve_devices && IS_DEVICE(mode))
905 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
906 if (protocol_version < 28) {
907 if (!(xflags & XMIT_SAME_RDEV_pre28))
908 rdev = (dev_t)read_int(f);
909 } else {
910 uint32 rdev_minor;
911 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
912 rdev_major = read_varint30(f);
913 if (protocol_version >= 30)
914 rdev_minor = read_varint(f);
915 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
916 rdev_minor = read_byte(f);
917 else
918 rdev_minor = read_int(f);
919 rdev = MAKEDEV(rdev_major, rdev_minor);
921 if (IS_DEVICE(mode))
922 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
923 file_length = 0;
924 } else if (protocol_version < 28)
925 rdev = MAKEDEV(0, 0);
927 #ifdef SUPPORT_LINKS
928 if (preserve_links && S_ISLNK(mode)) {
929 linkname_len = read_varint30(f) + 1; /* count the '\0' */
930 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
931 rprintf(FERROR, "overflow: linkname_len=%d\n",
932 linkname_len - 1);
933 overflow_exit("recv_file_entry");
935 #ifdef ICONV_OPTION
936 /* We don't know how much extra room we need to convert
937 * the as-yet-unread symlink data, so let's hope that a
938 * double-size buffer is plenty. */
939 if (sender_symlink_iconv)
940 linkname_len *= 2;
941 #endif
942 if (munge_symlinks)
943 linkname_len += SYMLINK_PREFIX_LEN;
945 else
946 #endif
947 linkname_len = 0;
949 if (copy_devices && IS_DEVICE(mode)) {
950 /* This is impossible in the official release, but some pre-release patches
951 * didn't convert the device into a file before sending, so we'll do it here
952 * (even though the length is typically 0 and any checksum data is zeros). */
953 mode = S_IFREG | (mode & ACCESSPERMS);
954 modtime = time(NULL); /* The mtime on the device is not up-to-date, so set it to "now". */
955 real_ISREG_entry = 0;
956 } else
957 real_ISREG_entry = S_ISREG(mode) ? 1 : 0;
959 #ifdef SUPPORT_HARD_LINKS
960 create_object:
961 if (preserve_hard_links) {
962 if (protocol_version < 28 && real_ISREG_entry)
963 xflags |= XMIT_HLINKED;
964 if (xflags & XMIT_HLINKED)
965 extra_len += (inc_recurse+1) * EXTRA_LEN;
967 #endif
969 #ifdef SUPPORT_ACLS
970 /* Directories need an extra int32 for the default ACL. */
971 if (preserve_acls && S_ISDIR(mode))
972 extra_len += EXTRA_LEN;
973 #endif
975 if (always_checksum && S_ISREG(mode))
976 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
978 #if SIZEOF_INT64 >= 8
979 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
980 extra_len += EXTRA_LEN;
981 #endif
982 #ifdef CAN_SET_NSEC
983 if (modtime_nsec)
984 extra_len += EXTRA_LEN;
985 #endif
986 if (file_length < 0) {
987 rprintf(FERROR, "Offset underflow: file-length is negative\n");
988 exit_cleanup(RERR_UNSUPPORTED);
991 if (*thisname == '/' ? thisname[1] != '.' || thisname[2] != '\0' : *thisname != '.' || thisname[1] != '\0') {
992 int filt_flags = S_ISDIR(mode) ? NAME_IS_DIR : NAME_IS_FILE;
993 if (!trust_sender_filter /* a per-dir filter rule means we must trust the sender's filtering */
994 && filter_list.head && check_server_filter(&filter_list, FINFO, thisname, filt_flags) < 0) {
995 rprintf(FERROR, "ERROR: rejecting excluded file-list name: %s\n", thisname);
996 exit_cleanup(RERR_UNSUPPORTED);
998 if (implied_filter_list.head && check_filter(&implied_filter_list, FINFO, thisname, filt_flags) <= 0) {
999 rprintf(FERROR, "ERROR: rejecting unrequested file-list name: %s\n", thisname);
1000 exit_cleanup(RERR_UNSUPPORTED);
1004 if (inc_recurse && S_ISDIR(mode)) {
1005 if (one_file_system) {
1006 /* Room to save the dir's device for -x */
1007 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
1009 pool = dir_flist->file_pool;
1010 } else
1011 pool = flist->file_pool;
1013 #if EXTRA_ROUNDING > 0
1014 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1015 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1016 #endif
1018 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1019 + linkname_len;
1020 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
1022 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1023 bp += extra_len;
1024 file = (struct file_struct *)bp;
1025 bp += FILE_STRUCT_LEN;
1027 memcpy(bp, basename, basename_len);
1029 #ifdef SUPPORT_HARD_LINKS
1030 if (xflags & XMIT_HLINKED
1031 #ifndef CAN_HARDLINK_SYMLINK
1032 && !S_ISLNK(mode)
1033 #endif
1034 #ifndef CAN_HARDLINK_SPECIAL
1035 && !IS_SPECIAL(mode) && !IS_DEVICE(mode)
1036 #endif
1038 file->flags |= FLAG_HLINKED;
1039 #endif
1040 file->modtime = (time_t)modtime;
1041 #ifdef CAN_SET_NSEC
1042 if (modtime_nsec) {
1043 file->flags |= FLAG_MOD_NSEC;
1044 F_MOD_NSEC(file) = modtime_nsec;
1046 #endif
1047 file->len32 = (uint32)file_length;
1048 #if SIZEOF_INT64 >= 8
1049 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
1050 #if SIZEOF_CAPITAL_OFF_T < 8
1051 rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
1052 exit_cleanup(RERR_UNSUPPORTED);
1053 #else
1054 file->flags |= FLAG_LENGTH64;
1055 F_HIGH_LEN(file) = (uint32)(file_length >> 32);
1056 #endif
1058 #endif
1059 file->mode = mode;
1060 if (preserve_uid)
1061 F_OWNER(file) = uid;
1062 if (preserve_gid) {
1063 F_GROUP(file) = gid;
1064 file->flags |= gid_flags;
1066 if (atimes_ndx && !S_ISDIR(mode))
1067 F_ATIME(file) = atime;
1068 #ifdef SUPPORT_CRTIMES
1069 if (crtimes_ndx)
1070 F_CRTIME(file) = crtime;
1071 #endif
1072 if (unsort_ndx)
1073 F_NDX(file) = flist->used + flist->ndx_start;
1075 if (basename != thisname) {
1076 file->dirname = lastdir;
1077 F_DEPTH(file) = lastdir_depth + 1;
1078 } else
1079 F_DEPTH(file) = 1;
1081 if (S_ISDIR(mode)) {
1082 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
1083 F_DEPTH(file)--;
1084 if (protocol_version >= 30) {
1085 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
1086 if (xflags & XMIT_TOP_DIR)
1087 file->flags |= FLAG_TOP_DIR;
1088 file->flags |= FLAG_CONTENT_DIR;
1089 } else if (xflags & XMIT_TOP_DIR)
1090 file->flags |= FLAG_IMPLIED_DIR;
1091 } else if (xflags & XMIT_TOP_DIR) {
1092 in_del_hier = recurse;
1093 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
1094 if (relative_paths && del_hier_name_len > 2
1095 && lastname[del_hier_name_len-1] == '.'
1096 && lastname[del_hier_name_len-2] == '/')
1097 del_hier_name_len -= 2;
1098 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
1099 } else if (in_del_hier) {
1100 if (!relative_paths || !del_hier_name_len
1101 || (l1 >= del_hier_name_len
1102 && lastname[del_hier_name_len] == '/'))
1103 file->flags |= FLAG_CONTENT_DIR;
1104 else
1105 in_del_hier = 0;
1109 if (preserve_devices && IS_DEVICE(mode)) {
1110 uint32 *devp = F_RDEV_P(file);
1111 DEV_MAJOR(devp) = major(rdev);
1112 DEV_MINOR(devp) = minor(rdev);
1115 #ifdef SUPPORT_LINKS
1116 if (linkname_len) {
1117 bp += basename_len;
1118 if (first_hlink_ndx >= flist->ndx_start) {
1119 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1120 memcpy(bp, F_SYMLINK(first), linkname_len);
1121 } else {
1122 if (munge_symlinks) {
1123 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
1124 bp += SYMLINK_PREFIX_LEN;
1125 linkname_len -= SYMLINK_PREFIX_LEN;
1127 #ifdef ICONV_OPTION
1128 if (sender_symlink_iconv) {
1129 xbuf outbuf, inbuf;
1131 alloc_len = linkname_len;
1132 linkname_len /= 2;
1134 /* Read the symlink data into the end of our double-sized
1135 * buffer and then convert it into the right spot. */
1136 INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
1137 linkname_len - 1, (size_t)-1);
1138 read_sbuf(f, inbuf.buf, inbuf.len);
1139 INIT_XBUF(outbuf, bp, 0, alloc_len);
1141 if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
1142 io_error |= IOERR_GENERAL;
1143 rprintf(FERROR_XFER,
1144 "[%s] cannot convert symlink data for: %s (%s)\n",
1145 who_am_i(), full_fname(thisname), strerror(errno));
1146 bp = (char*)file->basename;
1147 *bp++ = '\0';
1148 outbuf.len = 0;
1150 bp[outbuf.len] = '\0';
1151 } else
1152 #endif
1153 read_sbuf(f, bp, linkname_len - 1);
1154 if (sanitize_paths && !munge_symlinks && *bp)
1155 sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
1158 #endif
1160 #ifdef SUPPORT_HARD_LINKS
1161 if (preserve_hard_links && xflags & XMIT_HLINKED) {
1162 if (protocol_version >= 30) {
1163 if (xflags & XMIT_HLINK_FIRST) {
1164 F_HL_GNUM(file) = flist->ndx_start + flist->used;
1165 } else
1166 F_HL_GNUM(file) = first_hlink_ndx;
1167 } else {
1168 static int32 cnt = 0;
1169 struct ht_int64_node *np;
1170 int64 ino;
1171 int32 ndx;
1172 if (protocol_version < 26) {
1173 dev = read_int(f);
1174 ino = read_int(f);
1175 } else {
1176 if (!(xflags & XMIT_SAME_DEV_pre30))
1177 dev = read_longint(f);
1178 ino = read_longint(f);
1180 np = idev_find(dev, ino);
1181 ndx = (int32)(long)np->data; /* is -1 when new */
1182 if (ndx < 0) {
1183 np->data = (void*)(long)cnt;
1184 ndx = cnt++;
1186 F_HL_GNUM(file) = ndx;
1189 #endif
1191 if (always_checksum && (real_ISREG_entry || protocol_version < 28)) {
1192 if (real_ISREG_entry)
1193 bp = F_SUM(file);
1194 else {
1195 /* Prior to 28, we get a useless set of nulls. */
1196 bp = tmp_sum;
1198 if (first_hlink_ndx >= flist->ndx_start) {
1199 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1200 memcpy(bp, F_SUM(first), flist_csum_len);
1201 } else
1202 read_buf(f, bp, flist_csum_len);
1205 #ifdef SUPPORT_ACLS
1206 if (preserve_acls && !S_ISLNK(mode))
1207 receive_acl(f, file);
1208 #endif
1209 #ifdef SUPPORT_XATTRS
1210 if (preserve_xattrs)
1211 receive_xattr(f, file);
1212 #endif
1214 if (S_ISREG(mode) || S_ISLNK(mode))
1215 stats.total_size += file_length;
1217 return file;
1220 /* Create a file_struct for a named file by reading its stat() information
1221 * and performing extensive checks against global options.
1223 * Returns a pointer to the new file struct, or NULL if there was an error
1224 * or this file should be excluded.
1226 * Note: Any error (here or in send_file_name) that results in the omission of
1227 * an existent source file from the file list should set
1228 * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1229 * destination if --delete is on. */
1230 struct file_struct *make_file(const char *fname, struct file_list *flist,
1231 STRUCT_STAT *stp, int flags, int filter_level)
1233 static char *lastdir;
1234 static int lastdir_len = -1;
1235 struct file_struct *file;
1236 char thisname[MAXPATHLEN];
1237 char linkname[MAXPATHLEN];
1238 int alloc_len, basename_len, linkname_len;
1239 int extra_len = file_extra_cnt * EXTRA_LEN;
1240 const char *basename;
1241 alloc_pool_t *pool;
1242 STRUCT_STAT st;
1243 char *bp;
1245 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1246 io_error |= IOERR_GENERAL;
1247 rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
1248 return NULL;
1250 clean_fname(thisname, 0);
1251 if (sanitize_paths)
1252 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
1254 if (stp && (S_ISDIR(stp->st_mode) || IS_MISSING_FILE(*stp))) {
1255 /* This is needed to handle a "symlink/." with a --relative
1256 * dir, or a request to delete a specific file. */
1257 st = *stp;
1258 *linkname = '\0'; /* make IBM code checker happy */
1259 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1260 int save_errno = errno;
1261 /* See if file is excluded before reporting an error. */
1262 if (filter_level != NO_FILTERS
1263 && (is_excluded(thisname, 0, filter_level)
1264 || is_excluded(thisname, 1, filter_level))) {
1265 if (ignore_perishable && save_errno != ENOENT)
1266 non_perishable_cnt++;
1267 return NULL;
1269 if (save_errno == ENOENT) {
1270 #ifdef SUPPORT_LINKS
1271 /* When our options tell us to follow a symlink that
1272 * points nowhere, tell the user about the symlink
1273 * instead of giving a "vanished" message. We only
1274 * dereference a symlink if one of the --copy*links
1275 * options was specified, so there's no need for the
1276 * extra lstat() if one of these options isn't on. */
1277 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1278 && x_lstat(thisname, &st, NULL) == 0
1279 && S_ISLNK(st.st_mode)) {
1280 io_error |= IOERR_GENERAL;
1281 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1282 full_fname(thisname));
1283 } else
1284 #endif
1286 enum logcode c = am_daemon && protocol_version < 28
1287 ? FERROR : FWARNING;
1288 io_error |= IOERR_VANISHED;
1289 rprintf(c, "file has vanished: %s\n",
1290 full_fname(thisname));
1292 } else {
1293 io_error |= IOERR_GENERAL;
1294 rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
1295 full_fname(thisname));
1297 return NULL;
1298 } else if (IS_MISSING_FILE(st)) {
1299 io_error |= IOERR_GENERAL;
1300 rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n",
1301 full_fname(thisname));
1302 return NULL;
1305 if (filter_level == NO_FILTERS)
1306 goto skip_filters;
1308 if (S_ISDIR(st.st_mode)) {
1309 if (!xfer_dirs) {
1310 rprintf(FINFO, "skipping directory %s\n", thisname);
1311 return NULL;
1313 /* -x only affects dirs because we need to avoid recursing
1314 * into a mount-point directory, not to avoid copying a
1315 * symlinked file if -L (or similar) was specified. */
1316 if (one_file_system && st.st_dev != filesystem_dev
1317 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1318 if (one_file_system > 1) {
1319 if (INFO_GTE(MOUNT, 1)) {
1320 rprintf(FINFO,
1321 "[%s] skipping mount-point dir %s\n",
1322 who_am_i(), thisname);
1324 return NULL;
1326 flags |= FLAG_MOUNT_DIR;
1327 flags &= ~FLAG_CONTENT_DIR;
1329 } else
1330 flags &= ~FLAG_CONTENT_DIR;
1332 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1333 if (ignore_perishable)
1334 non_perishable_cnt++;
1335 return NULL;
1338 if (lp_ignore_nonreadable(module_id)) {
1339 #ifdef SUPPORT_LINKS
1340 if (!S_ISLNK(st.st_mode))
1341 #endif
1342 if (access(thisname, R_OK) != 0)
1343 return NULL;
1346 skip_filters:
1348 /* Only divert a directory in the main transfer. */
1349 if (flist) {
1350 if (flist->prev && S_ISDIR(st.st_mode)
1351 && flags & FLAG_DIVERT_DIRS) {
1352 /* Room for parent/sibling/next-child info. */
1353 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1354 if (relative_paths)
1355 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1356 pool = dir_flist->file_pool;
1357 } else
1358 pool = flist->file_pool;
1359 } else {
1360 #ifdef SUPPORT_ACLS
1361 /* Directories need an extra int32 for the default ACL. */
1362 if (preserve_acls && S_ISDIR(st.st_mode))
1363 extra_len += EXTRA_LEN;
1364 #endif
1365 pool = NULL;
1368 if (DEBUG_GTE(FLIST, 2)) {
1369 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1370 who_am_i(), thisname, filter_level);
1373 if ((basename = strrchr(thisname, '/')) != NULL) {
1374 int len = basename++ - thisname;
1375 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1376 lastdir = new_array(char, len + 1);
1377 memcpy(lastdir, thisname, len);
1378 lastdir[len] = '\0';
1379 lastdir_len = len;
1381 } else
1382 basename = thisname;
1383 basename_len = strlen(basename) + 1; /* count the '\0' */
1385 #ifdef SUPPORT_LINKS
1386 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1387 #else
1388 linkname_len = 0;
1389 #endif
1391 if (copy_devices && am_sender && IS_DEVICE(st.st_mode)) {
1392 if (st.st_size == 0) {
1393 int fd = do_open(fname, O_RDONLY, 0);
1394 if (fd >= 0) {
1395 st.st_size = get_device_size(fd, fname);
1396 close(fd);
1399 st.st_mode = S_IFREG | (st.st_mode & ACCESSPERMS);
1400 st.st_mtime = time(NULL); /* The mtime on the device is not up-to-date, so set it to "now". */
1403 #ifdef ST_MTIME_NSEC
1404 if (st.ST_MTIME_NSEC && protocol_version >= 31)
1405 extra_len += EXTRA_LEN;
1406 #endif
1407 #if SIZEOF_CAPITAL_OFF_T >= 8
1408 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1409 extra_len += EXTRA_LEN;
1410 #endif
1412 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1413 file_checksum(thisname, &st, tmp_sum);
1414 if (sender_keeps_checksum)
1415 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1418 #if EXTRA_ROUNDING > 0
1419 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1420 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1421 #endif
1423 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1424 + linkname_len;
1425 if (pool)
1426 bp = pool_alloc(pool, alloc_len, "make_file");
1427 else
1428 bp = new_array(char, alloc_len);
1430 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1431 bp += extra_len;
1432 file = (struct file_struct *)bp;
1433 bp += FILE_STRUCT_LEN;
1435 memcpy(bp, basename, basename_len);
1437 #ifdef SUPPORT_HARD_LINKS
1438 if (preserve_hard_links && flist && flist->prev) {
1439 if (protocol_version >= 28
1440 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1441 : S_ISREG(st.st_mode)) {
1442 tmp_dev = (int64)st.st_dev;
1443 tmp_ino = (int64)st.st_ino;
1444 } else
1445 tmp_dev = -1;
1447 #endif
1449 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1450 if (IS_DEVICE(st.st_mode)) {
1451 tmp_rdev = st.st_rdev;
1452 st.st_size = 0;
1453 } else if (IS_SPECIAL(st.st_mode))
1454 st.st_size = 0;
1455 #endif
1457 file->flags = flags;
1458 file->modtime = st.st_mtime;
1459 #ifdef ST_MTIME_NSEC
1460 if (st.ST_MTIME_NSEC && protocol_version >= 31) {
1461 file->flags |= FLAG_MOD_NSEC;
1462 F_MOD_NSEC(file) = st.ST_MTIME_NSEC;
1464 #endif
1465 file->len32 = (uint32)st.st_size;
1466 #if SIZEOF_CAPITAL_OFF_T >= 8
1467 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1468 file->flags |= FLAG_LENGTH64;
1469 F_HIGH_LEN(file) = (uint32)(st.st_size >> 32);
1471 #endif
1472 file->mode = st.st_mode;
1473 if (preserve_uid)
1474 F_OWNER(file) = st.st_uid;
1475 if (preserve_gid)
1476 F_GROUP(file) = st.st_gid;
1477 if (am_generator && st.st_uid == our_uid)
1478 file->flags |= FLAG_OWNED_BY_US;
1479 if (atimes_ndx && !S_ISDIR(file->mode))
1480 F_ATIME(file) = st.st_atime;
1481 #ifdef SUPPORT_CRTIMES
1482 if (crtimes_ndx)
1483 F_CRTIME(file) = get_create_time(fname, &st);
1484 #endif
1486 if (basename != thisname)
1487 file->dirname = lastdir;
1489 #ifdef SUPPORT_LINKS
1490 if (linkname_len)
1491 memcpy(bp + basename_len, linkname, linkname_len);
1492 #endif
1494 if (am_sender)
1495 F_PATHNAME(file) = pathname;
1496 else if (!pool)
1497 F_DEPTH(file) = extra_len / EXTRA_LEN;
1499 if (basename_len == 0+1) {
1500 if (!pool)
1501 unmake_file(file);
1502 return NULL;
1505 if (sender_keeps_checksum && S_ISREG(st.st_mode))
1506 memcpy(F_SUM(file), tmp_sum, flist_csum_len);
1508 if (unsort_ndx)
1509 F_NDX(file) = stats.num_dirs;
1511 return file;
1514 OFF_T get_device_size(int fd, const char *fname)
1516 OFF_T off = lseek(fd, 0, SEEK_END);
1518 if (off == (OFF_T) -1) {
1519 rsyserr(FERROR, errno, "failed to get device size via seek: %s", fname);
1520 return 0;
1522 if (lseek(fd, 0, SEEK_SET) != 0)
1523 rsyserr(FERROR, errno, "failed to seek device back to start: %s", fname);
1525 return off;
1528 /* Only called for temporary file_struct entries created by make_file(). */
1529 void unmake_file(struct file_struct *file)
1531 free(REQ_EXTRA(file, F_DEPTH(file)));
1534 static struct file_struct *send_file_name(int f, struct file_list *flist,
1535 const char *fname, STRUCT_STAT *stp,
1536 int flags, int filter_level)
1538 struct file_struct *file;
1540 file = make_file(fname, flist, stp, flags, filter_level);
1541 if (!file)
1542 return NULL;
1544 if (chmod_modes && !S_ISLNK(file->mode) && file->mode)
1545 file->mode = tweak_mode(file->mode, chmod_modes);
1547 if (f >= 0) {
1548 char fbuf[MAXPATHLEN];
1549 #ifdef SUPPORT_LINKS
1550 const char *symlink_name;
1551 int symlink_len;
1552 #ifdef ICONV_OPTION
1553 char symlink_buf[MAXPATHLEN];
1554 #endif
1555 #endif
1556 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1557 stat_x sx;
1558 init_stat_x(&sx);
1559 #endif
1561 #ifdef SUPPORT_LINKS
1562 if (preserve_links && S_ISLNK(file->mode)) {
1563 symlink_name = F_SYMLINK(file);
1564 symlink_len = strlen(symlink_name);
1565 if (symlink_len == 0) {
1566 io_error |= IOERR_GENERAL;
1567 f_name(file, fbuf);
1568 rprintf(FERROR_XFER,
1569 "skipping symlink with 0-length value: %s\n",
1570 full_fname(fbuf));
1571 return NULL;
1573 } else {
1574 symlink_name = NULL;
1575 symlink_len = 0;
1577 #endif
1579 #ifdef ICONV_OPTION
1580 if (ic_send != (iconv_t)-1) {
1581 xbuf outbuf, inbuf;
1583 INIT_CONST_XBUF(outbuf, fbuf);
1585 if (file->dirname) {
1586 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1587 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1588 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0)
1589 goto convert_error;
1590 outbuf.size += 2;
1591 fbuf[outbuf.len++] = '/';
1594 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1595 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1596 convert_error:
1597 io_error |= IOERR_GENERAL;
1598 rprintf(FERROR_XFER,
1599 "[%s] cannot convert filename: %s (%s)\n",
1600 who_am_i(), f_name(file, fbuf), strerror(errno));
1601 return NULL;
1603 fbuf[outbuf.len] = '\0';
1605 #ifdef SUPPORT_LINKS
1606 if (symlink_len && sender_symlink_iconv) {
1607 INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1608 INIT_CONST_XBUF(outbuf, symlink_buf);
1609 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1610 io_error |= IOERR_GENERAL;
1611 f_name(file, fbuf);
1612 rprintf(FERROR_XFER,
1613 "[%s] cannot convert symlink data for: %s (%s)\n",
1614 who_am_i(), full_fname(fbuf), strerror(errno));
1615 return NULL;
1617 symlink_buf[outbuf.len] = '\0';
1619 symlink_name = symlink_buf;
1620 symlink_len = outbuf.len;
1622 #endif
1623 } else
1624 #endif
1625 f_name(file, fbuf);
1627 #ifdef SUPPORT_ACLS
1628 if (preserve_acls && !S_ISLNK(file->mode)) {
1629 sx.st.st_mode = file->mode;
1630 if (get_acl(fname, &sx) < 0) {
1631 io_error |= IOERR_GENERAL;
1632 return NULL;
1635 #endif
1636 #ifdef SUPPORT_XATTRS
1637 if (preserve_xattrs) {
1638 sx.st.st_mode = file->mode;
1639 if (get_xattr(fname, &sx) < 0) {
1640 io_error |= IOERR_GENERAL;
1641 return NULL;
1644 #endif
1646 send_file_entry(f, fbuf, file,
1647 #ifdef SUPPORT_LINKS
1648 symlink_name, symlink_len,
1649 #endif
1650 flist->used, flist->ndx_start);
1652 #ifdef SUPPORT_ACLS
1653 if (preserve_acls && !S_ISLNK(file->mode)) {
1654 send_acl(f, &sx);
1655 free_acl(&sx);
1657 #endif
1658 #ifdef SUPPORT_XATTRS
1659 if (preserve_xattrs) {
1660 F_XATTR(file) = send_xattr(f, &sx);
1661 free_xattr(&sx);
1663 #endif
1666 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1668 flist_expand(flist, 1);
1669 flist->files[flist->used++] = file;
1671 return file;
1674 static void send_if_directory(int f, struct file_list *flist,
1675 struct file_struct *file,
1676 char *fbuf, unsigned int ol,
1677 int flags)
1679 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1681 if (S_ISDIR(file->mode)
1682 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1683 void *save_filters;
1684 unsigned int len = strlen(fbuf);
1685 if (len > 1 && fbuf[len-1] == '/')
1686 fbuf[--len] = '\0';
1687 save_filters = push_local_filters(fbuf, len);
1688 send_directory(f, flist, fbuf, len, flags);
1689 pop_local_filters(save_filters);
1690 fbuf[ol] = '\0';
1691 if (is_dot_dir)
1692 fbuf[ol-1] = '.';
1696 static int file_compare(const void *file1, const void *file2)
1698 return f_name_cmp(*(struct file_struct **)file1,
1699 *(struct file_struct **)file2);
1702 /* The guts of a merge-sort algorithm. This was derived from the glibc
1703 * version, but I (Wayne) changed the merge code to do less copying and
1704 * to require only half the amount of temporary memory. */
1705 static void fsort_tmp(struct file_struct **fp, size_t num,
1706 struct file_struct **tmp)
1708 struct file_struct **f1, **f2, **t;
1709 size_t n1, n2;
1711 n1 = num / 2;
1712 n2 = num - n1;
1713 f1 = fp;
1714 f2 = fp + n1;
1716 if (n1 > 1)
1717 fsort_tmp(f1, n1, tmp);
1718 if (n2 > 1)
1719 fsort_tmp(f2, n2, tmp);
1721 while (f_name_cmp(*f1, *f2) <= 0) {
1722 if (!--n1)
1723 return;
1724 f1++;
1727 t = tmp;
1728 memcpy(t, f1, n1 * PTR_SIZE);
1730 *f1++ = *f2++, n2--;
1732 while (n1 > 0 && n2 > 0) {
1733 if (f_name_cmp(*t, *f2) <= 0)
1734 *f1++ = *t++, n1--;
1735 else
1736 *f1++ = *f2++, n2--;
1739 if (n1 > 0)
1740 memcpy(f1, t, n1 * PTR_SIZE);
1743 /* This file-struct sorting routine makes sure that any identical names in
1744 * the file list stay in the same order as they were in the original list.
1745 * This is particularly vital in inc_recurse mode where we expect a sort
1746 * on the flist to match the exact order of a sort on the dir_flist. */
1747 static void fsort(struct file_struct **fp, size_t num)
1749 if (num <= 1)
1750 return;
1752 if (use_qsort)
1753 qsort(fp, num, PTR_SIZE, file_compare);
1754 else {
1755 struct file_struct **tmp = new_array(struct file_struct *, (num+1) / 2);
1756 fsort_tmp(fp, num, tmp);
1757 free(tmp);
1761 /* We take an entire set of sibling dirs from the sorted flist and link them
1762 * into the tree, setting the appropriate parent/child/sibling pointers. */
1763 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1764 int dir_cnt)
1766 int i;
1767 int32 *dp = NULL;
1768 int32 *parent_dp = parent_ndx < 0 ? NULL
1769 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1771 /* The sending side is adding entries to dir_flist in sorted order, so sorted & files are the same. */
1772 flist_expand(dir_flist, dir_cnt);
1773 dir_flist->sorted = dir_flist->files;
1775 for (i = 0; dir_cnt; i++) {
1776 struct file_struct *file = from_flist->sorted[i];
1778 if (!S_ISDIR(file->mode))
1779 continue;
1781 dir_flist->files[dir_flist->used++] = file;
1782 dir_cnt--;
1784 if (file->basename[0] == '.' && file->basename[1] == '\0')
1785 continue;
1787 if (dp)
1788 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1789 else if (parent_dp)
1790 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1791 else
1792 send_dir_ndx = dir_flist->used - 1;
1794 dp = F_DIR_NODE_P(file);
1795 DIR_PARENT(dp) = parent_ndx;
1796 DIR_FIRST_CHILD(dp) = -1;
1798 if (dp)
1799 DIR_NEXT_SIBLING(dp) = -1;
1802 static void interpret_stat_error(const char *fname, int is_dir)
1804 if (errno == ENOENT) {
1805 io_error |= IOERR_VANISHED;
1806 rprintf(FWARNING, "%s has vanished: %s\n",
1807 is_dir ? "directory" : "file", full_fname(fname));
1808 } else {
1809 io_error |= IOERR_GENERAL;
1810 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1811 full_fname(fname));
1815 /* This function is normally called by the sender, but the receiving side also
1816 * calls it from get_dirlist() with f set to -1 so that we just construct the
1817 * file list in memory without sending it over the wire. Also, get_dirlist()
1818 * might call this with f set to -2, which also indicates that local filter
1819 * rules should be ignored. */
1820 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1821 int flags)
1823 struct dirent *di;
1824 unsigned remainder;
1825 char *p;
1826 DIR *d;
1827 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1828 int start = flist->used;
1829 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1831 assert(flist != NULL);
1833 if (!(d = opendir(fbuf))) {
1834 if (errno == ENOENT) {
1835 if (am_sender) /* Can abuse this for vanished error w/ENOENT: */
1836 interpret_stat_error(fbuf, True);
1837 return;
1839 if (errno == ENOTDIR && (flags & FLAG_PERHAPS_DIR))
1840 return;
1841 io_error |= IOERR_GENERAL;
1842 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1843 return;
1846 p = fbuf + len;
1847 if (len == 1 && *fbuf == '/')
1848 remainder = MAXPATHLEN - 1;
1849 else if (len < MAXPATHLEN-1) {
1850 *p++ = '/';
1851 *p = '\0';
1852 remainder = MAXPATHLEN - (len + 1);
1853 } else
1854 remainder = 0;
1856 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1857 unsigned name_len;
1858 char *dname = d_name(di);
1859 if (dname[0] == '.' && (dname[1] == '\0'
1860 || (dname[1] == '.' && dname[2] == '\0')))
1861 continue;
1862 name_len = strlcpy(p, dname, remainder);
1863 if (name_len >= remainder) {
1864 char save = fbuf[len];
1865 fbuf[len] = '\0';
1866 io_error |= IOERR_GENERAL;
1867 rprintf(FERROR_XFER,
1868 "filename overflows max-path len by %u: %s/%s\n",
1869 name_len - remainder + 1, fbuf, dname);
1870 fbuf[len] = save;
1871 continue;
1873 if (dname[0] == '\0') {
1874 io_error |= IOERR_GENERAL;
1875 rprintf(FERROR_XFER,
1876 "cannot send file with empty name in %s\n",
1877 full_fname(fbuf));
1878 continue;
1881 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1884 fbuf[len] = '\0';
1886 if (errno) {
1887 io_error |= IOERR_GENERAL;
1888 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1891 closedir(d);
1893 if (f >= 0 && recurse && !divert_dirs) {
1894 int i, end = flist->used - 1;
1895 /* send_if_directory() bumps flist->used, so use "end". */
1896 for (i = start; i <= end; i++)
1897 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1901 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1902 char *start, char *limit, int flags, char name_type)
1904 static char lastpath[MAXPATHLEN] = "";
1905 static int lastpath_len = 0;
1906 static struct file_struct *lastpath_struct = NULL;
1907 struct file_struct *file;
1908 item_list *relname_list;
1909 relnamecache **rnpp;
1910 int len, need_new_dir, depth = 0;
1911 filter_rule_list save_filter_list = filter_list;
1913 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1914 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1916 if (inc_recurse) {
1917 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1918 && lastpath_len == limit - fname
1919 && strncmp(lastpath, fname, lastpath_len) == 0)
1920 need_new_dir = 0;
1921 else
1922 need_new_dir = 1;
1923 } else {
1924 char *tp = fname, *lp = lastpath;
1925 /* Skip any initial directories in our path that we
1926 * have in common with lastpath. */
1927 assert(start == fname);
1928 for ( ; ; tp++, lp++) {
1929 if (tp == limit) {
1930 if (*lp == '/' || *lp == '\0')
1931 goto done;
1932 break;
1934 if (*lp != *tp)
1935 break;
1936 if (*tp == '/') {
1937 start = tp;
1938 depth++;
1941 need_new_dir = 1;
1944 if (need_new_dir) {
1945 int save_copy_links = copy_links;
1946 int save_xfer_dirs = xfer_dirs;
1947 char *slash;
1949 copy_links = xfer_dirs = 1;
1951 *limit = '\0';
1953 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1954 *slash = '\0';
1955 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1956 depth++;
1957 if (!inc_recurse && file && S_ISDIR(file->mode))
1958 change_local_filter_dir(fname, strlen(fname), depth);
1959 *slash = '/';
1962 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1963 if (inc_recurse) {
1964 if (file && !S_ISDIR(file->mode))
1965 file = NULL;
1966 lastpath_struct = file;
1967 } else if (file && S_ISDIR(file->mode))
1968 change_local_filter_dir(fname, strlen(fname), ++depth);
1970 strlcpy(lastpath, fname, sizeof lastpath);
1971 lastpath_len = limit - fname;
1973 *limit = '/';
1975 copy_links = save_copy_links;
1976 xfer_dirs = save_xfer_dirs;
1978 if (!inc_recurse)
1979 goto done;
1982 if (!lastpath_struct)
1983 goto done; /* dir must have vanished */
1985 len = strlen(limit+1);
1986 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1987 if (!relname_list) {
1988 relname_list = new0(item_list);
1989 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1991 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1992 *rnpp = (relnamecache*)new_array(char, RELNAMECACHE_LEN + len + 1);
1993 (*rnpp)->name_type = name_type;
1994 strlcpy((*rnpp)->fname, limit+1, len + 1);
1996 done:
1997 filter_list = save_filter_list;
2000 static NORETURN void fatal_unsafe_io_error(void)
2002 /* This (sadly) can only happen when pushing data because
2003 * the sender does not know about what kind of delete
2004 * is in effect on the receiving side when pulling. */
2005 rprintf(FERROR_XFER, "FATAL I/O ERROR: dying to avoid a --delete-%s issue with a pre-3.0.7 receiver.\n",
2006 delete_during == 2 ? "delay" : "during");
2007 exit_cleanup(RERR_UNSUPPORTED);
2010 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
2012 char fbuf[MAXPATHLEN];
2013 item_list *relname_list;
2014 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
2015 size_t j;
2017 f_name(file, fbuf);
2018 dlen = strlen(fbuf);
2020 if (!change_pathname(file, NULL, 0))
2021 exit_cleanup(RERR_FILESELECT);
2023 change_local_filter_dir(fbuf, dlen, send_dir_depth);
2025 if (file->flags & FLAG_CONTENT_DIR) {
2026 if (one_file_system) {
2027 STRUCT_STAT st;
2028 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
2029 interpret_stat_error(fbuf, True);
2030 return;
2032 filesystem_dev = st.st_dev;
2034 send_directory(f, flist, fbuf, dlen, flags);
2037 if (!relative_paths)
2038 return;
2040 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
2041 if (!relname_list)
2042 return;
2044 for (j = 0; j < relname_list->count; j++) {
2045 char *slash;
2046 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
2047 char name_type = rnp->name_type;
2049 fbuf[dlen] = '/';
2050 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
2051 free(rnp);
2052 if (len >= (int)sizeof fbuf)
2053 continue; /* Impossible... */
2055 slash = strchr(fbuf+dlen+1, '/');
2056 if (slash) {
2057 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
2058 continue;
2061 if (name_type != NORMAL_NAME) {
2062 STRUCT_STAT st;
2063 if (name_type == MISSING_NAME)
2064 memset(&st, 0, sizeof st);
2065 else if (link_stat(fbuf, &st, 1) != 0) {
2066 interpret_stat_error(fbuf, True);
2067 continue;
2069 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
2070 } else
2071 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
2074 free(relname_list);
2077 static void write_end_of_flist(int f, int send_io_error)
2079 if (xfer_flags_as_varint) {
2080 write_varint(f, 0);
2081 write_varint(f, send_io_error ? io_error : 0);
2082 } else if (send_io_error) {
2083 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2084 write_varint(f, io_error);
2085 } else
2086 write_byte(f, 0);
2089 void send_extra_file_list(int f, int at_least)
2091 struct file_list *flist;
2092 int64 start_write;
2093 uint16 prev_flags;
2094 int save_io_error = io_error;
2096 if (flist_eof)
2097 return;
2099 if (at_least < 0)
2100 at_least = file_total - file_old_total + 1;
2102 /* Keep sending data until we have the requested number of
2103 * files in the upcoming file-lists. */
2104 while (file_total - file_old_total < at_least) {
2105 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
2106 int dir_ndx, dstart = stats.num_dirs;
2107 const char *pathname = F_PATHNAME(file);
2108 int32 *dp;
2110 flist = flist_new(0, "send_extra_file_list");
2111 start_write = stats.total_written;
2113 if (unsort_ndx)
2114 dir_ndx = F_NDX(file);
2115 else
2116 dir_ndx = send_dir_ndx;
2117 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
2118 flist->parent_ndx = send_dir_ndx; /* the sending side must remember the sorted ndx value */
2120 send1extra(f, file, flist);
2121 prev_flags = file->flags;
2122 dp = F_DIR_NODE_P(file);
2124 /* If there are any duplicate directory names that follow, we
2125 * send all the dirs together in one file-list. The dir_flist
2126 * tree links all the child subdirs onto the last dup dir. */
2127 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
2128 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
2129 send_dir_ndx = dir_ndx;
2130 file = dir_flist->sorted[dir_ndx];
2131 /* Try to avoid some duplicate scanning of identical dirs. */
2132 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
2133 file->flags &= ~FLAG_CONTENT_DIR;
2134 send1extra(f, file, flist);
2135 prev_flags = file->flags;
2136 dp = F_DIR_NODE_P(file);
2139 if (io_error == save_io_error || ignore_errors)
2140 write_end_of_flist(f, 0);
2141 else if (use_safe_inc_flist)
2142 write_end_of_flist(f, 1);
2143 else {
2144 if (delete_during)
2145 fatal_unsafe_io_error();
2146 write_end_of_flist(f, 0);
2149 if (need_unsorted_flist) {
2150 flist->sorted = new_array(struct file_struct *, flist->used);
2151 memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2152 } else
2153 flist->sorted = flist->files;
2155 flist_sort_and_clean(flist, 0);
2157 add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart);
2158 flist_done_allocating(flist);
2160 file_total += flist->used;
2161 stats.flist_size += stats.total_written - start_write;
2162 stats.num_files += flist->used;
2163 if (DEBUG_GTE(FLIST, 3))
2164 output_flist(flist);
2166 if (DIR_FIRST_CHILD(dp) >= 0) {
2167 send_dir_ndx = DIR_FIRST_CHILD(dp);
2168 send_dir_depth++;
2169 } else {
2170 while (DIR_NEXT_SIBLING(dp) < 0) {
2171 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
2172 write_ndx(f, NDX_FLIST_EOF);
2173 flist_eof = 1;
2174 if (DEBUG_GTE(FLIST, 3))
2175 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2176 change_local_filter_dir(NULL, 0, 0);
2177 goto finish;
2179 send_dir_depth--;
2180 file = dir_flist->sorted[send_dir_ndx];
2181 dp = F_DIR_NODE_P(file);
2183 send_dir_ndx = DIR_NEXT_SIBLING(dp);
2187 finish:
2188 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2189 send_msg_int(MSG_IO_ERROR, io_error);
2192 struct file_list *send_file_list(int f, int argc, char *argv[])
2194 static const char *lastdir;
2195 static int lastdir_len = -1;
2196 int len, dirlen;
2197 STRUCT_STAT st;
2198 char *p, *dir;
2199 struct file_list *flist;
2200 struct timeval start_tv, end_tv;
2201 int64 start_write;
2202 int use_ff_fd = 0;
2203 int disable_buffering, reenable_multiplex = -1;
2204 int flags = recurse ? FLAG_CONTENT_DIR : 0;
2205 int reading_remotely = filesfrom_host != NULL;
2206 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2207 #ifdef ICONV_OPTION
2208 | (filesfrom_convert ? RL_CONVERT : 0)
2209 #endif
2210 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2211 int implied_dot_dir = 0;
2213 rprintf(FLOG, "building file list\n");
2214 if (show_filelist_progress)
2215 start_filelist_progress("building file list");
2216 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2217 rprintf(FCLIENT, "sending incremental file list\n");
2219 start_write = stats.total_written;
2220 gettimeofday(&start_tv, NULL);
2222 if (relative_paths && protocol_version >= 30)
2223 implied_dirs = 1; /* We send flagged implied dirs */
2225 #ifdef SUPPORT_HARD_LINKS
2226 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2227 init_hard_links();
2228 #endif
2230 flist = cur_flist = flist_new(0, "send_file_list");
2231 flist_expand(flist, FLIST_START_LARGE);
2232 if (inc_recurse) {
2233 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2234 flist_expand(dir_flist, FLIST_START_LARGE);
2235 flags |= FLAG_DIVERT_DIRS;
2236 } else
2237 dir_flist = cur_flist;
2239 disable_buffering = io_start_buffering_out(f);
2240 if (filesfrom_fd >= 0) {
2241 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2242 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2243 full_fname(argv[0]));
2244 exit_cleanup(RERR_FILESELECT);
2246 if (protocol_version < 31) {
2247 /* Older protocols send the files-from data w/o packaging
2248 * it in multiplexed I/O packets, so temporarily switch
2249 * to buffered I/O to match this behavior. */
2250 reenable_multiplex = io_end_multiplex_in(MPLX_TO_BUFFERED);
2252 use_ff_fd = 1;
2255 if (!orig_dir)
2256 orig_dir = strdup(curr_dir);
2258 while (1) {
2259 char fbuf[MAXPATHLEN], *fn, name_type;
2261 if (use_ff_fd) {
2262 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2263 break;
2264 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2265 } else {
2266 if (argc-- == 0)
2267 break;
2268 strlcpy(fbuf, *argv++, MAXPATHLEN);
2269 if (sanitize_paths)
2270 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2273 len = strlen(fbuf);
2274 if (relative_paths) {
2275 /* We clean up fbuf below. */
2276 name_type = NORMAL_NAME;
2277 } else if (!len || fbuf[len - 1] == '/') {
2278 if (len == 2 && fbuf[0] == '.') {
2279 /* Turn "./" into just "." rather than "./." */
2280 fbuf[--len] = '\0';
2281 } else {
2282 if (len + 1 >= MAXPATHLEN)
2283 overflow_exit("send_file_list");
2284 fbuf[len++] = '.';
2285 fbuf[len] = '\0';
2287 name_type = DOTDIR_NAME;
2288 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2289 && (len == 2 || fbuf[len-3] == '/')) {
2290 if (len + 2 >= MAXPATHLEN)
2291 overflow_exit("send_file_list");
2292 fbuf[len++] = '/';
2293 fbuf[len++] = '.';
2294 fbuf[len] = '\0';
2295 name_type = DOTDIR_NAME;
2296 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2297 name_type = DOTDIR_NAME;
2298 else
2299 name_type = NORMAL_NAME;
2301 dir = NULL;
2303 if (!relative_paths) {
2304 p = strrchr(fbuf, '/');
2305 if (p) {
2306 *p = '\0';
2307 if (p == fbuf)
2308 dir = "/";
2309 else
2310 dir = fbuf;
2311 len -= p - fbuf + 1;
2312 fn = p + 1;
2313 } else
2314 fn = fbuf;
2315 } else {
2316 if ((p = strstr(fbuf, "/./")) != NULL) {
2317 *p = '\0';
2318 if (p == fbuf)
2319 dir = "/";
2320 else {
2321 dir = fbuf;
2322 clean_fname(dir, 0);
2324 fn = p + 3;
2325 while (*fn == '/')
2326 fn++;
2327 if (!*fn)
2328 *--fn = '\0'; /* ensure room for '.' */
2329 } else
2330 fn = fbuf;
2331 /* A leading ./ can be used in relative mode to affect
2332 * the dest dir without its name being in the path. */
2333 if (*fn == '.' && fn[1] == '/' && fn[2] && !implied_dot_dir)
2334 implied_dot_dir = -1;
2335 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2336 | CFN_DROP_TRAILING_DOT_DIR);
2337 if (len == 1) {
2338 if (fn[0] == '/') {
2339 fn = "/.";
2340 len = 2;
2341 name_type = DOTDIR_NAME;
2342 } else if (fn[0] == '.')
2343 name_type = DOTDIR_NAME;
2344 } else if (fn[len-1] == '/') {
2345 fn[--len] = '\0';
2346 if (len == 1 && *fn == '.')
2347 name_type = DOTDIR_NAME;
2348 else
2349 name_type = SLASH_ENDING_NAME;
2351 /* Reject a ".." dir in the active part of the path. */
2352 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2353 if ((p[2] == '/' || p[2] == '\0')
2354 && (p == fn || p[-1] == '/')) {
2355 rprintf(FERROR,
2356 "found \"..\" dir in relative path: %s\n",
2357 fn);
2358 exit_cleanup(RERR_SYNTAX);
2363 if (!*fn) {
2364 len = 1;
2365 fn = ".";
2366 name_type = DOTDIR_NAME;
2369 dirlen = dir ? strlen(dir) : 0;
2370 if (dirlen != lastdir_len || (dirlen && memcmp(lastdir, dir, dirlen) != 0)) {
2371 if (!change_pathname(NULL, dir, -dirlen))
2372 goto bad_path;
2373 lastdir = pathname;
2374 lastdir_len = pathname_len;
2375 } else if (!change_pathname(NULL, lastdir, lastdir_len)) {
2376 bad_path:
2377 if (implied_dot_dir < 0)
2378 implied_dot_dir = 0;
2379 continue;
2382 if (implied_dot_dir < 0) {
2383 implied_dot_dir = 1;
2384 send_file_name(f, flist, ".", NULL, (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR, ALL_FILTERS);
2387 if (fn != fbuf)
2388 memmove(fbuf, fn, len + 1);
2390 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2391 || (name_type != DOTDIR_NAME && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, SERVER_FILTERS))
2392 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2393 if (errno != ENOENT || missing_args == 0) {
2394 /* This is a transfer error, but inhibit deletion
2395 * only if we might be omitting an existing file. */
2396 if (errno != ENOENT)
2397 io_error |= IOERR_GENERAL;
2398 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2399 full_fname(fbuf));
2400 continue;
2401 } else if (missing_args == 1) {
2402 /* Just ignore the arg. */
2403 continue;
2404 } else /* (missing_args == 2) */ {
2405 /* Send the arg as a "missing" entry with
2406 * mode 0, which tells the generator to delete it. */
2407 memset(&st, 0, sizeof st);
2411 /* A dot-dir should not be excluded! */
2412 if (name_type != DOTDIR_NAME && st.st_mode != 0
2413 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2414 continue;
2416 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2417 rprintf(FINFO, "skipping directory %s\n", fbuf);
2418 continue;
2421 if (inc_recurse && relative_paths && *fbuf) {
2422 if ((p = strchr(fbuf+1, '/')) != NULL) {
2423 if (p - fbuf == 1 && *fbuf == '.') {
2424 if ((fn = strchr(p+1, '/')) != NULL)
2425 p = fn;
2426 } else
2427 fn = p;
2428 send_implied_dirs(f, flist, fbuf, fbuf, p, flags,
2429 IS_MISSING_FILE(st) ? MISSING_NAME : name_type);
2430 if (fn == p)
2431 continue;
2433 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2434 /* Send the implied directories at the start of the
2435 * source spec, so we get their permissions right. */
2436 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2439 if (one_file_system)
2440 filesystem_dev = st.st_dev;
2442 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2443 struct file_struct *file;
2444 file = send_file_name(f, flist, fbuf, &st,
2445 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2446 NO_FILTERS);
2447 if (!file)
2448 continue;
2449 if (inc_recurse) {
2450 if (name_type == DOTDIR_NAME) {
2451 if (send_dir_depth < 0) {
2452 send_dir_depth = 0;
2453 change_local_filter_dir(fbuf, len, send_dir_depth);
2455 send_directory(f, flist, fbuf, len, flags);
2457 } else
2458 send_if_directory(f, flist, file, fbuf, len, flags);
2459 } else
2460 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2463 if (reenable_multiplex >= 0)
2464 io_start_multiplex_in(reenable_multiplex);
2466 gettimeofday(&end_tv, NULL);
2467 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2468 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2469 if (stats.flist_buildtime == 0)
2470 stats.flist_buildtime = 1;
2471 start_tv = end_tv;
2473 /* Indicate end of file list */
2474 if (io_error == 0 || ignore_errors)
2475 write_end_of_flist(f, 0);
2476 else if (use_safe_inc_flist)
2477 write_end_of_flist(f, 1);
2478 else {
2479 if (delete_during && inc_recurse)
2480 fatal_unsafe_io_error();
2481 write_end_of_flist(f, 0);
2484 #ifdef SUPPORT_HARD_LINKS
2485 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2486 idev_destroy();
2487 #endif
2489 if (show_filelist_progress)
2490 finish_filelist_progress(flist);
2492 gettimeofday(&end_tv, NULL);
2493 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2494 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2496 /* When converting names, both sides keep an unsorted file-list array
2497 * because the names will differ on the sending and receiving sides
2498 * (both sides will use the unsorted index number for each item). */
2500 /* Sort the list without removing any duplicates. This allows the
2501 * receiving side to ask for whatever name it kept. For incremental
2502 * recursion mode, the sender marks duplicate dirs so that it can
2503 * send them together in a single file-list. */
2504 if (need_unsorted_flist) {
2505 flist->sorted = new_array(struct file_struct *, flist->used);
2506 memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2507 } else
2508 flist->sorted = flist->files;
2509 flist_sort_and_clean(flist, 0);
2510 file_total += flist->used;
2511 file_old_total += flist->used;
2513 if (numeric_ids <= 0 && !inc_recurse)
2514 send_id_lists(f);
2516 /* send the io_error flag */
2517 if (protocol_version < 30)
2518 write_int(f, ignore_errors ? 0 : io_error);
2519 else if (!use_safe_inc_flist && io_error && !ignore_errors)
2520 send_msg_int(MSG_IO_ERROR, io_error);
2522 if (disable_buffering)
2523 io_end_buffering_out(IOBUF_FREE_BUFS);
2525 stats.flist_size = stats.total_written - start_write;
2526 stats.num_files = flist->used;
2528 if (DEBUG_GTE(FLIST, 3))
2529 output_flist(flist);
2531 if (DEBUG_GTE(FLIST, 2))
2532 rprintf(FINFO, "send_file_list done\n");
2534 if (inc_recurse) {
2535 send_dir_depth = 1;
2536 add_dirs_to_tree(-1, flist, stats.num_dirs);
2537 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2538 flist->parent_ndx = -1;
2539 flist_done_allocating(flist);
2540 if (send_dir_ndx < 0) {
2541 write_ndx(f, NDX_FLIST_EOF);
2542 flist_eof = 1;
2543 if (DEBUG_GTE(FLIST, 3))
2544 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2546 else if (file_total == 1) {
2547 /* If we're creating incremental file-lists and there
2548 * was just 1 item in the first file-list, send 1 more
2549 * file-list to check if this is a 1-file xfer. */
2550 send_extra_file_list(f, 1);
2552 } else {
2553 flist_eof = 1;
2554 if (DEBUG_GTE(FLIST, 3))
2555 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2558 return flist;
2561 struct file_list *recv_file_list(int f, int dir_ndx)
2563 const char *good_dirname = NULL;
2564 struct file_list *flist;
2565 int dstart, flags;
2566 int64 start_read;
2568 if (!first_flist) {
2569 if (show_filelist_progress)
2570 start_filelist_progress("receiving file list");
2571 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2572 rprintf(FCLIENT, "receiving incremental file list\n");
2573 rprintf(FLOG, "receiving file list\n");
2574 if (usermap)
2575 parse_name_map(usermap, True);
2576 if (groupmap)
2577 parse_name_map(groupmap, False);
2580 start_read = stats.total_read;
2582 #ifdef SUPPORT_HARD_LINKS
2583 if (preserve_hard_links && !first_flist)
2584 init_hard_links();
2585 #endif
2587 flist = flist_new(0, "recv_file_list");
2588 flist_expand(flist, FLIST_START_LARGE);
2590 if (inc_recurse) {
2591 if (flist->ndx_start == 1) {
2592 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2593 flist_expand(dir_flist, FLIST_START_LARGE);
2595 dstart = dir_flist->used;
2596 } else {
2597 dir_flist = flist;
2598 dstart = 0;
2601 while (1) {
2602 struct file_struct *file;
2604 if (xfer_flags_as_varint) {
2605 if ((flags = read_varint(f)) == 0) {
2606 int err = read_varint(f);
2607 if (!ignore_errors)
2608 io_error |= err;
2609 break;
2611 } else {
2612 if ((flags = read_byte(f)) == 0)
2613 break;
2615 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2616 flags |= read_byte(f) << 8;
2618 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2619 int err;
2620 if (!use_safe_inc_flist) {
2621 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2622 exit_cleanup(RERR_PROTOCOL);
2624 err = read_varint(f);
2625 if (!ignore_errors)
2626 io_error |= err;
2627 break;
2631 flist_expand(flist, 1);
2632 file = recv_file_entry(f, flist, flags);
2634 if (inc_recurse) {
2635 static const char empty_dir[] = "\0";
2636 const char *cur_dir = file->dirname ? file->dirname : empty_dir;
2637 if (relative_paths && *cur_dir == '/')
2638 cur_dir++;
2639 if (cur_dir != good_dirname) {
2640 const char *d = dir_ndx >= 0 ? f_name(dir_flist->files[dir_ndx], NULL) : empty_dir;
2641 if (strcmp(cur_dir, d) != 0) {
2642 rprintf(FERROR,
2643 "ABORTING due to invalid path from sender: %s/%s\n",
2644 cur_dir, file->basename);
2645 exit_cleanup(RERR_UNSUPPORTED);
2647 good_dirname = cur_dir;
2651 if (S_ISREG(file->mode)) {
2652 /* Already counted */
2653 } else if (S_ISDIR(file->mode)) {
2654 if (inc_recurse) {
2655 flist_expand(dir_flist, 1);
2656 dir_flist->files[dir_flist->used++] = file;
2658 stats.num_dirs++;
2659 } else if (S_ISLNK(file->mode))
2660 stats.num_symlinks++;
2661 else if (IS_DEVICE(file->mode))
2662 stats.num_devices++;
2663 else
2664 stats.num_specials++;
2666 flist->files[flist->used++] = file;
2668 maybe_emit_filelist_progress(flist->used);
2670 if (DEBUG_GTE(FLIST, 2)) {
2671 char *name = f_name(file, NULL);
2672 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2675 file_total += flist->used;
2677 if (DEBUG_GTE(FLIST, 2))
2678 rprintf(FINFO, "received %d names\n", flist->used);
2680 if (show_filelist_progress)
2681 finish_filelist_progress(flist);
2683 if (need_unsorted_flist) {
2684 /* Create an extra array of index pointers that we can sort for
2685 * the generator's use (for wading through the files in sorted
2686 * order and for calling flist_find()). We keep the "files"
2687 * list unsorted for our exchange of index numbers with the
2688 * other side (since their names may not sort the same). */
2689 flist->sorted = new_array(struct file_struct *, flist->used);
2690 memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2691 if (inc_recurse && dir_flist->used > dstart) {
2692 static int dir_flist_malloced = 0;
2693 if (dir_flist_malloced < dir_flist->malloced) {
2694 dir_flist->sorted = realloc_array(dir_flist->sorted,
2695 struct file_struct *,
2696 dir_flist->malloced);
2697 dir_flist_malloced = dir_flist->malloced;
2699 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2700 (dir_flist->used - dstart) * PTR_SIZE);
2701 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2703 } else {
2704 flist->sorted = flist->files;
2705 if (inc_recurse && dir_flist->used > dstart) {
2706 dir_flist->sorted = dir_flist->files;
2707 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2711 if (inc_recurse)
2712 flist_done_allocating(flist);
2713 else if (f >= 0) {
2714 recv_id_list(f, flist);
2715 flist_eof = 1;
2716 if (DEBUG_GTE(FLIST, 3))
2717 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2720 /* The --relative option sends paths with a leading slash, so we need
2721 * to specify the strip_root option here. We rejected leading slashes
2722 * for a non-relative transfer in recv_file_entry(). */
2723 flist_sort_and_clean(flist, relative_paths);
2725 if (protocol_version < 30) {
2726 /* Recv the io_error flag */
2727 int err = read_int(f);
2728 if (!ignore_errors)
2729 io_error |= err;
2730 } else if (inc_recurse && flist->ndx_start == 1) {
2731 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2732 flist->parent_ndx = -1;
2735 if (DEBUG_GTE(FLIST, 3))
2736 output_flist(flist);
2738 if (DEBUG_GTE(FLIST, 2))
2739 rprintf(FINFO, "recv_file_list done\n");
2741 stats.flist_size += stats.total_read - start_read;
2742 stats.num_files += flist->used;
2744 return flist;
2747 /* This is only used once by the receiver if the very first file-list
2748 * has exactly one item in it. */
2749 void recv_additional_file_list(int f)
2751 struct file_list *flist;
2752 int ndx = read_ndx(f);
2753 if (ndx == NDX_FLIST_EOF) {
2754 flist_eof = 1;
2755 if (DEBUG_GTE(FLIST, 3))
2756 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2757 change_local_filter_dir(NULL, 0, 0);
2758 } else {
2759 ndx = NDX_FLIST_OFFSET - ndx;
2760 if (ndx < 0 || ndx >= dir_flist->used) {
2761 ndx = NDX_FLIST_OFFSET - ndx;
2762 rprintf(FERROR,
2763 "[%s] Invalid dir index: %d (%d - %d)\n",
2764 who_am_i(), ndx, NDX_FLIST_OFFSET,
2765 NDX_FLIST_OFFSET - dir_flist->used + 1);
2766 exit_cleanup(RERR_PROTOCOL);
2768 if (DEBUG_GTE(FLIST, 3)) {
2769 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2770 who_am_i(), ndx);
2772 flist = recv_file_list(f, ndx);
2773 flist->parent_ndx = ndx;
2777 /* Search for an identically-named item in the file list. Note that the
2778 * items must agree in their directory-ness, or no match is returned. */
2779 int flist_find(struct file_list *flist, struct file_struct *f)
2781 int low = flist->low, high = flist->high;
2782 int diff, mid, mid_up;
2784 while (low <= high) {
2785 mid = (low + high) / 2;
2786 if (F_IS_ACTIVE(flist->sorted[mid]))
2787 mid_up = mid;
2788 else {
2789 /* Scan for the next non-empty entry using the cached
2790 * distance values. If the value isn't fully up-to-
2791 * date, update it. */
2792 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2793 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2794 do {
2795 mid_up += F_DEPTH(flist->sorted[mid_up]);
2796 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2797 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2799 if (mid_up > high) {
2800 /* If there's nothing left above us, set high to
2801 * a non-empty entry below us and continue. */
2802 high = mid - (int)flist->sorted[mid]->len32;
2803 if (!F_IS_ACTIVE(flist->sorted[high])) {
2804 do {
2805 high -= (int)flist->sorted[high]->len32;
2806 } while (!F_IS_ACTIVE(flist->sorted[high]));
2807 flist->sorted[mid]->len32 = mid - high;
2809 continue;
2812 diff = f_name_cmp(flist->sorted[mid_up], f);
2813 if (diff == 0) {
2814 if (protocol_version < 29
2815 && S_ISDIR(flist->sorted[mid_up]->mode)
2816 != S_ISDIR(f->mode))
2817 return -1;
2818 return mid_up;
2820 if (diff < 0)
2821 low = mid_up + 1;
2822 else
2823 high = mid - 1;
2825 return -1;
2828 /* Search for a name in the file list. You must specify want_dir_match as:
2829 * 1=match directories, 0=match non-directories, or -1=match either. */
2830 int flist_find_name(struct file_list *flist, const char *fname, int want_dir_match)
2832 static struct file_struct *f;
2833 char fbuf[MAXPATHLEN];
2834 const char *slash = strrchr(fname, '/');
2835 const char *basename = slash ? slash+1 : fname;
2837 if (!f)
2838 f = (struct file_struct*)new_array(char, FILE_STRUCT_LEN + MAXPATHLEN + 1);
2840 memset(f, 0, FILE_STRUCT_LEN);
2841 memcpy((void*)f->basename, basename, strlen(basename)+1);
2843 if (slash) {
2844 strlcpy(fbuf, fname, slash - fname + 1);
2845 f->dirname = fbuf;
2846 } else
2847 f->dirname = NULL;
2849 f->mode = want_dir_match > 0 ? S_IFDIR : S_IFREG;
2851 if (want_dir_match < 0)
2852 return flist_find_ignore_dirness(flist, f);
2853 return flist_find(flist, f);
2856 /* Search for an identically-named item in the file list. Differs from
2857 * flist_find in that an item that agrees with "f" in directory-ness is
2858 * preferred but one that does not is still found. */
2859 int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2861 mode_t save_mode;
2862 int ndx;
2864 /* First look for an item that agrees in directory-ness. */
2865 ndx = flist_find(flist, f);
2866 if (ndx >= 0)
2867 return ndx;
2869 /* Temporarily flip f->mode to look for an item of opposite
2870 * directory-ness. */
2871 save_mode = f->mode;
2872 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2873 ndx = flist_find(flist, f);
2874 f->mode = save_mode;
2875 return ndx;
2879 * Free up any resources a file_struct has allocated
2880 * and clear the file.
2882 void clear_file(struct file_struct *file)
2884 /* The +1 zeros out the first char of the basename. */
2885 memset(file, 0, FILE_STRUCT_LEN + 1);
2886 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2887 * entry. Likewise for len32 in the opposite direction. We assume
2888 * that we're alone for now since flist_find() will adjust the counts
2889 * it runs into that aren't up-to-date. */
2890 file->len32 = F_DEPTH(file) = 1;
2893 /* Allocate a new file list. */
2894 static struct file_list *flist_new(int flags, const char *msg)
2896 struct file_list *flist;
2898 flist = new0(struct file_list);
2900 if (flags & FLIST_TEMP) {
2901 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
2902 out_of_memory(msg);
2903 } else {
2904 /* This is a doubly linked list with prev looping back to
2905 * the end of the list, but the last next pointer is NULL. */
2906 if (!first_flist) {
2907 if (!(flist->file_pool = pool_create(NORMAL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
2908 out_of_memory(msg);
2910 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2912 first_flist = cur_flist = flist->prev = flist;
2913 } else {
2914 struct file_list *prev = first_flist->prev;
2916 flist->file_pool = first_flist->file_pool;
2918 flist->ndx_start = prev->ndx_start + prev->used + 1;
2919 flist->flist_num = prev->flist_num + 1;
2921 flist->prev = prev;
2922 prev->next = first_flist->prev = flist;
2924 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2925 flist_cnt++;
2928 return flist;
2931 /* Free up all elements in a flist. */
2932 void flist_free(struct file_list *flist)
2934 if (!flist->prev) {
2935 /* Was FLIST_TEMP dir-list. */
2936 } else if (flist == flist->prev) {
2937 first_flist = cur_flist = NULL;
2938 file_total = 0;
2939 flist_cnt = 0;
2940 } else {
2941 if (flist == cur_flist)
2942 cur_flist = flist->next;
2943 if (flist == first_flist)
2944 first_flist = first_flist->next;
2945 else {
2946 flist->prev->next = flist->next;
2947 if (!flist->next)
2948 flist->next = first_flist;
2950 flist->next->prev = flist->prev;
2951 file_total -= flist->used;
2952 flist_cnt--;
2955 if (!flist->prev || !flist_cnt)
2956 pool_destroy(flist->file_pool);
2957 else
2958 pool_free_old(flist->file_pool, flist->pool_boundary);
2960 if (flist->sorted && flist->sorted != flist->files)
2961 free(flist->sorted);
2962 free(flist->files);
2963 free(flist);
2966 /* This routine ensures we don't have any duplicate names in our file list.
2967 * duplicate names can cause corruption because of the pipelining. */
2968 static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2970 char fbuf[MAXPATHLEN];
2971 int i, prev_i;
2973 if (!flist)
2974 return;
2975 if (flist->used == 0) {
2976 flist->high = -1;
2977 flist->low = 0;
2978 return;
2981 fsort(flist->sorted, flist->used);
2983 if (!am_sender || inc_recurse) {
2984 for (i = prev_i = 0; i < flist->used; i++) {
2985 if (F_IS_ACTIVE(flist->sorted[i])) {
2986 prev_i = i;
2987 break;
2990 flist->low = prev_i;
2991 } else {
2992 i = prev_i = flist->used - 1;
2993 flist->low = 0;
2996 while (++i < flist->used) {
2997 int j;
2998 struct file_struct *file = flist->sorted[i];
3000 if (!F_IS_ACTIVE(file))
3001 continue;
3002 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
3003 j = prev_i;
3004 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
3005 int save_mode = file->mode;
3006 /* Make sure that this directory doesn't duplicate a
3007 * non-directory earlier in the list. */
3008 flist->high = prev_i;
3009 file->mode = S_IFREG;
3010 j = flist_find(flist, file);
3011 file->mode = save_mode;
3012 } else
3013 j = -1;
3014 if (j >= 0) {
3015 int keep, drop;
3016 /* If one is a dir and the other is not, we want to
3017 * keep the dir because it might have contents in the
3018 * list. Otherwise keep the first one. */
3019 if (S_ISDIR(file->mode)) {
3020 struct file_struct *fp = flist->sorted[j];
3021 if (!S_ISDIR(fp->mode))
3022 keep = i, drop = j;
3023 else {
3024 if (am_sender)
3025 file->flags |= FLAG_DUPLICATE;
3026 else { /* Make sure we merge our vital flags. */
3027 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
3028 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
3030 keep = j, drop = i;
3032 } else
3033 keep = j, drop = i;
3035 if (!am_sender) {
3036 if (DEBUG_GTE(DUP, 1)) {
3037 rprintf(FINFO,
3038 "removing duplicate name %s from file list (%d)\n",
3039 f_name(file, fbuf), drop + flist->ndx_start);
3041 clear_file(flist->sorted[drop]);
3044 if (keep == i) {
3045 if (flist->low == drop) {
3046 for (j = drop + 1;
3047 j < i && !F_IS_ACTIVE(flist->sorted[j]);
3048 j++) {}
3049 flist->low = j;
3051 prev_i = i;
3053 } else
3054 prev_i = i;
3056 flist->high = prev_i;
3058 if (strip_root) {
3059 /* We need to strip off the leading slashes for relative
3060 * paths, but this must be done _after_ the sorting phase. */
3061 for (i = flist->low; i <= flist->high; i++) {
3062 struct file_struct *file = flist->sorted[i];
3064 if (!file->dirname)
3065 continue;
3066 while (*file->dirname == '/')
3067 file->dirname++;
3068 if (!*file->dirname)
3069 file->dirname = NULL;
3073 if (prune_empty_dirs && !am_sender) {
3074 int j, prev_depth = 0;
3076 prev_i = 0; /* It's OK that this isn't really true. */
3078 for (i = flist->low; i <= flist->high; i++) {
3079 struct file_struct *fp, *file = flist->sorted[i];
3081 /* This temporarily abuses the F_DEPTH() value for a
3082 * directory that is in a chain that might get pruned.
3083 * We restore the old value if it gets a reprieve. */
3084 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
3085 /* Dump empty dirs when coming back down. */
3086 for (j = prev_depth; j >= F_DEPTH(file); j--) {
3087 fp = flist->sorted[prev_i];
3088 if (F_DEPTH(fp) >= 0)
3089 break;
3090 prev_i = -F_DEPTH(fp)-1;
3091 clear_file(fp);
3093 prev_depth = F_DEPTH(file);
3094 if (is_excluded(f_name(file, fbuf), 1, ALL_FILTERS)) {
3095 /* Keep dirs through this dir. */
3096 for (j = prev_depth-1; ; j--) {
3097 fp = flist->sorted[prev_i];
3098 if (F_DEPTH(fp) >= 0)
3099 break;
3100 prev_i = -F_DEPTH(fp)-1;
3101 F_DEPTH(fp) = j;
3103 } else
3104 F_DEPTH(file) = -prev_i-1;
3105 prev_i = i;
3106 } else {
3107 /* Keep dirs through this non-dir. */
3108 for (j = prev_depth; ; j--) {
3109 fp = flist->sorted[prev_i];
3110 if (F_DEPTH(fp) >= 0)
3111 break;
3112 prev_i = -F_DEPTH(fp)-1;
3113 F_DEPTH(fp) = j;
3117 /* Dump all remaining empty dirs. */
3118 while (1) {
3119 struct file_struct *fp = flist->sorted[prev_i];
3120 if (F_DEPTH(fp) >= 0)
3121 break;
3122 prev_i = -F_DEPTH(fp)-1;
3123 clear_file(fp);
3126 for (i = flist->low; i <= flist->high; i++) {
3127 if (F_IS_ACTIVE(flist->sorted[i]))
3128 break;
3130 flist->low = i;
3131 for (i = flist->high; i >= flist->low; i--) {
3132 if (F_IS_ACTIVE(flist->sorted[i]))
3133 break;
3135 flist->high = i;
3139 static void output_flist(struct file_list *flist)
3141 char uidbuf[16], gidbuf[16], depthbuf[16];
3142 struct file_struct *file;
3143 const char *root, *dir, *slash, *name, *trail;
3144 const char *who = who_am_i();
3145 int i;
3147 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
3148 who, flist->ndx_start, flist->used, flist->low, flist->high);
3149 for (i = 0; i < flist->used; i++) {
3150 file = flist->files[i];
3151 if ((am_root || am_sender) && uid_ndx) {
3152 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
3153 F_OWNER(file));
3154 } else
3155 *uidbuf = '\0';
3156 if (gid_ndx) {
3157 static char parens[] = "(\0)\0\0\0";
3158 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
3159 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
3160 pp, F_GROUP(file), pp + 2);
3161 } else
3162 *gidbuf = '\0';
3163 if (!am_sender)
3164 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
3165 if (F_IS_ACTIVE(file)) {
3166 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
3167 if ((dir = file->dirname) == NULL)
3168 dir = slash = "";
3169 else
3170 slash = "/";
3171 name = file->basename;
3172 trail = S_ISDIR(file->mode) ? "/" : "";
3173 } else
3174 root = dir = slash = name = trail = "";
3175 rprintf(FINFO,
3176 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
3177 who, i + flist->ndx_start,
3178 root, dir, slash, name, trail,
3179 (int)file->mode, comma_num(F_LENGTH(file)),
3180 uidbuf, gidbuf, file->flags);
3184 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
3185 enum fnc_type { t_PATH, t_ITEM };
3187 static int found_prefix;
3189 /* Compare the names of two file_struct entities, similar to how strcmp()
3190 * would do if it were operating on the joined strings.
3192 * Some differences beginning with protocol_version 29: (1) directory names
3193 * are compared with an assumed trailing slash so that they compare in a
3194 * way that would cause them to sort immediately prior to any content they
3195 * may have; (2) a directory of any name compares after a non-directory of
3196 * any name at the same depth; (3) a directory with name "." compares prior
3197 * to anything else. These changes mean that a directory and a non-dir
3198 * with the same name will not compare as equal (protocol_version >= 29).
3200 * The dirname component can be an empty string, but the basename component
3201 * cannot (and never is in the current codebase). The basename component
3202 * may be NULL (for a removed item), in which case it is considered to be
3203 * after any existing item. */
3204 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
3206 int dif;
3207 const uchar *c1, *c2;
3208 enum fnc_state state1, state2;
3209 enum fnc_type type1, type2;
3210 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
3212 if (!f1 || !F_IS_ACTIVE(f1)) {
3213 if (!f2 || !F_IS_ACTIVE(f2))
3214 return 0;
3215 return -1;
3217 if (!f2 || !F_IS_ACTIVE(f2))
3218 return 1;
3220 c1 = (uchar*)f1->dirname;
3221 c2 = (uchar*)f2->dirname;
3222 if (c1 == c2)
3223 c1 = c2 = NULL;
3224 if (!c1) {
3225 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3226 c1 = (const uchar*)f1->basename;
3227 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3228 type1 = t_ITEM;
3229 state1 = s_TRAILING;
3230 c1 = (uchar*)"";
3231 } else
3232 state1 = s_BASE;
3233 } else {
3234 type1 = t_path;
3235 state1 = s_DIR;
3237 if (!c2) {
3238 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3239 c2 = (const uchar*)f2->basename;
3240 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3241 type2 = t_ITEM;
3242 state2 = s_TRAILING;
3243 c2 = (uchar*)"";
3244 } else
3245 state2 = s_BASE;
3246 } else {
3247 type2 = t_path;
3248 state2 = s_DIR;
3251 if (type1 != type2)
3252 return type1 == t_PATH ? 1 : -1;
3254 do {
3255 if (!*c1) {
3256 switch (state1) {
3257 case s_DIR:
3258 state1 = s_SLASH;
3259 c1 = (uchar*)"/";
3260 break;
3261 case s_SLASH:
3262 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3263 c1 = (const uchar*)f1->basename;
3264 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3265 type1 = t_ITEM;
3266 state1 = s_TRAILING;
3267 c1 = (uchar*)"";
3268 } else
3269 state1 = s_BASE;
3270 break;
3271 case s_BASE:
3272 state1 = s_TRAILING;
3273 if (type1 == t_PATH) {
3274 c1 = (uchar*)"/";
3275 break;
3277 /* FALL THROUGH */
3278 case s_TRAILING:
3279 type1 = t_ITEM;
3280 break;
3282 if (*c2 && type1 != type2)
3283 return type1 == t_PATH ? 1 : -1;
3285 if (!*c2) {
3286 switch (state2) {
3287 case s_DIR:
3288 state2 = s_SLASH;
3289 c2 = (uchar*)"/";
3290 break;
3291 case s_SLASH:
3292 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3293 c2 = (const uchar*)f2->basename;
3294 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3295 type2 = t_ITEM;
3296 state2 = s_TRAILING;
3297 c2 = (uchar*)"";
3298 } else
3299 state2 = s_BASE;
3300 break;
3301 case s_BASE:
3302 state2 = s_TRAILING;
3303 if (type2 == t_PATH) {
3304 c2 = (uchar*)"/";
3305 break;
3307 /* FALL THROUGH */
3308 case s_TRAILING:
3309 found_prefix = 1;
3310 if (!*c1)
3311 return 0;
3312 type2 = t_ITEM;
3313 break;
3315 if (type1 != type2)
3316 return type1 == t_PATH ? 1 : -1;
3318 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3320 return dif;
3323 /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3324 * not match if f2's basename is not an exact match of a path element in f1.
3325 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3326 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3328 found_prefix = 0;
3329 f_name_cmp(f1, f2);
3330 return found_prefix;
3333 char *f_name_buf(void)
3335 static char names[5][MAXPATHLEN];
3336 static unsigned int n;
3338 n = (n + 1) % (sizeof names / sizeof names[0]);
3340 return names[n];
3343 /* Return a copy of the full filename of a flist entry, using the indicated
3344 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3345 * done because we checked the size when creating the file_struct entry.
3347 char *f_name(const struct file_struct *f, char *fbuf)
3349 if (!f || !F_IS_ACTIVE(f))
3350 return NULL;
3352 if (!fbuf)
3353 fbuf = f_name_buf();
3355 if (f->dirname) {
3356 int len = strlen(f->dirname);
3357 memcpy(fbuf, f->dirname, len);
3358 fbuf[len] = '/';
3359 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3360 } else
3361 strlcpy(fbuf, f->basename, MAXPATHLEN);
3363 return fbuf;
3366 /* Do a non-recursive scan of the named directory, possibly ignoring all
3367 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3368 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3369 * buffer (the functions we call will append names onto the end, but the old
3370 * dir value will be restored on exit). */
3371 struct file_list *get_dirlist(char *dirname, int dlen, int flags)
3373 struct file_list *dirlist;
3374 char dirbuf[MAXPATHLEN];
3375 int save_recurse = recurse;
3376 int save_xfer_dirs = xfer_dirs;
3377 int save_prune_empty_dirs = prune_empty_dirs;
3378 int senddir_fd = flags & GDL_IGNORE_FILTER_RULES ? -2 : -1;
3379 int senddir_flags = FLAG_CONTENT_DIR;
3381 if (dlen < 0) {
3382 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3383 if (dlen >= MAXPATHLEN)
3384 return NULL;
3385 dirname = dirbuf;
3388 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3390 if (flags & GDL_PERHAPS_DIR)
3391 senddir_flags |= FLAG_PERHAPS_DIR;
3393 recurse = 0;
3394 xfer_dirs = 1;
3395 send_directory(senddir_fd, dirlist, dirname, dlen, senddir_flags);
3396 xfer_dirs = save_xfer_dirs;
3397 recurse = save_recurse;
3398 if (INFO_GTE(PROGRESS, 1))
3399 flist_count_offset += dirlist->used;
3401 prune_empty_dirs = 0;
3402 dirlist->sorted = dirlist->files;
3403 flist_sort_and_clean(dirlist, 0);
3404 prune_empty_dirs = save_prune_empty_dirs;
3406 if (DEBUG_GTE(FLIST, 3))
3407 output_flist(dirlist);
3409 return dirlist;