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