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