A few more --files-from fixes, and an enhanced testsuite for it.
[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-2009 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 3 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, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24#include "ifuncs.h"
25#include "rounding.h"
26#include "inums.h"
27#include "io.h"
28
29extern int am_root;
30extern int am_server;
31extern int am_daemon;
32extern int am_sender;
33extern int am_generator;
34extern int inc_recurse;
35extern int always_checksum;
36extern int module_id;
37extern int ignore_errors;
38extern int numeric_ids;
39extern int recurse;
40extern int use_qsort;
41extern int xfer_dirs;
42extern int filesfrom_fd;
43extern int one_file_system;
44extern int copy_dirlinks;
45extern int keep_dirlinks;
46extern int preserve_uid;
47extern int preserve_gid;
48extern int preserve_acls;
49extern int preserve_xattrs;
50extern int preserve_links;
51extern int preserve_hard_links;
52extern int preserve_devices;
53extern int preserve_specials;
54extern int missing_args;
55extern int uid_ndx;
56extern int gid_ndx;
57extern int eol_nulls;
58extern int relative_paths;
59extern int implied_dirs;
60extern int file_extra_cnt;
61extern int ignore_perishable;
62extern int non_perishable_cnt;
63extern int prune_empty_dirs;
64extern int copy_links;
65extern int copy_unsafe_links;
66extern int protocol_version;
67extern int sanitize_paths;
68extern int munge_symlinks;
69extern int need_unsorted_flist;
70extern int sender_symlink_iconv;
71extern int output_needs_newline;
72extern int sender_keeps_checksum;
73extern int unsort_ndx;
74extern struct stats stats;
75extern char *filesfrom_host;
76extern char *usermap, *groupmap;
77
78extern char curr_dir[MAXPATHLEN];
79
80extern struct chmod_mode_struct *chmod_modes;
81
82extern filter_rule_list filter_list;
83extern filter_rule_list daemon_filter_list;
84
85#ifdef ICONV_OPTION
86extern int filesfrom_convert;
87extern iconv_t ic_send, ic_recv;
88#endif
89
90#ifdef HAVE_UTIMENSAT
91#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
92#define ST_MTIME_NSEC st_mtim.tv_nsec
93#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
94#define ST_MTIME_NSEC st_mtimensec
95#endif
96#endif
97
98#define PTR_SIZE (sizeof (struct file_struct *))
99
100int io_error;
101int checksum_len;
102dev_t filesystem_dev; /* used to implement -x */
103
104struct file_list *cur_flist, *first_flist, *dir_flist;
105int send_dir_ndx = -1, send_dir_depth = -1;
106int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
107int file_total = 0; /* total of all active items over all file-lists */
108int file_old_total = 0; /* total of active items that will soon be gone */
109int flist_eof = 0; /* all the file-lists are now known */
110
111#define NORMAL_NAME 0
112#define SLASH_ENDING_NAME 1
113#define DOTDIR_NAME 2
114
115/* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
116 * internally, even if this platform does not allow files to have 64-bit inums.
117 * The only exception is if we're on a platform with no 64-bit type at all.
118 *
119 * Because we use read_longint() to get these off the wire, if you transfer
120 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
121 * machine with no 64-bit types then you will get an overflow error.
122 *
123 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
124 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
125 * be truncated. But it's a kind of silly thing to do anyhow. */
126
127/* The tmp_* vars are used as a cache area by make_file() to store data
128 * that the sender doesn't need to remember in its file list. The data
129 * will survive just long enough to be used by send_file_entry(). */
130static dev_t tmp_rdev;
131#ifdef SUPPORT_HARD_LINKS
132static int64 tmp_dev, tmp_ino;
133#endif
134static char tmp_sum[MAX_DIGEST_LEN];
135
136static char empty_sum[MAX_DIGEST_LEN];
137static int flist_count_offset; /* for --delete --progress */
138
139static void flist_sort_and_clean(struct file_list *flist, int strip_root);
140static void output_flist(struct file_list *flist);
141
142void init_flist(void)
143{
144 if (DEBUG_GTE(FLIST, 4)) {
145 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
146 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
147 }
148 checksum_len = protocol_version < 21 ? 2
149 : protocol_version < 30 ? MD4_DIGEST_LEN
150 : MD5_DIGEST_LEN;
151}
152
153static int show_filelist_p(void)
154{
155 return INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
156}
157
158static void start_filelist_progress(char *kind)
159{
160 rprintf(FCLIENT, "%s ... ", kind);
161 output_needs_newline = 1;
162 rflush(FINFO);
163}
164
165static void emit_filelist_progress(int count)
166{
167 rprintf(FCLIENT, " %d files...\r", count);
168}
169
170static void maybe_emit_filelist_progress(int count)
171{
172 if (INFO_GTE(FLIST, 2) && show_filelist_p() && (count % 100) == 0)
173 emit_filelist_progress(count);
174}
175
176static void finish_filelist_progress(const struct file_list *flist)
177{
178 if (INFO_GTE(FLIST, 2)) {
179 /* This overwrites the progress line */
180 rprintf(FINFO, "%d file%sto consider\n",
181 flist->used, flist->used == 1 ? " " : "s ");
182 } else {
183 output_needs_newline = 0;
184 rprintf(FINFO, "done\n");
185 }
186}
187
188void show_flist_stats(void)
189{
190 /* Nothing yet */
191}
192
193/* Stat either a symlink or its referent, depending on the settings of
194 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
195 *
196 * If path is the name of a symlink, then the linkbuf buffer (which must hold
197 * MAXPATHLEN chars) will be set to the symlink's target string.
198 *
199 * The stat structure pointed to by stp will contain information about the
200 * link or the referent as appropriate, if they exist. */
201static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
202{
203#ifdef SUPPORT_LINKS
204 if (link_stat(path, stp, copy_dirlinks) < 0)
205 return -1;
206 if (S_ISLNK(stp->st_mode)) {
207 int llen = do_readlink(path, linkbuf, MAXPATHLEN - 1);
208 if (llen < 0)
209 return -1;
210 linkbuf[llen] = '\0';
211 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
212 if (INFO_GTE(SYMSAFE, 1)) {
213 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
214 path, linkbuf);
215 }
216 return x_stat(path, stp, NULL);
217 }
218 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
219 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
220 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
221 llen - SYMLINK_PREFIX_LEN + 1);
222 }
223 }
224 return 0;
225#else
226 return x_stat(path, stp, NULL);
227#endif
228}
229
230int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
231{
232#ifdef SUPPORT_LINKS
233 if (copy_links)
234 return x_stat(path, stp, NULL);
235 if (x_lstat(path, stp, NULL) < 0)
236 return -1;
237 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
238 STRUCT_STAT st;
239 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
240 *stp = st;
241 }
242 return 0;
243#else
244 return x_stat(path, stp, NULL);
245#endif
246}
247
248static inline int is_daemon_excluded(const char *fname, int is_dir)
249{
250 if (daemon_filter_list.head
251 && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
252 errno = ENOENT;
253 return 1;
254 }
255 return 0;
256}
257
258static inline int path_is_daemon_excluded(char *path, int ignore_filename)
259{
260 if (daemon_filter_list.head) {
261 char *slash = path;
262
263 while ((slash = strchr(slash+1, '/')) != NULL) {
264 int ret;
265 *slash = '\0';
266 ret = check_filter(&daemon_filter_list, FLOG, path, 1);
267 *slash = '/';
268 if (ret < 0) {
269 errno = ENOENT;
270 return 1;
271 }
272 }
273
274 if (!ignore_filename
275 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
276 errno = ENOENT;
277 return 1;
278 }
279 }
280
281 return 0;
282}
283
284/* This function is used to check if a file should be included/excluded
285 * from the list of files based on its name and type etc. The value of
286 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
287static int is_excluded(const char *fname, int is_dir, int filter_level)
288{
289#if 0 /* This currently never happens, so avoid a useless compare. */
290 if (filter_level == NO_FILTERS)
291 return 0;
292#endif
293 if (is_daemon_excluded(fname, is_dir))
294 return 1;
295 if (filter_level != ALL_FILTERS)
296 return 0;
297 if (filter_list.head
298 && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
299 return 1;
300 return 0;
301}
302
303static void send_directory(int f, struct file_list *flist,
304 char *fbuf, int len, int flags);
305
306static const char *pathname, *orig_dir;
307static int pathname_len;
308
309/* Make sure flist can hold at least flist->used + extra entries. */
310static void flist_expand(struct file_list *flist, int extra)
311{
312 struct file_struct **new_ptr;
313
314 if (flist->used + extra <= flist->malloced)
315 return;
316
317 if (flist->malloced < FLIST_START)
318 flist->malloced = FLIST_START;
319 else if (flist->malloced >= FLIST_LINEAR)
320 flist->malloced += FLIST_LINEAR;
321 else
322 flist->malloced *= 2;
323
324 /* In case count jumped or we are starting the list
325 * with a known size just set it. */
326 if (flist->malloced < flist->used + extra)
327 flist->malloced = flist->used + extra;
328
329 new_ptr = realloc_array(flist->files, struct file_struct *,
330 flist->malloced);
331
332 if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
333 rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
334 who_am_i(),
335 big_num(sizeof flist->files[0] * flist->malloced),
336 (new_ptr == flist->files) ? " not" : "");
337 }
338
339 flist->files = new_ptr;
340
341 if (!flist->files)
342 out_of_memory("flist_expand");
343}
344
345static void flist_done_allocating(struct file_list *flist)
346{
347 void *ptr = pool_boundary(flist->file_pool, 8*1024);
348 if (flist->pool_boundary == ptr)
349 flist->pool_boundary = NULL; /* list didn't use any pool memory */
350 else
351 flist->pool_boundary = ptr;
352}
353
354/* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
355 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
356 * with dir == NULL taken to be the starting directory, and dirlen < 0
357 * indicating that strdup(dir) should be called and then the -dirlen length
358 * value checked to ensure that it is not daemon-excluded. */
359int change_pathname(struct file_struct *file, const char *dir, int dirlen)
360{
361 if (dirlen < 0) {
362 char *cpy = strdup(dir);
363 if (*cpy != '/')
364 change_dir(orig_dir, CD_SKIP_CHDIR);
365 if (path_is_daemon_excluded(cpy, 0))
366 goto chdir_error;
367 dir = cpy;
368 dirlen = -dirlen;
369 } else {
370 if (file) {
371 if (pathname == F_PATHNAME(file))
372 return 1;
373 dir = F_PATHNAME(file);
374 if (dir)
375 dirlen = strlen(dir);
376 } else if (pathname == dir)
377 return 1;
378 if (dir && *dir != '/')
379 change_dir(orig_dir, CD_SKIP_CHDIR);
380 }
381
382 pathname = dir;
383 pathname_len = dirlen;
384
385 if (!dir)
386 dir = orig_dir;
387
388 if (!change_dir(dir, CD_NORMAL)) {
389 chdir_error:
390 io_error |= IOERR_GENERAL;
391 rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
392 if (dir != orig_dir)
393 change_dir(orig_dir, CD_NORMAL);
394 pathname = NULL;
395 pathname_len = 0;
396 return 0;
397 }
398
399 return 1;
400}
401
402static void send_file_entry(int f, const char *fname, struct file_struct *file,
403#ifdef SUPPORT_LINKS
404 const char *symlink_name, int symlink_len,
405#endif
406 int ndx, int first_ndx)
407{
408 static time_t modtime;
409 static mode_t mode;
410#ifdef SUPPORT_HARD_LINKS
411 static int64 dev;
412#endif
413 static dev_t rdev;
414 static uint32 rdev_major;
415 static uid_t uid;
416 static gid_t gid;
417 static const char *user_name, *group_name;
418 static char lastname[MAXPATHLEN];
419#ifdef SUPPORT_HARD_LINKS
420 int first_hlink_ndx = -1;
421#endif
422 int l1, l2;
423 int xflags;
424
425 /* Initialize starting value of xflags and adjust counts. */
426 if (S_ISREG(file->mode))
427 xflags = 0;
428 else if (S_ISDIR(file->mode)) {
429 stats.num_dirs++;
430 if (protocol_version >= 30) {
431 if (file->flags & FLAG_CONTENT_DIR)
432 xflags = file->flags & FLAG_TOP_DIR;
433 else if (file->flags & FLAG_IMPLIED_DIR)
434 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
435 else
436 xflags = XMIT_NO_CONTENT_DIR;
437 } else
438 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
439 } else {
440 if (S_ISLNK(file->mode))
441 stats.num_symlinks++;
442 else if (IS_DEVICE(file->mode))
443 stats.num_devices++;
444 else if (IS_SPECIAL(file->mode))
445 stats.num_specials++;
446 xflags = 0;
447 }
448
449 if (file->mode == mode)
450 xflags |= XMIT_SAME_MODE;
451 else
452 mode = file->mode;
453
454 if (preserve_devices && IS_DEVICE(mode)) {
455 if (protocol_version < 28) {
456 if (tmp_rdev == rdev)
457 xflags |= XMIT_SAME_RDEV_pre28;
458 else
459 rdev = tmp_rdev;
460 } else {
461 rdev = tmp_rdev;
462 if ((uint32)major(rdev) == rdev_major)
463 xflags |= XMIT_SAME_RDEV_MAJOR;
464 else
465 rdev_major = major(rdev);
466 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
467 xflags |= XMIT_RDEV_MINOR_8_pre30;
468 }
469 } else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) {
470 /* Special files don't need an rdev number, so just make
471 * the historical transmission of the value efficient. */
472 if (protocol_version < 28)
473 xflags |= XMIT_SAME_RDEV_pre28;
474 else {
475 rdev = MAKEDEV(major(rdev), 0);
476 xflags |= XMIT_SAME_RDEV_MAJOR;
477 if (protocol_version < 30)
478 xflags |= XMIT_RDEV_MINOR_8_pre30;
479 }
480 } else if (protocol_version < 28)
481 rdev = MAKEDEV(0, 0);
482 if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
483 xflags |= XMIT_SAME_UID;
484 else {
485 uid = F_OWNER(file);
486 if (!numeric_ids) {
487 user_name = add_uid(uid);
488 if (inc_recurse && user_name)
489 xflags |= XMIT_USER_NAME_FOLLOWS;
490 }
491 }
492 if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
493 xflags |= XMIT_SAME_GID;
494 else {
495 gid = F_GROUP(file);
496 if (!numeric_ids) {
497 group_name = add_gid(gid);
498 if (inc_recurse && group_name)
499 xflags |= XMIT_GROUP_NAME_FOLLOWS;
500 }
501 }
502 if (file->modtime == modtime)
503 xflags |= XMIT_SAME_TIME;
504 else
505 modtime = file->modtime;
506 if (NSEC_BUMP(file) && protocol_version >= 31)
507 xflags |= XMIT_MOD_NSEC;
508
509#ifdef SUPPORT_HARD_LINKS
510 if (tmp_dev != 0) {
511 if (protocol_version >= 30) {
512 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
513 first_hlink_ndx = (int32)(long)np->data - 1;
514 if (first_hlink_ndx < 0) {
515 np->data = (void*)(long)(first_ndx + ndx + 1);
516 xflags |= XMIT_HLINK_FIRST;
517 }
518 if (DEBUG_GTE(HLINK, 1)) {
519 if (first_hlink_ndx >= 0) {
520 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
521 who_am_i(), first_ndx + ndx, first_hlink_ndx,
522 first_hlink_ndx >= first_ndx ? "" : "un");
523 } else if (DEBUG_GTE(HLINK, 3)) {
524 rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
525 who_am_i(), first_ndx + ndx,
526 big_num(tmp_dev), big_num(tmp_ino));
527 }
528 }
529 } else {
530 if (tmp_dev == dev) {
531 if (protocol_version >= 28)
532 xflags |= XMIT_SAME_DEV_pre30;
533 } else
534 dev = tmp_dev;
535 }
536 xflags |= XMIT_HLINKED;
537 }
538#endif
539
540 for (l1 = 0;
541 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
542 l1++) {}
543 l2 = strlen(fname+l1);
544
545 if (l1 > 0)
546 xflags |= XMIT_SAME_NAME;
547 if (l2 > 255)
548 xflags |= XMIT_LONG_NAME;
549
550 /* We must make sure we don't send a zero flag byte or the
551 * other end will terminate the flist transfer. Note that
552 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
553 * it's harmless way to add a bit to the first flag byte. */
554 if (protocol_version >= 28) {
555 if (!xflags && !S_ISDIR(mode))
556 xflags |= XMIT_TOP_DIR;
557 if ((xflags & 0xFF00) || !xflags) {
558 xflags |= XMIT_EXTENDED_FLAGS;
559 write_shortint(f, xflags);
560 } else
561 write_byte(f, xflags);
562 } else {
563 if (!(xflags & 0xFF))
564 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
565 write_byte(f, xflags);
566 }
567 if (xflags & XMIT_SAME_NAME)
568 write_byte(f, l1);
569 if (xflags & XMIT_LONG_NAME)
570 write_varint30(f, l2);
571 else
572 write_byte(f, l2);
573 write_buf(f, fname + l1, l2);
574
575#ifdef SUPPORT_HARD_LINKS
576 if (first_hlink_ndx >= 0) {
577 write_varint(f, first_hlink_ndx);
578 if (first_hlink_ndx >= first_ndx)
579 goto the_end;
580 }
581#endif
582
583 write_varlong30(f, F_LENGTH(file), 3);
584 if (!(xflags & XMIT_SAME_TIME)) {
585 if (protocol_version >= 30)
586 write_varlong(f, modtime, 4);
587 else
588 write_int(f, modtime);
589 }
590 if (xflags & XMIT_MOD_NSEC)
591 write_varint(f, F_MOD_NSEC(file));
592 if (!(xflags & XMIT_SAME_MODE))
593 write_int(f, to_wire_mode(mode));
594 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
595 if (protocol_version < 30)
596 write_int(f, uid);
597 else {
598 write_varint(f, uid);
599 if (xflags & XMIT_USER_NAME_FOLLOWS) {
600 int len = strlen(user_name);
601 write_byte(f, len);
602 write_buf(f, user_name, len);
603 }
604 }
605 }
606 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
607 if (protocol_version < 30)
608 write_int(f, gid);
609 else {
610 write_varint(f, gid);
611 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
612 int len = strlen(group_name);
613 write_byte(f, len);
614 write_buf(f, group_name, len);
615 }
616 }
617 }
618 if ((preserve_devices && IS_DEVICE(mode))
619 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
620 if (protocol_version < 28) {
621 if (!(xflags & XMIT_SAME_RDEV_pre28))
622 write_int(f, (int)rdev);
623 } else {
624 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
625 write_varint30(f, major(rdev));
626 if (protocol_version >= 30)
627 write_varint(f, minor(rdev));
628 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
629 write_byte(f, minor(rdev));
630 else
631 write_int(f, minor(rdev));
632 }
633 }
634
635#ifdef SUPPORT_LINKS
636 if (symlink_len) {
637 write_varint30(f, symlink_len);
638 write_buf(f, symlink_name, symlink_len);
639 }
640#endif
641
642#ifdef SUPPORT_HARD_LINKS
643 if (tmp_dev != 0 && protocol_version < 30) {
644 if (protocol_version < 26) {
645 /* 32-bit dev_t and ino_t */
646 write_int(f, (int32)dev);
647 write_int(f, (int32)tmp_ino);
648 } else {
649 /* 64-bit dev_t and ino_t */
650 if (!(xflags & XMIT_SAME_DEV_pre30))
651 write_longint(f, dev);
652 write_longint(f, tmp_ino);
653 }
654 }
655#endif
656
657 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
658 const char *sum;
659 if (S_ISREG(mode))
660 sum = tmp_sum;
661 else {
662 /* Prior to 28, we sent a useless set of nulls. */
663 sum = empty_sum;
664 }
665 write_buf(f, sum, checksum_len);
666 }
667
668#ifdef SUPPORT_HARD_LINKS
669 the_end:
670#endif
671 strlcpy(lastname, fname, MAXPATHLEN);
672
673 if (S_ISREG(mode) || S_ISLNK(mode))
674 stats.total_size += F_LENGTH(file);
675}
676
677static struct file_struct *recv_file_entry(int f, struct file_list *flist, int xflags)
678{
679 static int64 modtime;
680 static mode_t mode;
681#ifdef SUPPORT_HARD_LINKS
682 static int64 dev;
683#endif
684 static dev_t rdev;
685 static uint32 rdev_major;
686 static uid_t uid;
687 static gid_t gid;
688 static uint16 gid_flags;
689 static char lastname[MAXPATHLEN], *lastdir;
690 static int lastdir_depth, lastdir_len = -1;
691 static unsigned int del_hier_name_len = 0;
692 static int in_del_hier = 0;
693 char thisname[MAXPATHLEN];
694 unsigned int l1 = 0, l2 = 0;
695 int alloc_len, basename_len, linkname_len;
696 int extra_len = file_extra_cnt * EXTRA_LEN;
697 int first_hlink_ndx = -1;
698 int64 file_length;
699 uint32 modtime_nsec;
700 const char *basename;
701 struct file_struct *file;
702 alloc_pool_t *pool;
703 char *bp;
704
705 if (xflags & XMIT_SAME_NAME)
706 l1 = read_byte(f);
707
708 if (xflags & XMIT_LONG_NAME)
709 l2 = read_varint30(f);
710 else
711 l2 = read_byte(f);
712
713 if (l2 >= MAXPATHLEN - l1) {
714 rprintf(FERROR,
715 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
716 xflags, l1, l2, lastname, who_am_i());
717 overflow_exit("recv_file_entry");
718 }
719
720 strlcpy(thisname, lastname, l1 + 1);
721 read_sbuf(f, &thisname[l1], l2);
722 thisname[l1 + l2] = 0;
723
724 /* Abuse basename_len for a moment... */
725 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
726
727#ifdef ICONV_OPTION
728 if (ic_recv != (iconv_t)-1) {
729 xbuf outbuf, inbuf;
730
731 INIT_CONST_XBUF(outbuf, thisname);
732 INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
733
734 if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
735 io_error |= IOERR_GENERAL;
736 rprintf(FERROR_UTF8,
737 "[%s] cannot convert filename: %s (%s)\n",
738 who_am_i(), lastname, strerror(errno));
739 outbuf.len = 0;
740 }
741 thisname[outbuf.len] = '\0';
742 }
743#endif
744
745 if (*thisname)
746 clean_fname(thisname, 0);
747
748 if (sanitize_paths)
749 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
750
751 if ((basename = strrchr(thisname, '/')) != NULL) {
752 int len = basename++ - thisname;
753 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
754 lastdir = new_array(char, len + 1);
755 memcpy(lastdir, thisname, len);
756 lastdir[len] = '\0';
757 lastdir_len = len;
758 lastdir_depth = count_dir_elements(lastdir);
759 }
760 } else
761 basename = thisname;
762 basename_len = strlen(basename) + 1; /* count the '\0' */
763
764#ifdef SUPPORT_HARD_LINKS
765 if (protocol_version >= 30
766 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
767 first_hlink_ndx = read_varint(f);
768 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
769 rprintf(FERROR,
770 "hard-link reference out of range: %d (%d)\n",
771 first_hlink_ndx, flist->ndx_start + flist->used);
772 exit_cleanup(RERR_PROTOCOL);
773 }
774 if (DEBUG_GTE(HLINK, 1)) {
775 rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
776 who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
777 first_hlink_ndx >= flist->ndx_start ? "" : "un");
778 }
779 if (first_hlink_ndx >= flist->ndx_start) {
780 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
781 file_length = F_LENGTH(first);
782 modtime = first->modtime;
783 modtime_nsec = F_MOD_NSEC(first);
784 mode = first->mode;
785 if (preserve_uid)
786 uid = F_OWNER(first);
787 if (preserve_gid)
788 gid = F_GROUP(first);
789 if (preserve_devices && IS_DEVICE(mode)) {
790 uint32 *devp = F_RDEV_P(first);
791 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
792 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
793 }
794 if (preserve_links && S_ISLNK(mode))
795 linkname_len = strlen(F_SYMLINK(first)) + 1;
796 else
797 linkname_len = 0;
798 goto create_object;
799 }
800 }
801#endif
802
803 file_length = read_varlong30(f, 3);
804 if (!(xflags & XMIT_SAME_TIME)) {
805 if (protocol_version >= 30) {
806 modtime = read_varlong(f, 4);
807#if SIZEOF_TIME_T < SIZEOF_INT64
808 if (!am_generator && (int64)(time_t)modtime != modtime) {
809 rprintf(FERROR_XFER,
810 "Time value of %s truncated on receiver.\n",
811 lastname);
812 }
813#endif
814 } else
815 modtime = read_int(f);
816 }
817 if (xflags & XMIT_MOD_NSEC)
818 modtime_nsec = read_varint(f);
819 else
820 modtime_nsec = 0;
821 if (!(xflags & XMIT_SAME_MODE))
822 mode = from_wire_mode(read_int(f));
823
824 if (chmod_modes && !S_ISLNK(mode) && mode)
825 mode = tweak_mode(mode, chmod_modes);
826
827 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
828 if (protocol_version < 30)
829 uid = (uid_t)read_int(f);
830 else {
831 uid = (uid_t)read_varint(f);
832 if (xflags & XMIT_USER_NAME_FOLLOWS)
833 uid = recv_user_name(f, uid);
834 else if (inc_recurse && am_root && (!numeric_ids || usermap))
835 uid = match_uid(uid);
836 }
837 }
838 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
839 if (protocol_version < 30)
840 gid = (gid_t)read_int(f);
841 else {
842 gid = (gid_t)read_varint(f);
843 gid_flags = 0;
844 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
845 gid = recv_group_name(f, gid, &gid_flags);
846 else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
847 gid = match_gid(gid, &gid_flags);
848 }
849 }
850
851 if ((preserve_devices && IS_DEVICE(mode))
852 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
853 if (protocol_version < 28) {
854 if (!(xflags & XMIT_SAME_RDEV_pre28))
855 rdev = (dev_t)read_int(f);
856 } else {
857 uint32 rdev_minor;
858 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
859 rdev_major = read_varint30(f);
860 if (protocol_version >= 30)
861 rdev_minor = read_varint(f);
862 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
863 rdev_minor = read_byte(f);
864 else
865 rdev_minor = read_int(f);
866 rdev = MAKEDEV(rdev_major, rdev_minor);
867 }
868 if (IS_DEVICE(mode))
869 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
870 file_length = 0;
871 } else if (protocol_version < 28)
872 rdev = MAKEDEV(0, 0);
873
874#ifdef SUPPORT_LINKS
875 if (preserve_links && S_ISLNK(mode)) {
876 linkname_len = read_varint30(f) + 1; /* count the '\0' */
877 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
878 rprintf(FERROR, "overflow: linkname_len=%d\n",
879 linkname_len - 1);
880 overflow_exit("recv_file_entry");
881 }
882#ifdef ICONV_OPTION
883 /* We don't know how much extra room we need to convert
884 * the as-yet-unread symlink data, so let's hope that a
885 * double-size buffer is plenty. */
886 if (sender_symlink_iconv)
887 linkname_len *= 2;
888#endif
889 if (munge_symlinks)
890 linkname_len += SYMLINK_PREFIX_LEN;
891 }
892 else
893#endif
894 linkname_len = 0;
895
896#ifdef SUPPORT_HARD_LINKS
897 create_object:
898 if (preserve_hard_links) {
899 if (protocol_version < 28 && S_ISREG(mode))
900 xflags |= XMIT_HLINKED;
901 if (xflags & XMIT_HLINKED)
902 extra_len += (inc_recurse+1) * EXTRA_LEN;
903 }
904#endif
905
906#ifdef SUPPORT_ACLS
907 /* Directories need an extra int32 for the default ACL. */
908 if (preserve_acls && S_ISDIR(mode))
909 extra_len += EXTRA_LEN;
910#endif
911
912 if (always_checksum && S_ISREG(mode))
913 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
914
915#if SIZEOF_INT64 >= 8
916 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
917 extra_len += EXTRA_LEN;
918#endif
919#ifdef HAVE_UTIMENSAT
920 if (modtime_nsec)
921 extra_len += EXTRA_LEN;
922#endif
923 if (file_length < 0) {
924 rprintf(FERROR, "Offset underflow: file-length is negative\n");
925 exit_cleanup(RERR_UNSUPPORTED);
926 }
927
928 if (inc_recurse && S_ISDIR(mode)) {
929 if (one_file_system) {
930 /* Room to save the dir's device for -x */
931 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
932 }
933 pool = dir_flist->file_pool;
934 } else
935 pool = flist->file_pool;
936
937#if EXTRA_ROUNDING > 0
938 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
939 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
940#endif
941
942 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
943 + linkname_len;
944 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
945
946 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
947 bp += extra_len;
948 file = (struct file_struct *)bp;
949 bp += FILE_STRUCT_LEN;
950
951 memcpy(bp, basename, basename_len);
952
953#ifdef SUPPORT_HARD_LINKS
954 if (xflags & XMIT_HLINKED)
955 file->flags |= FLAG_HLINKED;
956#endif
957 file->modtime = (time_t)modtime;
958#ifdef HAVE_UTIMENSAT
959 if (modtime_nsec) {
960 file->flags |= FLAG_MOD_NSEC;
961 OPT_EXTRA(file, 0)->unum = modtime_nsec;
962 }
963#endif
964 file->len32 = (uint32)file_length;
965#if SIZEOF_INT64 >= 8
966 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
967#if SIZEOF_CAPITAL_OFF_T < 8
968 rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
969 exit_cleanup(RERR_UNSUPPORTED);
970#else
971 file->flags |= FLAG_LENGTH64;
972 OPT_EXTRA(file, NSEC_BUMP(file))->unum = (uint32)(file_length >> 32);
973#endif
974 }
975#endif
976 file->mode = mode;
977 if (preserve_uid)
978 F_OWNER(file) = uid;
979 if (preserve_gid) {
980 F_GROUP(file) = gid;
981 file->flags |= gid_flags;
982 }
983 if (unsort_ndx)
984 F_NDX(file) = flist->used + flist->ndx_start;
985
986 if (basename != thisname) {
987 file->dirname = lastdir;
988 F_DEPTH(file) = lastdir_depth + 1;
989 } else
990 F_DEPTH(file) = 1;
991
992 if (S_ISDIR(mode)) {
993 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
994 F_DEPTH(file)--;
995 if (protocol_version >= 30) {
996 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
997 if (xflags & XMIT_TOP_DIR)
998 file->flags |= FLAG_TOP_DIR;
999 file->flags |= FLAG_CONTENT_DIR;
1000 } else if (xflags & XMIT_TOP_DIR)
1001 file->flags |= FLAG_IMPLIED_DIR;
1002 } else if (xflags & XMIT_TOP_DIR) {
1003 in_del_hier = recurse;
1004 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
1005 if (relative_paths && del_hier_name_len > 2
1006 && lastname[del_hier_name_len-1] == '.'
1007 && lastname[del_hier_name_len-2] == '/')
1008 del_hier_name_len -= 2;
1009 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
1010 } else if (in_del_hier) {
1011 if (!relative_paths || !del_hier_name_len
1012 || (l1 >= del_hier_name_len
1013 && lastname[del_hier_name_len] == '/'))
1014 file->flags |= FLAG_CONTENT_DIR;
1015 else
1016 in_del_hier = 0;
1017 }
1018 }
1019
1020 if (preserve_devices && IS_DEVICE(mode)) {
1021 uint32 *devp = F_RDEV_P(file);
1022 DEV_MAJOR(devp) = major(rdev);
1023 DEV_MINOR(devp) = minor(rdev);
1024 }
1025
1026#ifdef SUPPORT_LINKS
1027 if (linkname_len) {
1028 bp += basename_len;
1029 if (first_hlink_ndx >= flist->ndx_start) {
1030 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1031 memcpy(bp, F_SYMLINK(first), linkname_len);
1032 } else {
1033 if (munge_symlinks) {
1034 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
1035 bp += SYMLINK_PREFIX_LEN;
1036 linkname_len -= SYMLINK_PREFIX_LEN;
1037 }
1038#ifdef ICONV_OPTION
1039 if (sender_symlink_iconv) {
1040 xbuf outbuf, inbuf;
1041
1042 alloc_len = linkname_len;
1043 linkname_len /= 2;
1044
1045 /* Read the symlink data into the end of our double-sized
1046 * buffer and then convert it into the right spot. */
1047 INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
1048 linkname_len - 1, (size_t)-1);
1049 read_sbuf(f, inbuf.buf, inbuf.len);
1050 INIT_XBUF(outbuf, bp, 0, alloc_len);
1051
1052 if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
1053 io_error |= IOERR_GENERAL;
1054 rprintf(FERROR_XFER,
1055 "[%s] cannot convert symlink data for: %s (%s)\n",
1056 who_am_i(), full_fname(thisname), strerror(errno));
1057 bp = (char*)file->basename;
1058 *bp++ = '\0';
1059 outbuf.len = 0;
1060 }
1061 bp[outbuf.len] = '\0';
1062 } else
1063#endif
1064 read_sbuf(f, bp, linkname_len - 1);
1065 if (sanitize_paths && !munge_symlinks && *bp)
1066 sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
1067 }
1068 }
1069#endif
1070
1071#ifdef SUPPORT_HARD_LINKS
1072 if (preserve_hard_links && xflags & XMIT_HLINKED) {
1073 if (protocol_version >= 30) {
1074 if (xflags & XMIT_HLINK_FIRST) {
1075 F_HL_GNUM(file) = flist->ndx_start + flist->used;
1076 } else
1077 F_HL_GNUM(file) = first_hlink_ndx;
1078 } else {
1079 static int32 cnt = 0;
1080 struct ht_int64_node *np;
1081 int64 ino;
1082 int32 ndx;
1083 if (protocol_version < 26) {
1084 dev = read_int(f);
1085 ino = read_int(f);
1086 } else {
1087 if (!(xflags & XMIT_SAME_DEV_pre30))
1088 dev = read_longint(f);
1089 ino = read_longint(f);
1090 }
1091 np = idev_find(dev, ino);
1092 ndx = (int32)(long)np->data - 1;
1093 if (ndx < 0) {
1094 ndx = cnt++;
1095 np->data = (void*)(long)cnt;
1096 }
1097 F_HL_GNUM(file) = ndx;
1098 }
1099 }
1100#endif
1101
1102 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
1103 if (S_ISREG(mode))
1104 bp = F_SUM(file);
1105 else {
1106 /* Prior to 28, we get a useless set of nulls. */
1107 bp = tmp_sum;
1108 }
1109 if (first_hlink_ndx >= flist->ndx_start) {
1110 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1111 memcpy(bp, F_SUM(first), checksum_len);
1112 } else
1113 read_buf(f, bp, checksum_len);
1114 }
1115
1116#ifdef SUPPORT_ACLS
1117 if (preserve_acls && !S_ISLNK(mode))
1118 receive_acl(f, file);
1119#endif
1120#ifdef SUPPORT_XATTRS
1121 if (preserve_xattrs)
1122 receive_xattr(f, file);
1123#endif
1124
1125 if (S_ISREG(mode) || S_ISLNK(mode))
1126 stats.total_size += file_length;
1127
1128 return file;
1129}
1130
1131/* Create a file_struct for a named file by reading its stat() information
1132 * and performing extensive checks against global options.
1133 *
1134 * Returns a pointer to the new file struct, or NULL if there was an error
1135 * or this file should be excluded.
1136 *
1137 * Note: Any error (here or in send_file_name) that results in the omission of
1138 * an existent source file from the file list should set
1139 * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1140 * destination if --delete is on. */
1141struct file_struct *make_file(const char *fname, struct file_list *flist,
1142 STRUCT_STAT *stp, int flags, int filter_level)
1143{
1144 static char *lastdir;
1145 static int lastdir_len = -1;
1146 struct file_struct *file;
1147 char thisname[MAXPATHLEN];
1148 char linkname[MAXPATHLEN];
1149 int alloc_len, basename_len, linkname_len;
1150 int extra_len = file_extra_cnt * EXTRA_LEN;
1151 const char *basename;
1152 alloc_pool_t *pool;
1153 STRUCT_STAT st;
1154 char *bp;
1155
1156 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1157 io_error |= IOERR_GENERAL;
1158 rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
1159 return NULL;
1160 }
1161 clean_fname(thisname, 0);
1162 if (sanitize_paths)
1163 sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
1164
1165 if (stp && (S_ISDIR(stp->st_mode) || stp->st_mode == 0)) {
1166 /* This is needed to handle a "symlink/." with a --relative
1167 * dir, or a request to delete a specific file. */
1168 st = *stp;
1169 *linkname = '\0'; /* make IBM code checker happy */
1170 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1171 int save_errno = errno;
1172 /* See if file is excluded before reporting an error. */
1173 if (filter_level != NO_FILTERS
1174 && (is_excluded(thisname, 0, filter_level)
1175 || is_excluded(thisname, 1, filter_level))) {
1176 if (ignore_perishable && save_errno != ENOENT)
1177 non_perishable_cnt++;
1178 return NULL;
1179 }
1180 if (save_errno == ENOENT) {
1181#ifdef SUPPORT_LINKS
1182 /* When our options tell us to follow a symlink that
1183 * points nowhere, tell the user about the symlink
1184 * instead of giving a "vanished" message. We only
1185 * dereference a symlink if one of the --copy*links
1186 * options was specified, so there's no need for the
1187 * extra lstat() if one of these options isn't on. */
1188 if ((copy_links || copy_unsafe_links || copy_dirlinks)
1189 && x_lstat(thisname, &st, NULL) == 0
1190 && S_ISLNK(st.st_mode)) {
1191 io_error |= IOERR_GENERAL;
1192 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1193 full_fname(thisname));
1194 } else
1195#endif
1196 {
1197 enum logcode c = am_daemon && protocol_version < 28
1198 ? FERROR : FWARNING;
1199 io_error |= IOERR_VANISHED;
1200 rprintf(c, "file has vanished: %s\n",
1201 full_fname(thisname));
1202 }
1203 } else {
1204 io_error |= IOERR_GENERAL;
1205 rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
1206 full_fname(thisname));
1207 }
1208 return NULL;
1209 } else if (st.st_mode == 0) {
1210 io_error |= IOERR_GENERAL;
1211 rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n",
1212 full_fname(thisname));
1213 return NULL;
1214 }
1215
1216 if (filter_level == NO_FILTERS)
1217 goto skip_filters;
1218
1219 if (S_ISDIR(st.st_mode)) {
1220 if (!xfer_dirs) {
1221 rprintf(FINFO, "skipping directory %s\n", thisname);
1222 return NULL;
1223 }
1224 /* -x only affects dirs because we need to avoid recursing
1225 * into a mount-point directory, not to avoid copying a
1226 * symlinked file if -L (or similar) was specified. */
1227 if (one_file_system && st.st_dev != filesystem_dev
1228 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1229 if (one_file_system > 1) {
1230 if (INFO_GTE(MOUNT, 1)) {
1231 rprintf(FINFO,
1232 "[%s] skipping mount-point dir %s\n",
1233 who_am_i(), thisname);
1234 }
1235 return NULL;
1236 }
1237 flags |= FLAG_MOUNT_DIR;
1238 flags &= ~FLAG_CONTENT_DIR;
1239 }
1240 } else
1241 flags &= ~FLAG_CONTENT_DIR;
1242
1243 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1244 if (ignore_perishable)
1245 non_perishable_cnt++;
1246 return NULL;
1247 }
1248
1249 if (lp_ignore_nonreadable(module_id)) {
1250#ifdef SUPPORT_LINKS
1251 if (!S_ISLNK(st.st_mode))
1252#endif
1253 if (access(thisname, R_OK) != 0)
1254 return NULL;
1255 }
1256
1257 skip_filters:
1258
1259 /* Only divert a directory in the main transfer. */
1260 if (flist) {
1261 if (flist->prev && S_ISDIR(st.st_mode)
1262 && flags & FLAG_DIVERT_DIRS) {
1263 /* Room for parent/sibling/next-child info. */
1264 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1265 if (relative_paths)
1266 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1267 pool = dir_flist->file_pool;
1268 } else
1269 pool = flist->file_pool;
1270 } else {
1271#ifdef SUPPORT_ACLS
1272 /* Directories need an extra int32 for the default ACL. */
1273 if (preserve_acls && S_ISDIR(st.st_mode))
1274 extra_len += EXTRA_LEN;
1275#endif
1276 pool = NULL;
1277 }
1278
1279 if (DEBUG_GTE(FLIST, 2)) {
1280 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1281 who_am_i(), thisname, filter_level);
1282 }
1283
1284 if ((basename = strrchr(thisname, '/')) != NULL) {
1285 int len = basename++ - thisname;
1286 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1287 lastdir = new_array(char, len + 1);
1288 memcpy(lastdir, thisname, len);
1289 lastdir[len] = '\0';
1290 lastdir_len = len;
1291 }
1292 } else
1293 basename = thisname;
1294 basename_len = strlen(basename) + 1; /* count the '\0' */
1295
1296#ifdef SUPPORT_LINKS
1297 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1298#else
1299 linkname_len = 0;
1300#endif
1301
1302#ifdef ST_MTIME_NSEC
1303 if (st.ST_MTIME_NSEC && protocol_version >= 31)
1304 extra_len += EXTRA_LEN;
1305#endif
1306#if SIZEOF_CAPITAL_OFF_T >= 8
1307 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1308 extra_len += EXTRA_LEN;
1309#endif
1310
1311 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1312 file_checksum(thisname, tmp_sum, st.st_size);
1313 if (sender_keeps_checksum)
1314 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1315 }
1316
1317#if EXTRA_ROUNDING > 0
1318 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1319 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1320#endif
1321
1322 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1323 + linkname_len;
1324 if (pool)
1325 bp = pool_alloc(pool, alloc_len, "make_file");
1326 else {
1327 if (!(bp = new_array(char, alloc_len)))
1328 out_of_memory("make_file");
1329 }
1330
1331 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1332 bp += extra_len;
1333 file = (struct file_struct *)bp;
1334 bp += FILE_STRUCT_LEN;
1335
1336 memcpy(bp, basename, basename_len);
1337
1338#ifdef SUPPORT_HARD_LINKS
1339 if (preserve_hard_links && flist && flist->prev) {
1340 if (protocol_version >= 28
1341 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1342 : S_ISREG(st.st_mode)) {
1343 tmp_dev = (int64)st.st_dev + 1;
1344 tmp_ino = (int64)st.st_ino;
1345 } else
1346 tmp_dev = 0;
1347 }
1348#endif
1349
1350#ifdef HAVE_STRUCT_STAT_ST_RDEV
1351 if (IS_DEVICE(st.st_mode)) {
1352 tmp_rdev = st.st_rdev;
1353 st.st_size = 0;
1354 } else if (IS_SPECIAL(st.st_mode))
1355 st.st_size = 0;
1356#endif
1357
1358 file->flags = flags;
1359 file->modtime = st.st_mtime;
1360#ifdef ST_MTIME_NSEC
1361 if (st.ST_MTIME_NSEC && protocol_version >= 31) {
1362 file->flags |= FLAG_MOD_NSEC;
1363 OPT_EXTRA(file, 0)->unum = st.ST_MTIME_NSEC;
1364 }
1365#endif
1366 file->len32 = (uint32)st.st_size;
1367#if SIZEOF_CAPITAL_OFF_T >= 8
1368 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1369 file->flags |= FLAG_LENGTH64;
1370 OPT_EXTRA(file, NSEC_BUMP(file))->unum = (uint32)(st.st_size >> 32);
1371 }
1372#endif
1373 file->mode = st.st_mode;
1374 if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
1375 F_OWNER(file) = st.st_uid;
1376 if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
1377 F_GROUP(file) = st.st_gid;
1378
1379 if (basename != thisname)
1380 file->dirname = lastdir;
1381
1382#ifdef SUPPORT_LINKS
1383 if (linkname_len)
1384 memcpy(bp + basename_len, linkname, linkname_len);
1385#endif
1386
1387 if (am_sender)
1388 F_PATHNAME(file) = pathname;
1389 else if (!pool)
1390 F_DEPTH(file) = extra_len / EXTRA_LEN;
1391
1392 if (basename_len == 0+1) {
1393 if (!pool)
1394 unmake_file(file);
1395 return NULL;
1396 }
1397
1398 if (sender_keeps_checksum && S_ISREG(st.st_mode))
1399 memcpy(F_SUM(file), tmp_sum, checksum_len);
1400
1401 if (unsort_ndx)
1402 F_NDX(file) = stats.num_dirs;
1403
1404 return file;
1405}
1406
1407/* Only called for temporary file_struct entries created by make_file(). */
1408void unmake_file(struct file_struct *file)
1409{
1410 free(REQ_EXTRA(file, F_DEPTH(file)));
1411}
1412
1413static struct file_struct *send_file_name(int f, struct file_list *flist,
1414 const char *fname, STRUCT_STAT *stp,
1415 int flags, int filter_level)
1416{
1417 struct file_struct *file;
1418
1419 file = make_file(fname, flist, stp, flags, filter_level);
1420 if (!file)
1421 return NULL;
1422
1423 if (chmod_modes && !S_ISLNK(file->mode) && file->mode)
1424 file->mode = tweak_mode(file->mode, chmod_modes);
1425
1426 if (f >= 0) {
1427 char fbuf[MAXPATHLEN];
1428#ifdef SUPPORT_LINKS
1429 const char *symlink_name;
1430 int symlink_len;
1431#ifdef ICONV_OPTION
1432 char symlink_buf[MAXPATHLEN];
1433#endif
1434#endif
1435#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1436 stat_x sx;
1437 init_stat_x(&sx);
1438#endif
1439
1440#ifdef SUPPORT_LINKS
1441 if (preserve_links && S_ISLNK(file->mode)) {
1442 symlink_name = F_SYMLINK(file);
1443 symlink_len = strlen(symlink_name);
1444 if (symlink_len == 0) {
1445 io_error |= IOERR_GENERAL;
1446 f_name(file, fbuf);
1447 rprintf(FERROR_XFER,
1448 "skipping symlink with 0-length value: %s\n",
1449 full_fname(fbuf));
1450 return NULL;
1451 }
1452 } else {
1453 symlink_name = NULL;
1454 symlink_len = 0;
1455 }
1456#endif
1457
1458#ifdef ICONV_OPTION
1459 if (ic_send != (iconv_t)-1) {
1460 xbuf outbuf, inbuf;
1461
1462 INIT_CONST_XBUF(outbuf, fbuf);
1463
1464 if (file->dirname) {
1465 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1466 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1467 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0)
1468 goto convert_error;
1469 outbuf.size += 2;
1470 fbuf[outbuf.len++] = '/';
1471 }
1472
1473 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1474 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1475 convert_error:
1476 io_error |= IOERR_GENERAL;
1477 rprintf(FERROR_XFER,
1478 "[%s] cannot convert filename: %s (%s)\n",
1479 who_am_i(), f_name(file, fbuf), strerror(errno));
1480 return NULL;
1481 }
1482 fbuf[outbuf.len] = '\0';
1483
1484#ifdef SUPPORT_LINKS
1485 if (symlink_len && sender_symlink_iconv) {
1486 INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1487 INIT_CONST_XBUF(outbuf, symlink_buf);
1488 if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1489 io_error |= IOERR_GENERAL;
1490 f_name(file, fbuf);
1491 rprintf(FERROR_XFER,
1492 "[%s] cannot convert symlink data for: %s (%s)\n",
1493 who_am_i(), full_fname(fbuf), strerror(errno));
1494 return NULL;
1495 }
1496 symlink_buf[outbuf.len] = '\0';
1497
1498 symlink_name = symlink_buf;
1499 symlink_len = outbuf.len;
1500 }
1501#endif
1502 } else
1503#endif
1504 f_name(file, fbuf);
1505
1506#ifdef SUPPORT_ACLS
1507 if (preserve_acls && !S_ISLNK(file->mode)) {
1508 sx.st.st_mode = file->mode;
1509 if (get_acl(fname, &sx) < 0) {
1510 io_error |= IOERR_GENERAL;
1511 return NULL;
1512 }
1513 }
1514#endif
1515#ifdef SUPPORT_XATTRS
1516 if (preserve_xattrs) {
1517 sx.st.st_mode = file->mode;
1518 if (get_xattr(fname, &sx) < 0) {
1519 io_error |= IOERR_GENERAL;
1520 return NULL;
1521 }
1522 }
1523#endif
1524
1525 send_file_entry(f, fbuf, file,
1526#ifdef SUPPORT_LINKS
1527 symlink_name, symlink_len,
1528#endif
1529 flist->used, flist->ndx_start);
1530
1531#ifdef SUPPORT_ACLS
1532 if (preserve_acls && !S_ISLNK(file->mode)) {
1533 send_acl(f, &sx);
1534 free_acl(&sx);
1535 }
1536#endif
1537#ifdef SUPPORT_XATTRS
1538 if (preserve_xattrs) {
1539 F_XATTR(file) = send_xattr(f, &sx);
1540 free_xattr(&sx);
1541 }
1542#endif
1543 }
1544
1545 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1546
1547 flist_expand(flist, 1);
1548 flist->files[flist->used++] = file;
1549
1550 return file;
1551}
1552
1553static void send_if_directory(int f, struct file_list *flist,
1554 struct file_struct *file,
1555 char *fbuf, unsigned int ol,
1556 int flags)
1557{
1558 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1559
1560 if (S_ISDIR(file->mode)
1561 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1562 void *save_filters;
1563 unsigned int len = strlen(fbuf);
1564 if (len > 1 && fbuf[len-1] == '/')
1565 fbuf[--len] = '\0';
1566 if (len >= MAXPATHLEN - 1) {
1567 io_error |= IOERR_GENERAL;
1568 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1569 full_fname(fbuf));
1570 return;
1571 }
1572 save_filters = push_local_filters(fbuf, len);
1573 send_directory(f, flist, fbuf, len, flags);
1574 pop_local_filters(save_filters);
1575 fbuf[ol] = '\0';
1576 if (is_dot_dir)
1577 fbuf[ol-1] = '.';
1578 }
1579}
1580
1581static int file_compare(const void *file1, const void *file2)
1582{
1583 return f_name_cmp(*(struct file_struct **)file1,
1584 *(struct file_struct **)file2);
1585}
1586
1587/* The guts of a merge-sort algorithm. This was derived from the glibc
1588 * version, but I (Wayne) changed the merge code to do less copying and
1589 * to require only half the amount of temporary memory. */
1590static void fsort_tmp(struct file_struct **fp, size_t num,
1591 struct file_struct **tmp)
1592{
1593 struct file_struct **f1, **f2, **t;
1594 size_t n1, n2;
1595
1596 n1 = num / 2;
1597 n2 = num - n1;
1598 f1 = fp;
1599 f2 = fp + n1;
1600
1601 if (n1 > 1)
1602 fsort_tmp(f1, n1, tmp);
1603 if (n2 > 1)
1604 fsort_tmp(f2, n2, tmp);
1605
1606 while (f_name_cmp(*f1, *f2) <= 0) {
1607 if (!--n1)
1608 return;
1609 f1++;
1610 }
1611
1612 t = tmp;
1613 memcpy(t, f1, n1 * PTR_SIZE);
1614
1615 *f1++ = *f2++, n2--;
1616
1617 while (n1 > 0 && n2 > 0) {
1618 if (f_name_cmp(*t, *f2) <= 0)
1619 *f1++ = *t++, n1--;
1620 else
1621 *f1++ = *f2++, n2--;
1622 }
1623
1624 if (n1 > 0)
1625 memcpy(f1, t, n1 * PTR_SIZE);
1626}
1627
1628/* This file-struct sorting routine makes sure that any identical names in
1629 * the file list stay in the same order as they were in the original list.
1630 * This is particularly vital in inc_recurse mode where we expect a sort
1631 * on the flist to match the exact order of a sort on the dir_flist. */
1632static void fsort(struct file_struct **fp, size_t num)
1633{
1634 if (num <= 1)
1635 return;
1636
1637 if (use_qsort)
1638 qsort(fp, num, PTR_SIZE, file_compare);
1639 else {
1640 struct file_struct **tmp = new_array(struct file_struct *,
1641 (num+1) / 2);
1642 fsort_tmp(fp, num, tmp);
1643 free(tmp);
1644 }
1645}
1646
1647/* We take an entire set of sibling dirs from the sorted flist and link them
1648 * into the tree, setting the appropriate parent/child/sibling pointers. */
1649static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1650 int dir_cnt)
1651{
1652 int i;
1653 int32 *dp = NULL;
1654 int32 *parent_dp = parent_ndx < 0 ? NULL
1655 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1656
1657 flist_expand(dir_flist, dir_cnt);
1658 dir_flist->sorted = dir_flist->files;
1659
1660 for (i = 0; dir_cnt; i++) {
1661 struct file_struct *file = from_flist->sorted[i];
1662
1663 if (!S_ISDIR(file->mode))
1664 continue;
1665
1666 dir_flist->files[dir_flist->used++] = file;
1667 dir_cnt--;
1668
1669 if (file->basename[0] == '.' && file->basename[1] == '\0')
1670 continue;
1671
1672 if (dp)
1673 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1674 else if (parent_dp)
1675 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1676 else
1677 send_dir_ndx = dir_flist->used - 1;
1678
1679 dp = F_DIR_NODE_P(file);
1680 DIR_PARENT(dp) = parent_ndx;
1681 DIR_FIRST_CHILD(dp) = -1;
1682 }
1683 if (dp)
1684 DIR_NEXT_SIBLING(dp) = -1;
1685}
1686
1687static void interpret_stat_error(const char *fname, int is_dir)
1688{
1689 if (errno == ENOENT) {
1690 io_error |= IOERR_VANISHED;
1691 rprintf(FWARNING, "%s has vanished: %s\n",
1692 is_dir ? "directory" : "file", full_fname(fname));
1693 } else {
1694 io_error |= IOERR_GENERAL;
1695 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1696 full_fname(fname));
1697 }
1698}
1699
1700/* This function is normally called by the sender, but the receiving side also
1701 * calls it from get_dirlist() with f set to -1 so that we just construct the
1702 * file list in memory without sending it over the wire. Also, get_dirlist()
1703 * might call this with f set to -2, which also indicates that local filter
1704 * rules should be ignored. */
1705static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1706 int flags)
1707{
1708 struct dirent *di;
1709 unsigned remainder;
1710 char *p;
1711 DIR *d;
1712 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1713 int start = flist->used;
1714 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1715
1716 assert(flist != NULL);
1717
1718 if (!(d = opendir(fbuf))) {
1719 if (errno == ENOENT) {
1720 if (am_sender) /* Can abuse this for vanished error w/ENOENT: */
1721 interpret_stat_error(fbuf, True);
1722 return;
1723 }
1724 io_error |= IOERR_GENERAL;
1725 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1726 return;
1727 }
1728
1729 p = fbuf + len;
1730 if (len != 1 || *fbuf != '/')
1731 *p++ = '/';
1732 *p = '\0';
1733 remainder = MAXPATHLEN - (p - fbuf);
1734
1735 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1736 char *dname = d_name(di);
1737 if (dname[0] == '.' && (dname[1] == '\0'
1738 || (dname[1] == '.' && dname[2] == '\0')))
1739 continue;
1740 if (strlcpy(p, dname, remainder) >= remainder) {
1741 io_error |= IOERR_GENERAL;
1742 rprintf(FERROR_XFER,
1743 "cannot send long-named file %s\n",
1744 full_fname(fbuf));
1745 continue;
1746 }
1747 if (dname[0] == '\0') {
1748 io_error |= IOERR_GENERAL;
1749 rprintf(FERROR_XFER,
1750 "cannot send file with empty name in %s\n",
1751 full_fname(fbuf));
1752 continue;
1753 }
1754
1755 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1756 }
1757
1758 fbuf[len] = '\0';
1759
1760 if (errno) {
1761 io_error |= IOERR_GENERAL;
1762 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1763 }
1764
1765 closedir(d);
1766
1767 if (f >= 0 && recurse && !divert_dirs) {
1768 int i, end = flist->used - 1;
1769 /* send_if_directory() bumps flist->used, so use "end". */
1770 for (i = start; i <= end; i++)
1771 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1772 }
1773}
1774
1775static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1776 char *start, char *limit, int flags, char name_type)
1777{
1778 static char lastpath[MAXPATHLEN] = "";
1779 static int lastpath_len = 0;
1780 static struct file_struct *lastpath_struct = NULL;
1781 struct file_struct *file;
1782 item_list *relname_list;
1783 relnamecache **rnpp;
1784 int len, need_new_dir, depth = 0;
1785 filter_rule_list save_filter_list = filter_list;
1786
1787 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1788 filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1789
1790 if (inc_recurse) {
1791 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1792 && lastpath_len == limit - fname
1793 && strncmp(lastpath, fname, lastpath_len) == 0)
1794 need_new_dir = 0;
1795 else
1796 need_new_dir = 1;
1797 } else {
1798 char *tp = fname, *lp = lastpath;
1799 /* Skip any initial directories in our path that we
1800 * have in common with lastpath. */
1801 assert(start == fname);
1802 for ( ; ; tp++, lp++) {
1803 if (tp == limit) {
1804 if (*lp == '/' || *lp == '\0')
1805 goto done;
1806 break;
1807 }
1808 if (*lp != *tp)
1809 break;
1810 if (*tp == '/') {
1811 start = tp;
1812 depth++;
1813 }
1814 }
1815 need_new_dir = 1;
1816 }
1817
1818 if (need_new_dir) {
1819 int save_copy_links = copy_links;
1820 int save_xfer_dirs = xfer_dirs;
1821 char *slash;
1822
1823 copy_links = xfer_dirs = 1;
1824
1825 *limit = '\0';
1826
1827 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1828 *slash = '\0';
1829 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1830 depth++;
1831 if (!inc_recurse && file && S_ISDIR(file->mode))
1832 change_local_filter_dir(fname, strlen(fname), depth);
1833 *slash = '/';
1834 }
1835
1836 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1837 if (inc_recurse) {
1838 if (file && !S_ISDIR(file->mode))
1839 file = NULL;
1840 lastpath_struct = file;
1841 } else if (file && S_ISDIR(file->mode))
1842 change_local_filter_dir(fname, strlen(fname), ++depth);
1843
1844 strlcpy(lastpath, fname, sizeof lastpath);
1845 lastpath_len = limit - fname;
1846
1847 *limit = '/';
1848
1849 copy_links = save_copy_links;
1850 xfer_dirs = save_xfer_dirs;
1851
1852 if (!inc_recurse)
1853 goto done;
1854 }
1855
1856 if (!lastpath_struct)
1857 goto done; /* dir must have vanished */
1858
1859 len = strlen(limit+1);
1860 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1861 if (!relname_list) {
1862 if (!(relname_list = new0(item_list)))
1863 out_of_memory("send_implied_dirs");
1864 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1865 }
1866 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1867 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1868 out_of_memory("send_implied_dirs");
1869 (*rnpp)->name_type = name_type;
1870 strlcpy((*rnpp)->fname, limit+1, len + 1);
1871
1872done:
1873 filter_list = save_filter_list;
1874}
1875
1876static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1877{
1878 char fbuf[MAXPATHLEN];
1879 item_list *relname_list;
1880 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1881 size_t j;
1882
1883 f_name(file, fbuf);
1884 dlen = strlen(fbuf);
1885
1886 if (!change_pathname(file, NULL, 0))
1887 exit_cleanup(RERR_FILESELECT);
1888
1889 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1890
1891 if (file->flags & FLAG_CONTENT_DIR) {
1892 if (one_file_system) {
1893 STRUCT_STAT st;
1894 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1895 interpret_stat_error(fbuf, True);
1896 return;
1897 }
1898 filesystem_dev = st.st_dev;
1899 }
1900 send_directory(f, flist, fbuf, dlen, flags);
1901 }
1902
1903 if (!relative_paths)
1904 return;
1905
1906 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1907 if (!relname_list)
1908 return;
1909
1910 for (j = 0; j < relname_list->count; j++) {
1911 char *slash;
1912 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1913 char name_type = rnp->name_type;
1914
1915 fbuf[dlen] = '/';
1916 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1917 free(rnp);
1918 if (len >= (int)sizeof fbuf)
1919 continue; /* Impossible... */
1920
1921 slash = strchr(fbuf+dlen+1, '/');
1922 if (slash) {
1923 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1924 continue;
1925 }
1926
1927 if (name_type != NORMAL_NAME) {
1928 STRUCT_STAT st;
1929 if (link_stat(fbuf, &st, 1) != 0) {
1930 interpret_stat_error(fbuf, True);
1931 continue;
1932 }
1933 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1934 } else
1935 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1936 }
1937
1938 free(relname_list);
1939}
1940
1941void send_extra_file_list(int f, int at_least)
1942{
1943 struct file_list *flist;
1944 int64 start_write;
1945 uint16 prev_flags;
1946 int save_io_error = io_error;
1947
1948 if (flist_eof)
1949 return;
1950
1951 if (at_least < 0)
1952 at_least = file_total - file_old_total + 1;
1953
1954 /* Keep sending data until we have the requested number of
1955 * files in the upcoming file-lists. */
1956 while (file_total - file_old_total < at_least) {
1957 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1958 int dir_ndx, dstart = stats.num_dirs;
1959 const char *pathname = F_PATHNAME(file);
1960 int32 *dp;
1961
1962 flist = flist_new(0, "send_extra_file_list");
1963 start_write = stats.total_written;
1964
1965 if (unsort_ndx)
1966 dir_ndx = F_NDX(file);
1967 else
1968 dir_ndx = send_dir_ndx;
1969 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1970 flist->parent_ndx = dir_ndx;
1971
1972 send1extra(f, file, flist);
1973 prev_flags = file->flags;
1974 dp = F_DIR_NODE_P(file);
1975
1976 /* If there are any duplicate directory names that follow, we
1977 * send all the dirs together in one file-list. The dir_flist
1978 * tree links all the child subdirs onto the last dup dir. */
1979 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1980 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1981 send_dir_ndx = dir_ndx;
1982 file = dir_flist->sorted[dir_ndx];
1983 /* Try to avoid some duplicate scanning of identical dirs. */
1984 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1985 file->flags &= ~FLAG_CONTENT_DIR;
1986 send1extra(f, file, flist);
1987 prev_flags = file->flags;
1988 dp = F_DIR_NODE_P(file);
1989 }
1990
1991 if (protocol_version < 31 || io_error == save_io_error || ignore_errors)
1992 write_byte(f, 0);
1993 else {
1994 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
1995 write_varint(f, io_error);
1996 }
1997
1998 if (need_unsorted_flist) {
1999 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2000 out_of_memory("send_extra_file_list");
2001 memcpy(flist->sorted, flist->files,
2002 flist->used * sizeof (struct file_struct*));
2003 } else
2004 flist->sorted = flist->files;
2005
2006 flist_sort_and_clean(flist, 0);
2007
2008 add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart);
2009 flist_done_allocating(flist);
2010
2011 file_total += flist->used;
2012 stats.flist_size += stats.total_written - start_write;
2013 stats.num_files += flist->used;
2014 if (DEBUG_GTE(FLIST, 3))
2015 output_flist(flist);
2016
2017 if (DIR_FIRST_CHILD(dp) >= 0) {
2018 send_dir_ndx = DIR_FIRST_CHILD(dp);
2019 send_dir_depth++;
2020 } else {
2021 while (DIR_NEXT_SIBLING(dp) < 0) {
2022 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
2023 write_ndx(f, NDX_FLIST_EOF);
2024 flist_eof = 1;
2025 if (DEBUG_GTE(FLIST, 3))
2026 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2027 change_local_filter_dir(NULL, 0, 0);
2028 goto finish;
2029 }
2030 send_dir_depth--;
2031 file = dir_flist->sorted[send_dir_ndx];
2032 dp = F_DIR_NODE_P(file);
2033 }
2034 send_dir_ndx = DIR_NEXT_SIBLING(dp);
2035 }
2036 }
2037
2038 finish:
2039 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2040 send_msg_int(MSG_IO_ERROR, io_error);
2041}
2042
2043struct file_list *send_file_list(int f, int argc, char *argv[])
2044{
2045 static const char *lastdir;
2046 static int lastdir_len = -1;
2047 int len, dirlen;
2048 STRUCT_STAT st;
2049 char *p, *dir;
2050 struct file_list *flist;
2051 struct timeval start_tv, end_tv;
2052 int64 start_write;
2053 int use_ff_fd = 0;
2054 int disable_buffering, reenable_multiplex = -1;
2055 int flags = recurse ? FLAG_CONTENT_DIR : 0;
2056 int reading_remotely = filesfrom_host != NULL;
2057 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2058#ifdef ICONV_OPTION
2059 | (filesfrom_convert ? RL_CONVERT : 0)
2060#endif
2061 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2062 int implied_dot_dir = 0;
2063
2064 rprintf(FLOG, "building file list\n");
2065 if (show_filelist_p())
2066 start_filelist_progress("building file list");
2067 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2068 rprintf(FCLIENT, "sending incremental file list\n");
2069
2070 start_write = stats.total_written;
2071 gettimeofday(&start_tv, NULL);
2072
2073 if (relative_paths && protocol_version >= 30)
2074 implied_dirs = 1; /* We send flagged implied dirs */
2075
2076#ifdef SUPPORT_HARD_LINKS
2077 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2078 init_hard_links();
2079#endif
2080
2081 flist = cur_flist = flist_new(0, "send_file_list");
2082 if (inc_recurse) {
2083 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2084 flags |= FLAG_DIVERT_DIRS;
2085 } else
2086 dir_flist = cur_flist;
2087
2088 disable_buffering = io_start_buffering_out(f);
2089 if (filesfrom_fd >= 0) {
2090 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2091 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2092 full_fname(argv[0]));
2093 exit_cleanup(RERR_FILESELECT);
2094 }
2095 if (protocol_version < 31) {
2096 /* Older protocols send the files-from data w/o packaging
2097 * it in multiplexed I/O packets, so temporarily switch
2098 * to buffered I/O to match this behavior. */
2099 reenable_multiplex = io_end_multiplex_in(MPLX_TO_BUFFERED);
2100 }
2101 use_ff_fd = 1;
2102 }
2103
2104 if (!orig_dir)
2105 orig_dir = strdup(curr_dir);
2106
2107 while (1) {
2108 char fbuf[MAXPATHLEN], *fn, name_type;
2109
2110 if (use_ff_fd) {
2111 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2112 break;
2113 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2114 } else {
2115 if (argc-- == 0)
2116 break;
2117 strlcpy(fbuf, *argv++, MAXPATHLEN);
2118 if (sanitize_paths)
2119 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2120 }
2121
2122 len = strlen(fbuf);
2123 if (relative_paths) {
2124 /* We clean up fbuf below. */
2125 name_type = NORMAL_NAME;
2126 } else if (!len || fbuf[len - 1] == '/') {
2127 if (len == 2 && fbuf[0] == '.') {
2128 /* Turn "./" into just "." rather than "./." */
2129 fbuf[--len] = '\0';
2130 } else {
2131 if (len + 1 >= MAXPATHLEN)
2132 overflow_exit("send_file_list");
2133 fbuf[len++] = '.';
2134 fbuf[len] = '\0';
2135 }
2136 name_type = DOTDIR_NAME;
2137 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2138 && (len == 2 || fbuf[len-3] == '/')) {
2139 if (len + 2 >= MAXPATHLEN)
2140 overflow_exit("send_file_list");
2141 fbuf[len++] = '/';
2142 fbuf[len++] = '.';
2143 fbuf[len] = '\0';
2144 name_type = DOTDIR_NAME;
2145 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2146 name_type = DOTDIR_NAME;
2147 else
2148 name_type = NORMAL_NAME;
2149
2150 dir = NULL;
2151
2152 if (!relative_paths) {
2153 p = strrchr(fbuf, '/');
2154 if (p) {
2155 *p = '\0';
2156 if (p == fbuf)
2157 dir = "/";
2158 else
2159 dir = fbuf;
2160 len -= p - fbuf + 1;
2161 fn = p + 1;
2162 } else
2163 fn = fbuf;
2164 } else {
2165 if ((p = strstr(fbuf, "/./")) != NULL) {
2166 *p = '\0';
2167 if (p == fbuf)
2168 dir = "/";
2169 else {
2170 dir = fbuf;
2171 clean_fname(dir, 0);
2172 }
2173 fn = p + 3;
2174 while (*fn == '/')
2175 fn++;
2176 if (!*fn)
2177 *--fn = '\0'; /* ensure room for '.' */
2178 } else
2179 fn = fbuf;
2180 /* A leading ./ can be used in relative mode to affect
2181 * the dest dir without its name being in the path. */
2182 if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
2183 send_file_name(f, flist, ".", NULL,
2184 (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
2185 ALL_FILTERS);
2186 implied_dot_dir = 1;
2187 }
2188 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2189 | CFN_DROP_TRAILING_DOT_DIR);
2190 if (len == 1) {
2191 if (fn[0] == '/') {
2192 fn = "/.";
2193 len = 2;
2194 name_type = DOTDIR_NAME;
2195 } else if (fn[0] == '.')
2196 name_type = DOTDIR_NAME;
2197 } else if (fn[len-1] == '/') {
2198 fn[--len] = '\0';
2199 if (len == 1 && *fn == '.')
2200 name_type = DOTDIR_NAME;
2201 else
2202 name_type = SLASH_ENDING_NAME;
2203 }
2204 /* Reject a ".." dir in the active part of the path. */
2205 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2206 if ((p[2] == '/' || p[2] == '\0')
2207 && (p == fn || p[-1] == '/')) {
2208 rprintf(FERROR,
2209 "found \"..\" dir in relative path: %s\n",
2210 fn);
2211 exit_cleanup(RERR_SYNTAX);
2212 }
2213 }
2214 }
2215
2216 if (!*fn) {
2217 len = 1;
2218 fn = ".";
2219 name_type = DOTDIR_NAME;
2220 }
2221
2222 dirlen = dir ? strlen(dir) : 0;
2223 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
2224 if (!change_pathname(NULL, dir, -dirlen))
2225 continue;
2226 lastdir = pathname;
2227 lastdir_len = pathname_len;
2228 } else if (!change_pathname(NULL, lastdir, lastdir_len))
2229 continue;
2230
2231 if (fn != fbuf)
2232 memmove(fbuf, fn, len + 1);
2233
2234 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2235 || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
2236 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2237 if (errno != ENOENT || missing_args == 0) {
2238 /* This is a transfer error, but inhibit deletion
2239 * only if we might be omitting an existing file. */
2240 if (errno != ENOENT)
2241 io_error |= IOERR_GENERAL;
2242 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2243 full_fname(fbuf));
2244 continue;
2245 } else if (missing_args == 1) {
2246 /* Just ignore the arg. */
2247 continue;
2248 } else /* (missing_args == 2) */ {
2249 /* Send the arg as a "missing" entry with
2250 * mode 0, which tells the generator to delete it. */
2251 memset(&st, 0, sizeof st);
2252 }
2253 }
2254
2255 /* A dot-dir should not be excluded! */
2256 if (name_type != DOTDIR_NAME && st.st_mode != 0
2257 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2258 continue;
2259
2260 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2261 rprintf(FINFO, "skipping directory %s\n", fbuf);
2262 continue;
2263 }
2264
2265 if (inc_recurse && relative_paths && *fbuf) {
2266 if ((p = strchr(fbuf+1, '/')) != NULL) {
2267 if (p - fbuf == 1 && *fbuf == '.') {
2268 if ((fn = strchr(p+1, '/')) != NULL)
2269 p = fn;
2270 } else
2271 fn = p;
2272 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2273 if (fn == p)
2274 continue;
2275 }
2276 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2277 /* Send the implied directories at the start of the
2278 * source spec, so we get their permissions right. */
2279 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2280 }
2281
2282 if (one_file_system)
2283 filesystem_dev = st.st_dev;
2284
2285 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2286 struct file_struct *file;
2287 file = send_file_name(f, flist, fbuf, &st,
2288 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2289 NO_FILTERS);
2290 if (!file)
2291 continue;
2292 if (inc_recurse) {
2293 if (name_type == DOTDIR_NAME) {
2294 if (send_dir_depth < 0) {
2295 send_dir_depth = 0;
2296 change_local_filter_dir(fbuf, len, send_dir_depth);
2297 }
2298 send_directory(f, flist, fbuf, len, flags);
2299 }
2300 } else
2301 send_if_directory(f, flist, file, fbuf, len, flags);
2302 } else
2303 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2304 }
2305
2306 if (reenable_multiplex >= 0)
2307 io_start_multiplex_in(reenable_multiplex);
2308
2309 gettimeofday(&end_tv, NULL);
2310 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2311 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2312 if (stats.flist_buildtime == 0)
2313 stats.flist_buildtime = 1;
2314 start_tv = end_tv;
2315
2316 /* Indicate end of file list */
2317 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2318 write_byte(f, 0);
2319 else {
2320 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2321 write_varint(f, io_error);
2322 }
2323
2324#ifdef SUPPORT_HARD_LINKS
2325 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2326 idev_destroy();
2327#endif
2328
2329 if (show_filelist_p())
2330 finish_filelist_progress(flist);
2331
2332 gettimeofday(&end_tv, NULL);
2333 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2334 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2335
2336 /* When converting names, both sides keep an unsorted file-list array
2337 * because the names will differ on the sending and receiving sides
2338 * (both sides will use the unsorted index number for each item). */
2339
2340 /* Sort the list without removing any duplicates. This allows the
2341 * receiving side to ask for whatever name it kept. For incremental
2342 * recursion mode, the sender marks duplicate dirs so that it can
2343 * send them together in a single file-list. */
2344 if (need_unsorted_flist) {
2345 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2346 out_of_memory("send_file_list");
2347 memcpy(flist->sorted, flist->files,
2348 flist->used * sizeof (struct file_struct*));
2349 } else
2350 flist->sorted = flist->files;
2351 flist_sort_and_clean(flist, 0);
2352 file_total += flist->used;
2353
2354 if (numeric_ids <= 0 && !inc_recurse)
2355 send_id_list(f);
2356
2357 /* send the io_error flag */
2358 if (protocol_version < 30)
2359 write_int(f, ignore_errors ? 0 : io_error);
2360 else if (io_error && protocol_version == 30 && !ignore_errors)
2361 send_msg_int(MSG_IO_ERROR, io_error);
2362
2363 if (disable_buffering)
2364 io_end_buffering_out(IOBUF_FREE_BUFS);
2365
2366 stats.flist_size = stats.total_written - start_write;
2367 stats.num_files = flist->used;
2368
2369 if (DEBUG_GTE(FLIST, 3))
2370 output_flist(flist);
2371
2372 if (DEBUG_GTE(FLIST, 2))
2373 rprintf(FINFO, "send_file_list done\n");
2374
2375 if (inc_recurse) {
2376 send_dir_depth = 1;
2377 add_dirs_to_tree(-1, flist, stats.num_dirs);
2378 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2379 flist->parent_ndx = -1;
2380 flist_done_allocating(flist);
2381 if (send_dir_ndx < 0) {
2382 write_ndx(f, NDX_FLIST_EOF);
2383 flist_eof = 1;
2384 if (DEBUG_GTE(FLIST, 3))
2385 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2386 }
2387 else if (file_total == 1) {
2388 /* If we're creating incremental file-lists and there
2389 * was just 1 item in the first file-list, send 1 more
2390 * file-list to check if this is a 1-file xfer. */
2391 send_extra_file_list(f, 1);
2392 }
2393 } else {
2394 flist_eof = 1;
2395 if (DEBUG_GTE(FLIST, 3))
2396 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2397 }
2398
2399 return flist;
2400}
2401
2402struct file_list *recv_file_list(int f)
2403{
2404 struct file_list *flist;
2405 int dstart, flags;
2406 int64 start_read;
2407
2408 if (!first_flist) {
2409 if (show_filelist_p())
2410 start_filelist_progress("receiving file list");
2411 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2412 rprintf(FCLIENT, "receiving incremental file list\n");
2413 rprintf(FLOG, "receiving file list\n");
2414 if (usermap)
2415 parse_name_map(usermap, True);
2416 if (groupmap)
2417 parse_name_map(groupmap, False);
2418 }
2419
2420 start_read = stats.total_read;
2421
2422#ifdef SUPPORT_HARD_LINKS
2423 if (preserve_hard_links && !first_flist)
2424 init_hard_links();
2425#endif
2426
2427 flist = flist_new(0, "recv_file_list");
2428
2429 if (inc_recurse) {
2430 if (flist->ndx_start == 1)
2431 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2432 dstart = dir_flist->used;
2433 } else {
2434 dir_flist = flist;
2435 dstart = 0;
2436 }
2437
2438 while ((flags = read_byte(f)) != 0) {
2439 struct file_struct *file;
2440
2441 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2442 flags |= read_byte(f) << 8;
2443
2444 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2445 int err;
2446 if (protocol_version < 31) {
2447 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2448 exit_cleanup(RERR_PROTOCOL);
2449 }
2450 err = read_varint(f);
2451 if (!ignore_errors)
2452 io_error |= err;
2453 break;
2454 }
2455
2456 flist_expand(flist, 1);
2457 file = recv_file_entry(f, flist, flags);
2458
2459 if (S_ISREG(file->mode)) {
2460 /* Already counted */
2461 } else if (S_ISDIR(file->mode)) {
2462 if (inc_recurse) {
2463 flist_expand(dir_flist, 1);
2464 dir_flist->files[dir_flist->used++] = file;
2465 }
2466 stats.num_dirs++;
2467 } else if (S_ISLNK(file->mode))
2468 stats.num_symlinks++;
2469 else if (IS_DEVICE(file->mode))
2470 stats.num_symlinks++;
2471 else
2472 stats.num_specials++;
2473
2474 flist->files[flist->used++] = file;
2475
2476 maybe_emit_filelist_progress(flist->used);
2477
2478 if (DEBUG_GTE(FLIST, 2)) {
2479 char *name = f_name(file, NULL);
2480 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2481 }
2482 }
2483 file_total += flist->used;
2484
2485 if (DEBUG_GTE(FLIST, 2))
2486 rprintf(FINFO, "received %d names\n", flist->used);
2487
2488 if (show_filelist_p())
2489 finish_filelist_progress(flist);
2490
2491 if (need_unsorted_flist) {
2492 /* Create an extra array of index pointers that we can sort for
2493 * the generator's use (for wading through the files in sorted
2494 * order and for calling flist_find()). We keep the "files"
2495 * list unsorted for our exchange of index numbers with the
2496 * other side (since their names may not sort the same). */
2497 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2498 out_of_memory("recv_file_list");
2499 memcpy(flist->sorted, flist->files,
2500 flist->used * sizeof (struct file_struct*));
2501 if (inc_recurse && dir_flist->used > dstart) {
2502 static int dir_flist_malloced = 0;
2503 if (dir_flist_malloced < dir_flist->malloced) {
2504 dir_flist->sorted = realloc_array(dir_flist->sorted,
2505 struct file_struct *,
2506 dir_flist->malloced);
2507 dir_flist_malloced = dir_flist->malloced;
2508 }
2509 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2510 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2511 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2512 }
2513 } else {
2514 flist->sorted = flist->files;
2515 if (inc_recurse && dir_flist->used > dstart) {
2516 dir_flist->sorted = dir_flist->files;
2517 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2518 }
2519 }
2520
2521 if (inc_recurse)
2522 flist_done_allocating(flist);
2523 else if (f >= 0) {
2524 recv_id_list(f, flist);
2525 flist_eof = 1;
2526 if (DEBUG_GTE(FLIST, 3))
2527 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2528 }
2529
2530 flist_sort_and_clean(flist, relative_paths);
2531
2532 if (protocol_version < 30) {
2533 /* Recv the io_error flag */
2534 int err = read_int(f);
2535 if (!ignore_errors)
2536 io_error |= err;
2537 } else if (inc_recurse && flist->ndx_start == 1) {
2538 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2539 flist->parent_ndx = -1;
2540 }
2541
2542 if (DEBUG_GTE(FLIST, 3))
2543 output_flist(flist);
2544
2545 if (DEBUG_GTE(FLIST, 2))
2546 rprintf(FINFO, "recv_file_list done\n");
2547
2548 stats.flist_size += stats.total_read - start_read;
2549 stats.num_files += flist->used;
2550
2551 return flist;
2552}
2553
2554/* This is only used once by the receiver if the very first file-list
2555 * has exactly one item in it. */
2556void recv_additional_file_list(int f)
2557{
2558 struct file_list *flist;
2559 int ndx = read_ndx(f);
2560 if (ndx == NDX_FLIST_EOF) {
2561 flist_eof = 1;
2562 if (DEBUG_GTE(FLIST, 3))
2563 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2564 change_local_filter_dir(NULL, 0, 0);
2565 } else {
2566 ndx = NDX_FLIST_OFFSET - ndx;
2567 if (ndx < 0 || ndx >= dir_flist->used) {
2568 ndx = NDX_FLIST_OFFSET - ndx;
2569 rprintf(FERROR,
2570 "[%s] Invalid dir index: %d (%d - %d)\n",
2571 who_am_i(), ndx, NDX_FLIST_OFFSET,
2572 NDX_FLIST_OFFSET - dir_flist->used + 1);
2573 exit_cleanup(RERR_PROTOCOL);
2574 }
2575 if (DEBUG_GTE(FLIST, 3)) {
2576 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2577 who_am_i(), ndx);
2578 }
2579 flist = recv_file_list(f);
2580 flist->parent_ndx = ndx;
2581 }
2582}
2583
2584/* Search for an identically-named item in the file list. Note that the
2585 * items must agree in their directory-ness, or no match is returned. */
2586int flist_find(struct file_list *flist, struct file_struct *f)
2587{
2588 int low = flist->low, high = flist->high;
2589 int diff, mid, mid_up;
2590
2591 while (low <= high) {
2592 mid = (low + high) / 2;
2593 if (F_IS_ACTIVE(flist->sorted[mid]))
2594 mid_up = mid;
2595 else {
2596 /* Scan for the next non-empty entry using the cached
2597 * distance values. If the value isn't fully up-to-
2598 * date, update it. */
2599 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2600 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2601 do {
2602 mid_up += F_DEPTH(flist->sorted[mid_up]);
2603 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2604 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2605 }
2606 if (mid_up > high) {
2607 /* If there's nothing left above us, set high to
2608 * a non-empty entry below us and continue. */
2609 high = mid - (int)flist->sorted[mid]->len32;
2610 if (!F_IS_ACTIVE(flist->sorted[high])) {
2611 do {
2612 high -= (int)flist->sorted[high]->len32;
2613 } while (!F_IS_ACTIVE(flist->sorted[high]));
2614 flist->sorted[mid]->len32 = mid - high;
2615 }
2616 continue;
2617 }
2618 }
2619 diff = f_name_cmp(flist->sorted[mid_up], f);
2620 if (diff == 0) {
2621 if (protocol_version < 29
2622 && S_ISDIR(flist->sorted[mid_up]->mode)
2623 != S_ISDIR(f->mode))
2624 return -1;
2625 return mid_up;
2626 }
2627 if (diff < 0)
2628 low = mid_up + 1;
2629 else
2630 high = mid - 1;
2631 }
2632 return -1;
2633}
2634
2635/* Search for an identically-named item in the file list. Differs from
2636 * flist_find in that an item that agrees with "f" in directory-ness is
2637 * preferred but one that does not is still found. */
2638int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2639{
2640 mode_t save_mode;
2641 int ndx;
2642
2643 /* First look for an item that agrees in directory-ness. */
2644 ndx = flist_find(flist, f);
2645 if (ndx >= 0)
2646 return ndx;
2647
2648 /* Temporarily flip f->mode to look for an item of opposite
2649 * directory-ness. */
2650 save_mode = f->mode;
2651 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2652 ndx = flist_find(flist, f);
2653 f->mode = save_mode;
2654 return ndx;
2655}
2656
2657/*
2658 * Free up any resources a file_struct has allocated
2659 * and clear the file.
2660 */
2661void clear_file(struct file_struct *file)
2662{
2663 /* The +1 zeros out the first char of the basename. */
2664 memset(file, 0, FILE_STRUCT_LEN + 1);
2665 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2666 * entry. Likewise for len32 in the opposite direction. We assume
2667 * that we're alone for now since flist_find() will adjust the counts
2668 * it runs into that aren't up-to-date. */
2669 file->len32 = F_DEPTH(file) = 1;
2670}
2671
2672/* Allocate a new file list. */
2673struct file_list *flist_new(int flags, char *msg)
2674{
2675 struct file_list *flist;
2676
2677 if (!(flist = new0(struct file_list)))
2678 out_of_memory(msg);
2679
2680 if (flags & FLIST_TEMP) {
2681 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2682 out_of_memory,
2683 POOL_INTERN)))
2684 out_of_memory(msg);
2685 } else {
2686 /* This is a doubly linked list with prev looping back to
2687 * the end of the list, but the last next pointer is NULL. */
2688 if (!first_flist) {
2689 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2690 out_of_memory,
2691 POOL_INTERN);
2692 if (!flist->file_pool)
2693 out_of_memory(msg);
2694
2695 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2696
2697 first_flist = cur_flist = flist->prev = flist;
2698 } else {
2699 struct file_list *prev = first_flist->prev;
2700
2701 flist->file_pool = first_flist->file_pool;
2702
2703 flist->ndx_start = prev->ndx_start + prev->used + 1;
2704 flist->flist_num = prev->flist_num + 1;
2705
2706 flist->prev = prev;
2707 prev->next = first_flist->prev = flist;
2708 }
2709 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2710 flist_cnt++;
2711 }
2712
2713 return flist;
2714}
2715
2716/* Free up all elements in a flist. */
2717void flist_free(struct file_list *flist)
2718{
2719 if (!flist->prev) {
2720 /* Was FLIST_TEMP dir-list. */
2721 } else if (flist == flist->prev) {
2722 first_flist = cur_flist = NULL;
2723 file_total = 0;
2724 flist_cnt = 0;
2725 } else {
2726 if (flist == cur_flist)
2727 cur_flist = flist->next;
2728 if (flist == first_flist)
2729 first_flist = first_flist->next;
2730 else {
2731 flist->prev->next = flist->next;
2732 if (!flist->next)
2733 flist->next = first_flist;
2734 }
2735 flist->next->prev = flist->prev;
2736 file_total -= flist->used;
2737 flist_cnt--;
2738 }
2739
2740 if (!flist->prev || !flist_cnt)
2741 pool_destroy(flist->file_pool);
2742 else
2743 pool_free_old(flist->file_pool, flist->pool_boundary);
2744
2745 if (flist->sorted && flist->sorted != flist->files)
2746 free(flist->sorted);
2747 free(flist->files);
2748 free(flist);
2749}
2750
2751/* This routine ensures we don't have any duplicate names in our file list.
2752 * duplicate names can cause corruption because of the pipelining. */
2753static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2754{
2755 char fbuf[MAXPATHLEN];
2756 int i, prev_i;
2757
2758 if (!flist)
2759 return;
2760 if (flist->used == 0) {
2761 flist->high = -1;
2762 flist->low = 0;
2763 return;
2764 }
2765
2766 fsort(flist->sorted, flist->used);
2767
2768 if (!am_sender || inc_recurse) {
2769 for (i = prev_i = 0; i < flist->used; i++) {
2770 if (F_IS_ACTIVE(flist->sorted[i])) {
2771 prev_i = i;
2772 break;
2773 }
2774 }
2775 flist->low = prev_i;
2776 } else {
2777 i = prev_i = flist->used - 1;
2778 flist->low = 0;
2779 }
2780
2781 while (++i < flist->used) {
2782 int j;
2783 struct file_struct *file = flist->sorted[i];
2784
2785 if (!F_IS_ACTIVE(file))
2786 continue;
2787 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2788 j = prev_i;
2789 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2790 int save_mode = file->mode;
2791 /* Make sure that this directory doesn't duplicate a
2792 * non-directory earlier in the list. */
2793 flist->high = prev_i;
2794 file->mode = S_IFREG;
2795 j = flist_find(flist, file);
2796 file->mode = save_mode;
2797 } else
2798 j = -1;
2799 if (j >= 0) {
2800 int keep, drop;
2801 /* If one is a dir and the other is not, we want to
2802 * keep the dir because it might have contents in the
2803 * list. Otherwise keep the first one. */
2804 if (S_ISDIR(file->mode)) {
2805 struct file_struct *fp = flist->sorted[j];
2806 if (!S_ISDIR(fp->mode))
2807 keep = i, drop = j;
2808 else {
2809 if (am_sender)
2810 file->flags |= FLAG_DUPLICATE;
2811 else { /* Make sure we merge our vital flags. */
2812 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2813 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2814 }
2815 keep = j, drop = i;
2816 }
2817 } else
2818 keep = j, drop = i;
2819
2820 if (!am_sender) {
2821 if (DEBUG_GTE(DUP, 1)) {
2822 rprintf(FINFO,
2823 "removing duplicate name %s from file list (%d)\n",
2824 f_name(file, fbuf), drop + flist->ndx_start);
2825 }
2826 clear_file(flist->sorted[drop]);
2827 }
2828
2829 if (keep == i) {
2830 if (flist->low == drop) {
2831 for (j = drop + 1;
2832 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2833 j++) {}
2834 flist->low = j;
2835 }
2836 prev_i = i;
2837 }
2838 } else
2839 prev_i = i;
2840 }
2841 flist->high = prev_i;
2842
2843 if (strip_root) {
2844 /* We need to strip off the leading slashes for relative
2845 * paths, but this must be done _after_ the sorting phase. */
2846 for (i = flist->low; i <= flist->high; i++) {
2847 struct file_struct *file = flist->sorted[i];
2848
2849 if (!file->dirname)
2850 continue;
2851 while (*file->dirname == '/')
2852 file->dirname++;
2853 if (!*file->dirname)
2854 file->dirname = NULL;
2855 }
2856 }
2857
2858 if (prune_empty_dirs && !am_sender) {
2859 int j, prev_depth = 0;
2860
2861 prev_i = 0; /* It's OK that this isn't really true. */
2862
2863 for (i = flist->low; i <= flist->high; i++) {
2864 struct file_struct *fp, *file = flist->sorted[i];
2865
2866 /* This temporarily abuses the F_DEPTH() value for a
2867 * directory that is in a chain that might get pruned.
2868 * We restore the old value if it gets a reprieve. */
2869 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2870 /* Dump empty dirs when coming back down. */
2871 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2872 fp = flist->sorted[prev_i];
2873 if (F_DEPTH(fp) >= 0)
2874 break;
2875 prev_i = -F_DEPTH(fp)-1;
2876 clear_file(fp);
2877 }
2878 prev_depth = F_DEPTH(file);
2879 if (is_excluded(f_name(file, fbuf), 1,
2880 ALL_FILTERS)) {
2881 /* Keep dirs through this dir. */
2882 for (j = prev_depth-1; ; j--) {
2883 fp = flist->sorted[prev_i];
2884 if (F_DEPTH(fp) >= 0)
2885 break;
2886 prev_i = -F_DEPTH(fp)-1;
2887 F_DEPTH(fp) = j;
2888 }
2889 } else
2890 F_DEPTH(file) = -prev_i-1;
2891 prev_i = i;
2892 } else {
2893 /* Keep dirs through this non-dir. */
2894 for (j = prev_depth; ; j--) {
2895 fp = flist->sorted[prev_i];
2896 if (F_DEPTH(fp) >= 0)
2897 break;
2898 prev_i = -F_DEPTH(fp)-1;
2899 F_DEPTH(fp) = j;
2900 }
2901 }
2902 }
2903 /* Dump all remaining empty dirs. */
2904 while (1) {
2905 struct file_struct *fp = flist->sorted[prev_i];
2906 if (F_DEPTH(fp) >= 0)
2907 break;
2908 prev_i = -F_DEPTH(fp)-1;
2909 clear_file(fp);
2910 }
2911
2912 for (i = flist->low; i <= flist->high; i++) {
2913 if (F_IS_ACTIVE(flist->sorted[i]))
2914 break;
2915 }
2916 flist->low = i;
2917 for (i = flist->high; i >= flist->low; i--) {
2918 if (F_IS_ACTIVE(flist->sorted[i]))
2919 break;
2920 }
2921 flist->high = i;
2922 }
2923}
2924
2925static void output_flist(struct file_list *flist)
2926{
2927 char uidbuf[16], gidbuf[16], depthbuf[16];
2928 struct file_struct *file;
2929 const char *root, *dir, *slash, *name, *trail;
2930 const char *who = who_am_i();
2931 int i;
2932
2933 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2934 who, flist->ndx_start, flist->used, flist->low, flist->high);
2935 for (i = 0; i < flist->used; i++) {
2936 file = flist->files[i];
2937 if ((am_root || am_sender) && uid_ndx) {
2938 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2939 F_OWNER(file));
2940 } else
2941 *uidbuf = '\0';
2942 if (gid_ndx) {
2943 static char parens[] = "(\0)\0\0\0";
2944 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2945 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2946 pp, F_GROUP(file), pp + 2);
2947 } else
2948 *gidbuf = '\0';
2949 if (!am_sender)
2950 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2951 if (F_IS_ACTIVE(file)) {
2952 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2953 if ((dir = file->dirname) == NULL)
2954 dir = slash = "";
2955 else
2956 slash = "/";
2957 name = file->basename;
2958 trail = S_ISDIR(file->mode) ? "/" : "";
2959 } else
2960 root = dir = slash = name = trail = "";
2961 rprintf(FINFO,
2962 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
2963 who, i + flist->ndx_start,
2964 root, dir, slash, name, trail,
2965 (int)file->mode, comma_num(F_LENGTH(file)),
2966 uidbuf, gidbuf, file->flags);
2967 }
2968}
2969
2970enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2971enum fnc_type { t_PATH, t_ITEM };
2972
2973static int found_prefix;
2974
2975/* Compare the names of two file_struct entities, similar to how strcmp()
2976 * would do if it were operating on the joined strings.
2977 *
2978 * Some differences beginning with protocol_version 29: (1) directory names
2979 * are compared with an assumed trailing slash so that they compare in a
2980 * way that would cause them to sort immediately prior to any content they
2981 * may have; (2) a directory of any name compares after a non-directory of
2982 * any name at the same depth; (3) a directory with name "." compares prior
2983 * to anything else. These changes mean that a directory and a non-dir
2984 * with the same name will not compare as equal (protocol_version >= 29).
2985 *
2986 * The dirname component can be an empty string, but the basename component
2987 * cannot (and never is in the current codebase). The basename component
2988 * may be NULL (for a removed item), in which case it is considered to be
2989 * after any existing item. */
2990int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2991{
2992 int dif;
2993 const uchar *c1, *c2;
2994 enum fnc_state state1, state2;
2995 enum fnc_type type1, type2;
2996 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2997
2998 if (!f1 || !F_IS_ACTIVE(f1)) {
2999 if (!f2 || !F_IS_ACTIVE(f2))
3000 return 0;
3001 return -1;
3002 }
3003 if (!f2 || !F_IS_ACTIVE(f2))
3004 return 1;
3005
3006 c1 = (uchar*)f1->dirname;
3007 c2 = (uchar*)f2->dirname;
3008 if (c1 == c2)
3009 c1 = c2 = NULL;
3010 if (!c1) {
3011 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3012 c1 = (const uchar*)f1->basename;
3013 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3014 type1 = t_ITEM;
3015 state1 = s_TRAILING;
3016 c1 = (uchar*)"";
3017 } else
3018 state1 = s_BASE;
3019 } else {
3020 type1 = t_path;
3021 state1 = s_DIR;
3022 }
3023 if (!c2) {
3024 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3025 c2 = (const uchar*)f2->basename;
3026 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3027 type2 = t_ITEM;
3028 state2 = s_TRAILING;
3029 c2 = (uchar*)"";
3030 } else
3031 state2 = s_BASE;
3032 } else {
3033 type2 = t_path;
3034 state2 = s_DIR;
3035 }
3036
3037 if (type1 != type2)
3038 return type1 == t_PATH ? 1 : -1;
3039
3040 do {
3041 if (!*c1) {
3042 switch (state1) {
3043 case s_DIR:
3044 state1 = s_SLASH;
3045 c1 = (uchar*)"/";
3046 break;
3047 case s_SLASH:
3048 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3049 c1 = (const uchar*)f1->basename;
3050 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3051 type1 = t_ITEM;
3052 state1 = s_TRAILING;
3053 c1 = (uchar*)"";
3054 } else
3055 state1 = s_BASE;
3056 break;
3057 case s_BASE:
3058 state1 = s_TRAILING;
3059 if (type1 == t_PATH) {
3060 c1 = (uchar*)"/";
3061 break;
3062 }
3063 /* FALL THROUGH */
3064 case s_TRAILING:
3065 type1 = t_ITEM;
3066 break;
3067 }
3068 if (*c2 && type1 != type2)
3069 return type1 == t_PATH ? 1 : -1;
3070 }
3071 if (!*c2) {
3072 switch (state2) {
3073 case s_DIR:
3074 state2 = s_SLASH;
3075 c2 = (uchar*)"/";
3076 break;
3077 case s_SLASH:
3078 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3079 c2 = (const uchar*)f2->basename;
3080 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3081 type2 = t_ITEM;
3082 state2 = s_TRAILING;
3083 c2 = (uchar*)"";
3084 } else
3085 state2 = s_BASE;
3086 break;
3087 case s_BASE:
3088 state2 = s_TRAILING;
3089 if (type2 == t_PATH) {
3090 c2 = (uchar*)"/";
3091 break;
3092 }
3093 /* FALL THROUGH */
3094 case s_TRAILING:
3095 found_prefix = 1;
3096 if (!*c1)
3097 return 0;
3098 type2 = t_ITEM;
3099 break;
3100 }
3101 if (type1 != type2)
3102 return type1 == t_PATH ? 1 : -1;
3103 }
3104 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3105
3106 return dif;
3107}
3108
3109/* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3110 * not match if f2's basename is not an exact match of a path element in f1.
3111 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3112int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3113{
3114 found_prefix = 0;
3115 f_name_cmp(f1, f2);
3116 return found_prefix;
3117}
3118
3119char *f_name_buf(void)
3120{
3121 static char names[5][MAXPATHLEN];
3122 static unsigned int n;
3123
3124 n = (n + 1) % (sizeof names / sizeof names[0]);
3125
3126 return names[n];
3127}
3128
3129/* Return a copy of the full filename of a flist entry, using the indicated
3130 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3131 * done because we checked the size when creating the file_struct entry.
3132 */
3133char *f_name(const struct file_struct *f, char *fbuf)
3134{
3135 if (!f || !F_IS_ACTIVE(f))
3136 return NULL;
3137
3138 if (!fbuf)
3139 fbuf = f_name_buf();
3140
3141 if (f->dirname) {
3142 int len = strlen(f->dirname);
3143 memcpy(fbuf, f->dirname, len);
3144 fbuf[len] = '/';
3145 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3146 } else
3147 strlcpy(fbuf, f->basename, MAXPATHLEN);
3148
3149 return fbuf;
3150}
3151
3152/* Do a non-recursive scan of the named directory, possibly ignoring all
3153 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3154 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3155 * buffer (the functions we call will append names onto the end, but the old
3156 * dir value will be restored on exit). */
3157struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
3158{
3159 struct file_list *dirlist;
3160 char dirbuf[MAXPATHLEN];
3161 int save_recurse = recurse;
3162 int save_xfer_dirs = xfer_dirs;
3163 int save_prune_empty_dirs = prune_empty_dirs;
3164
3165 if (dlen < 0) {
3166 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3167 if (dlen >= MAXPATHLEN)
3168 return NULL;
3169 dirname = dirbuf;
3170 }
3171
3172 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3173
3174 recurse = 0;
3175 xfer_dirs = 1;
3176 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, FLAG_CONTENT_DIR);
3177 xfer_dirs = save_xfer_dirs;
3178 recurse = save_recurse;
3179 if (INFO_GTE(PROGRESS, 1))
3180 flist_count_offset += dirlist->used;
3181
3182 prune_empty_dirs = 0;
3183 dirlist->sorted = dirlist->files;
3184 flist_sort_and_clean(dirlist, 0);
3185 prune_empty_dirs = save_prune_empty_dirs;
3186
3187 if (DEBUG_GTE(FLIST, 3))
3188 output_flist(dirlist);
3189
3190 return dirlist;
3191}