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