file_subs.c revision 169926
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 169926 2007-05-24 06:44:37Z rse $"); 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) 42876351Skris res = ((arcn->type == PAX_SLK) ? 42976351Skris set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) : 43076351Skris set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid)); 4311556Srgrimes else 4321556Srgrimes res = 0; 4331556Srgrimes 4341556Srgrimes /* 43576351Skris * symlinks are done now. 43676351Skris */ 43776351Skris if (arcn->type == PAX_SLK) 43876351Skris return(0); 43976351Skris 44076351Skris /* 4411556Srgrimes * IMPORTANT SECURITY NOTE: 4421556Srgrimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4431556Srgrimes * set uid/gid bits 4441556Srgrimes */ 4451556Srgrimes if (!pmode || res) 4461556Srgrimes arcn->sb.st_mode &= ~(SETBITS); 4471556Srgrimes if (pmode) 4481556Srgrimes set_pmode(arcn->name, arcn->sb.st_mode); 4491556Srgrimes 45076351Skris if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 4511556Srgrimes /* 4521556Srgrimes * Dirs must be processed again at end of extract to set times 4531556Srgrimes * and modes to agree with those stored in the archive. However 4541556Srgrimes * to allow extract to continue, we may have to also set owner 4551556Srgrimes * rights. This allows nodes in the archive that are children 4561556Srgrimes * of this directory to be extracted without failure. Both time 4571556Srgrimes * and modes will be fixed after the entire archive is read and 4581556Srgrimes * before pax exits. 4591556Srgrimes */ 4601556Srgrimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 4611556Srgrimes if (lstat(arcn->name, &sb) < 0) { 46276017Skris syswarn(0, errno,"Could not access %s (stat)", 4631556Srgrimes arcn->name); 4641556Srgrimes set_pmode(arcn->name,file_mode | S_IRWXU); 4651556Srgrimes } else { 4661556Srgrimes /* 4671556Srgrimes * We have to add rights to the dir, so we make 4681556Srgrimes * sure to restore the mode. The mode must be 4691556Srgrimes * restored AS CREATED and not as stored if 4701556Srgrimes * pmode is not set. 4711556Srgrimes */ 4721556Srgrimes set_pmode(arcn->name, 4731556Srgrimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 4741556Srgrimes if (!pmode) 4751556Srgrimes arcn->sb.st_mode = sb.st_mode; 4761556Srgrimes } 4771556Srgrimes 4781556Srgrimes /* 4791556Srgrimes * we have to force the mode to what was set here, 4801556Srgrimes * since we changed it from the default as created. 4811556Srgrimes */ 4821556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 4831556Srgrimes } else if (pmode || patime || pmtime) 4841556Srgrimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 4851556Srgrimes } 4861556Srgrimes 4871556Srgrimes if (patime || pmtime) 4881556Srgrimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 4891556Srgrimes return(0); 4901556Srgrimes} 4911556Srgrimes 4921556Srgrimes/* 4931556Srgrimes * unlnk_exist() 494102230Strhodes * Remove node from file system with the specified name. We pass the type 4951556Srgrimes * of the node that is going to replace it. When we try to create a 4961556Srgrimes * directory and find that it already exists, we allow processing to 4971556Srgrimes * continue as proper modes etc will always be set for it later on. 4981556Srgrimes * Return: 4991556Srgrimes * 0 is ok to proceed, no file with the specified name exists 5001556Srgrimes * -1 we were unable to remove the node, or we should not remove it (-k) 5011556Srgrimes * 1 we found a directory and we were going to create a directory. 5021556Srgrimes */ 5031556Srgrimes 5041556Srgrimesint 50590113Simpunlnk_exist(char *name, int type) 5061556Srgrimes{ 5071556Srgrimes struct stat sb; 5081556Srgrimes 5091556Srgrimes /* 5101556Srgrimes * the file does not exist, or -k we are done 5111556Srgrimes */ 5121556Srgrimes if (lstat(name, &sb) < 0) 5131556Srgrimes return(0); 5141556Srgrimes if (kflag) 5151556Srgrimes return(-1); 5161556Srgrimes 5171556Srgrimes if (S_ISDIR(sb.st_mode)) { 5181556Srgrimes /* 5191556Srgrimes * try to remove a directory, if it fails and we were going to 5201556Srgrimes * create a directory anyway, tell the caller (return a 1) 5211556Srgrimes */ 5221556Srgrimes if (rmdir(name) < 0) { 5231556Srgrimes if (type == PAX_DIR) 5248855Srgrimes return(1); 52576017Skris syswarn(1,errno,"Unable to remove directory %s", name); 5261556Srgrimes return(-1); 5271556Srgrimes } 5281556Srgrimes return(0); 5291556Srgrimes } 5301556Srgrimes 5311556Srgrimes /* 5321556Srgrimes * try to get rid of all non-directory type nodes 5331556Srgrimes */ 5341556Srgrimes if (unlink(name) < 0) { 53576017Skris syswarn(1, errno, "Could not unlink %s", name); 5361556Srgrimes return(-1); 5371556Srgrimes } 5381556Srgrimes return(0); 5391556Srgrimes} 5401556Srgrimes 5411556Srgrimes/* 5421556Srgrimes * chk_path() 543102230Strhodes * We were trying to create some kind of node in the file system and it 5441556Srgrimes * failed. chk_path() makes sure the path up to the node exists and is 5451556Srgrimes * writeable. When we have to create a directory that is missing along the 5461556Srgrimes * path somewhere, the directory we create will be set to the same 5471556Srgrimes * uid/gid as the file has (when uid and gid are being preserved). 5481556Srgrimes * NOTE: this routine is a real performance loss. It is only used as a 549102230Strhodes * last resort when trying to create entries in the file system. 5501556Srgrimes * Return: 5511556Srgrimes * -1 when it could find nothing it is allowed to fix. 5521556Srgrimes * 0 otherwise 5531556Srgrimes */ 5541556Srgrimes 5551556Srgrimesint 55690113Simpchk_path( char *name, uid_t st_uid, gid_t st_gid) 5571556Srgrimes{ 55890113Simp char *spt = name; 5591556Srgrimes struct stat sb; 5601556Srgrimes int retval = -1; 5611556Srgrimes 5621556Srgrimes /* 5631556Srgrimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 5641556Srgrimes */ 5651556Srgrimes if (*spt == '/') 5661556Srgrimes ++spt; 5671556Srgrimes 5681556Srgrimes for(;;) { 5691556Srgrimes /* 5701556Srgrimes * work foward from the first / and check each part of the path 5711556Srgrimes */ 5721556Srgrimes spt = strchr(spt, '/'); 5731556Srgrimes if (spt == NULL) 5741556Srgrimes break; 5751556Srgrimes *spt = '\0'; 5761556Srgrimes 5771556Srgrimes /* 5781556Srgrimes * if it exists we assume it is a directory, it is not within 5791556Srgrimes * the spec (at least it seems to read that way) to alter the 580102230Strhodes * file system for nodes NOT EXPLICITLY stored on the archive. 5811556Srgrimes * If that assumption is changed, you would test the node here 5821556Srgrimes * and figure out how to get rid of it (probably like some 5831556Srgrimes * recursive unlink()) or fix up the directory permissions if 5841556Srgrimes * required (do an access()). 5851556Srgrimes */ 5861556Srgrimes if (lstat(name, &sb) == 0) { 5871556Srgrimes *(spt++) = '/'; 5881556Srgrimes continue; 5891556Srgrimes } 5901556Srgrimes 5911556Srgrimes /* 5921556Srgrimes * the path fails at this point, see if we can create the 5931556Srgrimes * needed directory and continue on 5941556Srgrimes */ 5951556Srgrimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 5961556Srgrimes *spt = '/'; 5971556Srgrimes retval = -1; 5981556Srgrimes break; 5991556Srgrimes } 6001556Srgrimes 6011556Srgrimes /* 6021556Srgrimes * we were able to create the directory. We will tell the 6031556Srgrimes * caller that we found something to fix, and it is ok to try 6041556Srgrimes * and create the node again. 6051556Srgrimes */ 6061556Srgrimes retval = 0; 6071556Srgrimes if (pids) 6081556Srgrimes (void)set_ids(name, st_uid, st_gid); 6091556Srgrimes 6101556Srgrimes /* 6111556Srgrimes * make sure the user doen't have some strange umask that 6121556Srgrimes * causes this newly created directory to be unusable. We fix 6131556Srgrimes * the modes and restore them back to the creation default at 6141556Srgrimes * the end of pax 6151556Srgrimes */ 6161556Srgrimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6171556Srgrimes (lstat(name, &sb) == 0)) { 6181556Srgrimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6191556Srgrimes add_dir(name, spt - name, &sb, 1); 6201556Srgrimes } 6211556Srgrimes *(spt++) = '/'; 6221556Srgrimes continue; 6231556Srgrimes } 6241556Srgrimes return(retval); 6251556Srgrimes} 6261556Srgrimes 6271556Srgrimes/* 6281556Srgrimes * set_ftime() 6291556Srgrimes * Set the access time and modification time for a named file. If frc is 63076017Skris * non-zero we force these times to be set even if the user did not 6311556Srgrimes * request access and/or modification time preservation (this is also 6321556Srgrimes * used by -t to reset access times). 6331556Srgrimes * When ign is zero, only those times the user has asked for are set, the 6341556Srgrimes * other ones are left alone. We do not assume the un-documented feature 6351556Srgrimes * of many utimes() implementations that consider a 0 time value as a do 6361556Srgrimes * not set request. 6371556Srgrimes */ 6381556Srgrimes 6391556Srgrimesvoid 6401556Srgrimesset_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6411556Srgrimes{ 6421556Srgrimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 6431556Srgrimes struct stat sb; 6441556Srgrimes 64585616Sdillon tv[0].tv_sec = atime; 64685616Sdillon tv[1].tv_sec = mtime; 6471556Srgrimes if (!frc && (!patime || !pmtime)) { 6481556Srgrimes /* 6491556Srgrimes * if we are not forcing, only set those times the user wants 6501556Srgrimes * set. We get the current values of the times if we need them. 6511556Srgrimes */ 6521556Srgrimes if (lstat(fnm, &sb) == 0) { 6531556Srgrimes if (!patime) 65485616Sdillon tv[0].tv_sec = sb.st_atime; 6551556Srgrimes if (!pmtime) 65685616Sdillon tv[1].tv_sec = sb.st_mtime; 6571556Srgrimes } else 65876017Skris syswarn(0,errno,"Unable to obtain file stats %s", fnm); 6591556Srgrimes } 6601556Srgrimes 6611556Srgrimes /* 6621556Srgrimes * set the times 6631556Srgrimes */ 6641556Srgrimes if (utimes(fnm, tv) < 0) 66576017Skris syswarn(1, errno, "Access/modification time set failed on: %s", 6661556Srgrimes fnm); 6671556Srgrimes return; 6681556Srgrimes} 6691556Srgrimes 6701556Srgrimes/* 6711556Srgrimes * set_ids() 672102230Strhodes * set the uid and gid of a file system node 6731556Srgrimes * Return: 6741556Srgrimes * 0 when set, -1 on failure 6751556Srgrimes */ 6761556Srgrimes 6771556Srgrimesint 6781556Srgrimesset_ids(char *fnm, uid_t uid, gid_t gid) 6791556Srgrimes{ 6801556Srgrimes if (chown(fnm, uid, gid) < 0) { 68176351Skris /* 68276351Skris * ignore EPERM unless in verbose mode or being run by root. 68376351Skris * if running as pax, POSIX requires a warning. 68476351Skris */ 68576351Skris if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 68676351Skris geteuid() == 0) 68776351Skris syswarn(1, errno, "Unable to set file uid/gid of %s", 68876351Skris fnm); 6891556Srgrimes return(-1); 6901556Srgrimes } 6911556Srgrimes return(0); 6921556Srgrimes} 6931556Srgrimes 6941556Srgrimes/* 69576351Skris * set_lids() 696102230Strhodes * set the uid and gid of a file system node 69776351Skris * Return: 69876351Skris * 0 when set, -1 on failure 69976351Skris */ 70076351Skris 70176351Skrisint 70276351Skrisset_lids(char *fnm, uid_t uid, gid_t gid) 70376351Skris{ 70476351Skris if (lchown(fnm, uid, gid) < 0) { 70576351Skris /* 70676351Skris * ignore EPERM unless in verbose mode or being run by root. 70776351Skris * if running as pax, POSIX requires a warning. 70876351Skris */ 70976351Skris if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 71076351Skris geteuid() == 0) 71176351Skris syswarn(1, errno, "Unable to set file uid/gid of %s", 71276351Skris fnm); 71376351Skris return(-1); 71476351Skris } 71576351Skris return(0); 71676351Skris} 71776351Skris 71876351Skris/* 7191556Srgrimes * set_pmode() 7201556Srgrimes * Set file access mode 7211556Srgrimes */ 7221556Srgrimes 7231556Srgrimesvoid 7241556Srgrimesset_pmode(char *fnm, mode_t mode) 7251556Srgrimes{ 7261556Srgrimes mode &= ABITS; 7271556Srgrimes if (chmod(fnm, mode) < 0) 72876017Skris syswarn(1, errno, "Could not set permissions on %s", fnm); 7291556Srgrimes return; 7301556Srgrimes} 7311556Srgrimes 7321556Srgrimes/* 7331556Srgrimes * file_write() 7341556Srgrimes * Write/copy a file (during copy or archive extract). This routine knows 7351556Srgrimes * how to copy files with lseek holes in it. (Which are read as file 7361556Srgrimes * blocks containing all 0's but do not have any file blocks associated 7371556Srgrimes * with the data). Typical examples of these are files created by dbm 7381556Srgrimes * variants (.pag files). While the file size of these files are huge, the 7391556Srgrimes * actual storage is quite small (the files are sparse). The problem is 7401556Srgrimes * the holes read as all zeros so are probably stored on the archive that 7411556Srgrimes * way (there is no way to determine if the file block is really a hole, 7421556Srgrimes * we only know that a file block of all zero's can be a hole). 7431556Srgrimes * At this writing, no major archive format knows how to archive files 7441556Srgrimes * with holes. However, on extraction (or during copy, -rw) we have to 7451556Srgrimes * deal with these files. Without detecting the holes, the files can 7461556Srgrimes * consume a lot of file space if just written to disk. This replacement 747102230Strhodes * for write when passed the basic allocation size of a file system block, 7481556Srgrimes * uses lseek whenever it detects the input data is all 0 within that 7491556Srgrimes * file block. In more detail, the strategy is as follows: 7501556Srgrimes * While the input is all zero keep doing an lseek. Keep track of when we 7511556Srgrimes * pass over file block boundries. Only write when we hit a non zero 7521556Srgrimes * input. once we have written a file block, we continue to write it to 7531556Srgrimes * the end (we stop looking at the input). When we reach the start of the 7541556Srgrimes * next file block, start checking for zero blocks again. Working on file 7551556Srgrimes * block boundries significantly reduces the overhead when copying files 7561556Srgrimes * that are NOT very sparse. This overhead (when compared to a write) is 7571556Srgrimes * almost below the measurement resolution on many systems. Without it, 7581556Srgrimes * files with holes cannot be safely copied. It does has a side effect as 7591556Srgrimes * it can put holes into files that did not have them before, but that is 7601556Srgrimes * not a problem since the file contents are unchanged (in fact it saves 7611556Srgrimes * file space). (Except on paging files for diskless clients. But since we 7621556Srgrimes * cannot determine one of those file from here, we ignore them). If this 7631556Srgrimes * ever ends up on a system where CTG files are supported and the holes 7641556Srgrimes * are not desired, just do a conditional test in those routines that 7651556Srgrimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 7661556Srgrimes * FILE, make sure to call file_flush() when the last write finishes with 767102230Strhodes * an empty block. A lot of file systems will not create an lseek hole at 7681556Srgrimes * the end. In this case we drop a single 0 at the end to force the 7691556Srgrimes * trailing 0's in the file. 7701556Srgrimes * ---Parameters--- 771102230Strhodes * rem: how many bytes left in this file system block 7721556Srgrimes * isempt: have we written to the file block yet (is it empty) 7731556Srgrimes * sz: basic file block allocation size 7741556Srgrimes * cnt: number of bytes on this write 7751556Srgrimes * str: buffer to write 7761556Srgrimes * Return: 7771556Srgrimes * number of bytes written, -1 on write (or lseek) error. 7781556Srgrimes */ 7791556Srgrimes 7801556Srgrimesint 78190113Simpfile_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 7821556Srgrimes char *name) 7831556Srgrimes{ 78490113Simp char *pt; 78590113Simp char *end; 78690113Simp int wcnt; 78790113Simp char *st = str; 7888855Srgrimes 7891556Srgrimes /* 7901556Srgrimes * while we have data to process 7911556Srgrimes */ 7921556Srgrimes while (cnt) { 7931556Srgrimes if (!*rem) { 7941556Srgrimes /* 795102230Strhodes * We are now at the start of file system block again 7961556Srgrimes * (or what we think one is...). start looking for 7971556Srgrimes * empty blocks again 7981556Srgrimes */ 7991556Srgrimes *isempt = 1; 8001556Srgrimes *rem = sz; 8011556Srgrimes } 8021556Srgrimes 8031556Srgrimes /* 8041556Srgrimes * only examine up to the end of the current file block or 8051556Srgrimes * remaining characters to write, whatever is smaller 8061556Srgrimes */ 8071556Srgrimes wcnt = MIN(cnt, *rem); 8081556Srgrimes cnt -= wcnt; 8091556Srgrimes *rem -= wcnt; 8101556Srgrimes if (*isempt) { 8111556Srgrimes /* 8121556Srgrimes * have not written to this block yet, so we keep 8131556Srgrimes * looking for zero's 8141556Srgrimes */ 8151556Srgrimes pt = st; 8161556Srgrimes end = st + wcnt; 8171556Srgrimes 8181556Srgrimes /* 8191556Srgrimes * look for a zero filled buffer 8201556Srgrimes */ 8211556Srgrimes while ((pt < end) && (*pt == '\0')) 8221556Srgrimes ++pt; 8231556Srgrimes 8241556Srgrimes if (pt == end) { 8251556Srgrimes /* 8261556Srgrimes * skip, buf is empty so far 8271556Srgrimes */ 8281556Srgrimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 82976017Skris syswarn(1,errno,"File seek on %s", 8301556Srgrimes name); 8311556Srgrimes return(-1); 8321556Srgrimes } 8331556Srgrimes st = pt; 8341556Srgrimes continue; 8351556Srgrimes } 8361556Srgrimes /* 8371556Srgrimes * drat, the buf is not zero filled 8381556Srgrimes */ 8391556Srgrimes *isempt = 0; 8401556Srgrimes } 8411556Srgrimes 8421556Srgrimes /* 843102230Strhodes * have non-zero data in this file system block, have to write 8441556Srgrimes */ 8451556Srgrimes if (write(fd, st, wcnt) != wcnt) { 84676017Skris syswarn(1, errno, "Failed write to file %s", name); 8471556Srgrimes return(-1); 8481556Srgrimes } 8491556Srgrimes st += wcnt; 8501556Srgrimes } 8511556Srgrimes return(st - str); 8521556Srgrimes} 8531556Srgrimes 8541556Srgrimes/* 8551556Srgrimes * file_flush() 856102230Strhodes * when the last file block in a file is zero, many file systems will not 8571556Srgrimes * let us create a hole at the end. To get the last block with zeros, we 8581556Srgrimes * write the last BYTE with a zero (back up one byte and write a zero). 8591556Srgrimes */ 8601556Srgrimes 8611556Srgrimesvoid 8621556Srgrimesfile_flush(int fd, char *fname, int isempt) 8631556Srgrimes{ 8641556Srgrimes static char blnk[] = "\0"; 8651556Srgrimes 8661556Srgrimes /* 8671556Srgrimes * silly test, but make sure we are only called when the last block is 8681556Srgrimes * filled with all zeros. 8691556Srgrimes */ 8701556Srgrimes if (!isempt) 8711556Srgrimes return; 8721556Srgrimes 8731556Srgrimes /* 8741556Srgrimes * move back one byte and write a zero 8751556Srgrimes */ 8761556Srgrimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 87776017Skris syswarn(1, errno, "Failed seek on file %s", fname); 8781556Srgrimes return; 8791556Srgrimes } 8801556Srgrimes 8811556Srgrimes if (write(fd, blnk, 1) < 0) 88276017Skris syswarn(1, errno, "Failed write to file %s", fname); 8831556Srgrimes return; 8841556Srgrimes} 8851556Srgrimes 8861556Srgrimes/* 8871556Srgrimes * rdfile_close() 8881556Srgrimes * close a file we have beed reading (to copy or archive). If we have to 8891556Srgrimes * reset access time (tflag) do so (the times are stored in arcn). 8901556Srgrimes */ 8911556Srgrimes 8921556Srgrimesvoid 89390113Simprdfile_close(ARCHD *arcn, int *fd) 8941556Srgrimes{ 8951556Srgrimes /* 8961556Srgrimes * make sure the file is open 8971556Srgrimes */ 8981556Srgrimes if (*fd < 0) 8991556Srgrimes return; 9001556Srgrimes 9011556Srgrimes (void)close(*fd); 9021556Srgrimes *fd = -1; 9031556Srgrimes if (!tflag) 9041556Srgrimes return; 9051556Srgrimes 9061556Srgrimes /* 9071556Srgrimes * user wants last access time reset 9081556Srgrimes */ 9091556Srgrimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 9101556Srgrimes return; 9111556Srgrimes} 9121556Srgrimes 9131556Srgrimes/* 9141556Srgrimes * set_crc() 9151556Srgrimes * read a file to calculate its crc. This is a real drag. Archive formats 9161556Srgrimes * that have this, end up reading the file twice (we have to write the 9171556Srgrimes * header WITH the crc before writing the file contents. Oh well... 9181556Srgrimes * Return: 9191556Srgrimes * 0 if was able to calculate the crc, -1 otherwise 9201556Srgrimes */ 9211556Srgrimes 9221556Srgrimesint 92390113Simpset_crc(ARCHD *arcn, int fd) 9241556Srgrimes{ 92590113Simp int i; 92690113Simp int res; 9271556Srgrimes off_t cpcnt = 0L; 9281556Srgrimes u_long size; 9291556Srgrimes unsigned long crc = 0L; 9301556Srgrimes char tbuf[FILEBLK]; 9311556Srgrimes struct stat sb; 9321556Srgrimes 9331556Srgrimes if (fd < 0) { 9341556Srgrimes /* 9351556Srgrimes * hmm, no fd, should never happen. well no crc then. 9361556Srgrimes */ 9371556Srgrimes arcn->crc = 0L; 9381556Srgrimes return(0); 9391556Srgrimes } 9401556Srgrimes 9411556Srgrimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 9421556Srgrimes size = (u_long)sizeof(tbuf); 9431556Srgrimes 9441556Srgrimes /* 9451556Srgrimes * read all the bytes we think that there are in the file. If the user 9461556Srgrimes * is trying to archive an active file, forget this file. 9471556Srgrimes */ 9481556Srgrimes for(;;) { 9491556Srgrimes if ((res = read(fd, tbuf, size)) <= 0) 9501556Srgrimes break; 9511556Srgrimes cpcnt += res; 9521556Srgrimes for (i = 0; i < res; ++i) 9531556Srgrimes crc += (tbuf[i] & 0xff); 9541556Srgrimes } 9551556Srgrimes 9561556Srgrimes /* 9571556Srgrimes * safety check. we want to avoid archiving files that are active as 9581556Srgrimes * they can create inconsistant archive copies. 9591556Srgrimes */ 9601556Srgrimes if (cpcnt != arcn->sb.st_size) 96176017Skris paxwarn(1, "File changed size %s", arcn->org_name); 9621556Srgrimes else if (fstat(fd, &sb) < 0) 96376017Skris syswarn(1, errno, "Failed stat on %s", arcn->org_name); 9641556Srgrimes else if (arcn->sb.st_mtime != sb.st_mtime) 96576017Skris paxwarn(1, "File %s was modified during read", arcn->org_name); 9661556Srgrimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 96776017Skris syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 9681556Srgrimes else { 9691556Srgrimes arcn->crc = crc; 9701556Srgrimes return(0); 9711556Srgrimes } 9721556Srgrimes return(-1); 9731556Srgrimes} 974