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