Put file descriptor arg at the start of the arg list for consistency.
[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, 0) < 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, 0) < 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, 0) < 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, 0) < 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, 0) < 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 change_local_filter_dir(NULL, 0, 0);
2026 goto finish;
2027 }
2028 send_dir_depth--;
2029 file = dir_flist->sorted[send_dir_ndx];
2030 dp = F_DIR_NODE_P(file);
2031 }
2032 send_dir_ndx = DIR_NEXT_SIBLING(dp);
2033 }
2034 }
2035
2036 finish:
2037 if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2038 send_msg_int(MSG_IO_ERROR, io_error);
2039}
2040
2041struct file_list *send_file_list(int f, int argc, char *argv[])
2042{
2043 static const char *lastdir;
2044 static int lastdir_len = -1;
2045 int len, dirlen;
2046 STRUCT_STAT st;
2047 char *p, *dir;
2048 struct file_list *flist;
2049 struct timeval start_tv, end_tv;
2050 int64 start_write;
2051 int use_ff_fd = 0;
2052 int disable_buffering;
2053 int flags = recurse ? FLAG_CONTENT_DIR : 0;
2054 int reading_remotely = filesfrom_host != NULL;
2055 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2056#ifdef ICONV_OPTION
2057 | (filesfrom_convert ? RL_CONVERT : 0)
2058#endif
2059 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2060 int implied_dot_dir = 0;
2061
2062 rprintf(FLOG, "building file list\n");
2063 if (show_filelist_p())
2064 start_filelist_progress("building file list");
2065 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2066 rprintf(FCLIENT, "sending incremental file list\n");
2067
2068 start_write = stats.total_written;
2069 gettimeofday(&start_tv, NULL);
2070
2071 if (relative_paths && protocol_version >= 30)
2072 implied_dirs = 1; /* We send flagged implied dirs */
2073
2074#ifdef SUPPORT_HARD_LINKS
2075 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2076 init_hard_links();
2077#endif
2078
2079 flist = cur_flist = flist_new(0, "send_file_list");
2080 if (inc_recurse) {
2081 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2082 flags |= FLAG_DIVERT_DIRS;
2083 } else
2084 dir_flist = cur_flist;
2085
2086 disable_buffering = io_start_buffering_out(f);
2087 if (filesfrom_fd >= 0) {
2088 if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2089 rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2090 full_fname(argv[0]));
2091 exit_cleanup(RERR_FILESELECT);
2092 }
2093 use_ff_fd = 1;
2094 }
2095
2096 if (!orig_dir)
2097 orig_dir = strdup(curr_dir);
2098
2099 while (1) {
2100 char fbuf[MAXPATHLEN], *fn, name_type;
2101
2102 if (use_ff_fd) {
2103 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2104 break;
2105 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2106 } else {
2107 if (argc-- == 0)
2108 break;
2109 strlcpy(fbuf, *argv++, MAXPATHLEN);
2110 if (sanitize_paths)
2111 sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2112 }
2113
2114 len = strlen(fbuf);
2115 if (relative_paths) {
2116 /* We clean up fbuf below. */
2117 name_type = NORMAL_NAME;
2118 } else if (!len || fbuf[len - 1] == '/') {
2119 if (len == 2 && fbuf[0] == '.') {
2120 /* Turn "./" into just "." rather than "./." */
2121 fbuf[--len] = '\0';
2122 } else {
2123 if (len + 1 >= MAXPATHLEN)
2124 overflow_exit("send_file_list");
2125 fbuf[len++] = '.';
2126 fbuf[len] = '\0';
2127 }
2128 name_type = DOTDIR_NAME;
2129 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2130 && (len == 2 || fbuf[len-3] == '/')) {
2131 if (len + 2 >= MAXPATHLEN)
2132 overflow_exit("send_file_list");
2133 fbuf[len++] = '/';
2134 fbuf[len++] = '.';
2135 fbuf[len] = '\0';
2136 name_type = DOTDIR_NAME;
2137 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2138 name_type = DOTDIR_NAME;
2139 else
2140 name_type = NORMAL_NAME;
2141
2142 dir = NULL;
2143
2144 if (!relative_paths) {
2145 p = strrchr(fbuf, '/');
2146 if (p) {
2147 *p = '\0';
2148 if (p == fbuf)
2149 dir = "/";
2150 else
2151 dir = fbuf;
2152 len -= p - fbuf + 1;
2153 fn = p + 1;
2154 } else
2155 fn = fbuf;
2156 } else {
2157 if ((p = strstr(fbuf, "/./")) != NULL) {
2158 *p = '\0';
2159 if (p == fbuf)
2160 dir = "/";
2161 else {
2162 dir = fbuf;
2163 clean_fname(dir, 0);
2164 }
2165 fn = p + 3;
2166 while (*fn == '/')
2167 fn++;
2168 if (!*fn)
2169 *--fn = '\0'; /* ensure room for '.' */
2170 } else
2171 fn = fbuf;
2172 /* A leading ./ can be used in relative mode to affect
2173 * the dest dir without its name being in the path. */
2174 if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
2175 send_file_name(f, flist, ".", NULL,
2176 (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
2177 ALL_FILTERS);
2178 implied_dot_dir = 1;
2179 }
2180 len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2181 | CFN_DROP_TRAILING_DOT_DIR);
2182 if (len == 1) {
2183 if (fn[0] == '/') {
2184 fn = "/.";
2185 len = 2;
2186 name_type = DOTDIR_NAME;
2187 } else if (fn[0] == '.')
2188 name_type = DOTDIR_NAME;
2189 } else if (fn[len-1] == '/') {
2190 fn[--len] = '\0';
2191 if (len == 1 && *fn == '.')
2192 name_type = DOTDIR_NAME;
2193 else
2194 name_type = SLASH_ENDING_NAME;
2195 }
2196 /* Reject a ".." dir in the active part of the path. */
2197 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2198 if ((p[2] == '/' || p[2] == '\0')
2199 && (p == fn || p[-1] == '/')) {
2200 rprintf(FERROR,
2201 "found \"..\" dir in relative path: %s\n",
2202 fn);
2203 exit_cleanup(RERR_SYNTAX);
2204 }
2205 }
2206 }
2207
2208 if (!*fn) {
2209 len = 1;
2210 fn = ".";
2211 name_type = DOTDIR_NAME;
2212 }
2213
2214 dirlen = dir ? strlen(dir) : 0;
2215 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
2216 if (!change_pathname(NULL, dir, -dirlen))
2217 continue;
2218 lastdir = pathname;
2219 lastdir_len = pathname_len;
2220 } else if (!change_pathname(NULL, lastdir, lastdir_len))
2221 continue;
2222
2223 if (fn != fbuf)
2224 memmove(fbuf, fn, len + 1);
2225
2226 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2227 || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
2228 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2229 if (errno != ENOENT || missing_args == 0) {
2230 /* This is a transfer error, but inhibit deletion
2231 * only if we might be omitting an existing file. */
2232 if (errno != ENOENT)
2233 io_error |= IOERR_GENERAL;
2234 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2235 full_fname(fbuf));
2236 continue;
2237 } else if (missing_args == 1) {
2238 /* Just ignore the arg. */
2239 continue;
2240 } else /* (missing_args == 2) */ {
2241 /* Send the arg as a "missing" entry with
2242 * mode 0, which tells the generator to delete it. */
2243 memset(&st, 0, sizeof st);
2244 }
2245 }
2246
2247 /* A dot-dir should not be excluded! */
2248 if (name_type != DOTDIR_NAME && st.st_mode != 0
2249 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2250 continue;
2251
2252 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2253 rprintf(FINFO, "skipping directory %s\n", fbuf);
2254 continue;
2255 }
2256
2257 if (inc_recurse && relative_paths && *fbuf) {
2258 if ((p = strchr(fbuf+1, '/')) != NULL) {
2259 if (p - fbuf == 1 && *fbuf == '.') {
2260 if ((fn = strchr(p+1, '/')) != NULL)
2261 p = fn;
2262 } else
2263 fn = p;
2264 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2265 if (fn == p)
2266 continue;
2267 }
2268 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2269 /* Send the implied directories at the start of the
2270 * source spec, so we get their permissions right. */
2271 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2272 }
2273
2274 if (one_file_system)
2275 filesystem_dev = st.st_dev;
2276
2277 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2278 struct file_struct *file;
2279 file = send_file_name(f, flist, fbuf, &st,
2280 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2281 NO_FILTERS);
2282 if (!file)
2283 continue;
2284 if (inc_recurse) {
2285 if (name_type == DOTDIR_NAME) {
2286 if (send_dir_depth < 0) {
2287 send_dir_depth = 0;
2288 change_local_filter_dir(fbuf, len, send_dir_depth);
2289 }
2290 send_directory(f, flist, fbuf, len, flags);
2291 }
2292 } else
2293 send_if_directory(f, flist, file, fbuf, len, flags);
2294 } else
2295 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2296 }
2297
2298 gettimeofday(&end_tv, NULL);
2299 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2300 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2301 if (stats.flist_buildtime == 0)
2302 stats.flist_buildtime = 1;
2303 start_tv = end_tv;
2304
2305 /* Indicate end of file list */
2306 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2307 write_byte(f, 0);
2308 else {
2309 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2310 write_varint(f, io_error);
2311 }
2312
2313#ifdef SUPPORT_HARD_LINKS
2314 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2315 idev_destroy();
2316#endif
2317
2318 if (show_filelist_p())
2319 finish_filelist_progress(flist);
2320
2321 gettimeofday(&end_tv, NULL);
2322 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2323 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2324
2325 /* When converting names, both sides keep an unsorted file-list array
2326 * because the names will differ on the sending and receiving sides
2327 * (both sides will use the unsorted index number for each item). */
2328
2329 /* Sort the list without removing any duplicates. This allows the
2330 * receiving side to ask for whatever name it kept. For incremental
2331 * recursion mode, the sender marks duplicate dirs so that it can
2332 * send them together in a single file-list. */
2333 if (need_unsorted_flist) {
2334 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2335 out_of_memory("send_file_list");
2336 memcpy(flist->sorted, flist->files,
2337 flist->used * sizeof (struct file_struct*));
2338 } else
2339 flist->sorted = flist->files;
2340 flist_sort_and_clean(flist, 0);
2341 file_total += flist->used;
2342
2343 if (numeric_ids <= 0 && !inc_recurse)
2344 send_id_list(f);
2345
2346 set_msg_fd_in(-1);
2347
2348 /* send the io_error flag */
2349 if (protocol_version < 30)
2350 write_int(f, ignore_errors ? 0 : io_error);
2351 else if (io_error && protocol_version == 30 && !ignore_errors)
2352 send_msg_int(MSG_IO_ERROR, io_error);
2353
2354 if (disable_buffering)
2355 io_end_buffering_out();
2356
2357 stats.flist_size = stats.total_written - start_write;
2358 stats.num_files = flist->used;
2359
2360 if (DEBUG_GTE(FLIST, 3))
2361 output_flist(flist);
2362
2363 if (DEBUG_GTE(FLIST, 2))
2364 rprintf(FINFO, "send_file_list done\n");
2365
2366 if (inc_recurse) {
2367 send_dir_depth = 1;
2368 add_dirs_to_tree(-1, flist, stats.num_dirs);
2369 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2370 flist->parent_ndx = -1;
2371 flist_done_allocating(flist);
2372 if (send_dir_ndx < 0) {
2373 write_ndx(f, NDX_FLIST_EOF);
2374 flist_eof = 1;
2375 }
2376 else if (file_total == 1) {
2377 /* If we're creating incremental file-lists and there
2378 * was just 1 item in the first file-list, send 1 more
2379 * file-list to check if this is a 1-file xfer. */
2380 send_extra_file_list(f, 1);
2381 }
2382 } else
2383 flist_eof = 1;
2384
2385 return flist;
2386}
2387
2388struct file_list *recv_file_list(int f)
2389{
2390 struct file_list *flist;
2391 int dstart, flags;
2392 int64 start_read;
2393
2394 if (!first_flist) {
2395 if (show_filelist_p())
2396 start_filelist_progress("receiving file list");
2397 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2398 rprintf(FCLIENT, "receiving incremental file list\n");
2399 rprintf(FLOG, "receiving file list\n");
2400 if (usermap)
2401 parse_name_map(usermap, True);
2402 if (groupmap)
2403 parse_name_map(groupmap, False);
2404 }
2405
2406 start_read = stats.total_read;
2407
2408#ifdef SUPPORT_HARD_LINKS
2409 if (preserve_hard_links && !first_flist)
2410 init_hard_links();
2411#endif
2412
2413 flist = flist_new(0, "recv_file_list");
2414
2415 if (inc_recurse) {
2416 if (flist->ndx_start == 1)
2417 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2418 dstart = dir_flist->used;
2419 } else {
2420 dir_flist = flist;
2421 dstart = 0;
2422 }
2423
2424 while ((flags = read_byte(f)) != 0) {
2425 struct file_struct *file;
2426
2427 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2428 flags |= read_byte(f) << 8;
2429
2430 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2431 int err;
2432 if (protocol_version < 31) {
2433 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2434 exit_cleanup(RERR_PROTOCOL);
2435 }
2436 err = read_varint(f);
2437 if (!ignore_errors)
2438 io_error |= err;
2439 break;
2440 }
2441
2442 flist_expand(flist, 1);
2443 file = recv_file_entry(f, flist, flags);
2444
2445 if (S_ISREG(file->mode)) {
2446 /* Already counted */
2447 } else if (S_ISDIR(file->mode)) {
2448 if (inc_recurse) {
2449 flist_expand(dir_flist, 1);
2450 dir_flist->files[dir_flist->used++] = file;
2451 }
2452 stats.num_dirs++;
2453 } else if (S_ISLNK(file->mode))
2454 stats.num_symlinks++;
2455 else if (IS_DEVICE(file->mode))
2456 stats.num_symlinks++;
2457 else
2458 stats.num_specials++;
2459
2460 flist->files[flist->used++] = file;
2461
2462 maybe_emit_filelist_progress(flist->used);
2463
2464 if (DEBUG_GTE(FLIST, 2)) {
2465 char *name = f_name(file, NULL);
2466 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2467 }
2468 }
2469 file_total += flist->used;
2470
2471 if (DEBUG_GTE(FLIST, 2))
2472 rprintf(FINFO, "received %d names\n", flist->used);
2473
2474 if (show_filelist_p())
2475 finish_filelist_progress(flist);
2476
2477 if (need_unsorted_flist) {
2478 /* Create an extra array of index pointers that we can sort for
2479 * the generator's use (for wading through the files in sorted
2480 * order and for calling flist_find()). We keep the "files"
2481 * list unsorted for our exchange of index numbers with the
2482 * other side (since their names may not sort the same). */
2483 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2484 out_of_memory("recv_file_list");
2485 memcpy(flist->sorted, flist->files,
2486 flist->used * sizeof (struct file_struct*));
2487 if (inc_recurse && dir_flist->used > dstart) {
2488 static int dir_flist_malloced = 0;
2489 if (dir_flist_malloced < dir_flist->malloced) {
2490 dir_flist->sorted = realloc_array(dir_flist->sorted,
2491 struct file_struct *,
2492 dir_flist->malloced);
2493 dir_flist_malloced = dir_flist->malloced;
2494 }
2495 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2496 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2497 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2498 }
2499 } else {
2500 flist->sorted = flist->files;
2501 if (inc_recurse && dir_flist->used > dstart) {
2502 dir_flist->sorted = dir_flist->files;
2503 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2504 }
2505 }
2506
2507 if (inc_recurse)
2508 flist_done_allocating(flist);
2509 else if (f >= 0) {
2510 recv_id_list(f, flist);
2511 flist_eof = 1;
2512 }
2513
2514 flist_sort_and_clean(flist, relative_paths);
2515
2516 if (protocol_version < 30) {
2517 /* Recv the io_error flag */
2518 int err = read_int(f);
2519 if (!ignore_errors)
2520 io_error |= err;
2521 } else if (inc_recurse && flist->ndx_start == 1) {
2522 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2523 flist->parent_ndx = -1;
2524 }
2525
2526 if (DEBUG_GTE(FLIST, 3))
2527 output_flist(flist);
2528
2529 if (DEBUG_GTE(FLIST, 2))
2530 rprintf(FINFO, "recv_file_list done\n");
2531
2532 stats.flist_size += stats.total_read - start_read;
2533 stats.num_files += flist->used;
2534
2535 return flist;
2536}
2537
2538/* This is only used once by the receiver if the very first file-list
2539 * has exactly one item in it. */
2540void recv_additional_file_list(int f)
2541{
2542 struct file_list *flist;
2543 int ndx = read_ndx(f);
2544 if (ndx == NDX_FLIST_EOF) {
2545 flist_eof = 1;
2546 change_local_filter_dir(NULL, 0, 0);
2547 } else {
2548 ndx = NDX_FLIST_OFFSET - ndx;
2549 if (ndx < 0 || ndx >= dir_flist->used) {
2550 ndx = NDX_FLIST_OFFSET - ndx;
2551 rprintf(FERROR,
2552 "[%s] Invalid dir index: %d (%d - %d)\n",
2553 who_am_i(), ndx, NDX_FLIST_OFFSET,
2554 NDX_FLIST_OFFSET - dir_flist->used + 1);
2555 exit_cleanup(RERR_PROTOCOL);
2556 }
2557 if (DEBUG_GTE(FLIST, 3)) {
2558 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2559 who_am_i(), ndx);
2560 }
2561 flist = recv_file_list(f);
2562 flist->parent_ndx = ndx;
2563 }
2564}
2565
2566/* Search for an identically-named item in the file list. Note that the
2567 * items must agree in their directory-ness, or no match is returned. */
2568int flist_find(struct file_list *flist, struct file_struct *f)
2569{
2570 int low = flist->low, high = flist->high;
2571 int diff, mid, mid_up;
2572
2573 while (low <= high) {
2574 mid = (low + high) / 2;
2575 if (F_IS_ACTIVE(flist->sorted[mid]))
2576 mid_up = mid;
2577 else {
2578 /* Scan for the next non-empty entry using the cached
2579 * distance values. If the value isn't fully up-to-
2580 * date, update it. */
2581 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2582 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2583 do {
2584 mid_up += F_DEPTH(flist->sorted[mid_up]);
2585 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2586 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2587 }
2588 if (mid_up > high) {
2589 /* If there's nothing left above us, set high to
2590 * a non-empty entry below us and continue. */
2591 high = mid - (int)flist->sorted[mid]->len32;
2592 if (!F_IS_ACTIVE(flist->sorted[high])) {
2593 do {
2594 high -= (int)flist->sorted[high]->len32;
2595 } while (!F_IS_ACTIVE(flist->sorted[high]));
2596 flist->sorted[mid]->len32 = mid - high;
2597 }
2598 continue;
2599 }
2600 }
2601 diff = f_name_cmp(flist->sorted[mid_up], f);
2602 if (diff == 0) {
2603 if (protocol_version < 29
2604 && S_ISDIR(flist->sorted[mid_up]->mode)
2605 != S_ISDIR(f->mode))
2606 return -1;
2607 return mid_up;
2608 }
2609 if (diff < 0)
2610 low = mid_up + 1;
2611 else
2612 high = mid - 1;
2613 }
2614 return -1;
2615}
2616
2617/* Search for an identically-named item in the file list. Differs from
2618 * flist_find in that an item that agrees with "f" in directory-ness is
2619 * preferred but one that does not is still found. */
2620int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2621{
2622 mode_t save_mode;
2623 int ndx;
2624
2625 /* First look for an item that agrees in directory-ness. */
2626 ndx = flist_find(flist, f);
2627 if (ndx >= 0)
2628 return ndx;
2629
2630 /* Temporarily flip f->mode to look for an item of opposite
2631 * directory-ness. */
2632 save_mode = f->mode;
2633 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2634 ndx = flist_find(flist, f);
2635 f->mode = save_mode;
2636 return ndx;
2637}
2638
2639/*
2640 * Free up any resources a file_struct has allocated
2641 * and clear the file.
2642 */
2643void clear_file(struct file_struct *file)
2644{
2645 /* The +1 zeros out the first char of the basename. */
2646 memset(file, 0, FILE_STRUCT_LEN + 1);
2647 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2648 * entry. Likewise for len32 in the opposite direction. We assume
2649 * that we're alone for now since flist_find() will adjust the counts
2650 * it runs into that aren't up-to-date. */
2651 file->len32 = F_DEPTH(file) = 1;
2652}
2653
2654/* Allocate a new file list. */
2655struct file_list *flist_new(int flags, char *msg)
2656{
2657 struct file_list *flist;
2658
2659 if (!(flist = new0(struct file_list)))
2660 out_of_memory(msg);
2661
2662 if (flags & FLIST_TEMP) {
2663 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2664 out_of_memory,
2665 POOL_INTERN)))
2666 out_of_memory(msg);
2667 } else {
2668 /* This is a doubly linked list with prev looping back to
2669 * the end of the list, but the last next pointer is NULL. */
2670 if (!first_flist) {
2671 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2672 out_of_memory,
2673 POOL_INTERN);
2674 if (!flist->file_pool)
2675 out_of_memory(msg);
2676
2677 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2678
2679 first_flist = cur_flist = flist->prev = flist;
2680 } else {
2681 struct file_list *prev = first_flist->prev;
2682
2683 flist->file_pool = first_flist->file_pool;
2684
2685 flist->ndx_start = prev->ndx_start + prev->used + 1;
2686 flist->flist_num = prev->flist_num + 1;
2687
2688 flist->prev = prev;
2689 prev->next = first_flist->prev = flist;
2690 }
2691 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2692 flist_cnt++;
2693 }
2694
2695 return flist;
2696}
2697
2698/* Free up all elements in a flist. */
2699void flist_free(struct file_list *flist)
2700{
2701 if (!flist->prev) {
2702 /* Was FLIST_TEMP dir-list. */
2703 } else if (flist == flist->prev) {
2704 first_flist = cur_flist = NULL;
2705 file_total = 0;
2706 flist_cnt = 0;
2707 } else {
2708 if (flist == cur_flist)
2709 cur_flist = flist->next;
2710 if (flist == first_flist)
2711 first_flist = first_flist->next;
2712 else {
2713 flist->prev->next = flist->next;
2714 if (!flist->next)
2715 flist->next = first_flist;
2716 }
2717 flist->next->prev = flist->prev;
2718 file_total -= flist->used;
2719 flist_cnt--;
2720 }
2721
2722 if (!flist->prev || !flist_cnt)
2723 pool_destroy(flist->file_pool);
2724 else
2725 pool_free_old(flist->file_pool, flist->pool_boundary);
2726
2727 if (flist->sorted && flist->sorted != flist->files)
2728 free(flist->sorted);
2729 free(flist->files);
2730 free(flist);
2731}
2732
2733/* This routine ensures we don't have any duplicate names in our file list.
2734 * duplicate names can cause corruption because of the pipelining. */
2735static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2736{
2737 char fbuf[MAXPATHLEN];
2738 int i, prev_i;
2739
2740 if (!flist)
2741 return;
2742 if (flist->used == 0) {
2743 flist->high = -1;
2744 flist->low = 0;
2745 return;
2746 }
2747
2748 fsort(flist->sorted, flist->used);
2749
2750 if (!am_sender || inc_recurse) {
2751 for (i = prev_i = 0; i < flist->used; i++) {
2752 if (F_IS_ACTIVE(flist->sorted[i])) {
2753 prev_i = i;
2754 break;
2755 }
2756 }
2757 flist->low = prev_i;
2758 } else {
2759 i = prev_i = flist->used - 1;
2760 flist->low = 0;
2761 }
2762
2763 while (++i < flist->used) {
2764 int j;
2765 struct file_struct *file = flist->sorted[i];
2766
2767 if (!F_IS_ACTIVE(file))
2768 continue;
2769 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2770 j = prev_i;
2771 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2772 int save_mode = file->mode;
2773 /* Make sure that this directory doesn't duplicate a
2774 * non-directory earlier in the list. */
2775 flist->high = prev_i;
2776 file->mode = S_IFREG;
2777 j = flist_find(flist, file);
2778 file->mode = save_mode;
2779 } else
2780 j = -1;
2781 if (j >= 0) {
2782 int keep, drop;
2783 /* If one is a dir and the other is not, we want to
2784 * keep the dir because it might have contents in the
2785 * list. Otherwise keep the first one. */
2786 if (S_ISDIR(file->mode)) {
2787 struct file_struct *fp = flist->sorted[j];
2788 if (!S_ISDIR(fp->mode))
2789 keep = i, drop = j;
2790 else {
2791 if (am_sender)
2792 file->flags |= FLAG_DUPLICATE;
2793 else { /* Make sure we merge our vital flags. */
2794 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2795 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2796 }
2797 keep = j, drop = i;
2798 }
2799 } else
2800 keep = j, drop = i;
2801
2802 if (!am_sender) {
2803 if (DEBUG_GTE(DUP, 1)) {
2804 rprintf(FINFO,
2805 "removing duplicate name %s from file list (%d)\n",
2806 f_name(file, fbuf), drop + flist->ndx_start);
2807 }
2808 clear_file(flist->sorted[drop]);
2809 }
2810
2811 if (keep == i) {
2812 if (flist->low == drop) {
2813 for (j = drop + 1;
2814 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2815 j++) {}
2816 flist->low = j;
2817 }
2818 prev_i = i;
2819 }
2820 } else
2821 prev_i = i;
2822 }
2823 flist->high = prev_i;
2824
2825 if (strip_root) {
2826 /* We need to strip off the leading slashes for relative
2827 * paths, but this must be done _after_ the sorting phase. */
2828 for (i = flist->low; i <= flist->high; i++) {
2829 struct file_struct *file = flist->sorted[i];
2830
2831 if (!file->dirname)
2832 continue;
2833 while (*file->dirname == '/')
2834 file->dirname++;
2835 if (!*file->dirname)
2836 file->dirname = NULL;
2837 }
2838 }
2839
2840 if (prune_empty_dirs && !am_sender) {
2841 int j, prev_depth = 0;
2842
2843 prev_i = 0; /* It's OK that this isn't really true. */
2844
2845 for (i = flist->low; i <= flist->high; i++) {
2846 struct file_struct *fp, *file = flist->sorted[i];
2847
2848 /* This temporarily abuses the F_DEPTH() value for a
2849 * directory that is in a chain that might get pruned.
2850 * We restore the old value if it gets a reprieve. */
2851 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2852 /* Dump empty dirs when coming back down. */
2853 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2854 fp = flist->sorted[prev_i];
2855 if (F_DEPTH(fp) >= 0)
2856 break;
2857 prev_i = -F_DEPTH(fp)-1;
2858 clear_file(fp);
2859 }
2860 prev_depth = F_DEPTH(file);
2861 if (is_excluded(f_name(file, fbuf), 1,
2862 ALL_FILTERS)) {
2863 /* Keep dirs through this dir. */
2864 for (j = prev_depth-1; ; j--) {
2865 fp = flist->sorted[prev_i];
2866 if (F_DEPTH(fp) >= 0)
2867 break;
2868 prev_i = -F_DEPTH(fp)-1;
2869 F_DEPTH(fp) = j;
2870 }
2871 } else
2872 F_DEPTH(file) = -prev_i-1;
2873 prev_i = i;
2874 } else {
2875 /* Keep dirs through this non-dir. */
2876 for (j = prev_depth; ; j--) {
2877 fp = flist->sorted[prev_i];
2878 if (F_DEPTH(fp) >= 0)
2879 break;
2880 prev_i = -F_DEPTH(fp)-1;
2881 F_DEPTH(fp) = j;
2882 }
2883 }
2884 }
2885 /* Dump all remaining empty dirs. */
2886 while (1) {
2887 struct file_struct *fp = flist->sorted[prev_i];
2888 if (F_DEPTH(fp) >= 0)
2889 break;
2890 prev_i = -F_DEPTH(fp)-1;
2891 clear_file(fp);
2892 }
2893
2894 for (i = flist->low; i <= flist->high; i++) {
2895 if (F_IS_ACTIVE(flist->sorted[i]))
2896 break;
2897 }
2898 flist->low = i;
2899 for (i = flist->high; i >= flist->low; i--) {
2900 if (F_IS_ACTIVE(flist->sorted[i]))
2901 break;
2902 }
2903 flist->high = i;
2904 }
2905}
2906
2907static void output_flist(struct file_list *flist)
2908{
2909 char uidbuf[16], gidbuf[16], depthbuf[16];
2910 struct file_struct *file;
2911 const char *root, *dir, *slash, *name, *trail;
2912 const char *who = who_am_i();
2913 int i;
2914
2915 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2916 who, flist->ndx_start, flist->used, flist->low, flist->high);
2917 for (i = 0; i < flist->used; i++) {
2918 file = flist->files[i];
2919 if ((am_root || am_sender) && uid_ndx) {
2920 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2921 F_OWNER(file));
2922 } else
2923 *uidbuf = '\0';
2924 if (gid_ndx) {
2925 static char parens[] = "(\0)\0\0\0";
2926 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2927 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2928 pp, F_GROUP(file), pp + 2);
2929 } else
2930 *gidbuf = '\0';
2931 if (!am_sender)
2932 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2933 if (F_IS_ACTIVE(file)) {
2934 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2935 if ((dir = file->dirname) == NULL)
2936 dir = slash = "";
2937 else
2938 slash = "/";
2939 name = file->basename;
2940 trail = S_ISDIR(file->mode) ? "/" : "";
2941 } else
2942 root = dir = slash = name = trail = "";
2943 rprintf(FINFO,
2944 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
2945 who, i + flist->ndx_start,
2946 root, dir, slash, name, trail,
2947 (int)file->mode, comma_num(F_LENGTH(file)),
2948 uidbuf, gidbuf, file->flags);
2949 }
2950}
2951
2952enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2953enum fnc_type { t_PATH, t_ITEM };
2954
2955static int found_prefix;
2956
2957/* Compare the names of two file_struct entities, similar to how strcmp()
2958 * would do if it were operating on the joined strings.
2959 *
2960 * Some differences beginning with protocol_version 29: (1) directory names
2961 * are compared with an assumed trailing slash so that they compare in a
2962 * way that would cause them to sort immediately prior to any content they
2963 * may have; (2) a directory of any name compares after a non-directory of
2964 * any name at the same depth; (3) a directory with name "." compares prior
2965 * to anything else. These changes mean that a directory and a non-dir
2966 * with the same name will not compare as equal (protocol_version >= 29).
2967 *
2968 * The dirname component can be an empty string, but the basename component
2969 * cannot (and never is in the current codebase). The basename component
2970 * may be NULL (for a removed item), in which case it is considered to be
2971 * after any existing item. */
2972int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2973{
2974 int dif;
2975 const uchar *c1, *c2;
2976 enum fnc_state state1, state2;
2977 enum fnc_type type1, type2;
2978 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2979
2980 if (!f1 || !F_IS_ACTIVE(f1)) {
2981 if (!f2 || !F_IS_ACTIVE(f2))
2982 return 0;
2983 return -1;
2984 }
2985 if (!f2 || !F_IS_ACTIVE(f2))
2986 return 1;
2987
2988 c1 = (uchar*)f1->dirname;
2989 c2 = (uchar*)f2->dirname;
2990 if (c1 == c2)
2991 c1 = c2 = NULL;
2992 if (!c1) {
2993 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2994 c1 = (const uchar*)f1->basename;
2995 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2996 type1 = t_ITEM;
2997 state1 = s_TRAILING;
2998 c1 = (uchar*)"";
2999 } else
3000 state1 = s_BASE;
3001 } else {
3002 type1 = t_path;
3003 state1 = s_DIR;
3004 }
3005 if (!c2) {
3006 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3007 c2 = (const uchar*)f2->basename;
3008 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3009 type2 = t_ITEM;
3010 state2 = s_TRAILING;
3011 c2 = (uchar*)"";
3012 } else
3013 state2 = s_BASE;
3014 } else {
3015 type2 = t_path;
3016 state2 = s_DIR;
3017 }
3018
3019 if (type1 != type2)
3020 return type1 == t_PATH ? 1 : -1;
3021
3022 do {
3023 if (!*c1) {
3024 switch (state1) {
3025 case s_DIR:
3026 state1 = s_SLASH;
3027 c1 = (uchar*)"/";
3028 break;
3029 case s_SLASH:
3030 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3031 c1 = (const uchar*)f1->basename;
3032 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3033 type1 = t_ITEM;
3034 state1 = s_TRAILING;
3035 c1 = (uchar*)"";
3036 } else
3037 state1 = s_BASE;
3038 break;
3039 case s_BASE:
3040 state1 = s_TRAILING;
3041 if (type1 == t_PATH) {
3042 c1 = (uchar*)"/";
3043 break;
3044 }
3045 /* FALL THROUGH */
3046 case s_TRAILING:
3047 type1 = t_ITEM;
3048 break;
3049 }
3050 if (*c2 && type1 != type2)
3051 return type1 == t_PATH ? 1 : -1;
3052 }
3053 if (!*c2) {
3054 switch (state2) {
3055 case s_DIR:
3056 state2 = s_SLASH;
3057 c2 = (uchar*)"/";
3058 break;
3059 case s_SLASH:
3060 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3061 c2 = (const uchar*)f2->basename;
3062 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3063 type2 = t_ITEM;
3064 state2 = s_TRAILING;
3065 c2 = (uchar*)"";
3066 } else
3067 state2 = s_BASE;
3068 break;
3069 case s_BASE:
3070 state2 = s_TRAILING;
3071 if (type2 == t_PATH) {
3072 c2 = (uchar*)"/";
3073 break;
3074 }
3075 /* FALL THROUGH */
3076 case s_TRAILING:
3077 found_prefix = 1;
3078 if (!*c1)
3079 return 0;
3080 type2 = t_ITEM;
3081 break;
3082 }
3083 if (type1 != type2)
3084 return type1 == t_PATH ? 1 : -1;
3085 }
3086 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3087
3088 return dif;
3089}
3090
3091/* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3092 * not match if f2's basename is not an exact match of a path element in f1.
3093 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3094int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3095{
3096 found_prefix = 0;
3097 f_name_cmp(f1, f2);
3098 return found_prefix;
3099}
3100
3101char *f_name_buf(void)
3102{
3103 static char names[5][MAXPATHLEN];
3104 static unsigned int n;
3105
3106 n = (n + 1) % (sizeof names / sizeof names[0]);
3107
3108 return names[n];
3109}
3110
3111/* Return a copy of the full filename of a flist entry, using the indicated
3112 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3113 * done because we checked the size when creating the file_struct entry.
3114 */
3115char *f_name(const struct file_struct *f, char *fbuf)
3116{
3117 if (!f || !F_IS_ACTIVE(f))
3118 return NULL;
3119
3120 if (!fbuf)
3121 fbuf = f_name_buf();
3122
3123 if (f->dirname) {
3124 int len = strlen(f->dirname);
3125 memcpy(fbuf, f->dirname, len);
3126 fbuf[len] = '/';
3127 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3128 } else
3129 strlcpy(fbuf, f->basename, MAXPATHLEN);
3130
3131 return fbuf;
3132}
3133
3134/* Do a non-recursive scan of the named directory, possibly ignoring all
3135 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3136 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3137 * buffer (the functions we call will append names onto the end, but the old
3138 * dir value will be restored on exit). */
3139struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
3140{
3141 struct file_list *dirlist;
3142 char dirbuf[MAXPATHLEN];
3143 int save_recurse = recurse;
3144 int save_xfer_dirs = xfer_dirs;
3145 int save_prune_empty_dirs = prune_empty_dirs;
3146
3147 if (dlen < 0) {
3148 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3149 if (dlen >= MAXPATHLEN)
3150 return NULL;
3151 dirname = dirbuf;
3152 }
3153
3154 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3155
3156 recurse = 0;
3157 xfer_dirs = 1;
3158 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, FLAG_CONTENT_DIR);
3159 xfer_dirs = save_xfer_dirs;
3160 recurse = save_recurse;
3161 if (INFO_GTE(PROGRESS, 1))
3162 flist_count_offset += dirlist->used;
3163
3164 prune_empty_dirs = 0;
3165 dirlist->sorted = dirlist->files;
3166 flist_sort_and_clean(dirlist, 0);
3167 prune_empty_dirs = save_prune_empty_dirs;
3168
3169 if (DEBUG_GTE(FLIST, 3))
3170 output_flist(dirlist);
3171
3172 return dirlist;
3173}