2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Generate and receive file lists
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
30 extern struct stats stats;
33 extern int do_progress;
37 extern int always_checksum;
39 extern int ignore_errors;
40 extern int numeric_ids;
42 extern int cvs_exclude;
45 extern char curr_dir[MAXPATHLEN];
46 extern char *files_from;
47 extern int filesfrom_fd;
49 extern int one_file_system;
50 extern int preserve_links;
51 extern int preserve_hard_links;
52 extern int preserve_perms;
53 extern int preserve_devices;
54 extern int preserve_uid;
55 extern int preserve_gid;
56 extern int preserve_times;
57 extern int relative_paths;
58 extern int implied_dirs;
59 extern int copy_links;
60 extern int copy_unsafe_links;
61 extern int protocol_version;
62 extern int sanitize_paths;
64 extern int read_batch;
65 extern int write_batch;
67 extern struct exclude_list_struct exclude_list;
68 extern struct exclude_list_struct server_exclude_list;
69 extern struct exclude_list_struct local_exclude_list;
73 static char empty_sum[MD4_SUM_LENGTH];
74 static unsigned int file_struct_len;
76 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
77 static void output_flist(struct file_list *flist);
83 /* Figure out how big the file_struct is without trailing padding */
84 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
88 static int show_filelist_p(void)
90 return verbose && (recurse || files_from) && !am_server;
93 static void start_filelist_progress(char *kind)
95 rprintf(FINFO, "%s ... ", kind);
96 if ((verbose > 1) || do_progress)
102 static void emit_filelist_progress(const struct file_list *flist)
104 rprintf(FINFO, " %d files...\r", flist->count);
108 static void maybe_emit_filelist_progress(const struct file_list *flist)
110 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
111 emit_filelist_progress(flist);
115 static void finish_filelist_progress(const struct file_list *flist)
118 /* This overwrites the progress line */
119 rprintf(FINFO, "%d file%sto consider\n",
120 flist->count, flist->count == 1 ? " " : "s ");
122 rprintf(FINFO, "done\n");
125 void show_flist_stats(void)
131 static void list_file_entry(struct file_struct *f)
136 /* this can happen if duplicate names were removed */
139 permstring(perms, f->mode);
142 if (preserve_links && S_ISLNK(f->mode)) {
143 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
145 (double) f->length, timestring(f->modtime),
146 f_name(f), f->u.link);
149 rprintf(FINFO, "%s %11.0f %s %s\n",
151 (double) f->length, timestring(f->modtime),
157 * Stat either a symlink or its referent, depending on the settings of
158 * copy_links, copy_unsafe_links, etc.
160 * @retval -1 on error
162 * @retval 0 for success
164 * @post If @p path is a symlink, then @p linkbuf (of size @c
165 * MAXPATHLEN) contains the symlink target.
167 * @post @p buffer contains information about the link or the
168 * referrent as appropriate, if they exist.
170 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
174 return do_stat(path, buffer);
175 if (do_lstat(path, buffer) == -1)
177 if (S_ISLNK(buffer->st_mode)) {
178 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
182 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
184 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
187 return do_stat(path, buffer);
192 return do_stat(path, buffer);
196 int link_stat(const char *path, STRUCT_STAT * buffer)
200 return do_stat(path, buffer);
201 return do_lstat(path, buffer);
203 return do_stat(path, buffer);
208 * This function is used to check if a file should be included/excluded
209 * from the list of files based on its name and type etc. The value of
210 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
212 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
214 #if 0 /* This currently never happens, so avoid a useless compare. */
215 if (exclude_level == NO_EXCLUDES)
219 /* never exclude '.', even if somebody does --exclude '*' */
220 if (fname[0] == '.' && !fname[1])
222 /* Handle the -R version of the '.' dir. */
223 if (fname[0] == '/') {
224 int len = strlen(fname);
225 if (fname[len-1] == '.' && fname[len-2] == '/')
229 if (server_exclude_list.head
230 && check_exclude(&server_exclude_list, fname, is_dir,
233 if (exclude_level != ALL_EXCLUDES)
235 if (exclude_list.head
236 && check_exclude(&exclude_list, fname, is_dir, "pattern"))
238 if (local_exclude_list.head
239 && check_exclude(&local_exclude_list, fname, is_dir,
245 /* used by the one_file_system code */
246 static dev_t filesystem_dev;
248 static void set_filesystem(char *fname)
251 if (link_stat(fname, &st) != 0)
253 filesystem_dev = st.st_dev;
257 static int to_wire_mode(mode_t mode)
260 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
261 return (mode & ~(_S_IFMT)) | 0120000;
266 static mode_t from_wire_mode(int mode)
268 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
269 return (mode & ~(_S_IFMT)) | _S_IFLNK;
270 return (mode_t) mode;
274 static void send_directory(int f, struct file_list *flist, char *dir);
276 static char *flist_dir;
277 static int flist_dir_len;
281 * Make sure @p flist is big enough to hold at least @p flist->count
284 void flist_expand(struct file_list *flist)
288 if (flist->count < flist->malloced)
291 if (flist->malloced < FLIST_START)
292 flist->malloced = FLIST_START;
293 else if (flist->malloced >= FLIST_LINEAR)
294 flist->malloced += FLIST_LINEAR;
296 flist->malloced *= 2;
299 * In case count jumped or we are starting the list
300 * with a known size just set it.
302 if (flist->malloced < flist->count)
303 flist->malloced = flist->count;
306 new_ptr = realloc_array(flist->files,
307 struct file_struct *, flist->malloced);
309 new_ptr = new_array(struct file_struct *, flist->malloced);
313 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
315 (double) sizeof flist->files[0] * flist->malloced,
316 (new_ptr == flist->files) ? " not" : "");
319 flist->files = (struct file_struct **) new_ptr;
322 out_of_memory("flist_expand");
325 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
327 unsigned short flags;
328 static time_t modtime;
330 static dev_t dev, rdev;
331 static uint32 rdev_major;
334 static char lastname[MAXPATHLEN];
335 char *fname, fbuf[MAXPATHLEN];
343 modtime = 0, mode = 0;
344 dev = rdev = makedev(0, 0);
351 io_write_phase = "send_file_entry";
353 fname = f_name_to(file, fbuf);
357 if (file->mode == mode)
358 flags |= XMIT_SAME_MODE;
361 if (preserve_devices) {
362 if (protocol_version < 28) {
363 if (IS_DEVICE(mode)) {
364 if (file->u.rdev == rdev)
365 flags |= XMIT_SAME_RDEV_pre28;
369 rdev = makedev(0, 0);
370 } else if (IS_DEVICE(mode)) {
372 if ((uint32)major(rdev) == rdev_major)
373 flags |= XMIT_SAME_RDEV_MAJOR;
375 rdev_major = major(rdev);
376 if ((uint32)minor(rdev) <= 0xFFu)
377 flags |= XMIT_RDEV_MINOR_IS_SMALL;
380 if (file->uid == uid)
381 flags |= XMIT_SAME_UID;
384 if (file->gid == gid)
385 flags |= XMIT_SAME_GID;
388 if (file->modtime == modtime)
389 flags |= XMIT_SAME_TIME;
391 modtime = file->modtime;
393 #if SUPPORT_HARD_LINKS
394 if (file->link_u.idev) {
395 if (file->F_DEV == dev) {
396 if (protocol_version >= 28)
397 flags |= XMIT_SAME_DEV;
400 flags |= XMIT_HAS_IDEV_DATA;
405 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
407 l2 = strlen(fname+l1);
410 flags |= XMIT_SAME_NAME;
412 flags |= XMIT_LONG_NAME;
414 /* We must make sure we don't send a zero flag byte or the
415 * other end will terminate the flist transfer. Note that
416 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
417 * it's harmless way to add a bit to the first flag byte. */
418 if (protocol_version >= 28) {
419 if (!flags && !S_ISDIR(mode))
420 flags |= XMIT_TOP_DIR;
421 if ((flags & 0xFF00) || !flags) {
422 flags |= XMIT_EXTENDED_FLAGS;
423 write_byte(f, flags);
424 write_byte(f, flags >> 8);
426 write_byte(f, flags);
428 if (!(flags & 0xFF) && !S_ISDIR(mode))
429 flags |= XMIT_TOP_DIR;
431 flags |= XMIT_LONG_NAME;
432 write_byte(f, flags);
434 if (flags & XMIT_SAME_NAME)
436 if (flags & XMIT_LONG_NAME)
440 write_buf(f, fname + l1, l2);
442 write_longint(f, file->length);
443 if (!(flags & XMIT_SAME_TIME))
444 write_int(f, modtime);
445 if (!(flags & XMIT_SAME_MODE))
446 write_int(f, to_wire_mode(mode));
447 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
452 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
457 if (preserve_devices && IS_DEVICE(mode)) {
458 if (protocol_version < 28) {
459 if (!(flags & XMIT_SAME_RDEV_pre28))
460 write_int(f, (int)rdev);
462 if (!(flags & XMIT_SAME_RDEV_MAJOR))
463 write_int(f, major(rdev));
464 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
465 write_byte(f, minor(rdev));
467 write_int(f, minor(rdev));
472 if (preserve_links && S_ISLNK(mode)) {
473 int len = strlen(file->u.link);
475 write_buf(f, file->u.link, len);
479 #if SUPPORT_HARD_LINKS
480 if (flags & XMIT_HAS_IDEV_DATA) {
481 if (protocol_version >= 28) {
482 /* major/minor dev_t and 64-bit ino_t */
483 if (!(flags & XMIT_SAME_DEV)) {
484 write_int(f, major(dev));
485 write_int(f, minor(dev));
487 write_longint(f, file->F_INODE);
488 } else if (protocol_version < 26) {
489 /* 32-bit dev_t and ino_t */
491 write_int(f, file->F_INODE);
493 /* 64-bit dev_t and ino_t */
494 write_longint(f, dev);
495 write_longint(f, file->F_INODE);
500 if (always_checksum) {
504 else if (protocol_version < 28) {
505 /* Prior to 28, we sent a useless set of nulls. */
511 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
515 strlcpy(lastname, fname, MAXPATHLEN);
517 io_write_phase = "unknown";
522 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
523 struct file_list *flist, int f)
525 static time_t modtime;
527 static dev_t dev, rdev;
528 static uint32 rdev_major;
531 static char lastname[MAXPATHLEN], *lastdir;
532 static int lastdir_len = -1;
533 char thisname[MAXPATHLEN];
534 unsigned int l1 = 0, l2 = 0;
535 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
537 char *basename, *dirname, *bp;
538 struct file_struct *file;
541 modtime = 0, mode = 0;
542 dev = rdev = makedev(0, 0);
549 if (flags & XMIT_SAME_NAME)
552 if (flags & XMIT_LONG_NAME)
557 if (l2 >= MAXPATHLEN - l1) {
559 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
560 flags, l1, l2, lastname);
561 overflow("receive_file_entry");
564 strlcpy(thisname, lastname, l1 + 1);
565 read_sbuf(f, &thisname[l1], l2);
566 thisname[l1 + l2] = 0;
568 strlcpy(lastname, thisname, MAXPATHLEN);
570 clean_fname(thisname);
573 sanitize_path(thisname, NULL);
575 if ((basename = strrchr(thisname, '/')) != NULL) {
576 dirname_len = ++basename - thisname; /* counts future '\0' */
577 if (lastdir_len == dirname_len - 1
578 && strncmp(thisname, lastdir, lastdir_len) == 0) {
580 dirname_len = 0; /* indicates no copy is needed */
588 basename_len = strlen(basename) + 1; /* count the '\0' */
590 file_length = read_longint(f);
591 if (!(flags & XMIT_SAME_TIME))
592 modtime = (time_t)read_int(f);
593 if (!(flags & XMIT_SAME_MODE))
594 mode = from_wire_mode(read_int(f));
596 if (preserve_uid && !(flags & XMIT_SAME_UID))
597 uid = (uid_t)read_int(f);
598 if (preserve_gid && !(flags & XMIT_SAME_GID))
599 gid = (gid_t)read_int(f);
601 if (preserve_devices) {
602 if (protocol_version < 28) {
603 if (IS_DEVICE(mode)) {
604 if (!(flags & XMIT_SAME_RDEV_pre28))
605 rdev = (dev_t)read_int(f);
607 rdev = makedev(0, 0);
608 } else if (IS_DEVICE(mode)) {
610 if (!(flags & XMIT_SAME_RDEV_MAJOR))
611 rdev_major = read_int(f);
612 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
613 rdev_minor = read_byte(f);
615 rdev_minor = read_int(f);
616 rdev = makedev(rdev_major, rdev_minor);
621 if (preserve_links && S_ISLNK(mode)) {
622 linkname_len = read_int(f) + 1; /* count the '\0' */
623 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
624 rprintf(FERROR, "overflow: linkname_len=%d\n",
626 overflow("receive_file_entry");
633 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
635 alloc_len = file_struct_len + dirname_len + basename_len
636 + linkname_len + sum_len;
637 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
639 file = *fptr = (struct file_struct *)bp;
640 memset(bp, 0, file_struct_len);
641 bp += file_struct_len;
643 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
644 file->modtime = modtime;
645 file->length = file_length;
651 file->dirname = lastdir = bp;
652 lastdir_len = dirname_len - 1;
653 memcpy(bp, dirname, dirname_len - 1);
657 file->dirname = dirname;
660 memcpy(bp, basename, basename_len);
663 if (preserve_devices && IS_DEVICE(mode))
669 read_sbuf(f, bp, linkname_len - 1);
671 sanitize_path(bp, lastdir);
676 #if SUPPORT_HARD_LINKS
677 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
678 flags |= XMIT_HAS_IDEV_DATA;
679 if (flags & XMIT_HAS_IDEV_DATA) {
681 if (protocol_version >= 28) {
682 if (!(flags & XMIT_SAME_DEV)) {
683 uint32 dev_major = read_int(f);
684 uint32 dev_minor = read_int(f);
685 dev = makedev(dev_major, dev_minor);
687 inode = read_longint(f);
688 } else if (protocol_version < 26) {
689 dev = (dev_t)read_int(f);
692 dev = (dev_t)read_longint(f);
693 inode = read_longint(f);
695 if (flist->hlink_pool) {
696 file->link_u.idev = pool_talloc(flist->hlink_pool,
697 struct idev, 1, "inode_table");
698 file->F_INODE = inode;
704 if (always_checksum) {
707 file->u.sum = sum = bp;
709 } else if (protocol_version < 28) {
710 /* Prior to 28, we get a useless set of nulls. */
716 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
720 if (!preserve_perms) {
721 extern int orig_umask;
722 /* set an appropriate set of permissions based on original
723 * permissions and umask. This emulates what GNU cp does */
724 file->mode &= ~orig_umask;
730 * Create a file_struct for a named file by reading its stat()
731 * information and performing extensive checks against global
734 * @return the new file, or NULL if there was an error or this file
735 * should be excluded.
737 * @todo There is a small optimization opportunity here to avoid
738 * stat()ing the file in some circumstances, which has a certain cost.
739 * We are called immediately after doing readdir(), and so we may
740 * already know the d_type of the file. We could for example avoid
741 * statting directories if we're not recursing, but this is not a very
742 * important case. Some systems may not have d_type.
744 struct file_struct *make_file(char *fname,
745 struct file_list *flist, int exclude_level)
747 static char *lastdir;
748 static int lastdir_len = -1;
749 struct file_struct *file;
751 char sum[SUM_LENGTH];
752 char thisname[MAXPATHLEN];
753 char linkname[MAXPATHLEN];
754 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
755 char *basename, *dirname, *bp;
756 unsigned short flags = 0;
758 if (!flist) /* lastdir isn't valid if flist is NULL */
761 if (strlcpy(thisname, fname, sizeof thisname)
762 >= sizeof thisname - flist_dir_len) {
763 rprintf(FINFO, "skipping overly long name: %s\n", fname);
766 clean_fname(thisname);
768 sanitize_path(thisname, NULL);
770 memset(sum, 0, SUM_LENGTH);
772 if (readlink_stat(thisname, &st, linkname) != 0) {
773 int save_errno = errno;
774 if (errno == ENOENT) {
775 enum logcode c = am_daemon && protocol_version < 28
777 /* either symlink pointing nowhere or file that
778 * was removed during rsync run; see if excluded
779 * before reporting an error */
780 if (exclude_level != NO_EXCLUDES
781 && check_exclude_file(thisname, 0, exclude_level)) {
782 /* file is excluded anyway, ignore silently */
785 io_error |= IOERR_VANISHED;
786 rprintf(c, "file has vanished: %s\n",
787 full_fname(thisname));
790 io_error |= IOERR_GENERAL;
791 rprintf(FERROR, "readlink %s failed: %s\n",
792 full_fname(thisname), strerror(save_errno));
797 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
798 if (exclude_level == NO_EXCLUDES)
801 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
802 rprintf(FINFO, "skipping directory %s\n", thisname);
806 /* We only care about directories because we need to avoid recursing
807 * into a mount-point directory, not to avoid copying a symlinked
808 * file if -L (or similar) was specified. */
809 if (one_file_system && st.st_dev != filesystem_dev
810 && S_ISDIR(st.st_mode))
811 flags |= FLAG_MOUNT_POINT;
813 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
816 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
822 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
823 who_am_i(), thisname, exclude_level);
826 if ((basename = strrchr(thisname, '/')) != NULL) {
827 dirname_len = ++basename - thisname; /* counts future '\0' */
828 if (lastdir_len == dirname_len - 1
829 && strncmp(thisname, lastdir, lastdir_len) == 0) {
831 dirname_len = 0; /* indicates no copy is needed */
839 basename_len = strlen(basename) + 1; /* count the '\0' */
842 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
847 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
849 alloc_len = file_struct_len + dirname_len + basename_len
850 + linkname_len + sum_len;
852 bp = pool_alloc(flist->file_pool, alloc_len,
853 "receive_file_entry");
855 if (!(bp = new_array(char, alloc_len)))
856 out_of_memory("receive_file_entry");
859 file = (struct file_struct *)bp;
860 memset(bp, 0, file_struct_len);
861 bp += file_struct_len;
864 file->modtime = st.st_mtime;
865 file->length = st.st_size;
866 file->mode = st.st_mode;
867 file->uid = st.st_uid;
868 file->gid = st.st_gid;
870 #if SUPPORT_HARD_LINKS
871 if (flist && flist->hlink_pool) {
872 if (protocol_version < 28) {
873 if (S_ISREG(st.st_mode))
874 file->link_u.idev = pool_talloc(
875 flist->hlink_pool, struct idev, 1,
878 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
879 file->link_u.idev = pool_talloc(
880 flist->hlink_pool, struct idev, 1,
884 if (file->link_u.idev) {
885 file->F_DEV = st.st_dev;
886 file->F_INODE = st.st_ino;
891 file->dirname = lastdir = bp;
892 lastdir_len = dirname_len - 1;
893 memcpy(bp, dirname, dirname_len - 1);
897 file->dirname = dirname;
900 memcpy(bp, basename, basename_len);
903 #ifdef HAVE_STRUCT_STAT_ST_RDEV
904 if (preserve_devices && IS_DEVICE(st.st_mode))
905 file->u.rdev = st.st_rdev;
911 memcpy(bp, linkname, linkname_len);
918 file_checksum(thisname, bp, st.st_size);
922 file->basedir = flist_dir;
924 if (!S_ISDIR(st.st_mode))
925 stats.total_size += st.st_size;
931 void send_file_name(int f, struct file_list *flist, char *fname,
932 int recursive, unsigned short base_flags)
934 struct file_struct *file;
935 char fbuf[MAXPATHLEN];
936 extern int delete_excluded;
938 /* f is set to -1 when calculating deletion file list */
939 file = make_file(fname, flist,
940 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
945 maybe_emit_filelist_progress(flist);
950 file->flags |= FLAG_TOP_DIR;
952 if (file->basename[0]) {
953 flist->files[flist->count++] = file;
954 send_file_entry(file, f, base_flags);
957 if (recursive && S_ISDIR(file->mode)
958 && !(file->flags & FLAG_MOUNT_POINT)) {
959 struct exclude_list_struct last_list = local_exclude_list;
960 memset(&local_exclude_list, 0, sizeof local_exclude_list);
961 send_directory(f, flist, f_name_to(file, fbuf));
962 free_exclude_list(&local_exclude_list);
963 local_exclude_list = last_list;
968 static void send_directory(int f, struct file_list *flist, char *dir)
972 char fname[MAXPATHLEN];
978 io_error |= IOERR_GENERAL;
979 rprintf(FERROR, "opendir %s failed: %s\n",
980 full_fname(dir), strerror(errno));
984 offset = strlcpy(fname, dir, MAXPATHLEN);
986 if (offset >= MAXPATHLEN || p[-1] != '/') {
987 if (offset >= MAXPATHLEN - 1) {
988 io_error |= IOERR_GENERAL;
989 rprintf(FERROR, "skipping long-named directory: %s\n",
999 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
1000 < MAXPATHLEN - offset) {
1001 add_exclude_file(&local_exclude_list, fname,
1002 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
1004 io_error |= IOERR_GENERAL;
1006 "cannot cvs-exclude in long-named directory %s\n",
1011 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1012 char *dname = d_name(di);
1013 if (dname[0] == '.' && (dname[1] == '\0'
1014 || (dname[1] == '.' && dname[2] == '\0')))
1016 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1017 send_file_name(f, flist, fname, recurse, 0);
1019 io_error |= IOERR_GENERAL;
1021 "cannot send long-named file %s\n",
1026 io_error |= IOERR_GENERAL;
1027 rprintf(FERROR, "readdir(%s): (%d) %s\n",
1028 dir, errno, strerror(errno));
1036 * The delete_files() function in receiver.c sets f to -1 so that we just
1037 * construct the file list in memory without sending it over the wire. It
1038 * also has the side-effect of ignoring user-excludes if delete_excluded
1039 * is set (so that the delete list includes user-excluded files).
1041 struct file_list *send_file_list(int f, int argc, char *argv[])
1045 char *p, *dir, olddir[sizeof curr_dir];
1046 char lastpath[MAXPATHLEN] = "";
1047 struct file_list *flist;
1051 if (show_filelist_p() && f != -1)
1052 start_filelist_progress("building file list");
1054 start_write = stats.total_written;
1056 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1060 io_start_buffering_out(f);
1061 if (filesfrom_fd >= 0) {
1062 if (argv[0] && !push_dir(argv[0])) {
1063 rprintf(FERROR, "push_dir %s failed: %s\n",
1064 full_fname(argv[0]), strerror(errno));
1065 exit_cleanup(RERR_FILESELECT);
1072 char fname2[MAXPATHLEN];
1073 char *fname = fname2;
1076 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1078 sanitize_path(fname, NULL);
1082 strlcpy(fname, *argv++, MAXPATHLEN);
1084 sanitize_path(fname, NULL);
1088 if (fname[l - 1] == '/') {
1089 if (l == 2 && fname[0] == '.') {
1090 /* Turn "./" into just "." rather than "./." */
1092 } else if (l < MAXPATHLEN) {
1098 if (link_stat(fname, &st) != 0) {
1100 io_error |= IOERR_GENERAL;
1101 rprintf(FERROR, "link_stat %s failed: %s\n",
1102 full_fname(fname), strerror(errno));
1107 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1108 rprintf(FINFO, "skipping directory %s\n", fname);
1115 if (!relative_paths) {
1116 p = strrchr(fname, '/');
1125 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1126 /* this ensures we send the intermediate directories,
1127 thus getting their permissions right */
1128 char *lp = lastpath, *fn = fname, *slash = fname;
1130 /* Skip any initial directories in our path that we
1131 * have in common with lastpath. */
1132 while (*fn && *lp == *fn) {
1138 if (fn != p || (*lp && *lp != '/')) {
1139 int copy_links_saved = copy_links;
1140 int recurse_saved = recurse;
1141 copy_links = copy_unsafe_links;
1142 /* set recurse to 1 to prevent make_file
1143 * from ignoring directory, but still
1144 * turn off the recursive parameter to
1147 while ((slash = strchr(slash+1, '/')) != 0) {
1149 send_file_name(f, flist, fname, 0, 0);
1152 copy_links = copy_links_saved;
1153 recurse = recurse_saved;
1155 strlcpy(lastpath, fname, sizeof lastpath);
1164 static char *lastdir;
1165 static int lastdir_len;
1167 strcpy(olddir, curr_dir); /* can't overflow */
1169 if (!push_dir(dir)) {
1170 io_error |= IOERR_GENERAL;
1171 rprintf(FERROR, "push_dir %s failed: %s\n",
1172 full_fname(dir), strerror(errno));
1176 if (lastdir && strcmp(lastdir, dir) == 0) {
1177 flist_dir = lastdir;
1178 flist_dir_len = lastdir_len;
1180 flist_dir = lastdir = strdup(dir);
1181 flist_dir_len = lastdir_len = strlen(dir);
1185 if (one_file_system)
1186 set_filesystem(fname);
1188 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1193 if (!pop_dir(olddir)) {
1194 rprintf(FERROR, "pop_dir %s failed: %s\n",
1195 full_fname(dir), strerror(errno));
1196 exit_cleanup(RERR_FILESELECT);
1202 send_file_entry(NULL, f, 0);
1204 if (show_filelist_p())
1205 finish_filelist_progress(flist);
1208 if (flist->hlink_pool) {
1209 pool_destroy(flist->hlink_pool);
1210 flist->hlink_pool = NULL;
1213 clean_flist(flist, 0, 0);
1216 /* Now send the uid/gid list. This was introduced in
1217 * protocol version 15 */
1220 /* send the io_error flag */
1221 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1224 stats.flist_size = stats.total_written - start_write;
1225 stats.num_files = flist->count;
1227 write_batch_flist_info(flist->count, flist->files);
1231 output_flist(flist);
1234 rprintf(FINFO, "send_file_list done\n");
1240 struct file_list *recv_file_list(int f)
1242 struct file_list *flist;
1243 unsigned short flags;
1245 extern int list_only;
1247 if (show_filelist_p())
1248 start_filelist_progress("receiving file list");
1250 start_read = stats.total_read;
1252 flist = flist_new(WITH_HLINK, "recv_file_list");
1255 flist->malloced = 1000;
1256 flist->files = new_array(struct file_struct *, flist->malloced);
1261 while ((flags = read_byte(f)) != 0) {
1262 int i = flist->count;
1264 flist_expand(flist);
1266 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1267 flags |= read_byte(f) << 8;
1268 receive_file_entry(&flist->files[i], flags, flist, f);
1270 if (S_ISREG(flist->files[i]->mode))
1271 stats.total_size += flist->files[i]->length;
1275 maybe_emit_filelist_progress(flist);
1278 rprintf(FINFO, "recv_file_name(%s)\n",
1279 f_name(flist->files[i]));
1282 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1285 rprintf(FINFO, "received %d names\n", flist->count);
1287 if (show_filelist_p())
1288 finish_filelist_progress(flist);
1290 clean_flist(flist, relative_paths, 1);
1293 /* Now send the uid/gid list. This was introduced in
1294 * protocol version 15 */
1295 recv_uid_list(f, flist);
1298 /* Recv the io_error flag */
1299 if (lp_ignore_errors(module_id) || ignore_errors)
1302 io_error |= read_int(f);
1307 output_flist(flist);
1311 for (i = 0; i < flist->count; i++)
1312 list_file_entry(flist->files[i]);
1316 rprintf(FINFO, "recv_file_list done\n");
1318 stats.flist_size = stats.total_read - start_read;
1319 stats.num_files = flist->count;
1324 out_of_memory("recv_file_list");
1325 return NULL; /* not reached */
1329 int file_compare(struct file_struct **file1, struct file_struct **file2)
1331 struct file_struct *f1 = *file1;
1332 struct file_struct *f2 = *file2;
1334 if (!f1->basename && !f2->basename)
1340 if (f1->dirname == f2->dirname)
1341 return u_strcmp(f1->basename, f2->basename);
1342 return f_name_cmp(f1, f2);
1346 int flist_find(struct file_list *flist, struct file_struct *f)
1348 int low = 0, high = flist->count - 1;
1350 while (high >= 0 && !flist->files[high]->basename) high--;
1355 while (low != high) {
1356 int mid = (low + high) / 2;
1357 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1359 return flist_up(flist, mid);
1366 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1367 return flist_up(flist, low);
1372 * Free up any resources a file_struct has allocated
1373 * and clear the file.
1375 void clear_file(int i, struct file_list *flist)
1377 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1378 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1379 memset(flist->files[i], 0, file_struct_len);
1384 * allocate a new file list
1386 struct file_list *flist_new(int with_hlink, char *msg)
1388 struct file_list *flist;
1390 flist = new(struct file_list);
1394 memset(flist, 0, sizeof (struct file_list));
1396 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1397 out_of_memory, POOL_INTERN)))
1400 #if SUPPORT_HARD_LINKS
1401 if (with_hlink && preserve_hard_links) {
1402 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1403 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1412 * free up all elements in a flist
1414 void flist_free(struct file_list *flist)
1416 pool_destroy(flist->file_pool);
1417 pool_destroy(flist->hlink_pool);
1424 * This routine ensures we don't have any duplicate names in our file list.
1425 * duplicate names can cause corruption because of the pipelining
1427 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1431 if (!flist || flist->count == 0)
1434 qsort(flist->files, flist->count,
1435 sizeof flist->files[0], (int (*)()) file_compare);
1437 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1438 if (flist->files[i]->basename) {
1443 while (++i < flist->count) {
1444 if (!flist->files[i]->basename)
1446 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1447 if (verbose > 1 && !am_server) {
1449 "removing duplicate name %s from file list %d\n",
1450 f_name(flist->files[i]), i);
1452 /* Make sure that if we unduplicate '.', that we don't
1453 * lose track of a user-specified starting point (or
1454 * else deletions will mysteriously fail with -R). */
1455 if (flist->files[i]->flags & FLAG_TOP_DIR)
1456 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1458 clear_file(i, flist);
1464 /* we need to strip off the root directory in the case
1465 of relative paths, but this must be done _after_
1466 the sorting phase */
1467 for (i = 0; i < flist->count; i++) {
1468 if (flist->files[i]->dirname &&
1469 flist->files[i]->dirname[0] == '/') {
1470 memmove(&flist->files[i]->dirname[0],
1471 &flist->files[i]->dirname[1],
1472 strlen(flist->files[i]->dirname));
1475 if (flist->files[i]->dirname &&
1476 !flist->files[i]->dirname[0]) {
1477 flist->files[i]->dirname = NULL;
1483 static void output_flist(struct file_list *flist)
1485 char uidbuf[16], gidbuf[16];
1486 struct file_struct *file;
1489 for (i = 0; i < flist->count; i++) {
1490 file = flist->files[i];
1491 if (am_root && preserve_uid)
1492 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1495 if (preserve_gid && file->gid != GID_NONE)
1496 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1499 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1500 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1501 NS(file->basename), (int) file->mode,
1502 (double) file->length, uidbuf, gidbuf);
1507 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1509 /* Compare the names of two file_struct entities, just like strcmp()
1510 * would do if it were operating on the joined strings. We assume
1511 * that there are no 0-length strings.
1513 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1516 const uchar *c1, *c2;
1517 enum fnc_state state1, state2;
1519 if (!f1 || !f1->basename) {
1520 if (!f2 || !f2->basename)
1524 if (!f2 || !f2->basename)
1527 if (!(c1 = (uchar*)f1->dirname)) {
1529 c1 = (uchar*)f1->basename;
1532 if (!(c2 = (uchar*)f2->dirname)) {
1534 c2 = (uchar*)f2->basename;
1539 if ((dif = (int)*c1 - (int)*c2) != 0)
1549 c1 = (uchar*)f1->basename;
1563 c2 = (uchar*)f2->basename;
1577 /* Return a copy of the full filename of a flist entry, using the indicated
1578 * buffer. No size-checking is done because we checked the size when creating
1579 * the file_struct entry.
1581 char *f_name_to(struct file_struct *f, char *fbuf)
1583 if (!f || !f->basename)
1587 int len = strlen(f->dirname);
1588 memcpy(fbuf, f->dirname, len);
1590 strcpy(fbuf + len + 1, f->basename);
1592 strcpy(fbuf, f->basename);
1597 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1599 char *f_name(struct file_struct *f)
1601 static char names[5][MAXPATHLEN];
1602 static unsigned int n;
1604 n = (n + 1) % (sizeof names / sizeof names[0]);
1606 return f_name_to(f, names[n]);