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