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