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