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