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