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