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