Changed the -x code to allow -L to copy a file on another filesystem
[rsync/rsync.git] / flist.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
5
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.
10
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.
15
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.
19*/
20
21/** @file flist.c
22 * Generate and receive file lists
23 *
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
25 *
26 **/
27
28#include "rsync.h"
29
30extern struct stats stats;
31
32extern int verbose;
33extern int do_progress;
34extern int am_server;
35extern int always_checksum;
36extern int module_id;
37extern int ignore_errors;
38
39extern int cvs_exclude;
40
41extern int recurse;
42extern char curr_dir[MAXPATHLEN];
43extern char *files_from;
44extern int filesfrom_fd;
45
46extern int one_file_system;
47extern int make_backups;
48extern int preserve_links;
49extern int preserve_hard_links;
50extern int preserve_perms;
51extern int preserve_devices;
52extern int preserve_uid;
53extern int preserve_gid;
54extern int preserve_times;
55extern int relative_paths;
56extern int implied_dirs;
57extern int copy_links;
58extern int copy_unsafe_links;
59extern int protocol_version;
60extern int sanitize_paths;
61
62extern int read_batch;
63extern int write_batch;
64
65extern struct exclude_struct **exclude_list;
66extern struct exclude_struct **server_exclude_list;
67extern struct exclude_struct **local_exclude_list;
68
69int io_error;
70
71static char empty_sum[MD4_SUM_LENGTH];
72static unsigned int min_file_struct_len;
73
74static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
75
76
77void init_flist(void)
78{
79 struct file_struct f;
80
81 /* Figure out how big the file_struct is without trailing padding */
82 min_file_struct_len = ((char*)&f.flags - (char*)&f) + sizeof f.flags;
83}
84
85
86static int show_filelist_p(void)
87{
88 return verbose && (recurse || files_from) && !am_server;
89}
90
91static void start_filelist_progress(char *kind)
92{
93 rprintf(FINFO, "%s ... ", kind);
94 if ((verbose > 1) || do_progress)
95 rprintf(FINFO, "\n");
96 rflush(FINFO);
97}
98
99
100static void emit_filelist_progress(const struct file_list *flist)
101{
102 rprintf(FINFO, " %d files...\r", flist->count);
103}
104
105
106static void maybe_emit_filelist_progress(const struct file_list *flist)
107{
108 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
109 emit_filelist_progress(flist);
110}
111
112
113static void finish_filelist_progress(const struct file_list *flist)
114{
115 if (do_progress) {
116 /* This overwrites the progress line */
117 rprintf(FINFO, "%d file%sto consider\n",
118 flist->count, flist->count == 1 ? " " : "s ");
119 } else
120 rprintf(FINFO, "done\n");
121}
122
123void show_flist_stats(void)
124{
125 /* Nothing yet */
126}
127
128
129static void list_file_entry(struct file_struct *f)
130{
131 char perms[11];
132
133 if (!f->basename)
134 /* this can happen if duplicate names were removed */
135 return;
136
137 permstring(perms, f->mode);
138
139#if SUPPORT_LINKS
140 if (preserve_links && S_ISLNK(f->mode)) {
141 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
142 perms,
143 (double) f->length, timestring(f->modtime),
144 f_name(f), f->u.link);
145 } else
146#endif
147 rprintf(FINFO, "%s %11.0f %s %s\n",
148 perms,
149 (double) f->length, timestring(f->modtime),
150 f_name(f));
151}
152
153
154/**
155 * Stat either a symlink or its referent, depending on the settings of
156 * copy_links, copy_unsafe_links, etc.
157 *
158 * @retval -1 on error
159 *
160 * @retval 0 for success
161 *
162 * @post If @p path is a symlink, then @p linkbuf (of size @c
163 * MAXPATHLEN) contains the symlink target.
164 *
165 * @post @p buffer contains information about the link or the
166 * referrent as appropriate, if they exist.
167 **/
168int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
169{
170#if SUPPORT_LINKS
171 if (copy_links)
172 return do_stat(path, buffer);
173 if (do_lstat(path, buffer) == -1)
174 return -1;
175 if (S_ISLNK(buffer->st_mode)) {
176 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
177 if (l == -1)
178 return -1;
179 linkbuf[l] = 0;
180 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
181 if (verbose > 1) {
182 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
183 path, linkbuf);
184 }
185 return do_stat(path, buffer);
186 }
187 }
188 return 0;
189#else
190 return do_stat(path, buffer);
191#endif
192}
193
194int link_stat(const char *path, STRUCT_STAT * buffer)
195{
196#if SUPPORT_LINKS
197 if (copy_links)
198 return do_stat(path, buffer);
199 return do_lstat(path, buffer);
200#else
201 return do_stat(path, buffer);
202#endif
203}
204
205/*
206 * This function is used to check if a file should be included/excluded
207 * from the list of files based on its name and type etc. The value of
208 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
209 */
210static int check_exclude_file(char *fname, int is_dir, int exclude_level)
211{
212#if 0 /* This currently never happens, so avoid a useless compare. */
213 if (exclude_level == NO_EXCLUDES)
214 return 0;
215#endif
216 if (fname) {
217 /* never exclude '.', even if somebody does --exclude '*' */
218 if (fname[0] == '.' && !fname[1])
219 return 0;
220 /* Handle the -R version of the '.' dir. */
221 if (fname[0] == '/') {
222 int len = strlen(fname);
223 if (fname[len-1] == '.' && fname[len-2] == '/')
224 return 0;
225 }
226 }
227 if (server_exclude_list
228 && check_exclude(server_exclude_list, fname, is_dir))
229 return 1;
230 if (exclude_level != ALL_EXCLUDES)
231 return 0;
232 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
233 return 1;
234 if (local_exclude_list
235 && check_exclude(local_exclude_list, fname, is_dir))
236 return 1;
237 return 0;
238}
239
240/* used by the one_file_system code */
241static dev_t filesystem_dev;
242
243static void set_filesystem(char *fname)
244{
245 STRUCT_STAT st;
246 if (link_stat(fname, &st) != 0)
247 return;
248 filesystem_dev = st.st_dev;
249}
250
251
252static int to_wire_mode(mode_t mode)
253{
254#if SUPPORT_LINKS
255 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
256 return (mode & ~(_S_IFMT)) | 0120000;
257#endif
258 return (int) mode;
259}
260
261static mode_t from_wire_mode(int mode)
262{
263 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
264 return (mode & ~(_S_IFMT)) | _S_IFLNK;
265 return (mode_t) mode;
266}
267
268
269static void send_directory(int f, struct file_list *flist, char *dir);
270
271static char *flist_dir;
272static int flist_dir_len;
273
274
275/**
276 * Make sure @p flist is big enough to hold at least @p flist->count
277 * entries.
278 **/
279static void flist_expand(struct file_list *flist)
280{
281 if (flist->count >= flist->malloced) {
282 void *new_ptr;
283
284 if (flist->malloced < 1000)
285 flist->malloced += 1000;
286 else
287 flist->malloced *= 2;
288
289 if (flist->files) {
290 new_ptr = realloc_array(flist->files,
291 struct file_struct *,
292 flist->malloced);
293 } else {
294 new_ptr = new_array(struct file_struct *,
295 flist->malloced);
296 }
297
298 if (verbose >= 2) {
299 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
300 who_am_i(),
301 (double) sizeof flist->files[0] * flist->malloced,
302 (new_ptr == flist->files) ? " not" : "");
303 }
304
305 flist->files = (struct file_struct **) new_ptr;
306
307 if (!flist->files)
308 out_of_memory("flist_expand");
309 }
310}
311
312void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
313{
314 unsigned short flags;
315 static time_t modtime;
316 static mode_t mode;
317 static DEV64_T rdev, rdev_high;
318 static DEV64_T dev;
319 static uid_t uid;
320 static gid_t gid;
321 static char lastname[MAXPATHLEN];
322 char *fname, fbuf[MAXPATHLEN];
323 int l1, l2;
324
325 if (f == -1)
326 return;
327
328 if (!file) {
329 write_byte(f, 0);
330 modtime = 0, mode = 0;
331 rdev = 0, rdev_high = 0, dev = 0;
332 uid = 0, gid = 0;
333 *lastname = '\0';
334 return;
335 }
336
337 io_write_phase = "send_file_entry";
338
339 fname = f_name_to(file, fbuf);
340
341 flags = base_flags;
342
343 if (file->mode == mode)
344 flags |= XMIT_SAME_MODE;
345 else
346 mode = file->mode;
347 if (preserve_devices) {
348 if (protocol_version < 28) {
349 if (IS_DEVICE(mode)) {
350 if (file->u.rdev == rdev) {
351 /* Set both flags to simplify the test
352 * when writing the data. */
353 flags |= XMIT_SAME_RDEV_pre28
354 | XMIT_SAME_HIGH_RDEV;
355 } else
356 rdev = file->u.rdev;
357 } else
358 rdev = 0;
359 } else if (IS_DEVICE(mode)) {
360 if ((file->u.rdev & ~0xFF) == rdev_high)
361 flags |= XMIT_SAME_HIGH_RDEV;
362 else {
363 rdev = file->u.rdev;
364 rdev_high = rdev & ~0xFF;
365 }
366 }
367 }
368 if (file->uid == uid)
369 flags |= XMIT_SAME_UID;
370 else
371 uid = file->uid;
372 if (file->gid == gid)
373 flags |= XMIT_SAME_GID;
374 else
375 gid = file->gid;
376 if (file->modtime == modtime)
377 flags |= XMIT_SAME_TIME;
378 else
379 modtime = file->modtime;
380
381#if SUPPORT_HARD_LINKS
382 if (file->link_u.idev) {
383 if (file->F_DEV == dev) {
384 if (protocol_version >= 28)
385 flags |= XMIT_SAME_DEV;
386 } else
387 dev = file->F_DEV;
388 flags |= XMIT_HAS_IDEV_DATA;
389 }
390#endif
391
392 for (l1 = 0;
393 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
394 l1++) {}
395 l2 = strlen(fname+l1);
396
397 if (l1 > 0)
398 flags |= XMIT_SAME_NAME;
399 if (l2 > 255)
400 flags |= XMIT_LONG_NAME;
401
402 /* We must make sure we don't send a zero flag byte or the
403 * other end will terminate the flist transfer. Note that
404 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
405 * it's harmless way to add a bit to the first flag byte. */
406 if (protocol_version >= 28) {
407 if (!flags && !S_ISDIR(mode))
408 flags |= XMIT_TOP_DIR;
409 if ((flags & 0xFF00) || !flags) {
410 flags |= XMIT_EXTENDED_FLAGS;
411 write_byte(f, flags);
412 write_byte(f, flags >> 8);
413 } else
414 write_byte(f, flags);
415 } else {
416 if (!(flags & 0xFF) && !S_ISDIR(mode))
417 flags |= XMIT_TOP_DIR;
418 if (!(flags & 0xFF))
419 flags |= XMIT_LONG_NAME;
420 write_byte(f, flags);
421 }
422 if (flags & XMIT_SAME_NAME)
423 write_byte(f, l1);
424 if (flags & XMIT_LONG_NAME)
425 write_int(f, l2);
426 else
427 write_byte(f, l2);
428 write_buf(f, fname + l1, l2);
429
430 write_longint(f, file->length);
431 if (!(flags & XMIT_SAME_TIME))
432 write_int(f, modtime);
433 if (!(flags & XMIT_SAME_MODE))
434 write_int(f, to_wire_mode(mode));
435 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
436 add_uid(uid);
437 write_int(f, uid);
438 }
439 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
440 add_gid(gid);
441 write_int(f, gid);
442 }
443 if (preserve_devices && IS_DEVICE(mode)) {
444 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
445 * also off. */
446 if (!(flags & XMIT_SAME_HIGH_RDEV))
447 write_int(f, rdev);
448 else if (protocol_version >= 28)
449 write_byte(f, rdev);
450 }
451
452#if SUPPORT_LINKS
453 if (preserve_links && S_ISLNK(mode)) {
454 int len = strlen(file->u.link);
455 write_int(f, len);
456 write_buf(f, file->u.link, len);
457 }
458#endif
459
460#if SUPPORT_HARD_LINKS
461 if (flags & XMIT_HAS_IDEV_DATA) {
462 if (protocol_version < 26) {
463 /* 32-bit dev_t and ino_t */
464 write_int(f, dev);
465 write_int(f, file->F_INODE);
466 } else {
467 /* 64-bit dev_t and ino_t */
468 if (!(flags & XMIT_SAME_DEV))
469 write_longint(f, dev);
470 write_longint(f, file->F_INODE);
471 }
472 }
473#endif
474
475 if (always_checksum) {
476 char *sum;
477 if (S_ISREG(mode))
478 sum = file->u.sum;
479 else if (protocol_version < 28) {
480 /* Prior to 28, we sent a useless set of nulls. */
481 sum = empty_sum;
482 } else
483 sum = NULL;
484 if (sum) {
485 write_buf(f, sum, protocol_version < 21? 2
486 : MD4_SUM_LENGTH);
487 }
488 }
489
490 strlcpy(lastname, fname, MAXPATHLEN);
491
492 io_write_phase = "unknown";
493}
494
495
496
497void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
498{
499 static time_t modtime;
500 static mode_t mode;
501 static DEV64_T rdev, rdev_high;
502 static DEV64_T dev;
503 static uid_t uid;
504 static gid_t gid;
505 static char lastname[MAXPATHLEN], *lastdir;
506 static int lastdir_len = -1;
507 char thisname[MAXPATHLEN];
508 unsigned int l1 = 0, l2 = 0;
509 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
510 int file_struct_len, idev_len;
511 OFF_T file_length;
512 char *basename, *dirname, *bp;
513 struct file_struct *file;
514
515 if (!fptr) {
516 modtime = 0, mode = 0;
517 rdev = 0, rdev_high = 0, dev = 0;
518 uid = 0, gid = 0;
519 *lastname = '\0';
520 return;
521 }
522
523 if (flags & XMIT_SAME_NAME)
524 l1 = read_byte(f);
525
526 if (flags & XMIT_LONG_NAME)
527 l2 = read_int(f);
528 else
529 l2 = read_byte(f);
530
531 if (l2 >= MAXPATHLEN - l1) {
532 rprintf(FERROR,
533 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
534 flags, l1, l2, lastname);
535 overflow("receive_file_entry");
536 }
537
538 strlcpy(thisname, lastname, l1 + 1);
539 read_sbuf(f, &thisname[l1], l2);
540 thisname[l1 + l2] = 0;
541
542 strlcpy(lastname, thisname, MAXPATHLEN);
543
544 clean_fname(thisname);
545
546 if (sanitize_paths)
547 sanitize_path(thisname, NULL);
548
549 if ((basename = strrchr(thisname, '/')) != NULL) {
550 dirname_len = ++basename - thisname; /* counts future '\0' */
551 if (lastdir_len == dirname_len - 1
552 && strncmp(thisname, lastdir, lastdir_len) == 0) {
553 dirname = lastdir;
554 dirname_len = 0; /* indicates no copy is needed */
555 } else
556 dirname = thisname;
557 } else {
558 basename = thisname;
559 dirname = NULL;
560 dirname_len = 0;
561 }
562 basename_len = strlen(basename) + 1; /* count the '\0' */
563
564 file_length = read_longint(f);
565 if (!(flags & XMIT_SAME_TIME))
566 modtime = (time_t)read_int(f);
567 if (!(flags & XMIT_SAME_MODE))
568 mode = from_wire_mode(read_int(f));
569
570 if (preserve_uid && !(flags & XMIT_SAME_UID))
571 uid = (uid_t)read_int(f);
572 if (preserve_gid && !(flags & XMIT_SAME_GID))
573 gid = (gid_t)read_int(f);
574
575 if (preserve_devices) {
576 if (protocol_version < 28) {
577 if (IS_DEVICE(mode)) {
578 if (!(flags & XMIT_SAME_RDEV_pre28))
579 rdev = (DEV64_T)read_int(f);
580 } else
581 rdev = 0;
582 } else if (IS_DEVICE(mode)) {
583 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
584 rdev = (DEV64_T)read_int(f);
585 rdev_high = rdev & ~0xFF;
586 } else
587 rdev = rdev_high | (DEV64_T)read_byte(f);
588 }
589 }
590
591#if SUPPORT_LINKS
592 if (preserve_links && S_ISLNK(mode)) {
593 linkname_len = read_int(f) + 1; /* count the '\0' */
594 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
595 rprintf(FERROR, "overflow: linkname_len=%d\n",
596 linkname_len - 1);
597 overflow("receive_file_entry");
598 }
599 }
600 else
601#endif
602 linkname_len = 0;
603
604#if SUPPORT_HARD_LINKS
605 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
606 flags |= XMIT_HAS_IDEV_DATA;
607 if (flags & XMIT_HAS_IDEV_DATA)
608 idev_len = sizeof (struct idev);
609 else
610#endif
611 idev_len = 0;
612
613 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
614 file_struct_len = idev_len? sizeof file[0] : min_file_struct_len;
615
616 alloc_len = file_struct_len + dirname_len + basename_len
617 + linkname_len + sum_len + idev_len;
618 if (!(bp = new_array(char, alloc_len)))
619 out_of_memory("receive_file_entry");
620 file = *fptr = (struct file_struct *)bp;
621 memset(bp, 0, min_file_struct_len);
622 bp += file_struct_len;
623
624 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
625 file->modtime = modtime;
626 file->length = file_length;
627 file->mode = mode;
628 file->uid = uid;
629 file->gid = gid;
630
631#if SUPPORT_HARD_LINKS
632 if (idev_len) {
633 file->link_u.idev = (struct idev *)bp;
634 bp += idev_len;
635 }
636#endif
637
638 if (dirname_len) {
639 file->dirname = lastdir = bp;
640 lastdir_len = dirname_len - 1;
641 memcpy(bp, dirname, dirname_len - 1);
642 bp += dirname_len;
643 bp[-1] = '\0';
644 } else if (dirname)
645 file->dirname = dirname;
646
647 file->basename = bp;
648 memcpy(bp, basename, basename_len);
649 bp += basename_len;
650
651 if (preserve_devices && IS_DEVICE(mode))
652 file->u.rdev = rdev;
653
654#if SUPPORT_LINKS
655 if (linkname_len) {
656 file->u.link = bp;
657 read_sbuf(f, bp, linkname_len - 1);
658 if (sanitize_paths)
659 sanitize_path(bp, lastdir);
660 bp += linkname_len;
661 }
662#endif
663
664#if SUPPORT_HARD_LINKS
665 if (idev_len) {
666 if (protocol_version < 26) {
667 dev = read_int(f);
668 file->F_INODE = read_int(f);
669 } else {
670 if (!(flags & XMIT_SAME_DEV))
671 dev = read_longint(f);
672 file->F_INODE = read_longint(f);
673 }
674 file->F_DEV = dev;
675 }
676#endif
677
678 if (always_checksum) {
679 char *sum;
680 if (sum_len) {
681 file->u.sum = sum = bp;
682 /*bp += sum_len;*/
683 } else if (protocol_version < 28) {
684 /* Prior to 28, we get a useless set of nulls. */
685 sum = empty_sum;
686 } else
687 sum = NULL;
688 if (sum) {
689 read_buf(f, sum, protocol_version < 21? 2
690 : MD4_SUM_LENGTH);
691 }
692 }
693
694 if (!preserve_perms) {
695 extern int orig_umask;
696 /* set an appropriate set of permissions based on original
697 * permissions and umask. This emulates what GNU cp does */
698 file->mode &= ~orig_umask;
699 }
700}
701
702
703/**
704 * Create a file_struct for a named file by reading its stat()
705 * information and performing extensive checks against global
706 * options.
707 *
708 * @return the new file, or NULL if there was an error or this file
709 * should be excluded.
710 *
711 * @todo There is a small optimization opportunity here to avoid
712 * stat()ing the file in some circumstances, which has a certain cost.
713 * We are called immediately after doing readdir(), and so we may
714 * already know the d_type of the file. We could for example avoid
715 * statting directories if we're not recursing, but this is not a very
716 * important case. Some systems may not have d_type.
717 **/
718struct file_struct *make_file(char *fname, int exclude_level)
719{
720 static char *lastdir;
721 static int lastdir_len = -1;
722 struct file_struct *file;
723 STRUCT_STAT st;
724 char sum[SUM_LENGTH];
725 char thisname[MAXPATHLEN];
726 char linkname[MAXPATHLEN];
727 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
728 int file_struct_len, idev_len;
729 char *basename, *dirname, *bp;
730 unsigned short flags = 0;
731
732 if (strlcpy(thisname, fname, sizeof thisname)
733 >= sizeof thisname - flist_dir_len) {
734 rprintf(FINFO, "skipping overly long name: %s\n", fname);
735 return NULL;
736 }
737 clean_fname(thisname);
738 if (sanitize_paths)
739 sanitize_path(thisname, NULL);
740
741 memset(sum, 0, SUM_LENGTH);
742
743 if (readlink_stat(thisname, &st, linkname) != 0) {
744 int save_errno = errno;
745 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
746 /* either symlink pointing nowhere or file that
747 * was removed during rsync run; see if excluded
748 * before reporting an error */
749 if (check_exclude_file(thisname, 0, exclude_level)) {
750 /* file is excluded anyway, ignore silently */
751 return NULL;
752 }
753 }
754 io_error |= IOERR_GENERAL;
755 rprintf(FERROR, "readlink %s failed: %s\n",
756 full_fname(thisname), strerror(save_errno));
757 return NULL;
758 }
759
760 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
761 if (exclude_level == NO_EXCLUDES)
762 goto skip_excludes;
763
764 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
765 rprintf(FINFO, "skipping directory %s\n", thisname);
766 return NULL;
767 }
768
769 /* We only care about directories because we need to avoid recursing
770 * into a mount-point directory, not to avoid copying a symlinked
771 * file if -L (or similar) was specified. */
772 if (one_file_system && st.st_dev != filesystem_dev
773 && S_ISDIR(st.st_mode))
774 flags |= FLAG_MOUNT_POINT;
775
776 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
777 return NULL;
778
779 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
780 return NULL;
781
782 skip_excludes:
783
784 if (verbose > 2) {
785 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
786 who_am_i(), thisname, exclude_level);
787 }
788
789 if ((basename = strrchr(thisname, '/')) != NULL) {
790 dirname_len = ++basename - thisname; /* counts future '\0' */
791 if (lastdir_len == dirname_len - 1
792 && strncmp(thisname, lastdir, lastdir_len) == 0) {
793 dirname = lastdir;
794 dirname_len = 0; /* indicates no copy is needed */
795 } else
796 dirname = thisname;
797 } else {
798 basename = thisname;
799 dirname = NULL;
800 dirname_len = 0;
801 }
802 basename_len = strlen(basename) + 1; /* count the '\0' */
803
804#if SUPPORT_LINKS
805 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
806#else
807 linkname_len = 0;
808#endif
809
810#if SUPPORT_HARD_LINKS
811 if (preserve_hard_links) {
812 if (protocol_version < 28) {
813 if (S_ISREG(st.st_mode))
814 idev_len = sizeof (struct idev);
815 else
816 idev_len = 0;
817 } else {
818 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
819 idev_len = sizeof (struct idev);
820 else
821 idev_len = 0;
822 }
823 } else
824#endif
825 idev_len = 0;
826
827 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
828 file_struct_len = idev_len? sizeof file[0] : min_file_struct_len;
829
830 alloc_len = file_struct_len + dirname_len + basename_len
831 + linkname_len + sum_len + idev_len;
832 if (!(bp = new_array(char, alloc_len)))
833 out_of_memory("receive_file_entry");
834 file = (struct file_struct *)bp;
835 memset(bp, 0, min_file_struct_len);
836 bp += file_struct_len;
837
838 file->flags = flags;
839 file->modtime = st.st_mtime;
840 file->length = st.st_size;
841 file->mode = st.st_mode;
842 file->uid = st.st_uid;
843 file->gid = st.st_gid;
844
845#if SUPPORT_HARD_LINKS
846 if (idev_len) {
847 file->link_u.idev = (struct idev *)bp;
848 bp += idev_len;
849 file->F_DEV = st.st_dev;
850 file->F_INODE = st.st_ino;
851 }
852#endif
853
854 if (dirname_len) {
855 file->dirname = lastdir = bp;
856 lastdir_len = dirname_len - 1;
857 memcpy(bp, dirname, dirname_len - 1);
858 bp += dirname_len;
859 bp[-1] = '\0';
860 } else if (dirname)
861 file->dirname = dirname;
862
863 file->basename = bp;
864 memcpy(bp, basename, basename_len);
865 bp += basename_len;
866
867#ifdef HAVE_STRUCT_STAT_ST_RDEV
868 if (preserve_devices && IS_DEVICE(st.st_mode))
869 file->u.rdev = st.st_rdev;
870#endif
871
872#if SUPPORT_LINKS
873 if (linkname_len) {
874 file->u.link = bp;
875 memcpy(bp, linkname, linkname_len);
876 bp += linkname_len;
877 }
878#endif
879
880 if (sum_len) {
881 file->u.sum = bp;
882 file_checksum(thisname, bp, st.st_size);
883 /*bp += sum_len;*/
884 }
885
886 file->basedir = flist_dir;
887
888 if (!S_ISDIR(st.st_mode))
889 stats.total_size += st.st_size;
890
891 return file;
892}
893
894
895void send_file_name(int f, struct file_list *flist, char *fname,
896 int recursive, unsigned short base_flags)
897{
898 struct file_struct *file;
899 char fbuf[MAXPATHLEN];
900 extern int delete_excluded;
901
902 /* f is set to -1 when calculating deletion file list */
903 file = make_file(fname,
904 f == -1 && delete_excluded? SERVER_EXCLUDES
905 : ALL_EXCLUDES);
906
907 if (!file)
908 return;
909
910 maybe_emit_filelist_progress(flist);
911
912 flist_expand(flist);
913
914 if (write_batch)
915 file->flags |= FLAG_TOP_DIR;
916
917 if (file->basename[0]) {
918 flist->files[flist->count++] = file;
919 send_file_entry(file, f, base_flags);
920 }
921
922 if (recursive && S_ISDIR(file->mode)
923 && !(file->flags & FLAG_MOUNT_POINT)) {
924 struct exclude_struct **last_exclude_list = local_exclude_list;
925 send_directory(f, flist, f_name_to(file, fbuf));
926 local_exclude_list = last_exclude_list;
927 return;
928 }
929}
930
931
932static void send_directory(int f, struct file_list *flist, char *dir)
933{
934 DIR *d;
935 struct dirent *di;
936 char fname[MAXPATHLEN];
937 unsigned int offset;
938 char *p;
939
940 d = opendir(dir);
941 if (!d) {
942 io_error |= IOERR_GENERAL;
943 rprintf(FERROR, "opendir %s failed: %s\n",
944 full_fname(dir), strerror(errno));
945 return;
946 }
947
948 offset = strlcpy(fname, dir, MAXPATHLEN);
949 p = fname + offset;
950 if (offset >= MAXPATHLEN || p[-1] != '/') {
951 if (offset >= MAXPATHLEN - 1) {
952 io_error |= IOERR_GENERAL;
953 rprintf(FERROR, "skipping long-named directory: %s\n",
954 full_fname(fname));
955 closedir(d);
956 return;
957 }
958 *p++ = '/';
959 offset++;
960 }
961
962 local_exclude_list = NULL;
963
964 if (cvs_exclude) {
965 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
966 < MAXPATHLEN - offset)
967 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
968 else {
969 io_error |= IOERR_GENERAL;
970 rprintf(FINFO,
971 "cannot cvs-exclude in long-named directory %s\n",
972 full_fname(fname));
973 }
974 }
975
976 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
977 char *dname = d_name(di);
978 if (dname[0] == '.' && (dname[1] == '\0'
979 || (dname[1] == '.' && dname[2] == '\0')))
980 continue;
981 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
982 send_file_name(f, flist, fname, recurse, 0);
983 else {
984 io_error |= IOERR_GENERAL;
985 rprintf(FINFO,
986 "cannot send long-named file %s\n",
987 full_fname(fname));
988 }
989 }
990 if (errno) {
991 io_error |= IOERR_GENERAL;
992 rprintf(FERROR, "readdir(%s): (%d) %s\n",
993 dir, errno, strerror(errno));
994 }
995
996 if (local_exclude_list)
997 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
998
999 closedir(d);
1000}
1001
1002
1003/**
1004 * The delete_files() function in receiver.c sets f to -1 so that we just
1005 * construct the file list in memory without sending it over the wire. It
1006 * also has the side-effect of ignoring user-excludes if delete_excluded
1007 * is set (so that the delete list includes user-excluded files).
1008 **/
1009struct file_list *send_file_list(int f, int argc, char *argv[])
1010{
1011 int l;
1012 STRUCT_STAT st;
1013 char *p, *dir, olddir[sizeof curr_dir];
1014 char lastpath[MAXPATHLEN] = "";
1015 struct file_list *flist;
1016 int64 start_write;
1017 int use_ff_fd = 0;
1018
1019 if (show_filelist_p() && f != -1)
1020 start_filelist_progress("building file list");
1021
1022 start_write = stats.total_written;
1023
1024 flist = flist_new();
1025
1026 if (f != -1) {
1027 io_start_buffering_out(f);
1028 if (filesfrom_fd >= 0) {
1029 if (argv[0] && !push_dir(argv[0])) {
1030 rprintf(FERROR, "push_dir %s failed: %s\n",
1031 full_fname(argv[0]), strerror(errno));
1032 exit_cleanup(RERR_FILESELECT);
1033 }
1034 use_ff_fd = 1;
1035 }
1036 }
1037
1038 while (1) {
1039 char fname2[MAXPATHLEN];
1040 char *fname = fname2;
1041
1042 if (use_ff_fd) {
1043 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1044 break;
1045 sanitize_path(fname, NULL);
1046 } else {
1047 if (argc-- == 0)
1048 break;
1049 strlcpy(fname, *argv++, MAXPATHLEN);
1050 if (sanitize_paths)
1051 sanitize_path(fname, NULL);
1052 }
1053
1054 l = strlen(fname);
1055 if (fname[l - 1] == '/') {
1056 if (l == 2 && fname[0] == '.') {
1057 /* Turn "./" into just "." rather than "./." */
1058 fname[1] = '\0';
1059 } else if (l < MAXPATHLEN) {
1060 fname[l++] = '.';
1061 fname[l] = '\0';
1062 }
1063 }
1064
1065 if (link_stat(fname, &st) != 0) {
1066 if (f != -1) {
1067 io_error |= IOERR_GENERAL;
1068 rprintf(FERROR, "link_stat %s failed: %s\n",
1069 full_fname(fname), strerror(errno));
1070 }
1071 continue;
1072 }
1073
1074 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1075 rprintf(FINFO, "skipping directory %s\n", fname);
1076 continue;
1077 }
1078
1079 dir = NULL;
1080 olddir[0] = '\0';
1081
1082 if (!relative_paths) {
1083 p = strrchr(fname, '/');
1084 if (p) {
1085 *p = 0;
1086 if (p == fname)
1087 dir = "/";
1088 else
1089 dir = fname;
1090 fname = p + 1;
1091 }
1092 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1093 /* this ensures we send the intermediate directories,
1094 thus getting their permissions right */
1095 char *lp = lastpath, *fn = fname, *slash = fname;
1096 *p = 0;
1097 /* Skip any initial directories in our path that we
1098 * have in common with lastpath. */
1099 while (*fn && *lp == *fn) {
1100 if (*fn == '/')
1101 slash = fn;
1102 lp++, fn++;
1103 }
1104 *p = '/';
1105 if (fn != p || (*lp && *lp != '/')) {
1106 int copy_links_saved = copy_links;
1107 int recurse_saved = recurse;
1108 copy_links = copy_unsafe_links;
1109 /* set recurse to 1 to prevent make_file
1110 * from ignoring directory, but still
1111 * turn off the recursive parameter to
1112 * send_file_name */
1113 recurse = 1;
1114 while ((slash = strchr(slash+1, '/')) != 0) {
1115 *slash = 0;
1116 send_file_name(f, flist, fname, 0, 0);
1117 *slash = '/';
1118 }
1119 copy_links = copy_links_saved;
1120 recurse = recurse_saved;
1121 *p = 0;
1122 strlcpy(lastpath, fname, sizeof lastpath);
1123 *p = '/';
1124 }
1125 }
1126
1127 if (!*fname)
1128 fname = ".";
1129
1130 if (dir && *dir) {
1131 static char *lastdir;
1132 static int lastdir_len;
1133
1134 strcpy(olddir, curr_dir); /* can't overflow */
1135
1136 if (!push_dir(dir)) {
1137 io_error |= IOERR_GENERAL;
1138 rprintf(FERROR, "push_dir %s failed: %s\n",
1139 full_fname(dir), strerror(errno));
1140 continue;
1141 }
1142
1143 if (lastdir && strcmp(lastdir, dir) == 0) {
1144 flist_dir = lastdir;
1145 flist_dir_len = lastdir_len;
1146 } else {
1147 flist_dir = lastdir = strdup(dir);
1148 flist_dir_len = lastdir_len = strlen(dir);
1149 }
1150 }
1151
1152 if (one_file_system)
1153 set_filesystem(fname);
1154
1155 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1156
1157 if (olddir[0]) {
1158 flist_dir = NULL;
1159 flist_dir_len = 0;
1160 if (!pop_dir(olddir)) {
1161 rprintf(FERROR, "pop_dir %s failed: %s\n",
1162 full_fname(dir), strerror(errno));
1163 exit_cleanup(RERR_FILESELECT);
1164 }
1165 }
1166 }
1167
1168 if (f != -1) {
1169 send_file_entry(NULL, f, 0);
1170
1171 if (show_filelist_p())
1172 finish_filelist_progress(flist);
1173 }
1174
1175 clean_flist(flist, 0, 0);
1176
1177 if (f != -1) {
1178 /* Now send the uid/gid list. This was introduced in
1179 * protocol version 15 */
1180 send_uid_list(f);
1181
1182 /* send the io_error flag */
1183 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1184
1185 io_end_buffering();
1186 stats.flist_size = stats.total_written - start_write;
1187 stats.num_files = flist->count;
1188 if (write_batch)
1189 write_batch_flist_info(flist->count, flist->files);
1190 }
1191
1192 if (verbose > 2)
1193 rprintf(FINFO, "send_file_list done\n");
1194
1195 return flist;
1196}
1197
1198
1199struct file_list *recv_file_list(int f)
1200{
1201 struct file_list *flist;
1202 unsigned short flags;
1203 int64 start_read;
1204 extern int list_only;
1205
1206 if (show_filelist_p())
1207 start_filelist_progress("receiving file list");
1208
1209 start_read = stats.total_read;
1210
1211 flist = new(struct file_list);
1212 if (!flist)
1213 goto oom;
1214
1215 flist->count = 0;
1216 flist->malloced = 1000;
1217 flist->files = new_array(struct file_struct *, flist->malloced);
1218 if (!flist->files)
1219 goto oom;
1220
1221
1222 while ((flags = read_byte(f)) != 0) {
1223 int i = flist->count;
1224
1225 flist_expand(flist);
1226
1227 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1228 flags |= read_byte(f) << 8;
1229 receive_file_entry(&flist->files[i], flags, f);
1230
1231 if (S_ISREG(flist->files[i]->mode))
1232 stats.total_size += flist->files[i]->length;
1233
1234 flist->count++;
1235
1236 maybe_emit_filelist_progress(flist);
1237
1238 if (verbose > 2) {
1239 rprintf(FINFO, "recv_file_name(%s)\n",
1240 f_name(flist->files[i]));
1241 }
1242 }
1243 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1244
1245 if (verbose > 2)
1246 rprintf(FINFO, "received %d names\n", flist->count);
1247
1248 if (show_filelist_p())
1249 finish_filelist_progress(flist);
1250
1251 clean_flist(flist, relative_paths, 1);
1252
1253 if (f != -1) {
1254 /* Now send the uid/gid list. This was introduced in
1255 * protocol version 15 */
1256 recv_uid_list(f, flist);
1257
1258 if (!read_batch) {
1259 /* Recv the io_error flag */
1260 if (lp_ignore_errors(module_id) || ignore_errors)
1261 read_int(f);
1262 else
1263 io_error |= read_int(f);
1264 }
1265 }
1266
1267 if (list_only) {
1268 int i;
1269 for (i = 0; i < flist->count; i++)
1270 list_file_entry(flist->files[i]);
1271 }
1272
1273 if (verbose > 2)
1274 rprintf(FINFO, "recv_file_list done\n");
1275
1276 stats.flist_size = stats.total_read - start_read;
1277 stats.num_files = flist->count;
1278
1279 return flist;
1280
1281 oom:
1282 out_of_memory("recv_file_list");
1283 return NULL; /* not reached */
1284}
1285
1286
1287int file_compare(struct file_struct **file1, struct file_struct **file2)
1288{
1289 struct file_struct *f1 = *file1;
1290 struct file_struct *f2 = *file2;
1291
1292 if (!f1->basename && !f2->basename)
1293 return 0;
1294 if (!f1->basename)
1295 return -1;
1296 if (!f2->basename)
1297 return 1;
1298 if (f1->dirname == f2->dirname)
1299 return u_strcmp(f1->basename, f2->basename);
1300 return f_name_cmp(f1, f2);
1301}
1302
1303
1304int flist_find(struct file_list *flist, struct file_struct *f)
1305{
1306 int low = 0, high = flist->count - 1;
1307
1308 while (high >= 0 && !flist->files[high]->basename) high--;
1309
1310 if (high < 0)
1311 return -1;
1312
1313 while (low != high) {
1314 int mid = (low + high) / 2;
1315 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1316 if (ret == 0)
1317 return flist_up(flist, mid);
1318 if (ret > 0)
1319 high = mid;
1320 else
1321 low = mid + 1;
1322 }
1323
1324 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1325 return flist_up(flist, low);
1326 return -1;
1327}
1328
1329
1330/*
1331 * Free up any resources a file_struct has allocated, and optionally free
1332 * it up as well.
1333 */
1334void free_file(struct file_struct *file, int free_the_struct)
1335{
1336 if (free_the_struct)
1337 free(file);
1338 else
1339 memset(file, 0, min_file_struct_len);
1340}
1341
1342
1343/*
1344 * allocate a new file list
1345 */
1346struct file_list *flist_new(void)
1347{
1348 struct file_list *flist;
1349
1350 flist = new(struct file_list);
1351 if (!flist)
1352 out_of_memory("send_file_list");
1353
1354 flist->count = 0;
1355 flist->malloced = 0;
1356 flist->files = NULL;
1357
1358 return flist;
1359}
1360
1361/*
1362 * free up all elements in a flist
1363 */
1364void flist_free(struct file_list *flist)
1365{
1366 int i;
1367 for (i = 1; i < flist->count; i++)
1368 free_file(flist->files[i], FREE_STRUCT);
1369 free(flist->files);
1370 free(flist);
1371}
1372
1373
1374/*
1375 * This routine ensures we don't have any duplicate names in our file list.
1376 * duplicate names can cause corruption because of the pipelining
1377 */
1378static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1379{
1380 int i, prev_i = 0;
1381
1382 if (!flist || flist->count == 0)
1383 return;
1384
1385 qsort(flist->files, flist->count,
1386 sizeof flist->files[0], (int (*)()) file_compare);
1387
1388 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1389 if (flist->files[i]->basename) {
1390 prev_i = i;
1391 break;
1392 }
1393 }
1394 while (++i < flist->count) {
1395 if (!flist->files[i]->basename)
1396 continue;
1397 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1398 if (verbose > 1 && !am_server) {
1399 rprintf(FINFO,
1400 "removing duplicate name %s from file list %d\n",
1401 f_name(flist->files[i]), i);
1402 }
1403 /* Make sure that if we unduplicate '.', that we don't
1404 * lose track of a user-specified starting point (or
1405 * else deletions will mysteriously fail with -R). */
1406 if (flist->files[i]->flags & FLAG_TOP_DIR)
1407 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1408 free_file(flist->files[i], CLEAR_STRUCT);
1409 } else
1410 prev_i = i;
1411 }
1412
1413 if (strip_root) {
1414 /* we need to strip off the root directory in the case
1415 of relative paths, but this must be done _after_
1416 the sorting phase */
1417 for (i = 0; i < flist->count; i++) {
1418 if (flist->files[i]->dirname &&
1419 flist->files[i]->dirname[0] == '/') {
1420 memmove(&flist->files[i]->dirname[0],
1421 &flist->files[i]->dirname[1],
1422 strlen(flist->files[i]->dirname));
1423 }
1424
1425 if (flist->files[i]->dirname &&
1426 !flist->files[i]->dirname[0]) {
1427 flist->files[i]->dirname = NULL;
1428 }
1429 }
1430 }
1431
1432 if (verbose <= 3)
1433 return;
1434
1435 for (i = 0; i < flist->count; i++) {
1436 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1437 who_am_i(), i,
1438 NS(flist->files[i]->basedir),
1439 NS(flist->files[i]->dirname),
1440 NS(flist->files[i]->basename),
1441 (int) flist->files[i]->mode,
1442 (double) flist->files[i]->length);
1443 }
1444}
1445
1446
1447enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1448
1449/* Compare the names of two file_struct entities, just like strcmp()
1450 * would do if it were operating on the joined strings. We assume
1451 * that there are no 0-length strings.
1452 */
1453int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1454{
1455 int dif;
1456 const uchar *c1, *c2;
1457 enum fnc_state state1, state2;
1458
1459 if (!f1 || !f1->basename) {
1460 if (!f2 || !f2->basename)
1461 return 0;
1462 return -1;
1463 }
1464 if (!f2 || !f2->basename)
1465 return 1;
1466
1467 if (!(c1 = (uchar*)f1->dirname)) {
1468 state1 = fnc_BASE;
1469 c1 = (uchar*)f1->basename;
1470 } else
1471 state1 = fnc_DIR;
1472 if (!(c2 = (uchar*)f2->dirname)) {
1473 state2 = fnc_BASE;
1474 c2 = (uchar*)f2->basename;
1475 } else
1476 state2 = fnc_DIR;
1477
1478 while (1) {
1479 if ((dif = (int)*c1 - (int)*c2) != 0)
1480 break;
1481 if (!*++c1) {
1482 switch (state1) {
1483 case fnc_DIR:
1484 state1 = fnc_SLASH;
1485 c1 = (uchar*)"/";
1486 break;
1487 case fnc_SLASH:
1488 state1 = fnc_BASE;
1489 c1 = (uchar*)f1->basename;
1490 break;
1491 case fnc_BASE:
1492 break;
1493 }
1494 }
1495 if (!*++c2) {
1496 switch (state2) {
1497 case fnc_DIR:
1498 state2 = fnc_SLASH;
1499 c2 = (uchar*)"/";
1500 break;
1501 case fnc_SLASH:
1502 state2 = fnc_BASE;
1503 c2 = (uchar*)f2->basename;
1504 break;
1505 case fnc_BASE:
1506 if (!*c1)
1507 return 0;
1508 break;
1509 }
1510 }
1511 }
1512
1513 return dif;
1514}
1515
1516
1517/* Return a copy of the full filename of a flist entry, using the indicated
1518 * buffer. No size-checking is done because we checked the size when creating
1519 * the file_struct entry.
1520 */
1521char *f_name_to(struct file_struct *f, char *fbuf)
1522{
1523 if (!f || !f->basename)
1524 return NULL;
1525
1526 if (f->dirname) {
1527 int len = strlen(f->dirname);
1528 memcpy(fbuf, f->dirname, len);
1529 fbuf[len] = '/';
1530 strcpy(fbuf + len + 1, f->basename);
1531 } else
1532 strcpy(fbuf, f->basename);
1533 return fbuf;
1534}
1535
1536
1537/* Like f_name_to(), but we rotate through 5 static buffers of our own.
1538 */
1539char *f_name(struct file_struct *f)
1540{
1541 static char names[5][MAXPATHLEN];
1542 static unsigned int n;
1543
1544 n = (n + 1) % (sizeof names / sizeof names[0]);
1545
1546 return f_name_to(f, names[n]);
1547}