Output some info about the size of our structures.
[rsync/rsync.git] / flist.c
... / ...
CommitLineData
1/*
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24#include "rsync.h"
25#include "rounding.h"
26
27extern int verbose;
28extern int list_only;
29extern int am_root;
30extern int am_server;
31extern int am_daemon;
32extern int am_sender;
33extern int do_progress;
34extern int always_checksum;
35extern int module_id;
36extern int ignore_errors;
37extern int numeric_ids;
38extern int recurse;
39extern int xfer_dirs;
40extern int filesfrom_fd;
41extern int one_file_system;
42extern int copy_dirlinks;
43extern int keep_dirlinks;
44extern int preserve_links;
45extern int preserve_hard_links;
46extern int preserve_devices;
47extern int preserve_specials;
48extern int preserve_uid;
49extern int preserve_gid;
50extern int relative_paths;
51extern int implied_dirs;
52extern int flist_extra_cnt;
53extern int ignore_perishable;
54extern int non_perishable_cnt;
55extern int prune_empty_dirs;
56extern int copy_links;
57extern int copy_unsafe_links;
58extern int protocol_version;
59extern int sanitize_paths;
60extern struct stats stats;
61extern struct file_list *the_file_list;
62extern alloc_pool_t hlink_pool;
63
64extern char curr_dir[MAXPATHLEN];
65
66extern struct chmod_mode_struct *chmod_modes;
67
68extern struct filter_list_struct filter_list;
69extern struct filter_list_struct server_filter_list;
70
71int io_error;
72int checksum_len;
73dev_t filesystem_dev; /* used to implement -x */
74
75/* The tmp_* vars are used as a cache area by make_file() to store data
76 * that the sender doesn't need to remember in its file list. The data
77 * will survive just long enough to be used by send_file_entry(). */
78static dev_t tmp_rdev;
79#ifdef SUPPORT_HARD_LINKS
80static struct idev tmp_idev;
81#endif
82static char tmp_sum[MD4_SUM_LENGTH];
83
84static char empty_sum[MD4_SUM_LENGTH];
85static int flist_count_offset; /* for --delete --progress */
86
87static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
88static void output_flist(struct file_list *flist);
89
90void init_flist(void)
91{
92 if (verbose > 4) {
93 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
94 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
95 }
96 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
97}
98
99static int show_filelist_p(void)
100{
101 return verbose && xfer_dirs && !am_server;
102}
103
104static void start_filelist_progress(char *kind)
105{
106 rprintf(FCLIENT, "%s ... ", kind);
107 if (verbose > 1 || do_progress)
108 rprintf(FCLIENT, "\n");
109 rflush(FINFO);
110}
111
112static void emit_filelist_progress(int count)
113{
114 rprintf(FCLIENT, " %d files...\r", count);
115}
116
117static void maybe_emit_filelist_progress(int count)
118{
119 if (do_progress && show_filelist_p() && (count % 100) == 0)
120 emit_filelist_progress(count);
121}
122
123static void finish_filelist_progress(const struct file_list *flist)
124{
125 if (do_progress) {
126 /* This overwrites the progress line */
127 rprintf(FINFO, "%d file%sto consider\n",
128 flist->count, flist->count == 1 ? " " : "s ");
129 } else
130 rprintf(FINFO, "done\n");
131}
132
133void show_flist_stats(void)
134{
135 /* Nothing yet */
136}
137
138static void list_file_entry(struct file_struct *f)
139{
140 char permbuf[PERMSTRING_SIZE];
141 double len;
142
143 if (!F_IS_ACTIVE(f)) {
144 /* this can happen if duplicate names were removed */
145 return;
146 }
147
148 permstring(permbuf, f->mode);
149 len = F_LENGTH(f);
150
151#ifdef SUPPORT_LINKS
152 if (preserve_links && S_ISLNK(f->mode)) {
153 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
154 permbuf, len, timestring(f->modtime),
155 f_name(f, NULL), F_SYMLINK(f));
156 } else
157#endif
158 {
159 rprintf(FINFO, "%s %11.0f %s %s\n",
160 permbuf, len, timestring(f->modtime),
161 f_name(f, NULL));
162 }
163}
164
165/* Stat either a symlink or its referent, depending on the settings of
166 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
167 *
168 * If path is the name of a symlink, then the linkbuf buffer (which must hold
169 * MAXPATHLEN chars) will be set to the symlink's target string.
170 *
171 * The stat structure pointed to by stp will contain information about the
172 * link or the referent as appropriate, if they exist. */
173static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
174{
175#ifdef SUPPORT_LINKS
176 if (link_stat(path, stp, copy_dirlinks) < 0)
177 return -1;
178 if (S_ISLNK(stp->st_mode)) {
179 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
180 if (llen < 0)
181 return -1;
182 linkbuf[llen] = '\0';
183 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
184 if (verbose > 1) {
185 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
186 path, linkbuf);
187 }
188 return do_stat(path, stp);
189 }
190 }
191 return 0;
192#else
193 return do_stat(path, stp);
194#endif
195}
196
197int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
198{
199#ifdef SUPPORT_LINKS
200 if (copy_links)
201 return do_stat(path, stp);
202 if (do_lstat(path, stp) < 0)
203 return -1;
204 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
205 STRUCT_STAT st;
206 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
207 *stp = st;
208 }
209 return 0;
210#else
211 return do_stat(path, stp);
212#endif
213}
214
215/* This function is used to check if a file should be included/excluded
216 * from the list of files based on its name and type etc. The value of
217 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
218static int is_excluded(char *fname, int is_dir, int filter_level)
219{
220#if 0 /* This currently never happens, so avoid a useless compare. */
221 if (filter_level == NO_FILTERS)
222 return 0;
223#endif
224 if (fname) {
225 /* never exclude '.', even if somebody does --exclude '*' */
226 if (fname[0] == '.' && !fname[1])
227 return 0;
228 /* Handle the -R version of the '.' dir. */
229 if (fname[0] == '/') {
230 int len = strlen(fname);
231 if (fname[len-1] == '.' && fname[len-2] == '/')
232 return 0;
233 }
234 }
235 if (server_filter_list.head
236 && check_filter(&server_filter_list, fname, is_dir) < 0)
237 return 1;
238 if (filter_level != ALL_FILTERS)
239 return 0;
240 if (filter_list.head
241 && check_filter(&filter_list, fname, is_dir) < 0)
242 return 1;
243 return 0;
244}
245
246static int to_wire_mode(mode_t mode)
247{
248#ifdef SUPPORT_LINKS
249#if _S_IFLNK != 0120000
250 if (S_ISLNK(mode))
251 return (mode & ~(_S_IFMT)) | 0120000;
252#endif
253#endif
254 return mode;
255}
256
257static mode_t from_wire_mode(int mode)
258{
259#if _S_IFLNK != 0120000
260 if ((mode & (_S_IFMT)) == 0120000)
261 return (mode & ~(_S_IFMT)) | _S_IFLNK;
262#endif
263 return mode;
264}
265
266static void send_directory(int f, struct file_list *flist,
267 char *fbuf, int len);
268
269static const char *flist_dir;
270static int flist_dir_len;
271
272
273/**
274 * Make sure @p flist is big enough to hold at least @p flist->count
275 * entries.
276 **/
277void flist_expand(struct file_list *flist)
278{
279 struct file_struct **new_ptr;
280
281 if (flist->count < flist->malloced)
282 return;
283
284 if (flist->malloced < FLIST_START)
285 flist->malloced = FLIST_START;
286 else if (flist->malloced >= FLIST_LINEAR)
287 flist->malloced += FLIST_LINEAR;
288 else
289 flist->malloced *= 2;
290
291 /*
292 * In case count jumped or we are starting the list
293 * with a known size just set it.
294 */
295 if (flist->malloced < flist->count)
296 flist->malloced = flist->count;
297
298 new_ptr = realloc_array(flist->files, struct file_struct *,
299 flist->malloced);
300
301 if (verbose >= 2 && flist->malloced != FLIST_START) {
302 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
303 who_am_i(),
304 (double)sizeof flist->files[0] * flist->malloced,
305 (new_ptr == flist->files) ? " not" : "");
306 }
307
308 flist->files = new_ptr;
309
310 if (!flist->files)
311 out_of_memory("flist_expand");
312}
313
314static void send_file_entry(struct file_struct *file, int f, int ndx)
315{
316 unsigned short flags;
317 static time_t modtime;
318 static mode_t mode;
319 static int64 dev;
320 static dev_t rdev;
321 static uint32 rdev_major;
322 static uid_t uid;
323 static gid_t gid;
324 static char lastname[MAXPATHLEN];
325 char fname[MAXPATHLEN];
326 int first_hlink_ndx = -1;
327 int l1, l2;
328
329 f_name(file, fname);
330
331 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
332
333 if (file->mode == mode)
334 flags |= XMIT_SAME_MODE;
335 else
336 mode = file->mode;
337 if ((preserve_devices && IS_DEVICE(mode))
338 || (preserve_specials && IS_SPECIAL(mode))) {
339 if (protocol_version < 28) {
340 if (tmp_rdev == rdev)
341 flags |= XMIT_SAME_RDEV_pre28;
342 else
343 rdev = tmp_rdev;
344 } else {
345 rdev = tmp_rdev;
346 if ((uint32)major(rdev) == rdev_major)
347 flags |= XMIT_SAME_RDEV_MAJOR;
348 else
349 rdev_major = major(rdev);
350 if ((uint32)minor(rdev) <= 0xFFu)
351 flags |= XMIT_RDEV_MINOR_IS_SMALL;
352 }
353 } else if (protocol_version < 28)
354 rdev = MAKEDEV(0, 0);
355 if (preserve_uid) {
356 if (F_UID(file) == uid)
357 flags |= XMIT_SAME_UID;
358 else
359 uid = F_UID(file);
360 }
361 if (preserve_gid) {
362 if (F_GID(file) == gid)
363 flags |= XMIT_SAME_GID;
364 else
365 gid = F_GID(file);
366 }
367 if (file->modtime == modtime)
368 flags |= XMIT_SAME_TIME;
369 else
370 modtime = file->modtime;
371
372#ifdef SUPPORT_HARD_LINKS
373 if (tmp_idev.dev != 0) {
374 if (protocol_version >= 30) {
375 struct idev_node *np = idev_node(tmp_idev.dev, tmp_idev.ino);
376 first_hlink_ndx = (int32)np->data - 1;
377 if (first_hlink_ndx < 0) {
378 np->data = (void*)(ndx + 1);
379 flags |= XMIT_HLINK_FIRST;
380 }
381 flags |= XMIT_HLINKED;
382 } else {
383 if (tmp_idev.dev == dev) {
384 if (protocol_version >= 28)
385 flags |= XMIT_SAME_DEV_pre30;
386 } else
387 dev = tmp_idev.dev;
388 flags |= XMIT_HLINKED;
389 }
390 }
391#endif
392
393 for (l1 = 0;
394 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
395 l1++) {}
396 l2 = strlen(fname+l1);
397
398 if (l1 > 0)
399 flags |= XMIT_SAME_NAME;
400 if (l2 > 255)
401 flags |= XMIT_LONG_NAME;
402
403 /* We must make sure we don't send a zero flag byte or the
404 * other end will terminate the flist transfer. Note that
405 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
406 * it's harmless way to add a bit to the first flag byte. */
407 if (protocol_version >= 28) {
408 if (!flags && !S_ISDIR(mode))
409 flags |= XMIT_TOP_DIR;
410 if ((flags & 0xFF00) || !flags) {
411 flags |= XMIT_EXTENDED_FLAGS;
412 write_shortint(f, flags);
413 } else
414 write_byte(f, flags);
415 } else {
416 if (!(flags & 0xFF))
417 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
418 write_byte(f, flags);
419 }
420 if (flags & XMIT_SAME_NAME)
421 write_byte(f, l1);
422 if (flags & XMIT_LONG_NAME)
423 write_int(f, l2);
424 else
425 write_byte(f, l2);
426 write_buf(f, fname + l1, l2);
427
428 if (first_hlink_ndx >= 0) {
429 write_int(f, first_hlink_ndx);
430 goto the_end;
431 }
432
433 write_longint(f, F_LENGTH(file));
434 if (!(flags & XMIT_SAME_TIME))
435 write_int(f, modtime);
436 if (!(flags & XMIT_SAME_MODE))
437 write_int(f, to_wire_mode(mode));
438 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
439 if (!numeric_ids)
440 add_uid(uid);
441 write_int(f, uid);
442 }
443 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
444 if (!numeric_ids)
445 add_gid(gid);
446 write_int(f, gid);
447 }
448 if ((preserve_devices && IS_DEVICE(mode))
449 || (preserve_specials && IS_SPECIAL(mode))) {
450 if (protocol_version < 28) {
451 if (!(flags & XMIT_SAME_RDEV_pre28))
452 write_int(f, (int)rdev);
453 } else {
454 if (!(flags & XMIT_SAME_RDEV_MAJOR))
455 write_int(f, major(rdev));
456 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
457 write_byte(f, minor(rdev));
458 else
459 write_int(f, minor(rdev));
460 }
461 }
462
463#ifdef SUPPORT_LINKS
464 if (preserve_links && S_ISLNK(mode)) {
465 const char *sl = F_SYMLINK(file);
466 int len = strlen(sl);
467 write_int(f, len);
468 write_buf(f, sl, len);
469 }
470#endif
471
472#ifdef SUPPORT_HARD_LINKS
473 if (tmp_idev.dev != 0 && protocol_version < 30) {
474 if (protocol_version < 26) {
475 /* 32-bit dev_t and ino_t */
476 write_int(f, (int32)dev);
477 write_int(f, (int32)tmp_idev.ino);
478 } else {
479 /* 64-bit dev_t and ino_t */
480 if (!(flags & XMIT_SAME_DEV_pre30))
481 write_longint(f, dev);
482 write_longint(f, tmp_idev.ino);
483 }
484 }
485#endif
486
487 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
488 const char *sum;
489 if (S_ISREG(mode))
490 sum = tmp_sum;
491 else {
492 /* Prior to 28, we sent a useless set of nulls. */
493 sum = empty_sum;
494 }
495 write_buf(f, sum, checksum_len);
496 }
497
498 the_end:
499 strlcpy(lastname, fname, MAXPATHLEN);
500}
501
502static struct file_struct *recv_file_entry(struct file_list *flist,
503 unsigned short flags, int f)
504{
505 static time_t modtime;
506 static mode_t mode;
507 static int64 dev;
508 static dev_t rdev;
509 static uint32 rdev_major;
510 static uid_t uid;
511 static gid_t gid;
512 static char lastname[MAXPATHLEN], *lastdir;
513 static int lastdir_depth, lastdir_len = -1;
514 static unsigned int del_hier_name_len = 0;
515 static int in_del_hier = 0;
516 char thisname[MAXPATHLEN];
517 unsigned int l1 = 0, l2 = 0;
518 int alloc_len, basename_len, dirname_len, linkname_len;
519 int extra_len = flist_extra_cnt * EXTRA_LEN;
520 int first_hlink_ndx = -1;
521 OFF_T file_length;
522 char *basename, *dirname, *bp;
523 struct file_struct *file;
524
525 if (!flist) {
526 modtime = 0, mode = 0;
527 dev = 0, rdev = MAKEDEV(0, 0);
528 rdev_major = 0;
529 uid = 0, gid = 0;
530 *lastname = '\0';
531 lastdir_len = -1;
532 in_del_hier = 0;
533 return NULL;
534 }
535
536 if (flags & XMIT_SAME_NAME)
537 l1 = read_byte(f);
538
539 if (flags & XMIT_LONG_NAME)
540 l2 = read_int(f);
541 else
542 l2 = read_byte(f);
543
544 if (l2 >= MAXPATHLEN - l1) {
545 rprintf(FERROR,
546 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
547 flags, l1, l2, lastname, who_am_i());
548 overflow_exit("recv_file_entry");
549 }
550
551 strlcpy(thisname, lastname, l1 + 1);
552 read_sbuf(f, &thisname[l1], l2);
553 thisname[l1 + l2] = 0;
554
555 strlcpy(lastname, thisname, MAXPATHLEN);
556
557 clean_fname(thisname, 0);
558
559 if (sanitize_paths)
560 sanitize_path(thisname, thisname, "", 0, NULL);
561
562 if ((basename = strrchr(thisname, '/')) != NULL) {
563 dirname_len = ++basename - thisname; /* counts future '\0' */
564 if (lastdir_len == dirname_len - 1
565 && strncmp(thisname, lastdir, lastdir_len) == 0) {
566 dirname = lastdir;
567 dirname_len = 0; /* indicates no copy is needed */
568 } else
569 dirname = thisname;
570 } else {
571 basename = thisname;
572 dirname = NULL;
573 dirname_len = 0;
574 }
575 basename_len = strlen(basename) + 1; /* count the '\0' */
576
577#ifdef SUPPORT_HARD_LINKS
578 if (protocol_version >= 30
579 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
580 struct file_struct *first;
581 first_hlink_ndx = read_int(f);
582 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
583 rprintf(FERROR,
584 "hard-link reference out of range: %d (%d)\n",
585 first_hlink_ndx, flist->count);
586 exit_cleanup(RERR_PROTOCOL);
587 }
588 first = flist->files[first_hlink_ndx];
589 file_length = F_LENGTH(first);
590 modtime = first->modtime;
591 mode = first->mode;
592 if (preserve_uid)
593 uid = F_UID(first);
594 if (preserve_gid)
595 gid = F_GID(first);
596 if ((preserve_devices && IS_DEVICE(mode))
597 || (preserve_specials && IS_SPECIAL(mode))) {
598 uint32 *devp = F_RDEV_P(first);
599 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
600 }
601 if (preserve_links && S_ISLNK(mode))
602 linkname_len = strlen(F_SYMLINK(first)) + 1;
603 else
604 linkname_len = 0;
605 goto create_object;
606 }
607#endif
608
609 file_length = read_longint(f);
610 if (!(flags & XMIT_SAME_TIME))
611 modtime = (time_t)read_int(f);
612 if (!(flags & XMIT_SAME_MODE))
613 mode = from_wire_mode(read_int(f));
614
615 if (chmod_modes && !S_ISLNK(mode))
616 mode = tweak_mode(mode, chmod_modes);
617
618 if (preserve_uid && !(flags & XMIT_SAME_UID))
619 uid = (uid_t)read_int(f);
620 if (preserve_gid && !(flags & XMIT_SAME_GID))
621 gid = (gid_t)read_int(f);
622
623 if ((preserve_devices && IS_DEVICE(mode))
624 || (preserve_specials && IS_SPECIAL(mode))) {
625 if (protocol_version < 28) {
626 if (!(flags & XMIT_SAME_RDEV_pre28))
627 rdev = (dev_t)read_int(f);
628 } else {
629 uint32 rdev_minor;
630 if (!(flags & XMIT_SAME_RDEV_MAJOR))
631 rdev_major = read_int(f);
632 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
633 rdev_minor = read_byte(f);
634 else
635 rdev_minor = read_int(f);
636 rdev = MAKEDEV(rdev_major, rdev_minor);
637 }
638 extra_len += 2 * EXTRA_LEN;
639 file_length = 0;
640 } else if (protocol_version < 28)
641 rdev = MAKEDEV(0, 0);
642
643#ifdef SUPPORT_LINKS
644 if (preserve_links && S_ISLNK(mode)) {
645 linkname_len = read_int(f) + 1; /* count the '\0' */
646 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
647 rprintf(FERROR, "overflow: linkname_len=%d\n",
648 linkname_len - 1);
649 overflow_exit("recv_file_entry");
650 }
651 }
652 else
653#endif
654 linkname_len = 0;
655
656#ifdef SUPPORT_HARD_LINKS
657 create_object:
658 if (preserve_hard_links) {
659 if (protocol_version < 28 && S_ISREG(mode))
660 flags |= XMIT_HLINKED;
661 if (flags & XMIT_HLINKED)
662 extra_len += EXTRA_LEN;
663 }
664#endif
665
666 if (always_checksum && S_ISREG(mode))
667 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
668
669 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
670 extra_len += EXTRA_LEN;
671
672#if EXTRA_ROUNDING > 0
673 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
674 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
675#endif
676
677 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
678 + linkname_len;
679 bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry");
680
681 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
682 bp += extra_len;
683 file = (struct file_struct *)bp;
684 bp += FILE_STRUCT_LEN;
685
686 memcpy(bp, basename, basename_len);
687 bp += basename_len + linkname_len; /* skip space for symlink too */
688
689#ifdef SUPPORT_HARD_LINKS
690 if (flags & XMIT_HLINKED)
691 file->flags |= FLAG_HLINKED;
692#endif
693 file->modtime = modtime;
694 file->len32 = (uint32)file_length;
695 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
696 file->flags |= FLAG_LENGTH64;
697 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
698 }
699 file->mode = mode;
700 if (preserve_uid)
701 F_UID(file) = uid;
702 if (preserve_gid)
703 F_GID(file) = gid;
704
705 if (dirname_len) {
706 file->dirname = lastdir = bp;
707 lastdir_len = dirname_len - 1;
708 memcpy(bp, dirname, dirname_len - 1);
709 bp += dirname_len;
710 bp[-1] = '\0';
711 lastdir_depth = count_dir_elements(lastdir);
712 file->dir.depth = lastdir_depth + 1;
713 } else if (dirname) {
714 file->dirname = dirname; /* we're reusing lastname */
715 file->dir.depth = lastdir_depth + 1;
716 } else
717 file->dir.depth = 1;
718
719 if (S_ISDIR(mode)) {
720 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
721 file->dir.depth--;
722 if (flags & XMIT_TOP_DIR) {
723 in_del_hier = recurse;
724 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
725 if (relative_paths && del_hier_name_len > 2
726 && lastname[del_hier_name_len-1] == '.'
727 && lastname[del_hier_name_len-2] == '/')
728 del_hier_name_len -= 2;
729 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
730 } else if (in_del_hier) {
731 if (!relative_paths || !del_hier_name_len
732 || (l1 >= del_hier_name_len
733 && lastname[del_hier_name_len] == '/'))
734 file->flags |= FLAG_XFER_DIR;
735 else
736 in_del_hier = 0;
737 }
738 }
739
740 if ((preserve_devices && IS_DEVICE(mode))
741 || (preserve_specials && IS_SPECIAL(mode))) {
742 uint32 *devp = F_RDEV_P(file);
743 DEV_MAJOR(devp) = major(rdev);
744 DEV_MINOR(devp) = minor(rdev);
745 }
746
747#ifdef SUPPORT_LINKS
748 if (linkname_len) {
749 bp = (char*)file->basename + basename_len;
750 if (first_hlink_ndx >= 0) {
751 struct file_struct *first = flist->files[first_hlink_ndx];
752 memcpy(bp, F_SYMLINK(first), linkname_len);
753 } else
754 read_sbuf(f, bp, linkname_len - 1);
755 if (sanitize_paths)
756 sanitize_path(bp, bp, "", lastdir_depth, NULL);
757 }
758#endif
759
760#ifdef SUPPORT_HARD_LINKS
761 if (preserve_hard_links && flags & XMIT_HLINKED) {
762 if (protocol_version >= 30) {
763 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
764 ? flist->count : first_hlink_ndx;
765 } else {
766 struct idev *idevp = pool_talloc(hlink_pool, struct idev,
767 1, "inode_table");
768 F_HL_IDEV(file) = idevp;
769 if (protocol_version < 26) {
770 idevp->dev = read_int(f);
771 idevp->ino = read_int(f);
772 } else {
773 if (!(flags & XMIT_SAME_DEV_pre30))
774 dev = read_longint(f);
775 idevp->dev = dev;
776 idevp->ino = read_longint(f);
777 }
778 }
779 }
780#endif
781
782 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
783 if (S_ISREG(mode))
784 bp = (char*)F_SUM(file);
785 else {
786 /* Prior to 28, we get a useless set of nulls. */
787 bp = tmp_sum;
788 }
789 if (first_hlink_ndx >= 0) {
790 struct file_struct *first = flist->files[first_hlink_ndx];
791 memcpy(bp, F_SUM(first), checksum_len);
792 } else
793 read_buf(f, bp, checksum_len);
794 }
795
796 return file;
797}
798
799/**
800 * Create a file_struct for a named file by reading its stat()
801 * information and performing extensive checks against global
802 * options.
803 *
804 * @return the new file, or NULL if there was an error or this file
805 * should be excluded.
806 *
807 * @todo There is a small optimization opportunity here to avoid
808 * stat()ing the file in some circumstances, which has a certain cost.
809 * We are called immediately after doing readdir(), and so we may
810 * already know the d_type of the file. We could for example avoid
811 * statting directories if we're not recursing, but this is not a very
812 * important case. Some systems may not have d_type.
813 **/
814struct file_struct *make_file(const char *fname, struct file_list *flist,
815 STRUCT_STAT *stp, int flags, int filter_level)
816{
817 static char *lastdir;
818 static int lastdir_len = -1;
819 struct file_struct *file;
820 STRUCT_STAT st;
821 char thisname[MAXPATHLEN];
822 char linkname[MAXPATHLEN];
823 int alloc_len, basename_len, dirname_len, linkname_len;
824 int extra_len = flist_extra_cnt * EXTRA_LEN;
825 char *basename, *dirname, *bp;
826
827 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
828 lastdir_len = -1;
829
830 if (strlcpy(thisname, fname, sizeof thisname)
831 >= sizeof thisname - flist_dir_len) {
832 rprintf(FINFO, "skipping overly long name: %s\n", fname);
833 return NULL;
834 }
835 clean_fname(thisname, 0);
836 if (sanitize_paths)
837 sanitize_path(thisname, thisname, "", 0, NULL);
838
839 if (stp && S_ISDIR(stp->st_mode)) {
840 st = *stp; /* Needed for "symlink/." with --relative. */
841 *linkname = '\0'; /* make IBM code checker happy */
842 } else if (readlink_stat(thisname, &st, linkname) != 0) {
843 int save_errno = errno;
844 /* See if file is excluded before reporting an error. */
845 if (filter_level != NO_FILTERS
846 && (is_excluded(thisname, 0, filter_level)
847 || is_excluded(thisname, 1, filter_level))) {
848 if (ignore_perishable && save_errno != ENOENT)
849 non_perishable_cnt++;
850 return NULL;
851 }
852 if (save_errno == ENOENT) {
853#ifdef SUPPORT_LINKS
854 /* Avoid "vanished" error if symlink points nowhere. */
855 if (copy_links && do_lstat(thisname, &st) == 0
856 && S_ISLNK(st.st_mode)) {
857 io_error |= IOERR_GENERAL;
858 rprintf(FERROR, "symlink has no referent: %s\n",
859 full_fname(thisname));
860 } else
861#endif
862 {
863 enum logcode c = am_daemon && protocol_version < 28
864 ? FERROR : FINFO;
865 io_error |= IOERR_VANISHED;
866 rprintf(c, "file has vanished: %s\n",
867 full_fname(thisname));
868 }
869 } else {
870 io_error |= IOERR_GENERAL;
871 rsyserr(FERROR, save_errno, "readlink %s failed",
872 full_fname(thisname));
873 }
874 return NULL;
875 }
876
877 /* backup.c calls us with filter_level set to NO_FILTERS. */
878 if (filter_level == NO_FILTERS)
879 goto skip_filters;
880
881 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
882 rprintf(FINFO, "skipping directory %s\n", thisname);
883 return NULL;
884 }
885
886 /* -x only affects directories because we need to avoid recursing
887 * into a mount-point directory, not to avoid copying a symlinked
888 * file if -L (or similar) was specified. */
889 if (one_file_system && st.st_dev != filesystem_dev
890 && S_ISDIR(st.st_mode)) {
891 if (one_file_system > 1) {
892 if (verbose > 2) {
893 rprintf(FINFO, "skipping mount-point dir %s\n",
894 thisname);
895 }
896 return NULL;
897 }
898 flags |= FLAG_MOUNT_DIR;
899 }
900
901 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
902 if (ignore_perishable)
903 non_perishable_cnt++;
904 return NULL;
905 }
906
907 if (lp_ignore_nonreadable(module_id)) {
908#ifdef SUPPORT_LINKS
909 if (!S_ISLNK(st.st_mode))
910#endif
911 if (access(thisname, R_OK) != 0)
912 return NULL;
913 }
914
915 skip_filters:
916
917 if (verbose > 2) {
918 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
919 who_am_i(), thisname, filter_level);
920 }
921
922 if ((basename = strrchr(thisname, '/')) != NULL) {
923 dirname_len = ++basename - thisname; /* counts future '\0' */
924 if (lastdir_len == dirname_len - 1
925 && strncmp(thisname, lastdir, lastdir_len) == 0) {
926 dirname = lastdir;
927 dirname_len = 0; /* indicates no copy is needed */
928 } else
929 dirname = thisname;
930 } else {
931 basename = thisname;
932 dirname = NULL;
933 dirname_len = 0;
934 }
935 basename_len = strlen(basename) + 1; /* count the '\0' */
936
937#ifdef SUPPORT_LINKS
938 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
939#else
940 linkname_len = 0;
941#endif
942
943 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
944 extra_len += EXTRA_LEN;
945
946#if EXTRA_ROUNDING > 0
947 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
948 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
949#endif
950
951 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len + dirname_len
952 + linkname_len;
953 if (flist)
954 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
955 else {
956 if (!(bp = new_array(char, alloc_len)))
957 out_of_memory("make_file");
958 }
959
960 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
961 bp += extra_len;
962 file = (struct file_struct *)bp;
963 bp += FILE_STRUCT_LEN;
964
965 memcpy(bp, basename, basename_len);
966 bp += basename_len + linkname_len; /* skip space for symlink too */
967
968#ifdef SUPPORT_HARD_LINKS
969 if (preserve_hard_links && flist) {
970 if (protocol_version >= 28
971 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
972 : S_ISREG(st.st_mode)) {
973 tmp_idev.dev = st.st_dev;
974 tmp_idev.ino = st.st_ino;
975 } else
976 tmp_idev.dev = tmp_idev.ino = 0;
977 }
978#endif
979
980#ifdef HAVE_STRUCT_STAT_ST_RDEV
981 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
982 tmp_rdev = st.st_rdev;
983 st.st_size = 0;
984 }
985#endif
986
987 file->flags = flags;
988 file->modtime = st.st_mtime;
989 file->len32 = (uint32)st.st_size;
990 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
991 file->flags |= FLAG_LENGTH64;
992 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
993 }
994 file->mode = st.st_mode;
995 if (preserve_uid)
996 F_UID(file) = st.st_uid;
997 if (preserve_gid)
998 F_GID(file) = st.st_gid;
999
1000 if (dirname_len) {
1001 file->dirname = lastdir = bp;
1002 lastdir_len = dirname_len - 1;
1003 memcpy(bp, dirname, dirname_len - 1);
1004 bp += dirname_len;
1005 bp[-1] = '\0';
1006 } else if (dirname)
1007 file->dirname = dirname;
1008
1009#ifdef SUPPORT_LINKS
1010 if (linkname_len) {
1011 bp = (char*)file->basename + basename_len;
1012 memcpy(bp, linkname, linkname_len);
1013 }
1014#endif
1015
1016 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1017 file_checksum(thisname, tmp_sum, st.st_size);
1018
1019 file->dir.root = flist_dir;
1020
1021 /* This code is only used by the receiver when it is building
1022 * a list of files for a delete pass. */
1023 if (keep_dirlinks && linkname_len && flist) {
1024 STRUCT_STAT st2;
1025 int save_mode = file->mode;
1026 file->mode = S_IFDIR; /* Find a directory with our name. */
1027 if (flist_find(the_file_list, file) >= 0
1028 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
1029 file->modtime = st2.st_mtime;
1030 file->len32 = 0;
1031 file->mode = st2.st_mode;
1032 if (preserve_uid)
1033 F_UID(file) = st2.st_uid;
1034 if (preserve_gid)
1035 F_GID(file) = st2.st_gid;
1036 } else
1037 file->mode = save_mode;
1038 }
1039
1040 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
1041 stats.total_size += st.st_size;
1042
1043 if (basename_len == 0+1)
1044 return NULL;
1045
1046 return file;
1047}
1048
1049/* Only called for temporary file_struct entries created by make_file(). */
1050void unmake_file(struct file_struct *file)
1051{
1052 int extra_cnt = flist_extra_cnt + LEN64_BUMP(file);
1053#if EXTRA_ROUNDING > 0
1054 if (extra_cnt & EXTRA_ROUNDING)
1055 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1056#endif
1057 free(REQ_EXTRA(file, extra_cnt));
1058}
1059
1060static struct file_struct *send_file_name(int f, struct file_list *flist,
1061 char *fname, STRUCT_STAT *stp,
1062 unsigned short flags)
1063{
1064 struct file_struct *file;
1065
1066 file = make_file(fname, flist, stp, flags,
1067 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1068 if (!file)
1069 return NULL;
1070
1071 if (chmod_modes && !S_ISLNK(file->mode))
1072 file->mode = tweak_mode(file->mode, chmod_modes);
1073
1074 maybe_emit_filelist_progress(flist->count + flist_count_offset);
1075
1076 flist_expand(flist);
1077 flist->files[flist->count++] = file;
1078 if (f >= 0)
1079 send_file_entry(file, f, flist->count - 1);
1080 return file;
1081}
1082
1083static void send_if_directory(int f, struct file_list *flist,
1084 struct file_struct *file,
1085 char *fbuf, unsigned int ol)
1086{
1087 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1088
1089 if (S_ISDIR(file->mode)
1090 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1091 void *save_filters;
1092 unsigned int len = strlen(fbuf);
1093 if (len > 1 && fbuf[len-1] == '/')
1094 fbuf[--len] = '\0';
1095 if (len >= MAXPATHLEN - 1) {
1096 io_error |= IOERR_GENERAL;
1097 rprintf(FERROR, "skipping long-named directory: %s\n",
1098 full_fname(fbuf));
1099 return;
1100 }
1101 save_filters = push_local_filters(fbuf, len);
1102 send_directory(f, flist, fbuf, len);
1103 pop_local_filters(save_filters);
1104 fbuf[ol] = '\0';
1105 if (is_dot_dir)
1106 fbuf[ol-1] = '.';
1107 }
1108}
1109
1110/* This function is normally called by the sender, but the receiving side also
1111 * calls it from get_dirlist() with f set to -1 so that we just construct the
1112 * file list in memory without sending it over the wire. Also, get_dirlist()
1113 * might call this with f set to -2, which also indicates that local filter
1114 * rules should be ignored. */
1115static void send_directory(int f, struct file_list *flist, char *fbuf, int len)
1116{
1117 struct dirent *di;
1118 unsigned remainder;
1119 char *p;
1120 DIR *d;
1121 int start = flist->count;
1122
1123 if (!(d = opendir(fbuf))) {
1124 io_error |= IOERR_GENERAL;
1125 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1126 return;
1127 }
1128
1129 p = fbuf + len;
1130 if (len != 1 || *fbuf != '/')
1131 *p++ = '/';
1132 *p = '\0';
1133 remainder = MAXPATHLEN - (p - fbuf);
1134
1135 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1136 char *dname = d_name(di);
1137 if (dname[0] == '.' && (dname[1] == '\0'
1138 || (dname[1] == '.' && dname[2] == '\0')))
1139 continue;
1140 if (strlcpy(p, dname, remainder) >= remainder) {
1141 io_error |= IOERR_GENERAL;
1142 rprintf(FINFO,
1143 "cannot send long-named file %s\n",
1144 full_fname(fbuf));
1145 continue;
1146 }
1147
1148 send_file_name(f, flist, fbuf, NULL, 0);
1149 }
1150
1151 fbuf[len] = '\0';
1152
1153 if (errno) {
1154 io_error |= IOERR_GENERAL;
1155 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1156 }
1157
1158 closedir(d);
1159
1160 if (recurse) {
1161 int i, end = flist->count - 1;
1162 /* send_if_directory() bumps flist->count, so use "end". */
1163 for (i = start; i <= end; i++)
1164 send_if_directory(f, flist, flist->files[i], fbuf, len);
1165 }
1166}
1167
1168struct file_list *send_file_list(int f, int argc, char *argv[])
1169{
1170 int len;
1171 STRUCT_STAT st;
1172 char *p, *dir, olddir[sizeof curr_dir];
1173 char lastpath[MAXPATHLEN] = "";
1174 struct file_list *flist;
1175 struct timeval start_tv, end_tv;
1176 int64 start_write;
1177 int use_ff_fd = 0;
1178
1179 rprintf(FLOG, "building file list\n");
1180 if (show_filelist_p())
1181 start_filelist_progress("building file list");
1182
1183 start_write = stats.total_written;
1184 gettimeofday(&start_tv, NULL);
1185
1186#ifdef SUPPORT_HARD_LINKS
1187 if (preserve_hard_links && protocol_version >= 30)
1188 init_hard_links();
1189#endif
1190
1191 flist = flist_new("send_file_list");
1192
1193 io_start_buffering_out();
1194 if (filesfrom_fd >= 0) {
1195 if (argv[0] && !push_dir(argv[0], 0)) {
1196 rsyserr(FERROR, errno, "push_dir %s failed",
1197 full_fname(argv[0]));
1198 exit_cleanup(RERR_FILESELECT);
1199 }
1200 use_ff_fd = 1;
1201 }
1202
1203 while (1) {
1204 char fbuf[MAXPATHLEN];
1205 char *fn;
1206 int is_dot_dir;
1207
1208 if (use_ff_fd) {
1209 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1210 break;
1211 sanitize_path(fbuf, fbuf, "", 0, NULL);
1212 } else {
1213 if (argc-- == 0)
1214 break;
1215 strlcpy(fbuf, *argv++, MAXPATHLEN);
1216 if (sanitize_paths)
1217 sanitize_path(fbuf, fbuf, "", 0, NULL);
1218 }
1219
1220 len = strlen(fbuf);
1221 if (relative_paths) {
1222 /* We clean up fbuf below. */
1223 is_dot_dir = 0;
1224 } else if (!len || fbuf[len - 1] == '/') {
1225 if (len == 2 && fbuf[0] == '.') {
1226 /* Turn "./" into just "." rather than "./." */
1227 fbuf[1] = '\0';
1228 } else {
1229 if (len + 1 >= MAXPATHLEN)
1230 overflow_exit("send_file_list");
1231 fbuf[len++] = '.';
1232 fbuf[len] = '\0';
1233 }
1234 is_dot_dir = 1;
1235 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1236 && (len == 2 || fbuf[len-3] == '/')) {
1237 if (len + 2 >= MAXPATHLEN)
1238 overflow_exit("send_file_list");
1239 fbuf[len++] = '/';
1240 fbuf[len++] = '.';
1241 fbuf[len] = '\0';
1242 is_dot_dir = 1;
1243 } else {
1244 is_dot_dir = fbuf[len-1] == '.'
1245 && (len == 1 || fbuf[len-2] == '/');
1246 }
1247
1248 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1249 io_error |= IOERR_GENERAL;
1250 rsyserr(FERROR, errno, "link_stat %s failed",
1251 full_fname(fbuf));
1252 continue;
1253 }
1254
1255 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1256 rprintf(FINFO, "skipping directory %s\n", fbuf);
1257 continue;
1258 }
1259
1260 dir = NULL;
1261 olddir[0] = '\0';
1262
1263 if (!relative_paths) {
1264 p = strrchr(fbuf, '/');
1265 if (p) {
1266 *p = '\0';
1267 if (p == fbuf)
1268 dir = "/";
1269 else
1270 dir = fbuf;
1271 len -= p - fbuf + 1;
1272 fn = p + 1;
1273 } else
1274 fn = fbuf;
1275 } else {
1276 if ((p = strstr(fbuf, "/./")) != NULL) {
1277 *p = '\0';
1278 if (p == fbuf)
1279 dir = "/";
1280 else
1281 dir = fbuf;
1282 len -= p - fbuf + 3;
1283 fn = p + 3;
1284 } else
1285 fn = fbuf;
1286 /* Get rid of trailing "/" and "/.". */
1287 while (len) {
1288 if (fn[len - 1] == '/') {
1289 is_dot_dir = 1;
1290 if (!--len && !dir) {
1291 len++;
1292 break;
1293 }
1294 }
1295 else if (len >= 2 && fn[len - 1] == '.'
1296 && fn[len - 2] == '/') {
1297 is_dot_dir = 1;
1298 if (!(len -= 2) && !dir) {
1299 len++;
1300 break;
1301 }
1302 } else
1303 break;
1304 }
1305 if (len == 1 && fn[0] == '/')
1306 fn[len++] = '.';
1307 fn[len] = '\0';
1308 /* Reject a ".." dir in the active part of the path. */
1309 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1310 if ((p[2] == '/' || p[2] == '\0')
1311 && (p == fn || p[-1] == '/')) {
1312 rprintf(FERROR,
1313 "found \"..\" dir in relative path: %s\n",
1314 fbuf);
1315 exit_cleanup(RERR_SYNTAX);
1316 }
1317 }
1318 }
1319
1320 if (!*fn) {
1321 len = 1;
1322 fn = ".";
1323 }
1324
1325 if (dir && *dir) {
1326 static const char *lastdir;
1327 static int lastdir_len;
1328
1329 strlcpy(olddir, curr_dir, sizeof olddir);
1330
1331 if (!push_dir(dir, 0)) {
1332 io_error |= IOERR_GENERAL;
1333 rsyserr(FERROR, errno, "push_dir %s failed",
1334 full_fname(dir));
1335 continue;
1336 }
1337
1338 if (lastdir && strcmp(lastdir, dir) == 0) {
1339 flist_dir = lastdir;
1340 flist_dir_len = lastdir_len;
1341 } else {
1342 flist_dir = lastdir = strdup(dir);
1343 flist_dir_len = lastdir_len = strlen(dir);
1344 }
1345 }
1346
1347 if (fn != fbuf)
1348 memmove(fbuf, fn, len + 1);
1349
1350 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1351 /* Send the implied directories at the start of the
1352 * source spec, so we get their permissions right. */
1353 char *lp = lastpath, *slash = fbuf;
1354 *p = '\0';
1355 /* Skip any initial directories in our path that we
1356 * have in common with lastpath. */
1357 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1358 if (*fn == '/')
1359 slash = fn;
1360 }
1361 *p = '/';
1362 if (fn != p || (*lp && *lp != '/')) {
1363 int save_copy_links = copy_links;
1364 int save_xfer_dirs = xfer_dirs;
1365 copy_links |= copy_unsafe_links;
1366 xfer_dirs = 1;
1367 while ((slash = strchr(slash+1, '/')) != 0) {
1368 *slash = '\0';
1369 send_file_name(f, flist, fbuf, NULL, 0);
1370 *slash = '/';
1371 }
1372 copy_links = save_copy_links;
1373 xfer_dirs = save_xfer_dirs;
1374 *p = '\0';
1375 strlcpy(lastpath, fbuf, sizeof lastpath);
1376 *p = '/';
1377 }
1378 }
1379
1380 if (one_file_system)
1381 filesystem_dev = st.st_dev;
1382
1383 if (recurse || (xfer_dirs && is_dot_dir)) {
1384 struct file_struct *file;
1385 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1386 if (file)
1387 send_if_directory(f, flist, file, fbuf, len);
1388 } else
1389 send_file_name(f, flist, fbuf, &st, 0);
1390
1391 if (olddir[0]) {
1392 flist_dir = NULL;
1393 flist_dir_len = 0;
1394 if (!pop_dir(olddir)) {
1395 rsyserr(FERROR, errno, "pop_dir %s failed",
1396 full_fname(olddir));
1397 exit_cleanup(RERR_FILESELECT);
1398 }
1399 }
1400 }
1401
1402 gettimeofday(&end_tv, NULL);
1403 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1404 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1405 if (stats.flist_buildtime == 0)
1406 stats.flist_buildtime = 1;
1407 start_tv = end_tv;
1408
1409 write_byte(f, 0); /* Indicate end of file list */
1410
1411#ifdef SUPPORT_HARD_LINKS
1412 if (preserve_hard_links && protocol_version >= 30)
1413 idev_destroy();
1414#endif
1415
1416 if (show_filelist_p())
1417 finish_filelist_progress(flist);
1418
1419 gettimeofday(&end_tv, NULL);
1420 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1421 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1422
1423 /* Sort the list without removing any duplicates. This allows the
1424 * receiving side to ask for any name they like, which gives us the
1425 * flexibility to change the way we unduplicate names in the future
1426 * without causing a compatibility problem with older versions. */
1427 clean_flist(flist, 0, 0);
1428
1429 if (!numeric_ids)
1430 send_uid_list(f);
1431
1432 /* send the io_error flag */
1433 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1434
1435 io_end_buffering();
1436 stats.flist_size = stats.total_written - start_write;
1437 stats.num_files = flist->count;
1438
1439 if (verbose > 3)
1440 output_flist(flist);
1441
1442 if (verbose > 2)
1443 rprintf(FINFO, "send_file_list done\n");
1444
1445 return flist;
1446}
1447
1448struct file_list *recv_file_list(int f)
1449{
1450 struct file_list *flist;
1451 unsigned short flags;
1452 int64 start_read;
1453
1454 rprintf(FLOG, "receiving file list\n");
1455 if (show_filelist_p())
1456 start_filelist_progress("receiving file list");
1457
1458 start_read = stats.total_read;
1459
1460 flist = flist_new("recv_file_list");
1461
1462#ifdef SUPPORT_HARD_LINKS
1463 if (preserve_hard_links && protocol_version < 30)
1464 init_hard_links();
1465#endif
1466
1467 while ((flags = read_byte(f)) != 0) {
1468 struct file_struct *file;
1469
1470 flist_expand(flist);
1471
1472 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1473 flags |= read_byte(f) << 8;
1474 file = recv_file_entry(flist, flags, f);
1475
1476 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1477 stats.total_size += F_LENGTH(file);
1478
1479 flist->files[flist->count++] = file;
1480
1481 maybe_emit_filelist_progress(flist->count);
1482
1483 if (verbose > 2) {
1484 rprintf(FINFO, "recv_file_name(%s)\n",
1485 f_name(file, NULL));
1486 }
1487 }
1488 recv_file_entry(NULL, 0, 0); /* Signal that we're done. */
1489
1490 if (verbose > 2)
1491 rprintf(FINFO, "received %d names\n", flist->count);
1492
1493 if (show_filelist_p())
1494 finish_filelist_progress(flist);
1495
1496 clean_flist(flist, relative_paths, 1);
1497
1498 if (f >= 0) {
1499 recv_uid_list(f, flist);
1500
1501 /* Recv the io_error flag */
1502 if (lp_ignore_errors(module_id) || ignore_errors)
1503 read_int(f);
1504 else
1505 io_error |= read_int(f);
1506 }
1507
1508 if (verbose > 3)
1509 output_flist(flist);
1510
1511 if (list_only) {
1512 int i;
1513 for (i = 0; i < flist->count; i++)
1514 list_file_entry(flist->files[i]);
1515 }
1516
1517 if (verbose > 2)
1518 rprintf(FINFO, "recv_file_list done\n");
1519
1520 stats.flist_size = stats.total_read - start_read;
1521 stats.num_files = flist->count;
1522
1523 return flist;
1524}
1525
1526static int file_compare(struct file_struct **file1, struct file_struct **file2)
1527{
1528 return f_name_cmp(*file1, *file2);
1529}
1530
1531/* Search for an identically-named item in the file list. Note that the
1532 * items must agree in their directory-ness, or no match is returned. */
1533int flist_find(struct file_list *flist, struct file_struct *f)
1534{
1535 int low = flist->low, high = flist->high;
1536 int diff, mid, mid_up;
1537
1538 while (low <= high) {
1539 mid = (low + high) / 2;
1540 if (F_IS_ACTIVE(flist->files[mid]))
1541 mid_up = mid;
1542 else {
1543 /* Scan for the next non-empty entry using the cached
1544 * distance values. If the value isn't fully up-to-
1545 * date, update it. */
1546 mid_up = mid + flist->files[mid]->dir.depth;
1547 if (!F_IS_ACTIVE(flist->files[mid_up])) {
1548 do {
1549 mid_up += flist->files[mid_up]->dir.depth;
1550 } while (!F_IS_ACTIVE(flist->files[mid_up]));
1551 flist->files[mid]->dir.depth = mid_up - mid;
1552 }
1553 if (mid_up > high) {
1554 /* If there's nothing left above us, set high to
1555 * a non-empty entry below us and continue. */
1556 high = mid - (int)flist->files[mid]->len32;
1557 if (!F_IS_ACTIVE(flist->files[high])) {
1558 do {
1559 high -= (int)flist->files[high]->len32;
1560 } while (!F_IS_ACTIVE(flist->files[high]));
1561 flist->files[mid]->len32 = mid - high;
1562 }
1563 continue;
1564 }
1565 }
1566 diff = f_name_cmp(flist->files[mid_up], f);
1567 if (diff == 0) {
1568 if (protocol_version < 29
1569 && S_ISDIR(flist->files[mid_up]->mode)
1570 != S_ISDIR(f->mode))
1571 return -1;
1572 return mid_up;
1573 }
1574 if (diff < 0)
1575 low = mid_up + 1;
1576 else
1577 high = mid - 1;
1578 }
1579 return -1;
1580}
1581
1582/*
1583 * Free up any resources a file_struct has allocated
1584 * and clear the file.
1585 */
1586void clear_file(struct file_struct *file)
1587{
1588 /* The +1 zeros out the first char of the basename. */
1589 memset(file, 0, FILE_STRUCT_LEN + 1);
1590 /* In an empty entry, dir.depth is an offset to the next non-empty
1591 * entry. Likewise for len32 in the opposite direction. We assume
1592 * that we're alone for now since flist_find() will adjust the counts
1593 * it runs into that aren't up-to-date. */
1594 file->len32 = file->dir.depth = 1;
1595}
1596
1597/* Allocate a new file list. */
1598struct file_list *flist_new(char *msg)
1599{
1600 struct file_list *flist;
1601
1602 flist = new(struct file_list);
1603 if (!flist)
1604 out_of_memory(msg);
1605
1606 memset(flist, 0, sizeof flist[0]);
1607
1608 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
1609 out_of_memory(msg);
1610
1611 return flist;
1612}
1613
1614/* Free up all elements in a flist. */
1615void flist_free(struct file_list *flist)
1616{
1617 pool_destroy(flist->file_pool);
1618 free(flist->files);
1619 free(flist);
1620}
1621
1622/*
1623 * This routine ensures we don't have any duplicate names in our file list.
1624 * duplicate names can cause corruption because of the pipelining
1625 */
1626static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1627{
1628 char fbuf[MAXPATHLEN];
1629 int i, prev_i = 0;
1630
1631 if (!flist)
1632 return;
1633 if (flist->count == 0) {
1634 flist->high = -1;
1635 return;
1636 }
1637
1638 qsort(flist->files, flist->count,
1639 sizeof flist->files[0], (int (*)())file_compare);
1640
1641 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1642 if (F_IS_ACTIVE(flist->files[i])) {
1643 prev_i = i;
1644 break;
1645 }
1646 }
1647 flist->low = prev_i;
1648 while (++i < flist->count) {
1649 int j;
1650 struct file_struct *file = flist->files[i];
1651
1652 if (!F_IS_ACTIVE(file))
1653 continue;
1654 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1655 j = prev_i;
1656 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1657 int save_mode = file->mode;
1658 /* Make sure that this directory doesn't duplicate a
1659 * non-directory earlier in the list. */
1660 flist->high = prev_i;
1661 file->mode = S_IFREG;
1662 j = flist_find(flist, file);
1663 file->mode = save_mode;
1664 } else
1665 j = -1;
1666 if (j >= 0) {
1667 struct file_struct *fp = flist->files[j];
1668 int keep, drop;
1669 /* If one is a dir and the other is not, we want to
1670 * keep the dir because it might have contents in the
1671 * list. */
1672 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1673 if (S_ISDIR(file->mode))
1674 keep = i, drop = j;
1675 else
1676 keep = j, drop = i;
1677 } else
1678 keep = j, drop = i;
1679 if (verbose > 1 && !am_server) {
1680 rprintf(FINFO,
1681 "removing duplicate name %s from file list (%d)\n",
1682 f_name(file, fbuf), drop);
1683 }
1684 /* Make sure we don't lose track of a user-specified
1685 * top directory. */
1686 flist->files[keep]->flags |= flist->files[drop]->flags
1687 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
1688
1689 clear_file(flist->files[drop]);
1690
1691 if (keep == i) {
1692 if (flist->low == drop) {
1693 for (j = drop + 1;
1694 j < i && !F_IS_ACTIVE(flist->files[j]);
1695 j++) {}
1696 flist->low = j;
1697 }
1698 prev_i = i;
1699 }
1700 } else
1701 prev_i = i;
1702 }
1703 flist->high = no_dups ? prev_i : flist->count - 1;
1704
1705 if (strip_root) {
1706 /* We need to strip off the leading slashes for relative
1707 * paths, but this must be done _after_ the sorting phase. */
1708 for (i = flist->low; i <= flist->high; i++) {
1709 struct file_struct *file = flist->files[i];
1710
1711 if (!file->dirname)
1712 continue;
1713 while (*file->dirname == '/')
1714 file->dirname++;
1715 if (!*file->dirname)
1716 file->dirname = NULL;
1717 }
1718 }
1719
1720 if (prune_empty_dirs && no_dups) {
1721 int j, prev_depth = 0;
1722
1723 prev_i = 0; /* It's OK that this isn't really true. */
1724
1725 for (i = flist->low; i <= flist->high; i++) {
1726 struct file_struct *fp, *file = flist->files[i];
1727
1728 /* This temporarily abuses the dir.depth value for a
1729 * directory that is in a chain that might get pruned.
1730 * We restore the old value if it gets a reprieve. */
1731 if (S_ISDIR(file->mode) && file->dir.depth) {
1732 /* Dump empty dirs when coming back down. */
1733 for (j = prev_depth; j >= file->dir.depth; j--) {
1734 fp = flist->files[prev_i];
1735 if (fp->dir.depth >= 0)
1736 break;
1737 prev_i = -fp->dir.depth-1;
1738 clear_file(fp);
1739 }
1740 prev_depth = file->dir.depth;
1741 if (is_excluded(f_name(file, fbuf), 1,
1742 ALL_FILTERS)) {
1743 /* Keep dirs through this dir. */
1744 for (j = prev_depth-1; ; j--) {
1745 fp = flist->files[prev_i];
1746 if (fp->dir.depth >= 0)
1747 break;
1748 prev_i = -fp->dir.depth-1;
1749 fp->dir.depth = j;
1750 }
1751 } else
1752 file->dir.depth = -prev_i-1;
1753 prev_i = i;
1754 } else {
1755 /* Keep dirs through this non-dir. */
1756 for (j = prev_depth; ; j--) {
1757 fp = flist->files[prev_i];
1758 if (fp->dir.depth >= 0)
1759 break;
1760 prev_i = -fp->dir.depth-1;
1761 fp->dir.depth = j;
1762 }
1763 }
1764 }
1765 /* Dump empty all remaining empty dirs. */
1766 while (1) {
1767 struct file_struct *fp = flist->files[prev_i];
1768 if (fp->dir.depth >= 0)
1769 break;
1770 prev_i = -fp->dir.depth-1;
1771 clear_file(fp);
1772 }
1773
1774 for (i = flist->low; i <= flist->high; i++) {
1775 if (F_IS_ACTIVE(flist->files[i]))
1776 break;
1777 }
1778 flist->low = i;
1779 for (i = flist->high; i >= flist->low; i--) {
1780 if (F_IS_ACTIVE(flist->files[i]))
1781 break;
1782 }
1783 flist->high = i;
1784 }
1785}
1786
1787static void output_flist(struct file_list *flist)
1788{
1789 char uidbuf[16], gidbuf[16], depthbuf[16];
1790 struct file_struct *file;
1791 const char *dir, *slash, *name, *trail;
1792 const char *who = who_am_i();
1793 int i;
1794
1795 for (i = 0; i < flist->count; i++) {
1796 file = flist->files[i];
1797 if ((am_root || am_sender) && preserve_uid) {
1798 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
1799 (long)F_UID(file));
1800 } else
1801 *uidbuf = '\0';
1802 if (preserve_gid && F_GID(file) != GID_NONE) {
1803 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
1804 (long)F_GID(file));
1805 } else
1806 *gidbuf = '\0';
1807 if (!am_sender)
1808 snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
1809 if (F_IS_ACTIVE(file)) {
1810 if ((dir = file->dirname) == NULL)
1811 dir = slash = "";
1812 else
1813 slash = "/";
1814 name = file->basename;
1815 trail = S_ISDIR(file->mode) ? "/" : "";
1816 } else
1817 dir = slash = name = trail = "";
1818 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1819 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1820 dir, slash, name, trail, (int)file->mode,
1821 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
1822 }
1823}
1824
1825enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1826enum fnc_type { t_PATH, t_ITEM };
1827
1828/* Compare the names of two file_struct entities, similar to how strcmp()
1829 * would do if it were operating on the joined strings.
1830 *
1831 * Some differences beginning with protocol_version 29: (1) directory names
1832 * are compared with an assumed trailing slash so that they compare in a
1833 * way that would cause them to sort immediately prior to any content they
1834 * may have; (2) a directory of any name compares after a non-directory of
1835 * any name at the same depth; (3) a directory with name "." compares prior
1836 * to anything else. These changes mean that a directory and a non-dir
1837 * with the same name will not compare as equal (protocol_version >= 29).
1838 *
1839 * The dirname component can be an empty string, but the basename component
1840 * cannot (and never is in the current codebase). The basename component
1841 * may be NULL (for a removed item), in which case it is considered to be
1842 * after any existing item. */
1843int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1844{
1845 int dif;
1846 const uchar *c1, *c2;
1847 enum fnc_state state1, state2;
1848 enum fnc_type type1, type2;
1849 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1850
1851 if (!f1 || !F_IS_ACTIVE(f1)) {
1852 if (!f2 || !F_IS_ACTIVE(f2))
1853 return 0;
1854 return -1;
1855 }
1856 if (!f2 || !F_IS_ACTIVE(f2))
1857 return 1;
1858
1859 c1 = (uchar*)f1->dirname;
1860 c2 = (uchar*)f2->dirname;
1861 if (c1 == c2)
1862 c1 = c2 = NULL;
1863 if (!c1) {
1864 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1865 c1 = (const uchar*)f1->basename;
1866 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1867 type1 = t_ITEM;
1868 state1 = s_TRAILING;
1869 c1 = (uchar*)"";
1870 } else
1871 state1 = s_BASE;
1872 } else {
1873 type1 = t_path;
1874 state1 = s_DIR;
1875 }
1876 if (!c2) {
1877 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1878 c2 = (const uchar*)f2->basename;
1879 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1880 type2 = t_ITEM;
1881 state2 = s_TRAILING;
1882 c2 = (uchar*)"";
1883 } else
1884 state2 = s_BASE;
1885 } else {
1886 type2 = t_path;
1887 state2 = s_DIR;
1888 }
1889
1890 if (type1 != type2)
1891 return type1 == t_PATH ? 1 : -1;
1892
1893 do {
1894 if (!*c1) {
1895 switch (state1) {
1896 case s_DIR:
1897 state1 = s_SLASH;
1898 c1 = (uchar*)"/";
1899 break;
1900 case s_SLASH:
1901 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1902 c1 = (const uchar*)f1->basename;
1903 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1904 type1 = t_ITEM;
1905 state1 = s_TRAILING;
1906 c1 = (uchar*)"";
1907 } else
1908 state1 = s_BASE;
1909 break;
1910 case s_BASE:
1911 state1 = s_TRAILING;
1912 if (type1 == t_PATH) {
1913 c1 = (uchar*)"/";
1914 break;
1915 }
1916 /* FALL THROUGH */
1917 case s_TRAILING:
1918 type1 = t_ITEM;
1919 break;
1920 }
1921 if (*c2 && type1 != type2)
1922 return type1 == t_PATH ? 1 : -1;
1923 }
1924 if (!*c2) {
1925 switch (state2) {
1926 case s_DIR:
1927 state2 = s_SLASH;
1928 c2 = (uchar*)"/";
1929 break;
1930 case s_SLASH:
1931 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1932 c2 = (const uchar*)f2->basename;
1933 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1934 type2 = t_ITEM;
1935 state2 = s_TRAILING;
1936 c2 = (uchar*)"";
1937 } else
1938 state2 = s_BASE;
1939 break;
1940 case s_BASE:
1941 state2 = s_TRAILING;
1942 if (type2 == t_PATH) {
1943 c2 = (uchar*)"/";
1944 break;
1945 }
1946 /* FALL THROUGH */
1947 case s_TRAILING:
1948 if (!*c1)
1949 return 0;
1950 type2 = t_ITEM;
1951 break;
1952 }
1953 if (type1 != type2)
1954 return type1 == t_PATH ? 1 : -1;
1955 }
1956 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
1957
1958 return dif;
1959}
1960
1961char *f_name_buf(void)
1962{
1963 static char names[5][MAXPATHLEN];
1964 static unsigned int n;
1965
1966 n = (n + 1) % (sizeof names / sizeof names[0]);
1967
1968 return names[n];
1969}
1970
1971/* Return a copy of the full filename of a flist entry, using the indicated
1972 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1973 * done because we checked the size when creating the file_struct entry.
1974 */
1975char *f_name(struct file_struct *f, char *fbuf)
1976{
1977 if (!f || !F_IS_ACTIVE(f))
1978 return NULL;
1979
1980 if (!fbuf)
1981 fbuf = f_name_buf();
1982
1983 if (f->dirname) {
1984 int len = strlen(f->dirname);
1985 memcpy(fbuf, f->dirname, len);
1986 fbuf[len] = '/';
1987 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
1988 } else
1989 strlcpy(fbuf, f->basename, MAXPATHLEN);
1990
1991 return fbuf;
1992}
1993
1994/* Do a non-recursive scan of the named directory, possibly ignoring all
1995 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1996 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1997 * buffer (the functions we call will append names onto the end, but the old
1998 * dir value will be restored on exit). */
1999struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2000{
2001 struct file_list *dirlist;
2002 char dirbuf[MAXPATHLEN];
2003 int save_recurse = recurse;
2004 int save_xfer_dirs = xfer_dirs;
2005
2006 if (dlen < 0) {
2007 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2008 if (dlen >= MAXPATHLEN)
2009 return NULL;
2010 dirname = dirbuf;
2011 }
2012
2013 dirlist = flist_new("get_dirlist");
2014
2015 recurse = 0;
2016 xfer_dirs = 1;
2017 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
2018 xfer_dirs = save_xfer_dirs;
2019 recurse = save_recurse;
2020 if (do_progress)
2021 flist_count_offset += dirlist->count;
2022
2023 clean_flist(dirlist, 0, 0);
2024
2025 if (verbose > 3)
2026 output_flist(dirlist);
2027
2028 return dirlist;
2029}