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 * @todo Get rid of the string_area optimization. Efficiently
25 * allocating blocks is the responsibility of the system's malloc
26 * library, not of rsync.
28 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
34 extern struct stats stats
;
37 extern int do_progress
;
39 extern int always_checksum
;
41 extern int cvs_exclude
;
44 extern char *files_from
;
45 extern int filesfrom_fd
;
47 extern int one_file_system
;
48 extern int make_backups
;
49 extern int preserve_links
;
50 extern int preserve_hard_links
;
51 extern int preserve_perms
;
52 extern int preserve_devices
;
53 extern int preserve_uid
;
54 extern int preserve_gid
;
55 extern int preserve_times
;
56 extern int relative_paths
;
57 extern int implied_dirs
;
58 extern int copy_links
;
59 extern int copy_unsafe_links
;
60 extern int protocol_version
;
61 extern int sanitize_paths
;
63 extern int read_batch
;
64 extern int write_batch
;
66 extern struct exclude_struct
**exclude_list
;
67 extern struct exclude_struct
**server_exclude_list
;
68 extern struct exclude_struct
**local_exclude_list
;
72 static struct file_struct null_file
;
74 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
);
77 static int show_filelist_p(void)
79 return verbose
&& (recurse
|| files_from
) && !am_server
;
82 static void start_filelist_progress(char *kind
)
84 rprintf(FINFO
, "%s ... ", kind
);
85 if ((verbose
> 1) || do_progress
)
91 static void emit_filelist_progress(const struct file_list
*flist
)
93 rprintf(FINFO
, " %d files...\r", flist
->count
);
97 static void maybe_emit_filelist_progress(const struct file_list
*flist
)
99 if (do_progress
&& show_filelist_p() && ((flist
->count
% 100) == 0))
100 emit_filelist_progress(flist
);
104 static void finish_filelist_progress(const struct file_list
*flist
)
107 /* This overwrites the progress line */
108 rprintf(FINFO
, "%d file%sto consider\n",
109 flist
->count
, flist
->count
== 1 ? " " : "s ");
111 rprintf(FINFO
, "done\n");
115 void show_flist_stats(void)
121 static struct string_area
*string_area_new(int size
)
123 struct string_area
*a
;
127 a
= new(struct string_area
);
129 out_of_memory("string_area_new");
130 a
->current
= a
->base
= new_array(char, size
);
132 out_of_memory("string_area_new buffer");
133 a
->end
= a
->base
+ size
;
139 static void string_area_free(struct string_area
*a
)
141 struct string_area
*next
;
143 for (; a
; a
= next
) {
149 static char *string_area_malloc(struct string_area
**ap
, int size
)
152 struct string_area
*a
;
154 /* does the request fit into the current space? */
156 if (a
->current
+ size
>= a
->end
) {
157 /* no; get space, move new string_area to front of the list */
158 a
= string_area_new(size
> ARENA_SIZE
? size
: ARENA_SIZE
);
163 /* have space; do the "allocation." */
169 static char *string_area_strdup(struct string_area
**ap
, const char *src
)
171 char *dest
= string_area_malloc(ap
, strlen(src
) + 1);
172 return strcpy(dest
, src
);
175 static void list_file_entry(struct file_struct
*f
)
180 /* this can happen if duplicate names were removed */
183 permstring(perms
, f
->mode
);
185 if (preserve_links
&& S_ISLNK(f
->mode
)) {
186 rprintf(FINFO
, "%s %11.0f %s %s -> %s\n",
188 (double) f
->length
, timestring(f
->modtime
),
191 rprintf(FINFO
, "%s %11.0f %s %s\n",
193 (double) f
->length
, timestring(f
->modtime
),
200 * Stat either a symlink or its referent, depending on the settings of
201 * copy_links, copy_unsafe_links, etc.
203 * @retval -1 on error
205 * @retval 0 for success
207 * @post If @p path is a symlink, then @p linkbuf (of size @c
208 * MAXPATHLEN) contains the symlink target.
210 * @post @p buffer contains information about the link or the
211 * referrent as appropriate, if they exist.
213 int readlink_stat(const char *path
, STRUCT_STAT
* buffer
, char *linkbuf
)
217 return do_stat(path
, buffer
);
219 if (do_lstat(path
, buffer
) == -1) {
222 if (S_ISLNK(buffer
->st_mode
)) {
224 l
= readlink((char *) path
, linkbuf
, MAXPATHLEN
- 1);
228 if (copy_unsafe_links
&& unsafe_symlink(linkbuf
, path
)) {
230 rprintf(FINFO
,"copying unsafe symlink \"%s\" -> \"%s\"\n",
233 return do_stat(path
, buffer
);
238 return do_stat(path
, buffer
);
242 int link_stat(const char *path
, STRUCT_STAT
* buffer
)
246 return do_stat(path
, buffer
);
248 return do_lstat(path
, buffer
);
251 return do_stat(path
, buffer
);
256 * This function is used to check if a file should be included/excluded
257 * from the list of files based on its name and type etc. The value of
258 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
260 static int check_exclude_file(char *fname
, int is_dir
, int exclude_level
)
262 #if 0 /* This currently never happens, so avoid a useless compare. */
263 if (exclude_level
== NO_EXCLUDES
)
267 /* never exclude '.', even if somebody does --exclude '*' */
268 if (fname
[0] == '.' && !fname
[1])
270 /* Handle the -R version of the '.' dir. */
271 if (fname
[0] == '/') {
272 int len
= strlen(fname
);
273 if (fname
[len
-1] == '.' && fname
[len
-2] == '/')
277 if (server_exclude_list
278 && check_exclude(server_exclude_list
, fname
, is_dir
))
280 if (exclude_level
!= ALL_EXCLUDES
)
282 if (exclude_list
&& check_exclude(exclude_list
, fname
, is_dir
))
284 if (local_exclude_list
285 && check_exclude(local_exclude_list
, fname
, is_dir
))
290 /* used by the one_file_system code */
291 static dev_t filesystem_dev
;
293 static void set_filesystem(char *fname
)
296 if (link_stat(fname
, &st
) != 0)
298 filesystem_dev
= st
.st_dev
;
302 static int to_wire_mode(mode_t mode
)
304 if (S_ISLNK(mode
) && (_S_IFLNK
!= 0120000)) {
305 return (mode
& ~(_S_IFMT
)) | 0120000;
310 static mode_t
from_wire_mode(int mode
)
312 if ((mode
& (_S_IFMT
)) == 0120000 && (_S_IFLNK
!= 0120000)) {
313 return (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
315 return (mode_t
) mode
;
319 static void send_directory(int f
, struct file_list
*flist
, char *dir
);
321 static char *flist_dir
;
325 * Make sure @p flist is big enough to hold at least @p flist->count
328 static void flist_expand(struct file_list
*flist
)
330 if (flist
->count
>= flist
->malloced
) {
333 if (flist
->malloced
< 1000)
334 flist
->malloced
+= 1000;
336 flist
->malloced
*= 2;
339 new_ptr
= realloc_array(flist
->files
,
340 struct file_struct
*,
343 new_ptr
= new_array(struct file_struct
*,
348 rprintf(FINFO
, "expand file_list to %.0f bytes, did%s move\n",
349 (double)sizeof(flist
->files
[0])
351 (new_ptr
== flist
->files
) ? " not" : "");
354 flist
->files
= (struct file_struct
**) new_ptr
;
357 out_of_memory("flist_expand");
362 static void send_file_entry(struct file_struct
*file
, int f
,
366 static time_t last_time
;
367 static mode_t last_mode
;
368 static DEV64_T last_rdev
;
369 static uid_t last_uid
;
370 static gid_t last_gid
;
371 static char lastname
[MAXPATHLEN
];
383 io_write_phase
= "send_file_entry";
385 fname
= f_name(file
);
389 if (file
->mode
== last_mode
)
391 if (file
->rdev
== last_rdev
)
393 if (file
->uid
== last_uid
)
395 if (file
->gid
== last_gid
)
397 if (file
->modtime
== last_time
)
401 lastname
[l1
] && (fname
[l1
] == lastname
[l1
]) && (l1
< 255);
403 l2
= strlen(fname
) - l1
;
410 /* we must make sure we don't send a zero flags byte or the other
411 end will terminate the flist transfer */
412 if (flags
== 0 && !S_ISDIR(file
->mode
))
413 flags
|= FLAG_DELETE
;
417 write_byte(f
, flags
);
418 if (flags
& SAME_NAME
)
420 if (flags
& LONG_NAME
)
424 write_buf(f
, fname
+ l1
, l2
);
426 write_longint(f
, file
->length
);
427 if (!(flags
& SAME_TIME
))
428 write_int(f
, (int) file
->modtime
);
429 if (!(flags
& SAME_MODE
))
430 write_int(f
, to_wire_mode(file
->mode
));
431 if (preserve_uid
&& !(flags
& SAME_UID
)) {
433 write_int(f
, (int) file
->uid
);
435 if (preserve_gid
&& !(flags
& SAME_GID
)) {
437 write_int(f
, (int) file
->gid
);
439 if (preserve_devices
&& IS_DEVICE(file
->mode
)
440 && !(flags
& SAME_RDEV
))
441 write_int(f
, (int) file
->rdev
);
444 if (preserve_links
&& S_ISLNK(file
->mode
)) {
445 write_int(f
, strlen(file
->link
));
446 write_buf(f
, file
->link
, strlen(file
->link
));
450 #if SUPPORT_HARD_LINKS
451 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
452 if (protocol_version
< 26) {
453 /* 32-bit dev_t and ino_t */
454 write_int(f
, (int) file
->dev
);
455 write_int(f
, (int) file
->inode
);
457 /* 64-bit dev_t and ino_t */
458 write_longint(f
, file
->dev
);
459 write_longint(f
, file
->inode
);
464 if (always_checksum
) {
465 if (protocol_version
< 21) {
466 write_buf(f
, file
->sum
, 2);
468 write_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
472 last_mode
= file
->mode
;
473 last_rdev
= file
->rdev
;
474 last_uid
= file
->uid
;
475 last_gid
= file
->gid
;
476 last_time
= file
->modtime
;
478 strlcpy(lastname
, fname
, MAXPATHLEN
);
479 lastname
[MAXPATHLEN
- 1] = 0;
481 io_write_phase
= "unknown";
486 static void receive_file_entry(struct file_struct
**fptr
,
487 unsigned flags
, int f
)
489 static time_t last_time
;
490 static mode_t last_mode
;
491 static DEV64_T last_rdev
;
492 static uid_t last_uid
;
493 static gid_t last_gid
;
494 static char lastname
[MAXPATHLEN
];
495 char thisname
[MAXPATHLEN
];
496 unsigned int l1
= 0, l2
= 0;
498 struct file_struct
*file
;
500 if (flags
& SAME_NAME
)
503 if (flags
& LONG_NAME
)
508 file
= new(struct file_struct
);
510 out_of_memory("receive_file_entry");
511 memset((char *) file
, 0, sizeof(*file
));
514 if (l2
>= MAXPATHLEN
- l1
) {
516 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
517 flags
, l1
, l2
, lastname
);
518 overflow("receive_file_entry");
521 strlcpy(thisname
, lastname
, l1
+ 1);
522 read_sbuf(f
, &thisname
[l1
], l2
);
523 thisname
[l1
+ l2
] = 0;
525 strlcpy(lastname
, thisname
, MAXPATHLEN
);
526 lastname
[MAXPATHLEN
- 1] = 0;
528 clean_fname(thisname
);
530 if (sanitize_paths
) {
531 sanitize_path(thisname
, NULL
);
534 if ((p
= strrchr(thisname
, '/'))) {
535 static char *lastdir
;
537 if (lastdir
&& strcmp(thisname
, lastdir
) == 0) {
538 file
->dirname
= lastdir
;
540 file
->dirname
= strdup(thisname
);
541 lastdir
= file
->dirname
;
543 file
->basename
= strdup(p
+ 1);
545 file
->dirname
= NULL
;
546 file
->basename
= strdup(thisname
);
550 out_of_memory("receive_file_entry 1");
554 file
->length
= read_longint(f
);
556 (flags
& SAME_TIME
) ? last_time
: (time_t) read_int(f
);
558 (flags
& SAME_MODE
) ? last_mode
: from_wire_mode(read_int(f
));
561 (flags
& SAME_UID
) ? last_uid
: (uid_t
) read_int(f
);
564 (flags
& SAME_GID
) ? last_gid
: (gid_t
) read_int(f
);
565 if (preserve_devices
&& IS_DEVICE(file
->mode
))
567 (flags
& SAME_RDEV
) ? last_rdev
: (DEV64_T
) read_int(f
);
569 if (preserve_links
&& S_ISLNK(file
->mode
)) {
572 rprintf(FERROR
, "overflow: l=%d\n", l
);
573 overflow("receive_file_entry");
575 file
->link
= new_array(char, l
+ 1);
577 out_of_memory("receive_file_entry 2");
578 read_sbuf(f
, file
->link
, l
);
579 if (sanitize_paths
) {
580 sanitize_path(file
->link
, file
->dirname
);
583 #if SUPPORT_HARD_LINKS
584 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
585 if (protocol_version
< 26) {
586 file
->dev
= read_int(f
);
587 file
->inode
= read_int(f
);
589 file
->dev
= read_longint(f
);
590 file
->inode
= read_longint(f
);
595 if (always_checksum
) {
596 file
->sum
= new_array(char, MD4_SUM_LENGTH
);
598 out_of_memory("md4 sum");
599 if (protocol_version
< 21) {
600 read_buf(f
, file
->sum
, 2);
602 read_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
606 last_mode
= file
->mode
;
607 last_rdev
= file
->rdev
;
608 last_uid
= file
->uid
;
609 last_gid
= file
->gid
;
610 last_time
= file
->modtime
;
612 if (!preserve_perms
) {
613 extern int orig_umask
;
614 /* set an appropriate set of permissions based on original
615 permissions and umask. This emulates what GNU cp does */
616 file
->mode
&= ~orig_umask
;
621 /* determine if a file in a different filesstem should be skipped
622 when one_file_system is set. We bascally only want to include
623 the mount points - but they can be hard to find! */
624 static int skip_filesystem(char *fname
, STRUCT_STAT
* st
)
627 char *p
= strrchr(fname
, '/');
629 /* skip all but directories */
630 if (!S_ISDIR(st
->st_mode
))
633 /* if its not a subdirectory then allow */
638 if (link_stat(fname
, &st2
)) {
644 return (st2
.st_dev
!= filesystem_dev
);
647 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
648 /* IRIX cc cares that the operands to the ternary have the same type. */
649 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
652 * Create a file_struct for a named file by reading its stat()
653 * information and performing extensive checks against global
656 * @return the new file, or NULL if there was an error or this file
657 * should be excluded.
659 * @todo There is a small optimization opportunity here to avoid
660 * stat()ing the file in some circumstances, which has a certain cost.
661 * We are called immediately after doing readdir(), and so we may
662 * already know the d_type of the file. We could for example avoid
663 * statting directories if we're not recursing, but this is not a very
664 * important case. Some systems may not have d_type.
666 struct file_struct
*make_file(char *fname
, struct string_area
**ap
,
669 struct file_struct
*file
;
671 char sum
[SUM_LENGTH
];
673 char cleaned_name
[MAXPATHLEN
];
674 char linkbuf
[MAXPATHLEN
];
675 extern int module_id
;
677 strlcpy(cleaned_name
, fname
, MAXPATHLEN
);
678 cleaned_name
[MAXPATHLEN
- 1] = 0;
679 clean_fname(cleaned_name
);
680 if (sanitize_paths
) {
681 sanitize_path(cleaned_name
, NULL
);
683 fname
= cleaned_name
;
685 memset(sum
, 0, SUM_LENGTH
);
687 if (readlink_stat(fname
, &st
, linkbuf
) != 0) {
688 int save_errno
= errno
;
689 if (errno
== ENOENT
&& exclude_level
!= NO_EXCLUDES
) {
690 /* either symlink pointing nowhere or file that
691 * was removed during rsync run; see if excluded
692 * before reporting an error */
693 if (check_exclude_file(fname
, 0, exclude_level
)) {
694 /* file is excluded anyway, ignore silently */
698 io_error
|= IOERR_GENERAL
;
699 rprintf(FERROR
, "readlink %s failed: %s\n",
700 full_fname(fname
), strerror(save_errno
));
704 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
705 if (exclude_level
== NO_EXCLUDES
)
708 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
709 rprintf(FINFO
, "skipping directory %s\n", fname
);
713 if (one_file_system
&& st
.st_dev
!= filesystem_dev
) {
714 if (skip_filesystem(fname
, &st
))
718 if (check_exclude_file(fname
, S_ISDIR(st
.st_mode
) != 0, exclude_level
))
721 if (lp_ignore_nonreadable(module_id
) && access(fname
, R_OK
) != 0)
727 rprintf(FINFO
, "make_file(%s,*,%d)\n", fname
, exclude_level
);
729 file
= new(struct file_struct
);
731 out_of_memory("make_file");
732 memset((char *) file
, 0, sizeof(*file
));
734 if ((p
= strrchr(fname
, '/'))) {
735 static char *lastdir
;
737 if (lastdir
&& strcmp(fname
, lastdir
) == 0) {
738 file
->dirname
= lastdir
;
740 file
->dirname
= strdup(fname
);
741 lastdir
= file
->dirname
;
743 file
->basename
= STRDUP(ap
, p
+ 1);
746 file
->dirname
= NULL
;
747 file
->basename
= STRDUP(ap
, fname
);
750 file
->modtime
= st
.st_mtime
;
751 file
->length
= st
.st_size
;
752 file
->mode
= st
.st_mode
;
753 file
->uid
= st
.st_uid
;
754 file
->gid
= st
.st_gid
;
755 file
->dev
= st
.st_dev
;
756 file
->inode
= st
.st_ino
;
757 #ifdef HAVE_STRUCT_STAT_ST_RDEV
758 file
->rdev
= st
.st_rdev
;
762 if (S_ISLNK(st
.st_mode
)) {
763 file
->link
= STRDUP(ap
, linkbuf
);
767 if (always_checksum
) {
768 file
->sum
= (char *) MALLOC(ap
, MD4_SUM_LENGTH
);
770 out_of_memory("md4 sum");
771 /* drat. we have to provide a null checksum for non-regular
772 files in order to be compatible with earlier versions
774 if (S_ISREG(st
.st_mode
)) {
775 file_checksum(fname
, file
->sum
, st
.st_size
);
777 memset(file
->sum
, 0, MD4_SUM_LENGTH
);
782 static char *lastdir
;
783 if (lastdir
&& strcmp(lastdir
, flist_dir
) == 0) {
784 file
->basedir
= lastdir
;
786 file
->basedir
= strdup(flist_dir
);
787 lastdir
= file
->basedir
;
790 file
->basedir
= NULL
;
793 if (!S_ISDIR(st
.st_mode
))
794 stats
.total_size
+= st
.st_size
;
801 void send_file_name(int f
, struct file_list
*flist
, char *fname
,
802 int recursive
, unsigned base_flags
)
804 struct file_struct
*file
;
805 extern int delete_excluded
;
807 /* f is set to -1 when calculating deletion file list */
808 file
= make_file(fname
, &flist
->string_area
,
809 f
== -1 && delete_excluded
? SERVER_EXCLUDES
815 maybe_emit_filelist_progress(flist
);
819 if (write_batch
) /* dw */
820 file
->flags
= FLAG_DELETE
;
822 if (file
->basename
[0]) {
823 flist
->files
[flist
->count
++] = file
;
824 send_file_entry(file
, f
, base_flags
);
827 if (S_ISDIR(file
->mode
) && recursive
) {
828 struct exclude_struct
**last_exclude_list
=
830 send_directory(f
, flist
, f_name(file
));
831 local_exclude_list
= last_exclude_list
;
838 static void send_directory(int f
, struct file_list
*flist
, char *dir
)
842 char fname
[MAXPATHLEN
];
848 io_error
|= IOERR_GENERAL
;
849 rprintf(FERROR
, "opendir %s failed: %s\n",
850 full_fname(dir
), strerror(errno
));
854 strlcpy(fname
, dir
, MAXPATHLEN
);
856 if (fname
[l
- 1] != '/') {
857 if (l
== MAXPATHLEN
- 1) {
858 io_error
|= IOERR_GENERAL
;
859 rprintf(FERROR
, "skipping long-named directory: %s\n",
864 strlcat(fname
, "/", MAXPATHLEN
);
867 p
= fname
+ strlen(fname
);
869 local_exclude_list
= NULL
;
872 if (strlen(fname
) + strlen(".cvsignore") <= MAXPATHLEN
- 1) {
873 strcpy(p
, ".cvsignore");
874 add_exclude_file(&exclude_list
,fname
,MISSING_OK
,ADD_EXCLUDE
);
876 io_error
|= IOERR_GENERAL
;
878 "cannot cvs-exclude in long-named directory %s\n",
883 for (errno
= 0, di
= readdir(d
); di
; errno
= 0, di
= readdir(d
)) {
884 char *dname
= d_name(di
);
885 if (dname
[0] == '.' && (dname
[1] == '\0'
886 || (dname
[1] == '.' && dname
[2] == '\0')))
888 strlcpy(p
, dname
, MAXPATHLEN
- l
);
889 send_file_name(f
, flist
, fname
, recurse
, 0);
892 io_error
|= IOERR_GENERAL
;
893 rprintf(FERROR
, "readdir(%s): (%d) %s\n",
894 dir
, errno
, strerror(errno
));
897 if (local_exclude_list
)
898 free_exclude_list(&local_exclude_list
); /* Zeros pointer too */
905 * The delete_files() function in receiver.c sets f to -1 so that we just
906 * construct the file list in memory without sending it over the wire. It
907 * also has the side-effect of ignoring user-excludes if delete_excluded
908 * is set (so that the delete list includes user-excluded files).
910 struct file_list
*send_file_list(int f
, int argc
, char *argv
[])
914 char *p
, *dir
, *olddir
;
915 char lastpath
[MAXPATHLEN
] = "";
916 struct file_list
*flist
;
920 if (show_filelist_p() && f
!= -1)
921 start_filelist_progress("building file list");
923 start_write
= stats
.total_written
;
928 io_start_buffering(f
);
929 if (filesfrom_fd
>= 0) {
930 if (argv
[0] && !push_dir(argv
[0], 0)) {
931 rprintf(FERROR
, "push_dir %s failed: %s\n",
932 full_fname(argv
[0]), strerror(errno
));
933 exit_cleanup(RERR_FILESELECT
);
940 char fname2
[MAXPATHLEN
];
941 char *fname
= fname2
;
944 if (read_filesfrom_line(filesfrom_fd
, fname
) == 0)
946 sanitize_path(fname
, NULL
);
950 strlcpy(fname
, *argv
++, MAXPATHLEN
);
952 sanitize_path(fname
, NULL
);
956 if (fname
[l
- 1] == '/') {
957 if (l
== 2 && fname
[0] == '.') {
958 /* Turn "./" into just "." rather than "./." */
961 strlcat(fname
, ".", MAXPATHLEN
);
965 if (link_stat(fname
, &st
) != 0) {
967 io_error
|= IOERR_GENERAL
;
968 rprintf(FERROR
, "link_stat %s failed: %s\n",
969 full_fname(fname
), strerror(errno
));
974 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
975 rprintf(FINFO
, "skipping directory %s\n", fname
);
982 if (!relative_paths
) {
983 p
= strrchr(fname
, '/');
992 } else if (f
!= -1 && implied_dirs
&& (p
=strrchr(fname
,'/')) && p
!= fname
) {
993 /* this ensures we send the intermediate directories,
994 thus getting their permissions right */
995 char *lp
= lastpath
, *fn
= fname
, *slash
= fname
;
997 /* Skip any initial directories in our path that we
998 * have in common with lastpath. */
999 while (*fn
&& *lp
== *fn
) {
1005 if (fn
!= p
|| (*lp
&& *lp
!= '/')) {
1006 int copy_links_saved
= copy_links
;
1007 int recurse_saved
= recurse
;
1008 copy_links
= copy_unsafe_links
;
1009 /* set recurse to 1 to prevent make_file
1010 * from ignoring directory, but still
1011 * turn off the recursive parameter to
1014 while ((slash
= strchr(slash
+1, '/')) != 0) {
1016 send_file_name(f
, flist
, fname
, 0, 0);
1019 copy_links
= copy_links_saved
;
1020 recurse
= recurse_saved
;
1022 strlcpy(lastpath
, fname
, sizeof lastpath
);
1031 olddir
= push_dir(dir
, 1);
1034 io_error
|= IOERR_GENERAL
;
1035 rprintf(FERROR
, "push_dir %s failed: %s\n",
1036 full_fname(dir
), strerror(errno
));
1043 if (one_file_system
)
1044 set_filesystem(fname
);
1046 send_file_name(f
, flist
, fname
, recurse
, FLAG_DELETE
);
1048 if (olddir
!= NULL
) {
1050 if (pop_dir(olddir
) != 0) {
1051 rprintf(FERROR
, "pop_dir %s failed: %s\n",
1052 full_fname(dir
), strerror(errno
));
1053 exit_cleanup(RERR_FILESELECT
);
1059 send_file_entry(NULL
, f
, 0);
1062 if (show_filelist_p() && f
!= -1) {
1063 finish_filelist_progress(flist
);
1066 clean_flist(flist
, 0, 0);
1068 /* now send the uid/gid list. This was introduced in protocol
1074 /* send the io_error flag */
1076 extern int module_id
;
1077 write_int(f
, lp_ignore_errors(module_id
) ? 0 : io_error
);
1082 stats
.flist_size
= stats
.total_written
- start_write
;
1083 stats
.num_files
= flist
->count
;
1084 if (write_batch
) /* dw */
1085 write_batch_flist_info(flist
->count
, flist
->files
);
1089 rprintf(FINFO
, "send_file_list done\n");
1095 struct file_list
*recv_file_list(int f
)
1097 struct file_list
*flist
;
1098 unsigned char flags
;
1100 extern int list_only
;
1102 if (show_filelist_p())
1103 start_filelist_progress("receiving file list");
1105 start_read
= stats
.total_read
;
1107 flist
= new(struct file_list
);
1112 flist
->malloced
= 1000;
1113 flist
->files
= new_array(struct file_struct
*, flist
->malloced
);
1118 for (flags
= read_byte(f
); flags
; flags
= read_byte(f
)) {
1119 int i
= flist
->count
;
1121 flist_expand(flist
);
1123 receive_file_entry(&flist
->files
[i
], flags
, f
);
1125 if (S_ISREG(flist
->files
[i
]->mode
))
1126 stats
.total_size
+= flist
->files
[i
]->length
;
1130 maybe_emit_filelist_progress(flist
);
1133 rprintf(FINFO
, "recv_file_name(%s)\n",
1134 f_name(flist
->files
[i
]));
1139 rprintf(FINFO
, "received %d names\n", flist
->count
);
1141 clean_flist(flist
, relative_paths
, 1);
1143 if (show_filelist_p()) {
1144 finish_filelist_progress(flist
);
1147 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1149 recv_uid_list(f
, flist
);
1152 /* recv the io_error flag */
1153 if (f
!= -1 && !read_batch
) { /* dw-added readbatch */
1154 extern int module_id
;
1155 extern int ignore_errors
;
1156 if (lp_ignore_errors(module_id
) || ignore_errors
) {
1159 io_error
|= read_int(f
);
1165 for (i
= 0; i
< flist
->count
; i
++) {
1166 list_file_entry(flist
->files
[i
]);
1172 rprintf(FINFO
, "recv_file_list done\n");
1174 stats
.flist_size
= stats
.total_read
- start_read
;
1175 stats
.num_files
= flist
->count
;
1180 out_of_memory("recv_file_list");
1181 return NULL
; /* not reached */
1186 * XXX: This is currently the hottest function while building the file
1187 * list, because building f_name()s every time is expensive.
1189 int file_compare(struct file_struct
**f1
, struct file_struct
**f2
)
1191 if (!(*f1
)->basename
&& !(*f2
)->basename
)
1193 if (!(*f1
)->basename
)
1195 if (!(*f2
)->basename
)
1197 if ((*f1
)->dirname
== (*f2
)->dirname
)
1198 return u_strcmp((*f1
)->basename
, (*f2
)->basename
);
1199 return u_strcmp(f_name(*f1
), f_name(*f2
));
1203 int flist_find(struct file_list
*flist
, struct file_struct
*f
)
1205 int low
= 0, high
= flist
->count
- 1;
1207 while (high
>= 0 && !flist
->files
[high
]->basename
) high
--;
1212 while (low
!= high
) {
1213 int mid
= (low
+ high
) / 2;
1215 file_compare(&flist
->files
[flist_up(flist
, mid
)], &f
);
1217 return flist_up(flist
, mid
);
1225 if (file_compare(&flist
->files
[flist_up(flist
, low
)], &f
) == 0)
1226 return flist_up(flist
, low
);
1234 void free_file(struct file_struct
*file
)
1239 free(file
->basename
);
1249 * allocate a new file list
1251 struct file_list
*flist_new(void)
1253 struct file_list
*flist
;
1255 flist
= new(struct file_list
);
1257 out_of_memory("send_file_list");
1260 flist
->malloced
= 0;
1261 flist
->files
= NULL
;
1264 flist
->string_area
= string_area_new(0);
1266 flist
->string_area
= NULL
;
1272 * free up all elements in a flist
1274 void flist_free(struct file_list
*flist
)
1277 for (i
= 1; i
< flist
->count
; i
++) {
1278 if (!flist
->string_area
)
1279 free_file(flist
->files
[i
]);
1280 free(flist
->files
[i
]);
1282 /* FIXME: I don't think we generally need to blank the flist
1283 * since it's about to be freed. This will just cause more
1284 * memory traffic. If you want a freed-memory debugger, you
1285 * know where to get it. */
1286 memset((char *) flist
->files
, 0,
1287 sizeof(flist
->files
[0]) * flist
->count
);
1289 if (flist
->string_area
)
1290 string_area_free(flist
->string_area
);
1291 memset((char *) flist
, 0, sizeof(*flist
));
1297 * This routine ensures we don't have any duplicate names in our file list.
1298 * duplicate names can cause corruption because of the pipelining
1300 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
)
1303 char *name
, *prev_name
= NULL
;
1305 if (!flist
|| flist
->count
== 0)
1308 qsort(flist
->files
, flist
->count
,
1309 sizeof(flist
->files
[0]), (int (*)()) file_compare
);
1311 for (i
= no_dups
? 0 : flist
->count
; i
< flist
->count
; i
++) {
1312 if (flist
->files
[i
]->basename
) {
1314 prev_name
= f_name(flist
->files
[i
]);
1318 while (++i
< flist
->count
) {
1319 if (!flist
->files
[i
]->basename
)
1321 name
= f_name(flist
->files
[i
]);
1322 if (strcmp(name
, prev_name
) == 0) {
1323 if (verbose
> 1 && !am_server
) {
1325 "removing duplicate name %s from file list %d\n",
1328 /* Make sure that if we unduplicate '.', that we don't
1329 * lose track of a user-specified starting point (or
1330 * else deletions will mysteriously fail with -R). */
1331 if (flist
->files
[i
]->flags
& FLAG_DELETE
)
1332 flist
->files
[prev_i
]->flags
|= FLAG_DELETE
;
1333 /* it's not great that the flist knows the semantics of
1334 * the file memory usage, but i'd rather not add a flag
1335 * byte to that struct.
1336 * XXX can i use a bit in the flags field? */
1337 if (flist
->string_area
)
1338 flist
->files
[i
][0] = null_file
;
1340 free_file(flist
->files
[i
]);
1344 /* We set prev_name every iteration to avoid it becoming
1345 * invalid when names[][] in f_name() wraps around. */
1350 /* we need to strip off the root directory in the case
1351 of relative paths, but this must be done _after_
1352 the sorting phase */
1353 for (i
= 0; i
< flist
->count
; i
++) {
1354 if (flist
->files
[i
]->dirname
&&
1355 flist
->files
[i
]->dirname
[0] == '/') {
1356 memmove(&flist
->files
[i
]->dirname
[0],
1357 &flist
->files
[i
]->dirname
[1],
1358 strlen(flist
->files
[i
]->dirname
));
1361 if (flist
->files
[i
]->dirname
&&
1362 !flist
->files
[i
]->dirname
[0]) {
1363 flist
->files
[i
]->dirname
= NULL
;
1371 for (i
= 0; i
< flist
->count
; i
++) {
1372 rprintf(FINFO
, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1374 NS(flist
->files
[i
]->dirname
),
1375 NS(flist
->files
[i
]->basename
),
1376 (int) flist
->files
[i
]->mode
,
1377 (double) flist
->files
[i
]->length
);
1383 * return the full filename of a flist entry
1385 * This function is too expensive at the moment, because it copies
1386 * strings when often we only want to compare them. In any case,
1387 * using strlcat is silly because it will walk the string repeatedly.
1389 char *f_name(struct file_struct
*f
)
1391 static char names
[10][MAXPATHLEN
];
1395 if (!f
|| !f
->basename
)
1403 off
= strlcpy(p
, f
->dirname
, MAXPATHLEN
);
1404 off
+= strlcpy(p
+ off
, "/", MAXPATHLEN
- off
);
1405 off
+= strlcpy(p
+ off
, f
->basename
, MAXPATHLEN
- off
);
1407 strlcpy(p
, f
->basename
, MAXPATHLEN
);