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