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