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