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 static struct exclude_struct
**local_exclude_list
;
69 static struct file_struct null_file
;
71 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
);
74 static int show_filelist_p(void)
76 return verbose
&& (recurse
|| files_from
) && !am_server
;
79 static void start_filelist_progress(char *kind
)
81 rprintf(FINFO
, "%s ... ", kind
);
82 if ((verbose
> 1) || do_progress
)
88 static void emit_filelist_progress(const struct file_list
*flist
)
90 rprintf(FINFO
, " %d files...\r", flist
->count
);
94 static void maybe_emit_filelist_progress(const struct file_list
*flist
)
96 if (do_progress
&& show_filelist_p() && ((flist
->count
% 100) == 0))
97 emit_filelist_progress(flist
);
101 static void finish_filelist_progress(const struct file_list
*flist
)
104 /* This overwrites the progress line */
105 rprintf(FINFO
, "%d file%sto consider\n",
106 flist
->count
, flist
->count
== 1 ? " " : "s ");
108 rprintf(FINFO
, "done\n");
112 void show_flist_stats(void)
118 static struct string_area
*string_area_new(int size
)
120 struct string_area
*a
;
124 a
= malloc(sizeof(*a
));
126 out_of_memory("string_area_new");
127 a
->current
= a
->base
= malloc(size
);
129 out_of_memory("string_area_new buffer");
130 a
->end
= a
->base
+ size
;
136 static void string_area_free(struct string_area
*a
)
138 struct string_area
*next
;
140 for (; a
; a
= next
) {
146 static char *string_area_malloc(struct string_area
**ap
, int size
)
149 struct string_area
*a
;
151 /* does the request fit into the current space? */
153 if (a
->current
+ size
>= a
->end
) {
154 /* no; get space, move new string_area to front of the list */
155 a
= string_area_new(size
> ARENA_SIZE
? size
: ARENA_SIZE
);
160 /* have space; do the "allocation." */
166 static char *string_area_strdup(struct string_area
**ap
, const char *src
)
168 char *dest
= string_area_malloc(ap
, strlen(src
) + 1);
169 return strcpy(dest
, src
);
172 static void list_file_entry(struct file_struct
*f
)
177 /* this can happen if duplicate names were removed */
180 permstring(perms
, f
->mode
);
182 if (preserve_links
&& S_ISLNK(f
->mode
)) {
183 rprintf(FINFO
, "%s %11.0f %s %s -> %s\n",
185 (double) f
->length
, timestring(f
->modtime
),
188 rprintf(FINFO
, "%s %11.0f %s %s\n",
190 (double) f
->length
, timestring(f
->modtime
),
197 * Stat either a symlink or its referent, depending on the settings of
198 * copy_links, copy_unsafe_links, etc.
200 * @retval -1 on error
202 * @retval 0 for success
204 * @post If @p path is a symlink, then @p linkbuf (of size @c
205 * MAXPATHLEN) contains the symlink target.
207 * @post @p buffer contains information about the link or the
208 * referrent as appropriate, if they exist.
210 int readlink_stat(const char *path
, STRUCT_STAT
* buffer
, char *linkbuf
)
214 return do_stat(path
, buffer
);
216 if (do_lstat(path
, buffer
) == -1) {
219 if (S_ISLNK(buffer
->st_mode
)) {
221 l
= readlink((char *) path
, linkbuf
, MAXPATHLEN
- 1);
225 if (copy_unsafe_links
&& unsafe_symlink(linkbuf
, path
)) {
227 rprintf(FINFO
,"copying unsafe symlink \"%s\" -> \"%s\"\n",
230 return do_stat(path
, buffer
);
235 return do_stat(path
, buffer
);
239 int link_stat(const char *path
, STRUCT_STAT
* buffer
)
243 return do_stat(path
, buffer
);
245 return do_lstat(path
, buffer
);
248 return do_stat(path
, buffer
);
253 This function is used to check if a file should be included/excluded
254 from the list of files based on its name and type etc
256 static int check_exclude_file(int f
, char *fname
, STRUCT_STAT
* st
)
258 extern int delete_excluded
;
260 /* f is set to -1 when calculating deletion file list */
261 if ((f
== -1) && delete_excluded
) {
264 if (check_exclude(fname
, local_exclude_list
, st
)) {
270 /* used by the one_file_system code */
271 static dev_t filesystem_dev
;
273 static void set_filesystem(char *fname
)
276 if (link_stat(fname
, &st
) != 0)
278 filesystem_dev
= st
.st_dev
;
282 static int to_wire_mode(mode_t mode
)
284 if (S_ISLNK(mode
) && (_S_IFLNK
!= 0120000)) {
285 return (mode
& ~(_S_IFMT
)) | 0120000;
290 static mode_t
from_wire_mode(int mode
)
292 if ((mode
& (_S_IFMT
)) == 0120000 && (_S_IFLNK
!= 0120000)) {
293 return (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
295 return (mode_t
) mode
;
299 static void send_directory(int f
, struct file_list
*flist
, char *dir
);
301 static char *flist_dir
;
305 * Make sure @p flist is big enough to hold at least @p flist->count
308 static void flist_expand(struct file_list
*flist
)
310 if (flist
->count
>= flist
->malloced
) {
314 if (flist
->malloced
< 1000)
315 flist
->malloced
+= 1000;
317 flist
->malloced
*= 2;
319 new_bytes
= sizeof(flist
->files
[0]) * flist
->malloced
;
322 new_ptr
= realloc(flist
->files
, new_bytes
);
324 new_ptr
= malloc(new_bytes
);
327 rprintf(FINFO
, "expand file_list to %.0f bytes, did%s move\n",
329 (new_ptr
== flist
->files
) ? " not" : "");
332 flist
->files
= (struct file_struct
**) new_ptr
;
335 out_of_memory("flist_expand");
340 static void send_file_entry(struct file_struct
*file
, int f
,
344 static time_t last_time
;
345 static mode_t last_mode
;
346 static DEV64_T last_rdev
;
347 static uid_t last_uid
;
348 static gid_t last_gid
;
349 static char lastname
[MAXPATHLEN
];
361 io_write_phase
= "send_file_entry";
363 fname
= f_name(file
);
367 if (file
->mode
== last_mode
)
369 if (file
->rdev
== last_rdev
)
371 if (file
->uid
== last_uid
)
373 if (file
->gid
== last_gid
)
375 if (file
->modtime
== last_time
)
379 lastname
[l1
] && (fname
[l1
] == lastname
[l1
]) && (l1
< 255);
381 l2
= strlen(fname
) - l1
;
388 /* we must make sure we don't send a zero flags byte or the other
389 end will terminate the flist transfer */
390 if (flags
== 0 && !S_ISDIR(file
->mode
))
391 flags
|= FLAG_DELETE
;
395 write_byte(f
, flags
);
396 if (flags
& SAME_NAME
)
398 if (flags
& LONG_NAME
)
402 write_buf(f
, fname
+ l1
, l2
);
404 write_longint(f
, file
->length
);
405 if (!(flags
& SAME_TIME
))
406 write_int(f
, (int) file
->modtime
);
407 if (!(flags
& SAME_MODE
))
408 write_int(f
, to_wire_mode(file
->mode
));
409 if (preserve_uid
&& !(flags
& SAME_UID
)) {
411 write_int(f
, (int) file
->uid
);
413 if (preserve_gid
&& !(flags
& SAME_GID
)) {
415 write_int(f
, (int) file
->gid
);
417 if (preserve_devices
&& IS_DEVICE(file
->mode
)
418 && !(flags
& SAME_RDEV
))
419 write_int(f
, (int) file
->rdev
);
422 if (preserve_links
&& S_ISLNK(file
->mode
)) {
423 write_int(f
, strlen(file
->link
));
424 write_buf(f
, file
->link
, strlen(file
->link
));
428 #if SUPPORT_HARD_LINKS
429 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
430 if (remote_version
< 26) {
431 /* 32-bit dev_t and ino_t */
432 write_int(f
, (int) file
->dev
);
433 write_int(f
, (int) file
->inode
);
435 /* 64-bit dev_t and ino_t */
436 write_longint(f
, file
->dev
);
437 write_longint(f
, file
->inode
);
442 if (always_checksum
) {
443 if (remote_version
< 21) {
444 write_buf(f
, file
->sum
, 2);
446 write_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
450 last_mode
= file
->mode
;
451 last_rdev
= file
->rdev
;
452 last_uid
= file
->uid
;
453 last_gid
= file
->gid
;
454 last_time
= file
->modtime
;
456 strlcpy(lastname
, fname
, MAXPATHLEN
);
457 lastname
[MAXPATHLEN
- 1] = 0;
459 io_write_phase
= "unknown";
464 static void receive_file_entry(struct file_struct
**fptr
,
465 unsigned flags
, int f
)
467 static time_t last_time
;
468 static mode_t last_mode
;
469 static DEV64_T last_rdev
;
470 static uid_t last_uid
;
471 static gid_t last_gid
;
472 static char lastname
[MAXPATHLEN
];
473 char thisname
[MAXPATHLEN
];
474 unsigned int l1
= 0, l2
= 0;
476 struct file_struct
*file
;
478 if (flags
& SAME_NAME
)
481 if (flags
& LONG_NAME
)
486 file
= (struct file_struct
*) malloc(sizeof(*file
));
488 out_of_memory("receive_file_entry");
489 memset((char *) file
, 0, sizeof(*file
));
492 if (l2
>= MAXPATHLEN
- l1
) {
494 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
495 flags
, l1
, l2
, lastname
);
496 overflow("receive_file_entry");
499 strlcpy(thisname
, lastname
, l1
+ 1);
500 read_sbuf(f
, &thisname
[l1
], l2
);
501 thisname
[l1
+ l2
] = 0;
503 strlcpy(lastname
, thisname
, MAXPATHLEN
);
504 lastname
[MAXPATHLEN
- 1] = 0;
506 clean_fname(thisname
);
508 if (sanitize_paths
) {
509 sanitize_path(thisname
, NULL
);
512 if ((p
= strrchr(thisname
, '/'))) {
513 static char *lastdir
;
515 if (lastdir
&& strcmp(thisname
, lastdir
) == 0) {
516 file
->dirname
= lastdir
;
518 file
->dirname
= strdup(thisname
);
519 lastdir
= file
->dirname
;
521 file
->basename
= strdup(p
+ 1);
523 file
->dirname
= NULL
;
524 file
->basename
= strdup(thisname
);
528 out_of_memory("receive_file_entry 1");
532 file
->length
= read_longint(f
);
534 (flags
& SAME_TIME
) ? last_time
: (time_t) read_int(f
);
536 (flags
& SAME_MODE
) ? last_mode
: from_wire_mode(read_int(f
));
539 (flags
& SAME_UID
) ? last_uid
: (uid_t
) read_int(f
);
542 (flags
& SAME_GID
) ? last_gid
: (gid_t
) read_int(f
);
543 if (preserve_devices
&& IS_DEVICE(file
->mode
))
545 (flags
& SAME_RDEV
) ? last_rdev
: (DEV64_T
) read_int(f
);
547 if (preserve_links
&& S_ISLNK(file
->mode
)) {
550 rprintf(FERROR
, "overflow: l=%d\n", l
);
551 overflow("receive_file_entry");
553 file
->link
= (char *) malloc(l
+ 1);
555 out_of_memory("receive_file_entry 2");
556 read_sbuf(f
, file
->link
, l
);
557 if (sanitize_paths
) {
558 sanitize_path(file
->link
, file
->dirname
);
561 #if SUPPORT_HARD_LINKS
562 if (preserve_hard_links
&& S_ISREG(file
->mode
)) {
563 if (remote_version
< 26) {
564 file
->dev
= read_int(f
);
565 file
->inode
= read_int(f
);
567 file
->dev
= read_longint(f
);
568 file
->inode
= read_longint(f
);
573 if (always_checksum
) {
574 file
->sum
= (char *) malloc(MD4_SUM_LENGTH
);
576 out_of_memory("md4 sum");
577 if (remote_version
< 21) {
578 read_buf(f
, file
->sum
, 2);
580 read_buf(f
, file
->sum
, MD4_SUM_LENGTH
);
584 last_mode
= file
->mode
;
585 last_rdev
= file
->rdev
;
586 last_uid
= file
->uid
;
587 last_gid
= file
->gid
;
588 last_time
= file
->modtime
;
590 if (!preserve_perms
) {
591 extern int orig_umask
;
592 /* set an appropriate set of permissions based on original
593 permissions and umask. This emulates what GNU cp does */
594 file
->mode
&= ~orig_umask
;
599 /* determine if a file in a different filesstem should be skipped
600 when one_file_system is set. We bascally only want to include
601 the mount points - but they can be hard to find! */
602 static int skip_filesystem(char *fname
, STRUCT_STAT
* st
)
605 char *p
= strrchr(fname
, '/');
607 /* skip all but directories */
608 if (!S_ISDIR(st
->st_mode
))
611 /* if its not a subdirectory then allow */
616 if (link_stat(fname
, &st2
)) {
622 return (st2
.st_dev
!= filesystem_dev
);
625 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
626 /* IRIX cc cares that the operands to the ternary have the same type. */
627 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
630 * Create a file_struct for a named file by reading its stat()
631 * information and performing extensive checks against global
634 * @return the new file, or NULL if there was an error or this file
635 * should be excluded.
637 * @todo There is a small optimization opportunity here to avoid
638 * stat()ing the file in some circumstances, which has a certain cost.
639 * We are called immediately after doing readdir(), and so we may
640 * already know the d_type of the file. We could for example avoid
641 * statting directories if we're not recursing, but this is not a very
642 * important case. Some systems may not have d_type.
644 struct file_struct
*make_file(int f
, char *fname
, struct string_area
**ap
,
647 struct file_struct
*file
;
649 char sum
[SUM_LENGTH
];
651 char cleaned_name
[MAXPATHLEN
];
652 char linkbuf
[MAXPATHLEN
];
653 extern int module_id
;
655 strlcpy(cleaned_name
, fname
, MAXPATHLEN
);
656 cleaned_name
[MAXPATHLEN
- 1] = 0;
657 clean_fname(cleaned_name
);
658 if (sanitize_paths
) {
659 sanitize_path(cleaned_name
, NULL
);
661 fname
= cleaned_name
;
663 memset(sum
, 0, SUM_LENGTH
);
665 if (readlink_stat(fname
, &st
, linkbuf
) != 0) {
666 int save_errno
= errno
;
667 if ((errno
== ENOENT
) && !noexcludes
) {
668 /* either symlink pointing nowhere or file that
669 * was removed during rsync run; see if excluded
670 * before reporting an error */
671 memset((char *) &st
, 0, sizeof(st
));
672 if (check_exclude_file(f
, fname
, &st
)) {
673 /* file is excluded anyway, ignore silently */
678 rprintf(FERROR
, "readlink %s: %s\n",
679 fname
, strerror(save_errno
));
683 /* we use noexcludes from backup.c */
687 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
688 rprintf(FINFO
, "skipping directory %s\n", fname
);
692 if (one_file_system
&& st
.st_dev
!= filesystem_dev
) {
693 if (skip_filesystem(fname
, &st
))
697 if (check_exclude_file(f
, fname
, &st
))
701 if (lp_ignore_nonreadable(module_id
) && access(fname
, R_OK
) != 0)
707 rprintf(FINFO
, "make_file(%d,%s)\n", f
, fname
);
709 file
= (struct file_struct
*) malloc(sizeof(*file
));
711 out_of_memory("make_file");
712 memset((char *) file
, 0, sizeof(*file
));
714 if ((p
= strrchr(fname
, '/'))) {
715 static char *lastdir
;
717 if (lastdir
&& strcmp(fname
, lastdir
) == 0) {
718 file
->dirname
= lastdir
;
720 file
->dirname
= strdup(fname
);
721 lastdir
= file
->dirname
;
723 file
->basename
= STRDUP(ap
, p
+ 1);
726 file
->dirname
= NULL
;
727 file
->basename
= STRDUP(ap
, fname
);
730 file
->modtime
= st
.st_mtime
;
731 file
->length
= st
.st_size
;
732 file
->mode
= st
.st_mode
;
733 file
->uid
= st
.st_uid
;
734 file
->gid
= st
.st_gid
;
735 file
->dev
= st
.st_dev
;
736 file
->inode
= st
.st_ino
;
737 #ifdef HAVE_STRUCT_STAT_ST_RDEV
738 file
->rdev
= st
.st_rdev
;
742 if (S_ISLNK(st
.st_mode
)) {
743 file
->link
= STRDUP(ap
, linkbuf
);
747 if (always_checksum
) {
748 file
->sum
= (char *) MALLOC(ap
, MD4_SUM_LENGTH
);
750 out_of_memory("md4 sum");
751 /* drat. we have to provide a null checksum for non-regular
752 files in order to be compatible with earlier versions
754 if (S_ISREG(st
.st_mode
)) {
755 file_checksum(fname
, file
->sum
, st
.st_size
);
757 memset(file
->sum
, 0, MD4_SUM_LENGTH
);
762 static char *lastdir
;
763 if (lastdir
&& strcmp(lastdir
, flist_dir
) == 0) {
764 file
->basedir
= lastdir
;
766 file
->basedir
= strdup(flist_dir
);
767 lastdir
= file
->basedir
;
770 file
->basedir
= NULL
;
773 if (!S_ISDIR(st
.st_mode
))
774 stats
.total_size
+= st
.st_size
;
781 void send_file_name(int f
, struct file_list
*flist
, char *fname
,
782 int recursive
, unsigned base_flags
)
784 struct file_struct
*file
;
786 file
= make_file(f
, fname
, &flist
->string_area
, 0);
791 maybe_emit_filelist_progress(flist
);
795 if (write_batch
) /* dw */
796 file
->flags
= FLAG_DELETE
;
798 if (strcmp(file
->basename
, "")) {
799 flist
->files
[flist
->count
++] = file
;
800 send_file_entry(file
, f
, base_flags
);
803 if (S_ISDIR(file
->mode
) && recursive
) {
804 struct exclude_struct
**last_exclude_list
=
806 send_directory(f
, flist
, f_name(file
));
807 local_exclude_list
= last_exclude_list
;
814 static void send_directory(int f
, struct file_list
*flist
, char *dir
)
818 char fname
[MAXPATHLEN
];
825 rprintf(FERROR
, "opendir(%s): %s\n", dir
, strerror(errno
));
829 strlcpy(fname
, dir
, MAXPATHLEN
);
831 if (fname
[l
- 1] != '/') {
832 if (l
== MAXPATHLEN
- 1) {
835 "skipping long-named directory %s\n",
840 strlcat(fname
, "/", MAXPATHLEN
);
843 p
= fname
+ strlen(fname
);
845 local_exclude_list
= NULL
;
848 if (strlen(fname
) + strlen(".cvsignore") <= MAXPATHLEN
- 1) {
849 strcpy(p
, ".cvsignore");
851 make_exclude_list(fname
, NULL
, 0, 0);
855 "cannot cvs-exclude in long-named directory %s\n",
860 for (di
= readdir(d
); di
; di
= readdir(d
)) {
861 char *dname
= d_name(di
);
862 if (strcmp(dname
, ".") == 0 || strcmp(dname
, "..") == 0)
864 strlcpy(p
, dname
, MAXPATHLEN
- l
);
865 send_file_name(f
, flist
, fname
, recurse
, 0);
868 if (local_exclude_list
) {
869 add_exclude_list("!", &local_exclude_list
, 0);
878 * I <b>think</b> f==-1 means that the list should just be built in
879 * memory and not transmitted. But who can tell? -- mbp
881 struct file_list
*send_file_list(int f
, int argc
, char *argv
[])
885 char *p
, *dir
, *olddir
;
886 char lastpath
[MAXPATHLEN
] = "";
887 struct file_list
*flist
;
891 if (show_filelist_p() && f
!= -1)
892 start_filelist_progress("building file list");
894 start_write
= stats
.total_written
;
899 io_start_buffering(f
);
900 if (filesfrom_fd
>= 0) {
901 if (argv
[0] && !push_dir(argv
[0], 0)) {
902 rprintf(FERROR
, "push_dir %s : %s\n",
903 argv
[0], strerror(errno
));
904 exit_cleanup(RERR_FILESELECT
);
911 char fname2
[MAXPATHLEN
];
912 char *fname
= fname2
;
915 if (read_filesfrom_line(filesfrom_fd
, fname
) == 0)
917 sanitize_path(fname
, NULL
);
921 strlcpy(fname
, *argv
++, MAXPATHLEN
);
923 sanitize_path(fname
, NULL
);
927 if (l
!= 1 && fname
[l
- 1] == '/') {
928 if ((l
== 2) && (fname
[0] == '.')) {
929 /* Turn ./ into just . rather than ./.
930 This was put in to avoid a problem with
931 rsync -aR --delete from ./
932 The send_file_name() below of ./ was
933 mysteriously preventing deletes */
936 strlcat(fname
, ".", MAXPATHLEN
);
940 if (link_stat(fname
, &st
) != 0) {
943 rprintf(FERROR
, "link_stat %s : %s\n",
944 fname
, strerror(errno
));
949 if (S_ISDIR(st
.st_mode
) && !recurse
&& !files_from
) {
950 rprintf(FINFO
, "skipping directory %s\n", fname
);
957 if (!relative_paths
) {
958 p
= strrchr(fname
, '/');
967 } else if (f
!= -1 && implied_dirs
&& (p
=strrchr(fname
,'/')) && p
!= fname
) {
968 /* this ensures we send the intermediate directories,
969 thus getting their permissions right */
970 char *lp
= lastpath
, *fn
= fname
, *slash
= fname
;
972 /* Skip any initial directories in our path that we
973 * have in common with lastpath. */
974 while (*fn
&& *lp
== *fn
) {
980 if (fn
!= p
|| (*lp
&& *lp
!= '/')) {
981 int copy_links_saved
= copy_links
;
982 int recurse_saved
= recurse
;
983 copy_links
= copy_unsafe_links
;
984 /* set recurse to 1 to prevent make_file
985 * from ignoring directory, but still
986 * turn off the recursive parameter to
989 while ((slash
= strchr(slash
+1, '/')) != 0) {
991 send_file_name(f
, flist
, fname
, 0, 0);
994 copy_links
= copy_links_saved
;
995 recurse
= recurse_saved
;
997 strlcpy(lastpath
, fname
, sizeof lastpath
);
1006 olddir
= push_dir(dir
, 1);
1010 rprintf(FERROR
, "push_dir %s : %s\n",
1011 dir
, strerror(errno
));
1018 if (one_file_system
)
1019 set_filesystem(fname
);
1021 send_file_name(f
, flist
, fname
, recurse
, FLAG_DELETE
);
1023 if (olddir
!= NULL
) {
1025 if (pop_dir(olddir
) != 0) {
1026 rprintf(FERROR
, "pop_dir %s : %s\n",
1027 dir
, strerror(errno
));
1028 exit_cleanup(RERR_FILESELECT
);
1034 send_file_entry(NULL
, f
, 0);
1037 if (show_filelist_p() && f
!= -1) {
1038 finish_filelist_progress(flist
);
1041 clean_flist(flist
, 0, 0);
1043 /* now send the uid/gid list. This was introduced in protocol
1045 if (f
!= -1 && remote_version
>= 15) {
1049 /* if protocol version is >= 17 then send the io_error flag */
1050 if (f
!= -1 && remote_version
>= 17) {
1051 extern int module_id
;
1052 write_int(f
, lp_ignore_errors(module_id
) ? 0 : io_error
);
1057 stats
.flist_size
= stats
.total_written
- start_write
;
1058 stats
.num_files
= flist
->count
;
1059 if (write_batch
) /* dw */
1060 write_batch_flist_info(flist
->count
, flist
->files
);
1064 rprintf(FINFO
, "send_file_list done\n");
1070 struct file_list
*recv_file_list(int f
)
1072 struct file_list
*flist
;
1073 unsigned char flags
;
1075 extern int list_only
;
1077 if (show_filelist_p())
1078 start_filelist_progress("receiving file list");
1080 start_read
= stats
.total_read
;
1082 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1087 flist
->malloced
= 1000;
1089 (struct file_struct
**) malloc(sizeof(flist
->files
[0]) *
1095 for (flags
= read_byte(f
); flags
; flags
= read_byte(f
)) {
1096 int i
= flist
->count
;
1098 flist_expand(flist
);
1100 receive_file_entry(&flist
->files
[i
], flags
, f
);
1102 if (S_ISREG(flist
->files
[i
]->mode
))
1103 stats
.total_size
+= flist
->files
[i
]->length
;
1107 maybe_emit_filelist_progress(flist
);
1110 rprintf(FINFO
, "recv_file_name(%s)\n",
1111 f_name(flist
->files
[i
]));
1116 rprintf(FINFO
, "received %d names\n", flist
->count
);
1118 clean_flist(flist
, relative_paths
, 1);
1120 if (show_filelist_p()) {
1121 finish_filelist_progress(flist
);
1124 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1125 if (f
!= -1 && remote_version
>= 15) {
1126 recv_uid_list(f
, flist
);
1129 /* if protocol version is >= 17 then recv the io_error flag */
1130 if (f
!= -1 && remote_version
>= 17 && !read_batch
) { /* dw-added readbatch */
1131 extern int module_id
;
1132 extern int ignore_errors
;
1133 if (lp_ignore_errors(module_id
) || ignore_errors
) {
1136 io_error
|= read_int(f
);
1142 for (i
= 0; i
< flist
->count
; i
++) {
1143 list_file_entry(flist
->files
[i
]);
1149 rprintf(FINFO
, "recv_file_list done\n");
1151 stats
.flist_size
= stats
.total_read
- start_read
;
1152 stats
.num_files
= flist
->count
;
1157 out_of_memory("recv_file_list");
1158 return NULL
; /* not reached */
1163 * XXX: This is currently the hottest function while building the file
1164 * list, because building f_name()s every time is expensive.
1166 int file_compare(struct file_struct
**f1
, struct file_struct
**f2
)
1168 if (!(*f1
)->basename
&& !(*f2
)->basename
)
1170 if (!(*f1
)->basename
)
1172 if (!(*f2
)->basename
)
1174 if ((*f1
)->dirname
== (*f2
)->dirname
)
1175 return u_strcmp((*f1
)->basename
, (*f2
)->basename
);
1176 return u_strcmp(f_name(*f1
), f_name(*f2
));
1180 int flist_find(struct file_list
*flist
, struct file_struct
*f
)
1182 int low
= 0, high
= flist
->count
- 1;
1184 while (high
>= 0 && !flist
->files
[high
]->basename
) high
--;
1189 while (low
!= high
) {
1190 int mid
= (low
+ high
) / 2;
1192 file_compare(&flist
->files
[flist_up(flist
, mid
)], &f
);
1194 return flist_up(flist
, mid
);
1202 if (file_compare(&flist
->files
[flist_up(flist
, low
)], &f
) == 0)
1203 return flist_up(flist
, low
);
1211 void free_file(struct file_struct
*file
)
1216 free(file
->basename
);
1226 * allocate a new file list
1228 struct file_list
*flist_new(void)
1230 struct file_list
*flist
;
1232 flist
= (struct file_list
*) malloc(sizeof(flist
[0]));
1234 out_of_memory("send_file_list");
1237 flist
->malloced
= 0;
1238 flist
->files
= NULL
;
1241 flist
->string_area
= string_area_new(0);
1243 flist
->string_area
= NULL
;
1249 * free up all elements in a flist
1251 void flist_free(struct file_list
*flist
)
1254 for (i
= 1; i
< flist
->count
; i
++) {
1255 if (!flist
->string_area
)
1256 free_file(flist
->files
[i
]);
1257 free(flist
->files
[i
]);
1259 /* FIXME: I don't think we generally need to blank the flist
1260 * since it's about to be freed. This will just cause more
1261 * memory traffic. If you want a freed-memory debugger, you
1262 * know where to get it. */
1263 memset((char *) flist
->files
, 0,
1264 sizeof(flist
->files
[0]) * flist
->count
);
1266 if (flist
->string_area
)
1267 string_area_free(flist
->string_area
);
1268 memset((char *) flist
, 0, sizeof(*flist
));
1274 * This routine ensures we don't have any duplicate names in our file list.
1275 * duplicate names can cause corruption because of the pipelining
1277 static void clean_flist(struct file_list
*flist
, int strip_root
, int no_dups
)
1280 char *name
, *prev_name
= NULL
;
1282 if (!flist
|| flist
->count
== 0)
1285 qsort(flist
->files
, flist
->count
,
1286 sizeof(flist
->files
[0]), (int (*)()) file_compare
);
1288 for (i
= no_dups
? 0 : flist
->count
; i
< flist
->count
; i
++) {
1289 if (flist
->files
[i
]->basename
) {
1290 prev_name
= f_name(flist
->files
[i
]);
1294 while (++i
< flist
->count
) {
1295 if (!flist
->files
[i
]->basename
)
1297 name
= f_name(flist
->files
[i
]);
1298 if (strcmp(name
, prev_name
) == 0) {
1299 if (verbose
> 1 && !am_server
) {
1301 "removing duplicate name %s from file list %d\n",
1304 /* it's not great that the flist knows the semantics of
1305 * the file memory usage, but i'd rather not add a flag
1306 * byte to that struct.
1307 * XXX can i use a bit in the flags field? */
1308 if (flist
->string_area
)
1309 flist
->files
[i
][0] = null_file
;
1311 free_file(flist
->files
[i
]);
1317 /* we need to strip off the root directory in the case
1318 of relative paths, but this must be done _after_
1319 the sorting phase */
1320 for (i
= 0; i
< flist
->count
; i
++) {
1321 if (flist
->files
[i
]->dirname
&&
1322 flist
->files
[i
]->dirname
[0] == '/') {
1323 memmove(&flist
->files
[i
]->dirname
[0],
1324 &flist
->files
[i
]->dirname
[1],
1325 strlen(flist
->files
[i
]->dirname
));
1328 if (flist
->files
[i
]->dirname
&&
1329 !flist
->files
[i
]->dirname
[0]) {
1330 flist
->files
[i
]->dirname
= NULL
;
1338 for (i
= 0; i
< flist
->count
; i
++) {
1339 rprintf(FINFO
, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1341 NS(flist
->files
[i
]->dirname
),
1342 NS(flist
->files
[i
]->basename
),
1343 (int) flist
->files
[i
]->mode
,
1344 (double) flist
->files
[i
]->length
);
1350 * return the full filename of a flist entry
1352 * This function is too expensive at the moment, because it copies
1353 * strings when often we only want to compare them. In any case,
1354 * using strlcat is silly because it will walk the string repeatedly.
1356 char *f_name(struct file_struct
*f
)
1358 static char names
[10][MAXPATHLEN
];
1362 if (!f
|| !f
->basename
)
1370 off
= strlcpy(p
, f
->dirname
, MAXPATHLEN
);
1371 off
+= strlcpy(p
+ off
, "/", MAXPATHLEN
- off
);
1372 off
+= strlcpy(p
+ off
, f
->basename
, MAXPATHLEN
- off
);
1374 strlcpy(p
, f
->basename
, MAXPATHLEN
);