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