2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
22 * Generate and receive file lists
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
37 extern int do_progress
;
38 extern int always_checksum
;
40 extern int ignore_errors
;
41 extern int numeric_ids
;
44 extern int filesfrom_fd
;
45 extern int one_file_system
;
46 extern int keep_dirlinks
;
47 extern int preserve_links
;
48 extern int preserve_hard_links
;
49 extern int preserve_perms
;
50 extern int preserve_devices
;
51 extern int preserve_uid
;
52 extern int preserve_gid
;
53 extern int relative_paths
;
54 extern int implied_dirs
;
55 extern int copy_links
;
56 extern int copy_unsafe_links
;
57 extern int protocol_version
;
58 extern int sanitize_paths
;
59 extern int orig_umask
;
60 extern struct stats stats
;
61 extern struct file_list
*the_file_list
;
63 extern char curr_dir
[MAXPATHLEN
];
65 extern struct filter_list_struct filter_list
;
66 extern struct filter_list_struct server_filter_list
;
69 dev_t filesystem_dev
; /* used to implement -x */
71 static char empty_sum
[MD4_SUM_LENGTH
];
72 static int flist_count_offset
;
73 static unsigned int file_struct_len
;
74 static struct file_list
*sorting_flist
;
76 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
);
77 static void output_flist(struct file_list
*flist
);
83 /* Figure out how big the file_struct is without trailing padding */
84 file_struct_len
= offsetof(struct file_struct
, flags
) + sizeof f
.flags
;
88 static int show_filelist_p(void)
90 return verbose
&& xfer_dirs
&& !am_server
;
93 static void start_filelist_progress(char *kind
)
95 rprintf(FINFO
, "%s ... ", kind
);
96 if (verbose
> 1 || do_progress
)
102 static void emit_filelist_progress(int count
)
104 rprintf(FINFO
, " %d files...\r", count
);
108 static void maybe_emit_filelist_progress(int count
)
110 if (do_progress
&& show_filelist_p() && (count
% 100) == 0)
111 emit_filelist_progress(count
);
115 static void finish_filelist_progress(const struct file_list
*flist
)
118 /* This overwrites the progress line */
119 rprintf(FINFO
, "%d file%sto consider\n",
120 flist
->count
, flist
->count
== 1 ? " " : "s ");
122 rprintf(FINFO
, "done\n");
125 void show_flist_stats(void)
131 static void list_file_entry(struct file_struct
*f
)
136 /* this can happen if duplicate names were removed */
140 permstring(perms
, f
->mode
);
143 if (preserve_links
&& S_ISLNK(f
->mode
)) {
144 rprintf(FINFO
, "%s %11.0f %s %s -> %s\n",
146 (double)f
->length
, timestring(f
->modtime
),
147 safe_fname(f_name(f
)), safe_fname(f
->u
.link
));
151 rprintf(FINFO
, "%s %11.0f %s %s\n",
153 (double)f
->length
, timestring(f
->modtime
),
154 safe_fname(f_name(f
)));
160 * Stat either a symlink or its referent, depending on the settings of
161 * copy_links, copy_unsafe_links, etc.
163 * @retval -1 on error
165 * @retval 0 for success
167 * @post If @p path is a symlink, then @p linkbuf (of size @c
168 * MAXPATHLEN) contains the symlink target.
170 * @post @p buffer contains information about the link or the
171 * referrent as appropriate, if they exist.
173 static int readlink_stat(const char *path
, STRUCT_STAT
*buffer
, char *linkbuf
)
177 return do_stat(path
, buffer
);
178 if (link_stat(path
, buffer
, 0) < 0)
180 if (S_ISLNK(buffer
->st_mode
)) {
181 int l
= readlink((char *)path
, linkbuf
, MAXPATHLEN
- 1);
185 if (copy_unsafe_links
&& unsafe_symlink(linkbuf
, path
)) {
187 rprintf(FINFO
,"copying unsafe symlink \"%s\" -> \"%s\"\n",
188 safe_fname(path
), safe_fname(linkbuf
));
190 return do_stat(path
, buffer
);
195 return do_stat(path
, buffer
);
199 int link_stat(const char *path
, STRUCT_STAT
*buffer
, int follow_dirlinks
)
203 return do_stat(path
, buffer
);
204 if (do_lstat(path
, buffer
) < 0)
206 if (follow_dirlinks
&& S_ISLNK(buffer
->st_mode
)) {
208 if (do_stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
213 return do_stat(path
, buffer
);
217 /* This function is used to check if a file should be included/excluded
218 * from the list of files based on its name and type etc. The value of
219 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
220 static int is_excluded(char *fname
, int is_dir
, int filter_level
)
222 #if 0 /* This currently never happens, so avoid a useless compare. */
223 if (filter_level
== NO_FILTERS
)
227 /* never exclude '.', even if somebody does --exclude '*' */
228 if (fname
[0] == '.' && !fname
[1])
230 /* Handle the -R version of the '.' dir. */
231 if (fname
[0] == '/') {
232 int len
= strlen(fname
);
233 if (fname
[len
-1] == '.' && fname
[len
-2] == '/')
237 if (server_filter_list
.head
238 && check_filter(&server_filter_list
, fname
, is_dir
) < 0)
240 if (filter_level
!= ALL_FILTERS
)
243 && check_filter(&filter_list
, fname
, is_dir
) < 0)
248 static int to_wire_mode(mode_t mode
)
251 if (S_ISLNK(mode
) && (_S_IFLNK
!= 0120000))
252 return (mode
& ~(_S_IFMT
)) | 0120000;
257 static mode_t
from_wire_mode(int mode
)
259 if ((mode
& (_S_IFMT
)) == 0120000 && (_S_IFLNK
!= 0120000))
260 return (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
265 static void send_directory(int f
, struct file_list
*flist
,
266 char *fbuf
, int len
);
268 static char *flist_dir
;
269 static int flist_dir_len
;
273 * Make sure @p flist is big enough to hold at least @p flist->count
276 void flist_expand(struct file_list
*flist
)
278 struct file_struct
**new_ptr
;
280 if (flist
->count
< flist
->malloced
)
283 if (flist
->malloced
< FLIST_START
)
284 flist
->malloced
= FLIST_START
;
285 else if (flist
->malloced
>= FLIST_LINEAR
)
286 flist
->malloced
+= FLIST_LINEAR
;
288 flist
->malloced
*= 2;
291 * In case count jumped or we are starting the list
292 * with a known size just set it.
294 if (flist
->malloced
< flist
->count
)
295 flist
->malloced
= flist
->count
;
297 new_ptr
= realloc_array(flist
->files
, struct file_struct
*,
300 if (verbose
>= 2 && flist
->malloced
!= FLIST_START
) {
301 rprintf(FINFO
, "[%s] expand file_list to %.0f bytes, did%s move\n",
303 (double)sizeof flist
->files
[0] * flist
->malloced
,
304 (new_ptr
== flist
->files
) ? " not" : "");
307 flist
->files
= new_ptr
;
310 out_of_memory("flist_expand");
313 void send_file_entry(struct file_struct
*file
, int f
, unsigned short base_flags
)
315 unsigned short flags
;
316 static time_t modtime
;
320 static uint32 rdev_major
;
323 static char lastname
[MAXPATHLEN
];
324 char fname
[MAXPATHLEN
];
332 modtime
= 0, mode
= 0;
333 dev
= 0, rdev
= makedev(0, 0);
340 io_write_phase
= "send_file_entry";
342 f_name_to(file
, fname
);
346 if (file
->mode
== mode
)
347 flags
|= XMIT_SAME_MODE
;
350 if (preserve_devices
) {
351 if (protocol_version
< 28) {
352 if (IS_DEVICE(mode
)) {
353 if (file
->u
.rdev
== rdev
)
354 flags
|= XMIT_SAME_RDEV_pre28
;
358 rdev
= makedev(0, 0);
359 } else if (IS_DEVICE(mode
)) {
361 if ((uint32
)major(rdev
) == rdev_major
)
362 flags
|= XMIT_SAME_RDEV_MAJOR
;
364 rdev_major
= major(rdev
);
365 if ((uint32
)minor(rdev
) <= 0xFFu
)
366 flags
|= XMIT_RDEV_MINOR_IS_SMALL
;
369 if (file
->uid
== uid
)
370 flags
|= XMIT_SAME_UID
;
373 if (file
->gid
== gid
)
374 flags
|= XMIT_SAME_GID
;
377 if (file
->modtime
== modtime
)
378 flags
|= XMIT_SAME_TIME
;
380 modtime
= file
->modtime
;
382 #ifdef SUPPORT_HARD_LINKS
383 if (file
->link_u
.idev
) {
384 if (file
->F_DEV
== dev
) {
385 if (protocol_version
>= 28)
386 flags
|= XMIT_SAME_DEV
;
389 flags
|= XMIT_HAS_IDEV_DATA
;
394 lastname
[l1
] && (fname
[l1
] == lastname
[l1
]) && (l1
< 255);
396 l2
= strlen(fname
+l1
);
399 flags
|= XMIT_SAME_NAME
;
401 flags
|= XMIT_LONG_NAME
;
403 /* We must make sure we don't send a zero flag byte or the
404 * other end will terminate the flist transfer. Note that
405 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
406 * it's harmless way to add a bit to the first flag byte. */
407 if (protocol_version
>= 28) {
408 if (!flags
&& !S_ISDIR(mode
))
409 flags
|= XMIT_TOP_DIR
;
410 if ((flags
& 0xFF00) || !flags
) {
411 flags
|= XMIT_EXTENDED_FLAGS
;
412 write_byte(f
, flags
);
413 write_byte(f
, flags
>> 8);
415 write_byte(f
, flags
);
417 if (!(flags
& 0xFF) && !S_ISDIR(mode
))
418 flags
|= XMIT_TOP_DIR
;
420 flags
|= XMIT_LONG_NAME
;
421 write_byte(f
, flags
);
423 if (flags
& XMIT_SAME_NAME
)
425 if (flags
& XMIT_LONG_NAME
)
429 write_buf(f
, fname
+ l1
, l2
);
431 write_longint(f
, file
->length
);
432 if (!(flags
& XMIT_SAME_TIME
))
433 write_int(f
, modtime
);
434 if (!(flags
& XMIT_SAME_MODE
))
435 write_int(f
, to_wire_mode(mode
));
436 if (preserve_uid
&& !(flags
& XMIT_SAME_UID
)) {
441 if (preserve_gid
&& !(flags
& XMIT_SAME_GID
)) {
446 if (preserve_devices
&& IS_DEVICE(mode
)) {
447 if (protocol_version
< 28) {
448 if (!(flags
& XMIT_SAME_RDEV_pre28
))
449 write_int(f
, (int)rdev
);
451 if (!(flags
& XMIT_SAME_RDEV_MAJOR
))
452 write_int(f
, major(rdev
));
453 if (flags
& XMIT_RDEV_MINOR_IS_SMALL
)
454 write_byte(f
, minor(rdev
));
456 write_int(f
, minor(rdev
));
461 if (preserve_links
&& S_ISLNK(mode
)) {
462 int len
= strlen(file
->u
.link
);
464 write_buf(f
, file
->u
.link
, len
);
468 #ifdef SUPPORT_HARD_LINKS
469 if (flags
& XMIT_HAS_IDEV_DATA
) {
470 if (protocol_version
< 26) {
471 /* 32-bit dev_t and ino_t */
473 write_int(f
, file
->F_INODE
);
475 /* 64-bit dev_t and ino_t */
476 if (!(flags
& XMIT_SAME_DEV
))
477 write_longint(f
, dev
);
478 write_longint(f
, file
->F_INODE
);
483 if (always_checksum
) {
487 else if (protocol_version
< 28) {
488 /* Prior to 28, we sent a useless set of nulls. */
494 protocol_version
< 21 ? 2 : MD4_SUM_LENGTH
);
498 strlcpy(lastname
, fname
, MAXPATHLEN
);
500 io_write_phase
= "unknown";
505 static struct file_struct
*receive_file_entry(struct file_list
*flist
,
506 unsigned short flags
, int f
)
508 static time_t modtime
;
512 static uint32 rdev_major
;
515 static char lastname
[MAXPATHLEN
], *lastdir
;
516 static int lastdir_depth
, lastdir_len
= -1;
517 static unsigned int del_hier_name_len
= 0;
518 static int in_del_hier
= 0;
519 char thisname
[MAXPATHLEN
];
520 unsigned int l1
= 0, l2
= 0;
521 int alloc_len
, basename_len
, dirname_len
, linkname_len
, sum_len
;
523 char *basename
, *dirname
, *bp
;
524 struct file_struct
*file
;
527 modtime
= 0, mode
= 0;
528 dev
= 0, rdev
= makedev(0, 0);
537 if (flags
& XMIT_SAME_NAME
)
540 if (flags
& XMIT_LONG_NAME
)
545 if (l2
>= MAXPATHLEN
- l1
) {
547 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
548 flags
, l1
, l2
, safe_fname(lastname
));
549 overflow("receive_file_entry");
552 strlcpy(thisname
, lastname
, l1
+ 1);
553 read_sbuf(f
, &thisname
[l1
], l2
);
554 thisname
[l1
+ l2
] = 0;
556 strlcpy(lastname
, thisname
, MAXPATHLEN
);
558 clean_fname(thisname
, 0);
561 sanitize_path(thisname
, thisname
, "", 0);
563 if ((basename
= strrchr(thisname
, '/')) != NULL
) {
564 dirname_len
= ++basename
- thisname
; /* counts future '\0' */
565 if (lastdir_len
== dirname_len
- 1
566 && strncmp(thisname
, lastdir
, lastdir_len
) == 0) {
568 dirname_len
= 0; /* indicates no copy is needed */
576 basename_len
= strlen(basename
) + 1; /* count the '\0' */
578 file_length
= read_longint(f
);
579 if (!(flags
& XMIT_SAME_TIME
))
580 modtime
= (time_t)read_int(f
);
581 if (!(flags
& XMIT_SAME_MODE
))
582 mode
= from_wire_mode(read_int(f
));
584 if (preserve_uid
&& !(flags
& XMIT_SAME_UID
))
585 uid
= (uid_t
)read_int(f
);
586 if (preserve_gid
&& !(flags
& XMIT_SAME_GID
))
587 gid
= (gid_t
)read_int(f
);
589 if (preserve_devices
) {
590 if (protocol_version
< 28) {
591 if (IS_DEVICE(mode
)) {
592 if (!(flags
& XMIT_SAME_RDEV_pre28
))
593 rdev
= (dev_t
)read_int(f
);
595 rdev
= makedev(0, 0);
596 } else if (IS_DEVICE(mode
)) {
598 if (!(flags
& XMIT_SAME_RDEV_MAJOR
))
599 rdev_major
= read_int(f
);
600 if (flags
& XMIT_RDEV_MINOR_IS_SMALL
)
601 rdev_minor
= read_byte(f
);
603 rdev_minor
= read_int(f
);
604 rdev
= makedev(rdev_major
, rdev_minor
);
609 if (preserve_links
&& S_ISLNK(mode
)) {
610 linkname_len
= read_int(f
) + 1; /* count the '\0' */
611 if (linkname_len
<= 0 || linkname_len
> MAXPATHLEN
) {
612 rprintf(FERROR
, "overflow: linkname_len=%d\n",
614 overflow("receive_file_entry");
621 sum_len
= always_checksum
&& S_ISREG(mode
) ? MD4_SUM_LENGTH
: 0;
623 alloc_len
= file_struct_len
+ dirname_len
+ basename_len
624 + linkname_len
+ sum_len
;
625 bp
= pool_alloc(flist
->file_pool
, alloc_len
, "receive_file_entry");
627 file
= (struct file_struct
*)bp
;
628 memset(bp
, 0, file_struct_len
);
629 bp
+= file_struct_len
;
632 file
->modtime
= modtime
;
633 file
->length
= file_length
;
639 file
->dirname
= lastdir
= bp
;
640 lastdir_len
= dirname_len
- 1;
641 memcpy(bp
, dirname
, dirname_len
- 1);
644 lastdir_depth
= count_dir_elements(lastdir
);
645 file
->dir
.depth
= lastdir_depth
+ 1;
646 } else if (dirname
) {
647 file
->dirname
= dirname
; /* we're reusing lastname */
648 file
->dir
.depth
= lastdir_depth
+ 1;
653 if (basename_len
== 1+1 && *basename
== '.') /* +1 for '\0' */
655 if (flags
& XMIT_TOP_DIR
) {
657 del_hier_name_len
= file
->dir
.depth
== 0 ? 0 : l1
+ l2
;
658 file
->flags
|= FLAG_TOP_DIR
| FLAG_DEL_HERE
;
659 } else if (in_del_hier
) {
660 if (!relative_paths
|| !del_hier_name_len
661 || (l1
>= del_hier_name_len
662 && thisname
[del_hier_name_len
] == '/'))
663 file
->flags
|= FLAG_DEL_HERE
;
670 memcpy(bp
, basename
, basename_len
);
673 if (preserve_devices
&& IS_DEVICE(mode
))
679 read_sbuf(f
, bp
, linkname_len
- 1);
681 sanitize_path(bp
, bp
, "", lastdir_depth
);
686 #ifdef SUPPORT_HARD_LINKS
687 if (preserve_hard_links
&& protocol_version
< 28 && S_ISREG(mode
))
688 flags
|= XMIT_HAS_IDEV_DATA
;
689 if (flags
& XMIT_HAS_IDEV_DATA
) {
691 if (protocol_version
< 26) {
695 if (!(flags
& XMIT_SAME_DEV
))
696 dev
= read_longint(f
);
697 inode
= read_longint(f
);
699 if (flist
->hlink_pool
) {
700 file
->link_u
.idev
= pool_talloc(flist
->hlink_pool
,
701 struct idev
, 1, "inode_table");
702 file
->F_INODE
= inode
;
708 if (always_checksum
) {
711 file
->u
.sum
= sum
= bp
;
713 } else if (protocol_version
< 28) {
714 /* Prior to 28, we get a useless set of nulls. */
720 protocol_version
< 21 ? 2 : MD4_SUM_LENGTH
);
724 if (!preserve_perms
) {
725 /* set an appropriate set of permissions based on original
726 * permissions and umask. This emulates what GNU cp does */
727 file
->mode
&= ~orig_umask
;
735 * Create a file_struct for a named file by reading its stat()
736 * information and performing extensive checks against global
739 * @return the new file, or NULL if there was an error or this file
740 * should be excluded.
742 * @todo There is a small optimization opportunity here to avoid
743 * stat()ing the file in some circumstances, which has a certain cost.
744 * We are called immediately after doing readdir(), and so we may
745 * already know the d_type of the file. We could for example avoid
746 * statting directories if we're not recursing, but this is not a very
747 * important case. Some systems may not have d_type.
749 struct file_struct
*make_file(char *fname
, struct file_list
*flist
,
752 static char *lastdir
;
753 static int lastdir_len
= -1;
754 struct file_struct
*file
;
756 char sum
[SUM_LENGTH
];
757 char thisname
[MAXPATHLEN
];
758 char linkname
[MAXPATHLEN
];
759 int alloc_len
, basename_len
, dirname_len
, linkname_len
, sum_len
;
760 char *basename
, *dirname
, *bp
;
761 unsigned short flags
= 0;
763 if (!flist
|| !flist
->count
) /* Ignore lastdir when invalid. */
766 if (strlcpy(thisname
, fname
, sizeof thisname
)
767 >= sizeof thisname
- flist_dir_len
) {
768 rprintf(FINFO
, "skipping overly long name: %s\n",
772 clean_fname(thisname
, 0);
774 sanitize_path(thisname
, thisname
, "", 0);
776 memset(sum
, 0, SUM_LENGTH
);
778 if (readlink_stat(thisname
, &st
, linkname
) != 0) {
779 int save_errno
= errno
;
780 /* See if file is excluded before reporting an error. */
781 if (filter_level
!= NO_FILTERS
782 && is_excluded(thisname
, 0, filter_level
))
784 if (save_errno
== ENOENT
) {
786 /* Avoid "vanished" error if symlink points nowhere. */
787 if (copy_links
&& do_lstat(thisname
, &st
) == 0
788 && S_ISLNK(st
.st_mode
)) {
789 io_error
|= IOERR_GENERAL
;
790 rprintf(FERROR
, "symlink has no referent: %s\n",
791 full_fname(thisname
));
795 enum logcode c
= am_daemon
&& protocol_version
< 28
797 io_error
|= IOERR_VANISHED
;
798 rprintf(c
, "file has vanished: %s\n",
799 full_fname(thisname
));
802 io_error
|= IOERR_GENERAL
;
803 rsyserr(FERROR
, save_errno
, "readlink %s failed",
804 full_fname(thisname
));
809 /* backup.c calls us with filter_level set to NO_FILTERS. */
810 if (filter_level
== NO_FILTERS
)
813 if (S_ISDIR(st
.st_mode
) && !xfer_dirs
) {
814 rprintf(FINFO
, "skipping directory %s\n", safe_fname(thisname
));
818 /* We only care about directories because we need to avoid recursing
819 * into a mount-point directory, not to avoid copying a symlinked
820 * file if -L (or similar) was specified. */
821 if (one_file_system
&& st
.st_dev
!= filesystem_dev
822 && S_ISDIR(st
.st_mode
))
823 flags
|= FLAG_MOUNT_POINT
;
825 if (is_excluded(thisname
, S_ISDIR(st
.st_mode
) != 0, filter_level
))
828 if (lp_ignore_nonreadable(module_id
)) {
830 if (!S_ISLNK(st
.st_mode
))
832 if (access(thisname
, R_OK
) != 0)
839 rprintf(FINFO
, "[%s] make_file(%s,*,%d)\n",
840 who_am_i(), safe_fname(thisname
), filter_level
);
843 if ((basename
= strrchr(thisname
, '/')) != NULL
) {
844 dirname_len
= ++basename
- thisname
; /* counts future '\0' */
845 if (lastdir_len
== dirname_len
- 1
846 && strncmp(thisname
, lastdir
, lastdir_len
) == 0) {
848 dirname_len
= 0; /* indicates no copy is needed */
856 basename_len
= strlen(basename
) + 1; /* count the '\0' */
859 linkname_len
= S_ISLNK(st
.st_mode
) ? strlen(linkname
) + 1 : 0;
864 sum_len
= always_checksum
&& S_ISREG(st
.st_mode
) ? MD4_SUM_LENGTH
: 0;
866 alloc_len
= file_struct_len
+ dirname_len
+ basename_len
867 + linkname_len
+ sum_len
;
869 bp
= pool_alloc(flist
->file_pool
, alloc_len
,
870 "receive_file_entry");
872 if (!(bp
= new_array(char, alloc_len
)))
873 out_of_memory("receive_file_entry");
876 file
= (struct file_struct
*)bp
;
877 memset(bp
, 0, file_struct_len
);
878 bp
+= file_struct_len
;
881 file
->modtime
= st
.st_mtime
;
882 file
->length
= st
.st_size
;
883 file
->mode
= st
.st_mode
;
884 file
->uid
= st
.st_uid
;
885 file
->gid
= st
.st_gid
;
887 #ifdef SUPPORT_HARD_LINKS
888 if (flist
&& flist
->hlink_pool
) {
889 if (protocol_version
< 28) {
890 if (S_ISREG(st
.st_mode
))
891 file
->link_u
.idev
= pool_talloc(
892 flist
->hlink_pool
, struct idev
, 1,
895 if (!S_ISDIR(st
.st_mode
) && st
.st_nlink
> 1)
896 file
->link_u
.idev
= pool_talloc(
897 flist
->hlink_pool
, struct idev
, 1,
901 if (file
->link_u
.idev
) {
902 file
->F_DEV
= st
.st_dev
;
903 file
->F_INODE
= st
.st_ino
;
908 file
->dirname
= lastdir
= bp
;
909 lastdir_len
= dirname_len
- 1;
910 memcpy(bp
, dirname
, dirname_len
- 1);
914 file
->dirname
= dirname
;
917 memcpy(bp
, basename
, basename_len
);
920 #ifdef HAVE_STRUCT_STAT_ST_RDEV
921 if (preserve_devices
&& IS_DEVICE(st
.st_mode
))
922 file
->u
.rdev
= st
.st_rdev
;
928 memcpy(bp
, linkname
, linkname_len
);
935 file_checksum(thisname
, bp
, st
.st_size
);
939 file
->dir
.root
= flist_dir
;
941 /* This code is only used by the receiver when it is building
942 * a list of files for a delete pass. */
943 if (keep_dirlinks
&& linkname_len
&& flist
) {
945 int save_mode
= file
->mode
;
946 file
->mode
= S_IFDIR
; /* find a directory w/our name */
947 if (flist_find(the_file_list
, file
) >= 0
948 && do_stat(thisname
, &st2
) == 0 && S_ISDIR(st2
.st_mode
)) {
949 file
->modtime
= st2
.st_mtime
;
950 file
->length
= st2
.st_size
;
951 file
->mode
= st2
.st_mode
;
952 file
->uid
= st2
.st_uid
;
953 file
->gid
= st2
.st_gid
;
956 file
->mode
= save_mode
;
959 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
960 stats
.total_size
+= st
.st_size
;
966 static struct file_struct
*send_file_name(int f
, struct file_list
*flist
,
967 char *fname
, unsigned short base_flags
)
969 struct file_struct
*file
;
971 file
= make_file(fname
, flist
, f
== -2 ? SERVER_FILTERS
: ALL_FILTERS
);
975 maybe_emit_filelist_progress(flist
->count
+ flist_count_offset
);
979 if (file
->basename
[0]) {
980 flist
->files
[flist
->count
++] = file
;
981 send_file_entry(file
, f
, base_flags
);
986 static void send_if_directory(int f
, struct file_list
*flist
,
987 struct file_struct
*file
)
989 char fbuf
[MAXPATHLEN
];
991 if (S_ISDIR(file
->mode
)
992 && !(file
->flags
& FLAG_MOUNT_POINT
) && f_name_to(file
, fbuf
)) {
994 unsigned int len
= strlen(fbuf
);
995 if (len
> 1 && fbuf
[len
-1] == '/')
997 if (len
>= MAXPATHLEN
- 1) {
998 io_error
|= IOERR_GENERAL
;
999 rprintf(FERROR
, "skipping long-named directory: %s\n",
1003 save_filters
= push_local_filters(fbuf
, len
);
1004 send_directory(f
, flist
, fbuf
, len
);
1005 pop_local_filters(save_filters
);
1010 /* This function is normally called by the sender, but the receiving side also
1011 * calls it from delete_in_dir() with f set to -1 so that we just construct the
1012 * file list in memory without sending it over the wire. Also, get_dirlist()
1013 * might call this with f set to -2, which also indicates that local filter
1014 * rules should be ignored. */
1015 static void send_directory(int f
, struct file_list
*flist
,
1016 char *fbuf
, int len
)
1022 int start
= flist
->count
;
1024 if (!(d
= opendir(fbuf
))) {
1025 io_error
|= IOERR_GENERAL
;
1026 rsyserr(FERROR
, errno
, "opendir %s failed", full_fname(fbuf
));
1031 if (len
!= 1 || *fbuf
!= '/')
1034 remainder
= MAXPATHLEN
- (p
- fbuf
);
1036 for (errno
= 0, di
= readdir(d
); di
; errno
= 0, di
= readdir(d
)) {
1037 char *dname
= d_name(di
);
1038 if (dname
[0] == '.' && (dname
[1] == '\0'
1039 || (dname
[1] == '.' && dname
[2] == '\0')))
1041 if (strlcpy(p
, dname
, remainder
) < remainder
)
1042 send_file_name(f
, flist
, fbuf
, 0);
1044 io_error
|= IOERR_GENERAL
;
1046 "cannot send long-named file %s\n",
1054 io_error
|= IOERR_GENERAL
;
1055 rsyserr(FERROR
, errno
, "readdir(%s)", full_fname(fbuf
));
1061 int i
, end
= flist
->count
- 1;
1062 for (i
= start
; i
<= end
; i
++)
1063 send_if_directory(f
, flist
, flist
->files
[i
]);
1068 struct file_list
*send_file_list(int f
, int argc
, char *argv
[])
1072 char *p
, *dir
, olddir
[sizeof curr_dir
];
1073 char lastpath
[MAXPATHLEN
] = "";
1074 struct file_list
*flist
;
1075 struct timeval start_tv
, end_tv
;
1079 if (show_filelist_p())
1080 start_filelist_progress("building file list");
1082 start_write
= stats
.total_written
;
1083 gettimeofday(&start_tv
, NULL
);
1085 flist
= flist_new(WITH_HLINK
, "send_file_list");
1087 io_start_buffering_out();
1088 if (filesfrom_fd
>= 0) {
1089 if (argv
[0] && !push_dir(argv
[0])) {
1090 rsyserr(FERROR
, errno
, "push_dir %s failed",
1091 full_fname(argv
[0]));
1092 exit_cleanup(RERR_FILESELECT
);
1098 struct file_struct
*file
;
1099 char fname2
[MAXPATHLEN
];
1100 char *fname
= fname2
;
1104 if (read_filesfrom_line(filesfrom_fd
, fname
) == 0)
1106 sanitize_path(fname
, fname
, "", 0);
1110 strlcpy(fname
, *argv
++, MAXPATHLEN
);
1112 sanitize_path(fname
, fname
, "", 0);
1116 if (!l
|| fname
[l
- 1] == '/') {
1117 if (l
== 2 && fname
[0] == '.') {
1118 /* Turn "./" into just "." rather than "./." */
1120 } else if (l
< MAXPATHLEN
) {
1126 is_dot_dir
= fname
[l
-1] == '.'
1127 && (l
== 1 || fname
[l
-2] == '/');
1130 if (link_stat(fname
, &st
, keep_dirlinks
) != 0) {
1131 io_error
|= IOERR_GENERAL
;
1132 rsyserr(FERROR
, errno
, "link_stat %s failed",
1137 if (S_ISDIR(st
.st_mode
) && !xfer_dirs
) {
1138 rprintf(FINFO
, "skipping directory %s\n",
1146 if (!relative_paths
) {
1147 p
= strrchr(fname
, '/');
1156 } else if (implied_dirs
&& (p
=strrchr(fname
,'/')) && p
!= fname
) {
1157 /* this ensures we send the intermediate directories,
1158 thus getting their permissions right */
1159 char *lp
= lastpath
, *fn
= fname
, *slash
= fname
;
1161 /* Skip any initial directories in our path that we
1162 * have in common with lastpath. */
1163 while (*fn
&& *lp
== *fn
) {
1169 if (fn
!= p
|| (*lp
&& *lp
!= '/')) {
1170 int save_copy_links
= copy_links
;
1171 int save_xfer_dirs
= xfer_dirs
;
1172 copy_links
= copy_unsafe_links
;
1174 while ((slash
= strchr(slash
+1, '/')) != 0) {
1176 send_file_name(f
, flist
, fname
, 0);
1179 copy_links
= save_copy_links
;
1180 xfer_dirs
= save_xfer_dirs
;
1182 strlcpy(lastpath
, fname
, sizeof lastpath
);
1191 static char *lastdir
;
1192 static int lastdir_len
;
1194 strcpy(olddir
, curr_dir
); /* can't overflow */
1196 if (!push_dir(dir
)) {
1197 io_error
|= IOERR_GENERAL
;
1198 rsyserr(FERROR
, errno
, "push_dir %s failed",
1203 if (lastdir
&& strcmp(lastdir
, dir
) == 0) {
1204 flist_dir
= lastdir
;
1205 flist_dir_len
= lastdir_len
;
1207 flist_dir
= lastdir
= strdup(dir
);
1208 flist_dir_len
= lastdir_len
= strlen(dir
);
1212 if (one_file_system
)
1213 filesystem_dev
= st
.st_dev
;
1215 if ((file
= send_file_name(f
, flist
, fname
, XMIT_TOP_DIR
))) {
1216 if (recurse
|| (xfer_dirs
&& is_dot_dir
))
1217 send_if_directory(f
, flist
, file
);
1223 if (!pop_dir(olddir
)) {
1224 rsyserr(FERROR
, errno
, "pop_dir %s failed",
1226 exit_cleanup(RERR_FILESELECT
);
1231 gettimeofday(&end_tv
, NULL
);
1232 stats
.flist_buildtime
= (int64
)(end_tv
.tv_sec
- start_tv
.tv_sec
) * 1000
1233 + (end_tv
.tv_usec
- start_tv
.tv_usec
) / 1000;
1234 if (stats
.flist_buildtime
== 0)
1235 stats
.flist_buildtime
= 1;
1238 send_file_entry(NULL
, f
, 0);
1240 if (show_filelist_p())
1241 finish_filelist_progress(flist
);
1243 gettimeofday(&end_tv
, NULL
);
1244 stats
.flist_xfertime
= (int64
)(end_tv
.tv_sec
- start_tv
.tv_sec
) * 1000
1245 + (end_tv
.tv_usec
- start_tv
.tv_usec
) / 1000;
1247 if (flist
->hlink_pool
) {
1248 pool_destroy(flist
->hlink_pool
);
1249 flist
->hlink_pool
= NULL
;
1252 /* Sort the list without removing any duplicates. This allows the
1253 * receiving side to ask for any name they like, which gives us the
1254 * flexibility to change the way we unduplicate names in the future
1255 * without causing a compatibility problem with older versions. */
1256 clean_flist(flist
, 0, 0);
1258 /* Now send the uid/gid list. This was introduced in
1259 * protocol version 15 */
1262 /* send the io_error flag */
1263 write_int(f
, lp_ignore_errors(module_id
) ? 0 : io_error
);
1266 stats
.flist_size
= stats
.total_written
- start_write
;
1267 stats
.num_files
= flist
->count
;
1270 output_flist(flist
);
1273 rprintf(FINFO
, "send_file_list done\n");
1279 struct file_list
*recv_file_list(int f
)
1281 struct file_list
*flist
;
1282 unsigned short flags
;
1285 if (show_filelist_p())
1286 start_filelist_progress("receiving file list");
1288 start_read
= stats
.total_read
;
1290 flist
= flist_new(WITH_HLINK
, "recv_file_list");
1293 flist
->malloced
= 1000;
1294 flist
->files
= new_array(struct file_struct
*, flist
->malloced
);
1299 while ((flags
= read_byte(f
)) != 0) {
1300 struct file_struct
*file
;
1302 flist_expand(flist
);
1304 if (protocol_version
>= 28 && (flags
& XMIT_EXTENDED_FLAGS
))
1305 flags
|= read_byte(f
) << 8;
1306 file
= receive_file_entry(flist
, flags
, f
);
1308 if (S_ISREG(file
->mode
))
1309 stats
.total_size
+= file
->length
;
1311 flist
->files
[flist
->count
++] = file
;
1313 maybe_emit_filelist_progress(flist
->count
);
1316 rprintf(FINFO
, "recv_file_name(%s)\n",
1317 safe_fname(f_name(file
)));
1320 receive_file_entry(NULL
, 0, 0); /* Signal that we're done. */
1323 rprintf(FINFO
, "received %d names\n", flist
->count
);
1325 if (show_filelist_p())
1326 finish_filelist_progress(flist
);
1328 clean_flist(flist
, relative_paths
, 1);
1331 /* Now send the uid/gid list. This was introduced in
1332 * protocol version 15 */
1333 recv_uid_list(f
, flist
);
1335 /* Recv the io_error flag */
1336 if (lp_ignore_errors(module_id
) || ignore_errors
)
1339 io_error
|= read_int(f
);
1343 output_flist(flist
);
1347 for (i
= 0; i
< flist
->count
; i
++)
1348 list_file_entry(flist
->files
[i
]);
1352 rprintf(FINFO
, "recv_file_list done\n");
1354 stats
.flist_size
= stats
.total_read
- start_read
;
1355 stats
.num_files
= flist
->count
;
1360 out_of_memory("recv_file_list");
1361 return NULL
; /* not reached */
1365 static int file_compare(struct file_struct
**file1
, struct file_struct
**file2
)
1367 return f_name_cmp(*file1
, *file2
);
1371 /* Search for an identically-named item in the file list. Note that the
1372 * items must agree in their directory-ness, or no match is returned. */
1373 int flist_find(struct file_list
*flist
, struct file_struct
*f
)
1375 int low
= flist
->low
, high
= flist
->high
;
1376 int ret
, mid
, mid_up
;
1378 while (low
<= high
) {
1379 mid
= (low
+ high
) / 2;
1380 for (mid_up
= mid
; !flist
->files
[mid_up
]->basename
; mid_up
++) {}
1382 ret
= f_name_cmp(flist
->files
[mid_up
], f
);
1386 if (protocol_version
< 29
1387 && S_ISDIR(flist
->files
[mid_up
]->mode
)
1388 != S_ISDIR(f
->mode
))
1402 * Free up any resources a file_struct has allocated
1403 * and clear the file.
1405 void clear_file(int i
, struct file_list
*flist
)
1407 if (flist
->hlink_pool
&& flist
->files
[i
]->link_u
.idev
)
1408 pool_free(flist
->hlink_pool
, 0, flist
->files
[i
]->link_u
.idev
);
1409 memset(flist
->files
[i
], 0, file_struct_len
);
1414 * allocate a new file list
1416 struct file_list
*flist_new(int with_hlink
, char *msg
)
1418 struct file_list
*flist
;
1420 flist
= new(struct file_list
);
1424 memset(flist
, 0, sizeof (struct file_list
));
1426 if (!(flist
->file_pool
= pool_create(FILE_EXTENT
, 0,
1427 out_of_memory
, POOL_INTERN
)))
1430 #ifdef SUPPORT_HARD_LINKS
1431 if (with_hlink
&& preserve_hard_links
) {
1432 if (!(flist
->hlink_pool
= pool_create(HLINK_EXTENT
,
1433 sizeof (struct idev
), out_of_memory
, POOL_INTERN
)))
1442 * free up all elements in a flist
1444 void flist_free(struct file_list
*flist
)
1446 pool_destroy(flist
->file_pool
);
1447 pool_destroy(flist
->hlink_pool
);
1454 * This routine ensures we don't have any duplicate names in our file list.
1455 * duplicate names can cause corruption because of the pipelining
1457 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
)
1461 if (!flist
|| flist
->count
== 0)
1464 sorting_flist
= flist
;
1465 qsort(flist
->files
, flist
->count
,
1466 sizeof flist
->files
[0], (int (*)())file_compare
);
1467 sorting_flist
= NULL
;
1469 for (i
= no_dups
? 0 : flist
->count
; i
< flist
->count
; i
++) {
1470 if (flist
->files
[i
]->basename
) {
1475 flist
->low
= prev_i
;
1476 while (++i
< flist
->count
) {
1478 struct file_struct
*file
= flist
->files
[i
];
1480 if (!file
->basename
)
1482 if (f_name_cmp(file
, flist
->files
[prev_i
]) == 0)
1484 else if (protocol_version
>= 29 && S_ISDIR(file
->mode
)) {
1485 int save_mode
= file
->mode
;
1486 /* Make sure that this directory doesn't duplicate a
1487 * non-directory earlier in the list. */
1488 flist
->high
= prev_i
;
1489 file
->mode
= S_IFREG
;
1490 j
= flist_find(flist
, file
);
1491 file
->mode
= save_mode
;
1495 struct file_struct
*fp
= flist
->files
[j
];
1497 /* If one is a dir and the other is not, we want to
1498 * keep the dir because it might have contents in the
1500 if (S_ISDIR(file
->mode
) != S_ISDIR(fp
->mode
)) {
1501 if (S_ISDIR(file
->mode
))
1507 if (verbose
> 1 && !am_server
) {
1509 "removing duplicate name %s from file list (%d)\n",
1510 safe_fname(f_name(file
)), drop
);
1512 /* Make sure that if we unduplicate '.', that we don't
1513 * lose track of a user-specified top directory. */
1514 if (flist
->files
[drop
]->flags
& FLAG_TOP_DIR
)
1515 flist
->files
[keep
]->flags
|= FLAG_TOP_DIR
;
1517 clear_file(drop
, flist
);
1520 if (flist
->low
== drop
) {
1522 j
< i
&& !flist
->files
[j
]->basename
;
1531 flist
->high
= no_dups
? prev_i
: flist
->count
- 1;
1534 /* We need to strip off the leading slashes for relative
1535 * paths, but this must be done _after_ the sorting phase. */
1536 for (i
= flist
->low
; i
<= flist
->high
; i
++) {
1537 struct file_struct
*file
= flist
->files
[i
];
1541 if (*file
->dirname
== '/') {
1542 char *s
= file
->dirname
+ 1;
1543 while (*s
== '/') s
++;
1544 memmove(file
->dirname
, s
, strlen(s
) + 1);
1547 if (!*file
->dirname
)
1548 file
->dirname
= NULL
;
1554 static void output_flist(struct file_list
*flist
)
1556 char uidbuf
[16], gidbuf
[16], depthbuf
[16];
1557 struct file_struct
*file
;
1558 const char *who
= who_am_i();
1561 for (i
= 0; i
< flist
->count
; i
++) {
1562 file
= flist
->files
[i
];
1563 if ((am_root
|| am_sender
) && preserve_uid
)
1564 sprintf(uidbuf
, " uid=%ld", (long)file
->uid
);
1567 if (preserve_gid
&& file
->gid
!= GID_NONE
)
1568 sprintf(gidbuf
, " gid=%ld", (long)file
->gid
);
1572 sprintf(depthbuf
, "%d", file
->dir
.depth
);
1573 rprintf(FINFO
, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1574 who
, i
, am_sender
? NS(file
->dir
.root
) : depthbuf
,
1575 file
->dirname
? safe_fname(file
->dirname
) : "",
1576 file
->dirname
? "/" : "", NS(file
->basename
),
1577 S_ISDIR(file
->mode
) ? "/" : "", (int)file
->mode
,
1578 (double)file
->length
, uidbuf
, gidbuf
, file
->flags
);
1583 enum fnc_state
{ s_DIR
, s_SLASH
, s_BASE
, s_TRAILING
};
1584 enum fnc_type
{ t_PATH
, t_ITEM
};
1586 /* Compare the names of two file_struct entities, similar to how strcmp()
1587 * would do if it were operating on the joined strings.
1589 * Some differences beginning with protocol_version 29: (1) directory names
1590 * are compared with an assumed trailing slash so that they compare in a
1591 * way that would cause them to sort immediately prior to any content they
1592 * may have; (2) a directory of any name compares after a non-directory of
1593 * any name at the same depth; (3) a directory with name "." compares prior
1594 * to anything else. These changes mean that a directory and a non-dir
1595 * with the same name will not compare as equal (protocol_version >= 29).
1597 * The dirname component can be an empty string, but the basename component
1598 * cannot (and never is in the current codebase). The basename component
1599 * may be NULL (for a removed item), in which case it is considered to be
1600 * after any existing item. */
1601 int f_name_cmp(struct file_struct
*f1
, struct file_struct
*f2
)
1604 const uchar
*c1
, *c2
;
1605 enum fnc_state state1
, state2
;
1606 enum fnc_type type1
, type2
;
1607 enum fnc_type t_path
= protocol_version
>= 29 ? t_PATH
: t_ITEM
;
1609 if (!f1
|| !f1
->basename
) {
1610 if (!f2
|| !f2
->basename
)
1614 if (!f2
|| !f2
->basename
)
1617 c1
= (uchar
*)f1
->dirname
;
1618 c2
= (uchar
*)f2
->dirname
;
1622 type1
= S_ISDIR(f1
->mode
) ? t_path
: t_ITEM
;
1623 c1
= (uchar
*)f1
->basename
;
1624 if (type1
== t_PATH
&& *c1
== '.' && !c1
[1]) {
1626 state1
= s_TRAILING
;
1639 type2
= S_ISDIR(f2
->mode
) ? t_path
: t_ITEM
;
1640 c2
= (uchar
*)f2
->basename
;
1641 if (type2
== t_PATH
&& *c2
== '.' && !c2
[1]) {
1643 state2
= s_TRAILING
;
1657 return type1
== t_PATH
? 1 : -1;
1660 if ((dif
= (int)*c1
++ - (int)*c2
++) != 0)
1669 type1
= S_ISDIR(f1
->mode
) ? t_path
: t_ITEM
;
1671 c1
= (uchar
*)f1
->basename
;
1674 state1
= s_TRAILING
;
1675 if (type1
== t_PATH
) {
1684 if (*c2
&& type1
!= type2
)
1685 return type1
== t_PATH
? 1 : -1;
1694 type2
= S_ISDIR(f2
->mode
) ? t_path
: t_ITEM
;
1696 c2
= (uchar
*)f2
->basename
;
1699 state2
= s_TRAILING
;
1700 if (type2
== t_PATH
) {
1712 return type1
== t_PATH
? 1 : -1;
1720 /* Return a copy of the full filename of a flist entry, using the indicated
1721 * buffer. No size-checking is done because we checked the size when creating
1722 * the file_struct entry.
1724 char *f_name_to(struct file_struct
*f
, char *fbuf
)
1726 if (!f
|| !f
->basename
)
1730 int len
= strlen(f
->dirname
);
1731 memcpy(fbuf
, f
->dirname
, len
);
1733 strcpy(fbuf
+ len
+ 1, f
->basename
);
1735 strcpy(fbuf
, f
->basename
);
1740 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1741 char *f_name(struct file_struct
*f
)
1743 static char names
[5][MAXPATHLEN
];
1744 static unsigned int n
;
1746 n
= (n
+ 1) % (sizeof names
/ sizeof names
[0]);
1748 return f_name_to(f
, names
[n
]);
1752 /* Do a non-recursive scan of the named directory, possibly ignoring all
1753 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1754 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1755 * buffer (the functions we call will append names onto the end, but the old
1756 * dir value will be restored on exit). */
1757 struct file_list
*get_dirlist(char *dirname
, int dlen
,
1758 int ignore_filter_rules
)
1760 struct file_list
*dirlist
;
1761 char dirbuf
[MAXPATHLEN
];
1762 int save_recurse
= recurse
;
1765 dlen
= strlcpy(dirbuf
, dirname
, MAXPATHLEN
);
1766 if (dlen
>= MAXPATHLEN
)
1771 dirlist
= flist_new(WITHOUT_HLINK
, "get_dirlist");
1774 send_directory(ignore_filter_rules
? -2 : -1, dirlist
, dirname
, dlen
);
1775 recurse
= save_recurse
;
1777 flist_count_offset
+= dirlist
->count
;
1779 clean_flist(dirlist
, 0, 0);
1782 output_flist(dirlist
);