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