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