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