Can just test ignore_errors w/o lp_ignore_errors(module_id) now.
[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, 2003, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 #include "rsync.h"
25 #include "rounding.h"
26
27 extern int verbose;
28 extern int list_only;
29 extern int am_root;
30 extern int am_server;
31 extern int am_daemon;
32 extern int am_sender;
33 extern int do_progress;
34 extern int always_checksum;
35 extern int module_id;
36 extern int ignore_errors;
37 extern int numeric_ids;
38 extern int recurse;
39 extern int xfer_dirs;
40 extern int filesfrom_fd;
41 extern int one_file_system;
42 extern int copy_dirlinks;
43 extern int keep_dirlinks;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_devices;
47 extern int preserve_specials;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int relative_paths;
51 extern int implied_dirs;
52 extern int file_extra_cnt;
53 extern int ignore_perishable;
54 extern int non_perishable_cnt;
55 extern int prune_empty_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
62
63 extern char curr_dir[MAXPATHLEN];
64
65 extern struct chmod_mode_struct *chmod_modes;
66
67 extern struct filter_list_struct filter_list;
68 extern struct filter_list_struct server_filter_list;
69
70 int io_error;
71 int checksum_len;
72 dev_t filesystem_dev; /* used to implement -x */
73
74 /* The tmp_* vars are used as a cache area by make_file() to store data
75  * that the sender doesn't need to remember in its file list.  The data
76  * will survive just long enough to be used by send_file_entry(). */
77 static dev_t tmp_rdev;
78 #ifdef SUPPORT_HARD_LINKS
79 static int64 tmp_dev, tmp_ino;
80 #endif
81 static char tmp_sum[MD4_SUM_LENGTH];
82
83 static char empty_sum[MD4_SUM_LENGTH];
84 static int flist_count_offset; /* for --delete --progress */
85
86 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
87 static void output_flist(struct file_list *flist);
88
89 void init_flist(void)
90 {
91         if (verbose > 4) {
92                 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
93                         (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
94         }
95         checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
96 }
97
98 static int show_filelist_p(void)
99 {
100         return verbose && xfer_dirs && !am_server;
101 }
102
103 static void start_filelist_progress(char *kind)
104 {
105         rprintf(FCLIENT, "%s ... ", kind);
106         if (verbose > 1 || do_progress)
107                 rprintf(FCLIENT, "\n");
108         rflush(FINFO);
109 }
110
111 static void emit_filelist_progress(int count)
112 {
113         rprintf(FCLIENT, " %d files...\r", count);
114 }
115
116 static void maybe_emit_filelist_progress(int count)
117 {
118         if (do_progress && show_filelist_p() && (count % 100) == 0)
119                 emit_filelist_progress(count);
120 }
121
122 static void finish_filelist_progress(const struct file_list *flist)
123 {
124         if (do_progress) {
125                 /* This overwrites the progress line */
126                 rprintf(FINFO, "%d file%sto consider\n",
127                         flist->count, flist->count == 1 ? " " : "s ");
128         } else
129                 rprintf(FINFO, "done\n");
130 }
131
132 void show_flist_stats(void)
133 {
134         /* Nothing yet */
135 }
136
137 static void list_file_entry(struct file_struct *f)
138 {
139         char permbuf[PERMSTRING_SIZE];
140         double len;
141
142         if (!F_IS_ACTIVE(f)) {
143                 /* this can happen if duplicate names were removed */
144                 return;
145         }
146
147         permstring(permbuf, f->mode);
148         len = F_LENGTH(f);
149
150 #ifdef SUPPORT_LINKS
151         if (preserve_links && S_ISLNK(f->mode)) {
152                 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
153                         permbuf, len, timestring(f->modtime),
154                         f_name(f, NULL), F_SYMLINK(f));
155         } else
156 #endif
157         {
158                 rprintf(FINFO, "%s %11.0f %s %s\n",
159                         permbuf, len, timestring(f->modtime),
160                         f_name(f, NULL));
161         }
162 }
163
164 /* Stat either a symlink or its referent, depending on the settings of
165  * copy_links, copy_unsafe_links, etc.  Returns -1 on error, 0 on success.
166  *
167  * If path is the name of a symlink, then the linkbuf buffer (which must hold
168  * MAXPATHLEN chars) will be set to the symlink's target string.
169  *
170  * The stat structure pointed to by stp will contain information about the
171  * link or the referent as appropriate, if they exist. */
172 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
173 {
174 #ifdef SUPPORT_LINKS
175         if (link_stat(path, stp, copy_dirlinks) < 0)
176                 return -1;
177         if (S_ISLNK(stp->st_mode)) {
178                 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
179                 if (llen < 0)
180                         return -1;
181                 linkbuf[llen] = '\0';
182                 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
183                         if (verbose > 1) {
184                                 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
185                                         path, linkbuf);
186                         }
187                         return do_stat(path, stp);
188                 }
189         }
190         return 0;
191 #else
192         return do_stat(path, stp);
193 #endif
194 }
195
196 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
197 {
198 #ifdef SUPPORT_LINKS
199         if (copy_links)
200                 return do_stat(path, stp);
201         if (do_lstat(path, stp) < 0)
202                 return -1;
203         if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
204                 STRUCT_STAT st;
205                 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
206                         *stp = st;
207         }
208         return 0;
209 #else
210         return do_stat(path, stp);
211 #endif
212 }
213
214 /* This function is used to check if a file should be included/excluded
215  * from the list of files based on its name and type etc.  The value of
216  * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
217 static int is_excluded(char *fname, int is_dir, int filter_level)
218 {
219 #if 0 /* This currently never happens, so avoid a useless compare. */
220         if (filter_level == NO_FILTERS)
221                 return 0;
222 #endif
223         if (fname) {
224                 /* never exclude '.', even if somebody does --exclude '*' */
225                 if (fname[0] == '.' && !fname[1])
226                         return 0;
227                 /* Handle the -R version of the '.' dir. */
228                 if (fname[0] == '/') {
229                         int len = strlen(fname);
230                         if (fname[len-1] == '.' && fname[len-2] == '/')
231                                 return 0;
232                 }
233         }
234         if (server_filter_list.head
235             && check_filter(&server_filter_list, fname, is_dir) < 0)
236                 return 1;
237         if (filter_level != ALL_FILTERS)
238                 return 0;
239         if (filter_list.head
240             && check_filter(&filter_list, fname, is_dir) < 0)
241                 return 1;
242         return 0;
243 }
244
245 static int to_wire_mode(mode_t mode)
246 {
247 #ifdef SUPPORT_LINKS
248 #if _S_IFLNK != 0120000
249         if (S_ISLNK(mode))
250                 return (mode & ~(_S_IFMT)) | 0120000;
251 #endif
252 #endif
253         return mode;
254 }
255
256 static mode_t from_wire_mode(int mode)
257 {
258 #if _S_IFLNK != 0120000
259         if ((mode & (_S_IFMT)) == 0120000)
260                 return (mode & ~(_S_IFMT)) | _S_IFLNK;
261 #endif
262         return mode;
263 }
264
265 static void send_directory(int f, struct file_list *flist,
266                            char *fbuf, int len);
267
268 static const char *flist_dir;
269 static int flist_dir_len;
270
271
272 /**
273  * Make sure @p flist is big enough to hold at least @p flist->count
274  * entries.
275  **/
276 void flist_expand(struct file_list *flist)
277 {
278         struct file_struct **new_ptr;
279
280         if (flist->count < flist->malloced)
281                 return;
282
283         if (flist->malloced < FLIST_START)
284                 flist->malloced = FLIST_START;
285         else if (flist->malloced >= FLIST_LINEAR)
286                 flist->malloced += FLIST_LINEAR;
287         else
288                 flist->malloced *= 2;
289
290         /*
291          * In case count jumped or we are starting the list
292          * with a known size just set it.
293          */
294         if (flist->malloced < flist->count)
295                 flist->malloced = flist->count;
296
297         new_ptr = realloc_array(flist->files, struct file_struct *,
298                                 flist->malloced);
299
300         if (verbose >= 2 && flist->malloced != FLIST_START) {
301                 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
302                     who_am_i(),
303                     (double)sizeof flist->files[0] * flist->malloced,
304                     (new_ptr == flist->files) ? " not" : "");
305         }
306
307         flist->files = new_ptr;
308
309         if (!flist->files)
310                 out_of_memory("flist_expand");
311 }
312
313 static void send_file_entry(struct file_struct *file, int f, int ndx)
314 {
315         static time_t modtime;
316         static mode_t mode;
317         static int64 dev;
318         static dev_t rdev;
319         static uint32 rdev_major;
320         static uid_t uid;
321         static gid_t gid;
322         static char lastname[MAXPATHLEN];
323         char fname[MAXPATHLEN];
324         int first_hlink_ndx = -1;
325         int l1, l2;
326         int flags;
327
328         f_name(file, fname);
329
330         flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
331
332         if (file->mode == mode)
333                 flags |= XMIT_SAME_MODE;
334         else
335                 mode = file->mode;
336         if ((preserve_devices && IS_DEVICE(mode))
337          || (preserve_specials && IS_SPECIAL(mode))) {
338                 if (protocol_version < 28) {
339                         if (tmp_rdev == rdev)
340                                 flags |= XMIT_SAME_RDEV_pre28;
341                         else
342                                 rdev = tmp_rdev;
343                 } else {
344                         rdev = tmp_rdev;
345                         if ((uint32)major(rdev) == rdev_major)
346                                 flags |= XMIT_SAME_RDEV_MAJOR;
347                         else
348                                 rdev_major = major(rdev);
349                         if ((uint32)minor(rdev) <= 0xFFu)
350                                 flags |= XMIT_RDEV_MINOR_IS_SMALL;
351                 }
352         } else if (protocol_version < 28)
353                 rdev = MAKEDEV(0, 0);
354         if (preserve_uid) {
355                 if (F_UID(file) == uid)
356                         flags |= XMIT_SAME_UID;
357                 else
358                         uid = F_UID(file);
359         }
360         if (preserve_gid) {
361                 if (F_GID(file) == gid)
362                         flags |= XMIT_SAME_GID;
363                 else
364                         gid = F_GID(file);
365         }
366         if (file->modtime == modtime)
367                 flags |= XMIT_SAME_TIME;
368         else
369                 modtime = file->modtime;
370
371 #ifdef SUPPORT_HARD_LINKS
372         if (tmp_dev != 0) {
373                 if (protocol_version >= 30) {
374                         struct idev_node *np = idev_node(tmp_dev, tmp_ino);
375                         first_hlink_ndx = (int32)np->data - 1;
376                         if (first_hlink_ndx < 0) {
377                                 np->data = (void*)(ndx + 1);
378                                 flags |= XMIT_HLINK_FIRST;
379                         }
380                         flags |= XMIT_HLINKED;
381                 } else {
382                         if (tmp_dev == dev) {
383                                 if (protocol_version >= 28)
384                                         flags |= XMIT_SAME_DEV_pre30;
385                         } else
386                                 dev = tmp_dev;
387                         flags |= XMIT_HLINKED;
388                 }
389         }
390 #endif
391
392         for (l1 = 0;
393             lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
394             l1++) {}
395         l2 = strlen(fname+l1);
396
397         if (l1 > 0)
398                 flags |= XMIT_SAME_NAME;
399         if (l2 > 255)
400                 flags |= XMIT_LONG_NAME;
401
402         /* We must make sure we don't send a zero flag byte or the
403          * other end will terminate the flist transfer.  Note that
404          * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
405          * it's harmless way to add a bit to the first flag byte. */
406         if (protocol_version >= 28) {
407                 if (!flags && !S_ISDIR(mode))
408                         flags |= XMIT_TOP_DIR;
409                 if ((flags & 0xFF00) || !flags) {
410                         flags |= XMIT_EXTENDED_FLAGS;
411                         write_shortint(f, flags);
412                 } else
413                         write_byte(f, flags);
414         } else {
415                 if (!(flags & 0xFF))
416                         flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
417                 write_byte(f, flags);
418         }
419         if (flags & XMIT_SAME_NAME)
420                 write_byte(f, l1);
421         if (flags & XMIT_LONG_NAME)
422                 write_int(f, l2);
423         else
424                 write_byte(f, l2);
425         write_buf(f, fname + l1, l2);
426
427         if (first_hlink_ndx >= 0) {
428                 write_int(f, first_hlink_ndx);
429                 goto the_end;
430         }
431
432         write_longint(f, F_LENGTH(file));
433         if (!(flags & XMIT_SAME_TIME))
434                 write_int(f, modtime);
435         if (!(flags & XMIT_SAME_MODE))
436                 write_int(f, to_wire_mode(mode));
437         if (preserve_uid && !(flags & XMIT_SAME_UID)) {
438                 if (!numeric_ids)
439                         add_uid(uid);
440                 write_int(f, uid);
441         }
442         if (preserve_gid && !(flags & XMIT_SAME_GID)) {
443                 if (!numeric_ids)
444                         add_gid(gid);
445                 write_int(f, gid);
446         }
447         if ((preserve_devices && IS_DEVICE(mode))
448          || (preserve_specials && IS_SPECIAL(mode))) {
449                 if (protocol_version < 28) {
450                         if (!(flags & XMIT_SAME_RDEV_pre28))
451                                 write_int(f, (int)rdev);
452                 } else {
453                         if (!(flags & XMIT_SAME_RDEV_MAJOR))
454                                 write_int(f, major(rdev));
455                         if (flags & XMIT_RDEV_MINOR_IS_SMALL)
456                                 write_byte(f, minor(rdev));
457                         else
458                                 write_int(f, minor(rdev));
459                 }
460         }
461
462 #ifdef SUPPORT_LINKS
463         if (preserve_links && S_ISLNK(mode)) {
464                 const char *sl = F_SYMLINK(file);
465                 int len = strlen(sl);
466                 write_int(f, len);
467                 write_buf(f, sl, len);
468         }
469 #endif
470
471 #ifdef SUPPORT_HARD_LINKS
472         if (tmp_dev != 0 && protocol_version < 30) {
473                 if (protocol_version < 26) {
474                         /* 32-bit dev_t and ino_t */
475                         write_int(f, (int32)dev);
476                         write_int(f, (int32)tmp_ino);
477                 } else {
478                         /* 64-bit dev_t and ino_t */
479                         if (!(flags & XMIT_SAME_DEV_pre30))
480                                 write_longint(f, dev);
481                         write_longint(f, tmp_ino);
482                 }
483         }
484 #endif
485
486         if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
487                 const char *sum;
488                 if (S_ISREG(mode))
489                         sum = tmp_sum;
490                 else {
491                         /* Prior to 28, we sent a useless set of nulls. */
492                         sum = empty_sum;
493                 }
494                 write_buf(f, sum, checksum_len);
495         }
496
497   the_end:
498         strlcpy(lastname, fname, MAXPATHLEN);
499 }
500
501 static struct file_struct *recv_file_entry(struct file_list *flist,
502                                            int flags, int f)
503 {
504         static time_t modtime;
505         static mode_t mode;
506         static int64 dev;
507         static dev_t rdev;
508         static uint32 rdev_major;
509         static uid_t uid;
510         static gid_t gid;
511         static char lastname[MAXPATHLEN], *lastdir;
512         static int lastdir_depth, lastdir_len = -1;
513         static unsigned int del_hier_name_len = 0;
514         static int in_del_hier = 0;
515         char thisname[MAXPATHLEN];
516         unsigned int l1 = 0, l2 = 0;
517         int alloc_len, basename_len, dirname_len, linkname_len;
518         int extra_len = file_extra_cnt * EXTRA_LEN;
519         int first_hlink_ndx = -1;
520         OFF_T file_length;
521         char *basename, *dirname, *bp;
522         struct file_struct *file;
523
524         if (!flist) {
525                 modtime = 0, mode = 0;
526                 dev = 0, rdev = MAKEDEV(0, 0);
527                 rdev_major = 0;
528                 uid = 0, gid = 0;
529                 *lastname = '\0';
530                 lastdir_len = -1;
531                 in_del_hier = 0;
532                 return NULL;
533         }
534
535         if (flags & XMIT_SAME_NAME)
536                 l1 = read_byte(f);
537
538         if (flags & XMIT_LONG_NAME)
539                 l2 = read_int(f);
540         else
541                 l2 = read_byte(f);
542
543         if (l2 >= MAXPATHLEN - l1) {
544                 rprintf(FERROR,
545                         "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
546                         flags, l1, l2, lastname, who_am_i());
547                 overflow_exit("recv_file_entry");
548         }
549
550         strlcpy(thisname, lastname, l1 + 1);
551         read_sbuf(f, &thisname[l1], l2);
552         thisname[l1 + l2] = 0;
553
554         strlcpy(lastname, thisname, MAXPATHLEN);
555
556         clean_fname(thisname, 0);
557
558         if (sanitize_paths)
559                 sanitize_path(thisname, thisname, "", 0, NULL);
560
561         if ((basename = strrchr(thisname, '/')) != NULL) {
562                 dirname_len = ++basename - thisname; /* counts future '\0' */
563                 if (lastdir_len == dirname_len - 1
564                     && strncmp(thisname, lastdir, lastdir_len) == 0) {
565                         dirname = lastdir;
566                         dirname_len = 0; /* indicates no copy is needed */
567                 } else
568                         dirname = thisname;
569         } else {
570                 basename = thisname;
571                 dirname = NULL;
572                 dirname_len = 0;
573         }
574         basename_len = strlen(basename) + 1; /* count the '\0' */
575
576 #ifdef SUPPORT_HARD_LINKS
577         if (protocol_version >= 30
578          && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
579                 struct file_struct *first;
580                 first_hlink_ndx = read_int(f);
581                 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
582                         rprintf(FERROR,
583                                 "hard-link reference out of range: %d (%d)\n",
584                                 first_hlink_ndx, flist->count);
585                         exit_cleanup(RERR_PROTOCOL);
586                 }
587                 first = flist->files[first_hlink_ndx];
588                 file_length = F_LENGTH(first);
589                 modtime = first->modtime;
590                 mode = first->mode;
591                 if (preserve_uid)
592                         uid = F_UID(first);
593                 if (preserve_gid)
594                         gid = F_GID(first);
595                 if ((preserve_devices && IS_DEVICE(mode))
596                  || (preserve_specials && IS_SPECIAL(mode))) {
597                         uint32 *devp = F_RDEV_P(first);
598                         rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
599                 }
600                 if (preserve_links && S_ISLNK(mode))
601                         linkname_len = strlen(F_SYMLINK(first)) + 1;
602                 else
603                         linkname_len = 0;
604                 goto create_object;
605         }
606 #endif
607
608         file_length = read_longint(f);
609         if (!(flags & XMIT_SAME_TIME))
610                 modtime = (time_t)read_int(f);
611         if (!(flags & XMIT_SAME_MODE))
612                 mode = from_wire_mode(read_int(f));
613
614         if (chmod_modes && !S_ISLNK(mode))
615                 mode = tweak_mode(mode, chmod_modes);
616
617         if (preserve_uid && !(flags & XMIT_SAME_UID))
618                 uid = (uid_t)read_int(f);
619         if (preserve_gid && !(flags & XMIT_SAME_GID))
620                 gid = (gid_t)read_int(f);
621
622         if ((preserve_devices && IS_DEVICE(mode))
623          || (preserve_specials && IS_SPECIAL(mode))) {
624                 if (protocol_version < 28) {
625                         if (!(flags & XMIT_SAME_RDEV_pre28))
626                                 rdev = (dev_t)read_int(f);
627                 } else {
628                         uint32 rdev_minor;
629                         if (!(flags & XMIT_SAME_RDEV_MAJOR))
630                                 rdev_major = read_int(f);
631                         if (flags & XMIT_RDEV_MINOR_IS_SMALL)
632                                 rdev_minor = read_byte(f);
633                         else
634                                 rdev_minor = read_int(f);
635                         rdev = MAKEDEV(rdev_major, rdev_minor);
636                 }
637                 extra_len += 2 * EXTRA_LEN;
638                 file_length = 0;
639         } else if (protocol_version < 28)
640                 rdev = MAKEDEV(0, 0);
641
642 #ifdef SUPPORT_LINKS
643         if (preserve_links && S_ISLNK(mode)) {
644                 linkname_len = read_int(f) + 1; /* count the '\0' */
645                 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
646                         rprintf(FERROR, "overflow: linkname_len=%d\n",
647                                 linkname_len - 1);
648                         overflow_exit("recv_file_entry");
649                 }
650         }
651         else
652 #endif
653                 linkname_len = 0;
654
655 #ifdef SUPPORT_HARD_LINKS
656   create_object:
657         if (preserve_hard_links) {
658                 if (protocol_version < 28 && S_ISREG(mode))
659                         flags |= XMIT_HLINKED;
660                 if (flags & XMIT_HLINKED)
661                         extra_len += EXTRA_LEN;
662         }
663 #endif
664
665         if (always_checksum && S_ISREG(mode))
666                 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
667
668         if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
669                 extra_len += EXTRA_LEN;
670
671 #if EXTRA_ROUNDING > 0
672         if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
673                 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
674 #endif
675
676         alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
677                   + linkname_len;
678         bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
679
680         memset(bp, 0, extra_len + FILE_STRUCT_LEN);
681         bp += extra_len;
682         file = (struct file_struct *)bp;
683         bp += FILE_STRUCT_LEN;
684
685         memcpy(bp, basename, basename_len);
686         bp += basename_len + linkname_len; /* skip space for symlink too */
687
688 #ifdef SUPPORT_HARD_LINKS
689         if (flags & XMIT_HLINKED)
690                 file->flags |= FLAG_HLINKED;
691 #endif
692         file->modtime = modtime;
693         file->len32 = (uint32)file_length;
694         if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
695                 file->flags |= FLAG_LENGTH64;
696                 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
697         }
698         file->mode = mode;
699         if (preserve_uid)
700                 F_UID(file) = uid;
701         if (preserve_gid)
702                 F_GID(file) = gid;
703
704         if (dirname_len) {
705                 file->dirname = lastdir = bp;
706                 lastdir_len = dirname_len - 1;
707                 memcpy(bp, dirname, dirname_len - 1);
708                 bp += dirname_len;
709                 bp[-1] = '\0';
710                 lastdir_depth = count_dir_elements(lastdir);
711                 F_DEPTH(file) = lastdir_depth + 1;
712         } else if (dirname) {
713                 file->dirname = dirname; /* we're reusing lastname */
714                 F_DEPTH(file) = lastdir_depth + 1;
715         } else
716                 F_DEPTH(file) = 1;
717
718         if (S_ISDIR(mode)) {
719                 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
720                         F_DEPTH(file)--;
721                 if (flags & XMIT_TOP_DIR) {
722                         in_del_hier = recurse;
723                         del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
724                         if (relative_paths && del_hier_name_len > 2
725                             && lastname[del_hier_name_len-1] == '.'
726                             && lastname[del_hier_name_len-2] == '/')
727                                 del_hier_name_len -= 2;
728                         file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
729                 } else if (in_del_hier) {
730                         if (!relative_paths || !del_hier_name_len
731                          || (l1 >= del_hier_name_len
732                           && lastname[del_hier_name_len] == '/'))
733                                 file->flags |= FLAG_XFER_DIR;
734                         else
735                                 in_del_hier = 0;
736                 }
737         }
738
739         if ((preserve_devices && IS_DEVICE(mode))
740          || (preserve_specials && IS_SPECIAL(mode))) {
741                 uint32 *devp = F_RDEV_P(file);
742                 DEV_MAJOR(devp) = major(rdev);
743                 DEV_MINOR(devp) = minor(rdev);
744         }
745
746 #ifdef SUPPORT_LINKS
747         if (linkname_len) {
748                 bp = (char*)file->basename + basename_len;
749                 if (first_hlink_ndx >= 0) {
750                         struct file_struct *first = flist->files[first_hlink_ndx];
751                         memcpy(bp, F_SYMLINK(first), linkname_len);
752                 } else
753                         read_sbuf(f, bp, linkname_len - 1);
754                 if (sanitize_paths)
755                         sanitize_path(bp, bp, "", lastdir_depth, NULL);
756         }
757 #endif
758
759 #ifdef SUPPORT_HARD_LINKS
760         if (preserve_hard_links && flags & XMIT_HLINKED) {
761                 if (protocol_version >= 30) {
762                         F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
763                                         ? flist->count : first_hlink_ndx;
764                 } else {
765                         static int32 cnt = 0;
766                         struct idev_node *np;
767                         int64 ino;
768                         int32 ndx;
769                         if (protocol_version < 26) {
770                                 dev = read_int(f);
771                                 ino = read_int(f);
772                         } else {
773                                 if (!(flags & XMIT_SAME_DEV_pre30))
774                                         dev = read_longint(f);
775                                 ino = read_longint(f);
776                         }
777                         np = idev_node(dev, ino);
778                         ndx = (int32)np->data - 1;
779                         if (ndx < 0) {
780                                 ndx = cnt++;
781                                 np->data = (void*)cnt;
782                         }
783                         F_HL_GNUM(file) = ndx;
784                 }
785         }
786 #endif
787
788         if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
789                 if (S_ISREG(mode))
790                         bp = (char*)F_SUM(file);
791                 else {
792                         /* Prior to 28, we get a useless set of nulls. */
793                         bp = tmp_sum;
794                 }
795                 if (first_hlink_ndx >= 0) {
796                         struct file_struct *first = flist->files[first_hlink_ndx];
797                         memcpy(bp, F_SUM(first), checksum_len);
798                 } else
799                         read_buf(f, bp, checksum_len);
800         }
801
802         return file;
803 }
804
805 /**
806  * Create a file_struct for a named file by reading its stat()
807  * information and performing extensive checks against global
808  * options.
809  *
810  * @return the new file, or NULL if there was an error or this file
811  * should be excluded.
812  *
813  * @todo There is a small optimization opportunity here to avoid
814  * stat()ing the file in some circumstances, which has a certain cost.
815  * We are called immediately after doing readdir(), and so we may
816  * already know the d_type of the file.  We could for example avoid
817  * statting directories if we're not recursing, but this is not a very
818  * important case.  Some systems may not have d_type.
819  **/
820 struct file_struct *make_file(const char *fname, struct file_list *flist,
821                               STRUCT_STAT *stp, int flags, int filter_level)
822 {
823         static char *lastdir;
824         static int lastdir_len = -1;
825         struct file_struct *file;
826         STRUCT_STAT st;
827         char thisname[MAXPATHLEN];
828         char linkname[MAXPATHLEN];
829         int alloc_len, basename_len, dirname_len, linkname_len;
830         int extra_len = file_extra_cnt * EXTRA_LEN;
831         char *basename, *dirname, *bp;
832
833         if (!flist || !flist->count)    /* Ignore lastdir when invalid. */
834                 lastdir_len = -1;
835
836         if (strlcpy(thisname, fname, sizeof thisname)
837             >= sizeof thisname - flist_dir_len) {
838                 rprintf(FINFO, "skipping overly long name: %s\n", fname);
839                 return NULL;
840         }
841         clean_fname(thisname, 0);
842         if (sanitize_paths)
843                 sanitize_path(thisname, thisname, "", 0, NULL);
844
845         if (stp && S_ISDIR(stp->st_mode)) {
846                 st = *stp; /* Needed for "symlink/." with --relative. */
847                 *linkname = '\0'; /* make IBM code checker happy */
848         } else if (readlink_stat(thisname, &st, linkname) != 0) {
849                 int save_errno = errno;
850                 /* See if file is excluded before reporting an error. */
851                 if (filter_level != NO_FILTERS
852                  && (is_excluded(thisname, 0, filter_level)
853                   || is_excluded(thisname, 1, filter_level))) {
854                         if (ignore_perishable && save_errno != ENOENT)
855                                 non_perishable_cnt++;
856                         return NULL;
857                 }
858                 if (save_errno == ENOENT) {
859 #ifdef SUPPORT_LINKS
860                         /* Avoid "vanished" error if symlink points nowhere. */
861                         if (copy_links && do_lstat(thisname, &st) == 0
862                             && S_ISLNK(st.st_mode)) {
863                                 io_error |= IOERR_GENERAL;
864                                 rprintf(FERROR, "symlink has no referent: %s\n",
865                                         full_fname(thisname));
866                         } else
867 #endif
868                         {
869                                 enum logcode c = am_daemon && protocol_version < 28
870                                     ? FERROR : FINFO;
871                                 io_error |= IOERR_VANISHED;
872                                 rprintf(c, "file has vanished: %s\n",
873                                         full_fname(thisname));
874                         }
875                 } else {
876                         io_error |= IOERR_GENERAL;
877                         rsyserr(FERROR, save_errno, "readlink %s failed",
878                                 full_fname(thisname));
879                 }
880                 return NULL;
881         }
882
883         /* backup.c calls us with filter_level set to NO_FILTERS. */
884         if (filter_level == NO_FILTERS)
885                 goto skip_filters;
886
887         if (S_ISDIR(st.st_mode) && !xfer_dirs) {
888                 rprintf(FINFO, "skipping directory %s\n", thisname);
889                 return NULL;
890         }
891
892         /* -x only affects directories because we need to avoid recursing
893          * into a mount-point directory, not to avoid copying a symlinked
894          * file if -L (or similar) was specified. */
895         if (one_file_system && st.st_dev != filesystem_dev
896          && S_ISDIR(st.st_mode)) {
897                 if (one_file_system > 1) {
898                         if (verbose > 2) {
899                                 rprintf(FINFO, "skipping mount-point dir %s\n",
900                                         thisname);
901                         }
902                         return NULL;
903                 }
904                 flags |= FLAG_MOUNT_DIR;
905         }
906
907         if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
908                 if (ignore_perishable)
909                         non_perishable_cnt++;
910                 return NULL;
911         }
912
913         if (lp_ignore_nonreadable(module_id)) {
914 #ifdef SUPPORT_LINKS
915                 if (!S_ISLNK(st.st_mode))
916 #endif
917                         if (access(thisname, R_OK) != 0)
918                                 return NULL;
919         }
920
921   skip_filters:
922
923         if (verbose > 2) {
924                 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
925                         who_am_i(), thisname, filter_level);
926         }
927
928         if ((basename = strrchr(thisname, '/')) != NULL) {
929                 dirname_len = ++basename - thisname; /* counts future '\0' */
930                 if (lastdir_len == dirname_len - 1
931                     && strncmp(thisname, lastdir, lastdir_len) == 0) {
932                         dirname = lastdir;
933                         dirname_len = 0; /* indicates no copy is needed */
934                 } else
935                         dirname = thisname;
936         } else {
937                 basename = thisname;
938                 dirname = NULL;
939                 dirname_len = 0;
940         }
941         basename_len = strlen(basename) + 1; /* count the '\0' */
942
943 #ifdef SUPPORT_LINKS
944         linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
945 #else
946         linkname_len = 0;
947 #endif
948
949         if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
950                 extra_len += EXTRA_LEN;
951
952 #if EXTRA_ROUNDING > 0
953         if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
954                 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
955 #endif
956
957         alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
958                   + linkname_len;
959         if (flist)
960                 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
961         else {
962                 if (!(bp = new_array(char, alloc_len)))
963                         out_of_memory("make_file");
964         }
965
966         memset(bp, 0, extra_len + FILE_STRUCT_LEN);
967         bp += extra_len;
968         file = (struct file_struct *)bp;
969         bp += FILE_STRUCT_LEN;
970
971         memcpy(bp, basename, basename_len);
972         bp += basename_len + linkname_len; /* skip space for symlink too */
973
974 #ifdef SUPPORT_HARD_LINKS
975         if (preserve_hard_links && flist) {
976                 if (protocol_version >= 28
977                  ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
978                  : S_ISREG(st.st_mode)) {
979                         tmp_dev = st.st_dev;
980                         tmp_ino = st.st_ino;
981                 } else
982                         tmp_dev = 0;
983         }
984 #endif
985
986 #ifdef HAVE_STRUCT_STAT_ST_RDEV
987         if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
988                 tmp_rdev = st.st_rdev;
989                 st.st_size = 0;
990         }
991 #endif
992
993         file->flags = flags;
994         file->modtime = st.st_mtime;
995         file->len32 = (uint32)st.st_size;
996         if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
997                 file->flags |= FLAG_LENGTH64;
998                 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
999         }
1000         file->mode = st.st_mode;
1001         if (preserve_uid)
1002                 F_UID(file) = st.st_uid;
1003         if (preserve_gid)
1004                 F_GID(file) = st.st_gid;
1005
1006         if (dirname_len) {
1007                 file->dirname = lastdir = bp;
1008                 lastdir_len = dirname_len - 1;
1009                 memcpy(bp, dirname, dirname_len - 1);
1010                 bp += dirname_len;
1011                 bp[-1] = '\0';
1012         } else if (dirname)
1013                 file->dirname = dirname;
1014
1015 #ifdef SUPPORT_LINKS
1016         if (linkname_len) {
1017                 bp = (char*)file->basename + basename_len;
1018                 memcpy(bp, linkname, linkname_len);
1019         }
1020 #endif
1021
1022         if (always_checksum && am_sender && S_ISREG(st.st_mode))
1023                 file_checksum(thisname, tmp_sum, st.st_size);
1024
1025         F_ROOTDIR(file) = flist_dir;
1026
1027         /* This code is only used by the receiver when it is building
1028          * a list of files for a delete pass. */
1029         if (keep_dirlinks && linkname_len && flist) {
1030                 STRUCT_STAT st2;
1031                 int save_mode = file->mode;
1032                 file->mode = S_IFDIR; /* Find a directory with our name. */
1033                 if (flist_find(the_file_list, file) >= 0
1034                     && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1035                         file->modtime = st2.st_mtime;
1036                         file->len32 = 0;
1037                         file->mode = st2.st_mode;
1038                         if (preserve_uid)
1039                                 F_UID(file) = st2.st_uid;
1040                         if (preserve_gid)
1041                                 F_GID(file) = st2.st_gid;
1042                 } else
1043                         file->mode = save_mode;
1044         }
1045
1046         if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
1047                 stats.total_size += st.st_size;
1048
1049         if (basename_len == 0+1)
1050                 return NULL;
1051
1052         return file;
1053 }
1054
1055 /* Only called for temporary file_struct entries created by make_file(). */
1056 void unmake_file(struct file_struct *file)
1057 {
1058         int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1059 #if EXTRA_ROUNDING > 0
1060         if (extra_cnt & EXTRA_ROUNDING)
1061                 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1062 #endif
1063         free(REQ_EXTRA(file, extra_cnt));
1064 }
1065
1066 static struct file_struct *send_file_name(int f, struct file_list *flist,
1067                                           char *fname, STRUCT_STAT *stp,
1068                                           int flags)
1069 {
1070         struct file_struct *file;
1071
1072         file = make_file(fname, flist, stp, flags,
1073                          f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1074         if (!file)
1075                 return NULL;
1076
1077         if (chmod_modes && !S_ISLNK(file->mode))
1078                 file->mode = tweak_mode(file->mode, chmod_modes);
1079
1080         maybe_emit_filelist_progress(flist->count + flist_count_offset);
1081
1082         flist_expand(flist);
1083         flist->files[flist->count++] = file;
1084         if (f >= 0)
1085                 send_file_entry(file, f, flist->count - 1);
1086         return file;
1087 }
1088
1089 static void send_if_directory(int f, struct file_list *flist,
1090                               struct file_struct *file,
1091                               char *fbuf, unsigned int ol)
1092 {
1093         char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1094
1095         if (S_ISDIR(file->mode)
1096             && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1097                 void *save_filters;
1098                 unsigned int len = strlen(fbuf);
1099                 if (len > 1 && fbuf[len-1] == '/')
1100                         fbuf[--len] = '\0';
1101                 if (len >= MAXPATHLEN - 1) {
1102                         io_error |= IOERR_GENERAL;
1103                         rprintf(FERROR, "skipping long-named directory: %s\n",
1104                                 full_fname(fbuf));
1105                         return;
1106                 }
1107                 save_filters = push_local_filters(fbuf, len);
1108                 send_directory(f, flist, fbuf, len);
1109                 pop_local_filters(save_filters);
1110                 fbuf[ol] = '\0';
1111                 if (is_dot_dir)
1112                         fbuf[ol-1] = '.';
1113         }
1114 }
1115
1116 /* This function is normally called by the sender, but the receiving side also
1117  * calls it from get_dirlist() with f set to -1 so that we just construct the
1118  * file list in memory without sending it over the wire.  Also, get_dirlist()
1119  * might call this with f set to -2, which also indicates that local filter
1120  * rules should be ignored. */
1121 static void send_directory(int f, struct file_list *flist, char *fbuf, int len)
1122 {
1123         struct dirent *di;
1124         unsigned remainder;
1125         char *p;
1126         DIR *d;
1127         int start = flist->count;
1128
1129         if (!(d = opendir(fbuf))) {
1130                 io_error |= IOERR_GENERAL;
1131                 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1132                 return;
1133         }
1134
1135         p = fbuf + len;
1136         if (len != 1 || *fbuf != '/')
1137                 *p++ = '/';
1138         *p = '\0';
1139         remainder = MAXPATHLEN - (p - fbuf);
1140
1141         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1142                 char *dname = d_name(di);
1143                 if (dname[0] == '.' && (dname[1] == '\0'
1144                     || (dname[1] == '.' && dname[2] == '\0')))
1145                         continue;
1146                 if (strlcpy(p, dname, remainder) >= remainder) {
1147                         io_error |= IOERR_GENERAL;
1148                         rprintf(FINFO,
1149                                 "cannot send long-named file %s\n",
1150                                 full_fname(fbuf));
1151                         continue;
1152                 }
1153
1154                 send_file_name(f, flist, fbuf, NULL, 0);
1155         }
1156
1157         fbuf[len] = '\0';
1158
1159         if (errno) {
1160                 io_error |= IOERR_GENERAL;
1161                 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1162         }
1163
1164         closedir(d);
1165
1166         if (recurse) {
1167                 int i, end = flist->count - 1;
1168                 /* send_if_directory() bumps flist->count, so use "end". */
1169                 for (i = start; i <= end; i++)
1170                         send_if_directory(f, flist, flist->files[i], fbuf, len);
1171         }
1172 }
1173
1174 struct file_list *send_file_list(int f, int argc, char *argv[])
1175 {
1176         int len;
1177         STRUCT_STAT st;
1178         char *p, *dir, olddir[sizeof curr_dir];
1179         char lastpath[MAXPATHLEN] = "";
1180         struct file_list *flist;
1181         struct timeval start_tv, end_tv;
1182         int64 start_write;
1183         int use_ff_fd = 0;
1184
1185         rprintf(FLOG, "building file list\n");
1186         if (show_filelist_p())
1187                 start_filelist_progress("building file list");
1188
1189         start_write = stats.total_written;
1190         gettimeofday(&start_tv, NULL);
1191
1192 #ifdef SUPPORT_HARD_LINKS
1193         if (preserve_hard_links && protocol_version >= 30)
1194                 init_hard_links();
1195 #endif
1196
1197         flist = flist_new("send_file_list");
1198
1199         io_start_buffering_out();
1200         if (filesfrom_fd >= 0) {
1201                 if (argv[0] && !push_dir(argv[0], 0)) {
1202                         rsyserr(FERROR, errno, "push_dir %s failed",
1203                                 full_fname(argv[0]));
1204                         exit_cleanup(RERR_FILESELECT);
1205                 }
1206                 use_ff_fd = 1;
1207         }
1208
1209         while (1) {
1210                 char fbuf[MAXPATHLEN];
1211                 char *fn;
1212                 int is_dot_dir;
1213
1214                 if (use_ff_fd) {
1215                         if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1216                                 break;
1217                         sanitize_path(fbuf, fbuf, "", 0, NULL);
1218                 } else {
1219                         if (argc-- == 0)
1220                                 break;
1221                         strlcpy(fbuf, *argv++, MAXPATHLEN);
1222                         if (sanitize_paths)
1223                                 sanitize_path(fbuf, fbuf, "", 0, NULL);
1224                 }
1225
1226                 len = strlen(fbuf);
1227                 if (relative_paths) {
1228                         /* We clean up fbuf below. */
1229                         is_dot_dir = 0;
1230                 } else if (!len || fbuf[len - 1] == '/') {
1231                         if (len == 2 && fbuf[0] == '.') {
1232                                 /* Turn "./" into just "." rather than "./." */
1233                                 fbuf[1] = '\0';
1234                         } else {
1235                                 if (len + 1 >= MAXPATHLEN)
1236                                         overflow_exit("send_file_list");
1237                                 fbuf[len++] = '.';
1238                                 fbuf[len] = '\0';
1239                         }
1240                         is_dot_dir = 1;
1241                 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1242                     && (len == 2 || fbuf[len-3] == '/')) {
1243                         if (len + 2 >= MAXPATHLEN)
1244                                 overflow_exit("send_file_list");
1245                         fbuf[len++] = '/';
1246                         fbuf[len++] = '.';
1247                         fbuf[len] = '\0';
1248                         is_dot_dir = 1;
1249                 } else {
1250                         is_dot_dir = fbuf[len-1] == '.'
1251                                    && (len == 1 || fbuf[len-2] == '/');
1252                 }
1253
1254                 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1255                         io_error |= IOERR_GENERAL;
1256                         rsyserr(FERROR, errno, "link_stat %s failed",
1257                                 full_fname(fbuf));
1258                         continue;
1259                 }
1260
1261                 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1262                         rprintf(FINFO, "skipping directory %s\n", fbuf);
1263                         continue;
1264                 }
1265
1266                 dir = NULL;
1267                 olddir[0] = '\0';
1268
1269                 if (!relative_paths) {
1270                         p = strrchr(fbuf, '/');
1271                         if (p) {
1272                                 *p = '\0';
1273                                 if (p == fbuf)
1274                                         dir = "/";
1275                                 else
1276                                         dir = fbuf;
1277                                 len -= p - fbuf + 1;
1278                                 fn = p + 1;
1279                         } else
1280                                 fn = fbuf;
1281                 } else {
1282                         if ((p = strstr(fbuf, "/./")) != NULL) {
1283                                 *p = '\0';
1284                                 if (p == fbuf)
1285                                         dir = "/";
1286                                 else
1287                                         dir = fbuf;
1288                                 len -= p - fbuf + 3;
1289                                 fn = p + 3;
1290                         } else
1291                                 fn = fbuf;
1292                         /* Get rid of trailing "/" and "/.". */
1293                         while (len) {
1294                                 if (fn[len - 1] == '/') {
1295                                         is_dot_dir = 1;
1296                                         if (!--len && !dir) {
1297                                                 len++;
1298                                                 break;
1299                                         }
1300                                 }
1301                                 else if (len >= 2 && fn[len - 1] == '.'
1302                                                   && fn[len - 2] == '/') {
1303                                         is_dot_dir = 1;
1304                                         if (!(len -= 2) && !dir) {
1305                                                 len++;
1306                                                 break;
1307                                         }
1308                                 } else
1309                                         break;
1310                         }
1311                         if (len == 1 && fn[0] == '/')
1312                                 fn[len++] = '.';
1313                         fn[len] = '\0';
1314                         /* Reject a ".." dir in the active part of the path. */
1315                         for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1316                                 if ((p[2] == '/' || p[2] == '\0')
1317                                  && (p == fn || p[-1] == '/')) {
1318                                         rprintf(FERROR,
1319                                             "found \"..\" dir in relative path: %s\n",
1320                                             fbuf);
1321                                         exit_cleanup(RERR_SYNTAX);
1322                                 }
1323                         }
1324                 }
1325
1326                 if (!*fn) {
1327                         len = 1;
1328                         fn = ".";
1329                 }
1330
1331                 if (dir && *dir) {
1332                         static const char *lastdir;
1333                         static int lastdir_len;
1334
1335                         strlcpy(olddir, curr_dir, sizeof olddir);
1336
1337                         if (!push_dir(dir, 0)) {
1338                                 io_error |= IOERR_GENERAL;
1339                                 rsyserr(FERROR, errno, "push_dir %s failed",
1340                                         full_fname(dir));
1341                                 continue;
1342                         }
1343
1344                         if (lastdir && strcmp(lastdir, dir) == 0) {
1345                                 flist_dir = lastdir;
1346                                 flist_dir_len = lastdir_len;
1347                         } else {
1348                                 flist_dir = lastdir = strdup(dir);
1349                                 flist_dir_len = lastdir_len = strlen(dir);
1350                         }
1351                 }
1352
1353                 if (fn != fbuf)
1354                         memmove(fbuf, fn, len + 1);
1355
1356                 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1357                         /* Send the implied directories at the start of the
1358                          * source spec, so we get their permissions right. */
1359                         char *lp = lastpath, *slash = fbuf;
1360                         *p = '\0';
1361                         /* Skip any initial directories in our path that we
1362                          * have in common with lastpath. */
1363                         for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1364                                 if (*fn == '/')
1365                                         slash = fn;
1366                         }
1367                         *p = '/';
1368                         if (fn != p || (*lp && *lp != '/')) {
1369                                 int save_copy_links = copy_links;
1370                                 int save_xfer_dirs = xfer_dirs;
1371                                 copy_links |= copy_unsafe_links;
1372                                 xfer_dirs = 1;
1373                                 while ((slash = strchr(slash+1, '/')) != 0) {
1374                                         *slash = '\0';
1375                                         send_file_name(f, flist, fbuf, NULL, 0);
1376                                         *slash = '/';
1377                                 }
1378                                 copy_links = save_copy_links;
1379                                 xfer_dirs = save_xfer_dirs;
1380                                 *p = '\0';
1381                                 strlcpy(lastpath, fbuf, sizeof lastpath);
1382                                 *p = '/';
1383                         }
1384                 }
1385
1386                 if (one_file_system)
1387                         filesystem_dev = st.st_dev;
1388
1389                 if (recurse || (xfer_dirs && is_dot_dir)) {
1390                         struct file_struct *file;
1391                         file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1392                         if (file)
1393                                 send_if_directory(f, flist, file, fbuf, len);
1394                 } else
1395                         send_file_name(f, flist, fbuf, &st, 0);
1396
1397                 if (olddir[0]) {
1398                         flist_dir = NULL;
1399                         flist_dir_len = 0;
1400                         if (!pop_dir(olddir)) {
1401                                 rsyserr(FERROR, errno, "pop_dir %s failed",
1402                                         full_fname(olddir));
1403                                 exit_cleanup(RERR_FILESELECT);
1404                         }
1405                 }
1406         }
1407
1408         gettimeofday(&end_tv, NULL);
1409         stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1410                               + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1411         if (stats.flist_buildtime == 0)
1412                 stats.flist_buildtime = 1;
1413         start_tv = end_tv;
1414
1415         write_byte(f, 0); /* Indicate end of file list */
1416
1417 #ifdef SUPPORT_HARD_LINKS
1418         if (preserve_hard_links && protocol_version >= 30)
1419                 idev_destroy();
1420 #endif
1421
1422         if (show_filelist_p())
1423                 finish_filelist_progress(flist);
1424
1425         gettimeofday(&end_tv, NULL);
1426         stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1427                              + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1428
1429         /* Sort the list without removing any duplicates.  This allows the
1430          * receiving side to ask for any name they like, which gives us the
1431          * flexibility to change the way we unduplicate names in the future
1432          * without causing a compatibility problem with older versions. */
1433         clean_flist(flist, 0, 0);
1434
1435         if (!numeric_ids)
1436                 send_uid_list(f);
1437
1438         /* send the io_error flag */
1439         write_int(f, ignore_errors ? 0 : io_error);
1440
1441         io_end_buffering();
1442         stats.flist_size = stats.total_written - start_write;
1443         stats.num_files = flist->count;
1444
1445         if (verbose > 3)
1446                 output_flist(flist);
1447
1448         if (verbose > 2)
1449                 rprintf(FINFO, "send_file_list done\n");
1450
1451         return flist;
1452 }
1453
1454 struct file_list *recv_file_list(int f)
1455 {
1456         struct file_list *flist;
1457         int flags;
1458         int64 start_read;
1459
1460         rprintf(FLOG, "receiving file list\n");
1461         if (show_filelist_p())
1462                 start_filelist_progress("receiving file list");
1463
1464         start_read = stats.total_read;
1465
1466         flist = flist_new("recv_file_list");
1467
1468 #ifdef SUPPORT_HARD_LINKS
1469         if (preserve_hard_links && protocol_version < 30)
1470                 init_hard_links();
1471 #endif
1472
1473         while ((flags = read_byte(f)) != 0) {
1474                 struct file_struct *file;
1475
1476                 flist_expand(flist);
1477
1478                 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1479                         flags |= read_byte(f) << 8;
1480                 file = recv_file_entry(flist, flags, f);
1481
1482                 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1483                         stats.total_size += F_LENGTH(file);
1484
1485                 flist->files[flist->count++] = file;
1486
1487                 maybe_emit_filelist_progress(flist->count);
1488
1489                 if (verbose > 2) {
1490                         rprintf(FINFO, "recv_file_name(%s)\n",
1491                                 f_name(file, NULL));
1492                 }
1493         }
1494         recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1495
1496         if (verbose > 2)
1497                 rprintf(FINFO, "received %d names\n", flist->count);
1498
1499         if (show_filelist_p())
1500                 finish_filelist_progress(flist);
1501
1502         clean_flist(flist, relative_paths, 1);
1503
1504         if (f >= 0) {
1505                 recv_uid_list(f, flist);
1506
1507                 /* Recv the io_error flag */
1508                 if (ignore_errors)
1509                         read_int(f);
1510                 else
1511                         io_error |= read_int(f);
1512         }
1513
1514         if (verbose > 3)
1515                 output_flist(flist);
1516
1517         if (list_only) {
1518                 int i;
1519                 for (i = 0; i < flist->count; i++)
1520                         list_file_entry(flist->files[i]);
1521         }
1522
1523         if (verbose > 2)
1524                 rprintf(FINFO, "recv_file_list done\n");
1525
1526         stats.flist_size = stats.total_read - start_read;
1527         stats.num_files = flist->count;
1528
1529         return flist;
1530 }
1531
1532 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1533 {
1534         return f_name_cmp(*file1, *file2);
1535 }
1536
1537 /* Search for an identically-named item in the file list.  Note that the
1538  * items must agree in their directory-ness, or no match is returned. */
1539 int flist_find(struct file_list *flist, struct file_struct *f)
1540 {
1541         int low = flist->low, high = flist->high;
1542         int diff, mid, mid_up;
1543
1544         while (low <= high) {
1545                 mid = (low + high) / 2;
1546                 if (F_IS_ACTIVE(flist->files[mid]))
1547                         mid_up = mid;
1548                 else {
1549                         /* Scan for the next non-empty entry using the cached
1550                          * distance values.  If the value isn't fully up-to-
1551                          * date, update it. */
1552                         mid_up = mid + F_DEPTH(flist->files[mid]);
1553                         if (!F_IS_ACTIVE(flist->files[mid_up])) {
1554                                 do {
1555                                     mid_up += F_DEPTH(flist->files[mid_up]);
1556                                 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1557                                 F_DEPTH(flist->files[mid]) = mid_up - mid;
1558                         }
1559                         if (mid_up > high) {
1560                                 /* If there's nothing left above us, set high to
1561                                  * a non-empty entry below us and continue. */
1562                                 high = mid - (int)flist->files[mid]->len32;
1563                                 if (!F_IS_ACTIVE(flist->files[high])) {
1564                                         do {
1565                                             high -= (int)flist->files[high]->len32;
1566                                         } while (!F_IS_ACTIVE(flist->files[high]));
1567                                         flist->files[mid]->len32 = mid - high;
1568                                 }
1569                                 continue;
1570                         }
1571                 }
1572                 diff = f_name_cmp(flist->files[mid_up], f);
1573                 if (diff == 0) {
1574                         if (protocol_version < 29
1575                             && S_ISDIR(flist->files[mid_up]->mode)
1576                             != S_ISDIR(f->mode))
1577                                 return -1;
1578                         return mid_up;
1579                 }
1580                 if (diff < 0)
1581                         low = mid_up + 1;
1582                 else
1583                         high = mid - 1;
1584         }
1585         return -1;
1586 }
1587
1588 /*
1589  * Free up any resources a file_struct has allocated
1590  * and clear the file.
1591  */
1592 void clear_file(struct file_struct *file)
1593 {
1594         /* The +1 zeros out the first char of the basename. */
1595         memset(file, 0, FILE_STRUCT_LEN + 1);
1596         /* In an empty entry, F_DEPTH() is an offset to the next non-empty
1597          * entry.  Likewise for len32 in the opposite direction.  We assume
1598          * that we're alone for now since flist_find() will adjust the counts
1599          * it runs into that aren't up-to-date. */
1600         file->len32 = F_DEPTH(file) = 1;
1601 }
1602
1603 /* Allocate a new file list. */
1604 struct file_list *flist_new(char *msg)
1605 {
1606         struct file_list *flist;
1607
1608         flist = new(struct file_list);
1609         if (!flist)
1610                 out_of_memory(msg);
1611
1612         memset(flist, 0, sizeof flist[0]);
1613
1614         if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1615                 out_of_memory(msg);
1616
1617         return flist;
1618 }
1619
1620 /* Free up all elements in a flist. */
1621 void flist_free(struct file_list *flist)
1622 {
1623         pool_destroy(flist->file_pool);
1624         free(flist->files);
1625         free(flist);
1626 }
1627
1628 /*
1629  * This routine ensures we don't have any duplicate names in our file list.
1630  * duplicate names can cause corruption because of the pipelining
1631  */
1632 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1633 {
1634         char fbuf[MAXPATHLEN];
1635         int i, prev_i = 0;
1636
1637         if (!flist)
1638                 return;
1639         if (flist->count == 0) {
1640                 flist->high = -1;
1641                 return;
1642         }
1643
1644         qsort(flist->files, flist->count,
1645             sizeof flist->files[0], (int (*)())file_compare);
1646
1647         for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1648                 if (F_IS_ACTIVE(flist->files[i])) {
1649                         prev_i = i;
1650                         break;
1651                 }
1652         }
1653         flist->low = prev_i;
1654         while (++i < flist->count) {
1655                 int j;
1656                 struct file_struct *file = flist->files[i];
1657
1658                 if (!F_IS_ACTIVE(file))
1659                         continue;
1660                 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1661                         j = prev_i;
1662                 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1663                         int save_mode = file->mode;
1664                         /* Make sure that this directory doesn't duplicate a
1665                          * non-directory earlier in the list. */
1666                         flist->high = prev_i;
1667                         file->mode = S_IFREG;
1668                         j = flist_find(flist, file);
1669                         file->mode = save_mode;
1670                 } else
1671                         j = -1;
1672                 if (j >= 0) {
1673                         struct file_struct *fp = flist->files[j];
1674                         int keep, drop;
1675                         /* If one is a dir and the other is not, we want to
1676                          * keep the dir because it might have contents in the
1677                          * list. */
1678                         if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1679                                 if (S_ISDIR(file->mode))
1680                                         keep = i, drop = j;
1681                                 else
1682                                         keep = j, drop = i;
1683                         } else
1684                                 keep = j, drop = i;
1685                         if (verbose > 1 && !am_server) {
1686                                 rprintf(FINFO,
1687                                         "removing duplicate name %s from file list (%d)\n",
1688                                         f_name(file, fbuf), drop);
1689                         }
1690                         /* Make sure we don't lose track of a user-specified
1691                          * top directory. */
1692                         flist->files[keep]->flags |= flist->files[drop]->flags
1693                                                    & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1694
1695                         clear_file(flist->files[drop]);
1696
1697                         if (keep == i) {
1698                                 if (flist->low == drop) {
1699                                         for (j = drop + 1;
1700                                              j < i && !F_IS_ACTIVE(flist->files[j]);
1701                                              j++) {}
1702                                         flist->low = j;
1703                                 }
1704                                 prev_i = i;
1705                         }
1706                 } else
1707                         prev_i = i;
1708         }
1709         flist->high = no_dups ? prev_i : flist->count - 1;
1710
1711         if (strip_root) {
1712                 /* We need to strip off the leading slashes for relative
1713                  * paths, but this must be done _after_ the sorting phase. */
1714                 for (i = flist->low; i <= flist->high; i++) {
1715                         struct file_struct *file = flist->files[i];
1716
1717                         if (!file->dirname)
1718                                 continue;
1719                         while (*file->dirname == '/')
1720                                 file->dirname++;
1721                         if (!*file->dirname)
1722                                 file->dirname = NULL;
1723                 }
1724         }
1725
1726         if (prune_empty_dirs && no_dups) {
1727                 int j, prev_depth = 0;
1728
1729                 prev_i = 0; /* It's OK that this isn't really true. */
1730
1731                 for (i = flist->low; i <= flist->high; i++) {
1732                         struct file_struct *fp, *file = flist->files[i];
1733
1734                         /* This temporarily abuses the F_DEPTH() value for a
1735                          * directory that is in a chain that might get pruned.
1736                          * We restore the old value if it gets a reprieve. */
1737                         if (S_ISDIR(file->mode) && F_DEPTH(file)) {
1738                                 /* Dump empty dirs when coming back down. */
1739                                 for (j = prev_depth; j >= F_DEPTH(file); j--) {
1740                                         fp = flist->files[prev_i];
1741                                         if (F_DEPTH(fp) >= 0)
1742                                                 break;
1743                                         prev_i = -F_DEPTH(fp)-1;
1744                                         clear_file(fp);
1745                                 }
1746                                 prev_depth = F_DEPTH(file);
1747                                 if (is_excluded(f_name(file, fbuf), 1,
1748                                                        ALL_FILTERS)) {
1749                                         /* Keep dirs through this dir. */
1750                                         for (j = prev_depth-1; ; j--) {
1751                                                 fp = flist->files[prev_i];
1752                                                 if (F_DEPTH(fp) >= 0)
1753                                                         break;
1754                                                 prev_i = -F_DEPTH(fp)-1;
1755                                                 F_DEPTH(fp) = j;
1756                                         }
1757                                 } else
1758                                         F_DEPTH(file) = -prev_i-1;
1759                                 prev_i = i;
1760                         } else {
1761                                 /* Keep dirs through this non-dir. */
1762                                 for (j = prev_depth; ; j--) {
1763                                         fp = flist->files[prev_i];
1764                                         if (F_DEPTH(fp) >= 0)
1765                                                 break;
1766                                         prev_i = -F_DEPTH(fp)-1;
1767                                         F_DEPTH(fp) = j;
1768                                 }
1769                         }
1770                 }
1771                 /* Dump empty all remaining empty dirs. */
1772                 while (1) {
1773                         struct file_struct *fp = flist->files[prev_i];
1774                         if (F_DEPTH(fp) >= 0)
1775                                 break;
1776                         prev_i = -F_DEPTH(fp)-1;
1777                         clear_file(fp);
1778                 }
1779
1780                 for (i = flist->low; i <= flist->high; i++) {
1781                         if (F_IS_ACTIVE(flist->files[i]))
1782                                 break;
1783                 }
1784                 flist->low = i;
1785                 for (i = flist->high; i >= flist->low; i--) {
1786                         if (F_IS_ACTIVE(flist->files[i]))
1787                                 break;
1788                 }
1789                 flist->high = i;
1790         }
1791 }
1792
1793 static void output_flist(struct file_list *flist)
1794 {
1795         char uidbuf[16], gidbuf[16], depthbuf[16];
1796         struct file_struct *file;
1797         const char *root, *dir, *slash, *name, *trail;
1798         const char *who = who_am_i();
1799         int i;
1800
1801         for (i = 0; i < flist->count; i++) {
1802                 file = flist->files[i];
1803                 if ((am_root || am_sender) && preserve_uid) {
1804                         snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1805                                  (long)F_UID(file));
1806                 } else
1807                         *uidbuf = '\0';
1808                 if (preserve_gid && F_GID(file) != GID_NONE) {
1809                         snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1810                                  (long)F_GID(file));
1811                 } else
1812                         *gidbuf = '\0';
1813                 if (!am_sender)
1814                         snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
1815                 if (F_IS_ACTIVE(file)) {
1816                         root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf;
1817                         if ((dir = file->dirname) == NULL)
1818                                 dir = slash = "";
1819                         else
1820                                 slash = "/";
1821                         name = file->basename;
1822                         trail = S_ISDIR(file->mode) ? "/" : "";
1823                 } else
1824                         root = dir = slash = name = trail = "";
1825                 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1826                         who, i, root, dir, slash, name, trail, (int)file->mode,
1827                         (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1828         }
1829 }
1830
1831 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1832 enum fnc_type { t_PATH, t_ITEM };
1833
1834 /* Compare the names of two file_struct entities, similar to how strcmp()
1835  * would do if it were operating on the joined strings.
1836  *
1837  * Some differences beginning with protocol_version 29: (1) directory names
1838  * are compared with an assumed trailing slash so that they compare in a
1839  * way that would cause them to sort immediately prior to any content they
1840  * may have; (2) a directory of any name compares after a non-directory of
1841  * any name at the same depth; (3) a directory with name "." compares prior
1842  * to anything else.  These changes mean that a directory and a non-dir
1843  * with the same name will not compare as equal (protocol_version >= 29).
1844  *
1845  * The dirname component can be an empty string, but the basename component
1846  * cannot (and never is in the current codebase).  The basename component
1847  * may be NULL (for a removed item), in which case it is considered to be
1848  * after any existing item. */
1849 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1850 {
1851         int dif;
1852         const uchar *c1, *c2;
1853         enum fnc_state state1, state2;
1854         enum fnc_type type1, type2;
1855         enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1856
1857         if (!f1 || !F_IS_ACTIVE(f1)) {
1858                 if (!f2 || !F_IS_ACTIVE(f2))
1859                         return 0;
1860                 return -1;
1861         }
1862         if (!f2 || !F_IS_ACTIVE(f2))
1863                 return 1;
1864
1865         c1 = (uchar*)f1->dirname;
1866         c2 = (uchar*)f2->dirname;
1867         if (c1 == c2)
1868                 c1 = c2 = NULL;
1869         if (!c1) {
1870                 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1871                 c1 = (const uchar*)f1->basename;
1872                 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1873                         type1 = t_ITEM;
1874                         state1 = s_TRAILING;
1875                         c1 = (uchar*)"";
1876                 } else
1877                         state1 = s_BASE;
1878         } else {
1879                 type1 = t_path;
1880                 state1 = s_DIR;
1881         }
1882         if (!c2) {
1883                 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1884                 c2 = (const uchar*)f2->basename;
1885                 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1886                         type2 = t_ITEM;
1887                         state2 = s_TRAILING;
1888                         c2 = (uchar*)"";
1889                 } else
1890                         state2 = s_BASE;
1891         } else {
1892                 type2 = t_path;
1893                 state2 = s_DIR;
1894         }
1895
1896         if (type1 != type2)
1897                 return type1 == t_PATH ? 1 : -1;
1898
1899         do {
1900                 if (!*c1) {
1901                         switch (state1) {
1902                         case s_DIR:
1903                                 state1 = s_SLASH;
1904                                 c1 = (uchar*)"/";
1905                                 break;
1906                         case s_SLASH:
1907                                 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1908                                 c1 = (const uchar*)f1->basename;
1909                                 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1910                                         type1 = t_ITEM;
1911                                         state1 = s_TRAILING;
1912                                         c1 = (uchar*)"";
1913                                 } else
1914                                         state1 = s_BASE;
1915                                 break;
1916                         case s_BASE:
1917                                 state1 = s_TRAILING;
1918                                 if (type1 == t_PATH) {
1919                                         c1 = (uchar*)"/";
1920                                         break;
1921                                 }
1922                                 /* FALL THROUGH */
1923                         case s_TRAILING:
1924                                 type1 = t_ITEM;
1925                                 break;
1926                         }
1927                         if (*c2 && type1 != type2)
1928                                 return type1 == t_PATH ? 1 : -1;
1929                 }
1930                 if (!*c2) {
1931                         switch (state2) {
1932                         case s_DIR:
1933                                 state2 = s_SLASH;
1934                                 c2 = (uchar*)"/";
1935                                 break;
1936                         case s_SLASH:
1937                                 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1938                                 c2 = (const uchar*)f2->basename;
1939                                 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1940                                         type2 = t_ITEM;
1941                                         state2 = s_TRAILING;
1942                                         c2 = (uchar*)"";
1943                                 } else
1944                                         state2 = s_BASE;
1945                                 break;
1946                         case s_BASE:
1947                                 state2 = s_TRAILING;
1948                                 if (type2 == t_PATH) {
1949                                         c2 = (uchar*)"/";
1950                                         break;
1951                                 }
1952                                 /* FALL THROUGH */
1953                         case s_TRAILING:
1954                                 if (!*c1)
1955                                         return 0;
1956                                 type2 = t_ITEM;
1957                                 break;
1958                         }
1959                         if (type1 != type2)
1960                                 return type1 == t_PATH ? 1 : -1;
1961                 }
1962         } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1963
1964         return dif;
1965 }
1966
1967 char *f_name_buf(void)
1968 {
1969         static char names[5][MAXPATHLEN];
1970         static unsigned int n;
1971
1972         n = (n + 1) % (sizeof names / sizeof names[0]);
1973
1974         return names[n];
1975 }
1976
1977 /* Return a copy of the full filename of a flist entry, using the indicated
1978  * buffer or one of 5 static buffers if fbuf is NULL.  No size-checking is
1979  * done because we checked the size when creating the file_struct entry.
1980  */
1981 char *f_name(struct file_struct *f, char *fbuf)
1982 {
1983         if (!f || !F_IS_ACTIVE(f))
1984                 return NULL;
1985
1986         if (!fbuf)
1987                 fbuf = f_name_buf();
1988
1989         if (f->dirname) {
1990                 int len = strlen(f->dirname);
1991                 memcpy(fbuf, f->dirname, len);
1992                 fbuf[len] = '/';
1993                 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1994         } else
1995                 strlcpy(fbuf, f->basename, MAXPATHLEN);
1996
1997         return fbuf;
1998 }
1999
2000 /* Do a non-recursive scan of the named directory, possibly ignoring all
2001  * exclude rules except for the daemon's.  If "dlen" is >=0, it is the length
2002  * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2003  * buffer (the functions we call will append names onto the end, but the old
2004  * dir value will be restored on exit). */
2005 struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2006 {
2007         struct file_list *dirlist;
2008         char dirbuf[MAXPATHLEN];
2009         int save_recurse = recurse;
2010         int save_xfer_dirs = xfer_dirs;
2011
2012         if (dlen < 0) {
2013                 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2014                 if (dlen >= MAXPATHLEN)
2015                         return NULL;
2016                 dirname = dirbuf;
2017         }
2018
2019         dirlist = flist_new("get_dirlist");
2020
2021         recurse = 0;
2022         xfer_dirs = 1;
2023         send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
2024         xfer_dirs = save_xfer_dirs;
2025         recurse = save_recurse;
2026         if (do_progress)
2027                 flist_count_offset += dirlist->count;
2028
2029         clean_flist(dirlist, 0, 0);
2030
2031         if (verbose > 3)
2032                 output_flist(dirlist);
2033
2034         return dirlist;
2035 }