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