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