14b88c807SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 44b88c807SRodney W. Grimes * Copyright (c) 1992 Keith Muller. 54b88c807SRodney W. Grimes * Copyright (c) 1992, 1993 64b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 94b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego. 104b88c807SRodney W. Grimes * 114b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 124b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 134b88c807SRodney W. Grimes * are met: 144b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 164b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 174b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 184b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 204b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 214b88c807SRodney W. Grimes * without specific prior written permission. 224b88c807SRodney W. Grimes * 234b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 244b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 254b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 264b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 274b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 284b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 294b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 304b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 314b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 324b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 334b88c807SRodney W. Grimes * SUCH DAMAGE. 344b88c807SRodney W. Grimes */ 354b88c807SRodney W. Grimes 364b88c807SRodney W. Grimes #include <sys/types.h> 374b88c807SRodney W. Grimes #include <sys/time.h> 384b88c807SRodney W. Grimes #include <sys/stat.h> 394b88c807SRodney W. Grimes #include <unistd.h> 404b88c807SRodney W. Grimes #include <fcntl.h> 414b88c807SRodney W. Grimes #include <string.h> 424b88c807SRodney W. Grimes #include <stdio.h> 434b88c807SRodney W. Grimes #include <errno.h> 444b88c807SRodney W. Grimes #include <sys/uio.h> 454b88c807SRodney W. Grimes #include "pax.h" 46b1787decSKris Kennaway #include "options.h" 474b88c807SRodney W. Grimes #include "extern.h" 484b88c807SRodney W. Grimes 494b88c807SRodney W. Grimes static int 50f789b261SWarner Losh mk_link(char *,struct stat *,char *, int); 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes /* 534b88c807SRodney W. Grimes * routines that deal with file operations such as: creating, removing; 544b88c807SRodney W. Grimes * and setting access modes, uid/gid and times of files 554b88c807SRodney W. Grimes */ 564b88c807SRodney W. Grimes 574b88c807SRodney W. Grimes #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 584b88c807SRodney W. Grimes #define SETBITS (S_ISUID | S_ISGID) 594b88c807SRodney W. Grimes #define ABITS (FILEBITS | SETBITS) 604b88c807SRodney W. Grimes 614b88c807SRodney W. Grimes /* 624b88c807SRodney W. Grimes * file_creat() 634b88c807SRodney W. Grimes * Create and open a file. 644b88c807SRodney W. Grimes * Return: 654b88c807SRodney W. Grimes * file descriptor or -1 for failure 664b88c807SRodney W. Grimes */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes int 69f789b261SWarner Losh file_creat(ARCHD *arcn) 704b88c807SRodney W. Grimes { 714b88c807SRodney W. Grimes int fd = -1; 724b88c807SRodney W. Grimes mode_t file_mode; 734b88c807SRodney W. Grimes int oerrno; 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes /* 764b88c807SRodney W. Grimes * assume file doesn't exist, so just try to create it, most times this 774b88c807SRodney W. Grimes * works. We have to take special handling when the file does exist. To 784b88c807SRodney W. Grimes * detect this, we use O_EXCL. For example when trying to create a 794b88c807SRodney W. Grimes * file and a character device or fifo exists with the same name, we 8001c99176SUlrich Spörlein * can accidentally open the device by mistake (or block waiting to 8101c99176SUlrich Spörlein * open). If we find that the open has failed, then spend the effort 8201c99176SUlrich Spörlein * to figure out why. This strategy was found to have better average 834b88c807SRodney W. Grimes * performance in common use than checking the file (and the path) 844b88c807SRodney W. Grimes * first with lstat. 854b88c807SRodney W. Grimes */ 864b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 874b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 884b88c807SRodney W. Grimes file_mode)) >= 0) 894b88c807SRodney W. Grimes return(fd); 904b88c807SRodney W. Grimes 914b88c807SRodney W. Grimes /* 924b88c807SRodney W. Grimes * the file seems to exist. First we try to get rid of it (found to be 934b88c807SRodney W. Grimes * the second most common failure when traced). If this fails, only 944b88c807SRodney W. Grimes * then we go to the expense to check and create the path to the file 954b88c807SRodney W. Grimes */ 964b88c807SRodney W. Grimes if (unlnk_exist(arcn->name, arcn->type) != 0) 974b88c807SRodney W. Grimes return(-1); 984b88c807SRodney W. Grimes 994b88c807SRodney W. Grimes for (;;) { 1004b88c807SRodney W. Grimes /* 1014b88c807SRodney W. Grimes * try to open it again, if this fails, check all the nodes in 1024b88c807SRodney W. Grimes * the path and give it a final try. if chk_path() finds that 1034b88c807SRodney W. Grimes * it cannot fix anything, we will skip the last attempt 1044b88c807SRodney W. Grimes */ 1054b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 1064b88c807SRodney W. Grimes file_mode)) >= 0) 1074b88c807SRodney W. Grimes break; 1084b88c807SRodney W. Grimes oerrno = errno; 109b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 110778766feSKris Kennaway syswarn(1, oerrno, "Unable to create %s", arcn->name); 1114b88c807SRodney W. Grimes return(-1); 1124b88c807SRodney W. Grimes } 1134b88c807SRodney W. Grimes } 1144b88c807SRodney W. Grimes return(fd); 1154b88c807SRodney W. Grimes } 1164b88c807SRodney W. Grimes 1174b88c807SRodney W. Grimes /* 1184b88c807SRodney W. Grimes * file_close() 1194b88c807SRodney W. Grimes * Close file descriptor to a file just created by pax. Sets modes, 1204b88c807SRodney W. Grimes * ownership and times as required. 1214b88c807SRodney W. Grimes * Return: 1224b88c807SRodney W. Grimes * 0 for success, -1 for failure 1234b88c807SRodney W. Grimes */ 1244b88c807SRodney W. Grimes 1254b88c807SRodney W. Grimes void 126f789b261SWarner Losh file_close(ARCHD *arcn, int fd) 1274b88c807SRodney W. Grimes { 1284b88c807SRodney W. Grimes int res = 0; 1294b88c807SRodney W. Grimes 1304b88c807SRodney W. Grimes if (fd < 0) 1314b88c807SRodney W. Grimes return; 1324b88c807SRodney W. Grimes if (close(fd) < 0) 133778766feSKris Kennaway syswarn(0, errno, "Unable to close file descriptor on %s", 1344b88c807SRodney W. Grimes arcn->name); 1354b88c807SRodney W. Grimes 1364b88c807SRodney W. Grimes /* 1374b88c807SRodney W. Grimes * set owner/groups first as this may strip off mode bits we want 1384b88c807SRodney W. Grimes * then set file permission modes. Then set file access and 1394b88c807SRodney W. Grimes * modification times. 1404b88c807SRodney W. Grimes */ 1414b88c807SRodney W. Grimes if (pids) 1424b88c807SRodney W. Grimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 1434b88c807SRodney W. Grimes 1444b88c807SRodney W. Grimes /* 1454b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 1464b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT 1474b88c807SRodney W. Grimes * set uid/gid bits 1484b88c807SRodney W. Grimes */ 1494b88c807SRodney W. Grimes if (!pmode || res) 1504b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 1514b88c807SRodney W. Grimes if (pmode) 1524b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 1534b88c807SRodney W. Grimes if (patime || pmtime) 1544b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 1554b88c807SRodney W. Grimes } 1564b88c807SRodney W. Grimes 1574b88c807SRodney W. Grimes /* 1584b88c807SRodney W. Grimes * lnk_creat() 1594b88c807SRodney W. Grimes * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 1604b88c807SRodney W. Grimes * must exist; 1614b88c807SRodney W. Grimes * Return: 1624b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 1634b88c807SRodney W. Grimes */ 1644b88c807SRodney W. Grimes 1654b88c807SRodney W. Grimes int 166f789b261SWarner Losh lnk_creat(ARCHD *arcn) 1674b88c807SRodney W. Grimes { 1684b88c807SRodney W. Grimes struct stat sb; 1694b88c807SRodney W. Grimes 1704b88c807SRodney W. Grimes /* 1714b88c807SRodney W. Grimes * we may be running as root, so we have to be sure that link target 1724b88c807SRodney W. Grimes * is not a directory, so we lstat and check 1734b88c807SRodney W. Grimes */ 1744b88c807SRodney W. Grimes if (lstat(arcn->ln_name, &sb) < 0) { 175778766feSKris Kennaway syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 1764b88c807SRodney W. Grimes arcn->name); 1774b88c807SRodney W. Grimes return(-1); 1784b88c807SRodney W. Grimes } 1794b88c807SRodney W. Grimes 1804b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 181778766feSKris Kennaway paxwarn(1, "A hard link to the directory %s is not allowed", 1824b88c807SRodney W. Grimes arcn->ln_name); 1834b88c807SRodney W. Grimes return(-1); 1844b88c807SRodney W. Grimes } 1854b88c807SRodney W. Grimes 1864b88c807SRodney W. Grimes return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 1874b88c807SRodney W. Grimes } 1884b88c807SRodney W. Grimes 1894b88c807SRodney W. Grimes /* 1904b88c807SRodney W. Grimes * cross_lnk() 1914b88c807SRodney W. Grimes * Create a hard link to arcn->org_name from arcn->name. Only used in copy 192778766feSKris Kennaway * with the -l flag. No warning or error if this does not succeed (we will 1934b88c807SRodney W. Grimes * then just create the file) 1944b88c807SRodney W. Grimes * Return: 1954b88c807SRodney W. Grimes * 1 if copy() should try to create this file node 1964b88c807SRodney W. Grimes * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 1974b88c807SRodney W. Grimes */ 1984b88c807SRodney W. Grimes 1994b88c807SRodney W. Grimes int 200f789b261SWarner Losh cross_lnk(ARCHD *arcn) 2014b88c807SRodney W. Grimes { 2024b88c807SRodney W. Grimes /* 20346be34b9SKris Kennaway * try to make a link to original file (-l flag in copy mode). make sure 2044b88c807SRodney W. Grimes * we do not try to link to directories in case we are running as root 2054b88c807SRodney W. Grimes * (and it might succeed). 2064b88c807SRodney W. Grimes */ 2074b88c807SRodney W. Grimes if (arcn->type == PAX_DIR) 2084b88c807SRodney W. Grimes return(1); 2094b88c807SRodney W. Grimes return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes /* 2134b88c807SRodney W. Grimes * chk_same() 2144b88c807SRodney W. Grimes * In copy mode if we are not trying to make hard links between the src 2154b88c807SRodney W. Grimes * and destinations, make sure we are not going to overwrite ourselves by 2164b88c807SRodney W. Grimes * accident. This slows things down a little, but we have to protect all 2174b88c807SRodney W. Grimes * those people who make typing errors. 2184b88c807SRodney W. Grimes * Return: 2194b88c807SRodney W. Grimes * 1 the target does not exist, go ahead and copy 2204b88c807SRodney W. Grimes * 0 skip it file exists (-k) or may be the same as source file 2214b88c807SRodney W. Grimes */ 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes int 224f789b261SWarner Losh chk_same(ARCHD *arcn) 2254b88c807SRodney W. Grimes { 2264b88c807SRodney W. Grimes struct stat sb; 2274b88c807SRodney W. Grimes 2284b88c807SRodney W. Grimes /* 2294b88c807SRodney W. Grimes * if file does not exist, return. if file exists and -k, skip it 2304b88c807SRodney W. Grimes * quietly 2314b88c807SRodney W. Grimes */ 2324b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) 2334b88c807SRodney W. Grimes return(1); 2344b88c807SRodney W. Grimes if (kflag) 2354b88c807SRodney W. Grimes return(0); 2364b88c807SRodney W. Grimes 2374b88c807SRodney W. Grimes /* 2384b88c807SRodney W. Grimes * better make sure the user does not have src == dest by mistake 2394b88c807SRodney W. Grimes */ 2404b88c807SRodney W. Grimes if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 241778766feSKris Kennaway paxwarn(1, "Unable to copy %s, file would overwrite itself", 2424b88c807SRodney W. Grimes arcn->name); 2434b88c807SRodney W. Grimes return(0); 2444b88c807SRodney W. Grimes } 2454b88c807SRodney W. Grimes return(1); 2464b88c807SRodney W. Grimes } 2474b88c807SRodney W. Grimes 2484b88c807SRodney W. Grimes /* 2494b88c807SRodney W. Grimes * mk_link() 2504b88c807SRodney W. Grimes * try to make a hard link between two files. if ign set, we do not 2514b88c807SRodney W. Grimes * complain. 2524b88c807SRodney W. Grimes * Return: 2534b88c807SRodney W. Grimes * 0 if successful (or we are done with this file but no error, such as 2544b88c807SRodney W. Grimes * finding the from file exists and the user has set -k). 2554b88c807SRodney W. Grimes * 1 when ign was set to indicates we could not make the link but we 2564b88c807SRodney W. Grimes * should try to copy/extract the file as that might work (and is an 2574b88c807SRodney W. Grimes * allowed option). -1 an error occurred. 2584b88c807SRodney W. Grimes */ 2594b88c807SRodney W. Grimes 2604b88c807SRodney W. Grimes static int 261f789b261SWarner Losh mk_link(char *to, struct stat *to_sb, char *from, 2624b88c807SRodney W. Grimes int ign) 2634b88c807SRodney W. Grimes { 2644b88c807SRodney W. Grimes struct stat sb; 2654b88c807SRodney W. Grimes int oerrno; 2664b88c807SRodney W. Grimes 2674b88c807SRodney W. Grimes /* 2684b88c807SRodney W. Grimes * if from file exists, it has to be unlinked to make the link. If the 2694b88c807SRodney W. Grimes * file exists and -k is set, skip it quietly 2704b88c807SRodney W. Grimes */ 2714b88c807SRodney W. Grimes if (lstat(from, &sb) == 0) { 2724b88c807SRodney W. Grimes if (kflag) 2734b88c807SRodney W. Grimes return(0); 2744b88c807SRodney W. Grimes 2754b88c807SRodney W. Grimes /* 2764b88c807SRodney W. Grimes * make sure it is not the same file, protect the user 2774b88c807SRodney W. Grimes */ 2784b88c807SRodney W. Grimes if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 279778766feSKris Kennaway paxwarn(1, "Unable to link file %s to itself", to); 280f2703795SRalf S. Engelschall return(-1); 2814b88c807SRodney W. Grimes } 2824b88c807SRodney W. Grimes 2834b88c807SRodney W. Grimes /* 2844b88c807SRodney W. Grimes * try to get rid of the file, based on the type 2854b88c807SRodney W. Grimes */ 2864b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 2874b88c807SRodney W. Grimes if (rmdir(from) < 0) { 288778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 2894b88c807SRodney W. Grimes return(-1); 2904b88c807SRodney W. Grimes } 2914b88c807SRodney W. Grimes } else if (unlink(from) < 0) { 2924b88c807SRodney W. Grimes if (!ign) { 293778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 2944b88c807SRodney W. Grimes return(-1); 2954b88c807SRodney W. Grimes } 2964b88c807SRodney W. Grimes return(1); 2974b88c807SRodney W. Grimes } 2984b88c807SRodney W. Grimes } 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes /* 3014b88c807SRodney W. Grimes * from file is gone (or did not exist), try to make the hard link. 3024b88c807SRodney W. Grimes * if it fails, check the path and try it again (if chk_path() says to 3034b88c807SRodney W. Grimes * try again) 3044b88c807SRodney W. Grimes */ 3054b88c807SRodney W. Grimes for (;;) { 3064b88c807SRodney W. Grimes if (link(to, from) == 0) 3074b88c807SRodney W. Grimes break; 3084b88c807SRodney W. Grimes oerrno = errno; 309b1787decSKris Kennaway if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 3104b88c807SRodney W. Grimes continue; 3114b88c807SRodney W. Grimes if (!ign) { 312778766feSKris Kennaway syswarn(1, oerrno, "Could not link to %s from %s", to, 3134b88c807SRodney W. Grimes from); 3144b88c807SRodney W. Grimes return(-1); 3154b88c807SRodney W. Grimes } 3164b88c807SRodney W. Grimes return(1); 3174b88c807SRodney W. Grimes } 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes /* 3204b88c807SRodney W. Grimes * all right the link was made 3214b88c807SRodney W. Grimes */ 3224b88c807SRodney W. Grimes return(0); 3234b88c807SRodney W. Grimes } 3244b88c807SRodney W. Grimes 3254b88c807SRodney W. Grimes /* 3264b88c807SRodney W. Grimes * node_creat() 3274b88c807SRodney W. Grimes * create an entry in the file system (other than a file or hard link). 3284b88c807SRodney W. Grimes * If successful, sets uid/gid modes and times as required. 3294b88c807SRodney W. Grimes * Return: 3304b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 3314b88c807SRodney W. Grimes */ 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes int 334f789b261SWarner Losh node_creat(ARCHD *arcn) 3354b88c807SRodney W. Grimes { 336f789b261SWarner Losh int res; 337f789b261SWarner Losh int ign = 0; 338f789b261SWarner Losh int oerrno; 339f789b261SWarner Losh int pass = 0; 3404b88c807SRodney W. Grimes mode_t file_mode; 3414b88c807SRodney W. Grimes struct stat sb; 3424b88c807SRodney W. Grimes 3434b88c807SRodney W. Grimes /* 3444b88c807SRodney W. Grimes * create node based on type, if that fails try to unlink the node and 3454b88c807SRodney W. Grimes * try again. finally check the path and try again. As noted in the 3464b88c807SRodney W. Grimes * file and link creation routines, this method seems to exhibit the 3474b88c807SRodney W. Grimes * best performance in general use workloads. 3484b88c807SRodney W. Grimes */ 3494b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 3504b88c807SRodney W. Grimes 3514b88c807SRodney W. Grimes for (;;) { 3524b88c807SRodney W. Grimes switch(arcn->type) { 3534b88c807SRodney W. Grimes case PAX_DIR: 3544b88c807SRodney W. Grimes res = mkdir(arcn->name, file_mode); 3554b88c807SRodney W. Grimes if (ign) 3564b88c807SRodney W. Grimes res = 0; 3574b88c807SRodney W. Grimes break; 3584b88c807SRodney W. Grimes case PAX_CHR: 3594b88c807SRodney W. Grimes file_mode |= S_IFCHR; 3604b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3614b88c807SRodney W. Grimes break; 3624b88c807SRodney W. Grimes case PAX_BLK: 3634b88c807SRodney W. Grimes file_mode |= S_IFBLK; 3644b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3654b88c807SRodney W. Grimes break; 3664b88c807SRodney W. Grimes case PAX_FIF: 3674b88c807SRodney W. Grimes res = mkfifo(arcn->name, file_mode); 3684b88c807SRodney W. Grimes break; 3694b88c807SRodney W. Grimes case PAX_SCK: 3704b88c807SRodney W. Grimes /* 3714b88c807SRodney W. Grimes * Skip sockets, operation has no meaning under BSD 3724b88c807SRodney W. Grimes */ 373778766feSKris Kennaway paxwarn(0, 3744b88c807SRodney W. Grimes "%s skipped. Sockets cannot be copied or extracted", 3754b88c807SRodney W. Grimes arcn->name); 3764b88c807SRodney W. Grimes return(-1); 3774b88c807SRodney W. Grimes case PAX_SLK: 378b1787decSKris Kennaway res = symlink(arcn->ln_name, arcn->name); 3794b88c807SRodney W. Grimes break; 3804b88c807SRodney W. Grimes case PAX_CTG: 3814b88c807SRodney W. Grimes case PAX_HLK: 3824b88c807SRodney W. Grimes case PAX_HRG: 3834b88c807SRodney W. Grimes case PAX_REG: 3844b88c807SRodney W. Grimes default: 3854b88c807SRodney W. Grimes /* 3864b88c807SRodney W. Grimes * we should never get here 3874b88c807SRodney W. Grimes */ 388778766feSKris Kennaway paxwarn(0, "%s has an unknown file type, skipping", 3894b88c807SRodney W. Grimes arcn->name); 3904b88c807SRodney W. Grimes return(-1); 3914b88c807SRodney W. Grimes } 3924b88c807SRodney W. Grimes 3934b88c807SRodney W. Grimes /* 3944b88c807SRodney W. Grimes * if we were able to create the node break out of the loop, 3954b88c807SRodney W. Grimes * otherwise try to unlink the node and try again. if that 3964b88c807SRodney W. Grimes * fails check the full path and try a final time. 3974b88c807SRodney W. Grimes */ 3984b88c807SRodney W. Grimes if (res == 0) 3994b88c807SRodney W. Grimes break; 4004b88c807SRodney W. Grimes 4014b88c807SRodney W. Grimes /* 4024b88c807SRodney W. Grimes * we failed to make the node 4034b88c807SRodney W. Grimes */ 4044b88c807SRodney W. Grimes oerrno = errno; 4054b88c807SRodney W. Grimes if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 4064b88c807SRodney W. Grimes return(-1); 4074b88c807SRodney W. Grimes 4084b88c807SRodney W. Grimes if (++pass <= 1) 4094b88c807SRodney W. Grimes continue; 4104b88c807SRodney W. Grimes 411b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 412778766feSKris Kennaway syswarn(1, oerrno, "Could not create: %s", arcn->name); 4134b88c807SRodney W. Grimes return(-1); 4144b88c807SRodney W. Grimes } 4154b88c807SRodney W. Grimes } 4164b88c807SRodney W. Grimes 4174b88c807SRodney W. Grimes /* 4184b88c807SRodney W. Grimes * we were able to create the node. set uid/gid, modes and times 4194b88c807SRodney W. Grimes */ 4204b88c807SRodney W. Grimes if (pids) 421db77c5b9STim Kientzle res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 4224b88c807SRodney W. Grimes else 4234b88c807SRodney W. Grimes res = 0; 4244b88c807SRodney W. Grimes 4254b88c807SRodney W. Grimes /* 4264b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 4274b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4284b88c807SRodney W. Grimes * set uid/gid bits 4294b88c807SRodney W. Grimes */ 4304b88c807SRodney W. Grimes if (!pmode || res) 4314b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 4324b88c807SRodney W. Grimes if (pmode) 4334b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 4344b88c807SRodney W. Grimes 435b1787decSKris Kennaway if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 4364b88c807SRodney W. Grimes /* 4374b88c807SRodney W. Grimes * Dirs must be processed again at end of extract to set times 4384b88c807SRodney W. Grimes * and modes to agree with those stored in the archive. However 4394b88c807SRodney W. Grimes * to allow extract to continue, we may have to also set owner 4404b88c807SRodney W. Grimes * rights. This allows nodes in the archive that are children 4414b88c807SRodney W. Grimes * of this directory to be extracted without failure. Both time 4424b88c807SRodney W. Grimes * and modes will be fixed after the entire archive is read and 4434b88c807SRodney W. Grimes * before pax exits. 4444b88c807SRodney W. Grimes */ 4454b88c807SRodney W. Grimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 4464b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) { 447778766feSKris Kennaway syswarn(0, errno,"Could not access %s (stat)", 4484b88c807SRodney W. Grimes arcn->name); 4494b88c807SRodney W. Grimes set_pmode(arcn->name,file_mode | S_IRWXU); 4504b88c807SRodney W. Grimes } else { 4514b88c807SRodney W. Grimes /* 4524b88c807SRodney W. Grimes * We have to add rights to the dir, so we make 4534b88c807SRodney W. Grimes * sure to restore the mode. The mode must be 4544b88c807SRodney W. Grimes * restored AS CREATED and not as stored if 4554b88c807SRodney W. Grimes * pmode is not set. 4564b88c807SRodney W. Grimes */ 4574b88c807SRodney W. Grimes set_pmode(arcn->name, 4584b88c807SRodney W. Grimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 4594b88c807SRodney W. Grimes if (!pmode) 4604b88c807SRodney W. Grimes arcn->sb.st_mode = sb.st_mode; 4614b88c807SRodney W. Grimes } 4624b88c807SRodney W. Grimes 4634b88c807SRodney W. Grimes /* 4644b88c807SRodney W. Grimes * we have to force the mode to what was set here, 4654b88c807SRodney W. Grimes * since we changed it from the default as created. 4664b88c807SRodney W. Grimes */ 4674b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 4684b88c807SRodney W. Grimes } else if (pmode || patime || pmtime) 4694b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 4704b88c807SRodney W. Grimes } 4714b88c807SRodney W. Grimes 4724b88c807SRodney W. Grimes if (patime || pmtime) 4734b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 4744b88c807SRodney W. Grimes return(0); 4754b88c807SRodney W. Grimes } 4764b88c807SRodney W. Grimes 4774b88c807SRodney W. Grimes /* 4784b88c807SRodney W. Grimes * unlnk_exist() 4794b88c807SRodney W. Grimes * Remove node from file system with the specified name. We pass the type 4804b88c807SRodney W. Grimes * of the node that is going to replace it. When we try to create a 4814b88c807SRodney W. Grimes * directory and find that it already exists, we allow processing to 4824b88c807SRodney W. Grimes * continue as proper modes etc will always be set for it later on. 4834b88c807SRodney W. Grimes * Return: 4844b88c807SRodney W. Grimes * 0 is ok to proceed, no file with the specified name exists 4854b88c807SRodney W. Grimes * -1 we were unable to remove the node, or we should not remove it (-k) 4864b88c807SRodney W. Grimes * 1 we found a directory and we were going to create a directory. 4874b88c807SRodney W. Grimes */ 4884b88c807SRodney W. Grimes 4894b88c807SRodney W. Grimes int 490f789b261SWarner Losh unlnk_exist(char *name, int type) 4914b88c807SRodney W. Grimes { 4924b88c807SRodney W. Grimes struct stat sb; 4934b88c807SRodney W. Grimes 4944b88c807SRodney W. Grimes /* 4954b88c807SRodney W. Grimes * the file does not exist, or -k we are done 4964b88c807SRodney W. Grimes */ 4974b88c807SRodney W. Grimes if (lstat(name, &sb) < 0) 4984b88c807SRodney W. Grimes return(0); 4994b88c807SRodney W. Grimes if (kflag) 5004b88c807SRodney W. Grimes return(-1); 5014b88c807SRodney W. Grimes 5024b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 5034b88c807SRodney W. Grimes /* 5044b88c807SRodney W. Grimes * try to remove a directory, if it fails and we were going to 5054b88c807SRodney W. Grimes * create a directory anyway, tell the caller (return a 1) 5064b88c807SRodney W. Grimes */ 5074b88c807SRodney W. Grimes if (rmdir(name) < 0) { 5084b88c807SRodney W. Grimes if (type == PAX_DIR) 5094b88c807SRodney W. Grimes return(1); 510778766feSKris Kennaway syswarn(1,errno,"Unable to remove directory %s", name); 5114b88c807SRodney W. Grimes return(-1); 5124b88c807SRodney W. Grimes } 5134b88c807SRodney W. Grimes return(0); 5144b88c807SRodney W. Grimes } 5154b88c807SRodney W. Grimes 5164b88c807SRodney W. Grimes /* 5174b88c807SRodney W. Grimes * try to get rid of all non-directory type nodes 5184b88c807SRodney W. Grimes */ 5194b88c807SRodney W. Grimes if (unlink(name) < 0) { 520778766feSKris Kennaway syswarn(1, errno, "Could not unlink %s", name); 5214b88c807SRodney W. Grimes return(-1); 5224b88c807SRodney W. Grimes } 5234b88c807SRodney W. Grimes return(0); 5244b88c807SRodney W. Grimes } 5254b88c807SRodney W. Grimes 5264b88c807SRodney W. Grimes /* 5274b88c807SRodney W. Grimes * chk_path() 5284b88c807SRodney W. Grimes * We were trying to create some kind of node in the file system and it 5294b88c807SRodney W. Grimes * failed. chk_path() makes sure the path up to the node exists and is 5304b88c807SRodney W. Grimes * writeable. When we have to create a directory that is missing along the 5314b88c807SRodney W. Grimes * path somewhere, the directory we create will be set to the same 5324b88c807SRodney W. Grimes * uid/gid as the file has (when uid and gid are being preserved). 5334b88c807SRodney W. Grimes * NOTE: this routine is a real performance loss. It is only used as a 5344b88c807SRodney W. Grimes * last resort when trying to create entries in the file system. 5354b88c807SRodney W. Grimes * Return: 5364b88c807SRodney W. Grimes * -1 when it could find nothing it is allowed to fix. 5374b88c807SRodney W. Grimes * 0 otherwise 5384b88c807SRodney W. Grimes */ 5394b88c807SRodney W. Grimes 5404b88c807SRodney W. Grimes int 541f789b261SWarner Losh chk_path( char *name, uid_t st_uid, gid_t st_gid) 5424b88c807SRodney W. Grimes { 543f789b261SWarner Losh char *spt = name; 5444b88c807SRodney W. Grimes struct stat sb; 5454b88c807SRodney W. Grimes int retval = -1; 5464b88c807SRodney W. Grimes 5474b88c807SRodney W. Grimes /* 5484b88c807SRodney W. Grimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 5494b88c807SRodney W. Grimes */ 5504b88c807SRodney W. Grimes if (*spt == '/') 5514b88c807SRodney W. Grimes ++spt; 5524b88c807SRodney W. Grimes 5534b88c807SRodney W. Grimes for(;;) { 5544b88c807SRodney W. Grimes /* 55501c99176SUlrich Spörlein * work forward from the first / and check each part of the path 5564b88c807SRodney W. Grimes */ 5574b88c807SRodney W. Grimes spt = strchr(spt, '/'); 5584b88c807SRodney W. Grimes if (spt == NULL) 5594b88c807SRodney W. Grimes break; 5604b88c807SRodney W. Grimes *spt = '\0'; 5614b88c807SRodney W. Grimes 5624b88c807SRodney W. Grimes /* 5634b88c807SRodney W. Grimes * if it exists we assume it is a directory, it is not within 5644b88c807SRodney W. Grimes * the spec (at least it seems to read that way) to alter the 5654b88c807SRodney W. Grimes * file system for nodes NOT EXPLICITLY stored on the archive. 5664b88c807SRodney W. Grimes * If that assumption is changed, you would test the node here 5674b88c807SRodney W. Grimes * and figure out how to get rid of it (probably like some 5684b88c807SRodney W. Grimes * recursive unlink()) or fix up the directory permissions if 5694b88c807SRodney W. Grimes * required (do an access()). 5704b88c807SRodney W. Grimes */ 5714b88c807SRodney W. Grimes if (lstat(name, &sb) == 0) { 5724b88c807SRodney W. Grimes *(spt++) = '/'; 5734b88c807SRodney W. Grimes continue; 5744b88c807SRodney W. Grimes } 5754b88c807SRodney W. Grimes 5764b88c807SRodney W. Grimes /* 5774b88c807SRodney W. Grimes * the path fails at this point, see if we can create the 5784b88c807SRodney W. Grimes * needed directory and continue on 5794b88c807SRodney W. Grimes */ 5804b88c807SRodney W. Grimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 5814b88c807SRodney W. Grimes *spt = '/'; 5824b88c807SRodney W. Grimes retval = -1; 5834b88c807SRodney W. Grimes break; 5844b88c807SRodney W. Grimes } 5854b88c807SRodney W. Grimes 5864b88c807SRodney W. Grimes /* 5874b88c807SRodney W. Grimes * we were able to create the directory. We will tell the 5884b88c807SRodney W. Grimes * caller that we found something to fix, and it is ok to try 5894b88c807SRodney W. Grimes * and create the node again. 5904b88c807SRodney W. Grimes */ 5914b88c807SRodney W. Grimes retval = 0; 5924b88c807SRodney W. Grimes if (pids) 5934b88c807SRodney W. Grimes (void)set_ids(name, st_uid, st_gid); 5944b88c807SRodney W. Grimes 5954b88c807SRodney W. Grimes /* 59601c99176SUlrich Spörlein * make sure the user doesn't have some strange umask that 5974b88c807SRodney W. Grimes * causes this newly created directory to be unusable. We fix 5984b88c807SRodney W. Grimes * the modes and restore them back to the creation default at 5994b88c807SRodney W. Grimes * the end of pax 6004b88c807SRodney W. Grimes */ 6014b88c807SRodney W. Grimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6024b88c807SRodney W. Grimes (lstat(name, &sb) == 0)) { 6034b88c807SRodney W. Grimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6044b88c807SRodney W. Grimes add_dir(name, spt - name, &sb, 1); 6054b88c807SRodney W. Grimes } 6064b88c807SRodney W. Grimes *(spt++) = '/'; 6074b88c807SRodney W. Grimes continue; 6084b88c807SRodney W. Grimes } 6094b88c807SRodney W. Grimes return(retval); 6104b88c807SRodney W. Grimes } 6114b88c807SRodney W. Grimes 6124b88c807SRodney W. Grimes /* 6134b88c807SRodney W. Grimes * set_ftime() 6144b88c807SRodney W. Grimes * Set the access time and modification time for a named file. If frc is 615778766feSKris Kennaway * non-zero we force these times to be set even if the user did not 6164b88c807SRodney W. Grimes * request access and/or modification time preservation (this is also 6174b88c807SRodney W. Grimes * used by -t to reset access times). 6180266a5d6SDag-Erling Smørgrav * When frc is zero, only those times the user has asked for are set, the 6194b88c807SRodney W. Grimes * other ones are left alone. We do not assume the un-documented feature 620db77c5b9STim Kientzle * of many lutimes() implementations that consider a 0 time value as a do 6214b88c807SRodney W. Grimes * not set request. 6224b88c807SRodney W. Grimes */ 6234b88c807SRodney W. Grimes 6244b88c807SRodney W. Grimes void 6254b88c807SRodney W. Grimes set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6264b88c807SRodney W. Grimes { 6274b88c807SRodney W. Grimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 6284b88c807SRodney W. Grimes struct stat sb; 6294b88c807SRodney W. Grimes 6306e63a104SMatthew Dillon tv[0].tv_sec = atime; 6316e63a104SMatthew Dillon tv[1].tv_sec = mtime; 6324b88c807SRodney W. Grimes if (!frc && (!patime || !pmtime)) { 6334b88c807SRodney W. Grimes /* 6344b88c807SRodney W. Grimes * if we are not forcing, only set those times the user wants 6354b88c807SRodney W. Grimes * set. We get the current values of the times if we need them. 6364b88c807SRodney W. Grimes */ 6374b88c807SRodney W. Grimes if (lstat(fnm, &sb) == 0) { 6384b88c807SRodney W. Grimes if (!patime) 6396e63a104SMatthew Dillon tv[0].tv_sec = sb.st_atime; 6404b88c807SRodney W. Grimes if (!pmtime) 6416e63a104SMatthew Dillon tv[1].tv_sec = sb.st_mtime; 6424b88c807SRodney W. Grimes } else 643778766feSKris Kennaway syswarn(0,errno,"Unable to obtain file stats %s", fnm); 6444b88c807SRodney W. Grimes } 6454b88c807SRodney W. Grimes 6464b88c807SRodney W. Grimes /* 6474b88c807SRodney W. Grimes * set the times 6484b88c807SRodney W. Grimes */ 649db77c5b9STim Kientzle if (lutimes(fnm, tv) < 0) 650778766feSKris Kennaway syswarn(1, errno, "Access/modification time set failed on: %s", 6514b88c807SRodney W. Grimes fnm); 6524b88c807SRodney W. Grimes return; 6534b88c807SRodney W. Grimes } 6544b88c807SRodney W. Grimes 6554b88c807SRodney W. Grimes /* 6564b88c807SRodney W. Grimes * set_ids() 6574b88c807SRodney W. Grimes * set the uid and gid of a file system node 6584b88c807SRodney W. Grimes * Return: 6594b88c807SRodney W. Grimes * 0 when set, -1 on failure 6604b88c807SRodney W. Grimes */ 6614b88c807SRodney W. Grimes 6624b88c807SRodney W. Grimes int 6634b88c807SRodney W. Grimes set_ids(char *fnm, uid_t uid, gid_t gid) 6644b88c807SRodney W. Grimes { 665b1787decSKris Kennaway if (lchown(fnm, uid, gid) < 0) { 666b1787decSKris Kennaway /* 667b1787decSKris Kennaway * ignore EPERM unless in verbose mode or being run by root. 668b1787decSKris Kennaway * if running as pax, POSIX requires a warning. 669b1787decSKris Kennaway */ 670b1787decSKris Kennaway if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 671b1787decSKris Kennaway geteuid() == 0) 672b1787decSKris Kennaway syswarn(1, errno, "Unable to set file uid/gid of %s", 673b1787decSKris Kennaway fnm); 6744b88c807SRodney W. Grimes return(-1); 6754b88c807SRodney W. Grimes } 6764b88c807SRodney W. Grimes return(0); 6774b88c807SRodney W. Grimes } 6784b88c807SRodney W. Grimes 6794b88c807SRodney W. Grimes /* 6804b88c807SRodney W. Grimes * set_pmode() 6814b88c807SRodney W. Grimes * Set file access mode 6824b88c807SRodney W. Grimes */ 6834b88c807SRodney W. Grimes 6844b88c807SRodney W. Grimes void 6854b88c807SRodney W. Grimes set_pmode(char *fnm, mode_t mode) 6864b88c807SRodney W. Grimes { 6874b88c807SRodney W. Grimes mode &= ABITS; 688db77c5b9STim Kientzle if (lchmod(fnm, mode) < 0) 689778766feSKris Kennaway syswarn(1, errno, "Could not set permissions on %s", fnm); 6904b88c807SRodney W. Grimes return; 6914b88c807SRodney W. Grimes } 6924b88c807SRodney W. Grimes 6934b88c807SRodney W. Grimes /* 6944b88c807SRodney W. Grimes * file_write() 6954b88c807SRodney W. Grimes * Write/copy a file (during copy or archive extract). This routine knows 6964b88c807SRodney W. Grimes * how to copy files with lseek holes in it. (Which are read as file 6974b88c807SRodney W. Grimes * blocks containing all 0's but do not have any file blocks associated 6984b88c807SRodney W. Grimes * with the data). Typical examples of these are files created by dbm 6994b88c807SRodney W. Grimes * variants (.pag files). While the file size of these files are huge, the 7004b88c807SRodney W. Grimes * actual storage is quite small (the files are sparse). The problem is 7014b88c807SRodney W. Grimes * the holes read as all zeros so are probably stored on the archive that 7024b88c807SRodney W. Grimes * way (there is no way to determine if the file block is really a hole, 7034b88c807SRodney W. Grimes * we only know that a file block of all zero's can be a hole). 7044b88c807SRodney W. Grimes * At this writing, no major archive format knows how to archive files 7054b88c807SRodney W. Grimes * with holes. However, on extraction (or during copy, -rw) we have to 7064b88c807SRodney W. Grimes * deal with these files. Without detecting the holes, the files can 7074b88c807SRodney W. Grimes * consume a lot of file space if just written to disk. This replacement 7084b88c807SRodney W. Grimes * for write when passed the basic allocation size of a file system block, 7094b88c807SRodney W. Grimes * uses lseek whenever it detects the input data is all 0 within that 7104b88c807SRodney W. Grimes * file block. In more detail, the strategy is as follows: 7114b88c807SRodney W. Grimes * While the input is all zero keep doing an lseek. Keep track of when we 71201c99176SUlrich Spörlein * pass over file block boundaries. Only write when we hit a non zero 7134b88c807SRodney W. Grimes * input. once we have written a file block, we continue to write it to 7144b88c807SRodney W. Grimes * the end (we stop looking at the input). When we reach the start of the 7154b88c807SRodney W. Grimes * next file block, start checking for zero blocks again. Working on file 71601c99176SUlrich Spörlein * block boundaries significantly reduces the overhead when copying files 7174b88c807SRodney W. Grimes * that are NOT very sparse. This overhead (when compared to a write) is 7184b88c807SRodney W. Grimes * almost below the measurement resolution on many systems. Without it, 719*711a74e1SHUNG-CHI CHANG * files with holes cannot be safely copied. It does have a side effect as 7204b88c807SRodney W. Grimes * it can put holes into files that did not have them before, but that is 7214b88c807SRodney W. Grimes * not a problem since the file contents are unchanged (in fact it saves 7224b88c807SRodney W. Grimes * file space). (Except on paging files for diskless clients. But since we 7234b88c807SRodney W. Grimes * cannot determine one of those file from here, we ignore them). If this 7244b88c807SRodney W. Grimes * ever ends up on a system where CTG files are supported and the holes 7254b88c807SRodney W. Grimes * are not desired, just do a conditional test in those routines that 7264b88c807SRodney W. Grimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 7274b88c807SRodney W. Grimes * FILE, make sure to call file_flush() when the last write finishes with 7284b88c807SRodney W. Grimes * an empty block. A lot of file systems will not create an lseek hole at 7294b88c807SRodney W. Grimes * the end. In this case we drop a single 0 at the end to force the 7304b88c807SRodney W. Grimes * trailing 0's in the file. 7314b88c807SRodney W. Grimes * ---Parameters--- 7324b88c807SRodney W. Grimes * rem: how many bytes left in this file system block 7334b88c807SRodney W. Grimes * isempt: have we written to the file block yet (is it empty) 7344b88c807SRodney W. Grimes * sz: basic file block allocation size 7354b88c807SRodney W. Grimes * cnt: number of bytes on this write 7364b88c807SRodney W. Grimes * str: buffer to write 7374b88c807SRodney W. Grimes * Return: 7384b88c807SRodney W. Grimes * number of bytes written, -1 on write (or lseek) error. 7394b88c807SRodney W. Grimes */ 7404b88c807SRodney W. Grimes 7414b88c807SRodney W. Grimes int 742f789b261SWarner Losh file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 7434b88c807SRodney W. Grimes char *name) 7444b88c807SRodney W. Grimes { 745f789b261SWarner Losh char *pt; 746f789b261SWarner Losh char *end; 747f789b261SWarner Losh int wcnt; 748f789b261SWarner Losh char *st = str; 7494b88c807SRodney W. Grimes 7504b88c807SRodney W. Grimes /* 7514b88c807SRodney W. Grimes * while we have data to process 7524b88c807SRodney W. Grimes */ 7534b88c807SRodney W. Grimes while (cnt) { 7544b88c807SRodney W. Grimes if (!*rem) { 7554b88c807SRodney W. Grimes /* 7564b88c807SRodney W. Grimes * We are now at the start of file system block again 7574b88c807SRodney W. Grimes * (or what we think one is...). start looking for 7584b88c807SRodney W. Grimes * empty blocks again 7594b88c807SRodney W. Grimes */ 7604b88c807SRodney W. Grimes *isempt = 1; 7614b88c807SRodney W. Grimes *rem = sz; 7624b88c807SRodney W. Grimes } 7634b88c807SRodney W. Grimes 7644b88c807SRodney W. Grimes /* 7654b88c807SRodney W. Grimes * only examine up to the end of the current file block or 7664b88c807SRodney W. Grimes * remaining characters to write, whatever is smaller 7674b88c807SRodney W. Grimes */ 7684b88c807SRodney W. Grimes wcnt = MIN(cnt, *rem); 7694b88c807SRodney W. Grimes cnt -= wcnt; 7704b88c807SRodney W. Grimes *rem -= wcnt; 7714b88c807SRodney W. Grimes if (*isempt) { 7724b88c807SRodney W. Grimes /* 7734b88c807SRodney W. Grimes * have not written to this block yet, so we keep 7744b88c807SRodney W. Grimes * looking for zero's 7754b88c807SRodney W. Grimes */ 7764b88c807SRodney W. Grimes pt = st; 7774b88c807SRodney W. Grimes end = st + wcnt; 7784b88c807SRodney W. Grimes 7794b88c807SRodney W. Grimes /* 7804b88c807SRodney W. Grimes * look for a zero filled buffer 7814b88c807SRodney W. Grimes */ 7824b88c807SRodney W. Grimes while ((pt < end) && (*pt == '\0')) 7834b88c807SRodney W. Grimes ++pt; 7844b88c807SRodney W. Grimes 7854b88c807SRodney W. Grimes if (pt == end) { 7864b88c807SRodney W. Grimes /* 7874b88c807SRodney W. Grimes * skip, buf is empty so far 7884b88c807SRodney W. Grimes */ 7894b88c807SRodney W. Grimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 790778766feSKris Kennaway syswarn(1,errno,"File seek on %s", 7914b88c807SRodney W. Grimes name); 7924b88c807SRodney W. Grimes return(-1); 7934b88c807SRodney W. Grimes } 7944b88c807SRodney W. Grimes st = pt; 7954b88c807SRodney W. Grimes continue; 7964b88c807SRodney W. Grimes } 7974b88c807SRodney W. Grimes /* 7984b88c807SRodney W. Grimes * drat, the buf is not zero filled 7994b88c807SRodney W. Grimes */ 8004b88c807SRodney W. Grimes *isempt = 0; 8014b88c807SRodney W. Grimes } 8024b88c807SRodney W. Grimes 8034b88c807SRodney W. Grimes /* 8044b88c807SRodney W. Grimes * have non-zero data in this file system block, have to write 8054b88c807SRodney W. Grimes */ 8064b88c807SRodney W. Grimes if (write(fd, st, wcnt) != wcnt) { 807778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", name); 8084b88c807SRodney W. Grimes return(-1); 8094b88c807SRodney W. Grimes } 8104b88c807SRodney W. Grimes st += wcnt; 8114b88c807SRodney W. Grimes } 8124b88c807SRodney W. Grimes return(st - str); 8134b88c807SRodney W. Grimes } 8144b88c807SRodney W. Grimes 8154b88c807SRodney W. Grimes /* 8164b88c807SRodney W. Grimes * file_flush() 8174b88c807SRodney W. Grimes * when the last file block in a file is zero, many file systems will not 8184b88c807SRodney W. Grimes * let us create a hole at the end. To get the last block with zeros, we 8194b88c807SRodney W. Grimes * write the last BYTE with a zero (back up one byte and write a zero). 8204b88c807SRodney W. Grimes */ 8214b88c807SRodney W. Grimes 8224b88c807SRodney W. Grimes void 8234b88c807SRodney W. Grimes file_flush(int fd, char *fname, int isempt) 8244b88c807SRodney W. Grimes { 8254b88c807SRodney W. Grimes static char blnk[] = "\0"; 8264b88c807SRodney W. Grimes 8274b88c807SRodney W. Grimes /* 8284b88c807SRodney W. Grimes * silly test, but make sure we are only called when the last block is 8294b88c807SRodney W. Grimes * filled with all zeros. 8304b88c807SRodney W. Grimes */ 8314b88c807SRodney W. Grimes if (!isempt) 8324b88c807SRodney W. Grimes return; 8334b88c807SRodney W. Grimes 8344b88c807SRodney W. Grimes /* 8354b88c807SRodney W. Grimes * move back one byte and write a zero 8364b88c807SRodney W. Grimes */ 8374b88c807SRodney W. Grimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 838778766feSKris Kennaway syswarn(1, errno, "Failed seek on file %s", fname); 8394b88c807SRodney W. Grimes return; 8404b88c807SRodney W. Grimes } 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes if (write(fd, blnk, 1) < 0) 843778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", fname); 8444b88c807SRodney W. Grimes return; 8454b88c807SRodney W. Grimes } 8464b88c807SRodney W. Grimes 8474b88c807SRodney W. Grimes /* 8484b88c807SRodney W. Grimes * rdfile_close() 8494b88c807SRodney W. Grimes * close a file we have beed reading (to copy or archive). If we have to 8504b88c807SRodney W. Grimes * reset access time (tflag) do so (the times are stored in arcn). 8514b88c807SRodney W. Grimes */ 8524b88c807SRodney W. Grimes 8534b88c807SRodney W. Grimes void 854f789b261SWarner Losh rdfile_close(ARCHD *arcn, int *fd) 8554b88c807SRodney W. Grimes { 8564b88c807SRodney W. Grimes /* 8574b88c807SRodney W. Grimes * make sure the file is open 8584b88c807SRodney W. Grimes */ 8594b88c807SRodney W. Grimes if (*fd < 0) 8604b88c807SRodney W. Grimes return; 8614b88c807SRodney W. Grimes 8624b88c807SRodney W. Grimes (void)close(*fd); 8634b88c807SRodney W. Grimes *fd = -1; 8644b88c807SRodney W. Grimes if (!tflag) 8654b88c807SRodney W. Grimes return; 8664b88c807SRodney W. Grimes 8674b88c807SRodney W. Grimes /* 8684b88c807SRodney W. Grimes * user wants last access time reset 8694b88c807SRodney W. Grimes */ 8704b88c807SRodney W. Grimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 8714b88c807SRodney W. Grimes return; 8724b88c807SRodney W. Grimes } 8734b88c807SRodney W. Grimes 8744b88c807SRodney W. Grimes /* 8754b88c807SRodney W. Grimes * set_crc() 8764b88c807SRodney W. Grimes * read a file to calculate its crc. This is a real drag. Archive formats 8774b88c807SRodney W. Grimes * that have this, end up reading the file twice (we have to write the 8784b88c807SRodney W. Grimes * header WITH the crc before writing the file contents. Oh well... 8794b88c807SRodney W. Grimes * Return: 8804b88c807SRodney W. Grimes * 0 if was able to calculate the crc, -1 otherwise 8814b88c807SRodney W. Grimes */ 8824b88c807SRodney W. Grimes 8834b88c807SRodney W. Grimes int 884f789b261SWarner Losh set_crc(ARCHD *arcn, int fd) 8854b88c807SRodney W. Grimes { 886f789b261SWarner Losh int i; 887f789b261SWarner Losh int res; 8884b88c807SRodney W. Grimes off_t cpcnt = 0L; 8894b88c807SRodney W. Grimes u_long size; 8904b88c807SRodney W. Grimes unsigned long crc = 0L; 8914b88c807SRodney W. Grimes char tbuf[FILEBLK]; 8924b88c807SRodney W. Grimes struct stat sb; 8934b88c807SRodney W. Grimes 8944b88c807SRodney W. Grimes if (fd < 0) { 8954b88c807SRodney W. Grimes /* 8964b88c807SRodney W. Grimes * hmm, no fd, should never happen. well no crc then. 8974b88c807SRodney W. Grimes */ 8984b88c807SRodney W. Grimes arcn->crc = 0L; 8994b88c807SRodney W. Grimes return(0); 9004b88c807SRodney W. Grimes } 9014b88c807SRodney W. Grimes 9024b88c807SRodney W. Grimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 9034b88c807SRodney W. Grimes size = (u_long)sizeof(tbuf); 9044b88c807SRodney W. Grimes 9054b88c807SRodney W. Grimes /* 9064b88c807SRodney W. Grimes * read all the bytes we think that there are in the file. If the user 9074b88c807SRodney W. Grimes * is trying to archive an active file, forget this file. 9084b88c807SRodney W. Grimes */ 9094b88c807SRodney W. Grimes for(;;) { 9104b88c807SRodney W. Grimes if ((res = read(fd, tbuf, size)) <= 0) 9114b88c807SRodney W. Grimes break; 9124b88c807SRodney W. Grimes cpcnt += res; 9134b88c807SRodney W. Grimes for (i = 0; i < res; ++i) 9144b88c807SRodney W. Grimes crc += (tbuf[i] & 0xff); 9154b88c807SRodney W. Grimes } 9164b88c807SRodney W. Grimes 9174b88c807SRodney W. Grimes /* 9184b88c807SRodney W. Grimes * safety check. we want to avoid archiving files that are active as 91901c99176SUlrich Spörlein * they can create inconsistent archive copies. 9204b88c807SRodney W. Grimes */ 9214b88c807SRodney W. Grimes if (cpcnt != arcn->sb.st_size) 922778766feSKris Kennaway paxwarn(1, "File changed size %s", arcn->org_name); 9234b88c807SRodney W. Grimes else if (fstat(fd, &sb) < 0) 924778766feSKris Kennaway syswarn(1, errno, "Failed stat on %s", arcn->org_name); 9254b88c807SRodney W. Grimes else if (arcn->sb.st_mtime != sb.st_mtime) 926778766feSKris Kennaway paxwarn(1, "File %s was modified during read", arcn->org_name); 9274b88c807SRodney W. Grimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 928778766feSKris Kennaway syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 9294b88c807SRodney W. Grimes else { 9304b88c807SRodney W. Grimes arcn->crc = crc; 9314b88c807SRodney W. Grimes return(0); 9324b88c807SRodney W. Grimes } 9334b88c807SRodney W. Grimes return(-1); 9344b88c807SRodney W. Grimes } 935