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