Improved link_or_rename() to handle prefer_rename better.
[rsync/rsync.git] / flist.c
... / ...
CommitLineData
1/*
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2009 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24#include "ifuncs.h"
25#include "rounding.h"
26#include "inums.h"
27#include "io.h"
28
29extern int am_root;
30extern int am_server;
31extern int am_daemon;
32extern int am_sender;
33extern int am_generator;
34extern int inc_recurse;
35extern int always_checksum;
36extern int module_id;
37extern int ignore_errors;
38extern int numeric_ids;
39extern int recurse;
40extern int use_qsort;
41extern int xfer_dirs;
42extern int filesfrom_fd;
43extern int one_file_system;
44extern int copy_dirlinks;
45extern int keep_dirlinks;
46extern int preserve_uid;
47extern int preserve_gid;
48extern int preserve_acls;
49extern int preserve_xattrs;
50extern int preserve_links;
51extern int preserve_hard_links;
52extern int preserve_devices;
53extern int preserve_specials;
54extern int missing_args;
55extern int uid_ndx;
56extern int gid_ndx;
57extern int eol_nulls;
58extern int relative_paths;
59extern int implied_dirs;
60extern int file_extra_cnt;
61extern int ignore_perishable;
62extern int non_perishable_cnt;
63extern int prune_empty_dirs;
64extern int copy_links;
65extern int copy_unsafe_links;
66extern int protocol_version;
67extern int sanitize_paths;
68extern int munge_symlinks;
69extern int need_unsorted_flist;
70extern int sender_symlink_iconv;
71extern int output_needs_newline;
72extern int sender_keeps_checksum;
73extern int unsort_ndx;
74extern struct stats stats;
75extern char *filesfrom_host;
76extern char *usermap, *groupmap;
77
78extern char curr_dir[MAXPATHLEN];
79
80extern struct chmod_mode_struct *chmod_modes;
81
82extern 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 || missing_args == 0) {
2181 /* This is a transfer error, but inhibit deletion
2182 * only if we might be omitting an existing file. */
2183 if (errno != ENOENT)
2184 io_error |= IOERR_GENERAL;
2185 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2186 full_fname(fbuf));
2187 continue;
2188 } else if (missing_args == 1) {
2189 /* Just ignore the arg. */
2190 continue;
2191 } else /* (missing_args == 2) */ {
2192 /* Send the arg as a "missing" entry with
2193 * mode 0, which tells the generator to delete it. */
2194 memset(&st, 0, sizeof st);
2195 }
2196 }
2197
2198 /* A dot-dir should not be excluded! */
2199 if (name_type != DOTDIR_NAME && st.st_mode != 0
2200 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2201 continue;
2202
2203 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2204 rprintf(FINFO, "skipping directory %s\n", fbuf);
2205 continue;
2206 }
2207
2208 if (inc_recurse && relative_paths && *fbuf) {
2209 if ((p = strchr(fbuf+1, '/')) != NULL) {
2210 if (p - fbuf == 1 && *fbuf == '.') {
2211 if ((fn = strchr(p+1, '/')) != NULL)
2212 p = fn;
2213 } else
2214 fn = p;
2215 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
2216 if (fn == p)
2217 continue;
2218 }
2219 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2220 /* Send the implied directories at the start of the
2221 * source spec, so we get their permissions right. */
2222 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2223 }
2224
2225 if (one_file_system)
2226 filesystem_dev = st.st_dev;
2227
2228 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2229 struct file_struct *file;
2230 file = send_file_name(f, flist, fbuf, &st,
2231 FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2232 NO_FILTERS);
2233 if (!file)
2234 continue;
2235 if (inc_recurse) {
2236 if (name_type == DOTDIR_NAME) {
2237 if (send_dir_depth < 0) {
2238 send_dir_depth = 0;
2239 change_local_filter_dir(fbuf, len, send_dir_depth);
2240 }
2241 send_directory(f, flist, fbuf, len, flags);
2242 }
2243 } else
2244 send_if_directory(f, flist, file, fbuf, len, flags);
2245 } else
2246 send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2247 }
2248
2249 gettimeofday(&end_tv, NULL);
2250 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2251 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2252 if (stats.flist_buildtime == 0)
2253 stats.flist_buildtime = 1;
2254 start_tv = end_tv;
2255
2256 /* Indicate end of file list */
2257 if (protocol_version < 31 || io_error == 0 || ignore_errors)
2258 write_byte(f, 0);
2259 else {
2260 write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2261 write_varint(f, io_error);
2262 }
2263
2264#ifdef SUPPORT_HARD_LINKS
2265 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2266 idev_destroy();
2267#endif
2268
2269 if (show_filelist_p())
2270 finish_filelist_progress(flist);
2271
2272 gettimeofday(&end_tv, NULL);
2273 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2274 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2275
2276 /* When converting names, both sides keep an unsorted file-list array
2277 * because the names will differ on the sending and receiving sides
2278 * (both sides will use the unsorted index number for each item). */
2279
2280 /* Sort the list without removing any duplicates. This allows the
2281 * receiving side to ask for whatever name it kept. For incremental
2282 * recursion mode, the sender marks duplicate dirs so that it can
2283 * send them together in a single file-list. */
2284 if (need_unsorted_flist) {
2285 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2286 out_of_memory("send_file_list");
2287 memcpy(flist->sorted, flist->files,
2288 flist->used * sizeof (struct file_struct*));
2289 } else
2290 flist->sorted = flist->files;
2291 flist_sort_and_clean(flist, 0);
2292 file_total += flist->used;
2293
2294 if (numeric_ids <= 0 && !inc_recurse)
2295 send_id_list(f);
2296
2297 set_msg_fd_in(-1);
2298
2299 /* send the io_error flag */
2300 if (protocol_version < 30)
2301 write_int(f, ignore_errors ? 0 : io_error);
2302 else if (io_error && protocol_version == 30 && !ignore_errors)
2303 send_msg_int(MSG_IO_ERROR, io_error);
2304
2305 if (disable_buffering)
2306 io_end_buffering_out();
2307
2308 stats.flist_size = stats.total_written - start_write;
2309 stats.num_files = flist->used;
2310
2311 if (DEBUG_GTE(FLIST, 3))
2312 output_flist(flist);
2313
2314 if (DEBUG_GTE(FLIST, 2))
2315 rprintf(FINFO, "send_file_list done\n");
2316
2317 if (inc_recurse) {
2318 send_dir_depth = 1;
2319 add_dirs_to_tree(-1, flist, stats.num_dirs);
2320 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2321 flist->parent_ndx = -1;
2322 flist_done_allocating(flist);
2323 if (send_dir_ndx < 0) {
2324 write_ndx(f, NDX_FLIST_EOF);
2325 flist_eof = 1;
2326 }
2327 else if (file_total == 1) {
2328 /* If we're creating incremental file-lists and there
2329 * was just 1 item in the first file-list, send 1 more
2330 * file-list to check if this is a 1-file xfer. */
2331 send_extra_file_list(f, 1);
2332 }
2333 } else
2334 flist_eof = 1;
2335
2336 return flist;
2337}
2338
2339struct file_list *recv_file_list(int f)
2340{
2341 struct file_list *flist;
2342 int dstart, flags;
2343 int64 start_read;
2344
2345 if (!first_flist) {
2346 if (show_filelist_p())
2347 start_filelist_progress("receiving file list");
2348 else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2349 rprintf(FCLIENT, "receiving incremental file list\n");
2350 rprintf(FLOG, "receiving file list\n");
2351 if (usermap)
2352 parse_name_map(usermap, True);
2353 if (groupmap)
2354 parse_name_map(groupmap, False);
2355 }
2356
2357 start_read = stats.total_read;
2358
2359#ifdef SUPPORT_HARD_LINKS
2360 if (preserve_hard_links && !first_flist)
2361 init_hard_links();
2362#endif
2363
2364 flist = flist_new(0, "recv_file_list");
2365
2366 if (inc_recurse) {
2367 if (flist->ndx_start == 1)
2368 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2369 dstart = dir_flist->used;
2370 } else {
2371 dir_flist = flist;
2372 dstart = 0;
2373 }
2374
2375 while ((flags = read_byte(f)) != 0) {
2376 struct file_struct *file;
2377
2378 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2379 flags |= read_byte(f) << 8;
2380
2381 if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2382 int err;
2383 if (protocol_version < 31) {
2384 rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2385 exit_cleanup(RERR_PROTOCOL);
2386 }
2387 err = read_varint(f);
2388 if (!ignore_errors)
2389 io_error |= err;
2390 break;
2391 }
2392
2393 flist_expand(flist, 1);
2394 file = recv_file_entry(flist, flags, f);
2395
2396 if (S_ISREG(file->mode)) {
2397 /* Already counted */
2398 } else if (S_ISDIR(file->mode)) {
2399 if (inc_recurse) {
2400 flist_expand(dir_flist, 1);
2401 dir_flist->files[dir_flist->used++] = file;
2402 }
2403 stats.num_dirs++;
2404 } else if (S_ISLNK(file->mode))
2405 stats.num_symlinks++;
2406 else if (IS_DEVICE(file->mode))
2407 stats.num_symlinks++;
2408 else
2409 stats.num_specials++;
2410
2411 flist->files[flist->used++] = file;
2412
2413 maybe_emit_filelist_progress(flist->used);
2414
2415 if (DEBUG_GTE(FLIST, 2)) {
2416 char *name = f_name(file, NULL);
2417 rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2418 }
2419 }
2420 file_total += flist->used;
2421
2422 if (DEBUG_GTE(FLIST, 2))
2423 rprintf(FINFO, "received %d names\n", flist->used);
2424
2425 if (show_filelist_p())
2426 finish_filelist_progress(flist);
2427
2428 if (need_unsorted_flist) {
2429 /* Create an extra array of index pointers that we can sort for
2430 * the generator's use (for wading through the files in sorted
2431 * order and for calling flist_find()). We keep the "files"
2432 * list unsorted for our exchange of index numbers with the
2433 * other side (since their names may not sort the same). */
2434 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2435 out_of_memory("recv_file_list");
2436 memcpy(flist->sorted, flist->files,
2437 flist->used * sizeof (struct file_struct*));
2438 if (inc_recurse && dir_flist->used > dstart) {
2439 static int dir_flist_malloced = 0;
2440 if (dir_flist_malloced < dir_flist->malloced) {
2441 dir_flist->sorted = realloc_array(dir_flist->sorted,
2442 struct file_struct *,
2443 dir_flist->malloced);
2444 dir_flist_malloced = dir_flist->malloced;
2445 }
2446 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2447 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2448 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2449 }
2450 } else {
2451 flist->sorted = flist->files;
2452 if (inc_recurse && dir_flist->used > dstart) {
2453 dir_flist->sorted = dir_flist->files;
2454 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2455 }
2456 }
2457
2458 if (inc_recurse)
2459 flist_done_allocating(flist);
2460 else if (f >= 0) {
2461 recv_id_list(f, flist);
2462 flist_eof = 1;
2463 }
2464
2465 flist_sort_and_clean(flist, relative_paths);
2466
2467 if (protocol_version < 30) {
2468 /* Recv the io_error flag */
2469 int err = read_int(f);
2470 if (!ignore_errors)
2471 io_error |= err;
2472 } else if (inc_recurse && flist->ndx_start == 1) {
2473 if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2474 flist->parent_ndx = -1;
2475 }
2476
2477 if (DEBUG_GTE(FLIST, 3))
2478 output_flist(flist);
2479
2480 if (DEBUG_GTE(FLIST, 2))
2481 rprintf(FINFO, "recv_file_list done\n");
2482
2483 stats.flist_size += stats.total_read - start_read;
2484 stats.num_files += flist->used;
2485
2486 return flist;
2487}
2488
2489/* This is only used once by the receiver if the very first file-list
2490 * has exactly one item in it. */
2491void recv_additional_file_list(int f)
2492{
2493 struct file_list *flist;
2494 int ndx = read_ndx(f);
2495 if (ndx == NDX_FLIST_EOF) {
2496 flist_eof = 1;
2497 change_local_filter_dir(NULL, 0, 0);
2498 } else {
2499 ndx = NDX_FLIST_OFFSET - ndx;
2500 if (ndx < 0 || ndx >= dir_flist->used) {
2501 ndx = NDX_FLIST_OFFSET - ndx;
2502 rprintf(FERROR,
2503 "[%s] Invalid dir index: %d (%d - %d)\n",
2504 who_am_i(), ndx, NDX_FLIST_OFFSET,
2505 NDX_FLIST_OFFSET - dir_flist->used + 1);
2506 exit_cleanup(RERR_PROTOCOL);
2507 }
2508 if (DEBUG_GTE(FLIST, 3)) {
2509 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2510 who_am_i(), ndx);
2511 }
2512 flist = recv_file_list(f);
2513 flist->parent_ndx = ndx;
2514 }
2515}
2516
2517/* Search for an identically-named item in the file list. Note that the
2518 * items must agree in their directory-ness, or no match is returned. */
2519int flist_find(struct file_list *flist, struct file_struct *f)
2520{
2521 int low = flist->low, high = flist->high;
2522 int diff, mid, mid_up;
2523
2524 while (low <= high) {
2525 mid = (low + high) / 2;
2526 if (F_IS_ACTIVE(flist->sorted[mid]))
2527 mid_up = mid;
2528 else {
2529 /* Scan for the next non-empty entry using the cached
2530 * distance values. If the value isn't fully up-to-
2531 * date, update it. */
2532 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2533 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2534 do {
2535 mid_up += F_DEPTH(flist->sorted[mid_up]);
2536 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2537 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2538 }
2539 if (mid_up > high) {
2540 /* If there's nothing left above us, set high to
2541 * a non-empty entry below us and continue. */
2542 high = mid - (int)flist->sorted[mid]->len32;
2543 if (!F_IS_ACTIVE(flist->sorted[high])) {
2544 do {
2545 high -= (int)flist->sorted[high]->len32;
2546 } while (!F_IS_ACTIVE(flist->sorted[high]));
2547 flist->sorted[mid]->len32 = mid - high;
2548 }
2549 continue;
2550 }
2551 }
2552 diff = f_name_cmp(flist->sorted[mid_up], f);
2553 if (diff == 0) {
2554 if (protocol_version < 29
2555 && S_ISDIR(flist->sorted[mid_up]->mode)
2556 != S_ISDIR(f->mode))
2557 return -1;
2558 return mid_up;
2559 }
2560 if (diff < 0)
2561 low = mid_up + 1;
2562 else
2563 high = mid - 1;
2564 }
2565 return -1;
2566}
2567
2568/* Search for an identically-named item in the file list. Differs from
2569 * flist_find in that an item that agrees with "f" in directory-ness is
2570 * preferred but one that does not is still found. */
2571int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2572{
2573 mode_t save_mode;
2574 int ndx;
2575
2576 /* First look for an item that agrees in directory-ness. */
2577 ndx = flist_find(flist, f);
2578 if (ndx >= 0)
2579 return ndx;
2580
2581 /* Temporarily flip f->mode to look for an item of opposite
2582 * directory-ness. */
2583 save_mode = f->mode;
2584 f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2585 ndx = flist_find(flist, f);
2586 f->mode = save_mode;
2587 return ndx;
2588}
2589
2590/*
2591 * Free up any resources a file_struct has allocated
2592 * and clear the file.
2593 */
2594void clear_file(struct file_struct *file)
2595{
2596 /* The +1 zeros out the first char of the basename. */
2597 memset(file, 0, FILE_STRUCT_LEN + 1);
2598 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2599 * entry. Likewise for len32 in the opposite direction. We assume
2600 * that we're alone for now since flist_find() will adjust the counts
2601 * it runs into that aren't up-to-date. */
2602 file->len32 = F_DEPTH(file) = 1;
2603}
2604
2605/* Allocate a new file list. */
2606struct file_list *flist_new(int flags, char *msg)
2607{
2608 struct file_list *flist;
2609
2610 if (!(flist = new0(struct file_list)))
2611 out_of_memory(msg);
2612
2613 if (flags & FLIST_TEMP) {
2614 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2615 out_of_memory,
2616 POOL_INTERN)))
2617 out_of_memory(msg);
2618 } else {
2619 /* This is a doubly linked list with prev looping back to
2620 * the end of the list, but the last next pointer is NULL. */
2621 if (!first_flist) {
2622 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2623 out_of_memory,
2624 POOL_INTERN);
2625 if (!flist->file_pool)
2626 out_of_memory(msg);
2627
2628 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2629
2630 first_flist = cur_flist = flist->prev = flist;
2631 } else {
2632 struct file_list *prev = first_flist->prev;
2633
2634 flist->file_pool = first_flist->file_pool;
2635
2636 flist->ndx_start = prev->ndx_start + prev->used + 1;
2637 flist->flist_num = prev->flist_num + 1;
2638
2639 flist->prev = prev;
2640 prev->next = first_flist->prev = flist;
2641 }
2642 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2643 flist_cnt++;
2644 }
2645
2646 return flist;
2647}
2648
2649/* Free up all elements in a flist. */
2650void flist_free(struct file_list *flist)
2651{
2652 if (!flist->prev) {
2653 /* Was FLIST_TEMP dir-list. */
2654 } else if (flist == flist->prev) {
2655 first_flist = cur_flist = NULL;
2656 file_total = 0;
2657 flist_cnt = 0;
2658 } else {
2659 if (flist == cur_flist)
2660 cur_flist = flist->next;
2661 if (flist == first_flist)
2662 first_flist = first_flist->next;
2663 else {
2664 flist->prev->next = flist->next;
2665 if (!flist->next)
2666 flist->next = first_flist;
2667 }
2668 flist->next->prev = flist->prev;
2669 file_total -= flist->used;
2670 flist_cnt--;
2671 }
2672
2673 if (!flist->prev || !flist_cnt)
2674 pool_destroy(flist->file_pool);
2675 else
2676 pool_free_old(flist->file_pool, flist->pool_boundary);
2677
2678 if (flist->sorted && flist->sorted != flist->files)
2679 free(flist->sorted);
2680 free(flist->files);
2681 free(flist);
2682}
2683
2684/* This routine ensures we don't have any duplicate names in our file list.
2685 * duplicate names can cause corruption because of the pipelining. */
2686static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2687{
2688 char fbuf[MAXPATHLEN];
2689 int i, prev_i;
2690
2691 if (!flist)
2692 return;
2693 if (flist->used == 0) {
2694 flist->high = -1;
2695 flist->low = 0;
2696 return;
2697 }
2698
2699 fsort(flist->sorted, flist->used);
2700
2701 if (!am_sender || inc_recurse) {
2702 for (i = prev_i = 0; i < flist->used; i++) {
2703 if (F_IS_ACTIVE(flist->sorted[i])) {
2704 prev_i = i;
2705 break;
2706 }
2707 }
2708 flist->low = prev_i;
2709 } else {
2710 i = prev_i = flist->used - 1;
2711 flist->low = 0;
2712 }
2713
2714 while (++i < flist->used) {
2715 int j;
2716 struct file_struct *file = flist->sorted[i];
2717
2718 if (!F_IS_ACTIVE(file))
2719 continue;
2720 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2721 j = prev_i;
2722 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2723 int save_mode = file->mode;
2724 /* Make sure that this directory doesn't duplicate a
2725 * non-directory earlier in the list. */
2726 flist->high = prev_i;
2727 file->mode = S_IFREG;
2728 j = flist_find(flist, file);
2729 file->mode = save_mode;
2730 } else
2731 j = -1;
2732 if (j >= 0) {
2733 int keep, drop;
2734 /* If one is a dir and the other is not, we want to
2735 * keep the dir because it might have contents in the
2736 * list. Otherwise keep the first one. */
2737 if (S_ISDIR(file->mode)) {
2738 struct file_struct *fp = flist->sorted[j];
2739 if (!S_ISDIR(fp->mode))
2740 keep = i, drop = j;
2741 else {
2742 if (am_sender)
2743 file->flags |= FLAG_DUPLICATE;
2744 else { /* Make sure we merge our vital flags. */
2745 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2746 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2747 }
2748 keep = j, drop = i;
2749 }
2750 } else
2751 keep = j, drop = i;
2752
2753 if (!am_sender) {
2754 if (DEBUG_GTE(DUP, 1)) {
2755 rprintf(FINFO,
2756 "removing duplicate name %s from file list (%d)\n",
2757 f_name(file, fbuf), drop + flist->ndx_start);
2758 }
2759 clear_file(flist->sorted[drop]);
2760 }
2761
2762 if (keep == i) {
2763 if (flist->low == drop) {
2764 for (j = drop + 1;
2765 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2766 j++) {}
2767 flist->low = j;
2768 }
2769 prev_i = i;
2770 }
2771 } else
2772 prev_i = i;
2773 }
2774 flist->high = prev_i;
2775
2776 if (strip_root) {
2777 /* We need to strip off the leading slashes for relative
2778 * paths, but this must be done _after_ the sorting phase. */
2779 for (i = flist->low; i <= flist->high; i++) {
2780 struct file_struct *file = flist->sorted[i];
2781
2782 if (!file->dirname)
2783 continue;
2784 while (*file->dirname == '/')
2785 file->dirname++;
2786 if (!*file->dirname)
2787 file->dirname = NULL;
2788 }
2789 }
2790
2791 if (prune_empty_dirs && !am_sender) {
2792 int j, prev_depth = 0;
2793
2794 prev_i = 0; /* It's OK that this isn't really true. */
2795
2796 for (i = flist->low; i <= flist->high; i++) {
2797 struct file_struct *fp, *file = flist->sorted[i];
2798
2799 /* This temporarily abuses the F_DEPTH() value for a
2800 * directory that is in a chain that might get pruned.
2801 * We restore the old value if it gets a reprieve. */
2802 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2803 /* Dump empty dirs when coming back down. */
2804 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2805 fp = flist->sorted[prev_i];
2806 if (F_DEPTH(fp) >= 0)
2807 break;
2808 prev_i = -F_DEPTH(fp)-1;
2809 clear_file(fp);
2810 }
2811 prev_depth = F_DEPTH(file);
2812 if (is_excluded(f_name(file, fbuf), 1,
2813 ALL_FILTERS)) {
2814 /* Keep dirs through this dir. */
2815 for (j = prev_depth-1; ; j--) {
2816 fp = flist->sorted[prev_i];
2817 if (F_DEPTH(fp) >= 0)
2818 break;
2819 prev_i = -F_DEPTH(fp)-1;
2820 F_DEPTH(fp) = j;
2821 }
2822 } else
2823 F_DEPTH(file) = -prev_i-1;
2824 prev_i = i;
2825 } else {
2826 /* Keep dirs through this non-dir. */
2827 for (j = prev_depth; ; j--) {
2828 fp = flist->sorted[prev_i];
2829 if (F_DEPTH(fp) >= 0)
2830 break;
2831 prev_i = -F_DEPTH(fp)-1;
2832 F_DEPTH(fp) = j;
2833 }
2834 }
2835 }
2836 /* Dump all remaining empty dirs. */
2837 while (1) {
2838 struct file_struct *fp = flist->sorted[prev_i];
2839 if (F_DEPTH(fp) >= 0)
2840 break;
2841 prev_i = -F_DEPTH(fp)-1;
2842 clear_file(fp);
2843 }
2844
2845 for (i = flist->low; i <= flist->high; i++) {
2846 if (F_IS_ACTIVE(flist->sorted[i]))
2847 break;
2848 }
2849 flist->low = i;
2850 for (i = flist->high; i >= flist->low; i--) {
2851 if (F_IS_ACTIVE(flist->sorted[i]))
2852 break;
2853 }
2854 flist->high = i;
2855 }
2856}
2857
2858static void output_flist(struct file_list *flist)
2859{
2860 char uidbuf[16], gidbuf[16], depthbuf[16];
2861 struct file_struct *file;
2862 const char *root, *dir, *slash, *name, *trail;
2863 const char *who = who_am_i();
2864 int i;
2865
2866 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2867 who, flist->ndx_start, flist->used, flist->low, flist->high);
2868 for (i = 0; i < flist->used; i++) {
2869 file = flist->files[i];
2870 if ((am_root || am_sender) && uid_ndx) {
2871 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2872 F_OWNER(file));
2873 } else
2874 *uidbuf = '\0';
2875 if (gid_ndx) {
2876 static char parens[] = "(\0)\0\0\0";
2877 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2878 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2879 pp, F_GROUP(file), pp + 2);
2880 } else
2881 *gidbuf = '\0';
2882 if (!am_sender)
2883 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2884 if (F_IS_ACTIVE(file)) {
2885 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2886 if ((dir = file->dirname) == NULL)
2887 dir = slash = "";
2888 else
2889 slash = "/";
2890 name = file->basename;
2891 trail = S_ISDIR(file->mode) ? "/" : "";
2892 } else
2893 root = dir = slash = name = trail = "";
2894 rprintf(FINFO,
2895 "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
2896 who, i + flist->ndx_start,
2897 root, dir, slash, name, trail,
2898 (int)file->mode, comma_num(F_LENGTH(file)),
2899 uidbuf, gidbuf, file->flags);
2900 }
2901}
2902
2903enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2904enum fnc_type { t_PATH, t_ITEM };
2905
2906static int found_prefix;
2907
2908/* Compare the names of two file_struct entities, similar to how strcmp()
2909 * would do if it were operating on the joined strings.
2910 *
2911 * Some differences beginning with protocol_version 29: (1) directory names
2912 * are compared with an assumed trailing slash so that they compare in a
2913 * way that would cause them to sort immediately prior to any content they
2914 * may have; (2) a directory of any name compares after a non-directory of
2915 * any name at the same depth; (3) a directory with name "." compares prior
2916 * to anything else. These changes mean that a directory and a non-dir
2917 * with the same name will not compare as equal (protocol_version >= 29).
2918 *
2919 * The dirname component can be an empty string, but the basename component
2920 * cannot (and never is in the current codebase). The basename component
2921 * may be NULL (for a removed item), in which case it is considered to be
2922 * after any existing item. */
2923int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2924{
2925 int dif;
2926 const uchar *c1, *c2;
2927 enum fnc_state state1, state2;
2928 enum fnc_type type1, type2;
2929 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2930
2931 if (!f1 || !F_IS_ACTIVE(f1)) {
2932 if (!f2 || !F_IS_ACTIVE(f2))
2933 return 0;
2934 return -1;
2935 }
2936 if (!f2 || !F_IS_ACTIVE(f2))
2937 return 1;
2938
2939 c1 = (uchar*)f1->dirname;
2940 c2 = (uchar*)f2->dirname;
2941 if (c1 == c2)
2942 c1 = c2 = NULL;
2943 if (!c1) {
2944 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2945 c1 = (const uchar*)f1->basename;
2946 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2947 type1 = t_ITEM;
2948 state1 = s_TRAILING;
2949 c1 = (uchar*)"";
2950 } else
2951 state1 = s_BASE;
2952 } else {
2953 type1 = t_path;
2954 state1 = s_DIR;
2955 }
2956 if (!c2) {
2957 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2958 c2 = (const uchar*)f2->basename;
2959 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2960 type2 = t_ITEM;
2961 state2 = s_TRAILING;
2962 c2 = (uchar*)"";
2963 } else
2964 state2 = s_BASE;
2965 } else {
2966 type2 = t_path;
2967 state2 = s_DIR;
2968 }
2969
2970 if (type1 != type2)
2971 return type1 == t_PATH ? 1 : -1;
2972
2973 do {
2974 if (!*c1) {
2975 switch (state1) {
2976 case s_DIR:
2977 state1 = s_SLASH;
2978 c1 = (uchar*)"/";
2979 break;
2980 case s_SLASH:
2981 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2982 c1 = (const uchar*)f1->basename;
2983 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2984 type1 = t_ITEM;
2985 state1 = s_TRAILING;
2986 c1 = (uchar*)"";
2987 } else
2988 state1 = s_BASE;
2989 break;
2990 case s_BASE:
2991 state1 = s_TRAILING;
2992 if (type1 == t_PATH) {
2993 c1 = (uchar*)"/";
2994 break;
2995 }
2996 /* FALL THROUGH */
2997 case s_TRAILING:
2998 type1 = t_ITEM;
2999 break;
3000 }
3001 if (*c2 && type1 != type2)
3002 return type1 == t_PATH ? 1 : -1;
3003 }
3004 if (!*c2) {
3005 switch (state2) {
3006 case s_DIR:
3007 state2 = s_SLASH;
3008 c2 = (uchar*)"/";
3009 break;
3010 case s_SLASH:
3011 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3012 c2 = (const uchar*)f2->basename;
3013 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3014 type2 = t_ITEM;
3015 state2 = s_TRAILING;
3016 c2 = (uchar*)"";
3017 } else
3018 state2 = s_BASE;
3019 break;
3020 case s_BASE:
3021 state2 = s_TRAILING;
3022 if (type2 == t_PATH) {
3023 c2 = (uchar*)"/";
3024 break;
3025 }
3026 /* FALL THROUGH */
3027 case s_TRAILING:
3028 found_prefix = 1;
3029 if (!*c1)
3030 return 0;
3031 type2 = t_ITEM;
3032 break;
3033 }
3034 if (type1 != type2)
3035 return type1 == t_PATH ? 1 : -1;
3036 }
3037 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
3038
3039 return dif;
3040}
3041
3042/* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
3043 * not match if f2's basename is not an exact match of a path element in f1.
3044 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
3045int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3046{
3047 found_prefix = 0;
3048 f_name_cmp(f1, f2);
3049 return found_prefix;
3050}
3051
3052char *f_name_buf(void)
3053{
3054 static char names[5][MAXPATHLEN];
3055 static unsigned int n;
3056
3057 n = (n + 1) % (sizeof names / sizeof names[0]);
3058
3059 return names[n];
3060}
3061
3062/* Return a copy of the full filename of a flist entry, using the indicated
3063 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
3064 * done because we checked the size when creating the file_struct entry.
3065 */
3066char *f_name(const struct file_struct *f, char *fbuf)
3067{
3068 if (!f || !F_IS_ACTIVE(f))
3069 return NULL;
3070
3071 if (!fbuf)
3072 fbuf = f_name_buf();
3073
3074 if (f->dirname) {
3075 int len = strlen(f->dirname);
3076 memcpy(fbuf, f->dirname, len);
3077 fbuf[len] = '/';
3078 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3079 } else
3080 strlcpy(fbuf, f->basename, MAXPATHLEN);
3081
3082 return fbuf;
3083}
3084
3085/* Do a non-recursive scan of the named directory, possibly ignoring all
3086 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
3087 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3088 * buffer (the functions we call will append names onto the end, but the old
3089 * dir value will be restored on exit). */
3090struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
3091{
3092 struct file_list *dirlist;
3093 char dirbuf[MAXPATHLEN];
3094 int save_recurse = recurse;
3095 int save_xfer_dirs = xfer_dirs;
3096 int save_prune_empty_dirs = prune_empty_dirs;
3097
3098 if (dlen < 0) {
3099 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3100 if (dlen >= MAXPATHLEN)
3101 return NULL;
3102 dirname = dirbuf;
3103 }
3104
3105 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3106
3107 recurse = 0;
3108 xfer_dirs = 1;
3109 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, FLAG_CONTENT_DIR);
3110 xfer_dirs = save_xfer_dirs;
3111 recurse = save_recurse;
3112 if (INFO_GTE(PROGRESS, 1))
3113 flist_count_offset += dirlist->used;
3114
3115 prune_empty_dirs = 0;
3116 dirlist->sorted = dirlist->files;
3117 flist_sort_and_clean(dirlist, 0);
3118 prune_empty_dirs = save_prune_empty_dirs;
3119
3120 if (DEBUG_GTE(FLIST, 3))
3121 output_flist(dirlist);
3122
3123 return dirlist;
3124}