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