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