Updated indexing for accurate progress and improved raw ndx values.
[rsync/rsync.git] / flist.c
... / ...
CommitLineData
1/*
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002-2007 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 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, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24#include "ifuncs.h"
25#include "rounding.h"
26#include "io.h"
27
28extern int verbose;
29extern int am_root;
30extern int am_server;
31extern int am_daemon;
32extern int am_sender;
33extern int am_generator;
34extern int inc_recurse;
35extern int do_progress;
36extern int always_checksum;
37extern int module_id;
38extern int ignore_errors;
39extern int numeric_ids;
40extern int recurse;
41extern int use_qsort;
42extern int xfer_dirs;
43extern int filesfrom_fd;
44extern int one_file_system;
45extern int copy_dirlinks;
46extern int keep_dirlinks;
47extern int preserve_acls;
48extern int preserve_xattrs;
49extern int preserve_links;
50extern int preserve_hard_links;
51extern int preserve_devices;
52extern int preserve_specials;
53extern int uid_ndx;
54extern int gid_ndx;
55extern int eol_nulls;
56extern int relative_paths;
57extern int implied_dirs;
58extern int file_extra_cnt;
59extern int ignore_perishable;
60extern int non_perishable_cnt;
61extern int prune_empty_dirs;
62extern int copy_links;
63extern int copy_unsafe_links;
64extern int protocol_version;
65extern int sanitize_paths;
66extern int munge_symlinks;
67extern int need_unsorted_flist;
68extern int unsort_ndx;
69extern struct stats stats;
70extern char *filesfrom_host;
71
72extern char curr_dir[MAXPATHLEN];
73
74extern struct chmod_mode_struct *chmod_modes;
75
76extern struct filter_list_struct filter_list;
77extern struct filter_list_struct server_filter_list;
78
79#ifdef ICONV_OPTION
80extern int filesfrom_convert;
81extern iconv_t ic_send, ic_recv;
82#endif
83
84#define PTR_SIZE (sizeof (struct file_struct *))
85
86int io_error;
87int checksum_len;
88dev_t filesystem_dev; /* used to implement -x */
89
90struct file_list *cur_flist, *first_flist, *dir_flist;
91int send_dir_ndx = -1, send_dir_depth = -1;
92int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
93int file_total = 0; /* total of all active items over all file-lists */
94int flist_eof = 0; /* all the file-lists are now known */
95
96#define NORMAL_NAME 0
97#define SLASH_ENDING_NAME 1
98#define DOT_NAME 2
99
100/* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
101 * internally, even if this platform does not allow files to have 64-bit inums.
102 * The only exception is if we're on a platform with no 64-bit type at all.
103 *
104 * Because we use read_longint() to get these off the wire, if you transfer
105 * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
106 * machine with no 64-bit types then you will get an overflow error.
107 *
108 * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
109 * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
110 * be truncated. But it's a kind of silly thing to do anyhow. */
111
112/* The tmp_* vars are used as a cache area by make_file() to store data
113 * that the sender doesn't need to remember in its file list. The data
114 * will survive just long enough to be used by send_file_entry(). */
115static dev_t tmp_rdev;
116#ifdef SUPPORT_HARD_LINKS
117static int64 tmp_dev, tmp_ino;
118#endif
119static char tmp_sum[MAX_DIGEST_LEN];
120
121static char empty_sum[MAX_DIGEST_LEN];
122static int flist_count_offset; /* for --delete --progress */
123static int dir_count = 0;
124
125static void flist_sort_and_clean(struct file_list *flist, int strip_root);
126static void output_flist(struct file_list *flist);
127
128void init_flist(void)
129{
130 if (verbose > 4) {
131 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
132 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
133 }
134 checksum_len = protocol_version < 21 ? 2
135 : protocol_version < 30 ? MD4_DIGEST_LEN
136 : MD5_DIGEST_LEN;
137}
138
139static int show_filelist_p(void)
140{
141 return verbose && xfer_dirs && !am_server && !inc_recurse;
142}
143
144static void start_filelist_progress(char *kind)
145{
146 rprintf(FCLIENT, "%s ... ", kind);
147 if (verbose > 1 || do_progress)
148 rprintf(FCLIENT, "\n");
149 rflush(FINFO);
150}
151
152static void emit_filelist_progress(int count)
153{
154 rprintf(FCLIENT, " %d files...\r", count);
155}
156
157static void maybe_emit_filelist_progress(int count)
158{
159 if (do_progress && show_filelist_p() && (count % 100) == 0)
160 emit_filelist_progress(count);
161}
162
163static void finish_filelist_progress(const struct file_list *flist)
164{
165 if (do_progress) {
166 /* This overwrites the progress line */
167 rprintf(FINFO, "%d file%sto consider\n",
168 flist->used, flist->used == 1 ? " " : "s ");
169 } else
170 rprintf(FINFO, "done\n");
171}
172
173void show_flist_stats(void)
174{
175 /* Nothing yet */
176}
177
178/* Stat either a symlink or its referent, depending on the settings of
179 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
180 *
181 * If path is the name of a symlink, then the linkbuf buffer (which must hold
182 * MAXPATHLEN chars) will be set to the symlink's target string.
183 *
184 * The stat structure pointed to by stp will contain information about the
185 * link or the referent as appropriate, if they exist. */
186static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
187{
188#ifdef SUPPORT_LINKS
189 if (link_stat(path, stp, copy_dirlinks) < 0)
190 return -1;
191 if (S_ISLNK(stp->st_mode)) {
192 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
193 if (llen < 0)
194 return -1;
195 linkbuf[llen] = '\0';
196 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
197 if (verbose > 1) {
198 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
199 path, linkbuf);
200 }
201 return x_stat(path, stp, NULL);
202 }
203 if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
204 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
205 memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
206 llen - SYMLINK_PREFIX_LEN + 1);
207 }
208 }
209 return 0;
210#else
211 return x_stat(path, stp, NULL);
212#endif
213}
214
215int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
216{
217#ifdef SUPPORT_LINKS
218 if (copy_links)
219 return x_stat(path, stp, NULL);
220 if (x_lstat(path, stp, NULL) < 0)
221 return -1;
222 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
223 STRUCT_STAT st;
224 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
225 *stp = st;
226 }
227 return 0;
228#else
229 return x_stat(path, stp, NULL);
230#endif
231}
232
233/* This function is used to check if a file should be included/excluded
234 * from the list of files based on its name and type etc. The value of
235 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
236static int is_excluded(char *fname, int is_dir, int filter_level)
237{
238#if 0 /* This currently never happens, so avoid a useless compare. */
239 if (filter_level == NO_FILTERS)
240 return 0;
241#endif
242 if (fname) {
243 /* never exclude '.', even if somebody does --exclude '*' */
244 if (fname[0] == '.' && !fname[1])
245 return 0;
246 /* Handle the -R version of the '.' dir. */
247 if (fname[0] == '/') {
248 int len = strlen(fname);
249 if (fname[len-1] == '.' && fname[len-2] == '/')
250 return 0;
251 }
252 }
253 if (server_filter_list.head
254 && check_filter(&server_filter_list, fname, is_dir) < 0)
255 return 1;
256 if (filter_level != ALL_FILTERS)
257 return 0;
258 if (filter_list.head
259 && check_filter(&filter_list, fname, is_dir) < 0)
260 return 1;
261 return 0;
262}
263
264static void send_directory(int f, struct file_list *flist,
265 char *fbuf, int len, int flags);
266
267static const char *pathname, *orig_dir;
268static int pathname_len;
269
270
271/* Make sure flist can hold at least flist->used + extra entries. */
272static void flist_expand(struct file_list *flist, int extra)
273{
274 struct file_struct **new_ptr;
275
276 if (flist->used + extra <= flist->malloced)
277 return;
278
279 if (flist->malloced < FLIST_START)
280 flist->malloced = FLIST_START;
281 else if (flist->malloced >= FLIST_LINEAR)
282 flist->malloced += FLIST_LINEAR;
283 else
284 flist->malloced *= 2;
285
286 /* In case count jumped or we are starting the list
287 * with a known size just set it. */
288 if (flist->malloced < flist->used + extra)
289 flist->malloced = flist->used + extra;
290
291 new_ptr = realloc_array(flist->files, struct file_struct *,
292 flist->malloced);
293
294 if (verbose >= 2 && flist->malloced != FLIST_START) {
295 rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
296 who_am_i(),
297 (double)sizeof flist->files[0] * flist->malloced,
298 (new_ptr == flist->files) ? " not" : "");
299 }
300
301 flist->files = new_ptr;
302
303 if (!flist->files)
304 out_of_memory("flist_expand");
305}
306
307static void flist_done_allocating(struct file_list *flist)
308{
309 void *ptr = pool_boundary(flist->file_pool, 8*1024);
310 if (flist->pool_boundary == ptr)
311 flist->pool_boundary = NULL; /* list didn't use any pool memory */
312 else
313 flist->pool_boundary = ptr;
314}
315
316int push_pathname(const char *dir, int len)
317{
318 if (dir == pathname)
319 return 1;
320
321 if (!orig_dir)
322 orig_dir = strdup(curr_dir);
323
324 if (pathname && !pop_dir(orig_dir)) {
325 rsyserr(FERROR, errno, "pop_dir %s failed",
326 full_fname(orig_dir));
327 exit_cleanup(RERR_FILESELECT);
328 }
329
330 if (dir && !push_dir(dir, 0)) {
331 io_error |= IOERR_GENERAL;
332 rsyserr(FERROR, errno, "push_dir %s failed in %s",
333 full_fname(dir), curr_dir);
334 return 0;
335 }
336
337 pathname = dir;
338 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
339
340 return 1;
341}
342
343static void send_file_entry(int f, struct file_struct *file, int ndx, int first_ndx)
344{
345 static time_t modtime;
346 static mode_t mode;
347#ifdef SUPPORT_HARD_LINKS
348 static int64 dev;
349#endif
350 static dev_t rdev;
351 static uint32 rdev_major;
352 static uid_t uid;
353 static gid_t gid;
354 static char *user_name, *group_name;
355 static char lastname[MAXPATHLEN];
356 char fname[MAXPATHLEN];
357 int first_hlink_ndx = -1;
358 int l1, l2;
359 int xflags;
360
361#ifdef ICONV_OPTION
362 if (ic_send != (iconv_t)-1) {
363 xbuf outbuf, inbuf;
364
365 INIT_CONST_XBUF(outbuf, fname);
366
367 if (file->dirname) {
368 INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
369 outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
370 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
371 goto convert_error;
372 outbuf.size += 2;
373 outbuf.buf[outbuf.len++] = '/';
374 }
375
376 INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
377 if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
378 convert_error:
379 io_error |= IOERR_GENERAL;
380 rprintf(FINFO,
381 "[%s] cannot convert filename: %s (%s)\n",
382 who_am_i(), f_name(file, fname), strerror(errno));
383 return;
384 }
385 outbuf.buf[outbuf.len] = '\0';
386 } else
387#endif
388 f_name(file, fname);
389
390 /* Initialize starting value of xflags. */
391 if (protocol_version >= 30 && S_ISDIR(file->mode)) {
392 if (file->flags & FLAG_CONTENT_DIR)
393 xflags = file->flags & FLAG_TOP_DIR;
394 else if (file->flags & FLAG_IMPLIED_DIR)
395 xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
396 else
397 xflags = XMIT_NO_CONTENT_DIR;
398 } else
399 xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
400
401 if (file->mode == mode)
402 xflags |= XMIT_SAME_MODE;
403 else
404 mode = file->mode;
405
406 if ((preserve_devices && IS_DEVICE(mode))
407 || (preserve_specials && IS_SPECIAL(mode))) {
408 if (protocol_version < 28) {
409 if (tmp_rdev == rdev)
410 xflags |= XMIT_SAME_RDEV_pre28;
411 else
412 rdev = tmp_rdev;
413 } else {
414 rdev = tmp_rdev;
415 if ((uint32)major(rdev) == rdev_major)
416 xflags |= XMIT_SAME_RDEV_MAJOR;
417 else
418 rdev_major = major(rdev);
419 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
420 xflags |= XMIT_RDEV_MINOR_8_pre30;
421 }
422 } else if (protocol_version < 28)
423 rdev = MAKEDEV(0, 0);
424 if (uid_ndx) {
425 if ((uid_t)F_OWNER(file) == uid && *lastname)
426 xflags |= XMIT_SAME_UID;
427 else {
428 uid = F_OWNER(file);
429 if (uid_ndx && !numeric_ids) {
430 user_name = add_uid(uid);
431 if (inc_recurse && user_name)
432 xflags |= XMIT_USER_NAME_FOLLOWS;
433 }
434 }
435 }
436 if (gid_ndx) {
437 if ((gid_t)F_GROUP(file) == gid && *lastname)
438 xflags |= XMIT_SAME_GID;
439 else {
440 gid = F_GROUP(file);
441 if (gid_ndx && !numeric_ids) {
442 group_name = add_gid(gid);
443 if (inc_recurse && group_name)
444 xflags |= XMIT_GROUP_NAME_FOLLOWS;
445 }
446 }
447 }
448 if (file->modtime == modtime)
449 xflags |= XMIT_SAME_TIME;
450 else
451 modtime = file->modtime;
452
453#ifdef SUPPORT_HARD_LINKS
454 if (tmp_dev != 0) {
455 if (protocol_version >= 30) {
456 struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
457 first_hlink_ndx = (int32)(long)np->data - 1;
458 if (first_hlink_ndx < 0) {
459 np->data = (void*)(long)(first_ndx + ndx + 1);
460 xflags |= XMIT_HLINK_FIRST;
461 }
462 xflags |= XMIT_HLINKED;
463 } else {
464 if (tmp_dev == dev) {
465 if (protocol_version >= 28)
466 xflags |= XMIT_SAME_DEV_pre30;
467 } else
468 dev = tmp_dev;
469 xflags |= XMIT_HLINKED;
470 }
471 }
472#endif
473
474 for (l1 = 0;
475 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
476 l1++) {}
477 l2 = strlen(fname+l1);
478
479 if (l1 > 0)
480 xflags |= XMIT_SAME_NAME;
481 if (l2 > 255)
482 xflags |= XMIT_LONG_NAME;
483
484 /* We must make sure we don't send a zero flag byte or the
485 * other end will terminate the flist transfer. Note that
486 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
487 * it's harmless way to add a bit to the first flag byte. */
488 if (protocol_version >= 28) {
489 if (!xflags && !S_ISDIR(mode))
490 xflags |= XMIT_TOP_DIR;
491 if ((xflags & 0xFF00) || !xflags) {
492 xflags |= XMIT_EXTENDED_FLAGS;
493 write_shortint(f, xflags);
494 } else
495 write_byte(f, xflags);
496 } else {
497 if (!(xflags & 0xFF))
498 xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
499 write_byte(f, xflags);
500 }
501 if (xflags & XMIT_SAME_NAME)
502 write_byte(f, l1);
503 if (xflags & XMIT_LONG_NAME)
504 write_varint30(f, l2);
505 else
506 write_byte(f, l2);
507 write_buf(f, fname + l1, l2);
508
509 if (first_hlink_ndx >= 0) {
510 write_varint(f, first_hlink_ndx);
511 if (first_hlink_ndx >= first_ndx)
512 goto the_end;
513 }
514
515 write_varlong30(f, F_LENGTH(file), 3);
516 if (!(xflags & XMIT_SAME_TIME)) {
517 if (protocol_version >= 30)
518 write_varlong(f, modtime, 4);
519 else
520 write_int(f, modtime);
521 }
522 if (!(xflags & XMIT_SAME_MODE))
523 write_int(f, to_wire_mode(mode));
524 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
525 if (protocol_version < 30)
526 write_int(f, uid);
527 else {
528 write_varint(f, uid);
529 if (xflags & XMIT_USER_NAME_FOLLOWS) {
530 int len = strlen(user_name);
531 write_byte(f, len);
532 write_buf(f, user_name, len);
533 }
534 }
535 }
536 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
537 if (protocol_version < 30)
538 write_int(f, gid);
539 else {
540 write_varint(f, gid);
541 if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
542 int len = strlen(group_name);
543 write_byte(f, len);
544 write_buf(f, group_name, len);
545 }
546 }
547 }
548 if ((preserve_devices && IS_DEVICE(mode))
549 || (preserve_specials && IS_SPECIAL(mode))) {
550 if (protocol_version < 28) {
551 if (!(xflags & XMIT_SAME_RDEV_pre28))
552 write_int(f, (int)rdev);
553 } else {
554 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
555 write_varint30(f, major(rdev));
556 if (protocol_version >= 30)
557 write_varint(f, minor(rdev));
558 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
559 write_byte(f, minor(rdev));
560 else
561 write_int(f, minor(rdev));
562 }
563 }
564
565#ifdef SUPPORT_LINKS
566 if (preserve_links && S_ISLNK(mode)) {
567 const char *sl = F_SYMLINK(file);
568 int len = strlen(sl);
569 write_varint30(f, len);
570 write_buf(f, sl, len);
571 }
572#endif
573
574#ifdef SUPPORT_HARD_LINKS
575 if (tmp_dev != 0 && protocol_version < 30) {
576 if (protocol_version < 26) {
577 /* 32-bit dev_t and ino_t */
578 write_int(f, (int32)dev);
579 write_int(f, (int32)tmp_ino);
580 } else {
581 /* 64-bit dev_t and ino_t */
582 if (!(xflags & XMIT_SAME_DEV_pre30))
583 write_longint(f, dev);
584 write_longint(f, tmp_ino);
585 }
586 }
587#endif
588
589 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
590 const char *sum;
591 if (S_ISREG(mode))
592 sum = tmp_sum;
593 else {
594 /* Prior to 28, we sent a useless set of nulls. */
595 sum = empty_sum;
596 }
597 write_buf(f, sum, checksum_len);
598 }
599
600 the_end:
601 strlcpy(lastname, fname, MAXPATHLEN);
602
603 if (S_ISREG(mode) || S_ISLNK(mode))
604 stats.total_size += F_LENGTH(file);
605}
606
607static struct file_struct *recv_file_entry(struct file_list *flist,
608 int xflags, int f)
609{
610 static int64 modtime;
611 static mode_t mode;
612#ifdef SUPPORT_HARD_LINKS
613 static int64 dev;
614#endif
615 static dev_t rdev;
616 static uint32 rdev_major;
617 static uid_t uid;
618 static gid_t gid;
619 static uint16 gid_flags;
620 static char lastname[MAXPATHLEN], *lastdir;
621 static int lastdir_depth, lastdir_len = -1;
622 static unsigned int del_hier_name_len = 0;
623 static int in_del_hier = 0;
624 char thisname[MAXPATHLEN];
625 unsigned int l1 = 0, l2 = 0;
626 int alloc_len, basename_len, linkname_len;
627 int extra_len = file_extra_cnt * EXTRA_LEN;
628 int first_hlink_ndx = -1;
629 OFF_T file_length;
630 const char *basename;
631 struct file_struct *file;
632 alloc_pool_t *pool;
633 char *bp;
634
635 if (xflags & XMIT_SAME_NAME)
636 l1 = read_byte(f);
637
638 if (xflags & XMIT_LONG_NAME)
639 l2 = read_varint30(f);
640 else
641 l2 = read_byte(f);
642
643 if (l2 >= MAXPATHLEN - l1) {
644 rprintf(FERROR,
645 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
646 xflags, l1, l2, lastname, who_am_i());
647 overflow_exit("recv_file_entry");
648 }
649
650 strlcpy(thisname, lastname, l1 + 1);
651 read_sbuf(f, &thisname[l1], l2);
652 thisname[l1 + l2] = 0;
653
654 /* Abuse basename_len for a moment... */
655 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
656
657#ifdef ICONV_OPTION
658 if (ic_recv != (iconv_t)-1) {
659 xbuf outbuf, inbuf;
660
661 INIT_CONST_XBUF(outbuf, thisname);
662 INIT_XBUF(inbuf, lastname, basename_len, -1);
663
664 if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
665 io_error |= IOERR_GENERAL;
666 rprintf(FINFO,
667 "[%s] cannot convert filename: %s (%s)\n",
668 who_am_i(), lastname, strerror(errno));
669 outbuf.len = 0;
670 }
671 outbuf.buf[outbuf.len] = '\0';
672 }
673#endif
674
675 clean_fname(thisname, 0);
676
677 if (sanitize_paths)
678 sanitize_path(thisname, thisname, "", 0);
679
680 if ((basename = strrchr(thisname, '/')) != NULL) {
681 int len = basename++ - thisname;
682 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
683 lastdir = new_array(char, len + 1);
684 memcpy(lastdir, thisname, len);
685 lastdir[len] = '\0';
686 lastdir_len = len;
687 lastdir_depth = count_dir_elements(lastdir);
688 }
689 } else
690 basename = thisname;
691 basename_len = strlen(basename) + 1; /* count the '\0' */
692
693#ifdef SUPPORT_HARD_LINKS
694 if (protocol_version >= 30
695 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
696 first_hlink_ndx = read_varint(f);
697 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
698 rprintf(FERROR,
699 "hard-link reference out of range: %d (%d)\n",
700 first_hlink_ndx, flist->ndx_start + flist->used);
701 exit_cleanup(RERR_PROTOCOL);
702 }
703 if (first_hlink_ndx >= flist->ndx_start) {
704 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
705 file_length = F_LENGTH(first);
706 modtime = first->modtime;
707 mode = first->mode;
708 if (uid_ndx)
709 uid = F_OWNER(first);
710 if (gid_ndx)
711 gid = F_GROUP(first);
712 if ((preserve_devices && IS_DEVICE(mode))
713 || (preserve_specials && IS_SPECIAL(mode))) {
714 uint32 *devp = F_RDEV_P(first);
715 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
716 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
717 }
718 if (preserve_links && S_ISLNK(mode))
719 linkname_len = strlen(F_SYMLINK(first)) + 1;
720 else
721 linkname_len = 0;
722 goto create_object;
723 }
724 }
725#endif
726
727 file_length = read_varlong30(f, 3);
728 if (!(xflags & XMIT_SAME_TIME)) {
729 if (protocol_version >= 30) {
730 modtime = read_varlong(f, 4);
731#if SIZEOF_TIME_T < SIZEOF_INT64
732 if (!am_generator && (int64)(time_t)modtime != modtime) {
733 rprintf(FERROR_XFER,
734 "Time value of %s truncated on receiver.\n",
735 lastname);
736 }
737#endif
738 } else
739 modtime = read_int(f);
740 }
741 if (!(xflags & XMIT_SAME_MODE))
742 mode = from_wire_mode(read_int(f));
743
744 if (chmod_modes && !S_ISLNK(mode))
745 mode = tweak_mode(mode, chmod_modes);
746
747 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
748 if (protocol_version < 30)
749 uid = (uid_t)read_int(f);
750 else {
751 uid = (uid_t)read_varint(f);
752 if (xflags & XMIT_USER_NAME_FOLLOWS)
753 uid = recv_user_name(f, uid);
754 else if (inc_recurse && am_root && !numeric_ids)
755 uid = match_uid(uid);
756 }
757 }
758 if (gid_ndx && !(xflags & XMIT_SAME_GID)) {
759 if (protocol_version < 30)
760 gid = (gid_t)read_int(f);
761 else {
762 gid = (gid_t)read_varint(f);
763 gid_flags = 0;
764 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
765 gid = recv_group_name(f, gid, &gid_flags);
766 else if (inc_recurse && (!am_root || !numeric_ids))
767 gid = match_gid(gid, &gid_flags);
768 }
769 }
770
771 if ((preserve_devices && IS_DEVICE(mode))
772 || (preserve_specials && IS_SPECIAL(mode))) {
773 if (protocol_version < 28) {
774 if (!(xflags & XMIT_SAME_RDEV_pre28))
775 rdev = (dev_t)read_int(f);
776 } else {
777 uint32 rdev_minor;
778 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
779 rdev_major = read_varint30(f);
780 if (protocol_version >= 30)
781 rdev_minor = read_varint(f);
782 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
783 rdev_minor = read_byte(f);
784 else
785 rdev_minor = read_int(f);
786 rdev = MAKEDEV(rdev_major, rdev_minor);
787 }
788 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
789 file_length = 0;
790 } else if (protocol_version < 28)
791 rdev = MAKEDEV(0, 0);
792
793#ifdef SUPPORT_LINKS
794 if (preserve_links && S_ISLNK(mode)) {
795 linkname_len = read_varint30(f) + 1; /* count the '\0' */
796 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
797 rprintf(FERROR, "overflow: linkname_len=%d\n",
798 linkname_len - 1);
799 overflow_exit("recv_file_entry");
800 }
801 if (munge_symlinks)
802 linkname_len += SYMLINK_PREFIX_LEN;
803 }
804 else
805#endif
806 linkname_len = 0;
807
808#ifdef SUPPORT_HARD_LINKS
809 create_object:
810 if (preserve_hard_links) {
811 if (protocol_version < 28 && S_ISREG(mode))
812 xflags |= XMIT_HLINKED;
813 if (xflags & XMIT_HLINKED)
814 extra_len += (inc_recurse+1) * EXTRA_LEN;
815 }
816#endif
817
818#ifdef SUPPORT_ACLS
819 /* We need one or two index int32s when we're preserving ACLs. */
820 if (preserve_acls)
821 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
822#endif
823
824 if (always_checksum && S_ISREG(mode))
825 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
826
827 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
828 extra_len += EXTRA_LEN;
829
830 if (inc_recurse && S_ISDIR(mode)) {
831 if (one_file_system) {
832 /* Room to save the dir's device for -x */
833 extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
834 }
835 pool = dir_flist->file_pool;
836 } else
837 pool = flist->file_pool;
838
839#if EXTRA_ROUNDING > 0
840 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
841 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
842#endif
843
844 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
845 + linkname_len;
846 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
847
848 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
849 bp += extra_len;
850 file = (struct file_struct *)bp;
851 bp += FILE_STRUCT_LEN;
852
853 memcpy(bp, basename, basename_len);
854 bp += basename_len + linkname_len; /* skip space for symlink too */
855
856#ifdef SUPPORT_HARD_LINKS
857 if (xflags & XMIT_HLINKED)
858 file->flags |= FLAG_HLINKED;
859#endif
860 file->modtime = (time_t)modtime;
861 file->len32 = (uint32)file_length;
862 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
863 file->flags |= FLAG_LENGTH64;
864 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
865 }
866 file->mode = mode;
867 if (uid_ndx)
868 F_OWNER(file) = uid;
869 if (gid_ndx) {
870 F_GROUP(file) = gid;
871 file->flags |= gid_flags;
872 }
873 if (unsort_ndx)
874 F_NDX(file) = flist->used + flist->ndx_start;
875
876 if (basename != thisname) {
877 file->dirname = lastdir;
878 F_DEPTH(file) = lastdir_depth + 1;
879 } else
880 F_DEPTH(file) = 1;
881
882 if (S_ISDIR(mode)) {
883 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
884 F_DEPTH(file)--;
885 if (protocol_version >= 30) {
886 if (!(xflags & XMIT_NO_CONTENT_DIR)) {
887 if (xflags & XMIT_TOP_DIR)
888 file->flags |= FLAG_TOP_DIR;
889 file->flags |= FLAG_CONTENT_DIR;
890 } else if (xflags & XMIT_TOP_DIR)
891 file->flags |= FLAG_IMPLIED_DIR;
892 } else if (xflags & XMIT_TOP_DIR) {
893 in_del_hier = recurse;
894 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
895 if (relative_paths && del_hier_name_len > 2
896 && lastname[del_hier_name_len-1] == '.'
897 && lastname[del_hier_name_len-2] == '/')
898 del_hier_name_len -= 2;
899 file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
900 } else if (in_del_hier) {
901 if (!relative_paths || !del_hier_name_len
902 || (l1 >= del_hier_name_len
903 && lastname[del_hier_name_len] == '/'))
904 file->flags |= FLAG_CONTENT_DIR;
905 else
906 in_del_hier = 0;
907 }
908 }
909
910 if ((preserve_devices && IS_DEVICE(mode))
911 || (preserve_specials && IS_SPECIAL(mode))) {
912 uint32 *devp = F_RDEV_P(file);
913 DEV_MAJOR(devp) = major(rdev);
914 DEV_MINOR(devp) = minor(rdev);
915 }
916
917#ifdef SUPPORT_LINKS
918 if (linkname_len) {
919 bp = (char*)file->basename + basename_len;
920 if (first_hlink_ndx >= flist->ndx_start) {
921 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
922 memcpy(bp, F_SYMLINK(first), linkname_len);
923 } else if (munge_symlinks) {
924 strlcpy(bp, SYMLINK_PREFIX, linkname_len);
925 bp += SYMLINK_PREFIX_LEN;
926 linkname_len -= SYMLINK_PREFIX_LEN;
927 read_sbuf(f, bp, linkname_len - 1);
928 } else {
929 read_sbuf(f, bp, linkname_len - 1);
930 if (sanitize_paths)
931 sanitize_path(bp, bp, "", lastdir_depth);
932 }
933 }
934#endif
935
936#ifdef SUPPORT_HARD_LINKS
937 if (preserve_hard_links && xflags & XMIT_HLINKED) {
938 if (protocol_version >= 30) {
939 if (xflags & XMIT_HLINK_FIRST) {
940 F_HL_GNUM(file) = flist->ndx_start + flist->used;
941 } else
942 F_HL_GNUM(file) = first_hlink_ndx;
943 } else {
944 static int32 cnt = 0;
945 struct ht_int64_node *np;
946 int64 ino;
947 int32 ndx;
948 if (protocol_version < 26) {
949 dev = read_int(f);
950 ino = read_int(f);
951 } else {
952 if (!(xflags & XMIT_SAME_DEV_pre30))
953 dev = read_longint(f);
954 ino = read_longint(f);
955 }
956 np = idev_find(dev, ino);
957 ndx = (int32)(long)np->data - 1;
958 if (ndx < 0) {
959 ndx = cnt++;
960 np->data = (void*)(long)cnt;
961 }
962 F_HL_GNUM(file) = ndx;
963 }
964 }
965#endif
966
967 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
968 if (S_ISREG(mode))
969 bp = F_SUM(file);
970 else {
971 /* Prior to 28, we get a useless set of nulls. */
972 bp = tmp_sum;
973 }
974 if (first_hlink_ndx >= flist->ndx_start) {
975 struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
976 memcpy(bp, F_SUM(first), checksum_len);
977 } else
978 read_buf(f, bp, checksum_len);
979 }
980
981#ifdef SUPPORT_ACLS
982 if (preserve_acls && !S_ISLNK(mode))
983 receive_acl(file, f);
984#endif
985#ifdef SUPPORT_XATTRS
986 if (preserve_xattrs)
987 receive_xattr(file, f );
988#endif
989
990 if (S_ISREG(mode) || S_ISLNK(mode))
991 stats.total_size += file_length;
992
993 return file;
994}
995
996/* Create a file_struct for a named file by reading its stat() information
997 * and performing extensive checks against global options.
998 *
999 * Returns a pointer to the new file struct, or NULL if there was an error
1000 * or this file should be excluded. */
1001struct file_struct *make_file(const char *fname, struct file_list *flist,
1002 STRUCT_STAT *stp, int flags, int filter_level)
1003{
1004 static char *lastdir;
1005 static int lastdir_len = -1;
1006 struct file_struct *file;
1007 char thisname[MAXPATHLEN];
1008 char linkname[MAXPATHLEN];
1009 int alloc_len, basename_len, linkname_len;
1010 int extra_len = file_extra_cnt * EXTRA_LEN;
1011 const char *basename;
1012 alloc_pool_t *pool;
1013 STRUCT_STAT st;
1014 char *bp;
1015
1016 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1017 rprintf(FINFO, "skipping overly long name: %s\n", fname);
1018 return NULL;
1019 }
1020 clean_fname(thisname, 0);
1021 if (sanitize_paths)
1022 sanitize_path(thisname, thisname, "", 0);
1023
1024 if (stp && S_ISDIR(stp->st_mode)) {
1025 st = *stp; /* Needed for "symlink/." with --relative. */
1026 *linkname = '\0'; /* make IBM code checker happy */
1027 } else if (readlink_stat(thisname, &st, linkname) != 0) {
1028 int save_errno = errno;
1029 /* See if file is excluded before reporting an error. */
1030 if (filter_level != NO_FILTERS
1031 && (is_excluded(thisname, 0, filter_level)
1032 || is_excluded(thisname, 1, filter_level))) {
1033 if (ignore_perishable && save_errno != ENOENT)
1034 non_perishable_cnt++;
1035 return NULL;
1036 }
1037 if (save_errno == ENOENT) {
1038#ifdef SUPPORT_LINKS
1039 /* Avoid "vanished" error if symlink points nowhere. */
1040 if (copy_links && x_lstat(thisname, &st, NULL) == 0
1041 && S_ISLNK(st.st_mode)) {
1042 io_error |= IOERR_GENERAL;
1043 rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1044 full_fname(thisname));
1045 } else
1046#endif
1047 {
1048 enum logcode c = am_daemon && protocol_version < 28
1049 ? FERROR : FWARNING;
1050 io_error |= IOERR_VANISHED;
1051 rprintf(c, "file has vanished: %s\n",
1052 full_fname(thisname));
1053 }
1054 } else {
1055 io_error |= IOERR_GENERAL;
1056 rsyserr(FERROR_XFER, save_errno, "readlink %s failed",
1057 full_fname(thisname));
1058 }
1059 return NULL;
1060 }
1061
1062 /* backup.c calls us with filter_level set to NO_FILTERS. */
1063 if (filter_level == NO_FILTERS)
1064 goto skip_filters;
1065
1066 if (S_ISDIR(st.st_mode)) {
1067 if (!xfer_dirs) {
1068 rprintf(FINFO, "skipping directory %s\n", thisname);
1069 return NULL;
1070 }
1071 /* -x only affects dirs because we need to avoid recursing
1072 * into a mount-point directory, not to avoid copying a
1073 * symlinked file if -L (or similar) was specified. */
1074 if (one_file_system && st.st_dev != filesystem_dev
1075 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1076 if (one_file_system > 1) {
1077 if (verbose > 1) {
1078 rprintf(FINFO,
1079 "[%s] skipping mount-point dir %s\n",
1080 who_am_i(), thisname);
1081 }
1082 return NULL;
1083 }
1084 flags |= FLAG_MOUNT_DIR;
1085 flags &= ~FLAG_CONTENT_DIR;
1086 }
1087 } else
1088 flags &= ~FLAG_CONTENT_DIR;
1089
1090 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1091 if (ignore_perishable)
1092 non_perishable_cnt++;
1093 return NULL;
1094 }
1095
1096 if (lp_ignore_nonreadable(module_id)) {
1097#ifdef SUPPORT_LINKS
1098 if (!S_ISLNK(st.st_mode))
1099#endif
1100 if (access(thisname, R_OK) != 0)
1101 return NULL;
1102 }
1103
1104 skip_filters:
1105
1106 /* Only divert a directory in the main transfer. */
1107 if (flist) {
1108 if (flist->prev && S_ISDIR(st.st_mode)
1109 && flags & FLAG_DIVERT_DIRS) {
1110 /* Room for parent/sibling/next-child info. */
1111 extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1112 if (relative_paths)
1113 extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1114 dir_count++;
1115 pool = dir_flist->file_pool;
1116 } else
1117 pool = flist->file_pool;
1118 } else
1119 pool = NULL;
1120
1121 if (verbose > 2) {
1122 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1123 who_am_i(), thisname, filter_level);
1124 }
1125
1126 if ((basename = strrchr(thisname, '/')) != NULL) {
1127 int len = basename++ - thisname;
1128 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1129 lastdir = new_array(char, len + 1);
1130 memcpy(lastdir, thisname, len);
1131 lastdir[len] = '\0';
1132 lastdir_len = len;
1133 }
1134 } else
1135 basename = thisname;
1136 basename_len = strlen(basename) + 1; /* count the '\0' */
1137
1138#ifdef SUPPORT_LINKS
1139 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1140#else
1141 linkname_len = 0;
1142#endif
1143
1144 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1145 extra_len += EXTRA_LEN;
1146
1147#if EXTRA_ROUNDING > 0
1148 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1149 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1150#endif
1151
1152 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1153 + linkname_len;
1154 if (pool)
1155 bp = pool_alloc(pool, alloc_len, "make_file");
1156 else {
1157 if (!(bp = new_array(char, alloc_len)))
1158 out_of_memory("make_file");
1159 }
1160
1161 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1162 bp += extra_len;
1163 file = (struct file_struct *)bp;
1164 bp += FILE_STRUCT_LEN;
1165
1166 memcpy(bp, basename, basename_len);
1167 bp += basename_len + linkname_len; /* skip space for symlink too */
1168
1169#ifdef SUPPORT_HARD_LINKS
1170 if (preserve_hard_links && flist && flist->prev) {
1171 if (protocol_version >= 28
1172 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1173 : S_ISREG(st.st_mode)) {
1174 tmp_dev = st.st_dev;
1175 tmp_ino = st.st_ino;
1176 } else
1177 tmp_dev = 0;
1178 }
1179#endif
1180
1181#ifdef HAVE_STRUCT_STAT_ST_RDEV
1182 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1183 tmp_rdev = st.st_rdev;
1184 st.st_size = 0;
1185 }
1186#endif
1187
1188 file->flags = flags;
1189 file->modtime = st.st_mtime;
1190 file->len32 = (uint32)st.st_size;
1191 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1192 file->flags |= FLAG_LENGTH64;
1193 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1194 }
1195 file->mode = st.st_mode;
1196 if (uid_ndx)
1197 F_OWNER(file) = st.st_uid;
1198 if (gid_ndx)
1199 F_GROUP(file) = st.st_gid;
1200
1201 if (basename != thisname)
1202 file->dirname = lastdir;
1203
1204#ifdef SUPPORT_LINKS
1205 if (linkname_len) {
1206 bp = (char*)file->basename + basename_len;
1207 memcpy(bp, linkname, linkname_len);
1208 }
1209#endif
1210
1211 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1212 file_checksum(thisname, tmp_sum, st.st_size);
1213
1214 F_PATHNAME(file) = pathname;
1215
1216 /* This code is only used by the receiver when it is building
1217 * a list of files for a delete pass. */
1218 if (keep_dirlinks && linkname_len && flist) {
1219 STRUCT_STAT st2;
1220 int save_mode = file->mode;
1221 file->mode = S_IFDIR; /* Find a directory with our name. */
1222 if (flist_find(dir_flist, file) >= 0
1223 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
1224 file->modtime = st2.st_mtime;
1225 file->len32 = 0;
1226 file->mode = st2.st_mode;
1227 if (uid_ndx)
1228 F_OWNER(file) = st2.st_uid;
1229 if (gid_ndx)
1230 F_GROUP(file) = st2.st_gid;
1231 } else
1232 file->mode = save_mode;
1233 }
1234
1235 if (basename_len == 0+1)
1236 return NULL;
1237
1238 if (unsort_ndx)
1239 F_NDX(file) = dir_count - 1;
1240
1241 return file;
1242}
1243
1244/* Only called for temporary file_struct entries created by make_file(). */
1245void unmake_file(struct file_struct *file)
1246{
1247 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
1248#if EXTRA_ROUNDING > 0
1249 if (extra_cnt & EXTRA_ROUNDING)
1250 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1251#endif
1252 free(REQ_EXTRA(file, extra_cnt));
1253}
1254
1255static struct file_struct *send_file_name(int f, struct file_list *flist,
1256 char *fname, STRUCT_STAT *stp,
1257 int flags, int filter_level)
1258{
1259 struct file_struct *file;
1260#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1261 stat_x sx;
1262#endif
1263
1264 file = make_file(fname, flist, stp, flags, filter_level);
1265 if (!file)
1266 return NULL;
1267
1268 if (chmod_modes && !S_ISLNK(file->mode))
1269 file->mode = tweak_mode(file->mode, chmod_modes);
1270
1271#ifdef SUPPORT_ACLS
1272 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1273 sx.st.st_mode = file->mode;
1274 sx.acc_acl = sx.def_acl = NULL;
1275 if (get_acl(fname, &sx) < 0)
1276 return NULL;
1277 }
1278#endif
1279#ifdef SUPPORT_XATTRS
1280 if (preserve_xattrs && f >= 0) {
1281 sx.xattr = NULL;
1282 if (get_xattr(fname, &sx) < 0)
1283 return NULL;
1284 }
1285#endif
1286
1287 maybe_emit_filelist_progress(flist->used + flist_count_offset);
1288
1289 flist_expand(flist, 1);
1290 flist->files[flist->used++] = file;
1291 if (f >= 0) {
1292 send_file_entry(f, file, flist->used - 1, flist->ndx_start);
1293#ifdef SUPPORT_ACLS
1294 if (preserve_acls && !S_ISLNK(file->mode)) {
1295 send_acl(&sx, f);
1296 free_acl(&sx);
1297 }
1298#endif
1299#ifdef SUPPORT_XATTRS
1300 if (preserve_xattrs) {
1301 F_XATTR(file) = send_xattr(&sx, f);
1302 free_xattr(&sx);
1303 }
1304#endif
1305 }
1306 return file;
1307}
1308
1309static void send_if_directory(int f, struct file_list *flist,
1310 struct file_struct *file,
1311 char *fbuf, unsigned int ol,
1312 int flags)
1313{
1314 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1315
1316 if (S_ISDIR(file->mode)
1317 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1318 void *save_filters;
1319 unsigned int len = strlen(fbuf);
1320 if (len > 1 && fbuf[len-1] == '/')
1321 fbuf[--len] = '\0';
1322 if (len >= MAXPATHLEN - 1) {
1323 io_error |= IOERR_GENERAL;
1324 rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
1325 full_fname(fbuf));
1326 return;
1327 }
1328 save_filters = push_local_filters(fbuf, len);
1329 send_directory(f, flist, fbuf, len, flags);
1330 pop_local_filters(save_filters);
1331 fbuf[ol] = '\0';
1332 if (is_dot_dir)
1333 fbuf[ol-1] = '.';
1334 }
1335}
1336
1337static int file_compare(const void *file1, const void *file2)
1338{
1339 return f_name_cmp(*(struct file_struct **)file1,
1340 *(struct file_struct **)file2);
1341}
1342
1343/* The guts of a merge-sort algorithm. This was derived from the glibc
1344 * version, but I (Wayne) changed the merge code to do less copying and
1345 * to require only half the amount of temporary memory. */
1346static void fsort_tmp(struct file_struct **fp, size_t num,
1347 struct file_struct **tmp)
1348{
1349 struct file_struct **f1, **f2, **t;
1350 size_t n1, n2;
1351
1352 n1 = num / 2;
1353 n2 = num - n1;
1354 f1 = fp;
1355 f2 = fp + n1;
1356
1357 if (n1 > 1)
1358 fsort_tmp(f1, n1, tmp);
1359 if (n2 > 1)
1360 fsort_tmp(f2, n2, tmp);
1361
1362 while (f_name_cmp(*f1, *f2) <= 0) {
1363 if (!--n1)
1364 return;
1365 f1++;
1366 }
1367
1368 t = tmp;
1369 memcpy(t, f1, n1 * PTR_SIZE);
1370
1371 *f1++ = *f2++, n2--;
1372
1373 while (n1 > 0 && n2 > 0) {
1374 if (f_name_cmp(*t, *f2) <= 0)
1375 *f1++ = *t++, n1--;
1376 else
1377 *f1++ = *f2++, n2--;
1378 }
1379
1380 if (n1 > 0)
1381 memcpy(f1, t, n1 * PTR_SIZE);
1382}
1383
1384/* This file-struct sorting routine makes sure that any identical names in
1385 * the file list stay in the same order as they were in the original list.
1386 * This is particularly vital in inc_recurse mode where we expect a sort
1387 * on the flist to match the exact order of a sort on the dir_flist. */
1388static void fsort(struct file_struct **fp, size_t num)
1389{
1390 if (num <= 1)
1391 return;
1392
1393 if (use_qsort)
1394 qsort(fp, num, PTR_SIZE, file_compare);
1395 else {
1396 struct file_struct **tmp = new_array(struct file_struct *,
1397 (num+1) / 2);
1398 fsort_tmp(fp, num, tmp);
1399 free(tmp);
1400 }
1401}
1402
1403/* We take an entire set of sibling dirs from the sorted flist and link them
1404 * into the tree, setting the appropriate parent/child/sibling pointers. */
1405static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1406 int dir_cnt)
1407{
1408 int i;
1409 int32 *dp = NULL;
1410 int32 *parent_dp = parent_ndx < 0 ? NULL
1411 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1412
1413 flist_expand(dir_flist, dir_cnt);
1414 dir_flist->sorted = dir_flist->files;
1415
1416 for (i = 0; dir_cnt; i++) {
1417 struct file_struct *file = from_flist->sorted[i];
1418
1419 if (!S_ISDIR(file->mode))
1420 continue;
1421
1422 dir_flist->files[dir_flist->used++] = file;
1423 dir_cnt--;
1424
1425 if (file->basename[0] == '.' && file->basename[1] == '\0')
1426 continue;
1427
1428 if (dp)
1429 DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1430 else if (parent_dp)
1431 DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1432 else
1433 send_dir_ndx = dir_flist->used - 1;
1434
1435 dp = F_DIR_NODE_P(file);
1436 DIR_PARENT(dp) = parent_ndx;
1437 DIR_FIRST_CHILD(dp) = -1;
1438 }
1439 if (dp)
1440 DIR_NEXT_SIBLING(dp) = -1;
1441}
1442
1443/* This function is normally called by the sender, but the receiving side also
1444 * calls it from get_dirlist() with f set to -1 so that we just construct the
1445 * file list in memory without sending it over the wire. Also, get_dirlist()
1446 * might call this with f set to -2, which also indicates that local filter
1447 * rules should be ignored. */
1448static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1449 int flags)
1450{
1451 struct dirent *di;
1452 unsigned remainder;
1453 char *p;
1454 DIR *d;
1455 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1456 int start = flist->used;
1457 int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1458
1459 assert(flist != NULL);
1460
1461 if (!(d = opendir(fbuf))) {
1462 io_error |= IOERR_GENERAL;
1463 rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1464 return;
1465 }
1466
1467 p = fbuf + len;
1468 if (len != 1 || *fbuf != '/')
1469 *p++ = '/';
1470 *p = '\0';
1471 remainder = MAXPATHLEN - (p - fbuf);
1472
1473 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1474 char *dname = d_name(di);
1475 if (dname[0] == '.' && (dname[1] == '\0'
1476 || (dname[1] == '.' && dname[2] == '\0')))
1477 continue;
1478 if (strlcpy(p, dname, remainder) >= remainder) {
1479 io_error |= IOERR_GENERAL;
1480 rprintf(FINFO,
1481 "cannot send long-named file %s\n",
1482 full_fname(fbuf));
1483 continue;
1484 }
1485 if (dname[0] == '\0') {
1486 io_error |= IOERR_GENERAL;
1487 rprintf(FINFO,
1488 "cannot send file with empty name in %s\n",
1489 full_fname(fbuf));
1490 continue;
1491 }
1492
1493 send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1494 }
1495
1496 fbuf[len] = '\0';
1497
1498 if (errno) {
1499 io_error |= IOERR_GENERAL;
1500 rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1501 }
1502
1503 closedir(d);
1504
1505 if (f >= 0 && recurse && !divert_dirs) {
1506 int i, end = flist->used - 1;
1507 /* send_if_directory() bumps flist->used, so use "end". */
1508 for (i = start; i <= end; i++)
1509 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1510 }
1511}
1512
1513static char lastpath[MAXPATHLEN] = "";
1514static int lastpath_len = 0;
1515static struct file_struct *lastpath_struct;
1516
1517static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1518 char *start, char *limit, int flags, char name_type)
1519{
1520 struct file_struct *file;
1521 item_list *relname_list;
1522 relnamecache **rnpp;
1523 char *slash;
1524 int len, need_new_dir;
1525
1526 flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1527
1528 if (inc_recurse) {
1529 if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1530 && lastpath_len == limit - fname
1531 && strncmp(lastpath, fname, lastpath_len) == 0)
1532 need_new_dir = 0;
1533 else
1534 need_new_dir = 1;
1535 } else
1536 need_new_dir = 1;
1537
1538 if (need_new_dir) {
1539 int save_copy_links = copy_links;
1540 int save_xfer_dirs = xfer_dirs;
1541
1542 copy_links = xfer_dirs = 1;
1543
1544 *limit = '\0';
1545
1546 for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1547 *slash = '\0';
1548 send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1549 *slash = '/';
1550 }
1551
1552 file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1553 if (inc_recurse) {
1554 if (file && !S_ISDIR(file->mode))
1555 file = NULL;
1556 lastpath_struct = file;
1557 }
1558
1559 strlcpy(lastpath, fname, sizeof lastpath);
1560 lastpath_len = limit - fname;
1561
1562 *limit = '/';
1563
1564 copy_links = save_copy_links;
1565 xfer_dirs = save_xfer_dirs;
1566
1567 if (!inc_recurse)
1568 return;
1569 }
1570
1571 if (!lastpath_struct)
1572 return; /* dir must have vanished */
1573
1574 len = strlen(limit+1);
1575 memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1576 if (!relname_list) {
1577 if (!(relname_list = new0(item_list)))
1578 out_of_memory("send_implied_dirs");
1579 memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1580 }
1581 rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1582 if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
1583 out_of_memory("send_implied_dirs");
1584 (*rnpp)->name_type = name_type;
1585 strlcpy((*rnpp)->fname, limit+1, len + 1);
1586}
1587
1588static void send1extra(int f, struct file_struct *file, struct file_list *flist)
1589{
1590 char fbuf[MAXPATHLEN];
1591 item_list *relname_list;
1592 int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
1593 size_t j;
1594
1595 f_name(file, fbuf);
1596 dlen = strlen(fbuf);
1597
1598 if (F_PATHNAME(file) != pathname) {
1599 if (!push_pathname(F_PATHNAME(file), -1))
1600 exit_cleanup(RERR_FILESELECT);
1601 }
1602
1603 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1604
1605 if (file->flags & FLAG_CONTENT_DIR) {
1606 if (one_file_system) {
1607 STRUCT_STAT st;
1608 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1609 io_error |= IOERR_GENERAL;
1610 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1611 full_fname(fbuf));
1612 return;
1613 }
1614 filesystem_dev = st.st_dev;
1615 }
1616 send_directory(f, flist, fbuf, dlen, flags);
1617 }
1618
1619 if (!relative_paths)
1620 return;
1621
1622 memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
1623 if (!relname_list)
1624 return;
1625
1626 for (j = 0; j < relname_list->count; j++) {
1627 char *slash;
1628 relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
1629 char name_type = rnp->name_type;
1630
1631 fbuf[dlen] = '/';
1632 len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
1633 free(rnp);
1634 if (len >= (int)sizeof fbuf)
1635 continue; /* Impossible... */
1636
1637 slash = strchr(fbuf+dlen+1, '/');
1638 if (slash) {
1639 send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
1640 continue;
1641 }
1642
1643 if (name_type != NORMAL_NAME) {
1644 STRUCT_STAT st;
1645 if (link_stat(fbuf, &st, 1) != 0) {
1646 io_error |= IOERR_GENERAL;
1647 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1648 full_fname(fbuf));
1649 continue;
1650 }
1651 send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
1652 } else
1653 send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
1654 }
1655
1656 free(relname_list);
1657}
1658
1659void send_extra_file_list(int f, int at_least)
1660{
1661 struct file_list *flist;
1662 int64 start_write;
1663 uint16 prev_flags;
1664 int old_cnt, save_io_error = io_error;
1665
1666 if (flist_eof)
1667 return;
1668
1669 /* Keep sending data until we have the requested number of
1670 * files in the upcoming file-lists. */
1671 old_cnt = cur_flist->used;
1672 for (flist = first_flist; flist != cur_flist; flist = flist->next)
1673 old_cnt += flist->used;
1674 while (file_total - old_cnt < at_least) {
1675 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1676 int dir_ndx, dstart = dir_count;
1677 const char *pathname = F_PATHNAME(file);
1678 int32 *dp;
1679
1680 flist = flist_new(0, "send_extra_file_list");
1681 start_write = stats.total_written;
1682
1683 if (unsort_ndx)
1684 dir_ndx = F_NDX(file);
1685 else
1686 dir_ndx = send_dir_ndx;
1687 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1688 flist->parent_ndx = dir_ndx;
1689
1690 send1extra(f, file, flist);
1691 prev_flags = file->flags;
1692 dp = F_DIR_NODE_P(file);
1693
1694 /* If there are any duplicate directory names that follow, we
1695 * send all the dirs together in one file-list. The dir_flist
1696 * tree links all the child subdirs onto the last dup dir. */
1697 while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
1698 && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
1699 send_dir_ndx = dir_ndx;
1700 file = dir_flist->sorted[dir_ndx];
1701 /* Try to avoid some duplicate scanning of identical dirs. */
1702 if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
1703 file->flags &= ~FLAG_CONTENT_DIR;
1704 send1extra(f, file, flist);
1705 prev_flags = file->flags;
1706 dp = F_DIR_NODE_P(file);
1707 }
1708
1709 write_byte(f, 0);
1710
1711 if (need_unsorted_flist) {
1712 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
1713 out_of_memory("send_extra_file_list");
1714 memcpy(flist->sorted, flist->files,
1715 flist->used * sizeof (struct file_struct*));
1716 } else
1717 flist->sorted = flist->files;
1718
1719 flist_sort_and_clean(flist, 0);
1720
1721 add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
1722 flist_done_allocating(flist);
1723
1724 file_total += flist->used;
1725 stats.flist_size += stats.total_written - start_write;
1726 stats.num_files += flist->used;
1727 if (verbose > 3)
1728 output_flist(flist);
1729
1730 if (DIR_FIRST_CHILD(dp) >= 0) {
1731 send_dir_ndx = DIR_FIRST_CHILD(dp);
1732 send_dir_depth++;
1733 } else {
1734 while (DIR_NEXT_SIBLING(dp) < 0) {
1735 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
1736 write_ndx(f, NDX_FLIST_EOF);
1737 flist_eof = 1;
1738 change_local_filter_dir(NULL, 0, 0);
1739 goto finish;
1740 }
1741 send_dir_depth--;
1742 file = dir_flist->sorted[send_dir_ndx];
1743 dp = F_DIR_NODE_P(file);
1744 }
1745 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1746 }
1747 }
1748
1749 finish:
1750 if (io_error != save_io_error && !ignore_errors)
1751 send_msg_int(MSG_IO_ERROR, io_error);
1752}
1753
1754struct file_list *send_file_list(int f, int argc, char *argv[])
1755{
1756 static const char *lastdir;
1757 static int lastdir_len = -1;
1758 int len, dirlen;
1759 STRUCT_STAT st;
1760 char *p, *dir;
1761 struct file_list *flist;
1762 struct timeval start_tv, end_tv;
1763 int64 start_write;
1764 int use_ff_fd = 0;
1765 int disable_buffering;
1766 int flags = recurse ? FLAG_CONTENT_DIR : 0;
1767 int reading_remotely = filesfrom_host != NULL;
1768 int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
1769#ifdef ICONV_OPTION
1770 | (filesfrom_convert ? RL_CONVERT : 0)
1771#endif
1772 | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
1773
1774 rprintf(FLOG, "building file list\n");
1775 if (show_filelist_p())
1776 start_filelist_progress("building file list");
1777 else if (inc_recurse && verbose && !am_server)
1778 rprintf(FCLIENT, "sending incremental file list\n");
1779
1780 start_write = stats.total_written;
1781 gettimeofday(&start_tv, NULL);
1782
1783 if (relative_paths && protocol_version >= 30)
1784 implied_dirs = 1; /* We send flagged implied dirs */
1785
1786#ifdef SUPPORT_HARD_LINKS
1787 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
1788 init_hard_links();
1789#endif
1790
1791 flist = cur_flist = flist_new(0, "send_file_list");
1792 if (inc_recurse) {
1793 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1794 flags |= FLAG_DIVERT_DIRS;
1795 } else
1796 dir_flist = cur_flist;
1797
1798 disable_buffering = io_start_buffering_out(f);
1799 if (filesfrom_fd >= 0) {
1800 if (argv[0] && !push_dir(argv[0], 0)) {
1801 rsyserr(FERROR_XFER, errno, "push_dir %s failed in %s",
1802 full_fname(argv[0]), curr_dir);
1803 exit_cleanup(RERR_FILESELECT);
1804 }
1805 use_ff_fd = 1;
1806 }
1807
1808 while (1) {
1809 char fbuf[MAXPATHLEN], *fn, name_type;
1810
1811 if (use_ff_fd) {
1812 if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
1813 break;
1814 sanitize_path(fbuf, fbuf, "", 0);
1815 } else {
1816 if (argc-- == 0)
1817 break;
1818 strlcpy(fbuf, *argv++, MAXPATHLEN);
1819 if (sanitize_paths)
1820 sanitize_path(fbuf, fbuf, "", 0);
1821 }
1822
1823 len = strlen(fbuf);
1824 if (relative_paths) {
1825 /* We clean up fbuf below. */
1826 name_type = NORMAL_NAME;
1827 } else if (!len || fbuf[len - 1] == '/') {
1828 if (len == 2 && fbuf[0] == '.') {
1829 /* Turn "./" into just "." rather than "./." */
1830 fbuf[1] = '\0';
1831 } else {
1832 if (len + 1 >= MAXPATHLEN)
1833 overflow_exit("send_file_list");
1834 fbuf[len++] = '.';
1835 fbuf[len] = '\0';
1836 }
1837 name_type = DOT_NAME;
1838 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1839 && (len == 2 || fbuf[len-3] == '/')) {
1840 if (len + 2 >= MAXPATHLEN)
1841 overflow_exit("send_file_list");
1842 fbuf[len++] = '/';
1843 fbuf[len++] = '.';
1844 fbuf[len] = '\0';
1845 name_type = DOT_NAME;
1846 } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
1847 name_type = DOT_NAME;
1848 else
1849 name_type = NORMAL_NAME;
1850
1851 dir = NULL;
1852
1853 if (!relative_paths) {
1854 p = strrchr(fbuf, '/');
1855 if (p) {
1856 *p = '\0';
1857 if (p == fbuf)
1858 dir = "/";
1859 else
1860 dir = fbuf;
1861 len -= p - fbuf + 1;
1862 fn = p + 1;
1863 } else
1864 fn = fbuf;
1865 } else {
1866 if ((p = strstr(fbuf, "/./")) != NULL) {
1867 *p = '\0';
1868 if (p == fbuf)
1869 dir = "/";
1870 else
1871 dir = fbuf;
1872 fn = p + 3;
1873 while (*fn == '/')
1874 fn++;
1875 if (!*fn)
1876 *--fn = '\0'; /* ensure room for '.' */
1877 } else
1878 fn = fbuf;
1879 len = clean_fname(fn, CFN_KEEP_LEADING_DOT_DIR
1880 | CFN_KEEP_TRAILING_SLASH
1881 | CFN_DROP_TRAILING_DOT_DIR);
1882 if (len == 1) {
1883 if (fn[0] == '/') {
1884 fn = "/.";
1885 len = 2;
1886 name_type = DOT_NAME;
1887 } else if (fn[0] == '.')
1888 name_type = DOT_NAME;
1889 } else if (fn[len-1] == '/') {
1890 fn[--len] = '\0';
1891 if (len == 1 && *fn == '.')
1892 name_type = DOT_NAME;
1893 else
1894 name_type = SLASH_ENDING_NAME;
1895 }
1896 /* Reject a ".." dir in the active part of the path. */
1897 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1898 if ((p[2] == '/' || p[2] == '\0')
1899 && (p == fn || p[-1] == '/')) {
1900 rprintf(FERROR,
1901 "found \"..\" dir in relative path: %s\n",
1902 fn);
1903 exit_cleanup(RERR_SYNTAX);
1904 }
1905 }
1906 }
1907
1908 if (!*fn) {
1909 len = 1;
1910 fn = ".";
1911 name_type = DOT_NAME;
1912 }
1913
1914 dirlen = dir ? strlen(dir) : 0;
1915 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
1916 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
1917 continue;
1918 lastdir = pathname;
1919 lastdir_len = pathname_len;
1920 } else if (!push_pathname(lastdir, lastdir_len))
1921 continue;
1922
1923 if (fn != fbuf)
1924 memmove(fbuf, fn, len + 1);
1925
1926 if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0) {
1927 io_error |= IOERR_GENERAL;
1928 rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1929 full_fname(fbuf));
1930 continue;
1931 }
1932
1933 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1934 rprintf(FINFO, "skipping directory %s\n", fbuf);
1935 continue;
1936 }
1937
1938 if (inc_recurse && relative_paths && *fbuf) {
1939 if ((p = strchr(fbuf+1, '/')) != NULL) {
1940 if (p - fbuf == 1 && *fbuf == '.') {
1941 if ((fn = strchr(p+1, '/')) != NULL)
1942 p = fn;
1943 } else
1944 fn = p;
1945 send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
1946 if (fn == p)
1947 continue;
1948 }
1949 } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1950 /* Send the implied directories at the start of the
1951 * source spec, so we get their permissions right. */
1952 char *lp = lastpath, *slash = fbuf;
1953 *p = '\0';
1954 /* Skip any initial directories in our path that we
1955 * have in common with lastpath. */
1956 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1957 if (*fn == '/')
1958 slash = fn;
1959 }
1960 *p = '/';
1961 if (fn != p || (*lp && *lp != '/'))
1962 send_implied_dirs(f, flist, fbuf, slash, p, flags, 0);
1963 }
1964
1965 if (one_file_system)
1966 filesystem_dev = st.st_dev;
1967
1968 if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
1969 struct file_struct *file;
1970 int top_flags = FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags;
1971 file = send_file_name(f, flist, fbuf, &st,
1972 top_flags, ALL_FILTERS);
1973 if (inc_recurse) {
1974 if (name_type == DOT_NAME) {
1975 if (send_dir_depth < 0) {
1976 send_dir_depth = 0;
1977 change_local_filter_dir(fbuf, len, send_dir_depth);
1978 }
1979 send_directory(f, flist, fbuf, len, flags);
1980 }
1981 } else if (file)
1982 send_if_directory(f, flist, file, fbuf, len, flags);
1983 } else
1984 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
1985 }
1986
1987 gettimeofday(&end_tv, NULL);
1988 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1989 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1990 if (stats.flist_buildtime == 0)
1991 stats.flist_buildtime = 1;
1992 start_tv = end_tv;
1993
1994 write_byte(f, 0); /* Indicate end of file list */
1995
1996#ifdef SUPPORT_HARD_LINKS
1997 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
1998 idev_destroy();
1999#endif
2000
2001 if (show_filelist_p())
2002 finish_filelist_progress(flist);
2003
2004 gettimeofday(&end_tv, NULL);
2005 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2006 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2007
2008 /* When converting names, both sides keep an unsorted file-list array
2009 * because the names will differ on the sending and receiving sides
2010 * (both sides will use the unsorted index number for each item). */
2011
2012 /* Sort the list without removing any duplicates. This allows the
2013 * receiving side to ask for whatever name it kept. For incremental
2014 * recursion mode, the sender marks duplicate dirs so that it can
2015 * send them together in a single file-list. */
2016 if (need_unsorted_flist) {
2017 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2018 out_of_memory("send_file_list");
2019 memcpy(flist->sorted, flist->files,
2020 flist->used * sizeof (struct file_struct*));
2021 } else
2022 flist->sorted = flist->files;
2023 flist_sort_and_clean(flist, 0);
2024 file_total += flist->used;
2025
2026 if (!numeric_ids && !inc_recurse)
2027 send_id_list(f);
2028
2029 /* send the io_error flag */
2030 if (protocol_version < 30)
2031 write_int(f, ignore_errors ? 0 : io_error);
2032 else if (io_error && !ignore_errors)
2033 send_msg_int(MSG_IO_ERROR, io_error);
2034
2035 if (disable_buffering)
2036 io_end_buffering_out();
2037
2038 stats.flist_size = stats.total_written - start_write;
2039 stats.num_files = flist->used;
2040
2041 if (verbose > 3)
2042 output_flist(flist);
2043
2044 if (verbose > 2)
2045 rprintf(FINFO, "send_file_list done\n");
2046
2047 if (inc_recurse) {
2048 send_dir_depth = 1;
2049 add_dirs_to_tree(-1, flist, dir_count);
2050 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2051 flist->parent_ndx = -1;
2052 flist_done_allocating(flist);
2053 if (send_dir_ndx < 0) {
2054 write_ndx(f, NDX_FLIST_EOF);
2055 flist_eof = 1;
2056 }
2057 else if (file_total == 1) {
2058 /* If we're creating incremental file-lists and there
2059 * was just 1 item in the first file-list, send 1 more
2060 * file-list to check if this is a 1-file xfer. */
2061 send_extra_file_list(f, 1);
2062 }
2063 }
2064
2065 return flist;
2066}
2067
2068struct file_list *recv_file_list(int f)
2069{
2070 struct file_list *flist;
2071 int dstart, flags;
2072 int64 start_read;
2073
2074 if (!first_flist)
2075 rprintf(FLOG, "receiving file list\n");
2076 if (show_filelist_p())
2077 start_filelist_progress("receiving file list");
2078 else if (inc_recurse && verbose && !am_server && !first_flist)
2079 rprintf(FCLIENT, "receiving incremental file list\n");
2080
2081 start_read = stats.total_read;
2082
2083#ifdef SUPPORT_HARD_LINKS
2084 if (preserve_hard_links && !first_flist)
2085 init_hard_links();
2086#endif
2087
2088 flist = flist_new(0, "recv_file_list");
2089
2090 if (inc_recurse) {
2091 if (flist->ndx_start == 1)
2092 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2093 dstart = dir_flist->used;
2094 } else {
2095 dir_flist = flist;
2096 dstart = 0;
2097 }
2098
2099 while ((flags = read_byte(f)) != 0) {
2100 struct file_struct *file;
2101
2102 flist_expand(flist, 1);
2103
2104 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2105 flags |= read_byte(f) << 8;
2106 file = recv_file_entry(flist, flags, f);
2107
2108 if (inc_recurse && S_ISDIR(file->mode)) {
2109 flist_expand(dir_flist, 1);
2110 dir_flist->files[dir_flist->used++] = file;
2111 }
2112
2113 flist->files[flist->used++] = file;
2114
2115 maybe_emit_filelist_progress(flist->used);
2116
2117 if (verbose > 2) {
2118 rprintf(FINFO, "recv_file_name(%s)\n",
2119 f_name(file, NULL));
2120 }
2121 }
2122 file_total += flist->used;
2123
2124 if (verbose > 2)
2125 rprintf(FINFO, "received %d names\n", flist->used);
2126
2127 if (show_filelist_p())
2128 finish_filelist_progress(flist);
2129
2130 if (need_unsorted_flist) {
2131 /* Create an extra array of index pointers that we can sort for
2132 * the generator's use (for wading through the files in sorted
2133 * order and for calling flist_find()). We keep the "files"
2134 * list unsorted for our exchange of index numbers with the
2135 * other side (since their names may not sort the same). */
2136 if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
2137 out_of_memory("recv_file_list");
2138 memcpy(flist->sorted, flist->files,
2139 flist->used * sizeof (struct file_struct*));
2140 if (inc_recurse && dir_flist->used > dstart) {
2141 static int dir_flist_malloced = 0;
2142 if (dir_flist_malloced < dir_flist->malloced) {
2143 dir_flist->sorted = realloc_array(dir_flist->sorted,
2144 struct file_struct *,
2145 dir_flist->malloced);
2146 dir_flist_malloced = dir_flist->malloced;
2147 }
2148 memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2149 (dir_flist->used - dstart) * sizeof (struct file_struct*));
2150 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2151 }
2152 } else {
2153 flist->sorted = flist->files;
2154 if (inc_recurse && dir_flist->used > dstart) {
2155 dir_flist->sorted = dir_flist->files;
2156 fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2157 }
2158 }
2159
2160 if (inc_recurse)
2161 flist_done_allocating(flist);
2162 else if (f >= 0)
2163 recv_id_list(f, flist);
2164
2165 flist_sort_and_clean(flist, relative_paths);
2166
2167 if (protocol_version < 30) {
2168 /* Recv the io_error flag */
2169 if (ignore_errors)
2170 read_int(f);
2171 else
2172 io_error |= read_int(f);
2173 } else if (inc_recurse && flist->ndx_start == 1) {
2174 if (!file_total || strcmp(flist->sorted[0]->basename, ".") != 0)
2175 flist->parent_ndx = -1;
2176 }
2177
2178 if (verbose > 3)
2179 output_flist(flist);
2180
2181 if (verbose > 2)
2182 rprintf(FINFO, "recv_file_list done\n");
2183
2184 stats.flist_size += stats.total_read - start_read;
2185 stats.num_files += flist->used;
2186
2187 return flist;
2188}
2189
2190/* This is only used once by the receiver if the very first file-list
2191 * has exactly one item in it. */
2192void recv_additional_file_list(int f)
2193{
2194 struct file_list *flist;
2195 int ndx = read_ndx(f);
2196 if (ndx == NDX_FLIST_EOF) {
2197 flist_eof = 1;
2198 change_local_filter_dir(NULL, 0, 0);
2199 } else {
2200 ndx = NDX_FLIST_OFFSET - ndx;
2201 if (ndx < 0 || ndx >= dir_flist->used) {
2202 ndx = NDX_FLIST_OFFSET - ndx;
2203 rprintf(FERROR,
2204 "[%s] Invalid dir index: %d (%d - %d)\n",
2205 who_am_i(), ndx, NDX_FLIST_OFFSET,
2206 NDX_FLIST_OFFSET - dir_flist->used + 1);
2207 exit_cleanup(RERR_PROTOCOL);
2208 }
2209 if (verbose > 3) {
2210 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2211 who_am_i(), ndx);
2212 }
2213 flist = recv_file_list(f);
2214 flist->parent_ndx = ndx;
2215 }
2216}
2217
2218/* Search for an identically-named item in the file list. Note that the
2219 * items must agree in their directory-ness, or no match is returned. */
2220int flist_find(struct file_list *flist, struct file_struct *f)
2221{
2222 int low = flist->low, high = flist->high;
2223 int diff, mid, mid_up;
2224
2225 while (low <= high) {
2226 mid = (low + high) / 2;
2227 if (F_IS_ACTIVE(flist->sorted[mid]))
2228 mid_up = mid;
2229 else {
2230 /* Scan for the next non-empty entry using the cached
2231 * distance values. If the value isn't fully up-to-
2232 * date, update it. */
2233 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2234 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2235 do {
2236 mid_up += F_DEPTH(flist->sorted[mid_up]);
2237 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2238 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2239 }
2240 if (mid_up > high) {
2241 /* If there's nothing left above us, set high to
2242 * a non-empty entry below us and continue. */
2243 high = mid - (int)flist->sorted[mid]->len32;
2244 if (!F_IS_ACTIVE(flist->sorted[high])) {
2245 do {
2246 high -= (int)flist->sorted[high]->len32;
2247 } while (!F_IS_ACTIVE(flist->sorted[high]));
2248 flist->sorted[mid]->len32 = mid - high;
2249 }
2250 continue;
2251 }
2252 }
2253 diff = f_name_cmp(flist->sorted[mid_up], f);
2254 if (diff == 0) {
2255 if (protocol_version < 29
2256 && S_ISDIR(flist->sorted[mid_up]->mode)
2257 != S_ISDIR(f->mode))
2258 return -1;
2259 return mid_up;
2260 }
2261 if (diff < 0)
2262 low = mid_up + 1;
2263 else
2264 high = mid - 1;
2265 }
2266 return -1;
2267}
2268
2269/*
2270 * Free up any resources a file_struct has allocated
2271 * and clear the file.
2272 */
2273void clear_file(struct file_struct *file)
2274{
2275 /* The +1 zeros out the first char of the basename. */
2276 memset(file, 0, FILE_STRUCT_LEN + 1);
2277 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
2278 * entry. Likewise for len32 in the opposite direction. We assume
2279 * that we're alone for now since flist_find() will adjust the counts
2280 * it runs into that aren't up-to-date. */
2281 file->len32 = F_DEPTH(file) = 1;
2282}
2283
2284/* Allocate a new file list. */
2285struct file_list *flist_new(int flags, char *msg)
2286{
2287 struct file_list *flist;
2288
2289 if (!(flist = new0(struct file_list)))
2290 out_of_memory(msg);
2291
2292 if (flags & FLIST_TEMP) {
2293 if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2294 out_of_memory, POOL_INTERN)))
2295 out_of_memory(msg);
2296 } else {
2297 /* This is a doubly linked list with prev looping back to
2298 * the end of the list, but the last next pointer is NULL. */
2299 if (!first_flist) {
2300 flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2301 out_of_memory, POOL_INTERN);
2302 if (!flist->file_pool)
2303 out_of_memory(msg);
2304
2305 flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2306
2307 first_flist = cur_flist = flist->prev = flist;
2308 } else {
2309 struct file_list *prev = first_flist->prev;
2310
2311 flist->file_pool = first_flist->file_pool;
2312
2313 flist->ndx_start = prev->ndx_start + prev->used + 1;
2314 flist->flist_num = prev->flist_num + 1;
2315
2316 flist->prev = prev;
2317 prev->next = first_flist->prev = flist;
2318 }
2319 flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2320 flist_cnt++;
2321 }
2322
2323 return flist;
2324}
2325
2326/* Free up all elements in a flist. */
2327void flist_free(struct file_list *flist)
2328{
2329 if (!flist->prev) {
2330 /* Was FLIST_TEMP dir-list. */
2331 } else if (flist == flist->prev) {
2332 first_flist = cur_flist = NULL;
2333 file_total = 0;
2334 flist_cnt = 0;
2335 } else {
2336 if (flist == cur_flist)
2337 cur_flist = flist->next;
2338 if (flist == first_flist)
2339 first_flist = first_flist->next;
2340 else {
2341 flist->prev->next = flist->next;
2342 if (!flist->next)
2343 flist->next = first_flist;
2344 }
2345 flist->next->prev = flist->prev;
2346 file_total -= flist->used;
2347 flist_cnt--;
2348 }
2349
2350 if (!flist->prev || !flist_cnt)
2351 pool_destroy(flist->file_pool);
2352 else
2353 pool_free_old(flist->file_pool, flist->pool_boundary);
2354
2355 if (flist->sorted && flist->sorted != flist->files)
2356 free(flist->sorted);
2357 free(flist->files);
2358 free(flist);
2359}
2360
2361/* This routine ensures we don't have any duplicate names in our file list.
2362 * duplicate names can cause corruption because of the pipelining. */
2363static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2364{
2365 char fbuf[MAXPATHLEN];
2366 int i, prev_i;
2367
2368 if (!flist)
2369 return;
2370 if (flist->used == 0) {
2371 flist->high = -1;
2372 flist->low = 0;
2373 return;
2374 }
2375
2376 fsort(flist->sorted, flist->used);
2377
2378 if (!am_sender || inc_recurse) {
2379 for (i = prev_i = 0; i < flist->used; i++) {
2380 if (F_IS_ACTIVE(flist->sorted[i])) {
2381 prev_i = i;
2382 break;
2383 }
2384 }
2385 flist->low = prev_i;
2386 } else {
2387 i = prev_i = flist->used - 1;
2388 flist->low = 0;
2389 }
2390
2391 while (++i < flist->used) {
2392 int j;
2393 struct file_struct *file = flist->sorted[i];
2394
2395 if (!F_IS_ACTIVE(file))
2396 continue;
2397 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2398 j = prev_i;
2399 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2400 int save_mode = file->mode;
2401 /* Make sure that this directory doesn't duplicate a
2402 * non-directory earlier in the list. */
2403 flist->high = prev_i;
2404 file->mode = S_IFREG;
2405 j = flist_find(flist, file);
2406 file->mode = save_mode;
2407 } else
2408 j = -1;
2409 if (j >= 0) {
2410 int keep, drop;
2411 /* If one is a dir and the other is not, we want to
2412 * keep the dir because it might have contents in the
2413 * list. Otherwise keep the first one. */
2414 if (S_ISDIR(file->mode)) {
2415 struct file_struct *fp = flist->sorted[j];
2416 if (!S_ISDIR(fp->mode))
2417 keep = i, drop = j;
2418 else {
2419 if (am_sender)
2420 file->flags |= FLAG_DUPLICATE;
2421 else { /* Make sure we merge our vital flags. */
2422 fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
2423 fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
2424 }
2425 keep = j, drop = i;
2426 }
2427 } else
2428 keep = j, drop = i;
2429
2430 if (!am_sender) {
2431 if (verbose > 1) {
2432 rprintf(FINFO,
2433 "removing duplicate name %s from file list (%d)\n",
2434 f_name(file, fbuf), drop + flist->ndx_start);
2435 }
2436 clear_file(flist->sorted[drop]);
2437 }
2438
2439 if (keep == i) {
2440 if (flist->low == drop) {
2441 for (j = drop + 1;
2442 j < i && !F_IS_ACTIVE(flist->sorted[j]);
2443 j++) {}
2444 flist->low = j;
2445 }
2446 prev_i = i;
2447 }
2448 } else
2449 prev_i = i;
2450 }
2451 flist->high = prev_i;
2452
2453 if (strip_root) {
2454 /* We need to strip off the leading slashes for relative
2455 * paths, but this must be done _after_ the sorting phase. */
2456 for (i = flist->low; i <= flist->high; i++) {
2457 struct file_struct *file = flist->sorted[i];
2458
2459 if (!file->dirname)
2460 continue;
2461 while (*file->dirname == '/')
2462 file->dirname++;
2463 if (!*file->dirname)
2464 file->dirname = NULL;
2465 }
2466 }
2467
2468 if (prune_empty_dirs && !am_sender) {
2469 int j, prev_depth = 0;
2470
2471 prev_i = 0; /* It's OK that this isn't really true. */
2472
2473 for (i = flist->low; i <= flist->high; i++) {
2474 struct file_struct *fp, *file = flist->sorted[i];
2475
2476 /* This temporarily abuses the F_DEPTH() value for a
2477 * directory that is in a chain that might get pruned.
2478 * We restore the old value if it gets a reprieve. */
2479 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
2480 /* Dump empty dirs when coming back down. */
2481 for (j = prev_depth; j >= F_DEPTH(file); j--) {
2482 fp = flist->sorted[prev_i];
2483 if (F_DEPTH(fp) >= 0)
2484 break;
2485 prev_i = -F_DEPTH(fp)-1;
2486 clear_file(fp);
2487 }
2488 prev_depth = F_DEPTH(file);
2489 if (is_excluded(f_name(file, fbuf), 1,
2490 ALL_FILTERS)) {
2491 /* Keep dirs through this dir. */
2492 for (j = prev_depth-1; ; j--) {
2493 fp = flist->sorted[prev_i];
2494 if (F_DEPTH(fp) >= 0)
2495 break;
2496 prev_i = -F_DEPTH(fp)-1;
2497 F_DEPTH(fp) = j;
2498 }
2499 } else
2500 F_DEPTH(file) = -prev_i-1;
2501 prev_i = i;
2502 } else {
2503 /* Keep dirs through this non-dir. */
2504 for (j = prev_depth; ; j--) {
2505 fp = flist->sorted[prev_i];
2506 if (F_DEPTH(fp) >= 0)
2507 break;
2508 prev_i = -F_DEPTH(fp)-1;
2509 F_DEPTH(fp) = j;
2510 }
2511 }
2512 }
2513 /* Dump all remaining empty dirs. */
2514 while (1) {
2515 struct file_struct *fp = flist->sorted[prev_i];
2516 if (F_DEPTH(fp) >= 0)
2517 break;
2518 prev_i = -F_DEPTH(fp)-1;
2519 clear_file(fp);
2520 }
2521
2522 for (i = flist->low; i <= flist->high; i++) {
2523 if (F_IS_ACTIVE(flist->sorted[i]))
2524 break;
2525 }
2526 flist->low = i;
2527 for (i = flist->high; i >= flist->low; i--) {
2528 if (F_IS_ACTIVE(flist->sorted[i]))
2529 break;
2530 }
2531 flist->high = i;
2532 }
2533}
2534
2535static void output_flist(struct file_list *flist)
2536{
2537 char uidbuf[16], gidbuf[16], depthbuf[16];
2538 struct file_struct *file;
2539 const char *root, *dir, *slash, *name, *trail;
2540 const char *who = who_am_i();
2541 int i;
2542
2543 rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
2544 who, flist->ndx_start, flist->used, flist->low, flist->high);
2545 for (i = 0; i < flist->used; i++) {
2546 file = flist->files[i];
2547 if ((am_root || am_sender) && uid_ndx) {
2548 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2549 F_OWNER(file));
2550 } else
2551 *uidbuf = '\0';
2552 if (gid_ndx) {
2553 static char parens[] = "(\0)\0\0\0";
2554 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2555 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2556 pp, F_GROUP(file), pp + 2);
2557 } else
2558 *gidbuf = '\0';
2559 if (!am_sender)
2560 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
2561 if (F_IS_ACTIVE(file)) {
2562 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
2563 if ((dir = file->dirname) == NULL)
2564 dir = slash = "";
2565 else
2566 slash = "/";
2567 name = file->basename;
2568 trail = S_ISDIR(file->mode) ? "/" : "";
2569 } else
2570 root = dir = slash = name = trail = "";
2571 rprintf(FINFO,
2572 "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
2573 who, i + flist->ndx_start,
2574 root, dir, slash, name, trail,
2575 (int)file->mode, (double)F_LENGTH(file),
2576 uidbuf, gidbuf, file->flags);
2577 }
2578}
2579
2580enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
2581enum fnc_type { t_PATH, t_ITEM };
2582
2583static int found_prefix;
2584
2585/* Compare the names of two file_struct entities, similar to how strcmp()
2586 * would do if it were operating on the joined strings.
2587 *
2588 * Some differences beginning with protocol_version 29: (1) directory names
2589 * are compared with an assumed trailing slash so that they compare in a
2590 * way that would cause them to sort immediately prior to any content they
2591 * may have; (2) a directory of any name compares after a non-directory of
2592 * any name at the same depth; (3) a directory with name "." compares prior
2593 * to anything else. These changes mean that a directory and a non-dir
2594 * with the same name will not compare as equal (protocol_version >= 29).
2595 *
2596 * The dirname component can be an empty string, but the basename component
2597 * cannot (and never is in the current codebase). The basename component
2598 * may be NULL (for a removed item), in which case it is considered to be
2599 * after any existing item. */
2600int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
2601{
2602 int dif;
2603 const uchar *c1, *c2;
2604 enum fnc_state state1, state2;
2605 enum fnc_type type1, type2;
2606 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
2607
2608 if (!f1 || !F_IS_ACTIVE(f1)) {
2609 if (!f2 || !F_IS_ACTIVE(f2))
2610 return 0;
2611 return -1;
2612 }
2613 if (!f2 || !F_IS_ACTIVE(f2))
2614 return 1;
2615
2616 c1 = (uchar*)f1->dirname;
2617 c2 = (uchar*)f2->dirname;
2618 if (c1 == c2)
2619 c1 = c2 = NULL;
2620 if (!c1) {
2621 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2622 c1 = (const uchar*)f1->basename;
2623 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2624 type1 = t_ITEM;
2625 state1 = s_TRAILING;
2626 c1 = (uchar*)"";
2627 } else
2628 state1 = s_BASE;
2629 } else {
2630 type1 = t_path;
2631 state1 = s_DIR;
2632 }
2633 if (!c2) {
2634 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2635 c2 = (const uchar*)f2->basename;
2636 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2637 type2 = t_ITEM;
2638 state2 = s_TRAILING;
2639 c2 = (uchar*)"";
2640 } else
2641 state2 = s_BASE;
2642 } else {
2643 type2 = t_path;
2644 state2 = s_DIR;
2645 }
2646
2647 if (type1 != type2)
2648 return type1 == t_PATH ? 1 : -1;
2649
2650 do {
2651 if (!*c1) {
2652 switch (state1) {
2653 case s_DIR:
2654 state1 = s_SLASH;
2655 c1 = (uchar*)"/";
2656 break;
2657 case s_SLASH:
2658 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
2659 c1 = (const uchar*)f1->basename;
2660 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2661 type1 = t_ITEM;
2662 state1 = s_TRAILING;
2663 c1 = (uchar*)"";
2664 } else
2665 state1 = s_BASE;
2666 break;
2667 case s_BASE:
2668 state1 = s_TRAILING;
2669 if (type1 == t_PATH) {
2670 c1 = (uchar*)"/";
2671 break;
2672 }
2673 /* FALL THROUGH */
2674 case s_TRAILING:
2675 type1 = t_ITEM;
2676 break;
2677 }
2678 if (*c2 && type1 != type2)
2679 return type1 == t_PATH ? 1 : -1;
2680 }
2681 if (!*c2) {
2682 switch (state2) {
2683 case s_DIR:
2684 state2 = s_SLASH;
2685 c2 = (uchar*)"/";
2686 break;
2687 case s_SLASH:
2688 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
2689 c2 = (const uchar*)f2->basename;
2690 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2691 type2 = t_ITEM;
2692 state2 = s_TRAILING;
2693 c2 = (uchar*)"";
2694 } else
2695 state2 = s_BASE;
2696 break;
2697 case s_BASE:
2698 state2 = s_TRAILING;
2699 if (type2 == t_PATH) {
2700 c2 = (uchar*)"/";
2701 break;
2702 }
2703 /* FALL THROUGH */
2704 case s_TRAILING:
2705 found_prefix = 1;
2706 if (!*c1)
2707 return 0;
2708 type2 = t_ITEM;
2709 break;
2710 }
2711 if (type1 != type2)
2712 return type1 == t_PATH ? 1 : -1;
2713 }
2714 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
2715
2716 return dif;
2717}
2718
2719/* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
2720 * not match if f2's basename is not an exact match of a path element in f1.
2721 * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
2722int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
2723{
2724 found_prefix = 0;
2725 f_name_cmp(f1, f2);
2726 return found_prefix;
2727}
2728
2729char *f_name_buf(void)
2730{
2731 static char names[5][MAXPATHLEN];
2732 static unsigned int n;
2733
2734 n = (n + 1) % (sizeof names / sizeof names[0]);
2735
2736 return names[n];
2737}
2738
2739/* Return a copy of the full filename of a flist entry, using the indicated
2740 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2741 * done because we checked the size when creating the file_struct entry.
2742 */
2743char *f_name(const struct file_struct *f, char *fbuf)
2744{
2745 if (!f || !F_IS_ACTIVE(f))
2746 return NULL;
2747
2748 if (!fbuf)
2749 fbuf = f_name_buf();
2750
2751 if (f->dirname) {
2752 int len = strlen(f->dirname);
2753 memcpy(fbuf, f->dirname, len);
2754 fbuf[len] = '/';
2755 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
2756 } else
2757 strlcpy(fbuf, f->basename, MAXPATHLEN);
2758
2759 return fbuf;
2760}
2761
2762/* Do a non-recursive scan of the named directory, possibly ignoring all
2763 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2764 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2765 * buffer (the functions we call will append names onto the end, but the old
2766 * dir value will be restored on exit). */
2767struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
2768{
2769 struct file_list *dirlist;
2770 char dirbuf[MAXPATHLEN];
2771 int save_recurse = recurse;
2772 int save_xfer_dirs = xfer_dirs;
2773 int save_prune_empty_dirs = prune_empty_dirs;
2774
2775 if (dlen < 0) {
2776 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2777 if (dlen >= MAXPATHLEN)
2778 return NULL;
2779 dirname = dirbuf;
2780 }
2781
2782 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
2783
2784 recurse = 0;
2785 xfer_dirs = 1;
2786 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
2787 xfer_dirs = save_xfer_dirs;
2788 recurse = save_recurse;
2789 if (do_progress)
2790 flist_count_offset += dirlist->used;
2791
2792 prune_empty_dirs = 0;
2793 dirlist->sorted = dirlist->files;
2794 flist_sort_and_clean(dirlist, 0);
2795 prune_empty_dirs = save_prune_empty_dirs;
2796
2797 if (verbose > 3)
2798 output_flist(dirlist);
2799
2800 return dirlist;
2801}