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