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