file_subs.c revision 76017
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1992 Keith Muller. 31556Srgrimes * Copyright (c) 1992, 1993 41556Srgrimes * The Regents of the University of California. All rights reserved. 51556Srgrimes * 61556Srgrimes * This code is derived from software contributed to Berkeley by 71556Srgrimes * Keith Muller of the University of California, San Diego. 81556Srgrimes * 91556Srgrimes * Redistribution and use in source and binary forms, with or without 101556Srgrimes * modification, are permitted provided that the following conditions 111556Srgrimes * are met: 121556Srgrimes * 1. Redistributions of source code must retain the above copyright 131556Srgrimes * notice, this list of conditions and the following disclaimer. 141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151556Srgrimes * notice, this list of conditions and the following disclaimer in the 161556Srgrimes * documentation and/or other materials provided with the distribution. 171556Srgrimes * 3. All advertising materials mentioning features or use of this software 181556Srgrimes * must display the following acknowledgement: 191556Srgrimes * This product includes software developed by the University of 201556Srgrimes * California, Berkeley and its contributors. 211556Srgrimes * 4. Neither the name of the University nor the names of its contributors 221556Srgrimes * may be used to endorse or promote products derived from this software 231556Srgrimes * without specific prior written permission. 241556Srgrimes * 251556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 261556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 271556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 281556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 291556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 301556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 311556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 321556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 331556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 341556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 351556Srgrimes * SUCH DAMAGE. 361556Srgrimes */ 371556Srgrimes 381556Srgrimes#ifndef lint 3936049Scharnier#if 0 4036049Scharnierstatic char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 4136049Scharnier#endif 4236049Scharnierstatic const char rcsid[] = 4350471Speter "$FreeBSD: head/bin/pax/file_subs.c 76017 2001-04-26 08:37:00Z kris $"; 441556Srgrimes#endif /* not lint */ 451556Srgrimes 461556Srgrimes#include <sys/types.h> 471556Srgrimes#include <sys/time.h> 481556Srgrimes#include <sys/stat.h> 491556Srgrimes#include <unistd.h> 501556Srgrimes#include <fcntl.h> 511556Srgrimes#include <string.h> 521556Srgrimes#include <stdio.h> 531556Srgrimes#include <errno.h> 541556Srgrimes#include <sys/uio.h> 551556Srgrimes#include <stdlib.h> 561556Srgrimes#include "pax.h" 571556Srgrimes#include "extern.h" 581556Srgrimes 591556Srgrimesstatic int 601556Srgrimesmk_link __P((register char *,register struct stat *,register char *, int)); 611556Srgrimes 621556Srgrimes/* 631556Srgrimes * routines that deal with file operations such as: creating, removing; 641556Srgrimes * and setting access modes, uid/gid and times of files 651556Srgrimes */ 661556Srgrimes 671556Srgrimes#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 681556Srgrimes#define SETBITS (S_ISUID | S_ISGID) 691556Srgrimes#define ABITS (FILEBITS | SETBITS) 701556Srgrimes 711556Srgrimes/* 721556Srgrimes * file_creat() 731556Srgrimes * Create and open a file. 741556Srgrimes * Return: 751556Srgrimes * file descriptor or -1 for failure 761556Srgrimes */ 771556Srgrimes 7876017Skris#ifdef __STDC__ 791556Srgrimesint 801556Srgrimesfile_creat(register ARCHD *arcn) 811556Srgrimes#else 821556Srgrimesint 831556Srgrimesfile_creat(arcn) 841556Srgrimes register ARCHD *arcn; 851556Srgrimes#endif 861556Srgrimes{ 871556Srgrimes int fd = -1; 881556Srgrimes mode_t file_mode; 891556Srgrimes int oerrno; 901556Srgrimes 911556Srgrimes /* 921556Srgrimes * assume file doesn't exist, so just try to create it, most times this 931556Srgrimes * works. We have to take special handling when the file does exist. To 941556Srgrimes * detect this, we use O_EXCL. For example when trying to create a 951556Srgrimes * file and a character device or fifo exists with the same name, we 961556Srgrimes * can accidently open the device by mistake (or block waiting to open) 9746684Skris * If we find that the open has failed, then figure spend the effort to 981556Srgrimes * figure out why. This strategy was found to have better average 991556Srgrimes * performance in common use than checking the file (and the path) 1001556Srgrimes * first with lstat. 1011556Srgrimes */ 1021556Srgrimes file_mode = arcn->sb.st_mode & FILEBITS; 1031556Srgrimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 1041556Srgrimes file_mode)) >= 0) 1051556Srgrimes return(fd); 1061556Srgrimes 1071556Srgrimes /* 1081556Srgrimes * the file seems to exist. First we try to get rid of it (found to be 1091556Srgrimes * the second most common failure when traced). If this fails, only 1101556Srgrimes * then we go to the expense to check and create the path to the file 1111556Srgrimes */ 1121556Srgrimes if (unlnk_exist(arcn->name, arcn->type) != 0) 1131556Srgrimes return(-1); 1141556Srgrimes 1151556Srgrimes for (;;) { 1161556Srgrimes /* 1171556Srgrimes * try to open it again, if this fails, check all the nodes in 1181556Srgrimes * the path and give it a final try. if chk_path() finds that 1191556Srgrimes * it cannot fix anything, we will skip the last attempt 1201556Srgrimes */ 1211556Srgrimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 1221556Srgrimes file_mode)) >= 0) 1231556Srgrimes break; 1241556Srgrimes oerrno = errno; 1251556Srgrimes if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 12676017Skris syswarn(1, oerrno, "Unable to create %s", arcn->name); 1271556Srgrimes return(-1); 1281556Srgrimes } 1291556Srgrimes } 1301556Srgrimes return(fd); 1311556Srgrimes} 1321556Srgrimes 1331556Srgrimes/* 1341556Srgrimes * file_close() 1351556Srgrimes * Close file descriptor to a file just created by pax. Sets modes, 1361556Srgrimes * ownership and times as required. 1371556Srgrimes * Return: 1381556Srgrimes * 0 for success, -1 for failure 1391556Srgrimes */ 1401556Srgrimes 14176017Skris#ifdef __STDC__ 1421556Srgrimesvoid 1431556Srgrimesfile_close(register ARCHD *arcn, int fd) 1441556Srgrimes#else 1451556Srgrimesvoid 1461556Srgrimesfile_close(arcn, fd) 1471556Srgrimes register ARCHD *arcn; 1481556Srgrimes int fd; 1491556Srgrimes#endif 1501556Srgrimes{ 1511556Srgrimes int res = 0; 1521556Srgrimes 1531556Srgrimes if (fd < 0) 1541556Srgrimes return; 1551556Srgrimes if (close(fd) < 0) 15676017Skris syswarn(0, errno, "Unable to close file descriptor on %s", 1571556Srgrimes arcn->name); 1581556Srgrimes 1591556Srgrimes /* 1601556Srgrimes * set owner/groups first as this may strip off mode bits we want 1611556Srgrimes * then set file permission modes. Then set file access and 1628855Srgrimes * modification times. 1631556Srgrimes */ 1641556Srgrimes if (pids) 1651556Srgrimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 1661556Srgrimes 1671556Srgrimes /* 1681556Srgrimes * IMPORTANT SECURITY NOTE: 1691556Srgrimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT 1701556Srgrimes * set uid/gid bits 1711556Srgrimes */ 1721556Srgrimes if (!pmode || res) 1731556Srgrimes arcn->sb.st_mode &= ~(SETBITS); 1741556Srgrimes if (pmode) 1751556Srgrimes set_pmode(arcn->name, arcn->sb.st_mode); 1761556Srgrimes if (patime || pmtime) 1771556Srgrimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 1781556Srgrimes} 1791556Srgrimes 1801556Srgrimes/* 1811556Srgrimes * lnk_creat() 1821556Srgrimes * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 1838855Srgrimes * must exist; 1841556Srgrimes * Return: 1851556Srgrimes * 0 if ok, -1 otherwise 1861556Srgrimes */ 1871556Srgrimes 18876017Skris#ifdef __STDC__ 1891556Srgrimesint 1901556Srgrimeslnk_creat(register ARCHD *arcn) 1911556Srgrimes#else 1921556Srgrimesint 1931556Srgrimeslnk_creat(arcn) 1941556Srgrimes register ARCHD *arcn; 1951556Srgrimes#endif 1961556Srgrimes{ 1971556Srgrimes struct stat sb; 1981556Srgrimes 1991556Srgrimes /* 2001556Srgrimes * we may be running as root, so we have to be sure that link target 2011556Srgrimes * is not a directory, so we lstat and check 2021556Srgrimes */ 2031556Srgrimes if (lstat(arcn->ln_name, &sb) < 0) { 20476017Skris syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 2051556Srgrimes arcn->name); 2061556Srgrimes return(-1); 2071556Srgrimes } 2081556Srgrimes 2091556Srgrimes if (S_ISDIR(sb.st_mode)) { 21076017Skris paxwarn(1, "A hard link to the directory %s is not allowed", 2111556Srgrimes arcn->ln_name); 2121556Srgrimes return(-1); 2131556Srgrimes } 2141556Srgrimes 2151556Srgrimes return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 2161556Srgrimes} 2171556Srgrimes 2181556Srgrimes/* 2191556Srgrimes * cross_lnk() 2201556Srgrimes * Create a hard link to arcn->org_name from arcn->name. Only used in copy 22176017Skris * with the -l flag. No warning or error if this does not succeed (we will 2221556Srgrimes * then just create the file) 2231556Srgrimes * Return: 2241556Srgrimes * 1 if copy() should try to create this file node 2251556Srgrimes * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 2261556Srgrimes */ 2271556Srgrimes 22876017Skris#ifdef __STDC__ 2291556Srgrimesint 2301556Srgrimescross_lnk(register ARCHD *arcn) 2311556Srgrimes#else 2321556Srgrimesint 2331556Srgrimescross_lnk(arcn) 2341556Srgrimes register ARCHD *arcn; 2351556Srgrimes#endif 2361556Srgrimes{ 2371556Srgrimes /* 23846684Skris * try to make a link to original file (-l flag in copy mode). make sure 2391556Srgrimes * we do not try to link to directories in case we are running as root 2401556Srgrimes * (and it might succeed). 2411556Srgrimes */ 2421556Srgrimes if (arcn->type == PAX_DIR) 2431556Srgrimes return(1); 2441556Srgrimes return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 2451556Srgrimes} 2461556Srgrimes 2471556Srgrimes/* 2481556Srgrimes * chk_same() 2491556Srgrimes * In copy mode if we are not trying to make hard links between the src 2501556Srgrimes * and destinations, make sure we are not going to overwrite ourselves by 2511556Srgrimes * accident. This slows things down a little, but we have to protect all 2521556Srgrimes * those people who make typing errors. 2531556Srgrimes * Return: 2541556Srgrimes * 1 the target does not exist, go ahead and copy 2551556Srgrimes * 0 skip it file exists (-k) or may be the same as source file 2561556Srgrimes */ 2571556Srgrimes 25876017Skris#ifdef __STDC__ 2591556Srgrimesint 2601556Srgrimeschk_same(register ARCHD *arcn) 2611556Srgrimes#else 2621556Srgrimesint 2631556Srgrimeschk_same(arcn) 2641556Srgrimes register ARCHD *arcn; 2651556Srgrimes#endif 2661556Srgrimes{ 2671556Srgrimes struct stat sb; 2681556Srgrimes 2698855Srgrimes /* 2701556Srgrimes * if file does not exist, return. if file exists and -k, skip it 2711556Srgrimes * quietly 2721556Srgrimes */ 2731556Srgrimes if (lstat(arcn->name, &sb) < 0) 2741556Srgrimes return(1); 2751556Srgrimes if (kflag) 2761556Srgrimes return(0); 2771556Srgrimes 2781556Srgrimes /* 2791556Srgrimes * better make sure the user does not have src == dest by mistake 2801556Srgrimes */ 2811556Srgrimes if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 28276017Skris paxwarn(1, "Unable to copy %s, file would overwrite itself", 2831556Srgrimes arcn->name); 2841556Srgrimes return(0); 2851556Srgrimes } 2861556Srgrimes return(1); 2871556Srgrimes} 2881556Srgrimes 2891556Srgrimes/* 2901556Srgrimes * mk_link() 2911556Srgrimes * try to make a hard link between two files. if ign set, we do not 2921556Srgrimes * complain. 2931556Srgrimes * Return: 2941556Srgrimes * 0 if successful (or we are done with this file but no error, such as 2951556Srgrimes * finding the from file exists and the user has set -k). 2961556Srgrimes * 1 when ign was set to indicates we could not make the link but we 2971556Srgrimes * should try to copy/extract the file as that might work (and is an 2981556Srgrimes * allowed option). -1 an error occurred. 2991556Srgrimes */ 3001556Srgrimes 30176017Skris#ifdef __STDC__ 3021556Srgrimesstatic int 3031556Srgrimesmk_link(register char *to, register struct stat *to_sb, register char *from, 3041556Srgrimes int ign) 3051556Srgrimes#else 3061556Srgrimesstatic int 3071556Srgrimesmk_link(to, to_sb, from, ign) 3081556Srgrimes register char *to; 3091556Srgrimes register struct stat *to_sb; 3101556Srgrimes register char *from; 3111556Srgrimes int ign; 3121556Srgrimes#endif 3131556Srgrimes{ 3141556Srgrimes struct stat sb; 3151556Srgrimes int oerrno; 3161556Srgrimes 3171556Srgrimes /* 3181556Srgrimes * if from file exists, it has to be unlinked to make the link. If the 3191556Srgrimes * file exists and -k is set, skip it quietly 3201556Srgrimes */ 3211556Srgrimes if (lstat(from, &sb) == 0) { 3221556Srgrimes if (kflag) 3231556Srgrimes return(0); 3241556Srgrimes 3251556Srgrimes /* 3261556Srgrimes * make sure it is not the same file, protect the user 3271556Srgrimes */ 3281556Srgrimes if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 32976017Skris paxwarn(1, "Unable to link file %s to itself", to); 3301556Srgrimes return(-1);; 3311556Srgrimes } 3321556Srgrimes 3331556Srgrimes /* 3341556Srgrimes * try to get rid of the file, based on the type 3351556Srgrimes */ 3361556Srgrimes if (S_ISDIR(sb.st_mode)) { 3371556Srgrimes if (rmdir(from) < 0) { 33876017Skris syswarn(1, errno, "Unable to remove %s", from); 3391556Srgrimes return(-1); 3401556Srgrimes } 3411556Srgrimes } else if (unlink(from) < 0) { 3421556Srgrimes if (!ign) { 34376017Skris syswarn(1, errno, "Unable to remove %s", from); 3441556Srgrimes return(-1); 3451556Srgrimes } 3461556Srgrimes return(1); 3471556Srgrimes } 3481556Srgrimes } 3491556Srgrimes 3501556Srgrimes /* 3511556Srgrimes * from file is gone (or did not exist), try to make the hard link. 3521556Srgrimes * if it fails, check the path and try it again (if chk_path() says to 3531556Srgrimes * try again) 3541556Srgrimes */ 3551556Srgrimes for (;;) { 3561556Srgrimes if (link(to, from) == 0) 3571556Srgrimes break; 3581556Srgrimes oerrno = errno; 3591556Srgrimes if (chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 3601556Srgrimes continue; 3611556Srgrimes if (!ign) { 36276017Skris syswarn(1, oerrno, "Could not link to %s from %s", to, 3631556Srgrimes from); 3641556Srgrimes return(-1); 3651556Srgrimes } 3661556Srgrimes return(1); 3671556Srgrimes } 3681556Srgrimes 3691556Srgrimes /* 3701556Srgrimes * all right the link was made 3711556Srgrimes */ 3721556Srgrimes return(0); 3731556Srgrimes} 3741556Srgrimes 3751556Srgrimes/* 3761556Srgrimes * node_creat() 3771556Srgrimes * create an entry in the file system (other than a file or hard link). 3781556Srgrimes * If successful, sets uid/gid modes and times as required. 3791556Srgrimes * Return: 3801556Srgrimes * 0 if ok, -1 otherwise 3811556Srgrimes */ 3821556Srgrimes 38376017Skris#ifdef __STDC__ 3841556Srgrimesint 3851556Srgrimesnode_creat(register ARCHD *arcn) 3861556Srgrimes#else 3871556Srgrimesint 3881556Srgrimesnode_creat(arcn) 3891556Srgrimes register ARCHD *arcn; 3901556Srgrimes#endif 3911556Srgrimes{ 3921556Srgrimes register int res; 3931556Srgrimes register int ign = 0; 3941556Srgrimes register int oerrno; 3951556Srgrimes register int pass = 0; 3961556Srgrimes mode_t file_mode; 3971556Srgrimes struct stat sb; 3981556Srgrimes 3991556Srgrimes /* 4001556Srgrimes * create node based on type, if that fails try to unlink the node and 4011556Srgrimes * try again. finally check the path and try again. As noted in the 4021556Srgrimes * file and link creation routines, this method seems to exhibit the 4031556Srgrimes * best performance in general use workloads. 4041556Srgrimes */ 4051556Srgrimes file_mode = arcn->sb.st_mode & FILEBITS; 4061556Srgrimes 4071556Srgrimes for (;;) { 4081556Srgrimes switch(arcn->type) { 4091556Srgrimes case PAX_DIR: 4101556Srgrimes res = mkdir(arcn->name, file_mode); 4111556Srgrimes if (ign) 4121556Srgrimes res = 0; 4131556Srgrimes break; 4141556Srgrimes case PAX_CHR: 4151556Srgrimes file_mode |= S_IFCHR; 4161556Srgrimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 4171556Srgrimes break; 4181556Srgrimes case PAX_BLK: 4191556Srgrimes file_mode |= S_IFBLK; 4201556Srgrimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 4211556Srgrimes break; 4221556Srgrimes case PAX_FIF: 4231556Srgrimes res = mkfifo(arcn->name, file_mode); 4241556Srgrimes break; 4251556Srgrimes case PAX_SCK: 4261556Srgrimes /* 4271556Srgrimes * Skip sockets, operation has no meaning under BSD 4281556Srgrimes */ 42976017Skris paxwarn(0, 4301556Srgrimes "%s skipped. Sockets cannot be copied or extracted", 4311556Srgrimes arcn->name); 4321556Srgrimes return(-1); 4331556Srgrimes case PAX_SLK: 4341556Srgrimes if ((res = symlink(arcn->ln_name, arcn->name)) == 0) 4351556Srgrimes return(0); 4361556Srgrimes break; 4371556Srgrimes case PAX_CTG: 4381556Srgrimes case PAX_HLK: 4391556Srgrimes case PAX_HRG: 4401556Srgrimes case PAX_REG: 4411556Srgrimes default: 4421556Srgrimes /* 4431556Srgrimes * we should never get here 4441556Srgrimes */ 44576017Skris paxwarn(0, "%s has an unknown file type, skipping", 4461556Srgrimes arcn->name); 4471556Srgrimes return(-1); 4481556Srgrimes } 4491556Srgrimes 4501556Srgrimes /* 4511556Srgrimes * if we were able to create the node break out of the loop, 4521556Srgrimes * otherwise try to unlink the node and try again. if that 4531556Srgrimes * fails check the full path and try a final time. 4541556Srgrimes */ 4551556Srgrimes if (res == 0) 4561556Srgrimes break; 4571556Srgrimes 4581556Srgrimes /* 4591556Srgrimes * we failed to make the node 4601556Srgrimes */ 4611556Srgrimes oerrno = errno; 4621556Srgrimes if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 4631556Srgrimes return(-1); 4641556Srgrimes 4651556Srgrimes if (++pass <= 1) 4661556Srgrimes continue; 4671556Srgrimes 4681556Srgrimes if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 46976017Skris syswarn(1, oerrno, "Could not create: %s", arcn->name); 4701556Srgrimes return(-1); 4711556Srgrimes } 4721556Srgrimes } 4731556Srgrimes 4741556Srgrimes /* 4751556Srgrimes * we were able to create the node. set uid/gid, modes and times 4761556Srgrimes */ 4771556Srgrimes if (pids) 4781556Srgrimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 4791556Srgrimes else 4801556Srgrimes res = 0; 4811556Srgrimes 4821556Srgrimes /* 4831556Srgrimes * IMPORTANT SECURITY NOTE: 4841556Srgrimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4851556Srgrimes * set uid/gid bits 4861556Srgrimes */ 4871556Srgrimes if (!pmode || res) 4881556Srgrimes arcn->sb.st_mode &= ~(SETBITS); 4891556Srgrimes if (pmode) 4901556Srgrimes set_pmode(arcn->name, arcn->sb.st_mode); 4911556Srgrimes 4921556Srgrimes if (arcn->type == PAX_DIR) { 4931556Srgrimes /* 4941556Srgrimes * Dirs must be processed again at end of extract to set times 4951556Srgrimes * and modes to agree with those stored in the archive. However 4961556Srgrimes * to allow extract to continue, we may have to also set owner 4971556Srgrimes * rights. This allows nodes in the archive that are children 4981556Srgrimes * of this directory to be extracted without failure. Both time 4991556Srgrimes * and modes will be fixed after the entire archive is read and 5001556Srgrimes * before pax exits. 5011556Srgrimes */ 5021556Srgrimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 5031556Srgrimes if (lstat(arcn->name, &sb) < 0) { 50476017Skris syswarn(0, errno,"Could not access %s (stat)", 5051556Srgrimes arcn->name); 5061556Srgrimes set_pmode(arcn->name,file_mode | S_IRWXU); 5071556Srgrimes } else { 5081556Srgrimes /* 5091556Srgrimes * We have to add rights to the dir, so we make 5101556Srgrimes * sure to restore the mode. The mode must be 5111556Srgrimes * restored AS CREATED and not as stored if 5121556Srgrimes * pmode is not set. 5131556Srgrimes */ 5141556Srgrimes set_pmode(arcn->name, 5151556Srgrimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 5161556Srgrimes if (!pmode) 5171556Srgrimes arcn->sb.st_mode = sb.st_mode; 5181556Srgrimes } 5191556Srgrimes 5201556Srgrimes /* 5211556Srgrimes * we have to force the mode to what was set here, 5221556Srgrimes * since we changed it from the default as created. 5231556Srgrimes */ 5241556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 5251556Srgrimes } else if (pmode || patime || pmtime) 5261556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 5271556Srgrimes } 5281556Srgrimes 5291556Srgrimes if (patime || pmtime) 5301556Srgrimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 5311556Srgrimes return(0); 5321556Srgrimes} 5331556Srgrimes 5341556Srgrimes/* 5351556Srgrimes * unlnk_exist() 5361556Srgrimes * Remove node from file system with the specified name. We pass the type 5371556Srgrimes * of the node that is going to replace it. When we try to create a 5381556Srgrimes * directory and find that it already exists, we allow processing to 5391556Srgrimes * continue as proper modes etc will always be set for it later on. 5401556Srgrimes * Return: 5411556Srgrimes * 0 is ok to proceed, no file with the specified name exists 5421556Srgrimes * -1 we were unable to remove the node, or we should not remove it (-k) 5431556Srgrimes * 1 we found a directory and we were going to create a directory. 5441556Srgrimes */ 5451556Srgrimes 54676017Skris#ifdef __STDC__ 5471556Srgrimesint 5481556Srgrimesunlnk_exist(register char *name, register int type) 5491556Srgrimes#else 5501556Srgrimesint 5511556Srgrimesunlnk_exist(name, type) 5521556Srgrimes register char *name; 5531556Srgrimes register int type; 5541556Srgrimes#endif 5551556Srgrimes{ 5561556Srgrimes struct stat sb; 5571556Srgrimes 5581556Srgrimes /* 5591556Srgrimes * the file does not exist, or -k we are done 5601556Srgrimes */ 5611556Srgrimes if (lstat(name, &sb) < 0) 5621556Srgrimes return(0); 5631556Srgrimes if (kflag) 5641556Srgrimes return(-1); 5651556Srgrimes 5661556Srgrimes if (S_ISDIR(sb.st_mode)) { 5671556Srgrimes /* 5681556Srgrimes * try to remove a directory, if it fails and we were going to 5691556Srgrimes * create a directory anyway, tell the caller (return a 1) 5701556Srgrimes */ 5711556Srgrimes if (rmdir(name) < 0) { 5721556Srgrimes if (type == PAX_DIR) 5738855Srgrimes return(1); 57476017Skris syswarn(1,errno,"Unable to remove directory %s", name); 5751556Srgrimes return(-1); 5761556Srgrimes } 5771556Srgrimes return(0); 5781556Srgrimes } 5791556Srgrimes 5801556Srgrimes /* 5811556Srgrimes * try to get rid of all non-directory type nodes 5821556Srgrimes */ 5831556Srgrimes if (unlink(name) < 0) { 58476017Skris syswarn(1, errno, "Could not unlink %s", name); 5851556Srgrimes return(-1); 5861556Srgrimes } 5871556Srgrimes return(0); 5881556Srgrimes} 5891556Srgrimes 5901556Srgrimes/* 5911556Srgrimes * chk_path() 5921556Srgrimes * We were trying to create some kind of node in the file system and it 5931556Srgrimes * failed. chk_path() makes sure the path up to the node exists and is 5941556Srgrimes * writeable. When we have to create a directory that is missing along the 5951556Srgrimes * path somewhere, the directory we create will be set to the same 5961556Srgrimes * uid/gid as the file has (when uid and gid are being preserved). 5971556Srgrimes * NOTE: this routine is a real performance loss. It is only used as a 5981556Srgrimes * last resort when trying to create entries in the file system. 5991556Srgrimes * Return: 6001556Srgrimes * -1 when it could find nothing it is allowed to fix. 6011556Srgrimes * 0 otherwise 6021556Srgrimes */ 6031556Srgrimes 60476017Skris#ifdef __STDC__ 6051556Srgrimesint 6061556Srgrimeschk_path( register char *name, uid_t st_uid, gid_t st_gid) 6071556Srgrimes#else 6081556Srgrimesint 6091556Srgrimeschk_path(name, st_uid, st_gid) 6101556Srgrimes register char *name; 6111556Srgrimes uid_t st_uid; 6121556Srgrimes gid_t st_gid; 6131556Srgrimes#endif 6141556Srgrimes{ 6151556Srgrimes register char *spt = name; 6161556Srgrimes struct stat sb; 6171556Srgrimes int retval = -1; 6181556Srgrimes 6191556Srgrimes /* 6201556Srgrimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 6211556Srgrimes */ 6221556Srgrimes if (*spt == '/') 6231556Srgrimes ++spt; 6241556Srgrimes 6251556Srgrimes for(;;) { 6261556Srgrimes /* 6271556Srgrimes * work foward from the first / and check each part of the path 6281556Srgrimes */ 6291556Srgrimes spt = strchr(spt, '/'); 6301556Srgrimes if (spt == NULL) 6311556Srgrimes break; 6321556Srgrimes *spt = '\0'; 6331556Srgrimes 6341556Srgrimes /* 6351556Srgrimes * if it exists we assume it is a directory, it is not within 6361556Srgrimes * the spec (at least it seems to read that way) to alter the 6371556Srgrimes * file system for nodes NOT EXPLICITLY stored on the archive. 6381556Srgrimes * If that assumption is changed, you would test the node here 6391556Srgrimes * and figure out how to get rid of it (probably like some 6401556Srgrimes * recursive unlink()) or fix up the directory permissions if 6411556Srgrimes * required (do an access()). 6421556Srgrimes */ 6431556Srgrimes if (lstat(name, &sb) == 0) { 6441556Srgrimes *(spt++) = '/'; 6451556Srgrimes continue; 6461556Srgrimes } 6471556Srgrimes 6481556Srgrimes /* 6491556Srgrimes * the path fails at this point, see if we can create the 6501556Srgrimes * needed directory and continue on 6511556Srgrimes */ 6521556Srgrimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 6531556Srgrimes *spt = '/'; 6541556Srgrimes retval = -1; 6551556Srgrimes break; 6561556Srgrimes } 6571556Srgrimes 6581556Srgrimes /* 6591556Srgrimes * we were able to create the directory. We will tell the 6601556Srgrimes * caller that we found something to fix, and it is ok to try 6611556Srgrimes * and create the node again. 6621556Srgrimes */ 6631556Srgrimes retval = 0; 6641556Srgrimes if (pids) 6651556Srgrimes (void)set_ids(name, st_uid, st_gid); 6661556Srgrimes 6671556Srgrimes /* 6681556Srgrimes * make sure the user doen't have some strange umask that 6691556Srgrimes * causes this newly created directory to be unusable. We fix 6701556Srgrimes * the modes and restore them back to the creation default at 6711556Srgrimes * the end of pax 6721556Srgrimes */ 6731556Srgrimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6741556Srgrimes (lstat(name, &sb) == 0)) { 6751556Srgrimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6761556Srgrimes add_dir(name, spt - name, &sb, 1); 6771556Srgrimes } 6781556Srgrimes *(spt++) = '/'; 6791556Srgrimes continue; 6801556Srgrimes } 6811556Srgrimes return(retval); 6821556Srgrimes} 6831556Srgrimes 6841556Srgrimes/* 6851556Srgrimes * set_ftime() 6861556Srgrimes * Set the access time and modification time for a named file. If frc is 68776017Skris * non-zero we force these times to be set even if the user did not 6881556Srgrimes * request access and/or modification time preservation (this is also 6891556Srgrimes * used by -t to reset access times). 6901556Srgrimes * When ign is zero, only those times the user has asked for are set, the 6911556Srgrimes * other ones are left alone. We do not assume the un-documented feature 6921556Srgrimes * of many utimes() implementations that consider a 0 time value as a do 6931556Srgrimes * not set request. 6941556Srgrimes */ 6951556Srgrimes 69676017Skris#ifdef __STDC__ 6971556Srgrimesvoid 6981556Srgrimesset_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6991556Srgrimes#else 7001556Srgrimesvoid 7011556Srgrimesset_ftime(fnm, mtime, atime, frc) 7021556Srgrimes char *fnm; 7031556Srgrimes time_t mtime; 7041556Srgrimes time_t atime; 7051556Srgrimes int frc; 7061556Srgrimes#endif 7071556Srgrimes{ 7081556Srgrimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 7091556Srgrimes struct stat sb; 7101556Srgrimes 7111556Srgrimes tv[0].tv_sec = (long)atime; 7121556Srgrimes tv[1].tv_sec = (long)mtime; 7131556Srgrimes if (!frc && (!patime || !pmtime)) { 7141556Srgrimes /* 7151556Srgrimes * if we are not forcing, only set those times the user wants 7161556Srgrimes * set. We get the current values of the times if we need them. 7171556Srgrimes */ 7181556Srgrimes if (lstat(fnm, &sb) == 0) { 7191556Srgrimes if (!patime) 7201556Srgrimes tv[0].tv_sec = (long)sb.st_atime; 7211556Srgrimes if (!pmtime) 7221556Srgrimes tv[1].tv_sec = (long)sb.st_mtime; 7231556Srgrimes } else 72476017Skris syswarn(0,errno,"Unable to obtain file stats %s", fnm); 7251556Srgrimes } 7261556Srgrimes 7271556Srgrimes /* 7281556Srgrimes * set the times 7291556Srgrimes */ 7301556Srgrimes if (utimes(fnm, tv) < 0) 73176017Skris syswarn(1, errno, "Access/modification time set failed on: %s", 7321556Srgrimes fnm); 7331556Srgrimes return; 7341556Srgrimes} 7351556Srgrimes 7361556Srgrimes/* 7371556Srgrimes * set_ids() 7381556Srgrimes * set the uid and gid of a file system node 7391556Srgrimes * Return: 7401556Srgrimes * 0 when set, -1 on failure 7411556Srgrimes */ 7421556Srgrimes 74376017Skris#ifdef __STDC__ 7441556Srgrimesint 7451556Srgrimesset_ids(char *fnm, uid_t uid, gid_t gid) 7461556Srgrimes#else 7471556Srgrimesint 7481556Srgrimesset_ids(fnm, uid, gid) 7491556Srgrimes char *fnm; 7501556Srgrimes uid_t uid; 7511556Srgrimes gid_t gid; 7521556Srgrimes#endif 7531556Srgrimes{ 7541556Srgrimes if (chown(fnm, uid, gid) < 0) { 75576017Skris syswarn(1, errno, "Unable to set file uid/gid of %s", fnm); 7561556Srgrimes return(-1); 7571556Srgrimes } 7581556Srgrimes return(0); 7591556Srgrimes} 7601556Srgrimes 7611556Srgrimes/* 7621556Srgrimes * set_pmode() 7631556Srgrimes * Set file access mode 7641556Srgrimes */ 7651556Srgrimes 76676017Skris#ifdef __STDC__ 7671556Srgrimesvoid 7681556Srgrimesset_pmode(char *fnm, mode_t mode) 7691556Srgrimes#else 7701556Srgrimesvoid 7711556Srgrimesset_pmode(fnm, mode) 7721556Srgrimes char *fnm; 7731556Srgrimes mode_t mode; 7741556Srgrimes#endif 7751556Srgrimes{ 7761556Srgrimes mode &= ABITS; 7771556Srgrimes if (chmod(fnm, mode) < 0) 77876017Skris syswarn(1, errno, "Could not set permissions on %s", fnm); 7791556Srgrimes return; 7801556Srgrimes} 7811556Srgrimes 7821556Srgrimes/* 7831556Srgrimes * file_write() 7841556Srgrimes * Write/copy a file (during copy or archive extract). This routine knows 7851556Srgrimes * how to copy files with lseek holes in it. (Which are read as file 7861556Srgrimes * blocks containing all 0's but do not have any file blocks associated 7871556Srgrimes * with the data). Typical examples of these are files created by dbm 7881556Srgrimes * variants (.pag files). While the file size of these files are huge, the 7891556Srgrimes * actual storage is quite small (the files are sparse). The problem is 7901556Srgrimes * the holes read as all zeros so are probably stored on the archive that 7911556Srgrimes * way (there is no way to determine if the file block is really a hole, 7921556Srgrimes * we only know that a file block of all zero's can be a hole). 7931556Srgrimes * At this writing, no major archive format knows how to archive files 7941556Srgrimes * with holes. However, on extraction (or during copy, -rw) we have to 7951556Srgrimes * deal with these files. Without detecting the holes, the files can 7961556Srgrimes * consume a lot of file space if just written to disk. This replacement 7971556Srgrimes * for write when passed the basic allocation size of a file system block, 7981556Srgrimes * uses lseek whenever it detects the input data is all 0 within that 7991556Srgrimes * file block. In more detail, the strategy is as follows: 8001556Srgrimes * While the input is all zero keep doing an lseek. Keep track of when we 8011556Srgrimes * pass over file block boundries. Only write when we hit a non zero 8021556Srgrimes * input. once we have written a file block, we continue to write it to 8031556Srgrimes * the end (we stop looking at the input). When we reach the start of the 8041556Srgrimes * next file block, start checking for zero blocks again. Working on file 8051556Srgrimes * block boundries significantly reduces the overhead when copying files 8061556Srgrimes * that are NOT very sparse. This overhead (when compared to a write) is 8071556Srgrimes * almost below the measurement resolution on many systems. Without it, 8081556Srgrimes * files with holes cannot be safely copied. It does has a side effect as 8091556Srgrimes * it can put holes into files that did not have them before, but that is 8101556Srgrimes * not a problem since the file contents are unchanged (in fact it saves 8111556Srgrimes * file space). (Except on paging files for diskless clients. But since we 8121556Srgrimes * cannot determine one of those file from here, we ignore them). If this 8131556Srgrimes * ever ends up on a system where CTG files are supported and the holes 8141556Srgrimes * are not desired, just do a conditional test in those routines that 8151556Srgrimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 8161556Srgrimes * FILE, make sure to call file_flush() when the last write finishes with 8171556Srgrimes * an empty block. A lot of file systems will not create an lseek hole at 8181556Srgrimes * the end. In this case we drop a single 0 at the end to force the 8191556Srgrimes * trailing 0's in the file. 8201556Srgrimes * ---Parameters--- 8211556Srgrimes * rem: how many bytes left in this file system block 8221556Srgrimes * isempt: have we written to the file block yet (is it empty) 8231556Srgrimes * sz: basic file block allocation size 8241556Srgrimes * cnt: number of bytes on this write 8251556Srgrimes * str: buffer to write 8261556Srgrimes * Return: 8271556Srgrimes * number of bytes written, -1 on write (or lseek) error. 8281556Srgrimes */ 8291556Srgrimes 83076017Skris#ifdef __STDC__ 8311556Srgrimesint 8321556Srgrimesfile_write(int fd, char *str, register int cnt, int *rem, int *isempt, int sz, 8331556Srgrimes char *name) 8341556Srgrimes#else 8351556Srgrimesint 8361556Srgrimesfile_write(fd, str, cnt, rem, isempt, sz, name) 8371556Srgrimes int fd; 8381556Srgrimes char *str; 8391556Srgrimes register int cnt; 8401556Srgrimes int *rem; 8411556Srgrimes int *isempt; 8421556Srgrimes int sz; 8431556Srgrimes char *name; 8441556Srgrimes#endif 8451556Srgrimes{ 8461556Srgrimes register char *pt; 8471556Srgrimes register char *end; 8481556Srgrimes register int wcnt; 8491556Srgrimes register char *st = str; 8508855Srgrimes 8511556Srgrimes /* 8521556Srgrimes * while we have data to process 8531556Srgrimes */ 8541556Srgrimes while (cnt) { 8551556Srgrimes if (!*rem) { 8561556Srgrimes /* 8571556Srgrimes * We are now at the start of file system block again 8581556Srgrimes * (or what we think one is...). start looking for 8591556Srgrimes * empty blocks again 8601556Srgrimes */ 8611556Srgrimes *isempt = 1; 8621556Srgrimes *rem = sz; 8631556Srgrimes } 8641556Srgrimes 8651556Srgrimes /* 8661556Srgrimes * only examine up to the end of the current file block or 8671556Srgrimes * remaining characters to write, whatever is smaller 8681556Srgrimes */ 8691556Srgrimes wcnt = MIN(cnt, *rem); 8701556Srgrimes cnt -= wcnt; 8711556Srgrimes *rem -= wcnt; 8721556Srgrimes if (*isempt) { 8731556Srgrimes /* 8741556Srgrimes * have not written to this block yet, so we keep 8751556Srgrimes * looking for zero's 8761556Srgrimes */ 8771556Srgrimes pt = st; 8781556Srgrimes end = st + wcnt; 8791556Srgrimes 8801556Srgrimes /* 8811556Srgrimes * look for a zero filled buffer 8821556Srgrimes */ 8831556Srgrimes while ((pt < end) && (*pt == '\0')) 8841556Srgrimes ++pt; 8851556Srgrimes 8861556Srgrimes if (pt == end) { 8871556Srgrimes /* 8881556Srgrimes * skip, buf is empty so far 8891556Srgrimes */ 8901556Srgrimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 89176017Skris syswarn(1,errno,"File seek on %s", 8921556Srgrimes name); 8931556Srgrimes return(-1); 8941556Srgrimes } 8951556Srgrimes st = pt; 8961556Srgrimes continue; 8971556Srgrimes } 8981556Srgrimes /* 8991556Srgrimes * drat, the buf is not zero filled 9001556Srgrimes */ 9011556Srgrimes *isempt = 0; 9021556Srgrimes } 9031556Srgrimes 9041556Srgrimes /* 9051556Srgrimes * have non-zero data in this file system block, have to write 9061556Srgrimes */ 9071556Srgrimes if (write(fd, st, wcnt) != wcnt) { 90876017Skris syswarn(1, errno, "Failed write to file %s", name); 9091556Srgrimes return(-1); 9101556Srgrimes } 9111556Srgrimes st += wcnt; 9121556Srgrimes } 9131556Srgrimes return(st - str); 9141556Srgrimes} 9151556Srgrimes 9161556Srgrimes/* 9171556Srgrimes * file_flush() 9181556Srgrimes * when the last file block in a file is zero, many file systems will not 9191556Srgrimes * let us create a hole at the end. To get the last block with zeros, we 9201556Srgrimes * write the last BYTE with a zero (back up one byte and write a zero). 9211556Srgrimes */ 9221556Srgrimes 92376017Skris#ifdef __STDC__ 9241556Srgrimesvoid 9251556Srgrimesfile_flush(int fd, char *fname, int isempt) 9261556Srgrimes#else 9271556Srgrimesvoid 9281556Srgrimesfile_flush(fd, fname, isempt) 9291556Srgrimes int fd; 9301556Srgrimes char *fname; 9311556Srgrimes int isempt; 9321556Srgrimes#endif 9331556Srgrimes{ 9341556Srgrimes static char blnk[] = "\0"; 9351556Srgrimes 9361556Srgrimes /* 9371556Srgrimes * silly test, but make sure we are only called when the last block is 9381556Srgrimes * filled with all zeros. 9391556Srgrimes */ 9401556Srgrimes if (!isempt) 9411556Srgrimes return; 9421556Srgrimes 9431556Srgrimes /* 9441556Srgrimes * move back one byte and write a zero 9451556Srgrimes */ 9461556Srgrimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 94776017Skris syswarn(1, errno, "Failed seek on file %s", fname); 9481556Srgrimes return; 9491556Srgrimes } 9501556Srgrimes 9511556Srgrimes if (write(fd, blnk, 1) < 0) 95276017Skris syswarn(1, errno, "Failed write to file %s", fname); 9531556Srgrimes return; 9541556Srgrimes} 9551556Srgrimes 9561556Srgrimes/* 9571556Srgrimes * rdfile_close() 9581556Srgrimes * close a file we have beed reading (to copy or archive). If we have to 9591556Srgrimes * reset access time (tflag) do so (the times are stored in arcn). 9601556Srgrimes */ 9611556Srgrimes 96276017Skris#ifdef __STDC__ 9631556Srgrimesvoid 9641556Srgrimesrdfile_close(register ARCHD *arcn, register int *fd) 9651556Srgrimes#else 9661556Srgrimesvoid 9671556Srgrimesrdfile_close(arcn, fd) 9681556Srgrimes register ARCHD *arcn; 9691556Srgrimes register int *fd; 9701556Srgrimes#endif 9711556Srgrimes{ 9721556Srgrimes /* 9731556Srgrimes * make sure the file is open 9741556Srgrimes */ 9751556Srgrimes if (*fd < 0) 9761556Srgrimes return; 9771556Srgrimes 9781556Srgrimes (void)close(*fd); 9791556Srgrimes *fd = -1; 9801556Srgrimes if (!tflag) 9811556Srgrimes return; 9821556Srgrimes 9831556Srgrimes /* 9841556Srgrimes * user wants last access time reset 9851556Srgrimes */ 9861556Srgrimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 9871556Srgrimes return; 9881556Srgrimes} 9891556Srgrimes 9901556Srgrimes/* 9911556Srgrimes * set_crc() 9921556Srgrimes * read a file to calculate its crc. This is a real drag. Archive formats 9931556Srgrimes * that have this, end up reading the file twice (we have to write the 9941556Srgrimes * header WITH the crc before writing the file contents. Oh well... 9951556Srgrimes * Return: 9961556Srgrimes * 0 if was able to calculate the crc, -1 otherwise 9971556Srgrimes */ 9981556Srgrimes 99976017Skris#ifdef __STDC__ 10001556Srgrimesint 10011556Srgrimesset_crc(register ARCHD *arcn, register int fd) 10021556Srgrimes#else 10031556Srgrimesint 10041556Srgrimesset_crc(arcn, fd) 10051556Srgrimes register ARCHD *arcn; 10061556Srgrimes register int fd; 10071556Srgrimes#endif 10081556Srgrimes{ 10091556Srgrimes register int i; 10101556Srgrimes register int res; 10111556Srgrimes off_t cpcnt = 0L; 10121556Srgrimes u_long size; 10131556Srgrimes unsigned long crc = 0L; 10141556Srgrimes char tbuf[FILEBLK]; 10151556Srgrimes struct stat sb; 10161556Srgrimes 10171556Srgrimes if (fd < 0) { 10181556Srgrimes /* 10191556Srgrimes * hmm, no fd, should never happen. well no crc then. 10201556Srgrimes */ 10211556Srgrimes arcn->crc = 0L; 10221556Srgrimes return(0); 10231556Srgrimes } 10241556Srgrimes 10251556Srgrimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 10261556Srgrimes size = (u_long)sizeof(tbuf); 10271556Srgrimes 10281556Srgrimes /* 10291556Srgrimes * read all the bytes we think that there are in the file. If the user 10301556Srgrimes * is trying to archive an active file, forget this file. 10311556Srgrimes */ 10321556Srgrimes for(;;) { 10331556Srgrimes if ((res = read(fd, tbuf, size)) <= 0) 10341556Srgrimes break; 10351556Srgrimes cpcnt += res; 10361556Srgrimes for (i = 0; i < res; ++i) 10371556Srgrimes crc += (tbuf[i] & 0xff); 10381556Srgrimes } 10391556Srgrimes 10401556Srgrimes /* 10411556Srgrimes * safety check. we want to avoid archiving files that are active as 10421556Srgrimes * they can create inconsistant archive copies. 10431556Srgrimes */ 10441556Srgrimes if (cpcnt != arcn->sb.st_size) 104576017Skris paxwarn(1, "File changed size %s", arcn->org_name); 10461556Srgrimes else if (fstat(fd, &sb) < 0) 104776017Skris syswarn(1, errno, "Failed stat on %s", arcn->org_name); 10481556Srgrimes else if (arcn->sb.st_mtime != sb.st_mtime) 104976017Skris paxwarn(1, "File %s was modified during read", arcn->org_name); 10501556Srgrimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 105176017Skris syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 10521556Srgrimes else { 10531556Srgrimes arcn->crc = crc; 10541556Srgrimes return(0); 10551556Srgrimes } 10561556Srgrimes return(-1); 10571556Srgrimes} 1058