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