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