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