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