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 remote_version
;
62 extern int sanitize_paths
;
64 extern int read_batch
;
65 extern int write_batch
;
67 extern struct exclude_struct
**exclude_list
;
68 extern struct exclude_struct
**server_exclude_list
;
69 extern struct exclude_struct
**local_exclude_list
;
71 static struct file_struct null_file
;
73 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
);
76 static int show_filelist_p(void)
78 return verbose
&& (recurse
|| files_from
) && !am_server
;
81 static void start_filelist_progress(char *kind
)
83 rprintf(FINFO
, "%s ... ", kind
);
84 if ((verbose
> 1) || do_progress
)
90 static void emit_filelist_progress(const struct file_list
*flist
)
92 rprintf(FINFO
, " %d files...\r", flist
->count
);
96 static void maybe_emit_filelist_progress(const struct file_list
*flist
)
98 if (do_progress
&& show_filelist_p() && ((flist
->count
% 100) == 0))
99 emit_filelist_progress(flist
);
103 static void finish_filelist_progress(const struct file_list
*flist
)
106 /* This overwrites the progress line */
107 rprintf(FINFO
, "%d file%sto consider\n",
108 flist
->count
, flist
->count
== 1 ? " " : "s ");
110 rprintf(FINFO
, "done\n");
114 void show_flist_stats(void)
120 static struct string_area
*string_area_new(int size
)
122 struct string_area
*a
;
126 a
= malloc(sizeof(*a
));
128 out_of_memory("string_area_new");
129 a
->current
= a
->base
= malloc(size
);
131 out_of_memory("string_area_new buffer");
132 a
->end
= a
->base
+ size
;
138 static void string_area_free(struct string_area
*a
)
140 struct string_area
*next
;
142 for (; a
; a
= next
) {
148 static char *string_area_malloc(struct string_area
**ap
, int size
)
151 struct string_area
*a
;
153 /* does the request fit into the current space? */
155 if (a
->current
+ size
>= a
->end
) {
156 /* no; get space, move new string_area to front of the list */
157 a
= string_area_new(size
> ARENA_SIZE
? size
: ARENA_SIZE
);
162 /* have space; do the "allocation." */
168 static char *string_area_strdup(struct string_area
**ap
, const char *src
)
170 char *dest
= string_area_malloc(ap
, strlen(src
) + 1);
171 return strcpy(dest
, src
);
174 static void list_file_entry(struct file_struct
*f
)
179 /* this can happen if duplicate names were removed */
182 permstring(perms
, f
->mode
);
184 if (preserve_links
&& S_ISLNK(f
->mode
)) {
185 rprintf(FINFO
, "%s %11.0f %s %s -> %s\n",
187 (double) f
->length
, timestring(f
->modtime
),
190 rprintf(FINFO
, "%s %11.0f %s %s\n",
192 (double) f
->length
, timestring(f
->modtime
),
199 * Stat either a symlink or its referent, depending on the settings of
200 * copy_links, copy_unsafe_links, etc.
202 * @retval -1 on error
204 * @retval 0 for success
206 * @post If @p path is a symlink, then @p linkbuf (of size @c
207 * MAXPATHLEN) contains the symlink target.
209 * @post @p buffer contains information about the link or the
210 * referrent as appropriate, if they exist.
212 int readlink_stat(const char *path
, STRUCT_STAT
* buffer
, char *linkbuf
)
216 return do_stat(path
, buffer
);
218 if (do_lstat(path
, buffer
) == -1) {
221 if (S_ISLNK(buffer
->st_mode
)) {
223 l
= readlink((char *) path
, linkbuf
, MAXPATHLEN
- 1);
227 if (copy_unsafe_links
&& unsafe_symlink(linkbuf
, path
)) {
229 rprintf(FINFO
,"copying unsafe symlink \"%s\" -> \"%s\"\n",
232 return do_stat(path
, buffer
);
237 return do_stat(path
, buffer
);
241 int link_stat(const char *path
, STRUCT_STAT
* buffer
)
245 return do_stat(path
, buffer
);
247 return do_lstat(path
, buffer
);
250 return do_stat(path
, buffer
);
255 * This function is used to check if a file should be included/excluded
256 * from the list of files based on its name and type etc. The value of
257 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
259 static int check_exclude_file(char *fname
, int is_dir
, int exclude_level
)
261 #if 0 /* This currently never happens, so avoid a useless compare. */
262 if (exclude_level
== NO_EXCLUDES
)
266 /* never exclude '.', even if somebody does --exclude '*' */
267 if (fname
[0] == '.' && !fname
[1])
269 /* Handle the -R version of the '.' dir. */
270 if (fname
[0] == '/') {
271 int len
= strlen(fname
);
272 if (fname
[len
-1] == '.' && fname
[len
-2] == '/')
276 if (server_exclude_list
277 && check_exclude(server_exclude_list
, fname
, is_dir
))
279 if (exclude_level
!= ALL_EXCLUDES
)
281 if (exclude_list
&& check_exclude(exclude_list
, fname
, is_dir
))
283 if (local_exclude_list
284 && check_exclude(local_exclude_list
, fname
, is_dir
))
289 /* used by the one_file_system code */
290 static dev_t filesystem_dev
;
292 static void set_filesystem(char *fname
)
295 if (link_stat(fname
, &st
) != 0)
297 filesystem_dev
= st
.st_dev
;
301 static int to_wire_mode(mode_t mode
)
303 if (S_ISLNK(mode
) && (_S_IFLNK
!= 0120000)) {
304 return (mode
& ~(_S_IFMT
)) | 0120000;
309 static mode_t
from_wire_mode(int mode
)
311 if ((mode
& (_S_IFMT
)) == 0120000 && (_S_IFLNK
!= 0120000)) {
312 return (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
314 return (mode_t
) mode
;
318 static void send_directory(int f
, struct file_list
*flist
, char *dir
);
320 static char *flist_dir
;
324 * Make sure @p flist is big enough to hold at least @p flist->count
327 static void flist_expand(struct file_list
*flist
)
329 if (flist
->count
>= flist
->malloced
) {
333 if (flist
->malloced
< 1000)
334 flist
->malloced
+= 1000;
336 flist
->malloced
*= 2;
338 new_bytes
= sizeof(flist
->files
[0]) * flist
->malloced
;
341 new_ptr
= realloc(flist
->files
, new_bytes
);
343 new_ptr
= malloc(new_bytes
);
346 rprintf(FINFO
, "expand file_list to %.0f bytes, did%s move\n",
348 (new_ptr
== flist
->files
) ? " not" : "");
351 flist
->files
= (struct file_struct
**) new_ptr
;
354 out_of_memory("flist_expand");
359 static void send_file_entry(struct file_struct
*file
, int f
,
363 static time_t last_time
;
364 static mode_t last_mode
;
365 static DEV64_T last_rdev
;
366 static uid_t last_uid
;
367 static gid_t last_gid
;
368 static char lastname
[MAXPATHLEN
];
380 io_write_phase
= "send_file_entry";
382 fname
= f_name(file
);
386 if (file
->mode
== last_mode
)
388 if (file
->rdev
== last_rdev
)
390 if (file
->uid
== last_uid
)
392 if (file
->gid
== last_gid
)
394 if (file
->modtime
== last_time
)
398 lastname
[l1
] && (fname
[l1
] == lastname
[l1
]) && (l1
< 255);
400 l2
= strlen(fname
) - l1
;
407 /* we must make sure we don't send a zero flags byte or the other
408 end will terminate the flist transfer */
409 if (flags
== 0 && !S_ISDIR(file
->mode
))
410 flags
|= FLAG_DELETE
;
414 write_byte(f
, flags
);
415 if (flags
& SAME_NAME
)
417 if (flags
& LONG_NAME
)
421 write_buf(f
, fname
+ l1
, l2
);
423 write_longint(f
, file
->length
);
424 if (!(flags
& SAME_TIME
))
425 write_int(f
, (int) file
->modtime
);
426 if (!(flags
& SAME_MODE
))
427 write_int(f
, to_wire_mode(file
->mode
));
428 if (preserve_uid
&& !(flags
& SAME_UID
)) {
430 write_int(f
, (int) file
->uid
);
432 if (preserve_gid
&& !(flags
& SAME_GID
)) {
434 write_int(f
, (int) file
->gid
);
436 if (preserve_devices
&& IS_DEVICE(file
->mode
)
437 && !(flags
& SAME_RDEV
))
438 write_int(f
, (int) file
->rdev
);
441 if (preserve_links
&& S_ISLNK(file
->mode
)) {
442 write_int(f
, strlen(file
->link
));
443 write_buf(f
, file
->link
, strlen(file
->link
));
447 #if SUPPORT_HARD_LINKS
448 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
449 if (remote_version
< 26) {
450 /* 32-bit dev_t and ino_t */
451 write_int(f
, (int) file
->dev
);
452 write_int(f
, (int) file
->inode
);
454 /* 64-bit dev_t and ino_t */
455 write_longint(f
, file
->dev
);
456 write_longint(f
, file
->inode
);
461 if (always_checksum
) {
462 if (remote_version
< 21) {
463 write_buf(f
, file
->sum
, 2);
465 write_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
469 last_mode
= file
->mode
;
470 last_rdev
= file
->rdev
;
471 last_uid
= file
->uid
;
472 last_gid
= file
->gid
;
473 last_time
= file
->modtime
;
475 strlcpy(lastname
, fname
, MAXPATHLEN
);
476 lastname
[MAXPATHLEN
- 1] = 0;
478 io_write_phase
= "unknown";
483 static void receive_file_entry(struct file_struct
**fptr
,
484 unsigned flags
, int f
)
486 static time_t last_time
;
487 static mode_t last_mode
;
488 static DEV64_T last_rdev
;
489 static uid_t last_uid
;
490 static gid_t last_gid
;
491 static char lastname
[MAXPATHLEN
];
492 char thisname
[MAXPATHLEN
];
493 unsigned int l1
= 0, l2
= 0;
495 struct file_struct
*file
;
497 if (flags
& SAME_NAME
)
500 if (flags
& LONG_NAME
)
505 file
= (struct file_struct
*) malloc(sizeof(*file
));
507 out_of_memory("receive_file_entry");
508 memset((char *) file
, 0, sizeof(*file
));
511 if (l2
>= MAXPATHLEN
- l1
) {
513 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
514 flags
, l1
, l2
, lastname
);
515 overflow("receive_file_entry");
518 strlcpy(thisname
, lastname
, l1
+ 1);
519 read_sbuf(f
, &thisname
[l1
], l2
);
520 thisname
[l1
+ l2
] = 0;
522 strlcpy(lastname
, thisname
, MAXPATHLEN
);
523 lastname
[MAXPATHLEN
- 1] = 0;
525 clean_fname(thisname
);
527 if (sanitize_paths
) {
528 sanitize_path(thisname
, NULL
);
531 if ((p
= strrchr(thisname
, '/'))) {
532 static char *lastdir
;
534 if (lastdir
&& strcmp(thisname
, lastdir
) == 0) {
535 file
->dirname
= lastdir
;
537 file
->dirname
= strdup(thisname
);
538 lastdir
= file
->dirname
;
540 file
->basename
= strdup(p
+ 1);
542 file
->dirname
= NULL
;
543 file
->basename
= strdup(thisname
);
547 out_of_memory("receive_file_entry 1");
551 file
->length
= read_longint(f
);
553 (flags
& SAME_TIME
) ? last_time
: (time_t) read_int(f
);
555 (flags
& SAME_MODE
) ? last_mode
: from_wire_mode(read_int(f
));
558 (flags
& SAME_UID
) ? last_uid
: (uid_t
) read_int(f
);
561 (flags
& SAME_GID
) ? last_gid
: (gid_t
) read_int(f
);
562 if (preserve_devices
&& IS_DEVICE(file
->mode
))
564 (flags
& SAME_RDEV
) ? last_rdev
: (DEV64_T
) read_int(f
);
566 if (preserve_links
&& S_ISLNK(file
->mode
)) {
569 rprintf(FERROR
, "overflow: l=%d\n", l
);
570 overflow("receive_file_entry");
572 file
->link
= (char *) malloc(l
+ 1);
574 out_of_memory("receive_file_entry 2");
575 read_sbuf(f
, file
->link
, l
);
576 if (sanitize_paths
) {
577 sanitize_path(file
->link
, file
->dirname
);
580 #if SUPPORT_HARD_LINKS
581 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
582 if (remote_version
< 26) {
583 file
->dev
= read_int(f
);
584 file
->inode
= read_int(f
);
586 file
->dev
= read_longint(f
);
587 file
->inode
= read_longint(f
);
592 if (always_checksum
) {
593 file
->sum
= (char *) malloc(MD4_SUM_LENGTH
);
595 out_of_memory("md4 sum");
596 if (remote_version
< 21) {
597 read_buf(f
, file
->sum
, 2);
599 read_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
603 last_mode
= file
->mode
;
604 last_rdev
= file
->rdev
;
605 last_uid
= file
->uid
;
606 last_gid
= file
->gid
;
607 last_time
= file
->modtime
;
609 if (!preserve_perms
) {
610 extern int orig_umask
;
611 /* set an appropriate set of permissions based on original
612 permissions and umask. This emulates what GNU cp does */
613 file
->mode
&= ~orig_umask
;
618 /* determine if a file in a different filesstem should be skipped
619 when one_file_system is set. We bascally only want to include
620 the mount points - but they can be hard to find! */
621 static int skip_filesystem(char *fname
, STRUCT_STAT
* st
)
624 char *p
= strrchr(fname
, '/');
626 /* skip all but directories */
627 if (!S_ISDIR(st
->st_mode
))
630 /* if its not a subdirectory then allow */
635 if (link_stat(fname
, &st2
)) {
641 return (st2
.st_dev
!= filesystem_dev
);
644 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
645 /* IRIX cc cares that the operands to the ternary have the same type. */
646 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
649 * Create a file_struct for a named file by reading its stat()
650 * information and performing extensive checks against global
653 * @return the new file, or NULL if there was an error or this file
654 * should be excluded.
656 * @todo There is a small optimization opportunity here to avoid
657 * stat()ing the file in some circumstances, which has a certain cost.
658 * We are called immediately after doing readdir(), and so we may
659 * already know the d_type of the file. We could for example avoid
660 * statting directories if we're not recursing, but this is not a very
661 * important case. Some systems may not have d_type.
663 struct file_struct
*make_file(char *fname
, struct string_area
**ap
,
666 struct file_struct
*file
;
668 char sum
[SUM_LENGTH
];
670 char cleaned_name
[MAXPATHLEN
];
671 char linkbuf
[MAXPATHLEN
];
672 extern int module_id
;
674 strlcpy(cleaned_name
, fname
, MAXPATHLEN
);
675 cleaned_name
[MAXPATHLEN
- 1] = 0;
676 clean_fname(cleaned_name
);
677 if (sanitize_paths
) {
678 sanitize_path(cleaned_name
, NULL
);
680 fname
= cleaned_name
;
682 memset(sum
, 0, SUM_LENGTH
);
684 if (readlink_stat(fname
, &st
, linkbuf
) != 0) {
685 int save_errno
= errno
;
686 if (errno
== ENOENT
&& exclude_level
!= NO_EXCLUDES
) {
687 /* either symlink pointing nowhere or file that
688 * was removed during rsync run; see if excluded
689 * before reporting an error */
690 if (check_exclude_file(fname
, 0, exclude_level
)) {
691 /* file is excluded anyway, ignore silently */
696 rprintf(FERROR
, "readlink %s: %s\n",
697 fname
, strerror(save_errno
));
701 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
702 if (exclude_level
== NO_EXCLUDES
)
705 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
706 rprintf(FINFO
, "skipping directory %s\n", fname
);
710 if (one_file_system
&& st
.st_dev
!= filesystem_dev
) {
711 if (skip_filesystem(fname
, &st
))
715 if (check_exclude_file(fname
, S_ISDIR(st
.st_mode
) != 0, exclude_level
))
718 if (lp_ignore_nonreadable(module_id
) && access(fname
, R_OK
) != 0)
724 rprintf(FINFO
, "make_file(%s,*,%d)\n", fname
, exclude_level
);
726 file
= (struct file_struct
*) malloc(sizeof(*file
));
728 out_of_memory("make_file");
729 memset((char *) file
, 0, sizeof(*file
));
731 if ((p
= strrchr(fname
, '/'))) {
732 static char *lastdir
;
734 if (lastdir
&& strcmp(fname
, lastdir
) == 0) {
735 file
->dirname
= lastdir
;
737 file
->dirname
= strdup(fname
);
738 lastdir
= file
->dirname
;
740 file
->basename
= STRDUP(ap
, p
+ 1);
743 file
->dirname
= NULL
;
744 file
->basename
= STRDUP(ap
, fname
);
747 file
->modtime
= st
.st_mtime
;
748 file
->length
= st
.st_size
;
749 file
->mode
= st
.st_mode
;
750 file
->uid
= st
.st_uid
;
751 file
->gid
= st
.st_gid
;
752 file
->dev
= st
.st_dev
;
753 file
->inode
= st
.st_ino
;
754 #ifdef HAVE_STRUCT_STAT_ST_RDEV
755 file
->rdev
= st
.st_rdev
;
759 if (S_ISLNK(st
.st_mode
)) {
760 file
->link
= STRDUP(ap
, linkbuf
);
764 if (always_checksum
) {
765 file
->sum
= (char *) MALLOC(ap
, MD4_SUM_LENGTH
);
767 out_of_memory("md4 sum");
768 /* drat. we have to provide a null checksum for non-regular
769 files in order to be compatible with earlier versions
771 if (S_ISREG(st
.st_mode
)) {
772 file_checksum(fname
, file
->sum
, st
.st_size
);
774 memset(file
->sum
, 0, MD4_SUM_LENGTH
);
779 static char *lastdir
;
780 if (lastdir
&& strcmp(lastdir
, flist_dir
) == 0) {
781 file
->basedir
= lastdir
;
783 file
->basedir
= strdup(flist_dir
);
784 lastdir
= file
->basedir
;
787 file
->basedir
= NULL
;
790 if (!S_ISDIR(st
.st_mode
))
791 stats
.total_size
+= st
.st_size
;
798 void send_file_name(int f
, struct file_list
*flist
, char *fname
,
799 int recursive
, unsigned base_flags
)
801 struct file_struct
*file
;
802 extern int delete_excluded
;
804 /* f is set to -1 when calculating deletion file list */
805 file
= make_file(fname
, &flist
->string_area
,
806 f
== -1 && delete_excluded
? SERVER_EXCLUDES
812 maybe_emit_filelist_progress(flist
);
816 if (write_batch
) /* dw */
817 file
->flags
= FLAG_DELETE
;
819 if (file
->basename
[0]) {
820 flist
->files
[flist
->count
++] = file
;
821 send_file_entry(file
, f
, base_flags
);
824 if (S_ISDIR(file
->mode
) && recursive
) {
825 struct exclude_struct
**last_exclude_list
=
827 send_directory(f
, flist
, f_name(file
));
828 local_exclude_list
= last_exclude_list
;
835 static void send_directory(int f
, struct file_list
*flist
, char *dir
)
839 char fname
[MAXPATHLEN
];
846 rprintf(FERROR
, "opendir(%s): %s\n", dir
, strerror(errno
));
850 strlcpy(fname
, dir
, MAXPATHLEN
);
852 if (fname
[l
- 1] != '/') {
853 if (l
== MAXPATHLEN
- 1) {
856 "skipping long-named directory %s\n",
861 strlcat(fname
, "/", MAXPATHLEN
);
864 p
= fname
+ strlen(fname
);
866 local_exclude_list
= NULL
;
869 if (strlen(fname
) + strlen(".cvsignore") <= MAXPATHLEN
- 1) {
870 strcpy(p
, ".cvsignore");
871 add_exclude_file(&exclude_list
,fname
,MISSING_OK
,ADD_EXCLUDE
);
875 "cannot cvs-exclude in long-named directory %s\n",
880 for (di
= readdir(d
); di
; di
= readdir(d
)) {
881 char *dname
= d_name(di
);
882 if (dname
[0] == '.' && (dname
[1] == '\0' ||
883 (dname
[1] == '.' && dname
[2] == '\0')))
885 strlcpy(p
, dname
, MAXPATHLEN
- l
);
886 send_file_name(f
, flist
, fname
, recurse
, 0);
889 if (local_exclude_list
)
890 free_exclude_list(&local_exclude_list
); /* Zeros pointer too */
897 * The delete_files() function in receiver.c sets f to -1 so that we just
898 * construct the file list in memory without sending it over the wire. It
899 * also has the side-effect of ignoring user-excludes if delete_excluded
900 * is set (so that the delete list includes user-excluded files).
902 struct file_list
*send_file_list(int f
, int argc
, char *argv
[])
906 char *p
, *dir
, *olddir
;
907 char lastpath
[MAXPATHLEN
] = "";
908 struct file_list
*flist
;
912 if (show_filelist_p() && f
!= -1)
913 start_filelist_progress("building file list");
915 start_write
= stats
.total_written
;
920 io_start_buffering(f
);
921 if (filesfrom_fd
>= 0) {
922 if (argv
[0] && !push_dir(argv
[0], 0)) {
923 rprintf(FERROR
, "push_dir %s : %s\n",
924 argv
[0], strerror(errno
));
925 exit_cleanup(RERR_FILESELECT
);
932 char fname2
[MAXPATHLEN
];
933 char *fname
= fname2
;
936 if (read_filesfrom_line(filesfrom_fd
, fname
) == 0)
938 sanitize_path(fname
, NULL
);
942 strlcpy(fname
, *argv
++, MAXPATHLEN
);
944 sanitize_path(fname
, NULL
);
948 if (fname
[l
- 1] == '/') {
949 if (l
== 2 && fname
[0] == '.') {
950 /* Turn "./" into just "." rather than "./." */
953 strlcat(fname
, ".", MAXPATHLEN
);
957 if (link_stat(fname
, &st
) != 0) {
960 rprintf(FERROR
, "link_stat %s : %s\n",
961 fname
, strerror(errno
));
966 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
967 rprintf(FINFO
, "skipping directory %s\n", fname
);
974 if (!relative_paths
) {
975 p
= strrchr(fname
, '/');
984 } else if (f
!= -1 && implied_dirs
&& (p
=strrchr(fname
,'/')) && p
!= fname
) {
985 /* this ensures we send the intermediate directories,
986 thus getting their permissions right */
987 char *lp
= lastpath
, *fn
= fname
, *slash
= fname
;
989 /* Skip any initial directories in our path that we
990 * have in common with lastpath. */
991 while (*fn
&& *lp
== *fn
) {
997 if (fn
!= p
|| (*lp
&& *lp
!= '/')) {
998 int copy_links_saved
= copy_links
;
999 int recurse_saved
= recurse
;
1000 copy_links
= copy_unsafe_links
;
1001 /* set recurse to 1 to prevent make_file
1002 * from ignoring directory, but still
1003 * turn off the recursive parameter to
1006 while ((slash
= strchr(slash
+1, '/')) != 0) {
1008 send_file_name(f
, flist
, fname
, 0, 0);
1011 copy_links
= copy_links_saved
;
1012 recurse
= recurse_saved
;
1014 strlcpy(lastpath
, fname
, sizeof lastpath
);
1023 olddir
= push_dir(dir
, 1);
1027 rprintf(FERROR
, "push_dir %s : %s\n",
1028 dir
, strerror(errno
));
1035 if (one_file_system
)
1036 set_filesystem(fname
);
1038 send_file_name(f
, flist
, fname
, recurse
, FLAG_DELETE
);
1040 if (olddir
!= NULL
) {
1042 if (pop_dir(olddir
) != 0) {
1043 rprintf(FERROR
, "pop_dir %s : %s\n",
1044 dir
, strerror(errno
));
1045 exit_cleanup(RERR_FILESELECT
);
1051 send_file_entry(NULL
, f
, 0);
1054 if (show_filelist_p() && f
!= -1) {
1055 finish_filelist_progress(flist
);
1058 clean_flist(flist
, 0, 0);
1060 /* now send the uid/gid list. This was introduced in protocol
1066 /* send the io_error flag */
1068 extern int module_id
;
1069 write_int(f
, lp_ignore_errors(module_id
) ? 0 : io_error
);
1074 stats
.flist_size
= stats
.total_written
- start_write
;
1075 stats
.num_files
= flist
->count
;
1076 if (write_batch
) /* dw */
1077 write_batch_flist_info(flist
->count
, flist
->files
);
1081 rprintf(FINFO
, "send_file_list done\n");
1087 struct file_list
*recv_file_list(int f
)
1089 struct file_list
*flist
;
1090 unsigned char flags
;
1092 extern int list_only
;
1094 if (show_filelist_p())
1095 start_filelist_progress("receiving file list");
1097 start_read
= stats
.total_read
;
1099 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1104 flist
->malloced
= 1000;
1106 (struct file_struct
**) malloc(sizeof(flist
->files
[0]) *
1112 for (flags
= read_byte(f
); flags
; flags
= read_byte(f
)) {
1113 int i
= flist
->count
;
1115 flist_expand(flist
);
1117 receive_file_entry(&flist
->files
[i
], flags
, f
);
1119 if (S_ISREG(flist
->files
[i
]->mode
))
1120 stats
.total_size
+= flist
->files
[i
]->length
;
1124 maybe_emit_filelist_progress(flist
);
1127 rprintf(FINFO
, "recv_file_name(%s)\n",
1128 f_name(flist
->files
[i
]));
1133 rprintf(FINFO
, "received %d names\n", flist
->count
);
1135 clean_flist(flist
, relative_paths
, 1);
1137 if (show_filelist_p()) {
1138 finish_filelist_progress(flist
);
1141 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1143 recv_uid_list(f
, flist
);
1146 /* recv the io_error flag */
1147 if (f
!= -1 && !read_batch
) { /* dw-added readbatch */
1148 extern int module_id
;
1149 extern int ignore_errors
;
1150 if (lp_ignore_errors(module_id
) || ignore_errors
) {
1153 io_error
|= read_int(f
);
1159 for (i
= 0; i
< flist
->count
; i
++) {
1160 list_file_entry(flist
->files
[i
]);
1166 rprintf(FINFO
, "recv_file_list done\n");
1168 stats
.flist_size
= stats
.total_read
- start_read
;
1169 stats
.num_files
= flist
->count
;
1174 out_of_memory("recv_file_list");
1175 return NULL
; /* not reached */
1180 * XXX: This is currently the hottest function while building the file
1181 * list, because building f_name()s every time is expensive.
1183 int file_compare(struct file_struct
**f1
, struct file_struct
**f2
)
1185 if (!(*f1
)->basename
&& !(*f2
)->basename
)
1187 if (!(*f1
)->basename
)
1189 if (!(*f2
)->basename
)
1191 if ((*f1
)->dirname
== (*f2
)->dirname
)
1192 return u_strcmp((*f1
)->basename
, (*f2
)->basename
);
1193 return u_strcmp(f_name(*f1
), f_name(*f2
));
1197 int flist_find(struct file_list
*flist
, struct file_struct
*f
)
1199 int low
= 0, high
= flist
->count
- 1;
1201 while (high
>= 0 && !flist
->files
[high
]->basename
) high
--;
1206 while (low
!= high
) {
1207 int mid
= (low
+ high
) / 2;
1209 file_compare(&flist
->files
[flist_up(flist
, mid
)], &f
);
1211 return flist_up(flist
, mid
);
1219 if (file_compare(&flist
->files
[flist_up(flist
, low
)], &f
) == 0)
1220 return flist_up(flist
, low
);
1228 void free_file(struct file_struct
*file
)
1233 free(file
->basename
);
1243 * allocate a new file list
1245 struct file_list
*flist_new(void)
1247 struct file_list
*flist
;
1249 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1251 out_of_memory("send_file_list");
1254 flist
->malloced
= 0;
1255 flist
->files
= NULL
;
1258 flist
->string_area
= string_area_new(0);
1260 flist
->string_area
= NULL
;
1266 * free up all elements in a flist
1268 void flist_free(struct file_list
*flist
)
1271 for (i
= 1; i
< flist
->count
; i
++) {
1272 if (!flist
->string_area
)
1273 free_file(flist
->files
[i
]);
1274 free(flist
->files
[i
]);
1276 /* FIXME: I don't think we generally need to blank the flist
1277 * since it's about to be freed. This will just cause more
1278 * memory traffic. If you want a freed-memory debugger, you
1279 * know where to get it. */
1280 memset((char *) flist
->files
, 0,
1281 sizeof(flist
->files
[0]) * flist
->count
);
1283 if (flist
->string_area
)
1284 string_area_free(flist
->string_area
);
1285 memset((char *) flist
, 0, sizeof(*flist
));
1291 * This routine ensures we don't have any duplicate names in our file list.
1292 * duplicate names can cause corruption because of the pipelining
1294 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
)
1297 char *name
, *prev_name
= NULL
;
1299 if (!flist
|| flist
->count
== 0)
1302 qsort(flist
->files
, flist
->count
,
1303 sizeof(flist
->files
[0]), (int (*)()) file_compare
);
1305 for (i
= no_dups
? 0 : flist
->count
; i
< flist
->count
; i
++) {
1306 if (flist
->files
[i
]->basename
) {
1308 prev_name
= f_name(flist
->files
[i
]);
1312 while (++i
< flist
->count
) {
1313 if (!flist
->files
[i
]->basename
)
1315 name
= f_name(flist
->files
[i
]);
1316 if (strcmp(name
, prev_name
) == 0) {
1317 if (verbose
> 1 && !am_server
) {
1319 "removing duplicate name %s from file list %d\n",
1322 /* Make sure that if we unduplicate '.', that we don't
1323 * lose track of a user-specified starting point (or
1324 * else deletions will mysteriously fail with -R). */
1325 if (flist
->files
[i
]->flags
& FLAG_DELETE
)
1326 flist
->files
[prev_i
]->flags
|= FLAG_DELETE
;
1327 /* it's not great that the flist knows the semantics of
1328 * the file memory usage, but i'd rather not add a flag
1329 * byte to that struct.
1330 * XXX can i use a bit in the flags field? */
1331 if (flist
->string_area
)
1332 flist
->files
[i
][0] = null_file
;
1334 free_file(flist
->files
[i
]);
1338 /* We set prev_name every iteration to avoid it becoming
1339 * invalid when names[][] in f_name() wraps around. */
1344 /* we need to strip off the root directory in the case
1345 of relative paths, but this must be done _after_
1346 the sorting phase */
1347 for (i
= 0; i
< flist
->count
; i
++) {
1348 if (flist
->files
[i
]->dirname
&&
1349 flist
->files
[i
]->dirname
[0] == '/') {
1350 memmove(&flist
->files
[i
]->dirname
[0],
1351 &flist
->files
[i
]->dirname
[1],
1352 strlen(flist
->files
[i
]->dirname
));
1355 if (flist
->files
[i
]->dirname
&&
1356 !flist
->files
[i
]->dirname
[0]) {
1357 flist
->files
[i
]->dirname
= NULL
;
1365 for (i
= 0; i
< flist
->count
; i
++) {
1366 rprintf(FINFO
, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1368 NS(flist
->files
[i
]->dirname
),
1369 NS(flist
->files
[i
]->basename
),
1370 (int) flist
->files
[i
]->mode
,
1371 (double) flist
->files
[i
]->length
);
1377 * return the full filename of a flist entry
1379 * This function is too expensive at the moment, because it copies
1380 * strings when often we only want to compare them. In any case,
1381 * using strlcat is silly because it will walk the string repeatedly.
1383 char *f_name(struct file_struct
*f
)
1385 static char names
[10][MAXPATHLEN
];
1389 if (!f
|| !f
->basename
)
1397 off
= strlcpy(p
, f
->dirname
, MAXPATHLEN
);
1398 off
+= strlcpy(p
+ off
, "/", MAXPATHLEN
- off
);
1399 off
+= strlcpy(p
+ off
, f
->basename
, MAXPATHLEN
- off
);
1401 strlcpy(p
, f
->basename
, MAXPATHLEN
);