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