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