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