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