14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1992 Keith Muller. 34b88c807SRodney W. Grimes * Copyright (c) 1992, 1993 44b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 54b88c807SRodney W. Grimes * 64b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 74b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego. 84b88c807SRodney W. Grimes * 94b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 104b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 114b88c807SRodney W. Grimes * are met: 124b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 144b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 164b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 174b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 184b88c807SRodney W. Grimes * must display the following acknowledgement: 194b88c807SRodney W. Grimes * This product includes software developed by the University of 204b88c807SRodney W. Grimes * California, Berkeley and its contributors. 214b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 224b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 234b88c807SRodney W. Grimes * without specific prior written permission. 244b88c807SRodney W. Grimes * 254b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 264b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 274b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 284b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 294b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 304b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 314b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 324b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 334b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 344b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 354b88c807SRodney W. Grimes * SUCH DAMAGE. 364b88c807SRodney W. Grimes */ 374b88c807SRodney W. Grimes 384b88c807SRodney W. Grimes #ifndef lint 39c9a8d1f4SPhilippe Charnier #if 0 40c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 41c9a8d1f4SPhilippe Charnier #endif 42c9a8d1f4SPhilippe Charnier static const char rcsid[] = 432a456239SPeter Wemm "$FreeBSD$"; 444b88c807SRodney W. Grimes #endif /* not lint */ 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #include <sys/types.h> 474b88c807SRodney W. Grimes #include <sys/time.h> 484b88c807SRodney W. Grimes #include <sys/stat.h> 494b88c807SRodney W. Grimes #include <unistd.h> 504b88c807SRodney W. Grimes #include <fcntl.h> 514b88c807SRodney W. Grimes #include <string.h> 524b88c807SRodney W. Grimes #include <stdio.h> 534b88c807SRodney W. Grimes #include <errno.h> 544b88c807SRodney W. Grimes #include <sys/uio.h> 554b88c807SRodney W. Grimes #include <stdlib.h> 564b88c807SRodney W. Grimes #include "pax.h" 57b1787decSKris Kennaway #include "options.h" 584b88c807SRodney W. Grimes #include "extern.h" 594b88c807SRodney W. Grimes 604b88c807SRodney W. Grimes static int 61f789b261SWarner Losh mk_link(char *,struct stat *,char *, int); 624b88c807SRodney W. Grimes 634b88c807SRodney W. Grimes /* 644b88c807SRodney W. Grimes * routines that deal with file operations such as: creating, removing; 654b88c807SRodney W. Grimes * and setting access modes, uid/gid and times of files 664b88c807SRodney W. Grimes */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 694b88c807SRodney W. Grimes #define SETBITS (S_ISUID | S_ISGID) 704b88c807SRodney W. Grimes #define ABITS (FILEBITS | SETBITS) 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes /* 734b88c807SRodney W. Grimes * file_creat() 744b88c807SRodney W. Grimes * Create and open a file. 754b88c807SRodney W. Grimes * Return: 764b88c807SRodney W. Grimes * file descriptor or -1 for failure 774b88c807SRodney W. Grimes */ 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes int 80f789b261SWarner Losh file_creat(ARCHD *arcn) 814b88c807SRodney W. Grimes { 824b88c807SRodney W. Grimes int fd = -1; 834b88c807SRodney W. Grimes mode_t file_mode; 844b88c807SRodney W. Grimes int oerrno; 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes /* 874b88c807SRodney W. Grimes * assume file doesn't exist, so just try to create it, most times this 884b88c807SRodney W. Grimes * works. We have to take special handling when the file does exist. To 894b88c807SRodney W. Grimes * detect this, we use O_EXCL. For example when trying to create a 904b88c807SRodney W. Grimes * file and a character device or fifo exists with the same name, we 914b88c807SRodney W. Grimes * can accidently open the device by mistake (or block waiting to open) 9246be34b9SKris Kennaway * If we find that the open has failed, then figure spend the effort to 934b88c807SRodney W. Grimes * figure out why. This strategy was found to have better average 944b88c807SRodney W. Grimes * performance in common use than checking the file (and the path) 954b88c807SRodney W. Grimes * first with lstat. 964b88c807SRodney W. Grimes */ 974b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 984b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 994b88c807SRodney W. Grimes file_mode)) >= 0) 1004b88c807SRodney W. Grimes return(fd); 1014b88c807SRodney W. Grimes 1024b88c807SRodney W. Grimes /* 1034b88c807SRodney W. Grimes * the file seems to exist. First we try to get rid of it (found to be 1044b88c807SRodney W. Grimes * the second most common failure when traced). If this fails, only 1054b88c807SRodney W. Grimes * then we go to the expense to check and create the path to the file 1064b88c807SRodney W. Grimes */ 1074b88c807SRodney W. Grimes if (unlnk_exist(arcn->name, arcn->type) != 0) 1084b88c807SRodney W. Grimes return(-1); 1094b88c807SRodney W. Grimes 1104b88c807SRodney W. Grimes for (;;) { 1114b88c807SRodney W. Grimes /* 1124b88c807SRodney W. Grimes * try to open it again, if this fails, check all the nodes in 1134b88c807SRodney W. Grimes * the path and give it a final try. if chk_path() finds that 1144b88c807SRodney W. Grimes * it cannot fix anything, we will skip the last attempt 1154b88c807SRodney W. Grimes */ 1164b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 1174b88c807SRodney W. Grimes file_mode)) >= 0) 1184b88c807SRodney W. Grimes break; 1194b88c807SRodney W. Grimes oerrno = errno; 120b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 121778766feSKris Kennaway syswarn(1, oerrno, "Unable to create %s", arcn->name); 1224b88c807SRodney W. Grimes return(-1); 1234b88c807SRodney W. Grimes } 1244b88c807SRodney W. Grimes } 1254b88c807SRodney W. Grimes return(fd); 1264b88c807SRodney W. Grimes } 1274b88c807SRodney W. Grimes 1284b88c807SRodney W. Grimes /* 1294b88c807SRodney W. Grimes * file_close() 1304b88c807SRodney W. Grimes * Close file descriptor to a file just created by pax. Sets modes, 1314b88c807SRodney W. Grimes * ownership and times as required. 1324b88c807SRodney W. Grimes * Return: 1334b88c807SRodney W. Grimes * 0 for success, -1 for failure 1344b88c807SRodney W. Grimes */ 1354b88c807SRodney W. Grimes 1364b88c807SRodney W. Grimes void 137f789b261SWarner Losh file_close(ARCHD *arcn, int fd) 1384b88c807SRodney W. Grimes { 1394b88c807SRodney W. Grimes int res = 0; 1404b88c807SRodney W. Grimes 1414b88c807SRodney W. Grimes if (fd < 0) 1424b88c807SRodney W. Grimes return; 1434b88c807SRodney W. Grimes if (close(fd) < 0) 144778766feSKris Kennaway syswarn(0, errno, "Unable to close file descriptor on %s", 1454b88c807SRodney W. Grimes arcn->name); 1464b88c807SRodney W. Grimes 1474b88c807SRodney W. Grimes /* 1484b88c807SRodney W. Grimes * set owner/groups first as this may strip off mode bits we want 1494b88c807SRodney W. Grimes * then set file permission modes. Then set file access and 1504b88c807SRodney W. Grimes * modification times. 1514b88c807SRodney W. Grimes */ 1524b88c807SRodney W. Grimes if (pids) 1534b88c807SRodney W. Grimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 1544b88c807SRodney W. Grimes 1554b88c807SRodney W. Grimes /* 1564b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 1574b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT 1584b88c807SRodney W. Grimes * set uid/gid bits 1594b88c807SRodney W. Grimes */ 1604b88c807SRodney W. Grimes if (!pmode || res) 1614b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 1624b88c807SRodney W. Grimes if (pmode) 1634b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 1644b88c807SRodney W. Grimes if (patime || pmtime) 1654b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 1664b88c807SRodney W. Grimes } 1674b88c807SRodney W. Grimes 1684b88c807SRodney W. Grimes /* 1694b88c807SRodney W. Grimes * lnk_creat() 1704b88c807SRodney W. Grimes * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 1714b88c807SRodney W. Grimes * must exist; 1724b88c807SRodney W. Grimes * Return: 1734b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 1744b88c807SRodney W. Grimes */ 1754b88c807SRodney W. Grimes 1764b88c807SRodney W. Grimes int 177f789b261SWarner Losh lnk_creat(ARCHD *arcn) 1784b88c807SRodney W. Grimes { 1794b88c807SRodney W. Grimes struct stat sb; 1804b88c807SRodney W. Grimes 1814b88c807SRodney W. Grimes /* 1824b88c807SRodney W. Grimes * we may be running as root, so we have to be sure that link target 1834b88c807SRodney W. Grimes * is not a directory, so we lstat and check 1844b88c807SRodney W. Grimes */ 1854b88c807SRodney W. Grimes if (lstat(arcn->ln_name, &sb) < 0) { 186778766feSKris Kennaway syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 1874b88c807SRodney W. Grimes arcn->name); 1884b88c807SRodney W. Grimes return(-1); 1894b88c807SRodney W. Grimes } 1904b88c807SRodney W. Grimes 1914b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 192778766feSKris Kennaway paxwarn(1, "A hard link to the directory %s is not allowed", 1934b88c807SRodney W. Grimes arcn->ln_name); 1944b88c807SRodney W. Grimes return(-1); 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes 1974b88c807SRodney W. Grimes return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 1984b88c807SRodney W. Grimes } 1994b88c807SRodney W. Grimes 2004b88c807SRodney W. Grimes /* 2014b88c807SRodney W. Grimes * cross_lnk() 2024b88c807SRodney W. Grimes * Create a hard link to arcn->org_name from arcn->name. Only used in copy 203778766feSKris Kennaway * with the -l flag. No warning or error if this does not succeed (we will 2044b88c807SRodney W. Grimes * then just create the file) 2054b88c807SRodney W. Grimes * Return: 2064b88c807SRodney W. Grimes * 1 if copy() should try to create this file node 2074b88c807SRodney W. Grimes * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 2084b88c807SRodney W. Grimes */ 2094b88c807SRodney W. Grimes 2104b88c807SRodney W. Grimes int 211f789b261SWarner Losh cross_lnk(ARCHD *arcn) 2124b88c807SRodney W. Grimes { 2134b88c807SRodney W. Grimes /* 21446be34b9SKris Kennaway * try to make a link to original file (-l flag in copy mode). make sure 2154b88c807SRodney W. Grimes * we do not try to link to directories in case we are running as root 2164b88c807SRodney W. Grimes * (and it might succeed). 2174b88c807SRodney W. Grimes */ 2184b88c807SRodney W. Grimes if (arcn->type == PAX_DIR) 2194b88c807SRodney W. Grimes return(1); 2204b88c807SRodney W. Grimes return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 2214b88c807SRodney W. Grimes } 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes /* 2244b88c807SRodney W. Grimes * chk_same() 2254b88c807SRodney W. Grimes * In copy mode if we are not trying to make hard links between the src 2264b88c807SRodney W. Grimes * and destinations, make sure we are not going to overwrite ourselves by 2274b88c807SRodney W. Grimes * accident. This slows things down a little, but we have to protect all 2284b88c807SRodney W. Grimes * those people who make typing errors. 2294b88c807SRodney W. Grimes * Return: 2304b88c807SRodney W. Grimes * 1 the target does not exist, go ahead and copy 2314b88c807SRodney W. Grimes * 0 skip it file exists (-k) or may be the same as source file 2324b88c807SRodney W. Grimes */ 2334b88c807SRodney W. Grimes 2344b88c807SRodney W. Grimes int 235f789b261SWarner Losh chk_same(ARCHD *arcn) 2364b88c807SRodney W. Grimes { 2374b88c807SRodney W. Grimes struct stat sb; 2384b88c807SRodney W. Grimes 2394b88c807SRodney W. Grimes /* 2404b88c807SRodney W. Grimes * if file does not exist, return. if file exists and -k, skip it 2414b88c807SRodney W. Grimes * quietly 2424b88c807SRodney W. Grimes */ 2434b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) 2444b88c807SRodney W. Grimes return(1); 2454b88c807SRodney W. Grimes if (kflag) 2464b88c807SRodney W. Grimes return(0); 2474b88c807SRodney W. Grimes 2484b88c807SRodney W. Grimes /* 2494b88c807SRodney W. Grimes * better make sure the user does not have src == dest by mistake 2504b88c807SRodney W. Grimes */ 2514b88c807SRodney W. Grimes if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 252778766feSKris Kennaway paxwarn(1, "Unable to copy %s, file would overwrite itself", 2534b88c807SRodney W. Grimes arcn->name); 2544b88c807SRodney W. Grimes return(0); 2554b88c807SRodney W. Grimes } 2564b88c807SRodney W. Grimes return(1); 2574b88c807SRodney W. Grimes } 2584b88c807SRodney W. Grimes 2594b88c807SRodney W. Grimes /* 2604b88c807SRodney W. Grimes * mk_link() 2614b88c807SRodney W. Grimes * try to make a hard link between two files. if ign set, we do not 2624b88c807SRodney W. Grimes * complain. 2634b88c807SRodney W. Grimes * Return: 2644b88c807SRodney W. Grimes * 0 if successful (or we are done with this file but no error, such as 2654b88c807SRodney W. Grimes * finding the from file exists and the user has set -k). 2664b88c807SRodney W. Grimes * 1 when ign was set to indicates we could not make the link but we 2674b88c807SRodney W. Grimes * should try to copy/extract the file as that might work (and is an 2684b88c807SRodney W. Grimes * allowed option). -1 an error occurred. 2694b88c807SRodney W. Grimes */ 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes static int 272f789b261SWarner Losh mk_link(char *to, struct stat *to_sb, char *from, 2734b88c807SRodney W. Grimes int ign) 2744b88c807SRodney W. Grimes { 2754b88c807SRodney W. Grimes struct stat sb; 2764b88c807SRodney W. Grimes int oerrno; 2774b88c807SRodney W. Grimes 2784b88c807SRodney W. Grimes /* 2794b88c807SRodney W. Grimes * if from file exists, it has to be unlinked to make the link. If the 2804b88c807SRodney W. Grimes * file exists and -k is set, skip it quietly 2814b88c807SRodney W. Grimes */ 2824b88c807SRodney W. Grimes if (lstat(from, &sb) == 0) { 2834b88c807SRodney W. Grimes if (kflag) 2844b88c807SRodney W. Grimes return(0); 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes /* 2874b88c807SRodney W. Grimes * make sure it is not the same file, protect the user 2884b88c807SRodney W. Grimes */ 2894b88c807SRodney W. Grimes if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 290778766feSKris Kennaway paxwarn(1, "Unable to link file %s to itself", to); 2914b88c807SRodney W. Grimes return(-1);; 2924b88c807SRodney W. Grimes } 2934b88c807SRodney W. Grimes 2944b88c807SRodney W. Grimes /* 2954b88c807SRodney W. Grimes * try to get rid of the file, based on the type 2964b88c807SRodney W. Grimes */ 2974b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 2984b88c807SRodney W. Grimes if (rmdir(from) < 0) { 299778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 3004b88c807SRodney W. Grimes return(-1); 3014b88c807SRodney W. Grimes } 3024b88c807SRodney W. Grimes } else if (unlink(from) < 0) { 3034b88c807SRodney W. Grimes if (!ign) { 304778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 3054b88c807SRodney W. Grimes return(-1); 3064b88c807SRodney W. Grimes } 3074b88c807SRodney W. Grimes return(1); 3084b88c807SRodney W. Grimes } 3094b88c807SRodney W. Grimes } 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes /* 3124b88c807SRodney W. Grimes * from file is gone (or did not exist), try to make the hard link. 3134b88c807SRodney W. Grimes * if it fails, check the path and try it again (if chk_path() says to 3144b88c807SRodney W. Grimes * try again) 3154b88c807SRodney W. Grimes */ 3164b88c807SRodney W. Grimes for (;;) { 3174b88c807SRodney W. Grimes if (link(to, from) == 0) 3184b88c807SRodney W. Grimes break; 3194b88c807SRodney W. Grimes oerrno = errno; 320b1787decSKris Kennaway if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 3214b88c807SRodney W. Grimes continue; 3224b88c807SRodney W. Grimes if (!ign) { 323778766feSKris Kennaway syswarn(1, oerrno, "Could not link to %s from %s", to, 3244b88c807SRodney W. Grimes from); 3254b88c807SRodney W. Grimes return(-1); 3264b88c807SRodney W. Grimes } 3274b88c807SRodney W. Grimes return(1); 3284b88c807SRodney W. Grimes } 3294b88c807SRodney W. Grimes 3304b88c807SRodney W. Grimes /* 3314b88c807SRodney W. Grimes * all right the link was made 3324b88c807SRodney W. Grimes */ 3334b88c807SRodney W. Grimes return(0); 3344b88c807SRodney W. Grimes } 3354b88c807SRodney W. Grimes 3364b88c807SRodney W. Grimes /* 3374b88c807SRodney W. Grimes * node_creat() 3384b88c807SRodney W. Grimes * create an entry in the file system (other than a file or hard link). 3394b88c807SRodney W. Grimes * If successful, sets uid/gid modes and times as required. 3404b88c807SRodney W. Grimes * Return: 3414b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 3424b88c807SRodney W. Grimes */ 3434b88c807SRodney W. Grimes 3444b88c807SRodney W. Grimes int 345f789b261SWarner Losh node_creat(ARCHD *arcn) 3464b88c807SRodney W. Grimes { 347f789b261SWarner Losh int res; 348f789b261SWarner Losh int ign = 0; 349f789b261SWarner Losh int oerrno; 350f789b261SWarner Losh int pass = 0; 3514b88c807SRodney W. Grimes mode_t file_mode; 3524b88c807SRodney W. Grimes struct stat sb; 3534b88c807SRodney W. Grimes 3544b88c807SRodney W. Grimes /* 3554b88c807SRodney W. Grimes * create node based on type, if that fails try to unlink the node and 3564b88c807SRodney W. Grimes * try again. finally check the path and try again. As noted in the 3574b88c807SRodney W. Grimes * file and link creation routines, this method seems to exhibit the 3584b88c807SRodney W. Grimes * best performance in general use workloads. 3594b88c807SRodney W. Grimes */ 3604b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 3614b88c807SRodney W. Grimes 3624b88c807SRodney W. Grimes for (;;) { 3634b88c807SRodney W. Grimes switch(arcn->type) { 3644b88c807SRodney W. Grimes case PAX_DIR: 3654b88c807SRodney W. Grimes res = mkdir(arcn->name, file_mode); 3664b88c807SRodney W. Grimes if (ign) 3674b88c807SRodney W. Grimes res = 0; 3684b88c807SRodney W. Grimes break; 3694b88c807SRodney W. Grimes case PAX_CHR: 3704b88c807SRodney W. Grimes file_mode |= S_IFCHR; 3714b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3724b88c807SRodney W. Grimes break; 3734b88c807SRodney W. Grimes case PAX_BLK: 3744b88c807SRodney W. Grimes file_mode |= S_IFBLK; 3754b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3764b88c807SRodney W. Grimes break; 3774b88c807SRodney W. Grimes case PAX_FIF: 3784b88c807SRodney W. Grimes res = mkfifo(arcn->name, file_mode); 3794b88c807SRodney W. Grimes break; 3804b88c807SRodney W. Grimes case PAX_SCK: 3814b88c807SRodney W. Grimes /* 3824b88c807SRodney W. Grimes * Skip sockets, operation has no meaning under BSD 3834b88c807SRodney W. Grimes */ 384778766feSKris Kennaway paxwarn(0, 3854b88c807SRodney W. Grimes "%s skipped. Sockets cannot be copied or extracted", 3864b88c807SRodney W. Grimes arcn->name); 3874b88c807SRodney W. Grimes return(-1); 3884b88c807SRodney W. Grimes case PAX_SLK: 389b1787decSKris Kennaway res = symlink(arcn->ln_name, arcn->name); 3904b88c807SRodney W. Grimes break; 3914b88c807SRodney W. Grimes case PAX_CTG: 3924b88c807SRodney W. Grimes case PAX_HLK: 3934b88c807SRodney W. Grimes case PAX_HRG: 3944b88c807SRodney W. Grimes case PAX_REG: 3954b88c807SRodney W. Grimes default: 3964b88c807SRodney W. Grimes /* 3974b88c807SRodney W. Grimes * we should never get here 3984b88c807SRodney W. Grimes */ 399778766feSKris Kennaway paxwarn(0, "%s has an unknown file type, skipping", 4004b88c807SRodney W. Grimes arcn->name); 4014b88c807SRodney W. Grimes return(-1); 4024b88c807SRodney W. Grimes } 4034b88c807SRodney W. Grimes 4044b88c807SRodney W. Grimes /* 4054b88c807SRodney W. Grimes * if we were able to create the node break out of the loop, 4064b88c807SRodney W. Grimes * otherwise try to unlink the node and try again. if that 4074b88c807SRodney W. Grimes * fails check the full path and try a final time. 4084b88c807SRodney W. Grimes */ 4094b88c807SRodney W. Grimes if (res == 0) 4104b88c807SRodney W. Grimes break; 4114b88c807SRodney W. Grimes 4124b88c807SRodney W. Grimes /* 4134b88c807SRodney W. Grimes * we failed to make the node 4144b88c807SRodney W. Grimes */ 4154b88c807SRodney W. Grimes oerrno = errno; 4164b88c807SRodney W. Grimes if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 4174b88c807SRodney W. Grimes return(-1); 4184b88c807SRodney W. Grimes 4194b88c807SRodney W. Grimes if (++pass <= 1) 4204b88c807SRodney W. Grimes continue; 4214b88c807SRodney W. Grimes 422b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 423778766feSKris Kennaway syswarn(1, oerrno, "Could not create: %s", arcn->name); 4244b88c807SRodney W. Grimes return(-1); 4254b88c807SRodney W. Grimes } 4264b88c807SRodney W. Grimes } 4274b88c807SRodney W. Grimes 4284b88c807SRodney W. Grimes /* 4294b88c807SRodney W. Grimes * we were able to create the node. set uid/gid, modes and times 4304b88c807SRodney W. Grimes */ 4314b88c807SRodney W. Grimes if (pids) 432b1787decSKris Kennaway res = ((arcn->type == PAX_SLK) ? 433b1787decSKris Kennaway set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) : 434b1787decSKris Kennaway set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid)); 4354b88c807SRodney W. Grimes else 4364b88c807SRodney W. Grimes res = 0; 4374b88c807SRodney W. Grimes 4384b88c807SRodney W. Grimes /* 439b1787decSKris Kennaway * symlinks are done now. 440b1787decSKris Kennaway */ 441b1787decSKris Kennaway if (arcn->type == PAX_SLK) 442b1787decSKris Kennaway return(0); 443b1787decSKris Kennaway 444b1787decSKris Kennaway /* 4454b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 4464b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4474b88c807SRodney W. Grimes * set uid/gid bits 4484b88c807SRodney W. Grimes */ 4494b88c807SRodney W. Grimes if (!pmode || res) 4504b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 4514b88c807SRodney W. Grimes if (pmode) 4524b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 4534b88c807SRodney W. Grimes 454b1787decSKris Kennaway if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 4554b88c807SRodney W. Grimes /* 4564b88c807SRodney W. Grimes * Dirs must be processed again at end of extract to set times 4574b88c807SRodney W. Grimes * and modes to agree with those stored in the archive. However 4584b88c807SRodney W. Grimes * to allow extract to continue, we may have to also set owner 4594b88c807SRodney W. Grimes * rights. This allows nodes in the archive that are children 4604b88c807SRodney W. Grimes * of this directory to be extracted without failure. Both time 4614b88c807SRodney W. Grimes * and modes will be fixed after the entire archive is read and 4624b88c807SRodney W. Grimes * before pax exits. 4634b88c807SRodney W. Grimes */ 4644b88c807SRodney W. Grimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 4654b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) { 466778766feSKris Kennaway syswarn(0, errno,"Could not access %s (stat)", 4674b88c807SRodney W. Grimes arcn->name); 4684b88c807SRodney W. Grimes set_pmode(arcn->name,file_mode | S_IRWXU); 4694b88c807SRodney W. Grimes } else { 4704b88c807SRodney W. Grimes /* 4714b88c807SRodney W. Grimes * We have to add rights to the dir, so we make 4724b88c807SRodney W. Grimes * sure to restore the mode. The mode must be 4734b88c807SRodney W. Grimes * restored AS CREATED and not as stored if 4744b88c807SRodney W. Grimes * pmode is not set. 4754b88c807SRodney W. Grimes */ 4764b88c807SRodney W. Grimes set_pmode(arcn->name, 4774b88c807SRodney W. Grimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 4784b88c807SRodney W. Grimes if (!pmode) 4794b88c807SRodney W. Grimes arcn->sb.st_mode = sb.st_mode; 4804b88c807SRodney W. Grimes } 4814b88c807SRodney W. Grimes 4824b88c807SRodney W. Grimes /* 4834b88c807SRodney W. Grimes * we have to force the mode to what was set here, 4844b88c807SRodney W. Grimes * since we changed it from the default as created. 4854b88c807SRodney W. Grimes */ 4864b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 4874b88c807SRodney W. Grimes } else if (pmode || patime || pmtime) 4884b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 4894b88c807SRodney W. Grimes } 4904b88c807SRodney W. Grimes 4914b88c807SRodney W. Grimes if (patime || pmtime) 4924b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 4934b88c807SRodney W. Grimes return(0); 4944b88c807SRodney W. Grimes } 4954b88c807SRodney W. Grimes 4964b88c807SRodney W. Grimes /* 4974b88c807SRodney W. Grimes * unlnk_exist() 4984b88c807SRodney W. Grimes * Remove node from file system with the specified name. We pass the type 4994b88c807SRodney W. Grimes * of the node that is going to replace it. When we try to create a 5004b88c807SRodney W. Grimes * directory and find that it already exists, we allow processing to 5014b88c807SRodney W. Grimes * continue as proper modes etc will always be set for it later on. 5024b88c807SRodney W. Grimes * Return: 5034b88c807SRodney W. Grimes * 0 is ok to proceed, no file with the specified name exists 5044b88c807SRodney W. Grimes * -1 we were unable to remove the node, or we should not remove it (-k) 5054b88c807SRodney W. Grimes * 1 we found a directory and we were going to create a directory. 5064b88c807SRodney W. Grimes */ 5074b88c807SRodney W. Grimes 5084b88c807SRodney W. Grimes int 509f789b261SWarner Losh unlnk_exist(char *name, int type) 5104b88c807SRodney W. Grimes { 5114b88c807SRodney W. Grimes struct stat sb; 5124b88c807SRodney W. Grimes 5134b88c807SRodney W. Grimes /* 5144b88c807SRodney W. Grimes * the file does not exist, or -k we are done 5154b88c807SRodney W. Grimes */ 5164b88c807SRodney W. Grimes if (lstat(name, &sb) < 0) 5174b88c807SRodney W. Grimes return(0); 5184b88c807SRodney W. Grimes if (kflag) 5194b88c807SRodney W. Grimes return(-1); 5204b88c807SRodney W. Grimes 5214b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 5224b88c807SRodney W. Grimes /* 5234b88c807SRodney W. Grimes * try to remove a directory, if it fails and we were going to 5244b88c807SRodney W. Grimes * create a directory anyway, tell the caller (return a 1) 5254b88c807SRodney W. Grimes */ 5264b88c807SRodney W. Grimes if (rmdir(name) < 0) { 5274b88c807SRodney W. Grimes if (type == PAX_DIR) 5284b88c807SRodney W. Grimes return(1); 529778766feSKris Kennaway syswarn(1,errno,"Unable to remove directory %s", name); 5304b88c807SRodney W. Grimes return(-1); 5314b88c807SRodney W. Grimes } 5324b88c807SRodney W. Grimes return(0); 5334b88c807SRodney W. Grimes } 5344b88c807SRodney W. Grimes 5354b88c807SRodney W. Grimes /* 5364b88c807SRodney W. Grimes * try to get rid of all non-directory type nodes 5374b88c807SRodney W. Grimes */ 5384b88c807SRodney W. Grimes if (unlink(name) < 0) { 539778766feSKris Kennaway syswarn(1, errno, "Could not unlink %s", name); 5404b88c807SRodney W. Grimes return(-1); 5414b88c807SRodney W. Grimes } 5424b88c807SRodney W. Grimes return(0); 5434b88c807SRodney W. Grimes } 5444b88c807SRodney W. Grimes 5454b88c807SRodney W. Grimes /* 5464b88c807SRodney W. Grimes * chk_path() 5474b88c807SRodney W. Grimes * We were trying to create some kind of node in the file system and it 5484b88c807SRodney W. Grimes * failed. chk_path() makes sure the path up to the node exists and is 5494b88c807SRodney W. Grimes * writeable. When we have to create a directory that is missing along the 5504b88c807SRodney W. Grimes * path somewhere, the directory we create will be set to the same 5514b88c807SRodney W. Grimes * uid/gid as the file has (when uid and gid are being preserved). 5524b88c807SRodney W. Grimes * NOTE: this routine is a real performance loss. It is only used as a 5534b88c807SRodney W. Grimes * last resort when trying to create entries in the file system. 5544b88c807SRodney W. Grimes * Return: 5554b88c807SRodney W. Grimes * -1 when it could find nothing it is allowed to fix. 5564b88c807SRodney W. Grimes * 0 otherwise 5574b88c807SRodney W. Grimes */ 5584b88c807SRodney W. Grimes 5594b88c807SRodney W. Grimes int 560f789b261SWarner Losh chk_path( char *name, uid_t st_uid, gid_t st_gid) 5614b88c807SRodney W. Grimes { 562f789b261SWarner Losh char *spt = name; 5634b88c807SRodney W. Grimes struct stat sb; 5644b88c807SRodney W. Grimes int retval = -1; 5654b88c807SRodney W. Grimes 5664b88c807SRodney W. Grimes /* 5674b88c807SRodney W. Grimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 5684b88c807SRodney W. Grimes */ 5694b88c807SRodney W. Grimes if (*spt == '/') 5704b88c807SRodney W. Grimes ++spt; 5714b88c807SRodney W. Grimes 5724b88c807SRodney W. Grimes for(;;) { 5734b88c807SRodney W. Grimes /* 5744b88c807SRodney W. Grimes * work foward from the first / and check each part of the path 5754b88c807SRodney W. Grimes */ 5764b88c807SRodney W. Grimes spt = strchr(spt, '/'); 5774b88c807SRodney W. Grimes if (spt == NULL) 5784b88c807SRodney W. Grimes break; 5794b88c807SRodney W. Grimes *spt = '\0'; 5804b88c807SRodney W. Grimes 5814b88c807SRodney W. Grimes /* 5824b88c807SRodney W. Grimes * if it exists we assume it is a directory, it is not within 5834b88c807SRodney W. Grimes * the spec (at least it seems to read that way) to alter the 5844b88c807SRodney W. Grimes * file system for nodes NOT EXPLICITLY stored on the archive. 5854b88c807SRodney W. Grimes * If that assumption is changed, you would test the node here 5864b88c807SRodney W. Grimes * and figure out how to get rid of it (probably like some 5874b88c807SRodney W. Grimes * recursive unlink()) or fix up the directory permissions if 5884b88c807SRodney W. Grimes * required (do an access()). 5894b88c807SRodney W. Grimes */ 5904b88c807SRodney W. Grimes if (lstat(name, &sb) == 0) { 5914b88c807SRodney W. Grimes *(spt++) = '/'; 5924b88c807SRodney W. Grimes continue; 5934b88c807SRodney W. Grimes } 5944b88c807SRodney W. Grimes 5954b88c807SRodney W. Grimes /* 5964b88c807SRodney W. Grimes * the path fails at this point, see if we can create the 5974b88c807SRodney W. Grimes * needed directory and continue on 5984b88c807SRodney W. Grimes */ 5994b88c807SRodney W. Grimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 6004b88c807SRodney W. Grimes *spt = '/'; 6014b88c807SRodney W. Grimes retval = -1; 6024b88c807SRodney W. Grimes break; 6034b88c807SRodney W. Grimes } 6044b88c807SRodney W. Grimes 6054b88c807SRodney W. Grimes /* 6064b88c807SRodney W. Grimes * we were able to create the directory. We will tell the 6074b88c807SRodney W. Grimes * caller that we found something to fix, and it is ok to try 6084b88c807SRodney W. Grimes * and create the node again. 6094b88c807SRodney W. Grimes */ 6104b88c807SRodney W. Grimes retval = 0; 6114b88c807SRodney W. Grimes if (pids) 6124b88c807SRodney W. Grimes (void)set_ids(name, st_uid, st_gid); 6134b88c807SRodney W. Grimes 6144b88c807SRodney W. Grimes /* 6154b88c807SRodney W. Grimes * make sure the user doen't have some strange umask that 6164b88c807SRodney W. Grimes * causes this newly created directory to be unusable. We fix 6174b88c807SRodney W. Grimes * the modes and restore them back to the creation default at 6184b88c807SRodney W. Grimes * the end of pax 6194b88c807SRodney W. Grimes */ 6204b88c807SRodney W. Grimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6214b88c807SRodney W. Grimes (lstat(name, &sb) == 0)) { 6224b88c807SRodney W. Grimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6234b88c807SRodney W. Grimes add_dir(name, spt - name, &sb, 1); 6244b88c807SRodney W. Grimes } 6254b88c807SRodney W. Grimes *(spt++) = '/'; 6264b88c807SRodney W. Grimes continue; 6274b88c807SRodney W. Grimes } 6284b88c807SRodney W. Grimes return(retval); 6294b88c807SRodney W. Grimes } 6304b88c807SRodney W. Grimes 6314b88c807SRodney W. Grimes /* 6324b88c807SRodney W. Grimes * set_ftime() 6334b88c807SRodney W. Grimes * Set the access time and modification time for a named file. If frc is 634778766feSKris Kennaway * non-zero we force these times to be set even if the user did not 6354b88c807SRodney W. Grimes * request access and/or modification time preservation (this is also 6364b88c807SRodney W. Grimes * used by -t to reset access times). 6374b88c807SRodney W. Grimes * When ign is zero, only those times the user has asked for are set, the 6384b88c807SRodney W. Grimes * other ones are left alone. We do not assume the un-documented feature 6394b88c807SRodney W. Grimes * of many utimes() implementations that consider a 0 time value as a do 6404b88c807SRodney W. Grimes * not set request. 6414b88c807SRodney W. Grimes */ 6424b88c807SRodney W. Grimes 6434b88c807SRodney W. Grimes void 6444b88c807SRodney W. Grimes set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6454b88c807SRodney W. Grimes { 6464b88c807SRodney W. Grimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 6474b88c807SRodney W. Grimes struct stat sb; 6484b88c807SRodney W. Grimes 6496e63a104SMatthew Dillon tv[0].tv_sec = atime; 6506e63a104SMatthew Dillon tv[1].tv_sec = mtime; 6514b88c807SRodney W. Grimes if (!frc && (!patime || !pmtime)) { 6524b88c807SRodney W. Grimes /* 6534b88c807SRodney W. Grimes * if we are not forcing, only set those times the user wants 6544b88c807SRodney W. Grimes * set. We get the current values of the times if we need them. 6554b88c807SRodney W. Grimes */ 6564b88c807SRodney W. Grimes if (lstat(fnm, &sb) == 0) { 6574b88c807SRodney W. Grimes if (!patime) 6586e63a104SMatthew Dillon tv[0].tv_sec = sb.st_atime; 6594b88c807SRodney W. Grimes if (!pmtime) 6606e63a104SMatthew Dillon tv[1].tv_sec = sb.st_mtime; 6614b88c807SRodney W. Grimes } else 662778766feSKris Kennaway syswarn(0,errno,"Unable to obtain file stats %s", fnm); 6634b88c807SRodney W. Grimes } 6644b88c807SRodney W. Grimes 6654b88c807SRodney W. Grimes /* 6664b88c807SRodney W. Grimes * set the times 6674b88c807SRodney W. Grimes */ 6684b88c807SRodney W. Grimes if (utimes(fnm, tv) < 0) 669778766feSKris Kennaway syswarn(1, errno, "Access/modification time set failed on: %s", 6704b88c807SRodney W. Grimes fnm); 6714b88c807SRodney W. Grimes return; 6724b88c807SRodney W. Grimes } 6734b88c807SRodney W. Grimes 6744b88c807SRodney W. Grimes /* 6754b88c807SRodney W. Grimes * set_ids() 6764b88c807SRodney W. Grimes * set the uid and gid of a file system node 6774b88c807SRodney W. Grimes * Return: 6784b88c807SRodney W. Grimes * 0 when set, -1 on failure 6794b88c807SRodney W. Grimes */ 6804b88c807SRodney W. Grimes 6814b88c807SRodney W. Grimes int 6824b88c807SRodney W. Grimes set_ids(char *fnm, uid_t uid, gid_t gid) 6834b88c807SRodney W. Grimes { 6844b88c807SRodney W. Grimes if (chown(fnm, uid, gid) < 0) { 685b1787decSKris Kennaway /* 686b1787decSKris Kennaway * ignore EPERM unless in verbose mode or being run by root. 687b1787decSKris Kennaway * if running as pax, POSIX requires a warning. 688b1787decSKris Kennaway */ 689b1787decSKris Kennaway if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 690b1787decSKris Kennaway geteuid() == 0) 691b1787decSKris Kennaway syswarn(1, errno, "Unable to set file uid/gid of %s", 692b1787decSKris Kennaway fnm); 693b1787decSKris Kennaway return(-1); 694b1787decSKris Kennaway } 695b1787decSKris Kennaway return(0); 696b1787decSKris Kennaway } 697b1787decSKris Kennaway 698b1787decSKris Kennaway /* 699b1787decSKris Kennaway * set_lids() 700b1787decSKris Kennaway * set the uid and gid of a file system node 701b1787decSKris Kennaway * Return: 702b1787decSKris Kennaway * 0 when set, -1 on failure 703b1787decSKris Kennaway */ 704b1787decSKris Kennaway 705b1787decSKris Kennaway int 706b1787decSKris Kennaway set_lids(char *fnm, uid_t uid, gid_t gid) 707b1787decSKris Kennaway { 708b1787decSKris Kennaway if (lchown(fnm, uid, gid) < 0) { 709b1787decSKris Kennaway /* 710b1787decSKris Kennaway * ignore EPERM unless in verbose mode or being run by root. 711b1787decSKris Kennaway * if running as pax, POSIX requires a warning. 712b1787decSKris Kennaway */ 713b1787decSKris Kennaway if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 714b1787decSKris Kennaway geteuid() == 0) 715b1787decSKris Kennaway syswarn(1, errno, "Unable to set file uid/gid of %s", 716b1787decSKris Kennaway fnm); 7174b88c807SRodney W. Grimes return(-1); 7184b88c807SRodney W. Grimes } 7194b88c807SRodney W. Grimes return(0); 7204b88c807SRodney W. Grimes } 7214b88c807SRodney W. Grimes 7224b88c807SRodney W. Grimes /* 7234b88c807SRodney W. Grimes * set_pmode() 7244b88c807SRodney W. Grimes * Set file access mode 7254b88c807SRodney W. Grimes */ 7264b88c807SRodney W. Grimes 7274b88c807SRodney W. Grimes void 7284b88c807SRodney W. Grimes set_pmode(char *fnm, mode_t mode) 7294b88c807SRodney W. Grimes { 7304b88c807SRodney W. Grimes mode &= ABITS; 7314b88c807SRodney W. Grimes if (chmod(fnm, mode) < 0) 732778766feSKris Kennaway syswarn(1, errno, "Could not set permissions on %s", fnm); 7334b88c807SRodney W. Grimes return; 7344b88c807SRodney W. Grimes } 7354b88c807SRodney W. Grimes 7364b88c807SRodney W. Grimes /* 7374b88c807SRodney W. Grimes * file_write() 7384b88c807SRodney W. Grimes * Write/copy a file (during copy or archive extract). This routine knows 7394b88c807SRodney W. Grimes * how to copy files with lseek holes in it. (Which are read as file 7404b88c807SRodney W. Grimes * blocks containing all 0's but do not have any file blocks associated 7414b88c807SRodney W. Grimes * with the data). Typical examples of these are files created by dbm 7424b88c807SRodney W. Grimes * variants (.pag files). While the file size of these files are huge, the 7434b88c807SRodney W. Grimes * actual storage is quite small (the files are sparse). The problem is 7444b88c807SRodney W. Grimes * the holes read as all zeros so are probably stored on the archive that 7454b88c807SRodney W. Grimes * way (there is no way to determine if the file block is really a hole, 7464b88c807SRodney W. Grimes * we only know that a file block of all zero's can be a hole). 7474b88c807SRodney W. Grimes * At this writing, no major archive format knows how to archive files 7484b88c807SRodney W. Grimes * with holes. However, on extraction (or during copy, -rw) we have to 7494b88c807SRodney W. Grimes * deal with these files. Without detecting the holes, the files can 7504b88c807SRodney W. Grimes * consume a lot of file space if just written to disk. This replacement 7514b88c807SRodney W. Grimes * for write when passed the basic allocation size of a file system block, 7524b88c807SRodney W. Grimes * uses lseek whenever it detects the input data is all 0 within that 7534b88c807SRodney W. Grimes * file block. In more detail, the strategy is as follows: 7544b88c807SRodney W. Grimes * While the input is all zero keep doing an lseek. Keep track of when we 7554b88c807SRodney W. Grimes * pass over file block boundries. Only write when we hit a non zero 7564b88c807SRodney W. Grimes * input. once we have written a file block, we continue to write it to 7574b88c807SRodney W. Grimes * the end (we stop looking at the input). When we reach the start of the 7584b88c807SRodney W. Grimes * next file block, start checking for zero blocks again. Working on file 7594b88c807SRodney W. Grimes * block boundries significantly reduces the overhead when copying files 7604b88c807SRodney W. Grimes * that are NOT very sparse. This overhead (when compared to a write) is 7614b88c807SRodney W. Grimes * almost below the measurement resolution on many systems. Without it, 7624b88c807SRodney W. Grimes * files with holes cannot be safely copied. It does has a side effect as 7634b88c807SRodney W. Grimes * it can put holes into files that did not have them before, but that is 7644b88c807SRodney W. Grimes * not a problem since the file contents are unchanged (in fact it saves 7654b88c807SRodney W. Grimes * file space). (Except on paging files for diskless clients. But since we 7664b88c807SRodney W. Grimes * cannot determine one of those file from here, we ignore them). If this 7674b88c807SRodney W. Grimes * ever ends up on a system where CTG files are supported and the holes 7684b88c807SRodney W. Grimes * are not desired, just do a conditional test in those routines that 7694b88c807SRodney W. Grimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 7704b88c807SRodney W. Grimes * FILE, make sure to call file_flush() when the last write finishes with 7714b88c807SRodney W. Grimes * an empty block. A lot of file systems will not create an lseek hole at 7724b88c807SRodney W. Grimes * the end. In this case we drop a single 0 at the end to force the 7734b88c807SRodney W. Grimes * trailing 0's in the file. 7744b88c807SRodney W. Grimes * ---Parameters--- 7754b88c807SRodney W. Grimes * rem: how many bytes left in this file system block 7764b88c807SRodney W. Grimes * isempt: have we written to the file block yet (is it empty) 7774b88c807SRodney W. Grimes * sz: basic file block allocation size 7784b88c807SRodney W. Grimes * cnt: number of bytes on this write 7794b88c807SRodney W. Grimes * str: buffer to write 7804b88c807SRodney W. Grimes * Return: 7814b88c807SRodney W. Grimes * number of bytes written, -1 on write (or lseek) error. 7824b88c807SRodney W. Grimes */ 7834b88c807SRodney W. Grimes 7844b88c807SRodney W. Grimes int 785f789b261SWarner Losh file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 7864b88c807SRodney W. Grimes char *name) 7874b88c807SRodney W. Grimes { 788f789b261SWarner Losh char *pt; 789f789b261SWarner Losh char *end; 790f789b261SWarner Losh int wcnt; 791f789b261SWarner Losh char *st = str; 7924b88c807SRodney W. Grimes 7934b88c807SRodney W. Grimes /* 7944b88c807SRodney W. Grimes * while we have data to process 7954b88c807SRodney W. Grimes */ 7964b88c807SRodney W. Grimes while (cnt) { 7974b88c807SRodney W. Grimes if (!*rem) { 7984b88c807SRodney W. Grimes /* 7994b88c807SRodney W. Grimes * We are now at the start of file system block again 8004b88c807SRodney W. Grimes * (or what we think one is...). start looking for 8014b88c807SRodney W. Grimes * empty blocks again 8024b88c807SRodney W. Grimes */ 8034b88c807SRodney W. Grimes *isempt = 1; 8044b88c807SRodney W. Grimes *rem = sz; 8054b88c807SRodney W. Grimes } 8064b88c807SRodney W. Grimes 8074b88c807SRodney W. Grimes /* 8084b88c807SRodney W. Grimes * only examine up to the end of the current file block or 8094b88c807SRodney W. Grimes * remaining characters to write, whatever is smaller 8104b88c807SRodney W. Grimes */ 8114b88c807SRodney W. Grimes wcnt = MIN(cnt, *rem); 8124b88c807SRodney W. Grimes cnt -= wcnt; 8134b88c807SRodney W. Grimes *rem -= wcnt; 8144b88c807SRodney W. Grimes if (*isempt) { 8154b88c807SRodney W. Grimes /* 8164b88c807SRodney W. Grimes * have not written to this block yet, so we keep 8174b88c807SRodney W. Grimes * looking for zero's 8184b88c807SRodney W. Grimes */ 8194b88c807SRodney W. Grimes pt = st; 8204b88c807SRodney W. Grimes end = st + wcnt; 8214b88c807SRodney W. Grimes 8224b88c807SRodney W. Grimes /* 8234b88c807SRodney W. Grimes * look for a zero filled buffer 8244b88c807SRodney W. Grimes */ 8254b88c807SRodney W. Grimes while ((pt < end) && (*pt == '\0')) 8264b88c807SRodney W. Grimes ++pt; 8274b88c807SRodney W. Grimes 8284b88c807SRodney W. Grimes if (pt == end) { 8294b88c807SRodney W. Grimes /* 8304b88c807SRodney W. Grimes * skip, buf is empty so far 8314b88c807SRodney W. Grimes */ 8324b88c807SRodney W. Grimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 833778766feSKris Kennaway syswarn(1,errno,"File seek on %s", 8344b88c807SRodney W. Grimes name); 8354b88c807SRodney W. Grimes return(-1); 8364b88c807SRodney W. Grimes } 8374b88c807SRodney W. Grimes st = pt; 8384b88c807SRodney W. Grimes continue; 8394b88c807SRodney W. Grimes } 8404b88c807SRodney W. Grimes /* 8414b88c807SRodney W. Grimes * drat, the buf is not zero filled 8424b88c807SRodney W. Grimes */ 8434b88c807SRodney W. Grimes *isempt = 0; 8444b88c807SRodney W. Grimes } 8454b88c807SRodney W. Grimes 8464b88c807SRodney W. Grimes /* 8474b88c807SRodney W. Grimes * have non-zero data in this file system block, have to write 8484b88c807SRodney W. Grimes */ 8494b88c807SRodney W. Grimes if (write(fd, st, wcnt) != wcnt) { 850778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", name); 8514b88c807SRodney W. Grimes return(-1); 8524b88c807SRodney W. Grimes } 8534b88c807SRodney W. Grimes st += wcnt; 8544b88c807SRodney W. Grimes } 8554b88c807SRodney W. Grimes return(st - str); 8564b88c807SRodney W. Grimes } 8574b88c807SRodney W. Grimes 8584b88c807SRodney W. Grimes /* 8594b88c807SRodney W. Grimes * file_flush() 8604b88c807SRodney W. Grimes * when the last file block in a file is zero, many file systems will not 8614b88c807SRodney W. Grimes * let us create a hole at the end. To get the last block with zeros, we 8624b88c807SRodney W. Grimes * write the last BYTE with a zero (back up one byte and write a zero). 8634b88c807SRodney W. Grimes */ 8644b88c807SRodney W. Grimes 8654b88c807SRodney W. Grimes void 8664b88c807SRodney W. Grimes file_flush(int fd, char *fname, int isempt) 8674b88c807SRodney W. Grimes { 8684b88c807SRodney W. Grimes static char blnk[] = "\0"; 8694b88c807SRodney W. Grimes 8704b88c807SRodney W. Grimes /* 8714b88c807SRodney W. Grimes * silly test, but make sure we are only called when the last block is 8724b88c807SRodney W. Grimes * filled with all zeros. 8734b88c807SRodney W. Grimes */ 8744b88c807SRodney W. Grimes if (!isempt) 8754b88c807SRodney W. Grimes return; 8764b88c807SRodney W. Grimes 8774b88c807SRodney W. Grimes /* 8784b88c807SRodney W. Grimes * move back one byte and write a zero 8794b88c807SRodney W. Grimes */ 8804b88c807SRodney W. Grimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 881778766feSKris Kennaway syswarn(1, errno, "Failed seek on file %s", fname); 8824b88c807SRodney W. Grimes return; 8834b88c807SRodney W. Grimes } 8844b88c807SRodney W. Grimes 8854b88c807SRodney W. Grimes if (write(fd, blnk, 1) < 0) 886778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", fname); 8874b88c807SRodney W. Grimes return; 8884b88c807SRodney W. Grimes } 8894b88c807SRodney W. Grimes 8904b88c807SRodney W. Grimes /* 8914b88c807SRodney W. Grimes * rdfile_close() 8924b88c807SRodney W. Grimes * close a file we have beed reading (to copy or archive). If we have to 8934b88c807SRodney W. Grimes * reset access time (tflag) do so (the times are stored in arcn). 8944b88c807SRodney W. Grimes */ 8954b88c807SRodney W. Grimes 8964b88c807SRodney W. Grimes void 897f789b261SWarner Losh rdfile_close(ARCHD *arcn, int *fd) 8984b88c807SRodney W. Grimes { 8994b88c807SRodney W. Grimes /* 9004b88c807SRodney W. Grimes * make sure the file is open 9014b88c807SRodney W. Grimes */ 9024b88c807SRodney W. Grimes if (*fd < 0) 9034b88c807SRodney W. Grimes return; 9044b88c807SRodney W. Grimes 9054b88c807SRodney W. Grimes (void)close(*fd); 9064b88c807SRodney W. Grimes *fd = -1; 9074b88c807SRodney W. Grimes if (!tflag) 9084b88c807SRodney W. Grimes return; 9094b88c807SRodney W. Grimes 9104b88c807SRodney W. Grimes /* 9114b88c807SRodney W. Grimes * user wants last access time reset 9124b88c807SRodney W. Grimes */ 9134b88c807SRodney W. Grimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 9144b88c807SRodney W. Grimes return; 9154b88c807SRodney W. Grimes } 9164b88c807SRodney W. Grimes 9174b88c807SRodney W. Grimes /* 9184b88c807SRodney W. Grimes * set_crc() 9194b88c807SRodney W. Grimes * read a file to calculate its crc. This is a real drag. Archive formats 9204b88c807SRodney W. Grimes * that have this, end up reading the file twice (we have to write the 9214b88c807SRodney W. Grimes * header WITH the crc before writing the file contents. Oh well... 9224b88c807SRodney W. Grimes * Return: 9234b88c807SRodney W. Grimes * 0 if was able to calculate the crc, -1 otherwise 9244b88c807SRodney W. Grimes */ 9254b88c807SRodney W. Grimes 9264b88c807SRodney W. Grimes int 927f789b261SWarner Losh set_crc(ARCHD *arcn, int fd) 9284b88c807SRodney W. Grimes { 929f789b261SWarner Losh int i; 930f789b261SWarner Losh int res; 9314b88c807SRodney W. Grimes off_t cpcnt = 0L; 9324b88c807SRodney W. Grimes u_long size; 9334b88c807SRodney W. Grimes unsigned long crc = 0L; 9344b88c807SRodney W. Grimes char tbuf[FILEBLK]; 9354b88c807SRodney W. Grimes struct stat sb; 9364b88c807SRodney W. Grimes 9374b88c807SRodney W. Grimes if (fd < 0) { 9384b88c807SRodney W. Grimes /* 9394b88c807SRodney W. Grimes * hmm, no fd, should never happen. well no crc then. 9404b88c807SRodney W. Grimes */ 9414b88c807SRodney W. Grimes arcn->crc = 0L; 9424b88c807SRodney W. Grimes return(0); 9434b88c807SRodney W. Grimes } 9444b88c807SRodney W. Grimes 9454b88c807SRodney W. Grimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 9464b88c807SRodney W. Grimes size = (u_long)sizeof(tbuf); 9474b88c807SRodney W. Grimes 9484b88c807SRodney W. Grimes /* 9494b88c807SRodney W. Grimes * read all the bytes we think that there are in the file. If the user 9504b88c807SRodney W. Grimes * is trying to archive an active file, forget this file. 9514b88c807SRodney W. Grimes */ 9524b88c807SRodney W. Grimes for(;;) { 9534b88c807SRodney W. Grimes if ((res = read(fd, tbuf, size)) <= 0) 9544b88c807SRodney W. Grimes break; 9554b88c807SRodney W. Grimes cpcnt += res; 9564b88c807SRodney W. Grimes for (i = 0; i < res; ++i) 9574b88c807SRodney W. Grimes crc += (tbuf[i] & 0xff); 9584b88c807SRodney W. Grimes } 9594b88c807SRodney W. Grimes 9604b88c807SRodney W. Grimes /* 9614b88c807SRodney W. Grimes * safety check. we want to avoid archiving files that are active as 9624b88c807SRodney W. Grimes * they can create inconsistant archive copies. 9634b88c807SRodney W. Grimes */ 9644b88c807SRodney W. Grimes if (cpcnt != arcn->sb.st_size) 965778766feSKris Kennaway paxwarn(1, "File changed size %s", arcn->org_name); 9664b88c807SRodney W. Grimes else if (fstat(fd, &sb) < 0) 967778766feSKris Kennaway syswarn(1, errno, "Failed stat on %s", arcn->org_name); 9684b88c807SRodney W. Grimes else if (arcn->sb.st_mtime != sb.st_mtime) 969778766feSKris Kennaway paxwarn(1, "File %s was modified during read", arcn->org_name); 9704b88c807SRodney W. Grimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 971778766feSKris Kennaway syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 9724b88c807SRodney W. Grimes else { 9734b88c807SRodney W. Grimes arcn->crc = crc; 9744b88c807SRodney W. Grimes return(0); 9754b88c807SRodney W. Grimes } 9764b88c807SRodney W. Grimes return(-1); 9774b88c807SRodney W. Grimes } 978