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