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