Don't send a bogus "-" option to an older server if there were
[rsync/rsync.git] / flist.c
CommitLineData
dbda5fbf 1/*
0f78b815
WD
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
d3d07a5e 7 * Copyright (C) 2002-2008 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
736a6a29 13 *
0f78b815
WD
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.
172875cf 18 *
e7c67065 19 * You should have received a copy of the GNU General Public License along
4fd842f9 20 * with this program; if not, visit the http://fsf.org website.
0f78b815 21 */
c627d613
AT
22
23#include "rsync.h"
1b42f628 24#include "ifuncs.h"
a3e18c76 25#include "rounding.h"
5dd14f0c 26#include "inums.h"
6d4e718f 27#include "io.h"
c627d613 28
f05f993e 29extern int am_root;
c627d613 30extern int am_server;
a8726d2a 31extern int am_daemon;
56194bcd 32extern int am_sender;
f14b3ef4 33extern int am_generator;
3ea6e0e7 34extern int inc_recurse;
c627d613 35extern int always_checksum;
983b1ed3
WD
36extern int module_id;
37extern int ignore_errors;
4836c3ee 38extern int numeric_ids;
a06d19e3 39extern int recurse;
a572e126 40extern int use_qsort;
7e037c42 41extern int xfer_dirs;
24d0fcde 42extern int filesfrom_fd;
c627d613 43extern int one_file_system;
88c2190a 44extern int copy_dirlinks;
314f4591 45extern int keep_dirlinks;
236adddc
WD
46extern int preserve_uid;
47extern int preserve_gid;
1c3344a1 48extern int preserve_acls;
16edf865 49extern int preserve_xattrs;
c627d613 50extern int preserve_links;
dc5ddbcc 51extern int preserve_hard_links;
c627d613 52extern int preserve_devices;
b5c6a6ae 53extern int preserve_specials;
9b25ef35
WD
54extern int uid_ndx;
55extern int gid_ndx;
95a44066 56extern int eol_nulls;
6574b4f7 57extern int relative_paths;
24d0fcde 58extern int implied_dirs;
99a957d3 59extern int file_extra_cnt;
53039410
WD
60extern int ignore_perishable;
61extern int non_perishable_cnt;
85aecef6 62extern int prune_empty_dirs;
82306bf6 63extern int copy_links;
b5313607 64extern int copy_unsafe_links;
d04e9c51 65extern int protocol_version;
cb13abfe 66extern int sanitize_paths;
9585b276 67extern int munge_symlinks;
f7a76b9c 68extern int need_unsorted_flist;
91fd15b8 69extern int sender_symlink_iconv;
951e826b 70extern int output_needs_newline;
886df221 71extern int sender_keeps_checksum;
f7a76b9c 72extern int unsort_ndx;
d64e6f42 73extern struct stats stats;
95a44066 74extern char *filesfrom_host;
c627d613 75
e1f40891
WD
76extern char curr_dir[MAXPATHLEN];
77
2b7e0f33
WD
78extern struct chmod_mode_struct *chmod_modes;
79
7842418b 80extern struct filter_list_struct filter_list;
819bfe45 81extern struct filter_list_struct daemon_filter_list;
c627d613 82
332cf6df 83#ifdef ICONV_OPTION
2509753f 84extern int filesfrom_convert;
332cf6df
WD
85extern iconv_t ic_send, ic_recv;
86#endif
87
a572e126
WD
88#define PTR_SIZE (sizeof (struct file_struct *))
89
06c28400 90int io_error;
7752df41 91int checksum_len;
535737bf 92dev_t filesystem_dev; /* used to implement -x */
82ad07c4 93
c7e6f84f 94struct file_list *cur_flist, *first_flist, *dir_flist;
4e421735 95int send_dir_ndx = -1, send_dir_depth = -1;
c7e6f84f
WD
96int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
97int file_total = 0; /* total of all active items over all file-lists */
98int flist_eof = 0; /* all the file-lists are now known */
99
4e421735
WD
100#define NORMAL_NAME 0
101#define SLASH_ENDING_NAME 1
84ecaa0e 102#define DOTDIR_NAME 2
4e421735 103
7e4b6b7b
WD
104/* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
105 * internally, even if this platform does not allow files to have 64-bit inums.
106 * The only exception is if we're on a platform with no 64-bit type at all.
107 *
108 * Because we use read_longint() to get these off the wire, if you transfer
109 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
110 * machine with no 64-bit types then you will get an overflow error.
111 *
112 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
113 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
114 * be truncated. But it's a kind of silly thing to do anyhow. */
115
82ad07c4
WD
116/* The tmp_* vars are used as a cache area by make_file() to store data
117 * that the sender doesn't need to remember in its file list. The data
118 * will survive just long enough to be used by send_file_entry(). */
119static dev_t tmp_rdev;
9d155ecd 120#ifdef SUPPORT_HARD_LINKS
9d737ecb 121static int64 tmp_dev, tmp_ino;
9d155ecd 122#endif
a0456b9c 123static char tmp_sum[MAX_DIGEST_LEN];
06c28400 124
a0456b9c 125static char empty_sum[MAX_DIGEST_LEN];
c7565dad 126static int flist_count_offset; /* for --delete --progress */
8ea07c00 127static int dir_count = 0;
3d382777 128
65b4e4b2 129static void flist_sort_and_clean(struct file_list *flist, int strip_root);
32cbfe7b 130static void output_flist(struct file_list *flist);
0199b05f 131
61dec11a
WD
132void init_flist(void)
133{
951e826b 134 if (DEBUG_GTE(FLIST, 4)) {
96293cf9 135 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
6aef83c9 136 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
96293cf9 137 }
a0456b9c
WD
138 checksum_len = protocol_version < 21 ? 2
139 : protocol_version < 30 ? MD4_DIGEST_LEN
140 : MD5_DIGEST_LEN;
61dec11a
WD
141}
142
1bbd10fe 143static int show_filelist_p(void)
58225000 144{
951e826b 145 return INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
1bbd10fe 146}
ebed4c3a 147
1bbd10fe
DD
148static void start_filelist_progress(char *kind)
149{
ea124cb3 150 rprintf(FCLIENT, "%s ... ", kind);
951e826b 151 output_needs_newline = 1;
1bbd10fe 152 rflush(FINFO);
58225000
MP
153}
154
53135fe8 155static void emit_filelist_progress(int count)
db719fb0 156{
ea124cb3 157 rprintf(FCLIENT, " %d files...\r", count);
db719fb0
MP
158}
159
53135fe8 160static void maybe_emit_filelist_progress(int count)
58225000 161{
951e826b 162 if (INFO_GTE(FLIST, 2) && show_filelist_p() && (count % 100) == 0)
53135fe8 163 emit_filelist_progress(count);
58225000
MP
164}
165
1bbd10fe 166static void finish_filelist_progress(const struct file_list *flist)
58225000 167{
951e826b 168 if (INFO_GTE(FLIST, 2)) {
1bbd10fe 169 /* This overwrites the progress line */
c7b562be 170 rprintf(FINFO, "%d file%sto consider\n",
9decb4d2 171 flist->used, flist->used == 1 ? " " : "s ");
951e826b
WD
172 } else {
173 output_needs_newline = 0;
1bbd10fe 174 rprintf(FINFO, "done\n");
951e826b 175 }
58225000
MP
176}
177
86943126
MP
178void show_flist_stats(void)
179{
180 /* Nothing yet */
181}
182
cad8f6f9
WD
183/* Stat either a symlink or its referent, depending on the settings of
184 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
4e5db0ad 185 *
cad8f6f9
WD
186 * If path is the name of a symlink, then the linkbuf buffer (which must hold
187 * MAXPATHLEN chars) will be set to the symlink's target string.
4e5db0ad 188 *
cad8f6f9
WD
189 * The stat structure pointed to by stp will contain information about the
190 * link or the referent as appropriate, if they exist. */
191static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
b5313607 192{
4f5b0756 193#ifdef SUPPORT_LINKS
cad8f6f9 194 if (link_stat(path, stp, copy_dirlinks) < 0)
b5313607 195 return -1;
cad8f6f9
WD
196 if (S_ISLNK(stp->st_mode)) {
197 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
198 if (llen < 0)
b5313607 199 return -1;
cad8f6f9 200 linkbuf[llen] = '\0';
fc638474 201 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
951e826b 202 if (INFO_GTE(SYMSAFE, 1)) {
dbda5fbf 203 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
0ee6ca98 204 path, linkbuf);
fc638474 205 }
9439c0cb 206 return x_stat(path, stp, NULL);
b5313607 207 }
9585b276
WD
208 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
209 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
210 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
211 llen - SYMLINK_PREFIX_LEN + 1);
212 }
b5313607
DD
213 }
214 return 0;
215#else
9439c0cb 216 return x_stat(path, stp, NULL);
b5313607
DD
217#endif
218}
219
cad8f6f9 220int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
82306bf6 221{
4f5b0756 222#ifdef SUPPORT_LINKS
b7736c79 223 if (copy_links)
9439c0cb
WD
224 return x_stat(path, stp, NULL);
225 if (x_lstat(path, stp, NULL) < 0)
314f4591 226 return -1;
cad8f6f9 227 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
314f4591 228 STRUCT_STAT st;
9439c0cb 229 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
cad8f6f9 230 *stp = st;
314f4591
WD
231 }
232 return 0;
82306bf6 233#else
9439c0cb 234 return x_stat(path, stp, NULL);
82306bf6
AT
235#endif
236}
237
1aefb7ef
WD
238static inline int is_daemon_excluded(const char *fname, int is_dir)
239{
819bfe45 240 if (daemon_filter_list.head
1df02d13 241 && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1aefb7ef
WD
242 errno = ENOENT;
243 return 1;
244 }
245 return 0;
246}
247
29a89172 248static inline int path_is_daemon_excluded(char *path, int ignore_filename)
b5daf530 249{
f8949e76 250 if (daemon_filter_list.head) {
29a89172 251 char *slash = path;
b5daf530
WD
252
253 while ((slash = strchr(slash+1, '/')) != NULL) {
254 int ret;
255 *slash = '\0';
1df02d13 256 ret = check_filter(&daemon_filter_list, FLOG, path, 1);
b5daf530
WD
257 *slash = '/';
258 if (ret < 0) {
259 errno = ENOENT;
260 return 1;
261 }
262 }
263
264 if (!ignore_filename
1df02d13 265 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
b5daf530
WD
266 errno = ENOENT;
267 return 1;
268 }
269 }
270
271 return 0;
272}
273
7842418b 274/* This function is used to check if a file should be included/excluded
429f9828 275 * from the list of files based on its name and type etc. The value of
7842418b 276 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
1aefb7ef 277static int is_excluded(const char *fname, int is_dir, int filter_level)
c627d613 278{
7d687932 279#if 0 /* This currently never happens, so avoid a useless compare. */
7842418b 280 if (filter_level == NO_FILTERS)
429f9828
WD
281 return 0;
282#endif
1aefb7ef 283 if (is_daemon_excluded(fname, is_dir))
429f9828 284 return 1;
7842418b 285 if (filter_level != ALL_FILTERS)
429f9828 286 return 0;
7842418b 287 if (filter_list.head
1df02d13 288 && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
76e26e10 289 return 1;
76e26e10 290 return 0;
c627d613
AT
291}
292
a6e7b978 293static void send_directory(int f, struct file_list *flist,
c7e6f84f 294 char *fbuf, int len, int flags);
c627d613 295
ef35abb2
WD
296static const char *pathname, *orig_dir;
297static int pathname_len;
c627d613 298
9decb4d2 299/* Make sure flist can hold at least flist->used + extra entries. */
8ea07c00 300static void flist_expand(struct file_list *flist, int extra)
d9d6bc52 301{
0501f363 302 struct file_struct **new_ptr;
2e7d1994 303
9decb4d2 304 if (flist->used + extra <= flist->malloced)
a85906c7 305 return;
dbda5fbf 306
a85906c7
S
307 if (flist->malloced < FLIST_START)
308 flist->malloced = FLIST_START;
309 else if (flist->malloced >= FLIST_LINEAR)
310 flist->malloced += FLIST_LINEAR;
311 else
312 flist->malloced *= 2;
313
a6e7b978
WD
314 /* In case count jumped or we are starting the list
315 * with a known size just set it. */
9decb4d2
WD
316 if (flist->malloced < flist->used + extra)
317 flist->malloced = flist->used + extra;
a6e7b978 318
0501f363
WD
319 new_ptr = realloc_array(flist->files, struct file_struct *,
320 flist->malloced);
2e7d1994 321
951e826b 322 if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
6d56efa6 323 rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
a85906c7 324 who_am_i(),
adc2476f 325 big_num(sizeof flist->files[0] * flist->malloced),
a85906c7 326 (new_ptr == flist->files) ? " not" : "");
d9d6bc52 327 }
a85906c7 328
0501f363 329 flist->files = new_ptr;
a85906c7
S
330
331 if (!flist->files)
332 out_of_memory("flist_expand");
d9d6bc52
MP
333}
334
abdcb21a
WD
335static void flist_done_allocating(struct file_list *flist)
336{
337 void *ptr = pool_boundary(flist->file_pool, 8*1024);
338 if (flist->pool_boundary == ptr)
339 flist->pool_boundary = NULL; /* list didn't use any pool memory */
340 else
341 flist->pool_boundary = ptr;
342}
343
29a89172
WD
344/* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
345 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
346 * with dir == NULL taken to be the starting directory, and dirlen < 0
347 * indicating that strdup(dir) should be called and then the -dirlen length
348 * value checked to ensure that it is not daemon-excluded. */
349int change_pathname(struct file_struct *file, const char *dir, int dirlen)
c7e6f84f 350{
29a89172
WD
351 if (dirlen < 0) {
352 char *cpy = strdup(dir);
353 if (*cpy != '/')
354 change_dir(orig_dir, CD_SKIP_CHDIR);
355 if (path_is_daemon_excluded(cpy, 0))
356 goto chdir_error;
357 dir = cpy;
358 dirlen = -dirlen;
359 } else {
360 if (file) {
361 if (pathname == F_PATHNAME(file))
362 return 1;
363 dir = F_PATHNAME(file);
364 if (dir)
365 dirlen = strlen(dir);
366 } else if (pathname == dir)
367 return 1;
368 if (dir && *dir != '/')
369 change_dir(orig_dir, CD_SKIP_CHDIR);
c7e6f84f
WD
370 }
371
5b838296
WD
372 pathname = dir;
373 pathname_len = dirlen;
374
375 if (!dir)
376 dir = orig_dir;
377
378 if (!change_dir(dir, CD_NORMAL)) {
29a89172 379 chdir_error:
c7e6f84f 380 io_error |= IOERR_GENERAL;
342bfb5e 381 rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
5b838296
WD
382 if (dir != orig_dir)
383 change_dir(orig_dir, CD_NORMAL);
29a89172
WD
384 pathname = NULL;
385 pathname_len = 0;
c7e6f84f
WD
386 return 0;
387 }
388
c7e6f84f
WD
389 return 1;
390}
391
6e5b6822
WD
392static void send_file_entry(int f, const char *fname, struct file_struct *file,
393#ifdef SUPPORT_LINKS
394 const char *symlink_name, int symlink_len,
395#endif
396 int ndx, int first_ndx)
c627d613 397{
5911fee5
WD
398 static time_t modtime;
399 static mode_t mode;
c126e66e 400#ifdef SUPPORT_HARD_LINKS
1490812a 401 static int64 dev;
c126e66e 402#endif
4124540d 403 static dev_t rdev;
9c5e91f8 404 static uint32 rdev_major;
5911fee5
WD
405 static uid_t uid;
406 static gid_t gid;
c78cb8f3 407 static const char *user_name, *group_name;
5911fee5 408 static char lastname[MAXPATHLEN];
0c096e29 409#ifdef SUPPORT_HARD_LINKS
8cae71eb 410 int first_hlink_ndx = -1;
0c096e29 411#endif
ebed4c3a 412 int l1, l2;
61cb031c 413 int xflags;
72914a60 414
3f309272
WD
415 /* Initialize starting value of xflags. */
416 if (protocol_version >= 30 && S_ISDIR(file->mode)) {
eb7715c1 417 dir_count++;
3f309272
WD
418 if (file->flags & FLAG_CONTENT_DIR)
419 xflags = file->flags & FLAG_TOP_DIR;
420 else if (file->flags & FLAG_IMPLIED_DIR)
421 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
422 else
423 xflags = XMIT_NO_CONTENT_DIR;
424 } else
425 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
72914a60 426
30f337c9 427 if (file->mode == mode)
61cb031c 428 xflags |= XMIT_SAME_MODE;
1ef00d20 429 else
30f337c9 430 mode = file->mode;
75f162e4 431
b5c6a6ae
WD
432 if ((preserve_devices && IS_DEVICE(mode))
433 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 434 if (protocol_version < 28) {
82ad07c4 435 if (tmp_rdev == rdev)
61cb031c 436 xflags |= XMIT_SAME_RDEV_pre28;
b5c6a6ae 437 else
82ad07c4 438 rdev = tmp_rdev;
b5c6a6ae 439 } else {
82ad07c4 440 rdev = tmp_rdev;
9c5e91f8 441 if ((uint32)major(rdev) == rdev_major)
61cb031c 442 xflags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 443 else
9c5e91f8 444 rdev_major = major(rdev);
18438f0b 445 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
61cb031c 446 xflags |= XMIT_RDEV_MINOR_8_pre30;
75bc8600 447 }
b5c6a6ae 448 } else if (protocol_version < 28)
b4a09b72 449 rdev = MAKEDEV(0, 0);
fd259802
WD
450 if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
451 xflags |= XMIT_SAME_UID;
452 else {
453 uid = F_OWNER(file);
454 if (!numeric_ids) {
455 user_name = add_uid(uid);
456 if (inc_recurse && user_name)
457 xflags |= XMIT_USER_NAME_FOLLOWS;
c7e6f84f 458 }
82ad07c4 459 }
fd259802
WD
460 if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
461 xflags |= XMIT_SAME_GID;
462 else {
463 gid = F_GROUP(file);
464 if (!numeric_ids) {
465 group_name = add_gid(gid);
466 if (inc_recurse && group_name)
467 xflags |= XMIT_GROUP_NAME_FOLLOWS;
c7e6f84f 468 }
82ad07c4 469 }
30f337c9 470 if (file->modtime == modtime)
61cb031c 471 xflags |= XMIT_SAME_TIME;
1ef00d20 472 else
30f337c9 473 modtime = file->modtime;
a289addd 474
4f5b0756 475#ifdef SUPPORT_HARD_LINKS
9d737ecb 476 if (tmp_dev != 0) {
8cae71eb 477 if (protocol_version >= 30) {
9863bdbc 478 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
424d3691 479 first_hlink_ndx = (int32)(long)np->data - 1;
8cae71eb 480 if (first_hlink_ndx < 0) {
65b4e4b2 481 np->data = (void*)(long)(first_ndx + ndx + 1);
61cb031c 482 xflags |= XMIT_HLINK_FIRST;
8cae71eb 483 }
0c096e29 484 if (DEBUG_GTE(HLINK, 1)) {
28a5b78c
WD
485 if (first_hlink_ndx >= 0) {
486 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
487 who_am_i(), first_ndx + ndx, first_hlink_ndx,
488 first_hlink_ndx >= first_ndx ? "" : "un");
489 } else if (DEBUG_GTE(HLINK, 3)) {
490 rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
491 who_am_i(), first_ndx + ndx,
adc2476f 492 big_num(tmp_dev), big_num(tmp_ino));
28a5b78c 493 }
0c096e29 494 }
8cae71eb 495 } else {
9d737ecb 496 if (tmp_dev == dev) {
8cae71eb 497 if (protocol_version >= 28)
61cb031c 498 xflags |= XMIT_SAME_DEV_pre30;
8cae71eb 499 } else
9d737ecb 500 dev = tmp_dev;
8cae71eb 501 }
fd259802 502 xflags |= XMIT_HLINKED;
c4b4df4f 503 }
0d162bd1 504#endif
ebed4c3a
MP
505
506 for (l1 = 0;
3e491682
S
507 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
508 l1++) {}
eddd5d12 509 l2 = strlen(fname+l1);
72914a60 510
ebed4c3a 511 if (l1 > 0)
61cb031c 512 xflags |= XMIT_SAME_NAME;
ebed4c3a 513 if (l2 > 255)
61cb031c 514 xflags |= XMIT_LONG_NAME;
72914a60 515
1aa4caf3
WD
516 /* We must make sure we don't send a zero flag byte or the
517 * other end will terminate the flist transfer. Note that
ee3751c8 518 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
1aa4caf3 519 * it's harmless way to add a bit to the first flag byte. */
75bc8600 520 if (protocol_version >= 28) {
61cb031c
WD
521 if (!xflags && !S_ISDIR(mode))
522 xflags |= XMIT_TOP_DIR;
523 if ((xflags & 0xFF00) || !xflags) {
524 xflags |= XMIT_EXTENDED_FLAGS;
525 write_shortint(f, xflags);
75bc8600 526 } else
61cb031c 527 write_byte(f, xflags);
75bc8600 528 } else {
61cb031c
WD
529 if (!(xflags & 0xFF))
530 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
531 write_byte(f, xflags);
75bc8600 532 }
61cb031c 533 if (xflags & XMIT_SAME_NAME)
ebed4c3a 534 write_byte(f, l1);
61cb031c 535 if (xflags & XMIT_LONG_NAME)
f31514ad 536 write_varint30(f, l2);
72914a60 537 else
ebed4c3a
MP
538 write_byte(f, l2);
539 write_buf(f, fname + l1, l2);
72914a60 540
0c096e29 541#ifdef SUPPORT_HARD_LINKS
8cae71eb 542 if (first_hlink_ndx >= 0) {
9863bdbc 543 write_varint(f, first_hlink_ndx);
28a5b78c 544 if (first_hlink_ndx >= first_ndx)
d4d6646a 545 goto the_end;
8cae71eb 546 }
0c096e29 547#endif
8cae71eb 548
ba59bd68 549 write_varlong30(f, F_LENGTH(file), 3);
61cb031c 550 if (!(xflags & XMIT_SAME_TIME)) {
ba59bd68
WD
551 if (protocol_version >= 30)
552 write_varlong(f, modtime, 4);
553 else
554 write_int(f, modtime);
555 }
61cb031c 556 if (!(xflags & XMIT_SAME_MODE))
30f337c9 557 write_int(f, to_wire_mode(mode));
236adddc 558 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
18438f0b
WD
559 if (protocol_version < 30)
560 write_int(f, uid);
561 else {
f31514ad 562 write_varint(f, uid);
61cb031c 563 if (xflags & XMIT_USER_NAME_FOLLOWS) {
18438f0b
WD
564 int len = strlen(user_name);
565 write_byte(f, len);
566 write_buf(f, user_name, len);
567 }
c7e6f84f 568 }
72914a60 569 }
236adddc 570 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
18438f0b
WD
571 if (protocol_version < 30)
572 write_int(f, gid);
573 else {
f31514ad 574 write_varint(f, gid);
61cb031c 575 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
18438f0b
WD
576 int len = strlen(group_name);
577 write_byte(f, len);
578 write_buf(f, group_name, len);
579 }
c7e6f84f 580 }
72914a60 581 }
b5c6a6ae
WD
582 if ((preserve_devices && IS_DEVICE(mode))
583 || (preserve_specials && IS_SPECIAL(mode))) {
9c5e91f8 584 if (protocol_version < 28) {
61cb031c 585 if (!(xflags & XMIT_SAME_RDEV_pre28))
9c5e91f8
WD
586 write_int(f, (int)rdev);
587 } else {
61cb031c 588 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
f31514ad 589 write_varint30(f, major(rdev));
18438f0b 590 if (protocol_version >= 30)
f31514ad 591 write_varint(f, minor(rdev));
61cb031c 592 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
593 write_byte(f, minor(rdev));
594 else
595 write_int(f, minor(rdev));
596 }
75bc8600 597 }
c627d613 598
4f5b0756 599#ifdef SUPPORT_LINKS
6e5b6822
WD
600 if (symlink_len) {
601 write_varint30(f, symlink_len);
602 write_buf(f, symlink_name, symlink_len);
72914a60 603 }
c627d613
AT
604#endif
605
4f5b0756 606#ifdef SUPPORT_HARD_LINKS
9d737ecb 607 if (tmp_dev != 0 && protocol_version < 30) {
4124540d 608 if (protocol_version < 26) {
736a6a29 609 /* 32-bit dev_t and ino_t */
26404276 610 write_int(f, (int32)dev);
9d737ecb 611 write_int(f, (int32)tmp_ino);
736a6a29
MP
612 } else {
613 /* 64-bit dev_t and ino_t */
61cb031c 614 if (!(xflags & XMIT_SAME_DEV_pre30))
4124540d 615 write_longint(f, dev);
9d737ecb 616 write_longint(f, tmp_ino);
736a6a29 617 }
72914a60 618 }
dc5ddbcc
AT
619#endif
620
9a4a237e 621 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
4a19c3b2 622 const char *sum;
728d0922 623 if (S_ISREG(mode))
82ad07c4 624 sum = tmp_sum;
9a4a237e 625 else {
728d0922
WD
626 /* Prior to 28, we sent a useless set of nulls. */
627 sum = empty_sum;
728d0922 628 }
7752df41 629 write_buf(f, sum, checksum_len);
ebed4c3a 630 }
182dca5c 631
0c096e29 632#ifdef SUPPORT_HARD_LINKS
8cae71eb 633 the_end:
0c096e29 634#endif
ebed4c3a 635 strlcpy(lastname, fname, MAXPATHLEN);
ca947dea
WD
636
637 if (S_ISREG(mode) || S_ISLNK(mode))
638 stats.total_size += F_LENGTH(file);
182dca5c
AT
639}
640
82ad07c4 641static struct file_struct *recv_file_entry(struct file_list *flist,
1564cd5a 642 int xflags, int f)
182dca5c 643{
f14b3ef4 644 static int64 modtime;
5911fee5 645 static mode_t mode;
c126e66e 646#ifdef SUPPORT_HARD_LINKS
1490812a 647 static int64 dev;
c126e66e 648#endif
4124540d 649 static dev_t rdev;
9c5e91f8 650 static uint32 rdev_major;
5911fee5
WD
651 static uid_t uid;
652 static gid_t gid;
cf1b4969 653 static uint16 gid_flags;
a289addd 654 static char lastname[MAXPATHLEN], *lastdir;
33ffd7c3 655 static int lastdir_depth, lastdir_len = -1;
42f23f47 656 static unsigned int del_hier_name_len = 0;
649f8742 657 static int in_del_hier = 0;
72914a60 658 char thisname[MAXPATHLEN];
ebed4c3a 659 unsigned int l1 = 0, l2 = 0;
c7e6f84f 660 int alloc_len, basename_len, linkname_len;
99a957d3 661 int extra_len = file_extra_cnt * EXTRA_LEN;
8cae71eb 662 int first_hlink_ndx = -1;
4ecf3e06 663 int64 file_length;
c7e6f84f 664 const char *basename;
72914a60 665 struct file_struct *file;
ef35abb2
WD
666 alloc_pool_t *pool;
667 char *bp;
72914a60 668
1564cd5a 669 if (xflags & XMIT_SAME_NAME)
72914a60 670 l1 = read_byte(f);
ebed4c3a 671
1564cd5a 672 if (xflags & XMIT_LONG_NAME)
f31514ad 673 l2 = read_varint30(f);
72914a60
AT
674 else
675 l2 = read_byte(f);
676
ebed4c3a
MP
677 if (l2 >= MAXPATHLEN - l1) {
678 rprintf(FERROR,
1564cd5a
WD
679 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
680 xflags, l1, l2, lastname, who_am_i());
82ad07c4 681 overflow_exit("recv_file_entry");
d0fd26aa 682 }
72914a60 683
ebed4c3a
MP
684 strlcpy(thisname, lastname, l1 + 1);
685 read_sbuf(f, &thisname[l1], l2);
686 thisname[l1 + l2] = 0;
72914a60 687
332cf6df
WD
688 /* Abuse basename_len for a moment... */
689 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
690
691#ifdef ICONV_OPTION
692 if (ic_recv != (iconv_t)-1) {
2509753f 693 xbuf outbuf, inbuf;
332cf6df 694
2509753f 695 INIT_CONST_XBUF(outbuf, thisname);
bb640d32 696 INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
332cf6df 697
2509753f 698 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
332cf6df 699 io_error |= IOERR_GENERAL;
7c7462cd 700 rprintf(FERROR_UTF8,
332cf6df
WD
701 "[%s] cannot convert filename: %s (%s)\n",
702 who_am_i(), lastname, strerror(errno));
2509753f 703 outbuf.len = 0;
332cf6df 704 }
6e5b6822 705 thisname[outbuf.len] = '\0';
332cf6df
WD
706 }
707#endif
72914a60 708
7568ff44
WD
709 if (*thisname)
710 clean_fname(thisname, 0);
72914a60 711
0d162bd1 712 if (sanitize_paths)
d48810ba 713 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
cb13abfe 714
a289addd 715 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
716 int len = basename++ - thisname;
717 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
718 lastdir = new_array(char, len + 1);
719 memcpy(lastdir, thisname, len);
720 lastdir[len] = '\0';
721 lastdir_len = len;
722 lastdir_depth = count_dir_elements(lastdir);
723 }
724 } else
a289addd 725 basename = thisname;
a289addd 726 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 727
8cae71eb
WD
728#ifdef SUPPORT_HARD_LINKS
729 if (protocol_version >= 30
1564cd5a 730 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
9863bdbc 731 first_hlink_ndx = read_varint(f);
d4d6646a 732 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
8cae71eb
WD
733 rprintf(FERROR,
734 "hard-link reference out of range: %d (%d)\n",
d4d6646a 735 first_hlink_ndx, flist->ndx_start + flist->used);
8cae71eb
WD
736 exit_cleanup(RERR_PROTOCOL);
737 }
28a5b78c
WD
738 if (DEBUG_GTE(HLINK, 1)) {
739 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
740 who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
0c096e29
WD
741 first_hlink_ndx >= flist->ndx_start ? "" : "un");
742 }
d4d6646a
WD
743 if (first_hlink_ndx >= flist->ndx_start) {
744 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
745 file_length = F_LENGTH(first);
746 modtime = first->modtime;
747 mode = first->mode;
236adddc 748 if (preserve_uid)
d4d6646a 749 uid = F_OWNER(first);
236adddc 750 if (preserve_gid)
d4d6646a
WD
751 gid = F_GROUP(first);
752 if ((preserve_devices && IS_DEVICE(mode))
753 || (preserve_specials && IS_SPECIAL(mode))) {
754 uint32 *devp = F_RDEV_P(first);
755 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
756 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
757 }
758 if (preserve_links && S_ISLNK(mode))
759 linkname_len = strlen(F_SYMLINK(first)) + 1;
760 else
761 linkname_len = 0;
762 goto create_object;
8cae71eb 763 }
8cae71eb
WD
764 }
765#endif
766
ba59bd68 767 file_length = read_varlong30(f, 3);
1564cd5a 768 if (!(xflags & XMIT_SAME_TIME)) {
f14b3ef4
WD
769 if (protocol_version >= 30) {
770 modtime = read_varlong(f, 4);
771#if SIZEOF_TIME_T < SIZEOF_INT64
b58f5e17 772 if (!am_generator && (int64)(time_t)modtime != modtime) {
3f0211b6 773 rprintf(FERROR_XFER,
f14b3ef4
WD
774 "Time value of %s truncated on receiver.\n",
775 lastname);
776 }
777#endif
778 } else
779 modtime = read_int(f);
ba59bd68 780 }
1564cd5a 781 if (!(xflags & XMIT_SAME_MODE))
30f337c9 782 mode = from_wire_mode(read_int(f));
1ef00d20 783
ebec5eb6 784 if (chmod_modes && !S_ISLNK(mode))
8bbe41b5
WD
785 mode = tweak_mode(mode, chmod_modes);
786
236adddc 787 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
18438f0b
WD
788 if (protocol_version < 30)
789 uid = (uid_t)read_int(f);
790 else {
f31514ad 791 uid = (uid_t)read_varint(f);
1564cd5a 792 if (xflags & XMIT_USER_NAME_FOLLOWS)
18438f0b
WD
793 uid = recv_user_name(f, uid);
794 else if (inc_recurse && am_root && !numeric_ids)
795 uid = match_uid(uid);
796 }
c7e6f84f 797 }
236adddc 798 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
18438f0b
WD
799 if (protocol_version < 30)
800 gid = (gid_t)read_int(f);
801 else {
f31514ad 802 gid = (gid_t)read_varint(f);
cf1b4969 803 gid_flags = 0;
1564cd5a 804 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
cf1b4969 805 gid = recv_group_name(f, gid, &gid_flags);
18438f0b 806 else if (inc_recurse && (!am_root || !numeric_ids))
cf1b4969 807 gid = match_gid(gid, &gid_flags);
18438f0b 808 }
c7e6f84f 809 }
a289addd 810
b5c6a6ae
WD
811 if ((preserve_devices && IS_DEVICE(mode))
812 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 813 if (protocol_version < 28) {
1564cd5a 814 if (!(xflags & XMIT_SAME_RDEV_pre28))
b5c6a6ae
WD
815 rdev = (dev_t)read_int(f);
816 } else {
9c5e91f8 817 uint32 rdev_minor;
1564cd5a 818 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
f31514ad 819 rdev_major = read_varint30(f);
18438f0b 820 if (protocol_version >= 30)
f31514ad 821 rdev_minor = read_varint(f);
1564cd5a 822 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
823 rdev_minor = read_byte(f);
824 else
825 rdev_minor = read_int(f);
b4a09b72 826 rdev = MAKEDEV(rdev_major, rdev_minor);
1ef00d20 827 }
83d5e900 828 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
96293cf9 829 file_length = 0;
b5c6a6ae 830 } else if (protocol_version < 28)
b4a09b72 831 rdev = MAKEDEV(0, 0);
72914a60 832
4f5b0756 833#ifdef SUPPORT_LINKS
30f337c9 834 if (preserve_links && S_ISLNK(mode)) {
f31514ad 835 linkname_len = read_varint30(f) + 1; /* count the '\0' */
a289addd
WD
836 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
837 rprintf(FERROR, "overflow: linkname_len=%d\n",
838 linkname_len - 1);
82ad07c4 839 overflow_exit("recv_file_entry");
9dd891bb 840 }
6e5b6822
WD
841#ifdef ICONV_OPTION
842 /* We don't know how much extra room we need to convert
87678cef
WD
843 * the as-yet-unread symlink data, so let's hope that a
844 * double-size buffer is plenty. */
91fd15b8 845 if (sender_symlink_iconv)
87678cef 846 linkname_len *= 2;
6e5b6822 847#endif
9585b276
WD
848 if (munge_symlinks)
849 linkname_len += SYMLINK_PREFIX_LEN;
72914a60 850 }
a289addd 851 else
0d162bd1 852#endif
a289addd 853 linkname_len = 0;
0d162bd1 854
82ad07c4 855#ifdef SUPPORT_HARD_LINKS
8cae71eb 856 create_object:
4785cd43
WD
857 if (preserve_hard_links) {
858 if (protocol_version < 28 && S_ISREG(mode))
1564cd5a 859 xflags |= XMIT_HLINKED;
cf1b4969 860 if (xflags & XMIT_HLINKED)
d4d6646a 861 extra_len += (inc_recurse+1) * EXTRA_LEN;
82ad07c4
WD
862 }
863#endif
864
1c3344a1 865#ifdef SUPPORT_ACLS
d6c9c331
WD
866 /* Directories need an extra int32 for the default ACL. */
867 if (preserve_acls && S_ISDIR(mode))
868 extra_len += EXTRA_LEN;
1c3344a1
WD
869#endif
870
96293cf9
WD
871 if (always_checksum && S_ISREG(mode))
872 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
a289addd 873
4ecf3e06 874#if SIZEOF_INT64 >= 8
96293cf9
WD
875 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
876 extra_len += EXTRA_LEN;
4ecf3e06
WD
877#endif
878 if (file_length < 0) {
879 rprintf(FERROR, "Offset underflow: file-length is negative\n");
880 exit_cleanup(RERR_UNSUPPORTED);
881 }
96293cf9 882
3ea6e0e7 883 if (inc_recurse && S_ISDIR(mode)) {
c7e6f84f
WD
884 if (one_file_system) {
885 /* Room to save the dir's device for -x */
83d5e900 886 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
c7e6f84f 887 }
ef35abb2
WD
888 pool = dir_flist->file_pool;
889 } else
890 pool = flist->file_pool;
c7e6f84f 891
b58f5e17
WD
892#if EXTRA_ROUNDING > 0
893 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
894 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
895#endif
896
c7e6f84f 897 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 898 + linkname_len;
ef35abb2 899 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
9935066b 900
a3e18c76 901 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 902 bp += extra_len;
f5db0993 903 file = (struct file_struct *)bp;
96293cf9
WD
904 bp += FILE_STRUCT_LEN;
905
906 memcpy(bp, basename, basename_len);
a289addd 907
cf1b4969 908#ifdef SUPPORT_HARD_LINKS
75f162e4 909 if (xflags & XMIT_HLINKED)
cf1b4969
WD
910 file->flags |= FLAG_HLINKED;
911#endif
f14b3ef4 912 file->modtime = (time_t)modtime;
60af9465 913 file->len32 = (uint32)file_length;
4ecf3e06 914#if SIZEOF_INT64 >= 8
96293cf9 915 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
4ecf3e06
WD
916#if SIZEOF_CAPITAL_OFF_T < 8
917 rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
918 exit_cleanup(RERR_UNSUPPORTED);
919#else
96293cf9
WD
920 file->flags |= FLAG_LENGTH64;
921 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
4ecf3e06 922#endif
96293cf9 923 }
4ecf3e06 924#endif
a289addd 925 file->mode = mode;
236adddc 926 if (preserve_uid)
f1482c33 927 F_OWNER(file) = uid;
236adddc 928 if (preserve_gid) {
f1482c33 929 F_GROUP(file) = gid;
cf1b4969
WD
930 file->flags |= gid_flags;
931 }
f7a76b9c 932 if (unsort_ndx)
9decb4d2 933 F_NDX(file) = flist->used + flist->ndx_start;
a289addd 934
c7e6f84f
WD
935 if (basename != thisname) {
936 file->dirname = lastdir;
9d737ecb 937 F_DEPTH(file) = lastdir_depth + 1;
f3c3ed44 938 } else
9d737ecb 939 F_DEPTH(file) = 1;
f3c3ed44 940
649f8742 941 if (S_ISDIR(mode)) {
ee3751c8 942 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
9d737ecb 943 F_DEPTH(file)--;
ff0284dd 944 if (protocol_version >= 30) {
3f309272
WD
945 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
946 if (xflags & XMIT_TOP_DIR)
947 file->flags |= FLAG_TOP_DIR;
948 file->flags |= FLAG_CONTENT_DIR;
949 } else if (xflags & XMIT_TOP_DIR)
950 file->flags |= FLAG_IMPLIED_DIR;
ff0284dd 951 } else if (xflags & XMIT_TOP_DIR) {
75c51953 952 in_del_hier = recurse;
9d737ecb 953 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
7a16e122 954 if (relative_paths && del_hier_name_len > 2
9cdadbb1
WD
955 && lastname[del_hier_name_len-1] == '.'
956 && lastname[del_hier_name_len-2] == '/')
7a16e122 957 del_hier_name_len -= 2;
3f309272 958 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
ee3751c8 959 } else if (in_del_hier) {
f3c3ed44
WD
960 if (!relative_paths || !del_hier_name_len
961 || (l1 >= del_hier_name_len
9cdadbb1 962 && lastname[del_hier_name_len] == '/'))
3f309272 963 file->flags |= FLAG_CONTENT_DIR;
649f8742
WD
964 else
965 in_del_hier = 0;
966 }
967 }
968
b5c6a6ae 969 if ((preserve_devices && IS_DEVICE(mode))
82ad07c4 970 || (preserve_specials && IS_SPECIAL(mode))) {
4785cd43
WD
971 uint32 *devp = F_RDEV_P(file);
972 DEV_MAJOR(devp) = major(rdev);
973 DEV_MINOR(devp) = minor(rdev);
82ad07c4 974 }
a289addd 975
4f5b0756 976#ifdef SUPPORT_LINKS
a289addd 977 if (linkname_len) {
cfdb27b0 978 bp += basename_len;
d4d6646a 979 if (first_hlink_ndx >= flist->ndx_start) {
c47e3ee1 980 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
8cae71eb 981 memcpy(bp, F_SYMLINK(first), linkname_len);
ef3f14e6 982 } else {
6e5b6822
WD
983 if (munge_symlinks) {
984 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
985 bp += SYMLINK_PREFIX_LEN;
986 linkname_len -= SYMLINK_PREFIX_LEN;
987 }
988#ifdef ICONV_OPTION
91fd15b8 989 if (sender_symlink_iconv) {
6e5b6822
WD
990 xbuf outbuf, inbuf;
991
992 alloc_len = linkname_len;
87678cef 993 linkname_len /= 2;
6e5b6822 994
e34ad4e9 995 /* Read the symlink data into the end of our double-sized
6e5b6822
WD
996 * buffer and then convert it into the right spot. */
997 INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
998 linkname_len - 1, (size_t)-1);
999 read_sbuf(f, inbuf.buf, inbuf.len);
1000 INIT_XBUF(outbuf, bp, 0, alloc_len);
1001
1002 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
1003 io_error |= IOERR_GENERAL;
1004 rprintf(FERROR_XFER,
e34ad4e9 1005 "[%s] cannot convert symlink data for: %s (%s)\n",
6e5b6822
WD
1006 who_am_i(), full_fname(thisname), strerror(errno));
1007 bp = (char*)file->basename;
1008 *bp++ = '\0';
1009 outbuf.len = 0;
1010 }
1011 bp[outbuf.len] = '\0';
1012 } else
1013#endif
1014 read_sbuf(f, bp, linkname_len - 1);
1015 if (sanitize_paths && !munge_symlinks && *bp)
d48810ba 1016 sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
ef3f14e6 1017 }
a289addd
WD
1018 }
1019#endif
1020
4f5b0756 1021#ifdef SUPPORT_HARD_LINKS
75f162e4 1022 if (preserve_hard_links && xflags & XMIT_HLINKED) {
8cae71eb 1023 if (protocol_version >= 30) {
a0f29ed8 1024 if (xflags & XMIT_HLINK_FIRST) {
65b4e4b2 1025 F_HL_GNUM(file) = flist->ndx_start + flist->used;
a0f29ed8
WD
1026 } else
1027 F_HL_GNUM(file) = first_hlink_ndx;
736a6a29 1028 } else {
9d737ecb 1029 static int32 cnt = 0;
9863bdbc 1030 struct ht_int64_node *np;
9d737ecb
WD
1031 int64 ino;
1032 int32 ndx;
8cae71eb 1033 if (protocol_version < 26) {
9d737ecb
WD
1034 dev = read_int(f);
1035 ino = read_int(f);
8cae71eb 1036 } else {
1564cd5a 1037 if (!(xflags & XMIT_SAME_DEV_pre30))
8cae71eb 1038 dev = read_longint(f);
9d737ecb 1039 ino = read_longint(f);
8cae71eb 1040 }
9863bdbc 1041 np = idev_find(dev, ino);
424d3691 1042 ndx = (int32)(long)np->data - 1;
9d737ecb
WD
1043 if (ndx < 0) {
1044 ndx = cnt++;
424d3691 1045 np->data = (void*)(long)cnt;
9d737ecb
WD
1046 }
1047 F_HL_GNUM(file) = ndx;
736a6a29 1048 }
72914a60 1049 }
dc5ddbcc 1050#endif
ebed4c3a 1051
96293cf9
WD
1052 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
1053 if (S_ISREG(mode))
83d5e900 1054 bp = F_SUM(file);
82ad07c4 1055 else {
fea4db62 1056 /* Prior to 28, we get a useless set of nulls. */
82ad07c4 1057 bp = tmp_sum;
fea4db62 1058 }
d4d6646a 1059 if (first_hlink_ndx >= flist->ndx_start) {
c47e3ee1 1060 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
8cae71eb
WD
1061 memcpy(bp, F_SUM(first), checksum_len);
1062 } else
1063 read_buf(f, bp, checksum_len);
72914a60 1064 }
ebed4c3a 1065
1c3344a1
WD
1066#ifdef SUPPORT_ACLS
1067 if (preserve_acls && !S_ISLNK(mode))
1068 receive_acl(file, f);
1069#endif
16edf865
WD
1070#ifdef SUPPORT_XATTRS
1071 if (preserve_xattrs)
1072 receive_xattr(file, f );
1073#endif
1c3344a1 1074
ca947dea
WD
1075 if (S_ISREG(mode) || S_ISLNK(mode))
1076 stats.total_size += file_length;
1077
f5db0993 1078 return file;
c627d613
AT
1079}
1080
2f188c8d
WD
1081/* Create a file_struct for a named file by reading its stat() information
1082 * and performing extensive checks against global options.
db719fb0 1083 *
2f188c8d 1084 * Returns a pointer to the new file struct, or NULL if there was an error
2171b939
MM
1085 * or this file should be excluded.
1086 *
1087 * Note: Any error (here or in send_file_name) that results in the omission of
1088 * an existent source file from the file list should set
1089 * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1090 * destination if --delete is on. */
45d8bfe0 1091struct file_struct *make_file(const char *fname, struct file_list *flist,
82ad07c4 1092 STRUCT_STAT *stp, int flags, int filter_level)
c627d613 1093{
a289addd
WD
1094 static char *lastdir;
1095 static int lastdir_len = -1;
3ec4dd97 1096 struct file_struct *file;
1923b1fc 1097 char thisname[MAXPATHLEN];
e0870f1d 1098 char linkname[MAXPATHLEN];
c7e6f84f 1099 int alloc_len, basename_len, linkname_len;
99a957d3 1100 int extra_len = file_extra_cnt * EXTRA_LEN;
c7e6f84f 1101 const char *basename;
8ea07c00 1102 alloc_pool_t *pool;
2f188c8d 1103 STRUCT_STAT st;
c7e6f84f 1104 char *bp;
9935066b 1105
b3b32601 1106 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
2171b939 1107 io_error |= IOERR_GENERAL;
342bfb5e 1108 rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
882e6893
WD
1109 return NULL;
1110 }
58b1999e 1111 clean_fname(thisname, 0);
b7736c79 1112 if (sanitize_paths)
d48810ba 1113 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
3ec4dd97 1114
e4fdf1de 1115 if (stp && S_ISDIR(stp->st_mode)) {
ede1f0eb 1116 st = *stp; /* Needed for "symlink/." with --relative. */
e4fdf1de
WD
1117 *linkname = '\0'; /* make IBM code checker happy */
1118 } else if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 1119 int save_errno = errno;
a4a7e64c 1120 /* See if file is excluded before reporting an error. */
7842418b 1121 if (filter_level != NO_FILTERS
60cc01a6 1122 && (is_excluded(thisname, 0, filter_level)
53039410
WD
1123 || is_excluded(thisname, 1, filter_level))) {
1124 if (ignore_perishable && save_errno != ENOENT)
1125 non_perishable_cnt++;
a4a7e64c 1126 return NULL;
53039410 1127 }
a4a7e64c 1128 if (save_errno == ENOENT) {
4f5b0756 1129#ifdef SUPPORT_LINKS
21897ecb
WD
1130 /* When our options tell us to follow a symlink that
1131 * points nowhere, tell the user about the symlink
1132 * instead of giving a "vanished" message. We only
1133 * dereference a symlink if one of the --copy*links
1134 * options was specified, so there's no need for the
1135 * extra lstat() if one of these options isn't on. */
1136 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1137 && x_lstat(thisname, &st, NULL) == 0
1138 && S_ISLNK(st.st_mode)) {
a4a7e64c 1139 io_error |= IOERR_GENERAL;
3f0211b6 1140 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
a4a7e64c 1141 full_fname(thisname));
e5ce3bcf
WD
1142 } else
1143#endif
1144 {
a4a7e64c 1145 enum logcode c = am_daemon && protocol_version < 28
3f0211b6 1146 ? FERROR : FWARNING;
a4a7e64c
WD
1147 io_error |= IOERR_VANISHED;
1148 rprintf(c, "file has vanished: %s\n",
1149 full_fname(thisname));
76e26e10 1150 }
a4a7e64c 1151 } else {
a8726d2a 1152 io_error |= IOERR_GENERAL;
c78e4ea9 1153 rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
d62bcc17 1154 full_fname(thisname));
76e26e10 1155 }
3ec4dd97
AT
1156 return NULL;
1157 }
c627d613 1158
7842418b
WD
1159 if (filter_level == NO_FILTERS)
1160 goto skip_filters;
ac1a0994 1161
75f162e4
WD
1162 if (S_ISDIR(st.st_mode)) {
1163 if (!xfer_dirs) {
1164 rprintf(FINFO, "skipping directory %s\n", thisname);
1165 return NULL;
1166 }
40e38376
WD
1167 /* -x only affects dirs because we need to avoid recursing
1168 * into a mount-point directory, not to avoid copying a
1169 * symlinked file if -L (or similar) was specified. */
53ec55a8
WD
1170 if (one_file_system && st.st_dev != filesystem_dev
1171 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1172 if (one_file_system > 1) {
951e826b 1173 if (INFO_GTE(MOUNT, 1)) {
53ec55a8
WD
1174 rprintf(FINFO,
1175 "[%s] skipping mount-point dir %s\n",
1176 who_am_i(), thisname);
3932c423 1177 }
53ec55a8 1178 return NULL;
ebec5eb6 1179 }
53ec55a8
WD
1180 flags |= FLAG_MOUNT_DIR;
1181 flags &= ~FLAG_CONTENT_DIR;
ebec5eb6 1182 }
40e38376 1183 } else
3f309272 1184 flags &= ~FLAG_CONTENT_DIR;
ebed4c3a 1185
0d9eba03 1186 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
53039410
WD
1187 if (ignore_perishable)
1188 non_perishable_cnt++;
76e26e10 1189 return NULL;
53039410 1190 }
76e26e10 1191
132fcf36 1192 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 1193#ifdef SUPPORT_LINKS
132fcf36
WD
1194 if (!S_ISLNK(st.st_mode))
1195#endif
1196 if (access(thisname, R_OK) != 0)
1197 return NULL;
1198 }
ac1a0994 1199
97b7bff4 1200 skip_filters:
ac1a0994 1201
c7e6f84f 1202 /* Only divert a directory in the main transfer. */
8ea07c00
WD
1203 if (flist) {
1204 if (flist->prev && S_ISDIR(st.st_mode)
1205 && flags & FLAG_DIVERT_DIRS) {
1206 /* Room for parent/sibling/next-child info. */
83d5e900 1207 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
3932c423
WD
1208 if (relative_paths)
1209 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
8ea07c00
WD
1210 pool = dir_flist->file_pool;
1211 } else
1212 pool = flist->file_pool;
d6c9c331
WD
1213 } else {
1214#ifdef SUPPORT_ACLS
1215 /* Directories need an extra int32 for the default ACL. */
1216 if (preserve_acls && S_ISDIR(st.st_mode))
1217 extra_len += EXTRA_LEN;
1218#endif
8ea07c00 1219 pool = NULL;
d6c9c331 1220 }
c7e6f84f 1221
951e826b 1222 if (DEBUG_GTE(FLIST, 2)) {
ea847c62 1223 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
0ee6ca98 1224 who_am_i(), thisname, filter_level);
ea847c62 1225 }
ebed4c3a 1226
a289addd 1227 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
1228 int len = basename++ - thisname;
1229 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1230 lastdir = new_array(char, len + 1);
1231 memcpy(lastdir, thisname, len);
1232 lastdir[len] = '\0';
1233 lastdir_len = len;
1234 }
1235 } else
a289addd 1236 basename = thisname;
a289addd 1237 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 1238
4f5b0756 1239#ifdef SUPPORT_LINKS
a289addd
WD
1240 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1241#else
1242 linkname_len = 0;
1243#endif
1244
4ecf3e06 1245#if SIZEOF_CAPITAL_OFF_T >= 8
96293cf9
WD
1246 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1247 extra_len += EXTRA_LEN;
4ecf3e06 1248#endif
96293cf9 1249
886df221
WD
1250 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1251 file_checksum(thisname, tmp_sum, st.st_size);
1252 if (sender_keeps_checksum)
1253 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1254 }
1255
a3e18c76
WD
1256#if EXTRA_ROUNDING > 0
1257 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1258 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1259#endif
1260
c7e6f84f 1261 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 1262 + linkname_len;
8ea07c00
WD
1263 if (pool)
1264 bp = pool_alloc(pool, alloc_len, "make_file");
99aaa6ca 1265 else {
9935066b 1266 if (!(bp = new_array(char, alloc_len)))
99aaa6ca 1267 out_of_memory("make_file");
9935066b
S
1268 }
1269
a3e18c76 1270 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 1271 bp += extra_len;
a289addd 1272 file = (struct file_struct *)bp;
96293cf9
WD
1273 bp += FILE_STRUCT_LEN;
1274
1275 memcpy(bp, basename, basename_len);
82ad07c4
WD
1276
1277#ifdef SUPPORT_HARD_LINKS
c7e6f84f 1278 if (preserve_hard_links && flist && flist->prev) {
82ad07c4
WD
1279 if (protocol_version >= 28
1280 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1281 : S_ISREG(st.st_mode)) {
c8b823f9
WD
1282 tmp_dev = (int64)st.st_dev + 1;
1283 tmp_ino = (int64)st.st_ino;
82ad07c4 1284 } else
9d737ecb 1285 tmp_dev = 0;
82ad07c4
WD
1286 }
1287#endif
a289addd 1288
96293cf9
WD
1289#ifdef HAVE_STRUCT_STAT_ST_RDEV
1290 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1291 tmp_rdev = st.st_rdev;
1292 st.st_size = 0;
1293 }
1294#endif
1295
a289addd 1296 file->flags = flags;
3ec4dd97 1297 file->modtime = st.st_mtime;
60af9465 1298 file->len32 = (uint32)st.st_size;
4ecf3e06 1299#if SIZEOF_CAPITAL_OFF_T >= 8
96293cf9
WD
1300 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1301 file->flags |= FLAG_LENGTH64;
1302 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1303 }
4ecf3e06 1304#endif
8bbe41b5 1305 file->mode = st.st_mode;
236adddc 1306 if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
f1482c33 1307 F_OWNER(file) = st.st_uid;
236adddc 1308 if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
f1482c33 1309 F_GROUP(file) = st.st_gid;
61dec11a 1310
c7e6f84f
WD
1311 if (basename != thisname)
1312 file->dirname = lastdir;
a289addd 1313
4f5b0756 1314#ifdef SUPPORT_LINKS
d6c9c331 1315 if (linkname_len)
cfdb27b0 1316 memcpy(bp + basename_len, linkname, linkname_len);
0d162bd1
WD
1317#endif
1318
d6c9c331
WD
1319 if (am_sender)
1320 F_PATHNAME(file) = pathname;
1321 else if (!pool)
1322 F_DEPTH(file) = extra_len / EXTRA_LEN;
c627d613 1323
23f4587f
WD
1324 /* This code is only used by the receiver when it is building
1325 * a list of files for a delete pass. */
1326 if (keep_dirlinks && linkname_len && flist) {
1327 STRUCT_STAT st2;
f5db0993 1328 int save_mode = file->mode;
8bbe41b5 1329 file->mode = S_IFDIR; /* Find a directory with our name. */
c7e6f84f 1330 if (flist_find(dir_flist, file) >= 0
8ea07c00 1331 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
23f4587f 1332 file->modtime = st2.st_mtime;
96293cf9 1333 file->len32 = 0;
23f4587f 1334 file->mode = st2.st_mode;
9b25ef35 1335 if (uid_ndx)
f1482c33 1336 F_OWNER(file) = st2.st_uid;
9b25ef35 1337 if (gid_ndx)
f1482c33 1338 F_GROUP(file) = st2.st_gid;
f5db0993
WD
1339 } else
1340 file->mode = save_mode;
23f4587f
WD
1341 }
1342
a7188cbf
WD
1343 if (basename_len == 0+1) {
1344 if (!pool)
1345 unmake_file(file);
82ad07c4 1346 return NULL;
a7188cbf 1347 }
82ad07c4 1348
886df221
WD
1349 if (sender_keeps_checksum && S_ISREG(st.st_mode))
1350 memcpy(F_SUM(file), tmp_sum, checksum_len);
1351
f7a76b9c 1352 if (unsort_ndx)
e6d05dcf 1353 F_NDX(file) = dir_count;
c7e6f84f 1354
3ec4dd97 1355 return file;
c627d613
AT
1356}
1357
17026f27 1358/* Only called for temporary file_struct entries created by make_file(). */
82ad07c4
WD
1359void unmake_file(struct file_struct *file)
1360{
d6c9c331 1361 free(REQ_EXTRA(file, F_DEPTH(file)));
82ad07c4
WD
1362}
1363
42c6b139 1364static struct file_struct *send_file_name(int f, struct file_list *flist,
7568ff44 1365 const char *fname, STRUCT_STAT *stp,
252af65b 1366 int flags, int filter_level)
c627d613 1367{
ebed4c3a
MP
1368 struct file_struct *file;
1369
252af65b 1370 file = make_file(fname, flist, stp, flags, filter_level);
37802f40 1371 if (!file)
301fb56c 1372 return NULL;
ebed4c3a 1373
ebec5eb6 1374 if (chmod_modes && !S_ISLNK(file->mode))
8bbe41b5
WD
1375 file->mode = tweak_mode(file->mode, chmod_modes);
1376
7568ff44
WD
1377 if (f >= 0) {
1378 char fbuf[MAXPATHLEN];
6e5b6822
WD
1379#ifdef SUPPORT_LINKS
1380 const char *symlink_name;
1381 int symlink_len;
1382#ifdef ICONV_OPTION
1383 char symlink_buf[MAXPATHLEN];
1384#endif
1385#endif
7568ff44
WD
1386#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1387 stat_x sx;
1388#endif
1389
6e5b6822
WD
1390#ifdef SUPPORT_LINKS
1391 if (preserve_links && S_ISLNK(file->mode)) {
1392 symlink_name = F_SYMLINK(file);
1393 symlink_len = strlen(symlink_name);
1394 } else {
1395 symlink_name = NULL;
1396 symlink_len = 0;
1397 }
1398#endif
1399
7568ff44
WD
1400#ifdef ICONV_OPTION
1401 if (ic_send != (iconv_t)-1) {
1402 xbuf outbuf, inbuf;
1403
1404 INIT_CONST_XBUF(outbuf, fbuf);
1405
1406 if (file->dirname) {
1407 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1408 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1409 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
1410 goto convert_error;
1411 outbuf.size += 2;
6e5b6822 1412 fbuf[outbuf.len++] = '/';
7568ff44
WD
1413 }
1414
1415 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1416 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1417 convert_error:
1418 io_error |= IOERR_GENERAL;
342bfb5e 1419 rprintf(FERROR_XFER,
7568ff44
WD
1420 "[%s] cannot convert filename: %s (%s)\n",
1421 who_am_i(), f_name(file, fbuf), strerror(errno));
1422 return NULL;
1423 }
6e5b6822
WD
1424 fbuf[outbuf.len] = '\0';
1425
1426#ifdef SUPPORT_LINKS
91fd15b8 1427 if (symlink_len && sender_symlink_iconv) {
6e5b6822
WD
1428 INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1429 INIT_CONST_XBUF(outbuf, symlink_buf);
1430 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
1431 io_error |= IOERR_GENERAL;
1432 f_name(file, fbuf);
1433 rprintf(FERROR_XFER,
e34ad4e9 1434 "[%s] cannot convert symlink data for: %s (%s)\n",
6e5b6822
WD
1435 who_am_i(), full_fname(fbuf), strerror(errno));
1436 return NULL;
1437 }
1438 symlink_buf[outbuf.len] = '\0';
1439
1440 symlink_name = symlink_buf;
1441 symlink_len = outbuf.len;
1442 }
1443#endif
7568ff44
WD
1444 } else
1445#endif
1446 f_name(file, fbuf);
1447
1c3344a1 1448#ifdef SUPPORT_ACLS
7568ff44
WD
1449 if (preserve_acls && !S_ISLNK(file->mode)) {
1450 sx.st.st_mode = file->mode;
1451 sx.acc_acl = sx.def_acl = NULL;
2171b939
MM
1452 if (get_acl(fname, &sx) < 0) {
1453 io_error |= IOERR_GENERAL;
7568ff44 1454 return NULL;
2171b939 1455 }
7568ff44 1456 }
1c3344a1 1457#endif
16edf865 1458#ifdef SUPPORT_XATTRS
7568ff44
WD
1459 if (preserve_xattrs) {
1460 sx.xattr = NULL;
2171b939
MM
1461 if (get_xattr(fname, &sx) < 0) {
1462 io_error |= IOERR_GENERAL;
7568ff44 1463 return NULL;
2171b939 1464 }
7568ff44 1465 }
16edf865 1466#endif
1c3344a1 1467
6e5b6822
WD
1468 send_file_entry(f, fbuf, file,
1469#ifdef SUPPORT_LINKS
1470 symlink_name, symlink_len,
1471#endif
1472 flist->used, flist->ndx_start);
ebed4c3a 1473
1c3344a1
WD
1474#ifdef SUPPORT_ACLS
1475 if (preserve_acls && !S_ISLNK(file->mode)) {
1476 send_acl(&sx, f);
1477 free_acl(&sx);
1478 }
16edf865
WD
1479#endif
1480#ifdef SUPPORT_XATTRS
1481 if (preserve_xattrs) {
1482 F_XATTR(file) = send_xattr(&sx, f);
1483 free_xattr(&sx);
1484 }
1c3344a1
WD
1485#endif
1486 }
7568ff44
WD
1487
1488 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1489
1490 flist_expand(flist, 1);
1491 flist->files[flist->used++] = file;
1492
301fb56c
WD
1493 return file;
1494}
ebed4c3a 1495
301fb56c 1496static void send_if_directory(int f, struct file_list *flist,
56f0c976 1497 struct file_struct *file,
c7e6f84f
WD
1498 char *fbuf, unsigned int ol,
1499 int flags)
301fb56c 1500{
56f0c976 1501 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
1502
1503 if (S_ISDIR(file->mode)
82ad07c4 1504 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
f1773e09
WD
1505 void *save_filters;
1506 unsigned int len = strlen(fbuf);
1507 if (len > 1 && fbuf[len-1] == '/')
1508 fbuf[--len] = '\0';
1509 if (len >= MAXPATHLEN - 1) {
1510 io_error |= IOERR_GENERAL;
3f0211b6 1511 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
f1773e09
WD
1512 full_fname(fbuf));
1513 return;
1514 }
1515 save_filters = push_local_filters(fbuf, len);
a6e7b978 1516 send_directory(f, flist, fbuf, len, flags);
f1773e09 1517 pop_local_filters(save_filters);
56f0c976
WD
1518 fbuf[ol] = '\0';
1519 if (is_dot_dir)
1520 fbuf[ol-1] = '.';
ebed4c3a 1521 }
c627d613
AT
1522}
1523
a572e126
WD
1524static int file_compare(const void *file1, const void *file2)
1525{
1526 return f_name_cmp(*(struct file_struct **)file1,
1527 *(struct file_struct **)file2);
1528}
1529
56ce72c4 1530/* The guts of a merge-sort algorithm. This was derived from the glibc
a572e126
WD
1531 * version, but I (Wayne) changed the merge code to do less copying and
1532 * to require only half the amount of temporary memory. */
1533static void fsort_tmp(struct file_struct **fp, size_t num,
1534 struct file_struct **tmp)
1535{
1536 struct file_struct **f1, **f2, **t;
1537 size_t n1, n2;
1538
1539 n1 = num / 2;
1540 n2 = num - n1;
1541 f1 = fp;
1542 f2 = fp + n1;
1543
1544 if (n1 > 1)
1545 fsort_tmp(f1, n1, tmp);
1546 if (n2 > 1)
1547 fsort_tmp(f2, n2, tmp);
1548
1549 while (f_name_cmp(*f1, *f2) <= 0) {
1550 if (!--n1)
1551 return;
1552 f1++;
1553 }
1554
1555 t = tmp;
1556 memcpy(t, f1, n1 * PTR_SIZE);
1557
1558 *f1++ = *f2++, n2--;
1559
1560 while (n1 > 0 && n2 > 0) {
1561 if (f_name_cmp(*t, *f2) <= 0)
1562 *f1++ = *t++, n1--;
1563 else
1564 *f1++ = *f2++, n2--;
1565 }
1566
1567 if (n1 > 0)
1568 memcpy(f1, t, n1 * PTR_SIZE);
1569}
1570
1571/* This file-struct sorting routine makes sure that any identical names in
1572 * the file list stay in the same order as they were in the original list.
1573 * This is particularly vital in inc_recurse mode where we expect a sort
1574 * on the flist to match the exact order of a sort on the dir_flist. */
1575static void fsort(struct file_struct **fp, size_t num)
c7e6f84f 1576{
a572e126
WD
1577 if (num <= 1)
1578 return;
1579
1580 if (use_qsort)
1581 qsort(fp, num, PTR_SIZE, file_compare);
1582 else {
1583 struct file_struct **tmp = new_array(struct file_struct *,
1584 (num+1) / 2);
1585 fsort_tmp(fp, num, tmp);
1586 free(tmp);
1587 }
c7e6f84f
WD
1588}
1589
8ea07c00
WD
1590/* We take an entire set of sibling dirs from the sorted flist and link them
1591 * into the tree, setting the appropriate parent/child/sibling pointers. */
1592static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1593 int dir_cnt)
c7e6f84f
WD
1594{
1595 int i;
1596 int32 *dp = NULL;
1597 int32 *parent_dp = parent_ndx < 0 ? NULL
83d5e900 1598 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
c7e6f84f 1599
8ea07c00
WD
1600 flist_expand(dir_flist, dir_cnt);
1601 dir_flist->sorted = dir_flist->files;
c7e6f84f 1602
8ea07c00
WD
1603 for (i = 0; dir_cnt; i++) {
1604 struct file_struct *file = from_flist->sorted[i];
1605
1606 if (!S_ISDIR(file->mode))
1607 continue;
1608
9decb4d2 1609 dir_flist->files[dir_flist->used++] = file;
8ea07c00 1610 dir_cnt--;
ae87c434 1611
4e421735
WD
1612 if (file->basename[0] == '.' && file->basename[1] == '\0')
1613 continue;
1614
c7e6f84f 1615 if (dp)
9decb4d2 1616 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
c7e6f84f 1617 else if (parent_dp)
9decb4d2 1618 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
c7e6f84f 1619 else
9decb4d2 1620 send_dir_ndx = dir_flist->used - 1;
8ea07c00 1621
83d5e900 1622 dp = F_DIR_NODE_P(file);
c7e6f84f
WD
1623 DIR_PARENT(dp) = parent_ndx;
1624 DIR_FIRST_CHILD(dp) = -1;
1625 }
1626 if (dp)
1627 DIR_NEXT_SIBLING(dp) = -1;
1628}
1629
301fb56c 1630/* This function is normally called by the sender, but the receiving side also
7b558d7f 1631 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1632 * file list in memory without sending it over the wire. Also, get_dirlist()
1633 * might call this with f set to -2, which also indicates that local filter
1634 * rules should be ignored. */
a6e7b978
WD
1635static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1636 int flags)
c627d613 1637{
3ec4dd97 1638 struct dirent *di;
32cbfe7b 1639 unsigned remainder;
3ec4dd97 1640 char *p;
f1773e09 1641 DIR *d;
c7e6f84f 1642 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
9decb4d2 1643 int start = flist->used;
252af65b 1644 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
c7e6f84f
WD
1645
1646 assert(flist != NULL);
3ec4dd97 1647
f1773e09 1648 if (!(d = opendir(fbuf))) {
06c28400 1649 io_error |= IOERR_GENERAL;
3f0211b6 1650 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1651 return;
1652 }
c627d613 1653
19b2a5d9
WD
1654 p = fbuf + len;
1655 if (len != 1 || *fbuf != '/')
eddd5d12 1656 *p++ = '/';
f1773e09 1657 *p = '\0';
32cbfe7b 1658 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1659
6a7cc46c 1660 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1661 char *dname = d_name(di);
6a7cc46c
S
1662 if (dname[0] == '.' && (dname[1] == '\0'
1663 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1664 continue;
0ee6ca98 1665 if (strlcpy(p, dname, remainder) >= remainder) {
eddd5d12 1666 io_error |= IOERR_GENERAL;
342bfb5e 1667 rprintf(FERROR_XFER,
eddd5d12 1668 "cannot send long-named file %s\n",
f1773e09 1669 full_fname(fbuf));
beaf4954 1670 continue;
eddd5d12 1671 }
411c04f0
WD
1672 if (dname[0] == '\0') {
1673 io_error |= IOERR_GENERAL;
342bfb5e 1674 rprintf(FERROR_XFER,
411c04f0
WD
1675 "cannot send file with empty name in %s\n",
1676 full_fname(fbuf));
1677 continue;
1678 }
0ee6ca98 1679
252af65b 1680 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
3ec4dd97 1681 }
32cbfe7b
WD
1682
1683 fbuf[len] = '\0';
1684
6a7cc46c 1685 if (errno) {
06c28400 1686 io_error |= IOERR_GENERAL;
3f0211b6 1687 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1688 }
c627d613 1689
3ec4dd97 1690 closedir(d);
301fb56c 1691
a6e7b978 1692 if (f >= 0 && recurse && !divert_dirs) {
9decb4d2
WD
1693 int i, end = flist->used - 1;
1694 /* send_if_directory() bumps flist->used, so use "end". */
301fb56c 1695 for (i = start; i <= end; i++)
c7e6f84f
WD
1696 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1697 }
1698}
1699
3932c423 1700static void send_implied_dirs(int f, struct file_list *flist, char *fname,
4e421735 1701 char *start, char *limit, int flags, char name_type)
3932c423 1702{
af3172c1
WD
1703 static char lastpath[MAXPATHLEN] = "";
1704 static int lastpath_len = 0;
1705 static struct file_struct *lastpath_struct = NULL;
3932c423 1706 struct file_struct *file;
54410882
WD
1707 item_list *relname_list;
1708 relnamecache **rnpp;
af3172c1 1709 int len, need_new_dir, depth = 0;
75a01a07 1710 struct filter_list_struct save_filter_list = filter_list;
3932c423 1711
3f309272 1712 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
75a01a07 1713 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
3932c423
WD
1714
1715 if (inc_recurse) {
1716 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1717 && lastpath_len == limit - fname
1718 && strncmp(lastpath, fname, lastpath_len) == 0)
1719 need_new_dir = 0;
1720 else
1721 need_new_dir = 1;
af3172c1
WD
1722 } else {
1723 char *tp = fname, *lp = lastpath;
1724 /* Skip any initial directories in our path that we
1725 * have in common with lastpath. */
1726 assert(start == fname);
1727 for ( ; ; tp++, lp++) {
1728 if (tp == limit) {
1729 if (*lp == '/' || *lp == '\0')
1730 goto done;
1731 break;
1732 }
1733 if (*lp != *tp)
1734 break;
1735 if (*tp == '/') {
1736 start = tp;
1737 depth++;
1738 }
1739 }
3932c423 1740 need_new_dir = 1;
af3172c1 1741 }
3932c423
WD
1742
1743 if (need_new_dir) {
1744 int save_copy_links = copy_links;
1745 int save_xfer_dirs = xfer_dirs;
af3172c1 1746 char *slash;
3932c423 1747
74db1f14 1748 copy_links = xfer_dirs = 1;
3932c423
WD
1749
1750 *limit = '\0';
1751
1752 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1753 *slash = '\0';
af3172c1
WD
1754 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1755 depth++;
1756 if (!inc_recurse && file && S_ISDIR(file->mode))
1757 change_local_filter_dir(fname, strlen(fname), depth);
3932c423
WD
1758 *slash = '/';
1759 }
1760
1761 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1762 if (inc_recurse) {
1763 if (file && !S_ISDIR(file->mode))
1764 file = NULL;
3932c423 1765 lastpath_struct = file;
af3172c1
WD
1766 } else if (file && S_ISDIR(file->mode))
1767 change_local_filter_dir(fname, strlen(fname), ++depth);
3932c423
WD
1768
1769 strlcpy(lastpath, fname, sizeof lastpath);
1770 lastpath_len = limit - fname;
1771
1772 *limit = '/';
1773
1774 copy_links = save_copy_links;
1775 xfer_dirs = save_xfer_dirs;
1776
1777 if (!inc_recurse)
75a01a07 1778 goto done;
3932c423
WD
1779 }
1780
1781 if (!lastpath_struct)
75a01a07 1782 goto done; /* dir must have vanished */
3932c423
WD
1783
1784 len = strlen(limit+1);
54410882
WD
1785 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1786 if (!relname_list) {
1787 if (!(relname_list = new0(item_list)))
3932c423 1788 out_of_memory("send_implied_dirs");
54410882 1789 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
3932c423 1790 }
54410882
WD
1791 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1792 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
3932c423 1793 out_of_memory("send_implied_dirs");
4e421735 1794 (*rnpp)->name_type = name_type;
54410882 1795 strlcpy((*rnpp)->fname, limit+1, len + 1);
75a01a07
WD
1796
1797done:
1798 filter_list = save_filter_list;
3932c423
WD
1799}
1800
a6e7b978 1801static void send1extra(int f, struct file_struct *file, struct file_list *flist)
c7e6f84f
WD
1802{
1803 char fbuf[MAXPATHLEN];
54410882 1804 item_list *relname_list;
3f309272 1805 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
3932c423 1806 size_t j;
a6e7b978
WD
1807
1808 f_name(file, fbuf);
1809 dlen = strlen(fbuf);
1810
29a89172
WD
1811 if (!change_pathname(file, NULL, 0))
1812 exit_cleanup(RERR_FILESELECT);
a6e7b978
WD
1813
1814 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1815
53ec55a8
WD
1816 if (file->flags & FLAG_CONTENT_DIR) {
1817 if (one_file_system) {
1818 STRUCT_STAT st;
1819 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1820 io_error |= IOERR_GENERAL;
3f0211b6 1821 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
53ec55a8
WD
1822 full_fname(fbuf));
1823 return;
1824 }
1825 filesystem_dev = st.st_dev;
1826 }
3932c423 1827 send_directory(f, flist, fbuf, dlen, flags);
53ec55a8 1828 }
3932c423
WD
1829
1830 if (!relative_paths)
1831 return;
1832
54410882
WD
1833 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1834 if (!relname_list)
3932c423
WD
1835 return;
1836
54410882
WD
1837 for (j = 0; j < relname_list->count; j++) {
1838 char *slash;
1839 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
4e421735 1840 char name_type = rnp->name_type;
3932c423
WD
1841
1842 fbuf[dlen] = '/';
54410882
WD
1843 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1844 free(rnp);
3932c423
WD
1845 if (len >= (int)sizeof fbuf)
1846 continue; /* Impossible... */
1847
1848 slash = strchr(fbuf+dlen+1, '/');
1849 if (slash) {
4e421735 1850 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
3932c423
WD
1851 continue;
1852 }
1853
4e421735 1854 if (name_type != NORMAL_NAME) {
3932c423 1855 STRUCT_STAT st;
a15c4b38 1856 if (link_stat(fbuf, &st, 1) != 0) {
3932c423 1857 io_error |= IOERR_GENERAL;
3f0211b6 1858 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
3932c423
WD
1859 full_fname(fbuf));
1860 continue;
1861 }
ff0284dd
WD
1862 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1863 } else
1864 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
3932c423
WD
1865 }
1866
54410882 1867 free(relname_list);
a6e7b978
WD
1868}
1869
1870void send_extra_file_list(int f, int at_least)
1871{
c7e6f84f
WD
1872 struct file_list *flist;
1873 int64 start_write;
40e38376 1874 uint16 prev_flags;
d620219d 1875 int old_cnt, save_io_error = io_error;
c7e6f84f 1876
25c2a6ac 1877 if (flist_eof)
c7e6f84f
WD
1878 return;
1879
1880 /* Keep sending data until we have the requested number of
1881 * files in the upcoming file-lists. */
d620219d
WD
1882 old_cnt = cur_flist->used;
1883 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1884 old_cnt += flist->used;
1885 while (file_total - old_cnt < at_least) {
a6e7b978 1886 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
79028af1 1887 int dir_ndx, dstart = dir_count;
8db8eacb 1888 const char *pathname = F_PATHNAME(file);
c7e6f84f 1889 int32 *dp;
c7e6f84f
WD
1890
1891 flist = flist_new(0, "send_extra_file_list");
4c9d5fef 1892 start_write = stats.total_written;
c7e6f84f 1893
f7a76b9c 1894 if (unsort_ndx)
79028af1
WD
1895 dir_ndx = F_NDX(file);
1896 else
79028af1
WD
1897 dir_ndx = send_dir_ndx;
1898 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
5459e693 1899 flist->parent_ndx = dir_ndx;
a6e7b978 1900
79028af1 1901 send1extra(f, file, flist);
40e38376 1902 prev_flags = file->flags;
83d5e900 1903 dp = F_DIR_NODE_P(file);
a6e7b978 1904
79028af1
WD
1905 /* If there are any duplicate directory names that follow, we
1906 * send all the dirs together in one file-list. The dir_flist
1907 * tree links all the child subdirs onto the last dup dir. */
1908 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1909 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1910 send_dir_ndx = dir_ndx;
1911 file = dir_flist->sorted[dir_ndx];
40e38376 1912 /* Try to avoid some duplicate scanning of identical dirs. */
3f309272
WD
1913 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1914 file->flags &= ~FLAG_CONTENT_DIR;
40e38376
WD
1915 send1extra(f, file, flist);
1916 prev_flags = file->flags;
83d5e900 1917 dp = F_DIR_NODE_P(file);
a6e7b978 1918 }
79028af1 1919
8b3e6052
WD
1920 if (protocol_version < 31 || io_error == save_io_error || ignore_errors)
1921 write_byte(f, 0);
1922 else {
1923 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
1924 write_int(f, io_error);
1925 }
a6e7b978 1926
a6e7b978 1927 if (need_unsorted_flist) {
9decb4d2 1928 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
8ea07c00
WD
1929 out_of_memory("send_extra_file_list");
1930 memcpy(flist->sorted, flist->files,
9decb4d2 1931 flist->used * sizeof (struct file_struct*));
a6e7b978 1932 } else
332cf6df 1933 flist->sorted = flist->files;
8ea07c00 1934
65b4e4b2 1935 flist_sort_and_clean(flist, 0);
5459e693 1936
8ea07c00 1937 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
abdcb21a 1938 flist_done_allocating(flist);
a6e7b978 1939
9decb4d2 1940 file_total += flist->used;
c7e6f84f 1941 stats.flist_size += stats.total_written - start_write;
9decb4d2 1942 stats.num_files += flist->used;
951e826b 1943 if (DEBUG_GTE(FLIST, 3))
c7e6f84f
WD
1944 output_flist(flist);
1945
c7e6f84f
WD
1946 if (DIR_FIRST_CHILD(dp) >= 0) {
1947 send_dir_ndx = DIR_FIRST_CHILD(dp);
1948 send_dir_depth++;
1949 } else {
1950 while (DIR_NEXT_SIBLING(dp) < 0) {
1951 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
9ae7a2cd 1952 write_ndx(f, NDX_FLIST_EOF);
c7e6f84f
WD
1953 flist_eof = 1;
1954 change_local_filter_dir(NULL, 0, 0);
1955 goto finish;
1956 }
1957 send_dir_depth--;
a6e7b978 1958 file = dir_flist->sorted[send_dir_ndx];
83d5e900 1959 dp = F_DIR_NODE_P(file);
c7e6f84f
WD
1960 }
1961 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1962 }
301fb56c 1963 }
c7e6f84f
WD
1964
1965 finish:
8b3e6052 1966 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
c7e6f84f 1967 send_msg_int(MSG_IO_ERROR, io_error);
c627d613
AT
1968}
1969
ebed4c3a 1970struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1971{
3bb88b43
WD
1972 static const char *lastdir;
1973 static int lastdir_len = -1;
1974 int len, dirlen;
bcacc18b 1975 STRUCT_STAT st;
c7e6f84f 1976 char *p, *dir;
649d65ed 1977 struct file_list *flist;
31b4d25d 1978 struct timeval start_tv, end_tv;
a800434a 1979 int64 start_write;
24d0fcde 1980 int use_ff_fd = 0;
ff0284dd 1981 int disable_buffering;
0d9eba03 1982 int flags = recurse ? FLAG_CONTENT_DIR : 0;
95a44066 1983 int reading_remotely = filesfrom_host != NULL;
2509753f 1984 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
22955408
WD
1985#ifdef ICONV_OPTION
1986 | (filesfrom_convert ? RL_CONVERT : 0)
1987#endif
1988 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
d48810ba 1989 int implied_dot_dir = 0;
649d65ed 1990
ea124cb3 1991 rprintf(FLOG, "building file list\n");
134f4338 1992 if (show_filelist_p())
1bbd10fe 1993 start_filelist_progress("building file list");
951e826b 1994 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
98b1689d 1995 rprintf(FCLIENT, "sending incremental file list\n");
c627d613 1996
a800434a 1997 start_write = stats.total_written;
31b4d25d 1998 gettimeofday(&start_tv, NULL);
a800434a 1999
29a89172
WD
2000 if (!orig_dir)
2001 orig_dir = strdup(curr_dir);
2002
3f309272
WD
2003 if (relative_paths && protocol_version >= 30)
2004 implied_dirs = 1; /* We send flagged implied dirs */
2005
8cae71eb 2006#ifdef SUPPORT_HARD_LINKS
c7e6f84f 2007 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
8cae71eb
WD
2008 init_hard_links();
2009#endif
2010
c7e6f84f 2011 flist = cur_flist = flist_new(0, "send_file_list");
3ea6e0e7 2012 if (inc_recurse) {
c7e6f84f 2013 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
ff0284dd
WD
2014 flags |= FLAG_DIVERT_DIRS;
2015 } else
c7e6f84f 2016 dir_flist = cur_flist;
c627d613 2017
c7e6f84f 2018 disable_buffering = io_start_buffering_out(f);
134f4338 2019 if (filesfrom_fd >= 0) {
29a89172
WD
2020 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2021 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
f8949e76 2022 full_fname(argv[0]));
134f4338 2023 exit_cleanup(RERR_FILESELECT);
24d0fcde 2024 }
134f4338 2025 use_ff_fd = 1;
d6dead6b
AT
2026 }
2027
24d0fcde 2028 while (1) {
4e421735 2029 char fbuf[MAXPATHLEN], *fn, name_type;
c627d613 2030
24d0fcde 2031 if (use_ff_fd) {
2509753f 2032 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
24d0fcde 2033 break;
d48810ba 2034 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
24d0fcde
WD
2035 } else {
2036 if (argc-- == 0)
2037 break;
56f0c976 2038 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 2039 if (sanitize_paths)
d48810ba 2040 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
24d0fcde 2041 }
c627d613 2042
56f0c976 2043 len = strlen(fbuf);
1902a765
WD
2044 if (relative_paths) {
2045 /* We clean up fbuf below. */
4e421735 2046 name_type = NORMAL_NAME;
1902a765 2047 } else if (!len || fbuf[len - 1] == '/') {
56f0c976 2048 if (len == 2 && fbuf[0] == '.') {
6931c138 2049 /* Turn "./" into just "." rather than "./." */
34aa616d 2050 fbuf[--len] = '\0';
557a35f5 2051 } else {
56f0c976 2052 if (len + 1 >= MAXPATHLEN)
a1f99493 2053 overflow_exit("send_file_list");
56f0c976
WD
2054 fbuf[len++] = '.';
2055 fbuf[len] = '\0';
53f821f1 2056 }
84ecaa0e 2057 name_type = DOTDIR_NAME;
56f0c976
WD
2058 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2059 && (len == 2 || fbuf[len-3] == '/')) {
2060 if (len + 2 >= MAXPATHLEN)
a1f99493 2061 overflow_exit("send_file_list");
56f0c976
WD
2062 fbuf[len++] = '/';
2063 fbuf[len++] = '.';
2064 fbuf[len] = '\0';
84ecaa0e 2065 name_type = DOTDIR_NAME;
4e421735 2066 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
84ecaa0e 2067 name_type = DOTDIR_NAME;
4e421735
WD
2068 else
2069 name_type = NORMAL_NAME;
c627d613 2070
649d65ed
AT
2071 dir = NULL;
2072
2073 if (!relative_paths) {
56f0c976 2074 p = strrchr(fbuf, '/');
649d65ed 2075 if (p) {
151f59f1 2076 *p = '\0';
56f0c976 2077 if (p == fbuf)
649d65ed
AT
2078 dir = "/";
2079 else
56f0c976
WD
2080 dir = fbuf;
2081 len -= p - fbuf + 1;
2082 fn = p + 1;
2083 } else
2084 fn = fbuf;
1902a765
WD
2085 } else {
2086 if ((p = strstr(fbuf, "/./")) != NULL) {
2087 *p = '\0';
2088 if (p == fbuf)
2089 dir = "/";
d48810ba 2090 else {
1902a765 2091 dir = fbuf;
d48810ba
WD
2092 clean_fname(dir, 0);
2093 }
1902a765 2094 fn = p + 3;
4e421735
WD
2095 while (*fn == '/')
2096 fn++;
2097 if (!*fn)
2098 *--fn = '\0'; /* ensure room for '.' */
1902a765
WD
2099 } else
2100 fn = fbuf;
d48810ba
WD
2101 /* A leading ./ can be used in relative mode to affect
2102 * the dest dir without its name being in the path. */
2103 if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
2104 send_file_name(f, flist, ".", NULL,
2105 (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
2106 ALL_FILTERS);
2107 implied_dot_dir = 1;
2108 }
2109 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
4e421735 2110 | CFN_DROP_TRAILING_DOT_DIR);
ff0284dd
WD
2111 if (len == 1) {
2112 if (fn[0] == '/') {
2113 fn = "/.";
2114 len = 2;
84ecaa0e 2115 name_type = DOTDIR_NAME;
ff0284dd 2116 } else if (fn[0] == '.')
84ecaa0e 2117 name_type = DOTDIR_NAME;
4e421735
WD
2118 } else if (fn[len-1] == '/') {
2119 fn[--len] = '\0';
2120 if (len == 1 && *fn == '.')
84ecaa0e 2121 name_type = DOTDIR_NAME;
4e421735
WD
2122 else
2123 name_type = SLASH_ENDING_NAME;
ff0284dd 2124 }
1902a765 2125 /* Reject a ".." dir in the active part of the path. */
ede1f0eb
WD
2126 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2127 if ((p[2] == '/' || p[2] == '\0')
2128 && (p == fn || p[-1] == '/')) {
2129 rprintf(FERROR,
2130 "found \"..\" dir in relative path: %s\n",
4e421735 2131 fn);
ede1f0eb
WD
2132 exit_cleanup(RERR_SYNTAX);
2133 }
1902a765
WD
2134 }
2135 }
d2ea5980 2136
56f0c976
WD
2137 if (!*fn) {
2138 len = 1;
2139 fn = ".";
84ecaa0e 2140 name_type = DOTDIR_NAME;
56f0c976 2141 }
d2ea5980 2142
3bb88b43
WD
2143 dirlen = dir ? strlen(dir) : 0;
2144 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
29a89172 2145 if (!change_pathname(NULL, dir, -dirlen))
3b7bcaaf 2146 continue;
ef35abb2
WD
2147 lastdir = pathname;
2148 lastdir_len = pathname_len;
29a89172 2149 } else if (!change_pathname(NULL, lastdir, lastdir_len))
3bb88b43 2150 continue;
d2ea5980 2151
99eba675 2152 if (fn != fbuf)
56f0c976
WD
2153 memmove(fbuf, fn, len + 1);
2154
1aefb7ef 2155 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
84ecaa0e 2156 || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
b5daf530 2157 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
25c2a6ac 2158 io_error |= IOERR_GENERAL;
3f0211b6 2159 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
25c2a6ac
WD
2160 full_fname(fbuf));
2161 continue;
2162 }
2163
0d9eba03
WD
2164 /* A dot-dir should not be excluded! */
2165 if (name_type != DOTDIR_NAME
2166 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2167 continue;
2168
25c2a6ac
WD
2169 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2170 rprintf(FINFO, "skipping directory %s\n", fbuf);
2171 continue;
2172 }
2173
3932c423 2174 if (inc_recurse && relative_paths && *fbuf) {
0d9eba03 2175 if ((p = strchr(fbuf+1, '/')) != NULL) {
4e421735
WD
2176 if (p - fbuf == 1 && *fbuf == '.') {
2177 if ((fn = strchr(p+1, '/')) != NULL)
2178 p = fn;
2179 } else
2180 fn = p;
2181 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2182 if (fn == p)
2183 continue;
3932c423 2184 }
0d9eba03 2185 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
2186 /* Send the implied directories at the start of the
2187 * source spec, so we get their permissions right. */
af3172c1 2188 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
649d65ed 2189 }
ebed4c3a 2190
53ec55a8
WD
2191 if (one_file_system)
2192 filesystem_dev = st.st_dev;
2193
4e421735 2194 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
42c6b139 2195 struct file_struct *file;
42c6b139 2196 file = send_file_name(f, flist, fbuf, &st,
0d9eba03
WD
2197 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2198 NO_FILTERS);
d48810ba
WD
2199 if (!file)
2200 continue;
4e421735 2201 if (inc_recurse) {
84ecaa0e 2202 if (name_type == DOTDIR_NAME) {
4e421735
WD
2203 if (send_dir_depth < 0) {
2204 send_dir_depth = 0;
2205 change_local_filter_dir(fbuf, len, send_dir_depth);
2206 }
2207 send_directory(f, flist, fbuf, len, flags);
2208 }
d48810ba 2209 } else
42c6b139
WD
2210 send_if_directory(f, flist, file, fbuf, len, flags);
2211 } else
0d9eba03 2212 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
649d65ed 2213 }
dc5ddbcc 2214
134f4338
WD
2215 gettimeofday(&end_tv, NULL);
2216 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2217 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2218 if (stats.flist_buildtime == 0)
2219 stats.flist_buildtime = 1;
2220 start_tv = end_tv;
31b4d25d 2221
8b3e6052
WD
2222 /* Indicate end of file list */
2223 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2224 write_byte(f, 0);
2225 else {
2226 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2227 write_int(f, io_error);
2228 }
8cae71eb
WD
2229
2230#ifdef SUPPORT_HARD_LINKS
3ea6e0e7 2231 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
8cae71eb
WD
2232 idev_destroy();
2233#endif
c627d613 2234
134f4338
WD
2235 if (show_filelist_p())
2236 finish_filelist_progress(flist);
31b4d25d 2237
134f4338
WD
2238 gettimeofday(&end_tv, NULL);
2239 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2240 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 2241
332cf6df
WD
2242 /* When converting names, both sides keep an unsorted file-list array
2243 * because the names will differ on the sending and receiving sides
2244 * (both sides will use the unsorted index number for each item). */
2245
a6e7b978
WD
2246 /* Sort the list without removing any duplicates. This allows the
2247 * receiving side to ask for whatever name it kept. For incremental
2248 * recursion mode, the sender marks duplicate dirs so that it can
2249 * send them together in a single file-list. */
332cf6df 2250 if (need_unsorted_flist) {
65b4e4b2
WD
2251 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2252 out_of_memory("send_file_list");
2253 memcpy(flist->sorted, flist->files,
2254 flist->used * sizeof (struct file_struct*));
2255 } else
332cf6df 2256 flist->sorted = flist->files;
65b4e4b2 2257 flist_sort_and_clean(flist, 0);
9decb4d2 2258 file_total += flist->used;
ebed4c3a 2259
0b52f94d 2260 if (numeric_ids <= 0 && !inc_recurse)
1564cd5a 2261 send_id_list(f);
f6c34742 2262
134f4338 2263 /* send the io_error flag */
c7e6f84f
WD
2264 if (protocol_version < 30)
2265 write_int(f, ignore_errors ? 0 : io_error);
8b3e6052 2266 else if (io_error && protocol_version == 30 && !ignore_errors)
c7e6f84f
WD
2267 send_msg_int(MSG_IO_ERROR, io_error);
2268
2269 if (disable_buffering)
2270 io_end_buffering_out();
6ba9279f 2271
134f4338 2272 stats.flist_size = stats.total_written - start_write;
9decb4d2 2273 stats.num_files = flist->used;
d6dead6b 2274
951e826b 2275 if (DEBUG_GTE(FLIST, 3))
32cbfe7b 2276 output_flist(flist);
cefed3e8 2277
951e826b 2278 if (DEBUG_GTE(FLIST, 2))
ebed4c3a 2279 rprintf(FINFO, "send_file_list done\n");
17faa41c 2280
3ea6e0e7 2281 if (inc_recurse) {
4e421735 2282 send_dir_depth = 1;
8ea07c00 2283 add_dirs_to_tree(-1, flist, dir_count);
902ee53e 2284 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
4e421735 2285 flist->parent_ndx = -1;
abdcb21a 2286 flist_done_allocating(flist);
25c2a6ac
WD
2287 if (send_dir_ndx < 0) {
2288 write_ndx(f, NDX_FLIST_EOF);
2289 flist_eof = 1;
2290 }
2291 else if (file_total == 1) {
c7e6f84f
WD
2292 /* If we're creating incremental file-lists and there
2293 * was just 1 item in the first file-list, send 1 more
2294 * file-list to check if this is a 1-file xfer. */
25c2a6ac 2295 send_extra_file_list(f, 1);
c7e6f84f
WD
2296 }
2297 }
2298
649d65ed 2299 return flist;
c627d613
AT
2300}
2301
c627d613
AT
2302struct file_list *recv_file_list(int f)
2303{
ebed4c3a 2304 struct file_list *flist;
c7e6f84f 2305 int dstart, flags;
ebed4c3a 2306 int64 start_read;
c627d613 2307
a02348b5
WD
2308 if (!first_flist) {
2309 if (show_filelist_p())
2310 start_filelist_progress("receiving file list");
2311 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2312 rprintf(FCLIENT, "receiving incremental file list\n");
c7e6f84f 2313 rprintf(FLOG, "receiving file list\n");
a02348b5 2314 }
c627d613 2315
ebed4c3a 2316 start_read = stats.total_read;
a800434a 2317
8cae71eb 2318#ifdef SUPPORT_HARD_LINKS
d4d6646a 2319 if (preserve_hard_links && !first_flist)
8cae71eb
WD
2320 init_hard_links();
2321#endif
c627d613 2322
9863bdbc
WD
2323 flist = flist_new(0, "recv_file_list");
2324
3ea6e0e7 2325 if (inc_recurse) {
4e421735 2326 if (flist->ndx_start == 1)
c7e6f84f 2327 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
9decb4d2 2328 dstart = dir_flist->used;
c7e6f84f
WD
2329 } else {
2330 dir_flist = flist;
2331 dstart = 0;
2332 }
2333
1ef00d20 2334 while ((flags = read_byte(f)) != 0) {
f5db0993 2335 struct file_struct *file;
dbda5fbf 2336
d01d15e0 2337 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 2338 flags |= read_byte(f) << 8;
8b3e6052
WD
2339
2340 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2341 int err;
2342 if (protocol_version < 31) {
2343 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2344 exit_cleanup(RERR_PROTOCOL);
2345 }
2346 err = read_int(f);
2347 if (!ignore_errors)
2348 io_error |= err;
2349 break;
2350 }
2351
2352 flist_expand(flist, 1);
82ad07c4 2353 file = recv_file_entry(flist, flags, f);
c627d613 2354
3ea6e0e7 2355 if (inc_recurse && S_ISDIR(file->mode)) {
8ea07c00 2356 flist_expand(dir_flist, 1);
9decb4d2 2357 dir_flist->files[dir_flist->used++] = file;
c7e6f84f
WD
2358 }
2359
9decb4d2 2360 flist->files[flist->used++] = file;
c627d613 2361
9decb4d2 2362 maybe_emit_filelist_progress(flist->used);
1bbd10fe 2363
951e826b 2364 if (DEBUG_GTE(FLIST, 2)) {
6e5b6822
WD
2365 char *name = f_name(file, NULL);
2366 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
5e4ff5f9 2367 }
ebed4c3a 2368 }
9decb4d2 2369 file_total += flist->used;
c627d613 2370
951e826b 2371 if (DEBUG_GTE(FLIST, 2))
9decb4d2 2372 rprintf(FINFO, "received %d names\n", flist->used);
c627d613 2373
b7736c79 2374 if (show_filelist_p())
1bbd10fe 2375 finish_filelist_progress(flist);
a06d19e3 2376
332cf6df
WD
2377 if (need_unsorted_flist) {
2378 /* Create an extra array of index pointers that we can sort for
2379 * the generator's use (for wading through the files in sorted
2380 * order and for calling flist_find()). We keep the "files"
2381 * list unsorted for our exchange of index numbers with the
2382 * other side (since their names may not sort the same). */
9decb4d2 2383 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
332cf6df
WD
2384 out_of_memory("recv_file_list");
2385 memcpy(flist->sorted, flist->files,
9decb4d2
WD
2386 flist->used * sizeof (struct file_struct*));
2387 if (inc_recurse && dir_flist->used > dstart) {
65b4e4b2
WD
2388 static int dir_flist_malloced = 0;
2389 if (dir_flist_malloced < dir_flist->malloced) {
2390 dir_flist->sorted = realloc_array(dir_flist->sorted,
2391 struct file_struct *,
2392 dir_flist->malloced);
2393 dir_flist_malloced = dir_flist->malloced;
2394 }
8ea07c00 2395 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
9decb4d2
WD
2396 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2397 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
a6e7b978 2398 }
f7a76b9c 2399 } else {
332cf6df 2400 flist->sorted = flist->files;
9decb4d2 2401 if (inc_recurse && dir_flist->used > dstart) {
a6e7b978 2402 dir_flist->sorted = dir_flist->files;
9decb4d2 2403 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
a6e7b978
WD
2404 }
2405 }
332cf6df 2406
abdcb21a
WD
2407 if (inc_recurse)
2408 flist_done_allocating(flist);
2409 else if (f >= 0)
1564cd5a 2410 recv_id_list(f, flist);
f6c34742 2411
65b4e4b2 2412 flist_sort_and_clean(flist, relative_paths);
a6e7b978 2413
c7e6f84f 2414 if (protocol_version < 30) {
b9f592fb 2415 /* Recv the io_error flag */
8b3e6052
WD
2416 int err = read_int(f);
2417 if (!ignore_errors)
2418 io_error |= err;
4e421735 2419 } else if (inc_recurse && flist->ndx_start == 1) {
902ee53e 2420 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
4e421735 2421 flist->parent_ndx = -1;
ebed4c3a 2422 }
6ba9279f 2423
951e826b 2424 if (DEBUG_GTE(FLIST, 3))
32cbfe7b 2425 output_flist(flist);
cefed3e8 2426
951e826b 2427 if (DEBUG_GTE(FLIST, 2))
ebed4c3a 2428 rprintf(FINFO, "recv_file_list done\n");
17faa41c 2429
c7e6f84f 2430 stats.flist_size += stats.total_read - start_read;
9decb4d2 2431 stats.num_files += flist->used;
a800434a 2432
ebed4c3a 2433 return flist;
c627d613
AT
2434}
2435
c7e6f84f
WD
2436/* This is only used once by the receiver if the very first file-list
2437 * has exactly one item in it. */
2438void recv_additional_file_list(int f)
c627d613 2439{
c7e6f84f 2440 struct file_list *flist;
9ae7a2cd 2441 int ndx = read_ndx(f);
25c2a6ac 2442 if (ndx == NDX_FLIST_EOF) {
c7e6f84f
WD
2443 flist_eof = 1;
2444 change_local_filter_dir(NULL, 0, 0);
2445 } else {
2446 ndx = NDX_FLIST_OFFSET - ndx;
9decb4d2 2447 if (ndx < 0 || ndx >= dir_flist->used) {
c7e6f84f
WD
2448 ndx = NDX_FLIST_OFFSET - ndx;
2449 rprintf(FERROR,
8ea07c00
WD
2450 "[%s] Invalid dir index: %d (%d - %d)\n",
2451 who_am_i(), ndx, NDX_FLIST_OFFSET,
9decb4d2 2452 NDX_FLIST_OFFSET - dir_flist->used + 1);
c7e6f84f
WD
2453 exit_cleanup(RERR_PROTOCOL);
2454 }
951e826b 2455 if (DEBUG_GTE(FLIST, 3)) {
1faa1a6d
WD
2456 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2457 who_am_i(), ndx);
2458 }
c7e6f84f
WD
2459 flist = recv_file_list(f);
2460 flist->parent_ndx = ndx;
2461 }
c627d613
AT
2462}
2463
f5db0993
WD
2464/* Search for an identically-named item in the file list. Note that the
2465 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 2466int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 2467{
f3c3ed44 2468 int low = flist->low, high = flist->high;
207522ae 2469 int diff, mid, mid_up;
f3c3ed44
WD
2470
2471 while (low <= high) {
2472 mid = (low + high) / 2;
332cf6df 2473 if (F_IS_ACTIVE(flist->sorted[mid]))
c0b134a4 2474 mid_up = mid;
7402d583
WD
2475 else {
2476 /* Scan for the next non-empty entry using the cached
2477 * distance values. If the value isn't fully up-to-
2478 * date, update it. */
332cf6df
WD
2479 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2480 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
85aecef6 2481 do {
332cf6df
WD
2482 mid_up += F_DEPTH(flist->sorted[mid_up]);
2483 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2484 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
c0b134a4 2485 }
c0b134a4 2486 if (mid_up > high) {
7402d583
WD
2487 /* If there's nothing left above us, set high to
2488 * a non-empty entry below us and continue. */
332cf6df
WD
2489 high = mid - (int)flist->sorted[mid]->len32;
2490 if (!F_IS_ACTIVE(flist->sorted[high])) {
85aecef6 2491 do {
332cf6df
WD
2492 high -= (int)flist->sorted[high]->len32;
2493 } while (!F_IS_ACTIVE(flist->sorted[high]));
2494 flist->sorted[mid]->len32 = mid - high;
7402d583 2495 }
c0b134a4
WD
2496 continue;
2497 }
c0b134a4 2498 }
332cf6df 2499 diff = f_name_cmp(flist->sorted[mid_up], f);
207522ae 2500 if (diff == 0) {
f5db0993 2501 if (protocol_version < 29
332cf6df 2502 && S_ISDIR(flist->sorted[mid_up]->mode)
f5db0993
WD
2503 != S_ISDIR(f->mode))
2504 return -1;
f3c3ed44 2505 return mid_up;
f5db0993 2506 }
207522ae 2507 if (diff < 0)
f3c3ed44 2508 low = mid_up + 1;
207522ae
WD
2509 else
2510 high = mid - 1;
d966ee25 2511 }
d966ee25 2512 return -1;
c627d613
AT
2513}
2514
3ec4dd97 2515/*
9935066b
S
2516 * Free up any resources a file_struct has allocated
2517 * and clear the file.
3ec4dd97 2518 */
82ad07c4 2519void clear_file(struct file_struct *file)
c627d613 2520{
a3e18c76
WD
2521 /* The +1 zeros out the first char of the basename. */
2522 memset(file, 0, FILE_STRUCT_LEN + 1);
9d737ecb 2523 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
96293cf9 2524 * entry. Likewise for len32 in the opposite direction. We assume
0679ac4c
WD
2525 * that we're alone for now since flist_find() will adjust the counts
2526 * it runs into that aren't up-to-date. */
9d737ecb 2527 file->len32 = F_DEPTH(file) = 1;
3ec4dd97 2528}
c627d613 2529
4785cd43 2530/* Allocate a new file list. */
c7e6f84f 2531struct file_list *flist_new(int flags, char *msg)
3d382777
AT
2532{
2533 struct file_list *flist;
2534
3932c423 2535 if (!(flist = new0(struct file_list)))
9935066b 2536 out_of_memory(msg);
3d382777 2537
abdcb21a
WD
2538 if (flags & FLIST_TEMP) {
2539 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
a02348b5 2540 out_of_memory,
fb01d1fb 2541 POOL_INTERN)))
abdcb21a
WD
2542 out_of_memory(msg);
2543 } else {
c7e6f84f
WD
2544 /* This is a doubly linked list with prev looping back to
2545 * the end of the list, but the last next pointer is NULL. */
abdcb21a
WD
2546 if (!first_flist) {
2547 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
a02348b5 2548 out_of_memory,
fb01d1fb 2549 POOL_INTERN);
abdcb21a
WD
2550 if (!flist->file_pool)
2551 out_of_memory(msg);
2552
65b4e4b2 2553 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
4e421735 2554
c7e6f84f 2555 first_flist = cur_flist = flist->prev = flist;
abdcb21a 2556 } else {
65b4e4b2
WD
2557 struct file_list *prev = first_flist->prev;
2558
abdcb21a
WD
2559 flist->file_pool = first_flist->file_pool;
2560
65b4e4b2
WD
2561 flist->ndx_start = prev->ndx_start + prev->used + 1;
2562 flist->flist_num = prev->flist_num + 1;
abdcb21a 2563
65b4e4b2
WD
2564 flist->prev = prev;
2565 prev->next = first_flist->prev = flist;
c7e6f84f 2566 }
abdcb21a 2567 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
c7e6f84f
WD
2568 flist_cnt++;
2569 }
2570
3d382777
AT
2571 return flist;
2572}
ebed4c3a 2573
4785cd43 2574/* Free up all elements in a flist. */
3ec4dd97
AT
2575void flist_free(struct file_list *flist)
2576{
abdcb21a
WD
2577 if (!flist->prev) {
2578 /* Was FLIST_TEMP dir-list. */
2579 } else if (flist == flist->prev) {
c7e6f84f
WD
2580 first_flist = cur_flist = NULL;
2581 file_total = 0;
2582 flist_cnt = 0;
2583 } else {
2584 if (flist == cur_flist)
2585 cur_flist = flist->next;
2586 if (flist == first_flist)
2587 first_flist = first_flist->next;
2588 else {
2589 flist->prev->next = flist->next;
2590 if (!flist->next)
2591 flist->next = first_flist;
2592 }
2593 flist->next->prev = flist->prev;
9decb4d2 2594 file_total -= flist->used;
c7e6f84f
WD
2595 flist_cnt--;
2596 }
2597
abdcb21a
WD
2598 if (!flist->prev || !flist_cnt)
2599 pool_destroy(flist->file_pool);
2600 else
2601 pool_free_old(flist->file_pool, flist->pool_boundary);
2602
332cf6df
WD
2603 if (flist->sorted && flist->sorted != flist->files)
2604 free(flist->sorted);
3ec4dd97 2605 free(flist->files);
3ec4dd97 2606 free(flist);
c627d613
AT
2607}
2608
a6e7b978
WD
2609/* This routine ensures we don't have any duplicate names in our file list.
2610 * duplicate names can cause corruption because of the pipelining. */
65b4e4b2 2611static void flist_sort_and_clean(struct file_list *flist, int strip_root)
c627d613 2612{
85aecef6 2613 char fbuf[MAXPATHLEN];
a6e7b978 2614 int i, prev_i;
c627d613 2615
910ee8c9
WD
2616 if (!flist)
2617 return;
9decb4d2 2618 if (flist->used == 0) {
910ee8c9 2619 flist->high = -1;
9decb4d2 2620 flist->low = 0;
3ec4dd97 2621 return;
910ee8c9 2622 }
3ec4dd97 2623
9decb4d2 2624 fsort(flist->sorted, flist->used);
ebed4c3a 2625
a6e7b978 2626 if (!am_sender || inc_recurse) {
9decb4d2 2627 for (i = prev_i = 0; i < flist->used; i++) {
a6e7b978
WD
2628 if (F_IS_ACTIVE(flist->sorted[i])) {
2629 prev_i = i;
2630 break;
2631 }
b91b50c0 2632 }
a6e7b978
WD
2633 flist->low = prev_i;
2634 } else {
9decb4d2 2635 i = prev_i = flist->used - 1;
a6e7b978 2636 flist->low = 0;
b91b50c0 2637 }
a6e7b978 2638
9decb4d2 2639 while (++i < flist->used) {
fe1c19dc 2640 int j;
332cf6df 2641 struct file_struct *file = flist->sorted[i];
f5db0993 2642
96293cf9 2643 if (!F_IS_ACTIVE(file))
b91b50c0 2644 continue;
332cf6df 2645 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
fe1c19dc
WD
2646 j = prev_i;
2647 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
2648 int save_mode = file->mode;
2649 /* Make sure that this directory doesn't duplicate a
2650 * non-directory earlier in the list. */
f5db0993 2651 flist->high = prev_i;
fe1c19dc
WD
2652 file->mode = S_IFREG;
2653 j = flist_find(flist, file);
f5db0993 2654 file->mode = save_mode;
fe1c19dc
WD
2655 } else
2656 j = -1;
2657 if (j >= 0) {
fe1c19dc
WD
2658 int keep, drop;
2659 /* If one is a dir and the other is not, we want to
2660 * keep the dir because it might have contents in the
e8e9e12c
WD
2661 * list. Otherwise keep the first one. */
2662 if (S_ISDIR(file->mode)) {
2663 struct file_struct *fp = flist->sorted[j];
3932c423 2664 if (!S_ISDIR(fp->mode))
e8e9e12c
WD
2665 keep = i, drop = j;
2666 else {
2667 if (am_sender)
2668 file->flags |= FLAG_DUPLICATE;
3f309272
WD
2669 else { /* Make sure we merge our vital flags. */
2670 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2671 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2672 }
e8e9e12c
WD
2673 keep = j, drop = i;
2674 }
2675 } else
fe1c19dc 2676 keep = j, drop = i;
fe1c19dc 2677
e8e9e12c 2678 if (!am_sender) {
951e826b 2679 if (DEBUG_GTE(DUP, 1)) {
a6e7b978
WD
2680 rprintf(FINFO,
2681 "removing duplicate name %s from file list (%d)\n",
ee83e1bd 2682 f_name(file, fbuf), drop + flist->ndx_start);
a6e7b978 2683 }
a6e7b978
WD
2684 clear_file(flist->sorted[drop]);
2685 }
9935066b 2686
fe1c19dc
WD
2687 if (keep == i) {
2688 if (flist->low == drop) {
2689 for (j = drop + 1;
332cf6df 2690 j < i && !F_IS_ACTIVE(flist->sorted[j]);
fe1c19dc
WD
2691 j++) {}
2692 flist->low = j;
2693 }
2694 prev_i = i;
2695 }
728d0922 2696 } else
6931c138 2697 prev_i = i;
3ec4dd97 2698 }
a6e7b978 2699 flist->high = prev_i;
0199b05f 2700
c0b134a4
WD
2701 if (strip_root) {
2702 /* We need to strip off the leading slashes for relative
2703 * paths, but this must be done _after_ the sorting phase. */
2704 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2705 struct file_struct *file = flist->sorted[i];
c0b134a4
WD
2706
2707 if (!file->dirname)
2708 continue;
2709 while (*file->dirname == '/')
2710 file->dirname++;
2711 if (!*file->dirname)
2712 file->dirname = NULL;
2713 }
2714 }
2715
a6e7b978 2716 if (prune_empty_dirs && !am_sender) {
e6ffb966 2717 int j, prev_depth = 0;
9c000f5e 2718
e6ffb966 2719 prev_i = 0; /* It's OK that this isn't really true. */
9c000f5e 2720
f5db0993 2721 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2722 struct file_struct *fp, *file = flist->sorted[i];
ebed4c3a 2723
9d737ecb 2724 /* This temporarily abuses the F_DEPTH() value for a
e6ffb966
WD
2725 * directory that is in a chain that might get pruned.
2726 * We restore the old value if it gets a reprieve. */
9d737ecb 2727 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
e6ffb966 2728 /* Dump empty dirs when coming back down. */
9d737ecb 2729 for (j = prev_depth; j >= F_DEPTH(file); j--) {
332cf6df 2730 fp = flist->sorted[prev_i];
9d737ecb 2731 if (F_DEPTH(fp) >= 0)
e6ffb966 2732 break;
9d737ecb 2733 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2734 clear_file(fp);
9c000f5e 2735 }
9d737ecb 2736 prev_depth = F_DEPTH(file);
85aecef6
WD
2737 if (is_excluded(f_name(file, fbuf), 1,
2738 ALL_FILTERS)) {
e6ffb966
WD
2739 /* Keep dirs through this dir. */
2740 for (j = prev_depth-1; ; j--) {
332cf6df 2741 fp = flist->sorted[prev_i];
9d737ecb 2742 if (F_DEPTH(fp) >= 0)
e6ffb966 2743 break;
9d737ecb
WD
2744 prev_i = -F_DEPTH(fp)-1;
2745 F_DEPTH(fp) = j;
e6ffb966 2746 }
85aecef6 2747 } else
9d737ecb 2748 F_DEPTH(file) = -prev_i-1;
e6ffb966
WD
2749 prev_i = i;
2750 } else {
2751 /* Keep dirs through this non-dir. */
2752 for (j = prev_depth; ; j--) {
332cf6df 2753 fp = flist->sorted[prev_i];
9d737ecb 2754 if (F_DEPTH(fp) >= 0)
e6ffb966 2755 break;
9d737ecb
WD
2756 prev_i = -F_DEPTH(fp)-1;
2757 F_DEPTH(fp) = j;
e6ffb966 2758 }
0199b05f 2759 }
9c000f5e 2760 }
ca947dea 2761 /* Dump all remaining empty dirs. */
e6ffb966 2762 while (1) {
332cf6df 2763 struct file_struct *fp = flist->sorted[prev_i];
9d737ecb 2764 if (F_DEPTH(fp) >= 0)
e6ffb966 2765 break;
9d737ecb 2766 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2767 clear_file(fp);
9c000f5e 2768 }
f5db0993 2769
9c000f5e 2770 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2771 if (F_IS_ACTIVE(flist->sorted[i]))
9c000f5e
WD
2772 break;
2773 }
2774 flist->low = i;
2775 for (i = flist->high; i >= flist->low; i--) {
332cf6df 2776 if (F_IS_ACTIVE(flist->sorted[i]))
9c000f5e 2777 break;
0199b05f 2778 }
9c000f5e 2779 flist->high = i;
0199b05f 2780 }
cefed3e8 2781}
0199b05f 2782
32cbfe7b 2783static void output_flist(struct file_list *flist)
cefed3e8 2784{
f3c3ed44 2785 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 2786 struct file_struct *file;
9d737ecb 2787 const char *root, *dir, *slash, *name, *trail;
32cbfe7b 2788 const char *who = who_am_i();
cefed3e8 2789 int i;
0199b05f 2790
65b4e4b2
WD
2791 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2792 who, flist->ndx_start, flist->used, flist->low, flist->high);
9decb4d2 2793 for (i = 0; i < flist->used; i++) {
2ad3c717 2794 file = flist->files[i];
9b25ef35 2795 if ((am_root || am_sender) && uid_ndx) {
1564cd5a
WD
2796 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2797 F_OWNER(file));
82ad07c4 2798 } else
f05f993e 2799 *uidbuf = '\0';
9b25ef35 2800 if (gid_ndx) {
1564cd5a
WD
2801 static char parens[] = "(\0)\0\0\0";
2802 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2803 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2804 pp, F_GROUP(file), pp + 2);
82ad07c4 2805 } else
f05f993e 2806 *gidbuf = '\0';
f3c3ed44 2807 if (!am_sender)
9d737ecb 2808 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
96293cf9 2809 if (F_IS_ACTIVE(file)) {
ef35abb2 2810 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
96293cf9
WD
2811 if ((dir = file->dirname) == NULL)
2812 dir = slash = "";
2813 else
2814 slash = "/";
c58c1dc4 2815 name = file->basename;
96293cf9
WD
2816 trail = S_ISDIR(file->mode) ? "/" : "";
2817 } else
9d737ecb 2818 root = dir = slash = name = trail = "";
ee83e1bd 2819 rprintf(FINFO,
6d56efa6 2820 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
ee83e1bd
WD
2821 who, i + flist->ndx_start,
2822 root, dir, slash, name, trail,
adc2476f 2823 (int)file->mode, comma_num(F_LENGTH(file)),
ee83e1bd 2824 uidbuf, gidbuf, file->flags);
0199b05f 2825 }
3ec4dd97
AT
2826}
2827
8824e2ce 2828enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 2829enum fnc_type { t_PATH, t_ITEM };
8018edd3 2830
569e6f43
WD
2831static int found_prefix;
2832
2f3cad89 2833/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
2834 * would do if it were operating on the joined strings.
2835 *
2836 * Some differences beginning with protocol_version 29: (1) directory names
2837 * are compared with an assumed trailing slash so that they compare in a
2838 * way that would cause them to sort immediately prior to any content they
2839 * may have; (2) a directory of any name compares after a non-directory of
2840 * any name at the same depth; (3) a directory with name "." compares prior
2841 * to anything else. These changes mean that a directory and a non-dir
2842 * with the same name will not compare as equal (protocol_version >= 29).
2843 *
2844 * The dirname component can be an empty string, but the basename component
2845 * cannot (and never is in the current codebase). The basename component
2846 * may be NULL (for a removed item), in which case it is considered to be
2847 * after any existing item. */
569e6f43 2848int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
3ec4dd97 2849{
8018edd3
WD
2850 int dif;
2851 const uchar *c1, *c2;
1ef00d20 2852 enum fnc_state state1, state2;
f5db0993
WD
2853 enum fnc_type type1, type2;
2854 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3 2855
96293cf9
WD
2856 if (!f1 || !F_IS_ACTIVE(f1)) {
2857 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2858 return 0;
2859 return -1;
2860 }
96293cf9 2861 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2862 return 1;
2863
14698a3a
WD
2864 c1 = (uchar*)f1->dirname;
2865 c2 = (uchar*)f2->dirname;
2866 if (c1 == c2)
2867 c1 = c2 = NULL;
2868 if (!c1) {
f5db0993 2869 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2870 c1 = (const uchar*)f1->basename;
f5db0993
WD
2871 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2872 type1 = t_ITEM;
2873 state1 = s_TRAILING;
2874 c1 = (uchar*)"";
2875 } else
2876 state1 = s_BASE;
f5db0993
WD
2877 } else {
2878 type1 = t_path;
8824e2ce 2879 state1 = s_DIR;
f5db0993 2880 }
14698a3a 2881 if (!c2) {
f5db0993 2882 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2883 c2 = (const uchar*)f2->basename;
f5db0993
WD
2884 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2885 type2 = t_ITEM;
2886 state2 = s_TRAILING;
2887 c2 = (uchar*)"";
2888 } else
2889 state2 = s_BASE;
f5db0993
WD
2890 } else {
2891 type2 = t_path;
8824e2ce 2892 state2 = s_DIR;
f5db0993
WD
2893 }
2894
2895 if (type1 != type2)
2896 return type1 == t_PATH ? 1 : -1;
8018edd3 2897
e4fdf1de 2898 do {
f5db0993 2899 if (!*c1) {
8018edd3 2900 switch (state1) {
8824e2ce
WD
2901 case s_DIR:
2902 state1 = s_SLASH;
e90b8ace 2903 c1 = (uchar*)"/";
8018edd3 2904 break;
8824e2ce 2905 case s_SLASH:
f5db0993 2906 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2907 c1 = (const uchar*)f1->basename;
cbb5fa4f
WD
2908 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2909 type1 = t_ITEM;
2910 state1 = s_TRAILING;
2911 c1 = (uchar*)"";
2912 } else
2913 state1 = s_BASE;
8018edd3 2914 break;
8824e2ce
WD
2915 case s_BASE:
2916 state1 = s_TRAILING;
f5db0993 2917 if (type1 == t_PATH) {
2f3cad89 2918 c1 = (uchar*)"/";
f5db0993
WD
2919 break;
2920 }
2921 /* FALL THROUGH */
8824e2ce 2922 case s_TRAILING:
f5db0993 2923 type1 = t_ITEM;
8018edd3
WD
2924 break;
2925 }
f5db0993
WD
2926 if (*c2 && type1 != type2)
2927 return type1 == t_PATH ? 1 : -1;
8018edd3 2928 }
f5db0993 2929 if (!*c2) {
8018edd3 2930 switch (state2) {
8824e2ce
WD
2931 case s_DIR:
2932 state2 = s_SLASH;
e90b8ace 2933 c2 = (uchar*)"/";
8018edd3 2934 break;
8824e2ce 2935 case s_SLASH:
f5db0993 2936 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2937 c2 = (const uchar*)f2->basename;
cbb5fa4f
WD
2938 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2939 type2 = t_ITEM;
2940 state2 = s_TRAILING;
2941 c2 = (uchar*)"";
2942 } else
2943 state2 = s_BASE;
8018edd3 2944 break;
8824e2ce 2945 case s_BASE:
8824e2ce 2946 state2 = s_TRAILING;
f5db0993 2947 if (type2 == t_PATH) {
2f3cad89 2948 c2 = (uchar*)"/";
f5db0993
WD
2949 break;
2950 }
2951 /* FALL THROUGH */
8824e2ce 2952 case s_TRAILING:
569e6f43 2953 found_prefix = 1;
f5db0993
WD
2954 if (!*c1)
2955 return 0;
2956 type2 = t_ITEM;
8018edd3
WD
2957 break;
2958 }
f5db0993
WD
2959 if (type1 != type2)
2960 return type1 == t_PATH ? 1 : -1;
8018edd3 2961 }
e4fdf1de 2962 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
8018edd3
WD
2963
2964 return dif;
2965}
2966
569e6f43
WD
2967/* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2968 * not match if f2's basename is not an exact match of a path element in f1.
2969 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2970int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2971{
2972 found_prefix = 0;
2973 f_name_cmp(f1, f2);
2974 return found_prefix;
2975}
2976
96293cf9
WD
2977char *f_name_buf(void)
2978{
2979 static char names[5][MAXPATHLEN];
2980 static unsigned int n;
2981
2982 n = (n + 1) % (sizeof names / sizeof names[0]);
2983
2984 return names[n];
2985}
2986
8018edd3 2987/* Return a copy of the full filename of a flist entry, using the indicated
5e4ff5f9
WD
2988 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2989 * done because we checked the size when creating the file_struct entry.
8018edd3 2990 */
569e6f43 2991char *f_name(const struct file_struct *f, char *fbuf)
8018edd3 2992{
96293cf9 2993 if (!f || !F_IS_ACTIVE(f))
ebed4c3a 2994 return NULL;
3ec4dd97 2995
96293cf9
WD
2996 if (!fbuf)
2997 fbuf = f_name_buf();
5e4ff5f9 2998
3ec4dd97 2999 if (f->dirname) {
882e6893
WD
3000 int len = strlen(f->dirname);
3001 memcpy(fbuf, f->dirname, len);
3002 fbuf[len] = '/';
c58c1dc4 3003 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
8018edd3 3004 } else
c58c1dc4 3005 strlcpy(fbuf, f->basename, MAXPATHLEN);
0ee6ca98 3006
b7736c79 3007 return fbuf;
8018edd3 3008}
e03dfae5 3009
32cbfe7b
WD
3010/* Do a non-recursive scan of the named directory, possibly ignoring all
3011 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3012 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3013 * buffer (the functions we call will append names onto the end, but the old
3014 * dir value will be restored on exit). */
263d3bfb 3015struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
37802f40
WD
3016{
3017 struct file_list *dirlist;
3018 char dirbuf[MAXPATHLEN];
37802f40 3019 int save_recurse = recurse;
1661fe9b 3020 int save_xfer_dirs = xfer_dirs;
4635fb99 3021 int save_prune_empty_dirs = prune_empty_dirs;
37802f40 3022
32cbfe7b
WD
3023 if (dlen < 0) {
3024 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3025 if (dlen >= MAXPATHLEN)
3026 return NULL;
3027 dirname = dirbuf;
3028 }
37802f40 3029
c7e6f84f 3030 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
32cbfe7b 3031
37802f40 3032 recurse = 0;
1661fe9b 3033 xfer_dirs = 1;
a6e7b978 3034 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
1661fe9b 3035 xfer_dirs = save_xfer_dirs;
37802f40 3036 recurse = save_recurse;
951e826b 3037 if (INFO_GTE(PROGRESS, 1))
9decb4d2 3038 flist_count_offset += dirlist->used;
37802f40 3039
4635fb99 3040 prune_empty_dirs = 0;
332cf6df 3041 dirlist->sorted = dirlist->files;
65b4e4b2 3042 flist_sort_and_clean(dirlist, 0);
4635fb99 3043 prune_empty_dirs = save_prune_empty_dirs;
564ef546 3044
951e826b 3045 if (DEBUG_GTE(FLIST, 3))
32cbfe7b 3046 output_flist(dirlist);
45478cc7 3047
32cbfe7b 3048 return dirlist;
45478cc7 3049}