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