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