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