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