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