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