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