| 1 | /* -*- c-file-style: "linux" -*- |
| 2 | |
| 3 | Copyright (C) 1996-2000 by Andrew Tridgell |
| 4 | Copyright (C) Paul Mackerras 1996 |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "rsync.h" |
| 22 | |
| 23 | extern int verbose; |
| 24 | extern int recurse; |
| 25 | extern int delete_mode; |
| 26 | extern int delete_after; |
| 27 | extern int max_delete; |
| 28 | extern int csum_length; |
| 29 | extern struct stats stats; |
| 30 | extern int dry_run; |
| 31 | extern int am_server; |
| 32 | extern int relative_paths; |
| 33 | extern int preserve_hard_links; |
| 34 | extern int preserve_perms; |
| 35 | extern int cvs_exclude; |
| 36 | extern int io_error; |
| 37 | extern char *tmpdir; |
| 38 | extern char *compare_dest; |
| 39 | extern int make_backups; |
| 40 | extern int do_progress; |
| 41 | extern char *backup_dir; |
| 42 | extern char *backup_suffix; |
| 43 | extern int backup_suffix_len; |
| 44 | extern int cleanup_got_literal; |
| 45 | extern int module_id; |
| 46 | extern int ignore_errors; |
| 47 | extern int orig_umask; |
| 48 | extern int keep_partial; |
| 49 | |
| 50 | static void delete_one(char *fn, int is_dir) |
| 51 | { |
| 52 | if (!is_dir) { |
| 53 | if (robust_unlink(fn) != 0) { |
| 54 | rsyserr(FERROR, errno, "delete_one: unlink %s failed", |
| 55 | full_fname(fn)); |
| 56 | } else if (verbose) { |
| 57 | rprintf(FINFO, "deleting %s\n", fn); |
| 58 | } |
| 59 | } else { |
| 60 | if (do_rmdir(fn) != 0) { |
| 61 | if (errno != ENOTEMPTY && errno != EEXIST) { |
| 62 | rsyserr(FERROR, errno, |
| 63 | "delete_one: rmdir %s failed", |
| 64 | full_fname(fn)); |
| 65 | } |
| 66 | } else if (verbose) { |
| 67 | rprintf(FINFO, "deleting directory %s\n", fn); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static int is_backup_file(char *fn) |
| 74 | { |
| 75 | int k = strlen(fn) - backup_suffix_len; |
| 76 | return k > 0 && strcmp(fn+k, backup_suffix) == 0; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* This deletes any files on the receiving side that are not present |
| 81 | * on the sending side. */ |
| 82 | void delete_files(struct file_list *flist) |
| 83 | { |
| 84 | struct file_list *local_file_list; |
| 85 | int i, j; |
| 86 | char *argv[1], fbuf[MAXPATHLEN]; |
| 87 | static int deletion_count; |
| 88 | |
| 89 | if (cvs_exclude) |
| 90 | add_cvs_excludes(); |
| 91 | |
| 92 | if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) { |
| 93 | rprintf(FINFO,"IO error encountered - skipping file deletion\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | for (j = 0; j < flist->count; j++) { |
| 98 | if (!(flist->files[j]->flags & FLAG_TOP_DIR) |
| 99 | || !S_ISDIR(flist->files[j]->mode)) |
| 100 | continue; |
| 101 | |
| 102 | argv[0] = f_name_to(flist->files[j], fbuf); |
| 103 | |
| 104 | if (!(local_file_list = send_file_list(-1, 1, argv))) |
| 105 | continue; |
| 106 | |
| 107 | if (verbose > 1) |
| 108 | rprintf(FINFO, "deleting in %s\n", fbuf); |
| 109 | |
| 110 | for (i = local_file_list->count-1; i >= 0; i--) { |
| 111 | if (max_delete && deletion_count > max_delete) |
| 112 | break; |
| 113 | if (!local_file_list->files[i]->basename) |
| 114 | continue; |
| 115 | if (flist_find(flist,local_file_list->files[i]) < 0) { |
| 116 | char *f = f_name(local_file_list->files[i]); |
| 117 | if (make_backups && (backup_dir || !is_backup_file(f))) { |
| 118 | (void) make_backup(f); |
| 119 | if (verbose) |
| 120 | rprintf(FINFO, "deleting %s\n", f); |
| 121 | } else { |
| 122 | int mode = local_file_list->files[i]->mode; |
| 123 | delete_one(f, S_ISDIR(mode) != 0); |
| 124 | } |
| 125 | deletion_count++; |
| 126 | } |
| 127 | } |
| 128 | flist_free(local_file_list); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
| 134 | * get_tmpname() - create a tmp filename for a given filename |
| 135 | * |
| 136 | * If a tmpdir is defined, use that as the directory to |
| 137 | * put it in. Otherwise, the tmp filename is in the same |
| 138 | * directory as the given name. Note that there may be no |
| 139 | * directory at all in the given name! |
| 140 | * |
| 141 | * The tmp filename is basically the given filename with a |
| 142 | * dot prepended, and .XXXXXX appended (for mkstemp() to |
| 143 | * put its unique gunk in). Take care to not exceed |
| 144 | * either the MAXPATHLEN or NAME_MAX, esp. the last, as |
| 145 | * the basename basically becomes 8 chars longer. In that |
| 146 | * case, the original name is shortened sufficiently to |
| 147 | * make it all fit. |
| 148 | * |
| 149 | * Of course, there's no real reason for the tmp name to |
| 150 | * look like the original, except to satisfy us humans. |
| 151 | * As long as it's unique, rsync will work. |
| 152 | */ |
| 153 | |
| 154 | static int get_tmpname(char *fnametmp, char *fname) |
| 155 | { |
| 156 | char *f; |
| 157 | int length = 0; |
| 158 | int maxname; |
| 159 | |
| 160 | if (tmpdir) { |
| 161 | /* Note: this can't overflow, so the return value is safe */ |
| 162 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
| 163 | fnametmp[length++] = '/'; |
| 164 | fnametmp[length] = '\0'; /* always NULL terminated */ |
| 165 | } |
| 166 | |
| 167 | if ((f = strrchr(fname, '/')) != NULL) { |
| 168 | ++f; |
| 169 | if (!tmpdir) { |
| 170 | length = f - fname; |
| 171 | /* copy up to and including the slash */ |
| 172 | strlcpy(fnametmp, fname, length + 1); |
| 173 | } |
| 174 | } else |
| 175 | f = fname; |
| 176 | fnametmp[length++] = '.'; |
| 177 | fnametmp[length] = '\0'; /* always NULL terminated */ |
| 178 | |
| 179 | maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8); |
| 180 | |
| 181 | if (maxname < 1) { |
| 182 | rprintf(FERROR, "temporary filename too long: %s\n", fname); |
| 183 | fnametmp[0] = '\0'; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | strlcpy(fnametmp + length, f, maxname); |
| 188 | strcat(fnametmp + length, ".XXXXXX"); |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname, |
| 195 | OFF_T total_size) |
| 196 | { |
| 197 | int i; |
| 198 | struct sum_struct sum; |
| 199 | unsigned int len; |
| 200 | OFF_T offset = 0; |
| 201 | OFF_T offset2; |
| 202 | char *data; |
| 203 | static char file_sum1[MD4_SUM_LENGTH]; |
| 204 | static char file_sum2[MD4_SUM_LENGTH]; |
| 205 | char *map=NULL; |
| 206 | |
| 207 | read_sum_head(f_in, &sum); |
| 208 | |
| 209 | sum_init(); |
| 210 | |
| 211 | while ((i = recv_token(f_in, &data)) != 0) { |
| 212 | if (do_progress) |
| 213 | show_progress(offset, total_size); |
| 214 | |
| 215 | if (i > 0) { |
| 216 | if (verbose > 3) { |
| 217 | rprintf(FINFO,"data recv %d at %.0f\n", |
| 218 | i,(double)offset); |
| 219 | } |
| 220 | |
| 221 | stats.literal_data += i; |
| 222 | cleanup_got_literal = 1; |
| 223 | |
| 224 | sum_update(data,i); |
| 225 | |
| 226 | if (fd != -1 && write_file(fd,data,i) != i) { |
| 227 | rsyserr(FERROR, errno, "write failed on %s", |
| 228 | full_fname(fname)); |
| 229 | exit_cleanup(RERR_FILEIO); |
| 230 | } |
| 231 | offset += i; |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | i = -(i+1); |
| 236 | offset2 = i*(OFF_T)sum.blength; |
| 237 | len = sum.blength; |
| 238 | if (i == (int) sum.count-1 && sum.remainder != 0) |
| 239 | len = sum.remainder; |
| 240 | |
| 241 | stats.matched_data += len; |
| 242 | |
| 243 | if (verbose > 3) |
| 244 | rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n", |
| 245 | i,len,(double)offset2,(double)offset); |
| 246 | |
| 247 | if (mapbuf) { |
| 248 | map = map_ptr(mapbuf,offset2,len); |
| 249 | |
| 250 | see_token(map, len); |
| 251 | sum_update(map,len); |
| 252 | } |
| 253 | |
| 254 | if (fd != -1 && write_file(fd,map,len) != (int) len) { |
| 255 | rsyserr(FERROR, errno, "write failed on %s", |
| 256 | full_fname(fname)); |
| 257 | exit_cleanup(RERR_FILEIO); |
| 258 | } |
| 259 | offset += len; |
| 260 | } |
| 261 | |
| 262 | flush_write_file(fd); |
| 263 | |
| 264 | if (do_progress) |
| 265 | end_progress(total_size); |
| 266 | |
| 267 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { |
| 268 | rsyserr(FERROR, errno, "write failed on %s", |
| 269 | full_fname(fname)); |
| 270 | exit_cleanup(RERR_FILEIO); |
| 271 | } |
| 272 | |
| 273 | sum_end(file_sum1); |
| 274 | |
| 275 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
| 276 | if (verbose > 2) { |
| 277 | rprintf(FINFO,"got file_sum\n"); |
| 278 | } |
| 279 | if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) { |
| 280 | return 0; |
| 281 | } |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /** |
| 287 | * main routine for receiver process. |
| 288 | * |
| 289 | * Receiver process runs on the same host as the generator process. */ |
| 290 | int recv_files(int f_in,struct file_list *flist,char *local_name) |
| 291 | { |
| 292 | int fd1,fd2; |
| 293 | STRUCT_STAT st; |
| 294 | char *fname, fbuf[MAXPATHLEN]; |
| 295 | char template[MAXPATHLEN]; |
| 296 | char fnametmp[MAXPATHLEN]; |
| 297 | char *fnamecmp; |
| 298 | char fnamecmpbuf[MAXPATHLEN]; |
| 299 | struct map_struct *mapbuf; |
| 300 | int i; |
| 301 | struct file_struct *file; |
| 302 | int phase=0; |
| 303 | int recv_ok; |
| 304 | struct stats initial_stats; |
| 305 | |
| 306 | if (verbose > 2) { |
| 307 | rprintf(FINFO,"recv_files(%d) starting\n",flist->count); |
| 308 | } |
| 309 | |
| 310 | if (flist->hlink_pool) { |
| 311 | pool_destroy(flist->hlink_pool); |
| 312 | flist->hlink_pool = NULL; |
| 313 | } |
| 314 | |
| 315 | while (1) { |
| 316 | cleanup_disable(); |
| 317 | |
| 318 | i = read_int(f_in); |
| 319 | if (i == -1) { |
| 320 | if (phase == 0) { |
| 321 | phase++; |
| 322 | csum_length = SUM_LENGTH; |
| 323 | if (verbose > 2) |
| 324 | rprintf(FINFO,"recv_files phase=%d\n",phase); |
| 325 | send_msg(MSG_DONE, "", 0); |
| 326 | continue; |
| 327 | } |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | if (i < 0 || i >= flist->count) { |
| 332 | rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n", |
| 333 | i, flist->count); |
| 334 | exit_cleanup(RERR_PROTOCOL); |
| 335 | } |
| 336 | |
| 337 | file = flist->files[i]; |
| 338 | |
| 339 | stats.current_file_index = i; |
| 340 | stats.num_transferred_files++; |
| 341 | stats.total_transferred_size += file->length; |
| 342 | cleanup_got_literal = 0; |
| 343 | |
| 344 | if (local_name) |
| 345 | fname = local_name; |
| 346 | else |
| 347 | fname = f_name_to(file, fbuf); |
| 348 | |
| 349 | if (dry_run) { |
| 350 | if (!am_server && verbose) { /* log transfer */ |
| 351 | rprintf(FINFO, "%s\n", fname); |
| 352 | } |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | initial_stats = stats; |
| 357 | |
| 358 | if (verbose > 2) |
| 359 | rprintf(FINFO,"recv_files(%s)\n",fname); |
| 360 | |
| 361 | fnamecmp = fname; |
| 362 | |
| 363 | /* open the file */ |
| 364 | fd1 = do_open(fnamecmp, O_RDONLY, 0); |
| 365 | |
| 366 | if (fd1 == -1 && compare_dest != NULL) { |
| 367 | /* try the file at compare_dest instead */ |
| 368 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
| 369 | compare_dest, fname); |
| 370 | fnamecmp = fnamecmpbuf; |
| 371 | fd1 = do_open(fnamecmp, O_RDONLY, 0); |
| 372 | } |
| 373 | |
| 374 | if (fd1 != -1 && do_fstat(fd1,&st) != 0) { |
| 375 | rsyserr(FERROR, errno, "fstat %s failed", |
| 376 | full_fname(fnamecmp)); |
| 377 | receive_data(f_in,NULL,-1,NULL,file->length); |
| 378 | close(fd1); |
| 379 | continue; |
| 380 | } |
| 381 | |
| 382 | if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) { |
| 383 | /* this special handling for directories |
| 384 | * wouldn't be necessary if robust_rename() |
| 385 | * and the underlying robust_unlink could cope |
| 386 | * with directories |
| 387 | */ |
| 388 | rprintf(FERROR,"recv_files: %s is a directory\n", |
| 389 | full_fname(fnamecmp)); |
| 390 | receive_data(f_in, NULL, -1, NULL, file->length); |
| 391 | close(fd1); |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
| 396 | close(fd1); |
| 397 | fd1 = -1; |
| 398 | mapbuf = NULL; |
| 399 | } |
| 400 | |
| 401 | if (fd1 != -1 && !preserve_perms) { |
| 402 | /* if the file exists already and we aren't preserving |
| 403 | * permissions then act as though the remote end sent |
| 404 | * us the file permissions we already have */ |
| 405 | file->mode = st.st_mode; |
| 406 | } |
| 407 | |
| 408 | if (fd1 != -1 && st.st_size > 0) { |
| 409 | mapbuf = map_file(fd1,st.st_size); |
| 410 | if (verbose > 2) |
| 411 | rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size); |
| 412 | } else |
| 413 | mapbuf = NULL; |
| 414 | |
| 415 | if (!get_tmpname(fnametmp,fname)) { |
| 416 | if (mapbuf) unmap_file(mapbuf); |
| 417 | if (fd1 != -1) close(fd1); |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | strlcpy(template, fnametmp, sizeof template); |
| 422 | |
| 423 | /* we initially set the perms without the |
| 424 | * setuid/setgid bits to ensure that there is no race |
| 425 | * condition. They are then correctly updated after |
| 426 | * the lchown. Thanks to snabb@epipe.fi for pointing |
| 427 | * this out. We also set it initially without group |
| 428 | * access because of a similar race condition. */ |
| 429 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
| 430 | |
| 431 | /* in most cases parent directories will already exist |
| 432 | * because their information should have been previously |
| 433 | * transferred, but that may not be the case with -R */ |
| 434 | if (fd2 == -1 && relative_paths && errno == ENOENT && |
| 435 | create_directory_path(fnametmp, orig_umask) == 0) { |
| 436 | strlcpy(fnametmp, template, sizeof fnametmp); |
| 437 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
| 438 | } |
| 439 | if (fd2 == -1) { |
| 440 | rsyserr(FERROR, errno, "mkstemp %s failed", |
| 441 | full_fname(fnametmp)); |
| 442 | receive_data(f_in,mapbuf,-1,NULL,file->length); |
| 443 | if (mapbuf) unmap_file(mapbuf); |
| 444 | if (fd1 != -1) close(fd1); |
| 445 | continue; |
| 446 | } |
| 447 | |
| 448 | cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2); |
| 449 | |
| 450 | if (!am_server && verbose) { /* log transfer */ |
| 451 | rprintf(FINFO, "%s\n", fname); |
| 452 | } |
| 453 | |
| 454 | /* recv file data */ |
| 455 | recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length); |
| 456 | |
| 457 | log_recv(file, &initial_stats); |
| 458 | |
| 459 | if (mapbuf) unmap_file(mapbuf); |
| 460 | if (fd1 != -1) { |
| 461 | close(fd1); |
| 462 | } |
| 463 | if (close(fd2) < 0) { |
| 464 | rsyserr(FERROR, errno, "close failed on %s", |
| 465 | full_fname(fnametmp)); |
| 466 | exit_cleanup(RERR_FILEIO); |
| 467 | } |
| 468 | |
| 469 | if (verbose > 2) |
| 470 | rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname); |
| 471 | |
| 472 | if (recv_ok || keep_partial) |
| 473 | finish_transfer(fname, fnametmp, file, recv_ok); |
| 474 | else |
| 475 | do_unlink(fnametmp); |
| 476 | |
| 477 | cleanup_disable(); |
| 478 | |
| 479 | if (!recv_ok) { |
| 480 | if (csum_length == SUM_LENGTH) { |
| 481 | rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n", |
| 482 | full_fname(fname)); |
| 483 | } else { |
| 484 | char buf[4]; |
| 485 | if (verbose > 1) |
| 486 | rprintf(FINFO,"redoing %s(%d)\n",fname,i); |
| 487 | SIVAL(buf, 0, i); |
| 488 | send_msg(MSG_REDO, buf, 4); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (delete_after && recurse && delete_mode && !local_name |
| 494 | && flist->count > 0) |
| 495 | delete_files(flist); |
| 496 | |
| 497 | if (verbose > 2) |
| 498 | rprintf(FINFO,"recv_files finished\n"); |
| 499 | |
| 500 | return 0; |
| 501 | } |