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