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