Made make_exclude() store anchored config-file excludes with the full
[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 (file->basename[0]) {
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 (dname[0] == '.' && (dname[1] == '\0' ||
863 (dname[1] == '.' && dname[2] == '\0')))
864 continue;
865 strlcpy(p, dname, MAXPATHLEN - l);
866 send_file_name(f, flist, fname, recurse, 0);
867 }
868
869 if (local_exclude_list) {
870 add_exclude_list("!", &local_exclude_list, 0);
871 }
872
873 closedir(d);
874}
875
876
877/**
878 *
879 * I <b>think</b> f==-1 means that the list should just be built in
880 * memory and not transmitted. But who can tell? -- mbp
881 **/
882struct file_list *send_file_list(int f, int argc, char *argv[])
883{
884 int l;
885 STRUCT_STAT st;
886 char *p, *dir, *olddir;
887 char lastpath[MAXPATHLEN] = "";
888 struct file_list *flist;
889 int64 start_write;
890 int use_ff_fd = 0;
891
892 if (show_filelist_p() && f != -1)
893 start_filelist_progress("building file list");
894
895 start_write = stats.total_written;
896
897 flist = flist_new();
898
899 if (f != -1) {
900 io_start_buffering(f);
901 if (filesfrom_fd >= 0) {
902 if (argv[0] && !push_dir(argv[0], 0)) {
903 rprintf(FERROR, "push_dir %s : %s\n",
904 argv[0], strerror(errno));
905 exit_cleanup(RERR_FILESELECT);
906 }
907 use_ff_fd = 1;
908 }
909 }
910
911 while (1) {
912 char fname2[MAXPATHLEN];
913 char *fname = fname2;
914
915 if (use_ff_fd) {
916 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
917 break;
918 sanitize_path(fname, NULL);
919 } else {
920 if (argc-- == 0)
921 break;
922 strlcpy(fname, *argv++, MAXPATHLEN);
923 if (sanitize_paths)
924 sanitize_path(fname, NULL);
925 }
926
927 l = strlen(fname);
928 if (l != 1 && fname[l - 1] == '/') {
929 if ((l == 2) && (fname[0] == '.')) {
930 /* Turn ./ into just . rather than ./.
931 This was put in to avoid a problem with
932 rsync -aR --delete from ./
933 The send_file_name() below of ./ was
934 mysteriously preventing deletes */
935 fname[1] = 0;
936 } else {
937 strlcat(fname, ".", MAXPATHLEN);
938 }
939 }
940
941 if (link_stat(fname, &st) != 0) {
942 if (f != -1) {
943 io_error = 1;
944 rprintf(FERROR, "link_stat %s : %s\n",
945 fname, strerror(errno));
946 }
947 continue;
948 }
949
950 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
951 rprintf(FINFO, "skipping directory %s\n", fname);
952 continue;
953 }
954
955 dir = NULL;
956 olddir = NULL;
957
958 if (!relative_paths) {
959 p = strrchr(fname, '/');
960 if (p) {
961 *p = 0;
962 if (p == fname)
963 dir = "/";
964 else
965 dir = fname;
966 fname = p + 1;
967 }
968 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
969 /* this ensures we send the intermediate directories,
970 thus getting their permissions right */
971 char *lp = lastpath, *fn = fname, *slash = fname;
972 *p = 0;
973 /* Skip any initial directories in our path that we
974 * have in common with lastpath. */
975 while (*fn && *lp == *fn) {
976 if (*fn == '/')
977 slash = fn;
978 lp++, fn++;
979 }
980 *p = '/';
981 if (fn != p || (*lp && *lp != '/')) {
982 int copy_links_saved = copy_links;
983 int recurse_saved = recurse;
984 copy_links = copy_unsafe_links;
985 /* set recurse to 1 to prevent make_file
986 * from ignoring directory, but still
987 * turn off the recursive parameter to
988 * send_file_name */
989 recurse = 1;
990 while ((slash = strchr(slash+1, '/')) != 0) {
991 *slash = 0;
992 send_file_name(f, flist, fname, 0, 0);
993 *slash = '/';
994 }
995 copy_links = copy_links_saved;
996 recurse = recurse_saved;
997 *p = 0;
998 strlcpy(lastpath, fname, sizeof lastpath);
999 *p = '/';
1000 }
1001 }
1002
1003 if (!*fname)
1004 fname = ".";
1005
1006 if (dir && *dir) {
1007 olddir = push_dir(dir, 1);
1008
1009 if (!olddir) {
1010 io_error = 1;
1011 rprintf(FERROR, "push_dir %s : %s\n",
1012 dir, strerror(errno));
1013 continue;
1014 }
1015
1016 flist_dir = dir;
1017 }
1018
1019 if (one_file_system)
1020 set_filesystem(fname);
1021
1022 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1023
1024 if (olddir != NULL) {
1025 flist_dir = NULL;
1026 if (pop_dir(olddir) != 0) {
1027 rprintf(FERROR, "pop_dir %s : %s\n",
1028 dir, strerror(errno));
1029 exit_cleanup(RERR_FILESELECT);
1030 }
1031 }
1032 }
1033
1034 if (f != -1) {
1035 send_file_entry(NULL, f, 0);
1036 }
1037
1038 if (show_filelist_p() && f != -1) {
1039 finish_filelist_progress(flist);
1040 }
1041
1042 clean_flist(flist, 0, 0);
1043
1044 /* now send the uid/gid list. This was introduced in protocol
1045 version 15 */
1046 if (f != -1) {
1047 send_uid_list(f);
1048 }
1049
1050 /* send the io_error flag */
1051 if (f != -1) {
1052 extern int module_id;
1053 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1054 }
1055
1056 if (f != -1) {
1057 io_end_buffering();
1058 stats.flist_size = stats.total_written - start_write;
1059 stats.num_files = flist->count;
1060 if (write_batch) /* dw */
1061 write_batch_flist_info(flist->count, flist->files);
1062 }
1063
1064 if (verbose > 2)
1065 rprintf(FINFO, "send_file_list done\n");
1066
1067 return flist;
1068}
1069
1070
1071struct file_list *recv_file_list(int f)
1072{
1073 struct file_list *flist;
1074 unsigned char flags;
1075 int64 start_read;
1076 extern int list_only;
1077
1078 if (show_filelist_p())
1079 start_filelist_progress("receiving file list");
1080
1081 start_read = stats.total_read;
1082
1083 flist = (struct file_list *) malloc(sizeof(flist[0]));
1084 if (!flist)
1085 goto oom;
1086
1087 flist->count = 0;
1088 flist->malloced = 1000;
1089 flist->files =
1090 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1091 flist->malloced);
1092 if (!flist->files)
1093 goto oom;
1094
1095
1096 for (flags = read_byte(f); flags; flags = read_byte(f)) {
1097 int i = flist->count;
1098
1099 flist_expand(flist);
1100
1101 receive_file_entry(&flist->files[i], flags, f);
1102
1103 if (S_ISREG(flist->files[i]->mode))
1104 stats.total_size += flist->files[i]->length;
1105
1106 flist->count++;
1107
1108 maybe_emit_filelist_progress(flist);
1109
1110 if (verbose > 2)
1111 rprintf(FINFO, "recv_file_name(%s)\n",
1112 f_name(flist->files[i]));
1113 }
1114
1115
1116 if (verbose > 2)
1117 rprintf(FINFO, "received %d names\n", flist->count);
1118
1119 clean_flist(flist, relative_paths, 1);
1120
1121 if (show_filelist_p()) {
1122 finish_filelist_progress(flist);
1123 }
1124
1125 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1126 if (f != -1) {
1127 recv_uid_list(f, flist);
1128 }
1129
1130 /* recv the io_error flag */
1131 if (f != -1 && !read_batch) { /* dw-added readbatch */
1132 extern int module_id;
1133 extern int ignore_errors;
1134 if (lp_ignore_errors(module_id) || ignore_errors) {
1135 read_int(f);
1136 } else {
1137 io_error |= read_int(f);
1138 }
1139 }
1140
1141 if (list_only) {
1142 int i;
1143 for (i = 0; i < flist->count; i++) {
1144 list_file_entry(flist->files[i]);
1145 }
1146 }
1147
1148
1149 if (verbose > 2)
1150 rprintf(FINFO, "recv_file_list done\n");
1151
1152 stats.flist_size = stats.total_read - start_read;
1153 stats.num_files = flist->count;
1154
1155 return flist;
1156
1157 oom:
1158 out_of_memory("recv_file_list");
1159 return NULL; /* not reached */
1160}
1161
1162
1163/*
1164 * XXX: This is currently the hottest function while building the file
1165 * list, because building f_name()s every time is expensive.
1166 **/
1167int file_compare(struct file_struct **f1, struct file_struct **f2)
1168{
1169 if (!(*f1)->basename && !(*f2)->basename)
1170 return 0;
1171 if (!(*f1)->basename)
1172 return -1;
1173 if (!(*f2)->basename)
1174 return 1;
1175 if ((*f1)->dirname == (*f2)->dirname)
1176 return u_strcmp((*f1)->basename, (*f2)->basename);
1177 return u_strcmp(f_name(*f1), f_name(*f2));
1178}
1179
1180
1181int flist_find(struct file_list *flist, struct file_struct *f)
1182{
1183 int low = 0, high = flist->count - 1;
1184
1185 while (high >= 0 && !flist->files[high]->basename) high--;
1186
1187 if (high < 0)
1188 return -1;
1189
1190 while (low != high) {
1191 int mid = (low + high) / 2;
1192 int ret =
1193 file_compare(&flist->files[flist_up(flist, mid)], &f);
1194 if (ret == 0)
1195 return flist_up(flist, mid);
1196 if (ret > 0) {
1197 high = mid;
1198 } else {
1199 low = mid + 1;
1200 }
1201 }
1202
1203 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1204 return flist_up(flist, low);
1205 return -1;
1206}
1207
1208
1209/*
1210 * free up one file
1211 */
1212void free_file(struct file_struct *file)
1213{
1214 if (!file)
1215 return;
1216 if (file->basename)
1217 free(file->basename);
1218 if (file->link)
1219 free(file->link);
1220 if (file->sum)
1221 free(file->sum);
1222 *file = null_file;
1223}
1224
1225
1226/*
1227 * allocate a new file list
1228 */
1229struct file_list *flist_new(void)
1230{
1231 struct file_list *flist;
1232
1233 flist = (struct file_list *) malloc(sizeof(flist[0]));
1234 if (!flist)
1235 out_of_memory("send_file_list");
1236
1237 flist->count = 0;
1238 flist->malloced = 0;
1239 flist->files = NULL;
1240
1241#if ARENA_SIZE > 0
1242 flist->string_area = string_area_new(0);
1243#else
1244 flist->string_area = NULL;
1245#endif
1246 return flist;
1247}
1248
1249/*
1250 * free up all elements in a flist
1251 */
1252void flist_free(struct file_list *flist)
1253{
1254 int i;
1255 for (i = 1; i < flist->count; i++) {
1256 if (!flist->string_area)
1257 free_file(flist->files[i]);
1258 free(flist->files[i]);
1259 }
1260 /* FIXME: I don't think we generally need to blank the flist
1261 * since it's about to be freed. This will just cause more
1262 * memory traffic. If you want a freed-memory debugger, you
1263 * know where to get it. */
1264 memset((char *) flist->files, 0,
1265 sizeof(flist->files[0]) * flist->count);
1266 free(flist->files);
1267 if (flist->string_area)
1268 string_area_free(flist->string_area);
1269 memset((char *) flist, 0, sizeof(*flist));
1270 free(flist);
1271}
1272
1273
1274/*
1275 * This routine ensures we don't have any duplicate names in our file list.
1276 * duplicate names can cause corruption because of the pipelining
1277 */
1278static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1279{
1280 int i;
1281 char *name, *prev_name = NULL;
1282
1283 if (!flist || flist->count == 0)
1284 return;
1285
1286 qsort(flist->files, flist->count,
1287 sizeof(flist->files[0]), (int (*)()) file_compare);
1288
1289 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1290 if (flist->files[i]->basename) {
1291 prev_name = f_name(flist->files[i]);
1292 break;
1293 }
1294 }
1295 while (++i < flist->count) {
1296 if (!flist->files[i]->basename)
1297 continue;
1298 name = f_name(flist->files[i]);
1299 if (strcmp(name, prev_name) == 0) {
1300 if (verbose > 1 && !am_server) {
1301 rprintf(FINFO,
1302 "removing duplicate name %s from file list %d\n",
1303 name, i);
1304 }
1305 /* it's not great that the flist knows the semantics of
1306 * the file memory usage, but i'd rather not add a flag
1307 * byte to that struct.
1308 * XXX can i use a bit in the flags field? */
1309 if (flist->string_area)
1310 flist->files[i][0] = null_file;
1311 else
1312 free_file(flist->files[i]);
1313 }
1314 prev_name = name;
1315 }
1316
1317 if (strip_root) {
1318 /* we need to strip off the root directory in the case
1319 of relative paths, but this must be done _after_
1320 the sorting phase */
1321 for (i = 0; i < flist->count; i++) {
1322 if (flist->files[i]->dirname &&
1323 flist->files[i]->dirname[0] == '/') {
1324 memmove(&flist->files[i]->dirname[0],
1325 &flist->files[i]->dirname[1],
1326 strlen(flist->files[i]->dirname));
1327 }
1328
1329 if (flist->files[i]->dirname &&
1330 !flist->files[i]->dirname[0]) {
1331 flist->files[i]->dirname = NULL;
1332 }
1333 }
1334 }
1335
1336 if (verbose <= 3)
1337 return;
1338
1339 for (i = 0; i < flist->count; i++) {
1340 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1341 (int) getpid(), i,
1342 NS(flist->files[i]->dirname),
1343 NS(flist->files[i]->basename),
1344 (int) flist->files[i]->mode,
1345 (double) flist->files[i]->length);
1346 }
1347}
1348
1349
1350/*
1351 * return the full filename of a flist entry
1352 *
1353 * This function is too expensive at the moment, because it copies
1354 * strings when often we only want to compare them. In any case,
1355 * using strlcat is silly because it will walk the string repeatedly.
1356 */
1357char *f_name(struct file_struct *f)
1358{
1359 static char names[10][MAXPATHLEN];
1360 static int n;
1361 char *p = names[n];
1362
1363 if (!f || !f->basename)
1364 return NULL;
1365
1366 n = (n + 1) % 10;
1367
1368 if (f->dirname) {
1369 int off;
1370
1371 off = strlcpy(p, f->dirname, MAXPATHLEN);
1372 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1373 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
1374 } else {
1375 strlcpy(p, f->basename, MAXPATHLEN);
1376 }
1377
1378 return p;
1379}