file_subs.c revision 187976
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 * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 3536049Scharnier#if 0 3636049Scharnierstatic char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 3736049Scharnier#endif 381556Srgrimes#endif /* not lint */ 3999110Sobrien#include <sys/cdefs.h> 4099110Sobrien__FBSDID("$FreeBSD: head/bin/pax/file_subs.c 187976 2009-02-01 06:15:46Z kientzle $"); 411556Srgrimes 421556Srgrimes#include <sys/types.h> 431556Srgrimes#include <sys/time.h> 441556Srgrimes#include <sys/stat.h> 451556Srgrimes#include <unistd.h> 461556Srgrimes#include <fcntl.h> 471556Srgrimes#include <string.h> 481556Srgrimes#include <stdio.h> 491556Srgrimes#include <errno.h> 501556Srgrimes#include <sys/uio.h> 511556Srgrimes#include <stdlib.h> 521556Srgrimes#include "pax.h" 5376351Skris#include "options.h" 541556Srgrimes#include "extern.h" 551556Srgrimes 561556Srgrimesstatic int 5790113Simpmk_link(char *,struct stat *,char *, int); 581556Srgrimes 591556Srgrimes/* 601556Srgrimes * routines that deal with file operations such as: creating, removing; 611556Srgrimes * and setting access modes, uid/gid and times of files 621556Srgrimes */ 631556Srgrimes 641556Srgrimes#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 651556Srgrimes#define SETBITS (S_ISUID | S_ISGID) 661556Srgrimes#define ABITS (FILEBITS | SETBITS) 671556Srgrimes 681556Srgrimes/* 691556Srgrimes * file_creat() 701556Srgrimes * Create and open a file. 711556Srgrimes * Return: 721556Srgrimes * file descriptor or -1 for failure 731556Srgrimes */ 741556Srgrimes 751556Srgrimesint 7690113Simpfile_creat(ARCHD *arcn) 771556Srgrimes{ 781556Srgrimes int fd = -1; 791556Srgrimes mode_t file_mode; 801556Srgrimes int oerrno; 811556Srgrimes 821556Srgrimes /* 831556Srgrimes * assume file doesn't exist, so just try to create it, most times this 841556Srgrimes * works. We have to take special handling when the file does exist. To 851556Srgrimes * detect this, we use O_EXCL. For example when trying to create a 861556Srgrimes * file and a character device or fifo exists with the same name, we 871556Srgrimes * can accidently open the device by mistake (or block waiting to open) 8846684Skris * If we find that the open has failed, then figure spend the effort to 891556Srgrimes * figure out why. This strategy was found to have better average 901556Srgrimes * performance in common use than checking the file (and the path) 911556Srgrimes * first with lstat. 921556Srgrimes */ 931556Srgrimes file_mode = arcn->sb.st_mode & FILEBITS; 941556Srgrimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 951556Srgrimes file_mode)) >= 0) 961556Srgrimes return(fd); 971556Srgrimes 981556Srgrimes /* 991556Srgrimes * the file seems to exist. First we try to get rid of it (found to be 1001556Srgrimes * the second most common failure when traced). If this fails, only 1011556Srgrimes * then we go to the expense to check and create the path to the file 1021556Srgrimes */ 1031556Srgrimes if (unlnk_exist(arcn->name, arcn->type) != 0) 1041556Srgrimes return(-1); 1051556Srgrimes 1061556Srgrimes for (;;) { 1071556Srgrimes /* 1081556Srgrimes * try to open it again, if this fails, check all the nodes in 1091556Srgrimes * the path and give it a final try. if chk_path() finds that 1101556Srgrimes * it cannot fix anything, we will skip the last attempt 1111556Srgrimes */ 1121556Srgrimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 1131556Srgrimes file_mode)) >= 0) 1141556Srgrimes break; 1151556Srgrimes oerrno = errno; 11676351Skris if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 11776017Skris syswarn(1, oerrno, "Unable to create %s", arcn->name); 1181556Srgrimes return(-1); 1191556Srgrimes } 1201556Srgrimes } 1211556Srgrimes return(fd); 1221556Srgrimes} 1231556Srgrimes 1241556Srgrimes/* 1251556Srgrimes * file_close() 1261556Srgrimes * Close file descriptor to a file just created by pax. Sets modes, 1271556Srgrimes * ownership and times as required. 1281556Srgrimes * Return: 1291556Srgrimes * 0 for success, -1 for failure 1301556Srgrimes */ 1311556Srgrimes 1321556Srgrimesvoid 13390113Simpfile_close(ARCHD *arcn, int fd) 1341556Srgrimes{ 1351556Srgrimes int res = 0; 1361556Srgrimes 1371556Srgrimes if (fd < 0) 1381556Srgrimes return; 1391556Srgrimes if (close(fd) < 0) 14076017Skris syswarn(0, errno, "Unable to close file descriptor on %s", 1411556Srgrimes arcn->name); 1421556Srgrimes 1431556Srgrimes /* 1441556Srgrimes * set owner/groups first as this may strip off mode bits we want 1451556Srgrimes * then set file permission modes. Then set file access and 1468855Srgrimes * modification times. 1471556Srgrimes */ 1481556Srgrimes if (pids) 1491556Srgrimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 1501556Srgrimes 1511556Srgrimes /* 1521556Srgrimes * IMPORTANT SECURITY NOTE: 1531556Srgrimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT 1541556Srgrimes * set uid/gid bits 1551556Srgrimes */ 1561556Srgrimes if (!pmode || res) 1571556Srgrimes arcn->sb.st_mode &= ~(SETBITS); 1581556Srgrimes if (pmode) 1591556Srgrimes set_pmode(arcn->name, arcn->sb.st_mode); 1601556Srgrimes if (patime || pmtime) 1611556Srgrimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 1621556Srgrimes} 1631556Srgrimes 1641556Srgrimes/* 1651556Srgrimes * lnk_creat() 1661556Srgrimes * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 1678855Srgrimes * must exist; 1681556Srgrimes * Return: 1691556Srgrimes * 0 if ok, -1 otherwise 1701556Srgrimes */ 1711556Srgrimes 1721556Srgrimesint 17390113Simplnk_creat(ARCHD *arcn) 1741556Srgrimes{ 1751556Srgrimes struct stat sb; 1761556Srgrimes 1771556Srgrimes /* 1781556Srgrimes * we may be running as root, so we have to be sure that link target 1791556Srgrimes * is not a directory, so we lstat and check 1801556Srgrimes */ 1811556Srgrimes if (lstat(arcn->ln_name, &sb) < 0) { 18276017Skris syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 1831556Srgrimes arcn->name); 1841556Srgrimes return(-1); 1851556Srgrimes } 1861556Srgrimes 1871556Srgrimes if (S_ISDIR(sb.st_mode)) { 18876017Skris paxwarn(1, "A hard link to the directory %s is not allowed", 1891556Srgrimes arcn->ln_name); 1901556Srgrimes return(-1); 1911556Srgrimes } 1921556Srgrimes 1931556Srgrimes return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 1941556Srgrimes} 1951556Srgrimes 1961556Srgrimes/* 1971556Srgrimes * cross_lnk() 1981556Srgrimes * Create a hard link to arcn->org_name from arcn->name. Only used in copy 19976017Skris * with the -l flag. No warning or error if this does not succeed (we will 2001556Srgrimes * then just create the file) 2011556Srgrimes * Return: 2021556Srgrimes * 1 if copy() should try to create this file node 2031556Srgrimes * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 2041556Srgrimes */ 2051556Srgrimes 2061556Srgrimesint 20790113Simpcross_lnk(ARCHD *arcn) 2081556Srgrimes{ 2091556Srgrimes /* 21046684Skris * try to make a link to original file (-l flag in copy mode). make sure 2111556Srgrimes * we do not try to link to directories in case we are running as root 2121556Srgrimes * (and it might succeed). 2131556Srgrimes */ 2141556Srgrimes if (arcn->type == PAX_DIR) 2151556Srgrimes return(1); 2161556Srgrimes return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 2171556Srgrimes} 2181556Srgrimes 2191556Srgrimes/* 2201556Srgrimes * chk_same() 2211556Srgrimes * In copy mode if we are not trying to make hard links between the src 2221556Srgrimes * and destinations, make sure we are not going to overwrite ourselves by 2231556Srgrimes * accident. This slows things down a little, but we have to protect all 2241556Srgrimes * those people who make typing errors. 2251556Srgrimes * Return: 2261556Srgrimes * 1 the target does not exist, go ahead and copy 2271556Srgrimes * 0 skip it file exists (-k) or may be the same as source file 2281556Srgrimes */ 2291556Srgrimes 2301556Srgrimesint 23190113Simpchk_same(ARCHD *arcn) 2321556Srgrimes{ 2331556Srgrimes struct stat sb; 2341556Srgrimes 2358855Srgrimes /* 2361556Srgrimes * if file does not exist, return. if file exists and -k, skip it 2371556Srgrimes * quietly 2381556Srgrimes */ 2391556Srgrimes if (lstat(arcn->name, &sb) < 0) 2401556Srgrimes return(1); 2411556Srgrimes if (kflag) 2421556Srgrimes return(0); 2431556Srgrimes 2441556Srgrimes /* 2451556Srgrimes * better make sure the user does not have src == dest by mistake 2461556Srgrimes */ 2471556Srgrimes if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 24876017Skris paxwarn(1, "Unable to copy %s, file would overwrite itself", 2491556Srgrimes arcn->name); 2501556Srgrimes return(0); 2511556Srgrimes } 2521556Srgrimes return(1); 2531556Srgrimes} 2541556Srgrimes 2551556Srgrimes/* 2561556Srgrimes * mk_link() 2571556Srgrimes * try to make a hard link between two files. if ign set, we do not 2581556Srgrimes * complain. 2591556Srgrimes * Return: 2601556Srgrimes * 0 if successful (or we are done with this file but no error, such as 2611556Srgrimes * finding the from file exists and the user has set -k). 2621556Srgrimes * 1 when ign was set to indicates we could not make the link but we 2631556Srgrimes * should try to copy/extract the file as that might work (and is an 2641556Srgrimes * allowed option). -1 an error occurred. 2651556Srgrimes */ 2661556Srgrimes 2671556Srgrimesstatic int 26890113Simpmk_link(char *to, struct stat *to_sb, char *from, 2691556Srgrimes int ign) 2701556Srgrimes{ 2711556Srgrimes struct stat sb; 2721556Srgrimes int oerrno; 2731556Srgrimes 2741556Srgrimes /* 2751556Srgrimes * if from file exists, it has to be unlinked to make the link. If the 2761556Srgrimes * file exists and -k is set, skip it quietly 2771556Srgrimes */ 2781556Srgrimes if (lstat(from, &sb) == 0) { 2791556Srgrimes if (kflag) 2801556Srgrimes return(0); 2811556Srgrimes 2821556Srgrimes /* 2831556Srgrimes * make sure it is not the same file, protect the user 2841556Srgrimes */ 2851556Srgrimes if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 28676017Skris paxwarn(1, "Unable to link file %s to itself", to); 287169926Srse return(-1); 2881556Srgrimes } 2891556Srgrimes 2901556Srgrimes /* 2911556Srgrimes * try to get rid of the file, based on the type 2921556Srgrimes */ 2931556Srgrimes if (S_ISDIR(sb.st_mode)) { 2941556Srgrimes if (rmdir(from) < 0) { 29576017Skris syswarn(1, errno, "Unable to remove %s", from); 2961556Srgrimes return(-1); 2971556Srgrimes } 2981556Srgrimes } else if (unlink(from) < 0) { 2991556Srgrimes if (!ign) { 30076017Skris syswarn(1, errno, "Unable to remove %s", from); 3011556Srgrimes return(-1); 3021556Srgrimes } 3031556Srgrimes return(1); 3041556Srgrimes } 3051556Srgrimes } 3061556Srgrimes 3071556Srgrimes /* 3081556Srgrimes * from file is gone (or did not exist), try to make the hard link. 3091556Srgrimes * if it fails, check the path and try it again (if chk_path() says to 3101556Srgrimes * try again) 3111556Srgrimes */ 3121556Srgrimes for (;;) { 3131556Srgrimes if (link(to, from) == 0) 3141556Srgrimes break; 3151556Srgrimes oerrno = errno; 31676351Skris if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 3171556Srgrimes continue; 3181556Srgrimes if (!ign) { 31976017Skris syswarn(1, oerrno, "Could not link to %s from %s", to, 3201556Srgrimes from); 3211556Srgrimes return(-1); 3221556Srgrimes } 3231556Srgrimes return(1); 3241556Srgrimes } 3251556Srgrimes 3261556Srgrimes /* 3271556Srgrimes * all right the link was made 3281556Srgrimes */ 3291556Srgrimes return(0); 3301556Srgrimes} 3311556Srgrimes 3321556Srgrimes/* 3331556Srgrimes * node_creat() 334102230Strhodes * create an entry in the file system (other than a file or hard link). 3351556Srgrimes * If successful, sets uid/gid modes and times as required. 3361556Srgrimes * Return: 3371556Srgrimes * 0 if ok, -1 otherwise 3381556Srgrimes */ 3391556Srgrimes 3401556Srgrimesint 34190113Simpnode_creat(ARCHD *arcn) 3421556Srgrimes{ 34390113Simp int res; 34490113Simp int ign = 0; 34590113Simp int oerrno; 34690113Simp int pass = 0; 3471556Srgrimes mode_t file_mode; 3481556Srgrimes struct stat sb; 3491556Srgrimes 3501556Srgrimes /* 3511556Srgrimes * create node based on type, if that fails try to unlink the node and 3521556Srgrimes * try again. finally check the path and try again. As noted in the 3531556Srgrimes * file and link creation routines, this method seems to exhibit the 3541556Srgrimes * best performance in general use workloads. 3551556Srgrimes */ 3561556Srgrimes file_mode = arcn->sb.st_mode & FILEBITS; 3571556Srgrimes 3581556Srgrimes for (;;) { 3591556Srgrimes switch(arcn->type) { 3601556Srgrimes case PAX_DIR: 3611556Srgrimes res = mkdir(arcn->name, file_mode); 3621556Srgrimes if (ign) 3631556Srgrimes res = 0; 3641556Srgrimes break; 3651556Srgrimes case PAX_CHR: 3661556Srgrimes file_mode |= S_IFCHR; 3671556Srgrimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3681556Srgrimes break; 3691556Srgrimes case PAX_BLK: 3701556Srgrimes file_mode |= S_IFBLK; 3711556Srgrimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3721556Srgrimes break; 3731556Srgrimes case PAX_FIF: 3741556Srgrimes res = mkfifo(arcn->name, file_mode); 3751556Srgrimes break; 3761556Srgrimes case PAX_SCK: 3771556Srgrimes /* 3781556Srgrimes * Skip sockets, operation has no meaning under BSD 3791556Srgrimes */ 38076017Skris paxwarn(0, 3811556Srgrimes "%s skipped. Sockets cannot be copied or extracted", 3821556Srgrimes arcn->name); 3831556Srgrimes return(-1); 3841556Srgrimes case PAX_SLK: 38576351Skris res = symlink(arcn->ln_name, arcn->name); 3861556Srgrimes break; 3871556Srgrimes case PAX_CTG: 3881556Srgrimes case PAX_HLK: 3891556Srgrimes case PAX_HRG: 3901556Srgrimes case PAX_REG: 3911556Srgrimes default: 3921556Srgrimes /* 3931556Srgrimes * we should never get here 3941556Srgrimes */ 39576017Skris paxwarn(0, "%s has an unknown file type, skipping", 3961556Srgrimes arcn->name); 3971556Srgrimes return(-1); 3981556Srgrimes } 3991556Srgrimes 4001556Srgrimes /* 4011556Srgrimes * if we were able to create the node break out of the loop, 4021556Srgrimes * otherwise try to unlink the node and try again. if that 4031556Srgrimes * fails check the full path and try a final time. 4041556Srgrimes */ 4051556Srgrimes if (res == 0) 4061556Srgrimes break; 4071556Srgrimes 4081556Srgrimes /* 4091556Srgrimes * we failed to make the node 4101556Srgrimes */ 4111556Srgrimes oerrno = errno; 4121556Srgrimes if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 4131556Srgrimes return(-1); 4141556Srgrimes 4151556Srgrimes if (++pass <= 1) 4161556Srgrimes continue; 4171556Srgrimes 41876351Skris if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 41976017Skris syswarn(1, oerrno, "Could not create: %s", arcn->name); 4201556Srgrimes return(-1); 4211556Srgrimes } 4221556Srgrimes } 4231556Srgrimes 4241556Srgrimes /* 4251556Srgrimes * we were able to create the node. set uid/gid, modes and times 4261556Srgrimes */ 4271556Srgrimes if (pids) 428187976Skientzle res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 4291556Srgrimes else 4301556Srgrimes res = 0; 4311556Srgrimes 4321556Srgrimes /* 4331556Srgrimes * IMPORTANT SECURITY NOTE: 4341556Srgrimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4351556Srgrimes * set uid/gid bits 4361556Srgrimes */ 4371556Srgrimes if (!pmode || res) 4381556Srgrimes arcn->sb.st_mode &= ~(SETBITS); 4391556Srgrimes if (pmode) 4401556Srgrimes set_pmode(arcn->name, arcn->sb.st_mode); 4411556Srgrimes 44276351Skris if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 4431556Srgrimes /* 4441556Srgrimes * Dirs must be processed again at end of extract to set times 4451556Srgrimes * and modes to agree with those stored in the archive. However 4461556Srgrimes * to allow extract to continue, we may have to also set owner 4471556Srgrimes * rights. This allows nodes in the archive that are children 4481556Srgrimes * of this directory to be extracted without failure. Both time 4491556Srgrimes * and modes will be fixed after the entire archive is read and 4501556Srgrimes * before pax exits. 4511556Srgrimes */ 4521556Srgrimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 4531556Srgrimes if (lstat(arcn->name, &sb) < 0) { 45476017Skris syswarn(0, errno,"Could not access %s (stat)", 4551556Srgrimes arcn->name); 4561556Srgrimes set_pmode(arcn->name,file_mode | S_IRWXU); 4571556Srgrimes } else { 4581556Srgrimes /* 4591556Srgrimes * We have to add rights to the dir, so we make 4601556Srgrimes * sure to restore the mode. The mode must be 4611556Srgrimes * restored AS CREATED and not as stored if 4621556Srgrimes * pmode is not set. 4631556Srgrimes */ 4641556Srgrimes set_pmode(arcn->name, 4651556Srgrimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 4661556Srgrimes if (!pmode) 4671556Srgrimes arcn->sb.st_mode = sb.st_mode; 4681556Srgrimes } 4691556Srgrimes 4701556Srgrimes /* 4711556Srgrimes * we have to force the mode to what was set here, 4721556Srgrimes * since we changed it from the default as created. 4731556Srgrimes */ 4741556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 4751556Srgrimes } else if (pmode || patime || pmtime) 4761556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 4771556Srgrimes } 4781556Srgrimes 4791556Srgrimes if (patime || pmtime) 4801556Srgrimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 4811556Srgrimes return(0); 4821556Srgrimes} 4831556Srgrimes 4841556Srgrimes/* 4851556Srgrimes * unlnk_exist() 486102230Strhodes * Remove node from file system with the specified name. We pass the type 4871556Srgrimes * of the node that is going to replace it. When we try to create a 4881556Srgrimes * directory and find that it already exists, we allow processing to 4891556Srgrimes * continue as proper modes etc will always be set for it later on. 4901556Srgrimes * Return: 4911556Srgrimes * 0 is ok to proceed, no file with the specified name exists 4921556Srgrimes * -1 we were unable to remove the node, or we should not remove it (-k) 4931556Srgrimes * 1 we found a directory and we were going to create a directory. 4941556Srgrimes */ 4951556Srgrimes 4961556Srgrimesint 49790113Simpunlnk_exist(char *name, int type) 4981556Srgrimes{ 4991556Srgrimes struct stat sb; 5001556Srgrimes 5011556Srgrimes /* 5021556Srgrimes * the file does not exist, or -k we are done 5031556Srgrimes */ 5041556Srgrimes if (lstat(name, &sb) < 0) 5051556Srgrimes return(0); 5061556Srgrimes if (kflag) 5071556Srgrimes return(-1); 5081556Srgrimes 5091556Srgrimes if (S_ISDIR(sb.st_mode)) { 5101556Srgrimes /* 5111556Srgrimes * try to remove a directory, if it fails and we were going to 5121556Srgrimes * create a directory anyway, tell the caller (return a 1) 5131556Srgrimes */ 5141556Srgrimes if (rmdir(name) < 0) { 5151556Srgrimes if (type == PAX_DIR) 5168855Srgrimes return(1); 51776017Skris syswarn(1,errno,"Unable to remove directory %s", name); 5181556Srgrimes return(-1); 5191556Srgrimes } 5201556Srgrimes return(0); 5211556Srgrimes } 5221556Srgrimes 5231556Srgrimes /* 5241556Srgrimes * try to get rid of all non-directory type nodes 5251556Srgrimes */ 5261556Srgrimes if (unlink(name) < 0) { 52776017Skris syswarn(1, errno, "Could not unlink %s", name); 5281556Srgrimes return(-1); 5291556Srgrimes } 5301556Srgrimes return(0); 5311556Srgrimes} 5321556Srgrimes 5331556Srgrimes/* 5341556Srgrimes * chk_path() 535102230Strhodes * We were trying to create some kind of node in the file system and it 5361556Srgrimes * failed. chk_path() makes sure the path up to the node exists and is 5371556Srgrimes * writeable. When we have to create a directory that is missing along the 5381556Srgrimes * path somewhere, the directory we create will be set to the same 5391556Srgrimes * uid/gid as the file has (when uid and gid are being preserved). 5401556Srgrimes * NOTE: this routine is a real performance loss. It is only used as a 541102230Strhodes * last resort when trying to create entries in the file system. 5421556Srgrimes * Return: 5431556Srgrimes * -1 when it could find nothing it is allowed to fix. 5441556Srgrimes * 0 otherwise 5451556Srgrimes */ 5461556Srgrimes 5471556Srgrimesint 54890113Simpchk_path( char *name, uid_t st_uid, gid_t st_gid) 5491556Srgrimes{ 55090113Simp char *spt = name; 5511556Srgrimes struct stat sb; 5521556Srgrimes int retval = -1; 5531556Srgrimes 5541556Srgrimes /* 5551556Srgrimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 5561556Srgrimes */ 5571556Srgrimes if (*spt == '/') 5581556Srgrimes ++spt; 5591556Srgrimes 5601556Srgrimes for(;;) { 5611556Srgrimes /* 5621556Srgrimes * work foward from the first / and check each part of the path 5631556Srgrimes */ 5641556Srgrimes spt = strchr(spt, '/'); 5651556Srgrimes if (spt == NULL) 5661556Srgrimes break; 5671556Srgrimes *spt = '\0'; 5681556Srgrimes 5691556Srgrimes /* 5701556Srgrimes * if it exists we assume it is a directory, it is not within 5711556Srgrimes * the spec (at least it seems to read that way) to alter the 572102230Strhodes * file system for nodes NOT EXPLICITLY stored on the archive. 5731556Srgrimes * If that assumption is changed, you would test the node here 5741556Srgrimes * and figure out how to get rid of it (probably like some 5751556Srgrimes * recursive unlink()) or fix up the directory permissions if 5761556Srgrimes * required (do an access()). 5771556Srgrimes */ 5781556Srgrimes if (lstat(name, &sb) == 0) { 5791556Srgrimes *(spt++) = '/'; 5801556Srgrimes continue; 5811556Srgrimes } 5821556Srgrimes 5831556Srgrimes /* 5841556Srgrimes * the path fails at this point, see if we can create the 5851556Srgrimes * needed directory and continue on 5861556Srgrimes */ 5871556Srgrimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 5881556Srgrimes *spt = '/'; 5891556Srgrimes retval = -1; 5901556Srgrimes break; 5911556Srgrimes } 5921556Srgrimes 5931556Srgrimes /* 5941556Srgrimes * we were able to create the directory. We will tell the 5951556Srgrimes * caller that we found something to fix, and it is ok to try 5961556Srgrimes * and create the node again. 5971556Srgrimes */ 5981556Srgrimes retval = 0; 5991556Srgrimes if (pids) 6001556Srgrimes (void)set_ids(name, st_uid, st_gid); 6011556Srgrimes 6021556Srgrimes /* 6031556Srgrimes * make sure the user doen't have some strange umask that 6041556Srgrimes * causes this newly created directory to be unusable. We fix 6051556Srgrimes * the modes and restore them back to the creation default at 6061556Srgrimes * the end of pax 6071556Srgrimes */ 6081556Srgrimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6091556Srgrimes (lstat(name, &sb) == 0)) { 6101556Srgrimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6111556Srgrimes add_dir(name, spt - name, &sb, 1); 6121556Srgrimes } 6131556Srgrimes *(spt++) = '/'; 6141556Srgrimes continue; 6151556Srgrimes } 6161556Srgrimes return(retval); 6171556Srgrimes} 6181556Srgrimes 6191556Srgrimes/* 6201556Srgrimes * set_ftime() 6211556Srgrimes * Set the access time and modification time for a named file. If frc is 62276017Skris * non-zero we force these times to be set even if the user did not 6231556Srgrimes * request access and/or modification time preservation (this is also 6241556Srgrimes * used by -t to reset access times). 6251556Srgrimes * When ign is zero, only those times the user has asked for are set, the 6261556Srgrimes * other ones are left alone. We do not assume the un-documented feature 627187976Skientzle * of many lutimes() implementations that consider a 0 time value as a do 6281556Srgrimes * not set request. 6291556Srgrimes */ 6301556Srgrimes 6311556Srgrimesvoid 6321556Srgrimesset_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6331556Srgrimes{ 6341556Srgrimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 6351556Srgrimes struct stat sb; 6361556Srgrimes 63785616Sdillon tv[0].tv_sec = atime; 63885616Sdillon tv[1].tv_sec = mtime; 6391556Srgrimes if (!frc && (!patime || !pmtime)) { 6401556Srgrimes /* 6411556Srgrimes * if we are not forcing, only set those times the user wants 6421556Srgrimes * set. We get the current values of the times if we need them. 6431556Srgrimes */ 6441556Srgrimes if (lstat(fnm, &sb) == 0) { 6451556Srgrimes if (!patime) 64685616Sdillon tv[0].tv_sec = sb.st_atime; 6471556Srgrimes if (!pmtime) 64885616Sdillon tv[1].tv_sec = sb.st_mtime; 6491556Srgrimes } else 65076017Skris syswarn(0,errno,"Unable to obtain file stats %s", fnm); 6511556Srgrimes } 6521556Srgrimes 6531556Srgrimes /* 6541556Srgrimes * set the times 6551556Srgrimes */ 656187976Skientzle if (lutimes(fnm, tv) < 0) 65776017Skris syswarn(1, errno, "Access/modification time set failed on: %s", 6581556Srgrimes fnm); 6591556Srgrimes return; 6601556Srgrimes} 6611556Srgrimes 6621556Srgrimes/* 6631556Srgrimes * set_ids() 664102230Strhodes * set the uid and gid of a file system node 6651556Srgrimes * Return: 6661556Srgrimes * 0 when set, -1 on failure 6671556Srgrimes */ 6681556Srgrimes 6691556Srgrimesint 6701556Srgrimesset_ids(char *fnm, uid_t uid, gid_t gid) 6711556Srgrimes{ 67276351Skris if (lchown(fnm, uid, gid) < 0) { 67376351Skris /* 67476351Skris * ignore EPERM unless in verbose mode or being run by root. 67576351Skris * if running as pax, POSIX requires a warning. 67676351Skris */ 67776351Skris if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 67876351Skris geteuid() == 0) 67976351Skris syswarn(1, errno, "Unable to set file uid/gid of %s", 68076351Skris fnm); 68176351Skris return(-1); 68276351Skris } 68376351Skris return(0); 68476351Skris} 68576351Skris 68676351Skris/* 6871556Srgrimes * set_pmode() 6881556Srgrimes * Set file access mode 6891556Srgrimes */ 6901556Srgrimes 6911556Srgrimesvoid 6921556Srgrimesset_pmode(char *fnm, mode_t mode) 6931556Srgrimes{ 6941556Srgrimes mode &= ABITS; 695187976Skientzle if (lchmod(fnm, mode) < 0) 69676017Skris syswarn(1, errno, "Could not set permissions on %s", fnm); 6971556Srgrimes return; 6981556Srgrimes} 6991556Srgrimes 7001556Srgrimes/* 7011556Srgrimes * file_write() 7021556Srgrimes * Write/copy a file (during copy or archive extract). This routine knows 7031556Srgrimes * how to copy files with lseek holes in it. (Which are read as file 7041556Srgrimes * blocks containing all 0's but do not have any file blocks associated 7051556Srgrimes * with the data). Typical examples of these are files created by dbm 7061556Srgrimes * variants (.pag files). While the file size of these files are huge, the 7071556Srgrimes * actual storage is quite small (the files are sparse). The problem is 7081556Srgrimes * the holes read as all zeros so are probably stored on the archive that 7091556Srgrimes * way (there is no way to determine if the file block is really a hole, 7101556Srgrimes * we only know that a file block of all zero's can be a hole). 7111556Srgrimes * At this writing, no major archive format knows how to archive files 7121556Srgrimes * with holes. However, on extraction (or during copy, -rw) we have to 7131556Srgrimes * deal with these files. Without detecting the holes, the files can 7141556Srgrimes * consume a lot of file space if just written to disk. This replacement 715102230Strhodes * for write when passed the basic allocation size of a file system block, 7161556Srgrimes * uses lseek whenever it detects the input data is all 0 within that 7171556Srgrimes * file block. In more detail, the strategy is as follows: 7181556Srgrimes * While the input is all zero keep doing an lseek. Keep track of when we 7191556Srgrimes * pass over file block boundries. Only write when we hit a non zero 7201556Srgrimes * input. once we have written a file block, we continue to write it to 7211556Srgrimes * the end (we stop looking at the input). When we reach the start of the 7221556Srgrimes * next file block, start checking for zero blocks again. Working on file 7231556Srgrimes * block boundries significantly reduces the overhead when copying files 7241556Srgrimes * that are NOT very sparse. This overhead (when compared to a write) is 7251556Srgrimes * almost below the measurement resolution on many systems. Without it, 7261556Srgrimes * files with holes cannot be safely copied. It does has a side effect as 7271556Srgrimes * it can put holes into files that did not have them before, but that is 7281556Srgrimes * not a problem since the file contents are unchanged (in fact it saves 7291556Srgrimes * file space). (Except on paging files for diskless clients. But since we 7301556Srgrimes * cannot determine one of those file from here, we ignore them). If this 7311556Srgrimes * ever ends up on a system where CTG files are supported and the holes 7321556Srgrimes * are not desired, just do a conditional test in those routines that 7331556Srgrimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 7341556Srgrimes * FILE, make sure to call file_flush() when the last write finishes with 735102230Strhodes * an empty block. A lot of file systems will not create an lseek hole at 7361556Srgrimes * the end. In this case we drop a single 0 at the end to force the 7371556Srgrimes * trailing 0's in the file. 7381556Srgrimes * ---Parameters--- 739102230Strhodes * rem: how many bytes left in this file system block 7401556Srgrimes * isempt: have we written to the file block yet (is it empty) 7411556Srgrimes * sz: basic file block allocation size 7421556Srgrimes * cnt: number of bytes on this write 7431556Srgrimes * str: buffer to write 7441556Srgrimes * Return: 7451556Srgrimes * number of bytes written, -1 on write (or lseek) error. 7461556Srgrimes */ 7471556Srgrimes 7481556Srgrimesint 74990113Simpfile_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 7501556Srgrimes char *name) 7511556Srgrimes{ 75290113Simp char *pt; 75390113Simp char *end; 75490113Simp int wcnt; 75590113Simp char *st = str; 7568855Srgrimes 7571556Srgrimes /* 7581556Srgrimes * while we have data to process 7591556Srgrimes */ 7601556Srgrimes while (cnt) { 7611556Srgrimes if (!*rem) { 7621556Srgrimes /* 763102230Strhodes * We are now at the start of file system block again 7641556Srgrimes * (or what we think one is...). start looking for 7651556Srgrimes * empty blocks again 7661556Srgrimes */ 7671556Srgrimes *isempt = 1; 7681556Srgrimes *rem = sz; 7691556Srgrimes } 7701556Srgrimes 7711556Srgrimes /* 7721556Srgrimes * only examine up to the end of the current file block or 7731556Srgrimes * remaining characters to write, whatever is smaller 7741556Srgrimes */ 7751556Srgrimes wcnt = MIN(cnt, *rem); 7761556Srgrimes cnt -= wcnt; 7771556Srgrimes *rem -= wcnt; 7781556Srgrimes if (*isempt) { 7791556Srgrimes /* 7801556Srgrimes * have not written to this block yet, so we keep 7811556Srgrimes * looking for zero's 7821556Srgrimes */ 7831556Srgrimes pt = st; 7841556Srgrimes end = st + wcnt; 7851556Srgrimes 7861556Srgrimes /* 7871556Srgrimes * look for a zero filled buffer 7881556Srgrimes */ 7891556Srgrimes while ((pt < end) && (*pt == '\0')) 7901556Srgrimes ++pt; 7911556Srgrimes 7921556Srgrimes if (pt == end) { 7931556Srgrimes /* 7941556Srgrimes * skip, buf is empty so far 7951556Srgrimes */ 7961556Srgrimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 79776017Skris syswarn(1,errno,"File seek on %s", 7981556Srgrimes name); 7991556Srgrimes return(-1); 8001556Srgrimes } 8011556Srgrimes st = pt; 8021556Srgrimes continue; 8031556Srgrimes } 8041556Srgrimes /* 8051556Srgrimes * drat, the buf is not zero filled 8061556Srgrimes */ 8071556Srgrimes *isempt = 0; 8081556Srgrimes } 8091556Srgrimes 8101556Srgrimes /* 811102230Strhodes * have non-zero data in this file system block, have to write 8121556Srgrimes */ 8131556Srgrimes if (write(fd, st, wcnt) != wcnt) { 81476017Skris syswarn(1, errno, "Failed write to file %s", name); 8151556Srgrimes return(-1); 8161556Srgrimes } 8171556Srgrimes st += wcnt; 8181556Srgrimes } 8191556Srgrimes return(st - str); 8201556Srgrimes} 8211556Srgrimes 8221556Srgrimes/* 8231556Srgrimes * file_flush() 824102230Strhodes * when the last file block in a file is zero, many file systems will not 8251556Srgrimes * let us create a hole at the end. To get the last block with zeros, we 8261556Srgrimes * write the last BYTE with a zero (back up one byte and write a zero). 8271556Srgrimes */ 8281556Srgrimes 8291556Srgrimesvoid 8301556Srgrimesfile_flush(int fd, char *fname, int isempt) 8311556Srgrimes{ 8321556Srgrimes static char blnk[] = "\0"; 8331556Srgrimes 8341556Srgrimes /* 8351556Srgrimes * silly test, but make sure we are only called when the last block is 8361556Srgrimes * filled with all zeros. 8371556Srgrimes */ 8381556Srgrimes if (!isempt) 8391556Srgrimes return; 8401556Srgrimes 8411556Srgrimes /* 8421556Srgrimes * move back one byte and write a zero 8431556Srgrimes */ 8441556Srgrimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 84576017Skris syswarn(1, errno, "Failed seek on file %s", fname); 8461556Srgrimes return; 8471556Srgrimes } 8481556Srgrimes 8491556Srgrimes if (write(fd, blnk, 1) < 0) 85076017Skris syswarn(1, errno, "Failed write to file %s", fname); 8511556Srgrimes return; 8521556Srgrimes} 8531556Srgrimes 8541556Srgrimes/* 8551556Srgrimes * rdfile_close() 8561556Srgrimes * close a file we have beed reading (to copy or archive). If we have to 8571556Srgrimes * reset access time (tflag) do so (the times are stored in arcn). 8581556Srgrimes */ 8591556Srgrimes 8601556Srgrimesvoid 86190113Simprdfile_close(ARCHD *arcn, int *fd) 8621556Srgrimes{ 8631556Srgrimes /* 8641556Srgrimes * make sure the file is open 8651556Srgrimes */ 8661556Srgrimes if (*fd < 0) 8671556Srgrimes return; 8681556Srgrimes 8691556Srgrimes (void)close(*fd); 8701556Srgrimes *fd = -1; 8711556Srgrimes if (!tflag) 8721556Srgrimes return; 8731556Srgrimes 8741556Srgrimes /* 8751556Srgrimes * user wants last access time reset 8761556Srgrimes */ 8771556Srgrimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 8781556Srgrimes return; 8791556Srgrimes} 8801556Srgrimes 8811556Srgrimes/* 8821556Srgrimes * set_crc() 8831556Srgrimes * read a file to calculate its crc. This is a real drag. Archive formats 8841556Srgrimes * that have this, end up reading the file twice (we have to write the 8851556Srgrimes * header WITH the crc before writing the file contents. Oh well... 8861556Srgrimes * Return: 8871556Srgrimes * 0 if was able to calculate the crc, -1 otherwise 8881556Srgrimes */ 8891556Srgrimes 8901556Srgrimesint 89190113Simpset_crc(ARCHD *arcn, int fd) 8921556Srgrimes{ 89390113Simp int i; 89490113Simp int res; 8951556Srgrimes off_t cpcnt = 0L; 8961556Srgrimes u_long size; 8971556Srgrimes unsigned long crc = 0L; 8981556Srgrimes char tbuf[FILEBLK]; 8991556Srgrimes struct stat sb; 9001556Srgrimes 9011556Srgrimes if (fd < 0) { 9021556Srgrimes /* 9031556Srgrimes * hmm, no fd, should never happen. well no crc then. 9041556Srgrimes */ 9051556Srgrimes arcn->crc = 0L; 9061556Srgrimes return(0); 9071556Srgrimes } 9081556Srgrimes 9091556Srgrimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 9101556Srgrimes size = (u_long)sizeof(tbuf); 9111556Srgrimes 9121556Srgrimes /* 9131556Srgrimes * read all the bytes we think that there are in the file. If the user 9141556Srgrimes * is trying to archive an active file, forget this file. 9151556Srgrimes */ 9161556Srgrimes for(;;) { 9171556Srgrimes if ((res = read(fd, tbuf, size)) <= 0) 9181556Srgrimes break; 9191556Srgrimes cpcnt += res; 9201556Srgrimes for (i = 0; i < res; ++i) 9211556Srgrimes crc += (tbuf[i] & 0xff); 9221556Srgrimes } 9231556Srgrimes 9241556Srgrimes /* 9251556Srgrimes * safety check. we want to avoid archiving files that are active as 9261556Srgrimes * they can create inconsistant archive copies. 9271556Srgrimes */ 9281556Srgrimes if (cpcnt != arcn->sb.st_size) 92976017Skris paxwarn(1, "File changed size %s", arcn->org_name); 9301556Srgrimes else if (fstat(fd, &sb) < 0) 93176017Skris syswarn(1, errno, "Failed stat on %s", arcn->org_name); 9321556Srgrimes else if (arcn->sb.st_mtime != sb.st_mtime) 93376017Skris paxwarn(1, "File %s was modified during read", arcn->org_name); 9341556Srgrimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 93576017Skris syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 9361556Srgrimes else { 9371556Srgrimes arcn->crc = crc; 9381556Srgrimes return(0); 9391556Srgrimes } 9401556Srgrimes return(-1); 9411556Srgrimes} 942