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