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