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