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