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