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