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