Improved the usage comments.
[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))
418 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
419 write_byte(f, flags);
420 }
421 if (flags & XMIT_SAME_NAME)
422 write_byte(f, l1);
423 if (flags & XMIT_LONG_NAME)
424 write_int(f, l2);
425 else
426 write_byte(f, l2);
427 write_buf(f, fname + l1, l2);
428
429 write_longint(f, file->length);
430 if (!(flags & XMIT_SAME_TIME))
431 write_int(f, modtime);
432 if (!(flags & XMIT_SAME_MODE))
433 write_int(f, to_wire_mode(mode));
434 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
435 if (!numeric_ids)
436 add_uid(uid);
437 write_int(f, uid);
438 }
439 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
440 if (!numeric_ids)
441 add_gid(gid);
442 write_int(f, gid);
443 }
444 if (preserve_devices && IS_DEVICE(mode)) {
445 if (protocol_version < 28) {
446 if (!(flags & XMIT_SAME_RDEV_pre28))
447 write_int(f, (int)rdev);
448 } else {
449 if (!(flags & XMIT_SAME_RDEV_MAJOR))
450 write_int(f, major(rdev));
451 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
452 write_byte(f, minor(rdev));
453 else
454 write_int(f, minor(rdev));
455 }
456 }
457
458#ifdef SUPPORT_LINKS
459 if (preserve_links && S_ISLNK(mode)) {
460 int len = strlen(file->u.link);
461 write_int(f, len);
462 write_buf(f, file->u.link, len);
463 }
464#endif
465
466#ifdef SUPPORT_HARD_LINKS
467 if (flags & XMIT_HAS_IDEV_DATA) {
468 if (protocol_version < 26) {
469 /* 32-bit dev_t and ino_t */
470 write_int(f, dev);
471 write_int(f, file->F_INODE);
472 } else {
473 /* 64-bit dev_t and ino_t */
474 if (!(flags & XMIT_SAME_DEV))
475 write_longint(f, dev);
476 write_longint(f, file->F_INODE);
477 }
478 }
479#endif
480
481 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
482 char *sum;
483 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
484 if (S_ISREG(mode))
485 sum = file->u.sum;
486 else {
487 /* Prior to 28, we sent a useless set of nulls. */
488 sum = empty_sum;
489 }
490 write_buf(f, sum, slen);
491 }
492
493 strlcpy(lastname, fname, MAXPATHLEN);
494
495 io_write_phase = "unknown";
496}
497
498
499
500static struct file_struct *receive_file_entry(struct file_list *flist,
501 unsigned short flags, int f)
502{
503 static time_t modtime;
504 static mode_t mode;
505 static int64 dev;
506 static dev_t rdev;
507 static uint32 rdev_major;
508 static uid_t uid;
509 static gid_t gid;
510 static char lastname[MAXPATHLEN], *lastdir;
511 static int lastdir_depth, lastdir_len = -1;
512 static unsigned int del_hier_name_len = 0;
513 static int in_del_hier = 0;
514 char thisname[MAXPATHLEN];
515 unsigned int l1 = 0, l2 = 0;
516 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
517 OFF_T file_length;
518 char *basename, *dirname, *bp;
519 struct file_struct *file;
520
521 if (!flist) {
522 modtime = 0, mode = 0;
523 dev = 0, rdev = makedev(0, 0);
524 rdev_major = 0;
525 uid = 0, gid = 0;
526 *lastname = '\0';
527 lastdir_len = -1;
528 in_del_hier = 0;
529 return NULL;
530 }
531
532 if (flags & XMIT_SAME_NAME)
533 l1 = read_byte(f);
534
535 if (flags & XMIT_LONG_NAME)
536 l2 = read_int(f);
537 else
538 l2 = read_byte(f);
539
540 if (l2 >= MAXPATHLEN - l1) {
541 rprintf(FERROR,
542 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
543 flags, l1, l2, safe_fname(lastname));
544 overflow_exit("receive_file_entry");
545 }
546
547 strlcpy(thisname, lastname, l1 + 1);
548 read_sbuf(f, &thisname[l1], l2);
549 thisname[l1 + l2] = 0;
550
551 strlcpy(lastname, thisname, MAXPATHLEN);
552
553 clean_fname(thisname, 0);
554
555 if (sanitize_paths)
556 sanitize_path(thisname, thisname, "", 0);
557
558 if ((basename = strrchr(thisname, '/')) != NULL) {
559 dirname_len = ++basename - thisname; /* counts future '\0' */
560 if (lastdir_len == dirname_len - 1
561 && strncmp(thisname, lastdir, lastdir_len) == 0) {
562 dirname = lastdir;
563 dirname_len = 0; /* indicates no copy is needed */
564 } else
565 dirname = thisname;
566 } else {
567 basename = thisname;
568 dirname = NULL;
569 dirname_len = 0;
570 }
571 basename_len = strlen(basename) + 1; /* count the '\0' */
572
573 file_length = read_longint(f);
574 if (!(flags & XMIT_SAME_TIME))
575 modtime = (time_t)read_int(f);
576 if (!(flags & XMIT_SAME_MODE))
577 mode = from_wire_mode(read_int(f));
578
579 if (preserve_uid && !(flags & XMIT_SAME_UID))
580 uid = (uid_t)read_int(f);
581 if (preserve_gid && !(flags & XMIT_SAME_GID))
582 gid = (gid_t)read_int(f);
583
584 if (preserve_devices) {
585 if (protocol_version < 28) {
586 if (IS_DEVICE(mode)) {
587 if (!(flags & XMIT_SAME_RDEV_pre28))
588 rdev = (dev_t)read_int(f);
589 } else
590 rdev = makedev(0, 0);
591 } else if (IS_DEVICE(mode)) {
592 uint32 rdev_minor;
593 if (!(flags & XMIT_SAME_RDEV_MAJOR))
594 rdev_major = read_int(f);
595 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
596 rdev_minor = read_byte(f);
597 else
598 rdev_minor = read_int(f);
599 rdev = makedev(rdev_major, rdev_minor);
600 }
601 }
602
603#ifdef SUPPORT_LINKS
604 if (preserve_links && S_ISLNK(mode)) {
605 linkname_len = read_int(f) + 1; /* count the '\0' */
606 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
607 rprintf(FERROR, "overflow: linkname_len=%d\n",
608 linkname_len - 1);
609 overflow_exit("receive_file_entry");
610 }
611 }
612 else
613#endif
614 linkname_len = 0;
615
616 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
617
618 alloc_len = file_struct_len + dirname_len + basename_len
619 + linkname_len + sum_len;
620 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
621
622 file = (struct file_struct *)bp;
623 memset(bp, 0, file_struct_len);
624 bp += file_struct_len;
625
626 file->flags = 0;
627 file->modtime = modtime;
628 file->length = file_length;
629 file->mode = mode;
630 file->uid = uid;
631 file->gid = gid;
632
633 if (dirname_len) {
634 file->dirname = lastdir = bp;
635 lastdir_len = dirname_len - 1;
636 memcpy(bp, dirname, dirname_len - 1);
637 bp += dirname_len;
638 bp[-1] = '\0';
639 lastdir_depth = count_dir_elements(lastdir);
640 file->dir.depth = lastdir_depth + 1;
641 } else if (dirname) {
642 file->dirname = dirname; /* we're reusing lastname */
643 file->dir.depth = lastdir_depth + 1;
644 } else
645 file->dir.depth = 1;
646
647 if (S_ISDIR(mode)) {
648 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
649 file->dir.depth--;
650 if (flags & XMIT_TOP_DIR) {
651 in_del_hier = recurse;
652 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
653 if (relative_paths && del_hier_name_len > 2
654 && basename_len == 1+1 && *basename == '.')
655 del_hier_name_len -= 2;
656 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
657 } else if (in_del_hier) {
658 if (!relative_paths || !del_hier_name_len
659 || (l1 >= del_hier_name_len
660 && thisname[del_hier_name_len] == '/'))
661 file->flags |= FLAG_DEL_HERE;
662 else
663 in_del_hier = 0;
664 }
665 }
666
667 file->basename = bp;
668 memcpy(bp, basename, basename_len);
669 bp += basename_len;
670
671 if (preserve_devices && IS_DEVICE(mode))
672 file->u.rdev = rdev;
673
674#ifdef SUPPORT_LINKS
675 if (linkname_len) {
676 file->u.link = bp;
677 read_sbuf(f, bp, linkname_len - 1);
678 if (sanitize_paths)
679 sanitize_path(bp, bp, "", lastdir_depth);
680 bp += linkname_len;
681 }
682#endif
683
684#ifdef SUPPORT_HARD_LINKS
685 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
686 flags |= XMIT_HAS_IDEV_DATA;
687 if (flags & XMIT_HAS_IDEV_DATA) {
688 int64 inode;
689 if (protocol_version < 26) {
690 dev = read_int(f);
691 inode = read_int(f);
692 } else {
693 if (!(flags & XMIT_SAME_DEV))
694 dev = read_longint(f);
695 inode = read_longint(f);
696 }
697 if (flist->hlink_pool) {
698 file->link_u.idev = pool_talloc(flist->hlink_pool,
699 struct idev, 1, "inode_table");
700 file->F_INODE = inode;
701 file->F_DEV = dev;
702 }
703 }
704#endif
705
706 if (always_checksum && (sum_len || protocol_version < 28)) {
707 char *sum;
708 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
709 if (sum_len) {
710 file->u.sum = sum = bp;
711 /*bp += sum_len;*/
712 } else {
713 /* Prior to 28, we get a useless set of nulls. */
714 sum = empty_sum;
715 }
716 read_buf(f, sum, slen);
717 }
718
719 if (!preserve_perms) {
720 /* set an appropriate set of permissions based on original
721 * permissions and umask. This emulates what GNU cp does */
722 file->mode &= ~orig_umask;
723 }
724
725 return file;
726}
727
728
729/**
730 * Create a file_struct for a named file by reading its stat()
731 * information and performing extensive checks against global
732 * options.
733 *
734 * @return the new file, or NULL if there was an error or this file
735 * should be excluded.
736 *
737 * @todo There is a small optimization opportunity here to avoid
738 * stat()ing the file in some circumstances, which has a certain cost.
739 * We are called immediately after doing readdir(), and so we may
740 * already know the d_type of the file. We could for example avoid
741 * statting directories if we're not recursing, but this is not a very
742 * important case. Some systems may not have d_type.
743 **/
744struct file_struct *make_file(char *fname, struct file_list *flist,
745 int filter_level)
746{
747 static char *lastdir;
748 static int lastdir_len = -1;
749 struct file_struct *file;
750 STRUCT_STAT st;
751 char sum[SUM_LENGTH];
752 char thisname[MAXPATHLEN];
753 char linkname[MAXPATHLEN];
754 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
755 char *basename, *dirname, *bp;
756 unsigned short flags = 0;
757
758 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
759 lastdir_len = -1;
760
761 if (strlcpy(thisname, fname, sizeof thisname)
762 >= sizeof thisname - flist_dir_len) {
763 rprintf(FINFO, "skipping overly long name: %s\n",
764 safe_fname(fname));
765 return NULL;
766 }
767 clean_fname(thisname, 0);
768 if (sanitize_paths)
769 sanitize_path(thisname, thisname, "", 0);
770
771 memset(sum, 0, SUM_LENGTH);
772
773 if (readlink_stat(thisname, &st, linkname) != 0) {
774 int save_errno = errno;
775 /* See if file is excluded before reporting an error. */
776 if (filter_level != NO_FILTERS
777 && is_excluded(thisname, 0, filter_level))
778 return NULL;
779 if (save_errno == ENOENT) {
780#ifdef SUPPORT_LINKS
781 /* Avoid "vanished" error if symlink points nowhere. */
782 if (copy_links && do_lstat(thisname, &st) == 0
783 && S_ISLNK(st.st_mode)) {
784 io_error |= IOERR_GENERAL;
785 rprintf(FERROR, "symlink has no referent: %s\n",
786 full_fname(thisname));
787 } else
788#endif
789 {
790 enum logcode c = am_daemon && protocol_version < 28
791 ? FERROR : FINFO;
792 io_error |= IOERR_VANISHED;
793 rprintf(c, "file has vanished: %s\n",
794 full_fname(thisname));
795 }
796 } else {
797 io_error |= IOERR_GENERAL;
798 rsyserr(FERROR, save_errno, "readlink %s failed",
799 full_fname(thisname));
800 }
801 return NULL;
802 }
803
804 /* backup.c calls us with filter_level set to NO_FILTERS. */
805 if (filter_level == NO_FILTERS)
806 goto skip_filters;
807
808 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
809 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
810 return NULL;
811 }
812
813 /* We only care about directories because we need to avoid recursing
814 * into a mount-point directory, not to avoid copying a symlinked
815 * file if -L (or similar) was specified. */
816 if (one_file_system && st.st_dev != filesystem_dev
817 && S_ISDIR(st.st_mode))
818 flags |= FLAG_MOUNT_POINT;
819
820 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
821 return NULL;
822
823 if (lp_ignore_nonreadable(module_id)) {
824#ifdef SUPPORT_LINKS
825 if (!S_ISLNK(st.st_mode))
826#endif
827 if (access(thisname, R_OK) != 0)
828 return NULL;
829 }
830
831skip_filters:
832
833 if (verbose > 2) {
834 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
835 who_am_i(), safe_fname(thisname), filter_level);
836 }
837
838 if ((basename = strrchr(thisname, '/')) != NULL) {
839 dirname_len = ++basename - thisname; /* counts future '\0' */
840 if (lastdir_len == dirname_len - 1
841 && strncmp(thisname, lastdir, lastdir_len) == 0) {
842 dirname = lastdir;
843 dirname_len = 0; /* indicates no copy is needed */
844 } else
845 dirname = thisname;
846 } else {
847 basename = thisname;
848 dirname = NULL;
849 dirname_len = 0;
850 }
851 basename_len = strlen(basename) + 1; /* count the '\0' */
852
853#ifdef SUPPORT_LINKS
854 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
855#else
856 linkname_len = 0;
857#endif
858
859 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
860
861 alloc_len = file_struct_len + dirname_len + basename_len
862 + linkname_len + sum_len;
863 if (flist) {
864 bp = pool_alloc(flist->file_pool, alloc_len,
865 "receive_file_entry");
866 } else {
867 if (!(bp = new_array(char, alloc_len)))
868 out_of_memory("receive_file_entry");
869 }
870
871 file = (struct file_struct *)bp;
872 memset(bp, 0, file_struct_len);
873 bp += file_struct_len;
874
875 file->flags = flags;
876 file->modtime = st.st_mtime;
877 file->length = st.st_size;
878 file->mode = st.st_mode;
879 file->uid = st.st_uid;
880 file->gid = st.st_gid;
881
882#ifdef SUPPORT_HARD_LINKS
883 if (flist && flist->hlink_pool) {
884 if (protocol_version < 28) {
885 if (S_ISREG(st.st_mode))
886 file->link_u.idev = pool_talloc(
887 flist->hlink_pool, struct idev, 1,
888 "inode_table");
889 } else {
890 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
891 file->link_u.idev = pool_talloc(
892 flist->hlink_pool, struct idev, 1,
893 "inode_table");
894 }
895 }
896 if (file->link_u.idev) {
897 file->F_DEV = st.st_dev;
898 file->F_INODE = st.st_ino;
899 }
900#endif
901
902 if (dirname_len) {
903 file->dirname = lastdir = bp;
904 lastdir_len = dirname_len - 1;
905 memcpy(bp, dirname, dirname_len - 1);
906 bp += dirname_len;
907 bp[-1] = '\0';
908 } else if (dirname)
909 file->dirname = dirname;
910
911 file->basename = bp;
912 memcpy(bp, basename, basename_len);
913 bp += basename_len;
914
915#ifdef HAVE_STRUCT_STAT_ST_RDEV
916 if (preserve_devices && IS_DEVICE(st.st_mode))
917 file->u.rdev = st.st_rdev;
918#endif
919
920#ifdef SUPPORT_LINKS
921 if (linkname_len) {
922 file->u.link = bp;
923 memcpy(bp, linkname, linkname_len);
924 bp += linkname_len;
925 }
926#endif
927
928 if (sum_len) {
929 file->u.sum = bp;
930 file_checksum(thisname, bp, st.st_size);
931 /*bp += sum_len;*/
932 }
933
934 file->dir.root = flist_dir;
935
936 /* This code is only used by the receiver when it is building
937 * a list of files for a delete pass. */
938 if (keep_dirlinks && linkname_len && flist) {
939 STRUCT_STAT st2;
940 int save_mode = file->mode;
941 file->mode = S_IFDIR; /* find a directory w/our name */
942 if (flist_find(the_file_list, file) >= 0
943 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
944 file->modtime = st2.st_mtime;
945 file->length = st2.st_size;
946 file->mode = st2.st_mode;
947 file->uid = st2.st_uid;
948 file->gid = st2.st_gid;
949 file->u.link = NULL;
950 } else
951 file->mode = save_mode;
952 }
953
954 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
955 stats.total_size += st.st_size;
956
957 return file;
958}
959
960
961static struct file_struct *send_file_name(int f, struct file_list *flist,
962 char *fname, unsigned short base_flags)
963{
964 struct file_struct *file;
965
966 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
967 if (!file)
968 return NULL;
969
970 maybe_emit_filelist_progress(flist->count + flist_count_offset);
971
972 flist_expand(flist);
973
974 if (file->basename[0]) {
975 flist->files[flist->count++] = file;
976 send_file_entry(file, f, base_flags);
977 }
978 return file;
979}
980
981static void send_if_directory(int f, struct file_list *flist,
982 struct file_struct *file)
983{
984 char fbuf[MAXPATHLEN];
985
986 if (S_ISDIR(file->mode)
987 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
988 void *save_filters;
989 unsigned int len = strlen(fbuf);
990 if (len > 1 && fbuf[len-1] == '/')
991 fbuf[--len] = '\0';
992 if (len >= MAXPATHLEN - 1) {
993 io_error |= IOERR_GENERAL;
994 rprintf(FERROR, "skipping long-named directory: %s\n",
995 full_fname(fbuf));
996 return;
997 }
998 save_filters = push_local_filters(fbuf, len);
999 send_directory(f, flist, fbuf, len);
1000 pop_local_filters(save_filters);
1001 }
1002}
1003
1004
1005/* This function is normally called by the sender, but the receiving side also
1006 * calls it from get_dirlist() with f set to -1 so that we just construct the
1007 * file list in memory without sending it over the wire. Also, get_dirlist()
1008 * might call this with f set to -2, which also indicates that local filter
1009 * rules should be ignored. */
1010static void send_directory(int f, struct file_list *flist,
1011 char *fbuf, int len)
1012{
1013 struct dirent *di;
1014 unsigned remainder;
1015 char *p;
1016 DIR *d;
1017 int start = flist->count;
1018
1019 if (!(d = opendir(fbuf))) {
1020 io_error |= IOERR_GENERAL;
1021 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1022 return;
1023 }
1024
1025 p = fbuf + len;
1026 if (len != 1 || *fbuf != '/')
1027 *p++ = '/';
1028 *p = '\0';
1029 remainder = MAXPATHLEN - (p - fbuf);
1030
1031 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1032 char *dname = d_name(di);
1033 if (dname[0] == '.' && (dname[1] == '\0'
1034 || (dname[1] == '.' && dname[2] == '\0')))
1035 continue;
1036 if (strlcpy(p, dname, remainder) < remainder)
1037 send_file_name(f, flist, fbuf, 0);
1038 else {
1039 io_error |= IOERR_GENERAL;
1040 rprintf(FINFO,
1041 "cannot send long-named file %s\n",
1042 full_fname(fbuf));
1043 }
1044 }
1045
1046 fbuf[len] = '\0';
1047
1048 if (errno) {
1049 io_error |= IOERR_GENERAL;
1050 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1051 }
1052
1053 closedir(d);
1054
1055 if (recurse) {
1056 int i, end = flist->count - 1;
1057 for (i = start; i <= end; i++)
1058 send_if_directory(f, flist, flist->files[i]);
1059 }
1060}
1061
1062
1063struct file_list *send_file_list(int f, int argc, char *argv[])
1064{
1065 int l;
1066 STRUCT_STAT st;
1067 char *p, *dir, olddir[sizeof curr_dir];
1068 char lastpath[MAXPATHLEN] = "";
1069 struct file_list *flist;
1070 struct timeval start_tv, end_tv;
1071 int64 start_write;
1072 int use_ff_fd = 0;
1073
1074 if (show_filelist_p())
1075 start_filelist_progress("building file list");
1076
1077 start_write = stats.total_written;
1078 gettimeofday(&start_tv, NULL);
1079
1080 flist = flist_new(WITH_HLINK, "send_file_list");
1081
1082 io_start_buffering_out();
1083 if (filesfrom_fd >= 0) {
1084 if (argv[0] && !push_dir(argv[0])) {
1085 rsyserr(FERROR, errno, "push_dir %s failed",
1086 full_fname(argv[0]));
1087 exit_cleanup(RERR_FILESELECT);
1088 }
1089 use_ff_fd = 1;
1090 }
1091
1092 while (1) {
1093 char fname2[MAXPATHLEN];
1094 char *fname = fname2;
1095 int is_dot_dir;
1096
1097 if (use_ff_fd) {
1098 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1099 break;
1100 sanitize_path(fname, fname, "", 0);
1101 } else {
1102 if (argc-- == 0)
1103 break;
1104 strlcpy(fname, *argv++, MAXPATHLEN);
1105 if (sanitize_paths)
1106 sanitize_path(fname, fname, "", 0);
1107 }
1108
1109 l = strlen(fname);
1110 if (!l || fname[l - 1] == '/') {
1111 if (l == 2 && fname[0] == '.') {
1112 /* Turn "./" into just "." rather than "./." */
1113 fname[1] = '\0';
1114 } else {
1115 if (l + 1 >= MAXPATHLEN)
1116 overflow_exit("send_file_list");
1117 fname[l++] = '.';
1118 fname[l] = '\0';
1119 }
1120 is_dot_dir = 1;
1121 } else if (l > 1 && fname[l-1] == '.' && fname[l-2] == '.'
1122 && (l == 2 || fname[l-3] == '/')) {
1123 if (l + 2 >= MAXPATHLEN)
1124 overflow_exit("send_file_list");
1125 fname[l++] = '/';
1126 fname[l++] = '.';
1127 fname[l] = '\0';
1128 is_dot_dir = 1;
1129 } else {
1130 is_dot_dir = fname[l-1] == '.'
1131 && (l == 1 || fname[l-2] == '/');
1132 }
1133
1134 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1135 io_error |= IOERR_GENERAL;
1136 rsyserr(FERROR, errno, "link_stat %s failed",
1137 full_fname(fname));
1138 continue;
1139 }
1140
1141 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1142 rprintf(FINFO, "skipping directory %s\n",
1143 safe_fname(fname));
1144 continue;
1145 }
1146
1147 dir = NULL;
1148 olddir[0] = '\0';
1149
1150 if (!relative_paths) {
1151 p = strrchr(fname, '/');
1152 if (p) {
1153 *p = '\0';
1154 if (p == fname)
1155 dir = "/";
1156 else
1157 dir = fname;
1158 fname = p + 1;
1159 }
1160 } else if ((p = strstr(fname, "/./")) != NULL) {
1161 *p = '\0';
1162 if (p == fname)
1163 dir = "/";
1164 else
1165 dir = fname;
1166 fname = p + 3;
1167 }
1168
1169 if (!*fname)
1170 fname = ".";
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 (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1195 /* Send the implied directories at the start of the
1196 * source spec, so we get their permissions right. */
1197 char *lp = lastpath, *fn = fname, *slash = fname;
1198 *p = '\0';
1199 /* Skip any initial directories in our path that we
1200 * have in common with lastpath. */
1201 while (*fn && *lp == *fn) {
1202 if (*fn == '/')
1203 slash = fn;
1204 lp++, fn++;
1205 }
1206 *p = '/';
1207 if (fn != p || (*lp && *lp != '/')) {
1208 int save_copy_links = copy_links;
1209 int save_xfer_dirs = xfer_dirs;
1210 copy_links = copy_unsafe_links;
1211 xfer_dirs = 1;
1212 while ((slash = strchr(slash+1, '/')) != 0) {
1213 *slash = '\0';
1214 send_file_name(f, flist, fname, 0);
1215 *slash = '/';
1216 }
1217 copy_links = save_copy_links;
1218 xfer_dirs = save_xfer_dirs;
1219 *p = '\0';
1220 strlcpy(lastpath, fname, sizeof lastpath);
1221 *p = '/';
1222 }
1223 }
1224
1225 if (one_file_system)
1226 filesystem_dev = st.st_dev;
1227
1228 if (recurse || (xfer_dirs && is_dot_dir)) {
1229 struct file_struct *file;
1230 if ((file = send_file_name(f, flist, fname, XMIT_TOP_DIR)))
1231 send_if_directory(f, flist, file);
1232 } else
1233 send_file_name(f, flist, fname, 0);
1234
1235 if (olddir[0]) {
1236 flist_dir = NULL;
1237 flist_dir_len = 0;
1238 if (!pop_dir(olddir)) {
1239 rsyserr(FERROR, errno, "pop_dir %s failed",
1240 full_fname(olddir));
1241 exit_cleanup(RERR_FILESELECT);
1242 }
1243 }
1244 }
1245
1246 gettimeofday(&end_tv, NULL);
1247 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1248 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1249 if (stats.flist_buildtime == 0)
1250 stats.flist_buildtime = 1;
1251 start_tv = end_tv;
1252
1253 send_file_entry(NULL, f, 0);
1254
1255 if (show_filelist_p())
1256 finish_filelist_progress(flist);
1257
1258 gettimeofday(&end_tv, NULL);
1259 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1260 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1261
1262 if (flist->hlink_pool) {
1263 pool_destroy(flist->hlink_pool);
1264 flist->hlink_pool = NULL;
1265 }
1266
1267 /* Sort the list without removing any duplicates. This allows the
1268 * receiving side to ask for any name they like, which gives us the
1269 * flexibility to change the way we unduplicate names in the future
1270 * without causing a compatibility problem with older versions. */
1271 clean_flist(flist, 0, 0);
1272
1273 /* Now send the uid/gid list. This was introduced in
1274 * protocol version 15 */
1275 send_uid_list(f);
1276
1277 /* send the io_error flag */
1278 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1279
1280 io_end_buffering();
1281 stats.flist_size = stats.total_written - start_write;
1282 stats.num_files = flist->count;
1283
1284 if (verbose > 3)
1285 output_flist(flist);
1286
1287 if (verbose > 2)
1288 rprintf(FINFO, "send_file_list done\n");
1289
1290 return flist;
1291}
1292
1293
1294struct file_list *recv_file_list(int f)
1295{
1296 struct file_list *flist;
1297 unsigned short flags;
1298 int64 start_read;
1299
1300 if (show_filelist_p())
1301 start_filelist_progress("receiving file list");
1302
1303 start_read = stats.total_read;
1304
1305 flist = flist_new(WITH_HLINK, "recv_file_list");
1306
1307 flist->count = 0;
1308 flist->malloced = 1000;
1309 flist->files = new_array(struct file_struct *, flist->malloced);
1310 if (!flist->files)
1311 goto oom;
1312
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 safe_fname(f_name(file)));
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
1374oom:
1375 out_of_memory("recv_file_list");
1376 return NULL; /* not reached */
1377}
1378
1379
1380static int file_compare(struct file_struct **file1, struct file_struct **file2)
1381{
1382 return f_name_cmp(*file1, *file2);
1383}
1384
1385
1386/* Search for an identically-named item in the file list. Note that the
1387 * items must agree in their directory-ness, or no match is returned. */
1388int flist_find(struct file_list *flist, struct file_struct *f)
1389{
1390 int low = flist->low, high = flist->high;
1391 int ret, mid, mid_up;
1392
1393 while (low <= high) {
1394 mid = (low + high) / 2;
1395 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1396 if (mid_up <= high)
1397 ret = f_name_cmp(flist->files[mid_up], f);
1398 else
1399 ret = 1;
1400 if (ret == 0) {
1401 if (protocol_version < 29
1402 && S_ISDIR(flist->files[mid_up]->mode)
1403 != S_ISDIR(f->mode))
1404 return -1;
1405 return mid_up;
1406 }
1407 if (ret > 0)
1408 high = mid - 1;
1409 else
1410 low = mid_up + 1;
1411 }
1412 return -1;
1413}
1414
1415
1416/*
1417 * Free up any resources a file_struct has allocated
1418 * and clear the file.
1419 */
1420void clear_file(int i, struct file_list *flist)
1421{
1422 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1423 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1424 memset(flist->files[i], 0, file_struct_len);
1425}
1426
1427
1428/*
1429 * allocate a new file list
1430 */
1431struct file_list *flist_new(int with_hlink, char *msg)
1432{
1433 struct file_list *flist;
1434
1435 flist = new(struct file_list);
1436 if (!flist)
1437 out_of_memory(msg);
1438
1439 memset(flist, 0, sizeof (struct file_list));
1440
1441 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1442 out_of_memory, POOL_INTERN)))
1443 out_of_memory(msg);
1444
1445#ifdef SUPPORT_HARD_LINKS
1446 if (with_hlink && preserve_hard_links) {
1447 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1448 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1449 out_of_memory(msg);
1450 }
1451#endif
1452
1453 return flist;
1454}
1455
1456/*
1457 * free up all elements in a flist
1458 */
1459void flist_free(struct file_list *flist)
1460{
1461 pool_destroy(flist->file_pool);
1462 pool_destroy(flist->hlink_pool);
1463 free(flist->files);
1464 free(flist);
1465}
1466
1467
1468/*
1469 * This routine ensures we don't have any duplicate names in our file list.
1470 * duplicate names can cause corruption because of the pipelining
1471 */
1472static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1473{
1474 int i, prev_i = 0;
1475
1476 if (!flist)
1477 return;
1478 if (flist->count == 0) {
1479 flist->high = -1;
1480 return;
1481 }
1482
1483 sorting_flist = flist;
1484 qsort(flist->files, flist->count,
1485 sizeof flist->files[0], (int (*)())file_compare);
1486 sorting_flist = NULL;
1487
1488 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1489 if (flist->files[i]->basename) {
1490 prev_i = i;
1491 break;
1492 }
1493 }
1494 flist->low = prev_i;
1495 while (++i < flist->count) {
1496 int j;
1497 struct file_struct *file = flist->files[i];
1498
1499 if (!file->basename)
1500 continue;
1501 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1502 j = prev_i;
1503 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1504 int save_mode = file->mode;
1505 /* Make sure that this directory doesn't duplicate a
1506 * non-directory earlier in the list. */
1507 flist->high = prev_i;
1508 file->mode = S_IFREG;
1509 j = flist_find(flist, file);
1510 file->mode = save_mode;
1511 } else
1512 j = -1;
1513 if (j >= 0) {
1514 struct file_struct *fp = flist->files[j];
1515 int keep, drop;
1516 /* If one is a dir and the other is not, we want to
1517 * keep the dir because it might have contents in the
1518 * list. */
1519 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1520 if (S_ISDIR(file->mode))
1521 keep = i, drop = j;
1522 else
1523 keep = j, drop = i;
1524 } else
1525 keep = j, drop = i;
1526 if (verbose > 1 && !am_server) {
1527 rprintf(FINFO,
1528 "removing duplicate name %s from file list (%d)\n",
1529 safe_fname(f_name(file)), drop);
1530 }
1531 /* Make sure that if we unduplicate '.', that we don't
1532 * lose track of a user-specified top directory. */
1533 flist->files[keep]->flags |= flist->files[drop]->flags
1534 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1535
1536 clear_file(drop, flist);
1537
1538 if (keep == i) {
1539 if (flist->low == drop) {
1540 for (j = drop + 1;
1541 j < i && !flist->files[j]->basename;
1542 j++) {}
1543 flist->low = j;
1544 }
1545 prev_i = i;
1546 }
1547 } else
1548 prev_i = i;
1549 }
1550 flist->high = no_dups ? prev_i : flist->count - 1;
1551
1552 if (strip_root) {
1553 /* We need to strip off the leading slashes for relative
1554 * paths, but this must be done _after_ the sorting phase. */
1555 for (i = flist->low; i <= flist->high; i++) {
1556 struct file_struct *file = flist->files[i];
1557
1558 if (!file->dirname)
1559 continue;
1560 if (*file->dirname == '/') {
1561 char *s = file->dirname + 1;
1562 while (*s == '/') s++;
1563 memmove(file->dirname, s, strlen(s) + 1);
1564 }
1565
1566 if (!*file->dirname)
1567 file->dirname = NULL;
1568 }
1569 }
1570}
1571
1572
1573static void output_flist(struct file_list *flist)
1574{
1575 char uidbuf[16], gidbuf[16], depthbuf[16];
1576 struct file_struct *file;
1577 const char *who = who_am_i();
1578 int i;
1579
1580 for (i = 0; i < flist->count; i++) {
1581 file = flist->files[i];
1582 if ((am_root || am_sender) && preserve_uid)
1583 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1584 else
1585 *uidbuf = '\0';
1586 if (preserve_gid && file->gid != GID_NONE)
1587 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1588 else
1589 *gidbuf = '\0';
1590 if (!am_sender)
1591 sprintf(depthbuf, "%d", file->dir.depth);
1592 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1593 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1594 file->dirname ? safe_fname(file->dirname) : "",
1595 file->dirname ? "/" : "", NS(file->basename),
1596 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1597 (double)file->length, uidbuf, gidbuf, file->flags);
1598 }
1599}
1600
1601
1602enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1603enum fnc_type { t_PATH, t_ITEM };
1604
1605/* Compare the names of two file_struct entities, similar to how strcmp()
1606 * would do if it were operating on the joined strings.
1607 *
1608 * Some differences beginning with protocol_version 29: (1) directory names
1609 * are compared with an assumed trailing slash so that they compare in a
1610 * way that would cause them to sort immediately prior to any content they
1611 * may have; (2) a directory of any name compares after a non-directory of
1612 * any name at the same depth; (3) a directory with name "." compares prior
1613 * to anything else. These changes mean that a directory and a non-dir
1614 * with the same name will not compare as equal (protocol_version >= 29).
1615 *
1616 * The dirname component can be an empty string, but the basename component
1617 * cannot (and never is in the current codebase). The basename component
1618 * may be NULL (for a removed item), in which case it is considered to be
1619 * after any existing item. */
1620int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1621{
1622 int dif;
1623 const uchar *c1, *c2;
1624 enum fnc_state state1, state2;
1625 enum fnc_type type1, type2;
1626 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1627
1628 if (!f1 || !f1->basename) {
1629 if (!f2 || !f2->basename)
1630 return 0;
1631 return -1;
1632 }
1633 if (!f2 || !f2->basename)
1634 return 1;
1635
1636 c1 = (uchar*)f1->dirname;
1637 c2 = (uchar*)f2->dirname;
1638 if (c1 == c2)
1639 c1 = c2 = NULL;
1640 if (!c1) {
1641 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1642 c1 = (uchar*)f1->basename;
1643 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1644 type1 = t_ITEM;
1645 state1 = s_TRAILING;
1646 c1 = (uchar*)"";
1647 } else
1648 state1 = s_BASE;
1649 } else if (!*c1) {
1650 type1 = t_path;
1651 state1 = s_SLASH;
1652 c1 = (uchar*)"/";
1653 } else {
1654 type1 = t_path;
1655 state1 = s_DIR;
1656 }
1657 if (!c2) {
1658 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1659 c2 = (uchar*)f2->basename;
1660 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1661 type2 = t_ITEM;
1662 state2 = s_TRAILING;
1663 c2 = (uchar*)"";
1664 } else
1665 state2 = s_BASE;
1666 } else if (!*c2) {
1667 type2 = t_path;
1668 state2 = s_SLASH;
1669 c2 = (uchar*)"/";
1670 } else {
1671 type2 = t_path;
1672 state2 = s_DIR;
1673 }
1674
1675 if (type1 != type2)
1676 return type1 == t_PATH ? 1 : -1;
1677
1678 while (1) {
1679 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1680 break;
1681 if (!*c1) {
1682 switch (state1) {
1683 case s_DIR:
1684 state1 = s_SLASH;
1685 c1 = (uchar*)"/";
1686 break;
1687 case s_SLASH:
1688 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1689 c1 = (uchar*)f1->basename;
1690 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1691 type1 = t_ITEM;
1692 state1 = s_TRAILING;
1693 c1 = (uchar*)"";
1694 } else
1695 state1 = s_BASE;
1696 break;
1697 case s_BASE:
1698 state1 = s_TRAILING;
1699 if (type1 == t_PATH) {
1700 c1 = (uchar*)"/";
1701 break;
1702 }
1703 /* FALL THROUGH */
1704 case s_TRAILING:
1705 type1 = t_ITEM;
1706 break;
1707 }
1708 if (*c2 && type1 != type2)
1709 return type1 == t_PATH ? 1 : -1;
1710 }
1711 if (!*c2) {
1712 switch (state2) {
1713 case s_DIR:
1714 state2 = s_SLASH;
1715 c2 = (uchar*)"/";
1716 break;
1717 case s_SLASH:
1718 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1719 c2 = (uchar*)f2->basename;
1720 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1721 type2 = t_ITEM;
1722 state2 = s_TRAILING;
1723 c2 = (uchar*)"";
1724 } else
1725 state2 = s_BASE;
1726 break;
1727 case s_BASE:
1728 state2 = s_TRAILING;
1729 if (type2 == t_PATH) {
1730 c2 = (uchar*)"/";
1731 break;
1732 }
1733 /* FALL THROUGH */
1734 case s_TRAILING:
1735 if (!*c1)
1736 return 0;
1737 type2 = t_ITEM;
1738 break;
1739 }
1740 if (type1 != type2)
1741 return type1 == t_PATH ? 1 : -1;
1742 }
1743 }
1744
1745 return dif;
1746}
1747
1748
1749/* Return a copy of the full filename of a flist entry, using the indicated
1750 * buffer. No size-checking is done because we checked the size when creating
1751 * the file_struct entry.
1752 */
1753char *f_name_to(struct file_struct *f, char *fbuf)
1754{
1755 if (!f || !f->basename)
1756 return NULL;
1757
1758 if (f->dirname) {
1759 int len = strlen(f->dirname);
1760 memcpy(fbuf, f->dirname, len);
1761 fbuf[len] = '/';
1762 strcpy(fbuf + len + 1, f->basename);
1763 } else
1764 strcpy(fbuf, f->basename);
1765 return fbuf;
1766}
1767
1768
1769/* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1770char *f_name(struct file_struct *f)
1771{
1772 static char names[5][MAXPATHLEN];
1773 static unsigned int n;
1774
1775 n = (n + 1) % (sizeof names / sizeof names[0]);
1776
1777 return f_name_to(f, names[n]);
1778}
1779
1780
1781/* Do a non-recursive scan of the named directory, possibly ignoring all
1782 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1783 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1784 * buffer (the functions we call will append names onto the end, but the old
1785 * dir value will be restored on exit). */
1786struct file_list *get_dirlist(char *dirname, int dlen,
1787 int ignore_filter_rules)
1788{
1789 struct file_list *dirlist;
1790 char dirbuf[MAXPATHLEN];
1791 int save_recurse = recurse;
1792
1793 if (dlen < 0) {
1794 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1795 if (dlen >= MAXPATHLEN)
1796 return NULL;
1797 dirname = dirbuf;
1798 }
1799
1800 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1801
1802 recurse = 0;
1803 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1804 recurse = save_recurse;
1805 if (do_progress)
1806 flist_count_offset += dirlist->count;
1807
1808 clean_flist(dirlist, 0, 0);
1809
1810 if (verbose > 3)
1811 output_flist(dirlist);
1812
1813 return dirlist;
1814}