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