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
)
265 if (fname
&& fname
[0] == '.' && !fname
[1]) {
266 /* never exclude '.', even if somebody does --exclude '*' */
269 if (server_exclude_list
&&
270 check_exclude(server_exclude_list
, fname
, is_dir
))
272 if (exclude_level
!= ALL_EXCLUDES
)
274 if (exclude_list
&& check_exclude(exclude_list
, fname
, is_dir
))
276 if (local_exclude_list
&&
277 check_exclude(local_exclude_list
, fname
, is_dir
))
282 /* used by the one_file_system code */
283 static dev_t filesystem_dev
;
285 static void set_filesystem(char *fname
)
288 if (link_stat(fname
, &st
) != 0)
290 filesystem_dev
= st
.st_dev
;
294 static int to_wire_mode(mode_t mode
)
296 if (S_ISLNK(mode
) && (_S_IFLNK
!= 0120000)) {
297 return (mode
& ~(_S_IFMT
)) | 0120000;
302 static mode_t
from_wire_mode(int mode
)
304 if ((mode
& (_S_IFMT
)) == 0120000 && (_S_IFLNK
!= 0120000)) {
305 return (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
307 return (mode_t
) mode
;
311 static void send_directory(int f
, struct file_list
*flist
, char *dir
);
313 static char *flist_dir
;
317 * Make sure @p flist is big enough to hold at least @p flist->count
320 static void flist_expand(struct file_list
*flist
)
322 if (flist
->count
>= flist
->malloced
) {
326 if (flist
->malloced
< 1000)
327 flist
->malloced
+= 1000;
329 flist
->malloced
*= 2;
331 new_bytes
= sizeof(flist
->files
[0]) * flist
->malloced
;
334 new_ptr
= realloc(flist
->files
, new_bytes
);
336 new_ptr
= malloc(new_bytes
);
339 rprintf(FINFO
, "expand file_list to %.0f bytes, did%s move\n",
341 (new_ptr
== flist
->files
) ? " not" : "");
344 flist
->files
= (struct file_struct
**) new_ptr
;
347 out_of_memory("flist_expand");
352 static void send_file_entry(struct file_struct
*file
, int f
,
356 static time_t last_time
;
357 static mode_t last_mode
;
358 static DEV64_T last_rdev
;
359 static uid_t last_uid
;
360 static gid_t last_gid
;
361 static char lastname
[MAXPATHLEN
];
373 io_write_phase
= "send_file_entry";
375 fname
= f_name(file
);
379 if (file
->mode
== last_mode
)
381 if (file
->rdev
== last_rdev
)
383 if (file
->uid
== last_uid
)
385 if (file
->gid
== last_gid
)
387 if (file
->modtime
== last_time
)
391 lastname
[l1
] && (fname
[l1
] == lastname
[l1
]) && (l1
< 255);
393 l2
= strlen(fname
) - l1
;
400 /* we must make sure we don't send a zero flags byte or the other
401 end will terminate the flist transfer */
402 if (flags
== 0 && !S_ISDIR(file
->mode
))
403 flags
|= FLAG_DELETE
;
407 write_byte(f
, flags
);
408 if (flags
& SAME_NAME
)
410 if (flags
& LONG_NAME
)
414 write_buf(f
, fname
+ l1
, l2
);
416 write_longint(f
, file
->length
);
417 if (!(flags
& SAME_TIME
))
418 write_int(f
, (int) file
->modtime
);
419 if (!(flags
& SAME_MODE
))
420 write_int(f
, to_wire_mode(file
->mode
));
421 if (preserve_uid
&& !(flags
& SAME_UID
)) {
423 write_int(f
, (int) file
->uid
);
425 if (preserve_gid
&& !(flags
& SAME_GID
)) {
427 write_int(f
, (int) file
->gid
);
429 if (preserve_devices
&& IS_DEVICE(file
->mode
)
430 && !(flags
& SAME_RDEV
))
431 write_int(f
, (int) file
->rdev
);
434 if (preserve_links
&& S_ISLNK(file
->mode
)) {
435 write_int(f
, strlen(file
->link
));
436 write_buf(f
, file
->link
, strlen(file
->link
));
440 #if SUPPORT_HARD_LINKS
441 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
442 if (remote_version
< 26) {
443 /* 32-bit dev_t and ino_t */
444 write_int(f
, (int) file
->dev
);
445 write_int(f
, (int) file
->inode
);
447 /* 64-bit dev_t and ino_t */
448 write_longint(f
, file
->dev
);
449 write_longint(f
, file
->inode
);
454 if (always_checksum
) {
455 if (remote_version
< 21) {
456 write_buf(f
, file
->sum
, 2);
458 write_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
462 last_mode
= file
->mode
;
463 last_rdev
= file
->rdev
;
464 last_uid
= file
->uid
;
465 last_gid
= file
->gid
;
466 last_time
= file
->modtime
;
468 strlcpy(lastname
, fname
, MAXPATHLEN
);
469 lastname
[MAXPATHLEN
- 1] = 0;
471 io_write_phase
= "unknown";
476 static void receive_file_entry(struct file_struct
**fptr
,
477 unsigned flags
, int f
)
479 static time_t last_time
;
480 static mode_t last_mode
;
481 static DEV64_T last_rdev
;
482 static uid_t last_uid
;
483 static gid_t last_gid
;
484 static char lastname
[MAXPATHLEN
];
485 char thisname
[MAXPATHLEN
];
486 unsigned int l1
= 0, l2
= 0;
488 struct file_struct
*file
;
490 if (flags
& SAME_NAME
)
493 if (flags
& LONG_NAME
)
498 file
= (struct file_struct
*) malloc(sizeof(*file
));
500 out_of_memory("receive_file_entry");
501 memset((char *) file
, 0, sizeof(*file
));
504 if (l2
>= MAXPATHLEN
- l1
) {
506 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
507 flags
, l1
, l2
, lastname
);
508 overflow("receive_file_entry");
511 strlcpy(thisname
, lastname
, l1
+ 1);
512 read_sbuf(f
, &thisname
[l1
], l2
);
513 thisname
[l1
+ l2
] = 0;
515 strlcpy(lastname
, thisname
, MAXPATHLEN
);
516 lastname
[MAXPATHLEN
- 1] = 0;
518 clean_fname(thisname
);
520 if (sanitize_paths
) {
521 sanitize_path(thisname
, NULL
);
524 if ((p
= strrchr(thisname
, '/'))) {
525 static char *lastdir
;
527 if (lastdir
&& strcmp(thisname
, lastdir
) == 0) {
528 file
->dirname
= lastdir
;
530 file
->dirname
= strdup(thisname
);
531 lastdir
= file
->dirname
;
533 file
->basename
= strdup(p
+ 1);
535 file
->dirname
= NULL
;
536 file
->basename
= strdup(thisname
);
540 out_of_memory("receive_file_entry 1");
544 file
->length
= read_longint(f
);
546 (flags
& SAME_TIME
) ? last_time
: (time_t) read_int(f
);
548 (flags
& SAME_MODE
) ? last_mode
: from_wire_mode(read_int(f
));
551 (flags
& SAME_UID
) ? last_uid
: (uid_t
) read_int(f
);
554 (flags
& SAME_GID
) ? last_gid
: (gid_t
) read_int(f
);
555 if (preserve_devices
&& IS_DEVICE(file
->mode
))
557 (flags
& SAME_RDEV
) ? last_rdev
: (DEV64_T
) read_int(f
);
559 if (preserve_links
&& S_ISLNK(file
->mode
)) {
562 rprintf(FERROR
, "overflow: l=%d\n", l
);
563 overflow("receive_file_entry");
565 file
->link
= (char *) malloc(l
+ 1);
567 out_of_memory("receive_file_entry 2");
568 read_sbuf(f
, file
->link
, l
);
569 if (sanitize_paths
) {
570 sanitize_path(file
->link
, file
->dirname
);
573 #if SUPPORT_HARD_LINKS
574 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
575 if (remote_version
< 26) {
576 file
->dev
= read_int(f
);
577 file
->inode
= read_int(f
);
579 file
->dev
= read_longint(f
);
580 file
->inode
= read_longint(f
);
585 if (always_checksum
) {
586 file
->sum
= (char *) malloc(MD4_SUM_LENGTH
);
588 out_of_memory("md4 sum");
589 if (remote_version
< 21) {
590 read_buf(f
, file
->sum
, 2);
592 read_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
596 last_mode
= file
->mode
;
597 last_rdev
= file
->rdev
;
598 last_uid
= file
->uid
;
599 last_gid
= file
->gid
;
600 last_time
= file
->modtime
;
602 if (!preserve_perms
) {
603 extern int orig_umask
;
604 /* set an appropriate set of permissions based on original
605 permissions and umask. This emulates what GNU cp does */
606 file
->mode
&= ~orig_umask
;
611 /* determine if a file in a different filesstem should be skipped
612 when one_file_system is set. We bascally only want to include
613 the mount points - but they can be hard to find! */
614 static int skip_filesystem(char *fname
, STRUCT_STAT
* st
)
617 char *p
= strrchr(fname
, '/');
619 /* skip all but directories */
620 if (!S_ISDIR(st
->st_mode
))
623 /* if its not a subdirectory then allow */
628 if (link_stat(fname
, &st2
)) {
634 return (st2
.st_dev
!= filesystem_dev
);
637 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
638 /* IRIX cc cares that the operands to the ternary have the same type. */
639 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
642 * Create a file_struct for a named file by reading its stat()
643 * information and performing extensive checks against global
646 * @return the new file, or NULL if there was an error or this file
647 * should be excluded.
649 * @todo There is a small optimization opportunity here to avoid
650 * stat()ing the file in some circumstances, which has a certain cost.
651 * We are called immediately after doing readdir(), and so we may
652 * already know the d_type of the file. We could for example avoid
653 * statting directories if we're not recursing, but this is not a very
654 * important case. Some systems may not have d_type.
656 struct file_struct
*make_file(char *fname
, struct string_area
**ap
,
659 struct file_struct
*file
;
661 char sum
[SUM_LENGTH
];
663 char cleaned_name
[MAXPATHLEN
];
664 char linkbuf
[MAXPATHLEN
];
665 extern int module_id
;
667 strlcpy(cleaned_name
, fname
, MAXPATHLEN
);
668 cleaned_name
[MAXPATHLEN
- 1] = 0;
669 clean_fname(cleaned_name
);
670 if (sanitize_paths
) {
671 sanitize_path(cleaned_name
, NULL
);
673 fname
= cleaned_name
;
675 memset(sum
, 0, SUM_LENGTH
);
677 if (readlink_stat(fname
, &st
, linkbuf
) != 0) {
678 int save_errno
= errno
;
679 if (errno
== ENOENT
&& exclude_level
!= NO_EXCLUDES
) {
680 /* either symlink pointing nowhere or file that
681 * was removed during rsync run; see if excluded
682 * before reporting an error */
683 if (check_exclude_file(fname
, 0, exclude_level
)) {
684 /* file is excluded anyway, ignore silently */
689 rprintf(FERROR
, "readlink %s: %s\n",
690 fname
, strerror(save_errno
));
694 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
695 if (exclude_level
== NO_EXCLUDES
)
698 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
699 rprintf(FINFO
, "skipping directory %s\n", fname
);
703 if (one_file_system
&& st
.st_dev
!= filesystem_dev
) {
704 if (skip_filesystem(fname
, &st
))
708 if (check_exclude_file(fname
, S_ISDIR(st
.st_mode
) != 0, exclude_level
))
711 if (lp_ignore_nonreadable(module_id
) && access(fname
, R_OK
) != 0)
717 rprintf(FINFO
, "make_file(%s,*,%d)\n", fname
, exclude_level
);
719 file
= (struct file_struct
*) malloc(sizeof(*file
));
721 out_of_memory("make_file");
722 memset((char *) file
, 0, sizeof(*file
));
724 if ((p
= strrchr(fname
, '/'))) {
725 static char *lastdir
;
727 if (lastdir
&& strcmp(fname
, lastdir
) == 0) {
728 file
->dirname
= lastdir
;
730 file
->dirname
= strdup(fname
);
731 lastdir
= file
->dirname
;
733 file
->basename
= STRDUP(ap
, p
+ 1);
736 file
->dirname
= NULL
;
737 file
->basename
= STRDUP(ap
, fname
);
740 file
->modtime
= st
.st_mtime
;
741 file
->length
= st
.st_size
;
742 file
->mode
= st
.st_mode
;
743 file
->uid
= st
.st_uid
;
744 file
->gid
= st
.st_gid
;
745 file
->dev
= st
.st_dev
;
746 file
->inode
= st
.st_ino
;
747 #ifdef HAVE_STRUCT_STAT_ST_RDEV
748 file
->rdev
= st
.st_rdev
;
752 if (S_ISLNK(st
.st_mode
)) {
753 file
->link
= STRDUP(ap
, linkbuf
);
757 if (always_checksum
) {
758 file
->sum
= (char *) MALLOC(ap
, MD4_SUM_LENGTH
);
760 out_of_memory("md4 sum");
761 /* drat. we have to provide a null checksum for non-regular
762 files in order to be compatible with earlier versions
764 if (S_ISREG(st
.st_mode
)) {
765 file_checksum(fname
, file
->sum
, st
.st_size
);
767 memset(file
->sum
, 0, MD4_SUM_LENGTH
);
772 static char *lastdir
;
773 if (lastdir
&& strcmp(lastdir
, flist_dir
) == 0) {
774 file
->basedir
= lastdir
;
776 file
->basedir
= strdup(flist_dir
);
777 lastdir
= file
->basedir
;
780 file
->basedir
= NULL
;
783 if (!S_ISDIR(st
.st_mode
))
784 stats
.total_size
+= st
.st_size
;
791 void send_file_name(int f
, struct file_list
*flist
, char *fname
,
792 int recursive
, unsigned base_flags
)
794 struct file_struct
*file
;
795 extern int delete_excluded
;
797 /* f is set to -1 when calculating deletion file list */
798 file
= make_file(fname
, &flist
->string_area
,
799 f
== -1 && delete_excluded
? SERVER_EXCLUDES
805 maybe_emit_filelist_progress(flist
);
809 if (write_batch
) /* dw */
810 file
->flags
= FLAG_DELETE
;
812 if (file
->basename
[0]) {
813 flist
->files
[flist
->count
++] = file
;
814 send_file_entry(file
, f
, base_flags
);
817 if (S_ISDIR(file
->mode
) && recursive
) {
818 struct exclude_struct
**last_exclude_list
=
820 send_directory(f
, flist
, f_name(file
));
821 local_exclude_list
= last_exclude_list
;
828 static void send_directory(int f
, struct file_list
*flist
, char *dir
)
832 char fname
[MAXPATHLEN
];
839 rprintf(FERROR
, "opendir(%s): %s\n", dir
, strerror(errno
));
843 strlcpy(fname
, dir
, MAXPATHLEN
);
845 if (fname
[l
- 1] != '/') {
846 if (l
== MAXPATHLEN
- 1) {
849 "skipping long-named directory %s\n",
854 strlcat(fname
, "/", MAXPATHLEN
);
857 p
= fname
+ strlen(fname
);
859 local_exclude_list
= NULL
;
862 if (strlen(fname
) + strlen(".cvsignore") <= MAXPATHLEN
- 1) {
863 strcpy(p
, ".cvsignore");
864 add_exclude_file(&exclude_list
,fname
,MISSING_OK
,ADD_EXCLUDE
);
868 "cannot cvs-exclude in long-named directory %s\n",
873 for (di
= readdir(d
); di
; di
= readdir(d
)) {
874 char *dname
= d_name(di
);
875 if (dname
[0] == '.' && (dname
[1] == '\0' ||
876 (dname
[1] == '.' && dname
[2] == '\0')))
878 strlcpy(p
, dname
, MAXPATHLEN
- l
);
879 send_file_name(f
, flist
, fname
, recurse
, 0);
882 if (local_exclude_list
)
883 free_exclude_list(&local_exclude_list
); /* Zeros pointer too */
890 * The delete_files() function in receiver.c sets f to -1 so that we just
891 * construct the file list in memory without sending it over the wire. It
892 * also has the side-effect of ignoring user-excludes if delete_excluded
893 * is set (so that the delete list includes user-excluded files).
895 struct file_list
*send_file_list(int f
, int argc
, char *argv
[])
899 char *p
, *dir
, *olddir
;
900 char lastpath
[MAXPATHLEN
] = "";
901 struct file_list
*flist
;
905 if (show_filelist_p() && f
!= -1)
906 start_filelist_progress("building file list");
908 start_write
= stats
.total_written
;
913 io_start_buffering(f
);
914 if (filesfrom_fd
>= 0) {
915 if (argv
[0] && !push_dir(argv
[0], 0)) {
916 rprintf(FERROR
, "push_dir %s : %s\n",
917 argv
[0], strerror(errno
));
918 exit_cleanup(RERR_FILESELECT
);
925 char fname2
[MAXPATHLEN
];
926 char *fname
= fname2
;
929 if (read_filesfrom_line(filesfrom_fd
, fname
) == 0)
931 sanitize_path(fname
, NULL
);
935 strlcpy(fname
, *argv
++, MAXPATHLEN
);
937 sanitize_path(fname
, NULL
);
941 if (l
!= 1 && fname
[l
- 1] == '/') {
942 if ((l
== 2) && (fname
[0] == '.')) {
943 /* Turn ./ into just . rather than ./.
944 This was put in to avoid a problem with
945 rsync -aR --delete from ./
946 The send_file_name() below of ./ was
947 mysteriously preventing deletes */
950 strlcat(fname
, ".", MAXPATHLEN
);
954 if (link_stat(fname
, &st
) != 0) {
957 rprintf(FERROR
, "link_stat %s : %s\n",
958 fname
, strerror(errno
));
963 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
964 rprintf(FINFO
, "skipping directory %s\n", fname
);
971 if (!relative_paths
) {
972 p
= strrchr(fname
, '/');
981 } else if (f
!= -1 && implied_dirs
&& (p
=strrchr(fname
,'/')) && p
!= fname
) {
982 /* this ensures we send the intermediate directories,
983 thus getting their permissions right */
984 char *lp
= lastpath
, *fn
= fname
, *slash
= fname
;
986 /* Skip any initial directories in our path that we
987 * have in common with lastpath. */
988 while (*fn
&& *lp
== *fn
) {
994 if (fn
!= p
|| (*lp
&& *lp
!= '/')) {
995 int copy_links_saved
= copy_links
;
996 int recurse_saved
= recurse
;
997 copy_links
= copy_unsafe_links
;
998 /* set recurse to 1 to prevent make_file
999 * from ignoring directory, but still
1000 * turn off the recursive parameter to
1003 while ((slash
= strchr(slash
+1, '/')) != 0) {
1005 send_file_name(f
, flist
, fname
, 0, 0);
1008 copy_links
= copy_links_saved
;
1009 recurse
= recurse_saved
;
1011 strlcpy(lastpath
, fname
, sizeof lastpath
);
1020 olddir
= push_dir(dir
, 1);
1024 rprintf(FERROR
, "push_dir %s : %s\n",
1025 dir
, strerror(errno
));
1032 if (one_file_system
)
1033 set_filesystem(fname
);
1035 send_file_name(f
, flist
, fname
, recurse
, FLAG_DELETE
);
1037 if (olddir
!= NULL
) {
1039 if (pop_dir(olddir
) != 0) {
1040 rprintf(FERROR
, "pop_dir %s : %s\n",
1041 dir
, strerror(errno
));
1042 exit_cleanup(RERR_FILESELECT
);
1048 send_file_entry(NULL
, f
, 0);
1051 if (show_filelist_p() && f
!= -1) {
1052 finish_filelist_progress(flist
);
1055 clean_flist(flist
, 0, 0);
1057 /* now send the uid/gid list. This was introduced in protocol
1063 /* send the io_error flag */
1065 extern int module_id
;
1066 write_int(f
, lp_ignore_errors(module_id
) ? 0 : io_error
);
1071 stats
.flist_size
= stats
.total_written
- start_write
;
1072 stats
.num_files
= flist
->count
;
1073 if (write_batch
) /* dw */
1074 write_batch_flist_info(flist
->count
, flist
->files
);
1078 rprintf(FINFO
, "send_file_list done\n");
1084 struct file_list
*recv_file_list(int f
)
1086 struct file_list
*flist
;
1087 unsigned char flags
;
1089 extern int list_only
;
1091 if (show_filelist_p())
1092 start_filelist_progress("receiving file list");
1094 start_read
= stats
.total_read
;
1096 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1101 flist
->malloced
= 1000;
1103 (struct file_struct
**) malloc(sizeof(flist
->files
[0]) *
1109 for (flags
= read_byte(f
); flags
; flags
= read_byte(f
)) {
1110 int i
= flist
->count
;
1112 flist_expand(flist
);
1114 receive_file_entry(&flist
->files
[i
], flags
, f
);
1116 if (S_ISREG(flist
->files
[i
]->mode
))
1117 stats
.total_size
+= flist
->files
[i
]->length
;
1121 maybe_emit_filelist_progress(flist
);
1124 rprintf(FINFO
, "recv_file_name(%s)\n",
1125 f_name(flist
->files
[i
]));
1130 rprintf(FINFO
, "received %d names\n", flist
->count
);
1132 clean_flist(flist
, relative_paths
, 1);
1134 if (show_filelist_p()) {
1135 finish_filelist_progress(flist
);
1138 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1140 recv_uid_list(f
, flist
);
1143 /* recv the io_error flag */
1144 if (f
!= -1 && !read_batch
) { /* dw-added readbatch */
1145 extern int module_id
;
1146 extern int ignore_errors
;
1147 if (lp_ignore_errors(module_id
) || ignore_errors
) {
1150 io_error
|= read_int(f
);
1156 for (i
= 0; i
< flist
->count
; i
++) {
1157 list_file_entry(flist
->files
[i
]);
1163 rprintf(FINFO
, "recv_file_list done\n");
1165 stats
.flist_size
= stats
.total_read
- start_read
;
1166 stats
.num_files
= flist
->count
;
1171 out_of_memory("recv_file_list");
1172 return NULL
; /* not reached */
1177 * XXX: This is currently the hottest function while building the file
1178 * list, because building f_name()s every time is expensive.
1180 int file_compare(struct file_struct
**f1
, struct file_struct
**f2
)
1182 if (!(*f1
)->basename
&& !(*f2
)->basename
)
1184 if (!(*f1
)->basename
)
1186 if (!(*f2
)->basename
)
1188 if ((*f1
)->dirname
== (*f2
)->dirname
)
1189 return u_strcmp((*f1
)->basename
, (*f2
)->basename
);
1190 return u_strcmp(f_name(*f1
), f_name(*f2
));
1194 int flist_find(struct file_list
*flist
, struct file_struct
*f
)
1196 int low
= 0, high
= flist
->count
- 1;
1198 while (high
>= 0 && !flist
->files
[high
]->basename
) high
--;
1203 while (low
!= high
) {
1204 int mid
= (low
+ high
) / 2;
1206 file_compare(&flist
->files
[flist_up(flist
, mid
)], &f
);
1208 return flist_up(flist
, mid
);
1216 if (file_compare(&flist
->files
[flist_up(flist
, low
)], &f
) == 0)
1217 return flist_up(flist
, low
);
1225 void free_file(struct file_struct
*file
)
1230 free(file
->basename
);
1240 * allocate a new file list
1242 struct file_list
*flist_new(void)
1244 struct file_list
*flist
;
1246 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1248 out_of_memory("send_file_list");
1251 flist
->malloced
= 0;
1252 flist
->files
= NULL
;
1255 flist
->string_area
= string_area_new(0);
1257 flist
->string_area
= NULL
;
1263 * free up all elements in a flist
1265 void flist_free(struct file_list
*flist
)
1268 for (i
= 1; i
< flist
->count
; i
++) {
1269 if (!flist
->string_area
)
1270 free_file(flist
->files
[i
]);
1271 free(flist
->files
[i
]);
1273 /* FIXME: I don't think we generally need to blank the flist
1274 * since it's about to be freed. This will just cause more
1275 * memory traffic. If you want a freed-memory debugger, you
1276 * know where to get it. */
1277 memset((char *) flist
->files
, 0,
1278 sizeof(flist
->files
[0]) * flist
->count
);
1280 if (flist
->string_area
)
1281 string_area_free(flist
->string_area
);
1282 memset((char *) flist
, 0, sizeof(*flist
));
1288 * This routine ensures we don't have any duplicate names in our file list.
1289 * duplicate names can cause corruption because of the pipelining
1291 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
)
1294 char *name
, *prev_name
= NULL
;
1296 if (!flist
|| flist
->count
== 0)
1299 qsort(flist
->files
, flist
->count
,
1300 sizeof(flist
->files
[0]), (int (*)()) file_compare
);
1302 for (i
= no_dups
? 0 : flist
->count
; i
< flist
->count
; i
++) {
1303 if (flist
->files
[i
]->basename
) {
1304 prev_name
= f_name(flist
->files
[i
]);
1308 while (++i
< flist
->count
) {
1309 if (!flist
->files
[i
]->basename
)
1311 name
= f_name(flist
->files
[i
]);
1312 if (strcmp(name
, prev_name
) == 0) {
1313 if (verbose
> 1 && !am_server
) {
1315 "removing duplicate name %s from file list %d\n",
1318 /* it's not great that the flist knows the semantics of
1319 * the file memory usage, but i'd rather not add a flag
1320 * byte to that struct.
1321 * XXX can i use a bit in the flags field? */
1322 if (flist
->string_area
)
1323 flist
->files
[i
][0] = null_file
;
1325 free_file(flist
->files
[i
]);
1331 /* we need to strip off the root directory in the case
1332 of relative paths, but this must be done _after_
1333 the sorting phase */
1334 for (i
= 0; i
< flist
->count
; i
++) {
1335 if (flist
->files
[i
]->dirname
&&
1336 flist
->files
[i
]->dirname
[0] == '/') {
1337 memmove(&flist
->files
[i
]->dirname
[0],
1338 &flist
->files
[i
]->dirname
[1],
1339 strlen(flist
->files
[i
]->dirname
));
1342 if (flist
->files
[i
]->dirname
&&
1343 !flist
->files
[i
]->dirname
[0]) {
1344 flist
->files
[i
]->dirname
= NULL
;
1352 for (i
= 0; i
< flist
->count
; i
++) {
1353 rprintf(FINFO
, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1355 NS(flist
->files
[i
]->dirname
),
1356 NS(flist
->files
[i
]->basename
),
1357 (int) flist
->files
[i
]->mode
,
1358 (double) flist
->files
[i
]->length
);
1364 * return the full filename of a flist entry
1366 * This function is too expensive at the moment, because it copies
1367 * strings when often we only want to compare them. In any case,
1368 * using strlcat is silly because it will walk the string repeatedly.
1370 char *f_name(struct file_struct
*f
)
1372 static char names
[10][MAXPATHLEN
];
1376 if (!f
|| !f
->basename
)
1384 off
= strlcpy(p
, f
->dirname
, MAXPATHLEN
);
1385 off
+= strlcpy(p
+ off
, "/", MAXPATHLEN
- off
);
1386 off
+= strlcpy(p
+ off
, f
->basename
, MAXPATHLEN
- off
);
1388 strlcpy(p
, f
->basename
, MAXPATHLEN
);