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