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