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