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