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