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