1 /*- 2 * Copyright (c) 1992 Keith Muller. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Keith Muller of the University of California, San Diego. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 41 #endif 42 #endif /* not lint */ 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/types.h> 47 #include <sys/time.h> 48 #include <sys/stat.h> 49 #include <unistd.h> 50 #include <fcntl.h> 51 #include <string.h> 52 #include <stdio.h> 53 #include <errno.h> 54 #include <sys/uio.h> 55 #include <stdlib.h> 56 #include "pax.h" 57 #include "options.h" 58 #include "extern.h" 59 60 static int 61 mk_link(char *,struct stat *,char *, int); 62 63 /* 64 * routines that deal with file operations such as: creating, removing; 65 * and setting access modes, uid/gid and times of files 66 */ 67 68 #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 69 #define SETBITS (S_ISUID | S_ISGID) 70 #define ABITS (FILEBITS | SETBITS) 71 72 /* 73 * file_creat() 74 * Create and open a file. 75 * Return: 76 * file descriptor or -1 for failure 77 */ 78 79 int 80 file_creat(ARCHD *arcn) 81 { 82 int fd = -1; 83 mode_t file_mode; 84 int oerrno; 85 86 /* 87 * assume file doesn't exist, so just try to create it, most times this 88 * works. We have to take special handling when the file does exist. To 89 * detect this, we use O_EXCL. For example when trying to create a 90 * file and a character device or fifo exists with the same name, we 91 * can accidently open the device by mistake (or block waiting to open) 92 * If we find that the open has failed, then figure spend the effort to 93 * figure out why. This strategy was found to have better average 94 * performance in common use than checking the file (and the path) 95 * first with lstat. 96 */ 97 file_mode = arcn->sb.st_mode & FILEBITS; 98 if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 99 file_mode)) >= 0) 100 return(fd); 101 102 /* 103 * the file seems to exist. First we try to get rid of it (found to be 104 * the second most common failure when traced). If this fails, only 105 * then we go to the expense to check and create the path to the file 106 */ 107 if (unlnk_exist(arcn->name, arcn->type) != 0) 108 return(-1); 109 110 for (;;) { 111 /* 112 * try to open it again, if this fails, check all the nodes in 113 * the path and give it a final try. if chk_path() finds that 114 * it cannot fix anything, we will skip the last attempt 115 */ 116 if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 117 file_mode)) >= 0) 118 break; 119 oerrno = errno; 120 if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 121 syswarn(1, oerrno, "Unable to create %s", arcn->name); 122 return(-1); 123 } 124 } 125 return(fd); 126 } 127 128 /* 129 * file_close() 130 * Close file descriptor to a file just created by pax. Sets modes, 131 * ownership and times as required. 132 * Return: 133 * 0 for success, -1 for failure 134 */ 135 136 void 137 file_close(ARCHD *arcn, int fd) 138 { 139 int res = 0; 140 141 if (fd < 0) 142 return; 143 if (close(fd) < 0) 144 syswarn(0, errno, "Unable to close file descriptor on %s", 145 arcn->name); 146 147 /* 148 * set owner/groups first as this may strip off mode bits we want 149 * then set file permission modes. Then set file access and 150 * modification times. 151 */ 152 if (pids) 153 res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 154 155 /* 156 * IMPORTANT SECURITY NOTE: 157 * if not preserving mode or we cannot set uid/gid, then PROHIBIT 158 * set uid/gid bits 159 */ 160 if (!pmode || res) 161 arcn->sb.st_mode &= ~(SETBITS); 162 if (pmode) 163 set_pmode(arcn->name, arcn->sb.st_mode); 164 if (patime || pmtime) 165 set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 166 } 167 168 /* 169 * lnk_creat() 170 * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 171 * must exist; 172 * Return: 173 * 0 if ok, -1 otherwise 174 */ 175 176 int 177 lnk_creat(ARCHD *arcn) 178 { 179 struct stat sb; 180 181 /* 182 * we may be running as root, so we have to be sure that link target 183 * is not a directory, so we lstat and check 184 */ 185 if (lstat(arcn->ln_name, &sb) < 0) { 186 syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 187 arcn->name); 188 return(-1); 189 } 190 191 if (S_ISDIR(sb.st_mode)) { 192 paxwarn(1, "A hard link to the directory %s is not allowed", 193 arcn->ln_name); 194 return(-1); 195 } 196 197 return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 198 } 199 200 /* 201 * cross_lnk() 202 * Create a hard link to arcn->org_name from arcn->name. Only used in copy 203 * with the -l flag. No warning or error if this does not succeed (we will 204 * then just create the file) 205 * Return: 206 * 1 if copy() should try to create this file node 207 * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 208 */ 209 210 int 211 cross_lnk(ARCHD *arcn) 212 { 213 /* 214 * try to make a link to original file (-l flag in copy mode). make sure 215 * we do not try to link to directories in case we are running as root 216 * (and it might succeed). 217 */ 218 if (arcn->type == PAX_DIR) 219 return(1); 220 return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 221 } 222 223 /* 224 * chk_same() 225 * In copy mode if we are not trying to make hard links between the src 226 * and destinations, make sure we are not going to overwrite ourselves by 227 * accident. This slows things down a little, but we have to protect all 228 * those people who make typing errors. 229 * Return: 230 * 1 the target does not exist, go ahead and copy 231 * 0 skip it file exists (-k) or may be the same as source file 232 */ 233 234 int 235 chk_same(ARCHD *arcn) 236 { 237 struct stat sb; 238 239 /* 240 * if file does not exist, return. if file exists and -k, skip it 241 * quietly 242 */ 243 if (lstat(arcn->name, &sb) < 0) 244 return(1); 245 if (kflag) 246 return(0); 247 248 /* 249 * better make sure the user does not have src == dest by mistake 250 */ 251 if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 252 paxwarn(1, "Unable to copy %s, file would overwrite itself", 253 arcn->name); 254 return(0); 255 } 256 return(1); 257 } 258 259 /* 260 * mk_link() 261 * try to make a hard link between two files. if ign set, we do not 262 * complain. 263 * Return: 264 * 0 if successful (or we are done with this file but no error, such as 265 * finding the from file exists and the user has set -k). 266 * 1 when ign was set to indicates we could not make the link but we 267 * should try to copy/extract the file as that might work (and is an 268 * allowed option). -1 an error occurred. 269 */ 270 271 static int 272 mk_link(char *to, struct stat *to_sb, char *from, 273 int ign) 274 { 275 struct stat sb; 276 int oerrno; 277 278 /* 279 * if from file exists, it has to be unlinked to make the link. If the 280 * file exists and -k is set, skip it quietly 281 */ 282 if (lstat(from, &sb) == 0) { 283 if (kflag) 284 return(0); 285 286 /* 287 * make sure it is not the same file, protect the user 288 */ 289 if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 290 paxwarn(1, "Unable to link file %s to itself", to); 291 return(-1);; 292 } 293 294 /* 295 * try to get rid of the file, based on the type 296 */ 297 if (S_ISDIR(sb.st_mode)) { 298 if (rmdir(from) < 0) { 299 syswarn(1, errno, "Unable to remove %s", from); 300 return(-1); 301 } 302 } else if (unlink(from) < 0) { 303 if (!ign) { 304 syswarn(1, errno, "Unable to remove %s", from); 305 return(-1); 306 } 307 return(1); 308 } 309 } 310 311 /* 312 * from file is gone (or did not exist), try to make the hard link. 313 * if it fails, check the path and try it again (if chk_path() says to 314 * try again) 315 */ 316 for (;;) { 317 if (link(to, from) == 0) 318 break; 319 oerrno = errno; 320 if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 321 continue; 322 if (!ign) { 323 syswarn(1, oerrno, "Could not link to %s from %s", to, 324 from); 325 return(-1); 326 } 327 return(1); 328 } 329 330 /* 331 * all right the link was made 332 */ 333 return(0); 334 } 335 336 /* 337 * node_creat() 338 * create an entry in the filesystem (other than a file or hard link). 339 * If successful, sets uid/gid modes and times as required. 340 * Return: 341 * 0 if ok, -1 otherwise 342 */ 343 344 int 345 node_creat(ARCHD *arcn) 346 { 347 int res; 348 int ign = 0; 349 int oerrno; 350 int pass = 0; 351 mode_t file_mode; 352 struct stat sb; 353 354 /* 355 * create node based on type, if that fails try to unlink the node and 356 * try again. finally check the path and try again. As noted in the 357 * file and link creation routines, this method seems to exhibit the 358 * best performance in general use workloads. 359 */ 360 file_mode = arcn->sb.st_mode & FILEBITS; 361 362 for (;;) { 363 switch(arcn->type) { 364 case PAX_DIR: 365 res = mkdir(arcn->name, file_mode); 366 if (ign) 367 res = 0; 368 break; 369 case PAX_CHR: 370 file_mode |= S_IFCHR; 371 res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 372 break; 373 case PAX_BLK: 374 file_mode |= S_IFBLK; 375 res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 376 break; 377 case PAX_FIF: 378 res = mkfifo(arcn->name, file_mode); 379 break; 380 case PAX_SCK: 381 /* 382 * Skip sockets, operation has no meaning under BSD 383 */ 384 paxwarn(0, 385 "%s skipped. Sockets cannot be copied or extracted", 386 arcn->name); 387 return(-1); 388 case PAX_SLK: 389 res = symlink(arcn->ln_name, arcn->name); 390 break; 391 case PAX_CTG: 392 case PAX_HLK: 393 case PAX_HRG: 394 case PAX_REG: 395 default: 396 /* 397 * we should never get here 398 */ 399 paxwarn(0, "%s has an unknown file type, skipping", 400 arcn->name); 401 return(-1); 402 } 403 404 /* 405 * if we were able to create the node break out of the loop, 406 * otherwise try to unlink the node and try again. if that 407 * fails check the full path and try a final time. 408 */ 409 if (res == 0) 410 break; 411 412 /* 413 * we failed to make the node 414 */ 415 oerrno = errno; 416 if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 417 return(-1); 418 419 if (++pass <= 1) 420 continue; 421 422 if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 423 syswarn(1, oerrno, "Could not create: %s", arcn->name); 424 return(-1); 425 } 426 } 427 428 /* 429 * we were able to create the node. set uid/gid, modes and times 430 */ 431 if (pids) 432 res = ((arcn->type == PAX_SLK) ? 433 set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) : 434 set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid)); 435 else 436 res = 0; 437 438 /* 439 * symlinks are done now. 440 */ 441 if (arcn->type == PAX_SLK) 442 return(0); 443 444 /* 445 * IMPORTANT SECURITY NOTE: 446 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 447 * set uid/gid bits 448 */ 449 if (!pmode || res) 450 arcn->sb.st_mode &= ~(SETBITS); 451 if (pmode) 452 set_pmode(arcn->name, arcn->sb.st_mode); 453 454 if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 455 /* 456 * Dirs must be processed again at end of extract to set times 457 * and modes to agree with those stored in the archive. However 458 * to allow extract to continue, we may have to also set owner 459 * rights. This allows nodes in the archive that are children 460 * of this directory to be extracted without failure. Both time 461 * and modes will be fixed after the entire archive is read and 462 * before pax exits. 463 */ 464 if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 465 if (lstat(arcn->name, &sb) < 0) { 466 syswarn(0, errno,"Could not access %s (stat)", 467 arcn->name); 468 set_pmode(arcn->name,file_mode | S_IRWXU); 469 } else { 470 /* 471 * We have to add rights to the dir, so we make 472 * sure to restore the mode. The mode must be 473 * restored AS CREATED and not as stored if 474 * pmode is not set. 475 */ 476 set_pmode(arcn->name, 477 ((sb.st_mode & FILEBITS) | S_IRWXU)); 478 if (!pmode) 479 arcn->sb.st_mode = sb.st_mode; 480 } 481 482 /* 483 * we have to force the mode to what was set here, 484 * since we changed it from the default as created. 485 */ 486 add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 487 } else if (pmode || patime || pmtime) 488 add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 489 } 490 491 if (patime || pmtime) 492 set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 493 return(0); 494 } 495 496 /* 497 * unlnk_exist() 498 * Remove node from filesystem with the specified name. We pass the type 499 * of the node that is going to replace it. When we try to create a 500 * directory and find that it already exists, we allow processing to 501 * continue as proper modes etc will always be set for it later on. 502 * Return: 503 * 0 is ok to proceed, no file with the specified name exists 504 * -1 we were unable to remove the node, or we should not remove it (-k) 505 * 1 we found a directory and we were going to create a directory. 506 */ 507 508 int 509 unlnk_exist(char *name, int type) 510 { 511 struct stat sb; 512 513 /* 514 * the file does not exist, or -k we are done 515 */ 516 if (lstat(name, &sb) < 0) 517 return(0); 518 if (kflag) 519 return(-1); 520 521 if (S_ISDIR(sb.st_mode)) { 522 /* 523 * try to remove a directory, if it fails and we were going to 524 * create a directory anyway, tell the caller (return a 1) 525 */ 526 if (rmdir(name) < 0) { 527 if (type == PAX_DIR) 528 return(1); 529 syswarn(1,errno,"Unable to remove directory %s", name); 530 return(-1); 531 } 532 return(0); 533 } 534 535 /* 536 * try to get rid of all non-directory type nodes 537 */ 538 if (unlink(name) < 0) { 539 syswarn(1, errno, "Could not unlink %s", name); 540 return(-1); 541 } 542 return(0); 543 } 544 545 /* 546 * chk_path() 547 * We were trying to create some kind of node in the filesystem and it 548 * failed. chk_path() makes sure the path up to the node exists and is 549 * writeable. When we have to create a directory that is missing along the 550 * path somewhere, the directory we create will be set to the same 551 * uid/gid as the file has (when uid and gid are being preserved). 552 * NOTE: this routine is a real performance loss. It is only used as a 553 * last resort when trying to create entries in the filesystem. 554 * Return: 555 * -1 when it could find nothing it is allowed to fix. 556 * 0 otherwise 557 */ 558 559 int 560 chk_path( char *name, uid_t st_uid, gid_t st_gid) 561 { 562 char *spt = name; 563 struct stat sb; 564 int retval = -1; 565 566 /* 567 * watch out for paths with nodes stored directly in / (e.g. /bozo) 568 */ 569 if (*spt == '/') 570 ++spt; 571 572 for(;;) { 573 /* 574 * work foward from the first / and check each part of the path 575 */ 576 spt = strchr(spt, '/'); 577 if (spt == NULL) 578 break; 579 *spt = '\0'; 580 581 /* 582 * if it exists we assume it is a directory, it is not within 583 * the spec (at least it seems to read that way) to alter the 584 * filesystem for nodes NOT EXPLICITLY stored on the archive. 585 * If that assumption is changed, you would test the node here 586 * and figure out how to get rid of it (probably like some 587 * recursive unlink()) or fix up the directory permissions if 588 * required (do an access()). 589 */ 590 if (lstat(name, &sb) == 0) { 591 *(spt++) = '/'; 592 continue; 593 } 594 595 /* 596 * the path fails at this point, see if we can create the 597 * needed directory and continue on 598 */ 599 if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 600 *spt = '/'; 601 retval = -1; 602 break; 603 } 604 605 /* 606 * we were able to create the directory. We will tell the 607 * caller that we found something to fix, and it is ok to try 608 * and create the node again. 609 */ 610 retval = 0; 611 if (pids) 612 (void)set_ids(name, st_uid, st_gid); 613 614 /* 615 * make sure the user doen't have some strange umask that 616 * causes this newly created directory to be unusable. We fix 617 * the modes and restore them back to the creation default at 618 * the end of pax 619 */ 620 if ((access(name, R_OK | W_OK | X_OK) < 0) && 621 (lstat(name, &sb) == 0)) { 622 set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 623 add_dir(name, spt - name, &sb, 1); 624 } 625 *(spt++) = '/'; 626 continue; 627 } 628 return(retval); 629 } 630 631 /* 632 * set_ftime() 633 * Set the access time and modification time for a named file. If frc is 634 * non-zero we force these times to be set even if the user did not 635 * request access and/or modification time preservation (this is also 636 * used by -t to reset access times). 637 * When ign is zero, only those times the user has asked for are set, the 638 * other ones are left alone. We do not assume the un-documented feature 639 * of many utimes() implementations that consider a 0 time value as a do 640 * not set request. 641 */ 642 643 void 644 set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 645 { 646 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 647 struct stat sb; 648 649 tv[0].tv_sec = atime; 650 tv[1].tv_sec = mtime; 651 if (!frc && (!patime || !pmtime)) { 652 /* 653 * if we are not forcing, only set those times the user wants 654 * set. We get the current values of the times if we need them. 655 */ 656 if (lstat(fnm, &sb) == 0) { 657 if (!patime) 658 tv[0].tv_sec = sb.st_atime; 659 if (!pmtime) 660 tv[1].tv_sec = sb.st_mtime; 661 } else 662 syswarn(0,errno,"Unable to obtain file stats %s", fnm); 663 } 664 665 /* 666 * set the times 667 */ 668 if (utimes(fnm, tv) < 0) 669 syswarn(1, errno, "Access/modification time set failed on: %s", 670 fnm); 671 return; 672 } 673 674 /* 675 * set_ids() 676 * set the uid and gid of a filesystem node 677 * Return: 678 * 0 when set, -1 on failure 679 */ 680 681 int 682 set_ids(char *fnm, uid_t uid, gid_t gid) 683 { 684 if (chown(fnm, uid, gid) < 0) { 685 /* 686 * ignore EPERM unless in verbose mode or being run by root. 687 * if running as pax, POSIX requires a warning. 688 */ 689 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 690 geteuid() == 0) 691 syswarn(1, errno, "Unable to set file uid/gid of %s", 692 fnm); 693 return(-1); 694 } 695 return(0); 696 } 697 698 /* 699 * set_lids() 700 * set the uid and gid of a filesystem node 701 * Return: 702 * 0 when set, -1 on failure 703 */ 704 705 int 706 set_lids(char *fnm, uid_t uid, gid_t gid) 707 { 708 if (lchown(fnm, uid, gid) < 0) { 709 /* 710 * ignore EPERM unless in verbose mode or being run by root. 711 * if running as pax, POSIX requires a warning. 712 */ 713 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 714 geteuid() == 0) 715 syswarn(1, errno, "Unable to set file uid/gid of %s", 716 fnm); 717 return(-1); 718 } 719 return(0); 720 } 721 722 /* 723 * set_pmode() 724 * Set file access mode 725 */ 726 727 void 728 set_pmode(char *fnm, mode_t mode) 729 { 730 mode &= ABITS; 731 if (chmod(fnm, mode) < 0) 732 syswarn(1, errno, "Could not set permissions on %s", fnm); 733 return; 734 } 735 736 /* 737 * file_write() 738 * Write/copy a file (during copy or archive extract). This routine knows 739 * how to copy files with lseek holes in it. (Which are read as file 740 * blocks containing all 0's but do not have any file blocks associated 741 * with the data). Typical examples of these are files created by dbm 742 * variants (.pag files). While the file size of these files are huge, the 743 * actual storage is quite small (the files are sparse). The problem is 744 * the holes read as all zeros so are probably stored on the archive that 745 * way (there is no way to determine if the file block is really a hole, 746 * we only know that a file block of all zero's can be a hole). 747 * At this writing, no major archive format knows how to archive files 748 * with holes. However, on extraction (or during copy, -rw) we have to 749 * deal with these files. Without detecting the holes, the files can 750 * consume a lot of file space if just written to disk. This replacement 751 * for write when passed the basic allocation size of a filesystem block, 752 * uses lseek whenever it detects the input data is all 0 within that 753 * file block. In more detail, the strategy is as follows: 754 * While the input is all zero keep doing an lseek. Keep track of when we 755 * pass over file block boundries. Only write when we hit a non zero 756 * input. once we have written a file block, we continue to write it to 757 * the end (we stop looking at the input). When we reach the start of the 758 * next file block, start checking for zero blocks again. Working on file 759 * block boundries significantly reduces the overhead when copying files 760 * that are NOT very sparse. This overhead (when compared to a write) is 761 * almost below the measurement resolution on many systems. Without it, 762 * files with holes cannot be safely copied. It does has a side effect as 763 * it can put holes into files that did not have them before, but that is 764 * not a problem since the file contents are unchanged (in fact it saves 765 * file space). (Except on paging files for diskless clients. But since we 766 * cannot determine one of those file from here, we ignore them). If this 767 * ever ends up on a system where CTG files are supported and the holes 768 * are not desired, just do a conditional test in those routines that 769 * call file_write() and have it call write() instead. BEFORE CLOSING THE 770 * FILE, make sure to call file_flush() when the last write finishes with 771 * an empty block. A lot of filesystems will not create an lseek hole at 772 * the end. In this case we drop a single 0 at the end to force the 773 * trailing 0's in the file. 774 * ---Parameters--- 775 * rem: how many bytes left in this filesystem block 776 * isempt: have we written to the file block yet (is it empty) 777 * sz: basic file block allocation size 778 * cnt: number of bytes on this write 779 * str: buffer to write 780 * Return: 781 * number of bytes written, -1 on write (or lseek) error. 782 */ 783 784 int 785 file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 786 char *name) 787 { 788 char *pt; 789 char *end; 790 int wcnt; 791 char *st = str; 792 793 /* 794 * while we have data to process 795 */ 796 while (cnt) { 797 if (!*rem) { 798 /* 799 * We are now at the start of filesystem block again 800 * (or what we think one is...). start looking for 801 * empty blocks again 802 */ 803 *isempt = 1; 804 *rem = sz; 805 } 806 807 /* 808 * only examine up to the end of the current file block or 809 * remaining characters to write, whatever is smaller 810 */ 811 wcnt = MIN(cnt, *rem); 812 cnt -= wcnt; 813 *rem -= wcnt; 814 if (*isempt) { 815 /* 816 * have not written to this block yet, so we keep 817 * looking for zero's 818 */ 819 pt = st; 820 end = st + wcnt; 821 822 /* 823 * look for a zero filled buffer 824 */ 825 while ((pt < end) && (*pt == '\0')) 826 ++pt; 827 828 if (pt == end) { 829 /* 830 * skip, buf is empty so far 831 */ 832 if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 833 syswarn(1,errno,"File seek on %s", 834 name); 835 return(-1); 836 } 837 st = pt; 838 continue; 839 } 840 /* 841 * drat, the buf is not zero filled 842 */ 843 *isempt = 0; 844 } 845 846 /* 847 * have non-zero data in this filesystem block, have to write 848 */ 849 if (write(fd, st, wcnt) != wcnt) { 850 syswarn(1, errno, "Failed write to file %s", name); 851 return(-1); 852 } 853 st += wcnt; 854 } 855 return(st - str); 856 } 857 858 /* 859 * file_flush() 860 * when the last file block in a file is zero, many filesystems will not 861 * let us create a hole at the end. To get the last block with zeros, we 862 * write the last BYTE with a zero (back up one byte and write a zero). 863 */ 864 865 void 866 file_flush(int fd, char *fname, int isempt) 867 { 868 static char blnk[] = "\0"; 869 870 /* 871 * silly test, but make sure we are only called when the last block is 872 * filled with all zeros. 873 */ 874 if (!isempt) 875 return; 876 877 /* 878 * move back one byte and write a zero 879 */ 880 if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 881 syswarn(1, errno, "Failed seek on file %s", fname); 882 return; 883 } 884 885 if (write(fd, blnk, 1) < 0) 886 syswarn(1, errno, "Failed write to file %s", fname); 887 return; 888 } 889 890 /* 891 * rdfile_close() 892 * close a file we have beed reading (to copy or archive). If we have to 893 * reset access time (tflag) do so (the times are stored in arcn). 894 */ 895 896 void 897 rdfile_close(ARCHD *arcn, int *fd) 898 { 899 /* 900 * make sure the file is open 901 */ 902 if (*fd < 0) 903 return; 904 905 (void)close(*fd); 906 *fd = -1; 907 if (!tflag) 908 return; 909 910 /* 911 * user wants last access time reset 912 */ 913 set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 914 return; 915 } 916 917 /* 918 * set_crc() 919 * read a file to calculate its crc. This is a real drag. Archive formats 920 * that have this, end up reading the file twice (we have to write the 921 * header WITH the crc before writing the file contents. Oh well... 922 * Return: 923 * 0 if was able to calculate the crc, -1 otherwise 924 */ 925 926 int 927 set_crc(ARCHD *arcn, int fd) 928 { 929 int i; 930 int res; 931 off_t cpcnt = 0L; 932 u_long size; 933 unsigned long crc = 0L; 934 char tbuf[FILEBLK]; 935 struct stat sb; 936 937 if (fd < 0) { 938 /* 939 * hmm, no fd, should never happen. well no crc then. 940 */ 941 arcn->crc = 0L; 942 return(0); 943 } 944 945 if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 946 size = (u_long)sizeof(tbuf); 947 948 /* 949 * read all the bytes we think that there are in the file. If the user 950 * is trying to archive an active file, forget this file. 951 */ 952 for(;;) { 953 if ((res = read(fd, tbuf, size)) <= 0) 954 break; 955 cpcnt += res; 956 for (i = 0; i < res; ++i) 957 crc += (tbuf[i] & 0xff); 958 } 959 960 /* 961 * safety check. we want to avoid archiving files that are active as 962 * they can create inconsistant archive copies. 963 */ 964 if (cpcnt != arcn->sb.st_size) 965 paxwarn(1, "File changed size %s", arcn->org_name); 966 else if (fstat(fd, &sb) < 0) 967 syswarn(1, errno, "Failed stat on %s", arcn->org_name); 968 else if (arcn->sb.st_mtime != sb.st_mtime) 969 paxwarn(1, "File %s was modified during read", arcn->org_name); 970 else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 971 syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 972 else { 973 arcn->crc = crc; 974 return(0); 975 } 976 return(-1); 977 } 978