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