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