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