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