14b88c807SRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro 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 #ifndef lint 37c9a8d1f4SPhilippe Charnier #if 0 38c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 39c9a8d1f4SPhilippe Charnier #endif 404b88c807SRodney W. Grimes #endif /* not lint */ 412749b141SDavid E. O'Brien #include <sys/cdefs.h> 422749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 434b88c807SRodney W. Grimes 444b88c807SRodney W. Grimes #include <sys/types.h> 454b88c807SRodney W. Grimes #include <sys/time.h> 464b88c807SRodney W. Grimes #include <sys/stat.h> 474b88c807SRodney W. Grimes #include <unistd.h> 484b88c807SRodney W. Grimes #include <fcntl.h> 494b88c807SRodney W. Grimes #include <string.h> 504b88c807SRodney W. Grimes #include <stdio.h> 514b88c807SRodney W. Grimes #include <errno.h> 524b88c807SRodney W. Grimes #include <sys/uio.h> 534b88c807SRodney W. Grimes #include "pax.h" 54b1787decSKris Kennaway #include "options.h" 554b88c807SRodney W. Grimes #include "extern.h" 564b88c807SRodney W. Grimes 574b88c807SRodney W. Grimes static int 58f789b261SWarner Losh mk_link(char *,struct stat *,char *, int); 594b88c807SRodney W. Grimes 604b88c807SRodney W. Grimes /* 614b88c807SRodney W. Grimes * routines that deal with file operations such as: creating, removing; 624b88c807SRodney W. Grimes * and setting access modes, uid/gid and times of files 634b88c807SRodney W. Grimes */ 644b88c807SRodney W. Grimes 654b88c807SRodney W. Grimes #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 664b88c807SRodney W. Grimes #define SETBITS (S_ISUID | S_ISGID) 674b88c807SRodney W. Grimes #define ABITS (FILEBITS | SETBITS) 684b88c807SRodney W. Grimes 694b88c807SRodney W. Grimes /* 704b88c807SRodney W. Grimes * file_creat() 714b88c807SRodney W. Grimes * Create and open a file. 724b88c807SRodney W. Grimes * Return: 734b88c807SRodney W. Grimes * file descriptor or -1 for failure 744b88c807SRodney W. Grimes */ 754b88c807SRodney W. Grimes 764b88c807SRodney W. Grimes int 77f789b261SWarner Losh file_creat(ARCHD *arcn) 784b88c807SRodney W. Grimes { 794b88c807SRodney W. Grimes int fd = -1; 804b88c807SRodney W. Grimes mode_t file_mode; 814b88c807SRodney W. Grimes int oerrno; 824b88c807SRodney W. Grimes 834b88c807SRodney W. Grimes /* 844b88c807SRodney W. Grimes * assume file doesn't exist, so just try to create it, most times this 854b88c807SRodney W. Grimes * works. We have to take special handling when the file does exist. To 864b88c807SRodney W. Grimes * detect this, we use O_EXCL. For example when trying to create a 874b88c807SRodney W. Grimes * file and a character device or fifo exists with the same name, we 8801c99176SUlrich Spörlein * can accidentally open the device by mistake (or block waiting to 8901c99176SUlrich Spörlein * open). If we find that the open has failed, then spend the effort 9001c99176SUlrich Spörlein * to figure out why. This strategy was found to have better average 914b88c807SRodney W. Grimes * performance in common use than checking the file (and the path) 924b88c807SRodney W. Grimes * first with lstat. 934b88c807SRodney W. Grimes */ 944b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 954b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 964b88c807SRodney W. Grimes file_mode)) >= 0) 974b88c807SRodney W. Grimes return(fd); 984b88c807SRodney W. Grimes 994b88c807SRodney W. Grimes /* 1004b88c807SRodney W. Grimes * the file seems to exist. First we try to get rid of it (found to be 1014b88c807SRodney W. Grimes * the second most common failure when traced). If this fails, only 1024b88c807SRodney W. Grimes * then we go to the expense to check and create the path to the file 1034b88c807SRodney W. Grimes */ 1044b88c807SRodney W. Grimes if (unlnk_exist(arcn->name, arcn->type) != 0) 1054b88c807SRodney W. Grimes return(-1); 1064b88c807SRodney W. Grimes 1074b88c807SRodney W. Grimes for (;;) { 1084b88c807SRodney W. Grimes /* 1094b88c807SRodney W. Grimes * try to open it again, if this fails, check all the nodes in 1104b88c807SRodney W. Grimes * the path and give it a final try. if chk_path() finds that 1114b88c807SRodney W. Grimes * it cannot fix anything, we will skip the last attempt 1124b88c807SRodney W. Grimes */ 1134b88c807SRodney W. Grimes if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 1144b88c807SRodney W. Grimes file_mode)) >= 0) 1154b88c807SRodney W. Grimes break; 1164b88c807SRodney W. Grimes oerrno = errno; 117b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 118778766feSKris Kennaway syswarn(1, oerrno, "Unable to create %s", arcn->name); 1194b88c807SRodney W. Grimes return(-1); 1204b88c807SRodney W. Grimes } 1214b88c807SRodney W. Grimes } 1224b88c807SRodney W. Grimes return(fd); 1234b88c807SRodney W. Grimes } 1244b88c807SRodney W. Grimes 1254b88c807SRodney W. Grimes /* 1264b88c807SRodney W. Grimes * file_close() 1274b88c807SRodney W. Grimes * Close file descriptor to a file just created by pax. Sets modes, 1284b88c807SRodney W. Grimes * ownership and times as required. 1294b88c807SRodney W. Grimes * Return: 1304b88c807SRodney W. Grimes * 0 for success, -1 for failure 1314b88c807SRodney W. Grimes */ 1324b88c807SRodney W. Grimes 1334b88c807SRodney W. Grimes void 134f789b261SWarner Losh file_close(ARCHD *arcn, int fd) 1354b88c807SRodney W. Grimes { 1364b88c807SRodney W. Grimes int res = 0; 1374b88c807SRodney W. Grimes 1384b88c807SRodney W. Grimes if (fd < 0) 1394b88c807SRodney W. Grimes return; 1404b88c807SRodney W. Grimes if (close(fd) < 0) 141778766feSKris Kennaway syswarn(0, errno, "Unable to close file descriptor on %s", 1424b88c807SRodney W. Grimes arcn->name); 1434b88c807SRodney W. Grimes 1444b88c807SRodney W. Grimes /* 1454b88c807SRodney W. Grimes * set owner/groups first as this may strip off mode bits we want 1464b88c807SRodney W. Grimes * then set file permission modes. Then set file access and 1474b88c807SRodney W. Grimes * modification times. 1484b88c807SRodney W. Grimes */ 1494b88c807SRodney W. Grimes if (pids) 1504b88c807SRodney W. Grimes res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes /* 1534b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 1544b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT 1554b88c807SRodney W. Grimes * set uid/gid bits 1564b88c807SRodney W. Grimes */ 1574b88c807SRodney W. Grimes if (!pmode || res) 1584b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 1594b88c807SRodney W. Grimes if (pmode) 1604b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 1614b88c807SRodney W. Grimes if (patime || pmtime) 1624b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 1634b88c807SRodney W. Grimes } 1644b88c807SRodney W. Grimes 1654b88c807SRodney W. Grimes /* 1664b88c807SRodney W. Grimes * lnk_creat() 1674b88c807SRodney W. Grimes * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 1684b88c807SRodney W. Grimes * must exist; 1694b88c807SRodney W. Grimes * Return: 1704b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 1714b88c807SRodney W. Grimes */ 1724b88c807SRodney W. Grimes 1734b88c807SRodney W. Grimes int 174f789b261SWarner Losh lnk_creat(ARCHD *arcn) 1754b88c807SRodney W. Grimes { 1764b88c807SRodney W. Grimes struct stat sb; 1774b88c807SRodney W. Grimes 1784b88c807SRodney W. Grimes /* 1794b88c807SRodney W. Grimes * we may be running as root, so we have to be sure that link target 1804b88c807SRodney W. Grimes * is not a directory, so we lstat and check 1814b88c807SRodney W. Grimes */ 1824b88c807SRodney W. Grimes if (lstat(arcn->ln_name, &sb) < 0) { 183778766feSKris Kennaway syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 1844b88c807SRodney W. Grimes arcn->name); 1854b88c807SRodney W. Grimes return(-1); 1864b88c807SRodney W. Grimes } 1874b88c807SRodney W. Grimes 1884b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 189778766feSKris Kennaway paxwarn(1, "A hard link to the directory %s is not allowed", 1904b88c807SRodney W. Grimes arcn->ln_name); 1914b88c807SRodney W. Grimes return(-1); 1924b88c807SRodney W. Grimes } 1934b88c807SRodney W. Grimes 1944b88c807SRodney W. Grimes return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes 1974b88c807SRodney W. Grimes /* 1984b88c807SRodney W. Grimes * cross_lnk() 1994b88c807SRodney W. Grimes * Create a hard link to arcn->org_name from arcn->name. Only used in copy 200778766feSKris Kennaway * with the -l flag. No warning or error if this does not succeed (we will 2014b88c807SRodney W. Grimes * then just create the file) 2024b88c807SRodney W. Grimes * Return: 2034b88c807SRodney W. Grimes * 1 if copy() should try to create this file node 2044b88c807SRodney W. Grimes * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 2054b88c807SRodney W. Grimes */ 2064b88c807SRodney W. Grimes 2074b88c807SRodney W. Grimes int 208f789b261SWarner Losh cross_lnk(ARCHD *arcn) 2094b88c807SRodney W. Grimes { 2104b88c807SRodney W. Grimes /* 21146be34b9SKris Kennaway * try to make a link to original file (-l flag in copy mode). make sure 2124b88c807SRodney W. Grimes * we do not try to link to directories in case we are running as root 2134b88c807SRodney W. Grimes * (and it might succeed). 2144b88c807SRodney W. Grimes */ 2154b88c807SRodney W. Grimes if (arcn->type == PAX_DIR) 2164b88c807SRodney W. Grimes return(1); 2174b88c807SRodney W. Grimes return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 2184b88c807SRodney W. Grimes } 2194b88c807SRodney W. Grimes 2204b88c807SRodney W. Grimes /* 2214b88c807SRodney W. Grimes * chk_same() 2224b88c807SRodney W. Grimes * In copy mode if we are not trying to make hard links between the src 2234b88c807SRodney W. Grimes * and destinations, make sure we are not going to overwrite ourselves by 2244b88c807SRodney W. Grimes * accident. This slows things down a little, but we have to protect all 2254b88c807SRodney W. Grimes * those people who make typing errors. 2264b88c807SRodney W. Grimes * Return: 2274b88c807SRodney W. Grimes * 1 the target does not exist, go ahead and copy 2284b88c807SRodney W. Grimes * 0 skip it file exists (-k) or may be the same as source file 2294b88c807SRodney W. Grimes */ 2304b88c807SRodney W. Grimes 2314b88c807SRodney W. Grimes int 232f789b261SWarner Losh chk_same(ARCHD *arcn) 2334b88c807SRodney W. Grimes { 2344b88c807SRodney W. Grimes struct stat sb; 2354b88c807SRodney W. Grimes 2364b88c807SRodney W. Grimes /* 2374b88c807SRodney W. Grimes * if file does not exist, return. if file exists and -k, skip it 2384b88c807SRodney W. Grimes * quietly 2394b88c807SRodney W. Grimes */ 2404b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) 2414b88c807SRodney W. Grimes return(1); 2424b88c807SRodney W. Grimes if (kflag) 2434b88c807SRodney W. Grimes return(0); 2444b88c807SRodney W. Grimes 2454b88c807SRodney W. Grimes /* 2464b88c807SRodney W. Grimes * better make sure the user does not have src == dest by mistake 2474b88c807SRodney W. Grimes */ 2484b88c807SRodney W. Grimes if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 249778766feSKris Kennaway paxwarn(1, "Unable to copy %s, file would overwrite itself", 2504b88c807SRodney W. Grimes arcn->name); 2514b88c807SRodney W. Grimes return(0); 2524b88c807SRodney W. Grimes } 2534b88c807SRodney W. Grimes return(1); 2544b88c807SRodney W. Grimes } 2554b88c807SRodney W. Grimes 2564b88c807SRodney W. Grimes /* 2574b88c807SRodney W. Grimes * mk_link() 2584b88c807SRodney W. Grimes * try to make a hard link between two files. if ign set, we do not 2594b88c807SRodney W. Grimes * complain. 2604b88c807SRodney W. Grimes * Return: 2614b88c807SRodney W. Grimes * 0 if successful (or we are done with this file but no error, such as 2624b88c807SRodney W. Grimes * finding the from file exists and the user has set -k). 2634b88c807SRodney W. Grimes * 1 when ign was set to indicates we could not make the link but we 2644b88c807SRodney W. Grimes * should try to copy/extract the file as that might work (and is an 2654b88c807SRodney W. Grimes * allowed option). -1 an error occurred. 2664b88c807SRodney W. Grimes */ 2674b88c807SRodney W. Grimes 2684b88c807SRodney W. Grimes static int 269f789b261SWarner Losh mk_link(char *to, struct stat *to_sb, char *from, 2704b88c807SRodney W. Grimes int ign) 2714b88c807SRodney W. Grimes { 2724b88c807SRodney W. Grimes struct stat sb; 2734b88c807SRodney W. Grimes int oerrno; 2744b88c807SRodney W. Grimes 2754b88c807SRodney W. Grimes /* 2764b88c807SRodney W. Grimes * if from file exists, it has to be unlinked to make the link. If the 2774b88c807SRodney W. Grimes * file exists and -k is set, skip it quietly 2784b88c807SRodney W. Grimes */ 2794b88c807SRodney W. Grimes if (lstat(from, &sb) == 0) { 2804b88c807SRodney W. Grimes if (kflag) 2814b88c807SRodney W. Grimes return(0); 2824b88c807SRodney W. Grimes 2834b88c807SRodney W. Grimes /* 2844b88c807SRodney W. Grimes * make sure it is not the same file, protect the user 2854b88c807SRodney W. Grimes */ 2864b88c807SRodney W. Grimes if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 287778766feSKris Kennaway paxwarn(1, "Unable to link file %s to itself", to); 288f2703795SRalf S. Engelschall return(-1); 2894b88c807SRodney W. Grimes } 2904b88c807SRodney W. Grimes 2914b88c807SRodney W. Grimes /* 2924b88c807SRodney W. Grimes * try to get rid of the file, based on the type 2934b88c807SRodney W. Grimes */ 2944b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 2954b88c807SRodney W. Grimes if (rmdir(from) < 0) { 296778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 2974b88c807SRodney W. Grimes return(-1); 2984b88c807SRodney W. Grimes } 2994b88c807SRodney W. Grimes } else if (unlink(from) < 0) { 3004b88c807SRodney W. Grimes if (!ign) { 301778766feSKris Kennaway syswarn(1, errno, "Unable to remove %s", from); 3024b88c807SRodney W. Grimes return(-1); 3034b88c807SRodney W. Grimes } 3044b88c807SRodney W. Grimes return(1); 3054b88c807SRodney W. Grimes } 3064b88c807SRodney W. Grimes } 3074b88c807SRodney W. Grimes 3084b88c807SRodney W. Grimes /* 3094b88c807SRodney W. Grimes * from file is gone (or did not exist), try to make the hard link. 3104b88c807SRodney W. Grimes * if it fails, check the path and try it again (if chk_path() says to 3114b88c807SRodney W. Grimes * try again) 3124b88c807SRodney W. Grimes */ 3134b88c807SRodney W. Grimes for (;;) { 3144b88c807SRodney W. Grimes if (link(to, from) == 0) 3154b88c807SRodney W. Grimes break; 3164b88c807SRodney W. Grimes oerrno = errno; 317b1787decSKris Kennaway if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 3184b88c807SRodney W. Grimes continue; 3194b88c807SRodney W. Grimes if (!ign) { 320778766feSKris Kennaway syswarn(1, oerrno, "Could not link to %s from %s", to, 3214b88c807SRodney W. Grimes from); 3224b88c807SRodney W. Grimes return(-1); 3234b88c807SRodney W. Grimes } 3244b88c807SRodney W. Grimes return(1); 3254b88c807SRodney W. Grimes } 3264b88c807SRodney W. Grimes 3274b88c807SRodney W. Grimes /* 3284b88c807SRodney W. Grimes * all right the link was made 3294b88c807SRodney W. Grimes */ 3304b88c807SRodney W. Grimes return(0); 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes /* 3344b88c807SRodney W. Grimes * node_creat() 3354b88c807SRodney W. Grimes * create an entry in the file system (other than a file or hard link). 3364b88c807SRodney W. Grimes * If successful, sets uid/gid modes and times as required. 3374b88c807SRodney W. Grimes * Return: 3384b88c807SRodney W. Grimes * 0 if ok, -1 otherwise 3394b88c807SRodney W. Grimes */ 3404b88c807SRodney W. Grimes 3414b88c807SRodney W. Grimes int 342f789b261SWarner Losh node_creat(ARCHD *arcn) 3434b88c807SRodney W. Grimes { 344f789b261SWarner Losh int res; 345f789b261SWarner Losh int ign = 0; 346f789b261SWarner Losh int oerrno; 347f789b261SWarner Losh int pass = 0; 3484b88c807SRodney W. Grimes mode_t file_mode; 3494b88c807SRodney W. Grimes struct stat sb; 3504b88c807SRodney W. Grimes 3514b88c807SRodney W. Grimes /* 3524b88c807SRodney W. Grimes * create node based on type, if that fails try to unlink the node and 3534b88c807SRodney W. Grimes * try again. finally check the path and try again. As noted in the 3544b88c807SRodney W. Grimes * file and link creation routines, this method seems to exhibit the 3554b88c807SRodney W. Grimes * best performance in general use workloads. 3564b88c807SRodney W. Grimes */ 3574b88c807SRodney W. Grimes file_mode = arcn->sb.st_mode & FILEBITS; 3584b88c807SRodney W. Grimes 3594b88c807SRodney W. Grimes for (;;) { 3604b88c807SRodney W. Grimes switch(arcn->type) { 3614b88c807SRodney W. Grimes case PAX_DIR: 3624b88c807SRodney W. Grimes res = mkdir(arcn->name, file_mode); 3634b88c807SRodney W. Grimes if (ign) 3644b88c807SRodney W. Grimes res = 0; 3654b88c807SRodney W. Grimes break; 3664b88c807SRodney W. Grimes case PAX_CHR: 3674b88c807SRodney W. Grimes file_mode |= S_IFCHR; 3684b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3694b88c807SRodney W. Grimes break; 3704b88c807SRodney W. Grimes case PAX_BLK: 3714b88c807SRodney W. Grimes file_mode |= S_IFBLK; 3724b88c807SRodney W. Grimes res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 3734b88c807SRodney W. Grimes break; 3744b88c807SRodney W. Grimes case PAX_FIF: 3754b88c807SRodney W. Grimes res = mkfifo(arcn->name, file_mode); 3764b88c807SRodney W. Grimes break; 3774b88c807SRodney W. Grimes case PAX_SCK: 3784b88c807SRodney W. Grimes /* 3794b88c807SRodney W. Grimes * Skip sockets, operation has no meaning under BSD 3804b88c807SRodney W. Grimes */ 381778766feSKris Kennaway paxwarn(0, 3824b88c807SRodney W. Grimes "%s skipped. Sockets cannot be copied or extracted", 3834b88c807SRodney W. Grimes arcn->name); 3844b88c807SRodney W. Grimes return(-1); 3854b88c807SRodney W. Grimes case PAX_SLK: 386b1787decSKris Kennaway res = symlink(arcn->ln_name, arcn->name); 3874b88c807SRodney W. Grimes break; 3884b88c807SRodney W. Grimes case PAX_CTG: 3894b88c807SRodney W. Grimes case PAX_HLK: 3904b88c807SRodney W. Grimes case PAX_HRG: 3914b88c807SRodney W. Grimes case PAX_REG: 3924b88c807SRodney W. Grimes default: 3934b88c807SRodney W. Grimes /* 3944b88c807SRodney W. Grimes * we should never get here 3954b88c807SRodney W. Grimes */ 396778766feSKris Kennaway paxwarn(0, "%s has an unknown file type, skipping", 3974b88c807SRodney W. Grimes arcn->name); 3984b88c807SRodney W. Grimes return(-1); 3994b88c807SRodney W. Grimes } 4004b88c807SRodney W. Grimes 4014b88c807SRodney W. Grimes /* 4024b88c807SRodney W. Grimes * if we were able to create the node break out of the loop, 4034b88c807SRodney W. Grimes * otherwise try to unlink the node and try again. if that 4044b88c807SRodney W. Grimes * fails check the full path and try a final time. 4054b88c807SRodney W. Grimes */ 4064b88c807SRodney W. Grimes if (res == 0) 4074b88c807SRodney W. Grimes break; 4084b88c807SRodney W. Grimes 4094b88c807SRodney W. Grimes /* 4104b88c807SRodney W. Grimes * we failed to make the node 4114b88c807SRodney W. Grimes */ 4124b88c807SRodney W. Grimes oerrno = errno; 4134b88c807SRodney W. Grimes if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 4144b88c807SRodney W. Grimes return(-1); 4154b88c807SRodney W. Grimes 4164b88c807SRodney W. Grimes if (++pass <= 1) 4174b88c807SRodney W. Grimes continue; 4184b88c807SRodney W. Grimes 419b1787decSKris Kennaway if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 420778766feSKris Kennaway syswarn(1, oerrno, "Could not create: %s", arcn->name); 4214b88c807SRodney W. Grimes return(-1); 4224b88c807SRodney W. Grimes } 4234b88c807SRodney W. Grimes } 4244b88c807SRodney W. Grimes 4254b88c807SRodney W. Grimes /* 4264b88c807SRodney W. Grimes * we were able to create the node. set uid/gid, modes and times 4274b88c807SRodney W. Grimes */ 4284b88c807SRodney W. Grimes if (pids) 429db77c5b9STim Kientzle res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 4304b88c807SRodney W. Grimes else 4314b88c807SRodney W. Grimes res = 0; 4324b88c807SRodney W. Grimes 4334b88c807SRodney W. Grimes /* 4344b88c807SRodney W. Grimes * IMPORTANT SECURITY NOTE: 4354b88c807SRodney W. Grimes * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 4364b88c807SRodney W. Grimes * set uid/gid bits 4374b88c807SRodney W. Grimes */ 4384b88c807SRodney W. Grimes if (!pmode || res) 4394b88c807SRodney W. Grimes arcn->sb.st_mode &= ~(SETBITS); 4404b88c807SRodney W. Grimes if (pmode) 4414b88c807SRodney W. Grimes set_pmode(arcn->name, arcn->sb.st_mode); 4424b88c807SRodney W. Grimes 443b1787decSKris Kennaway if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 4444b88c807SRodney W. Grimes /* 4454b88c807SRodney W. Grimes * Dirs must be processed again at end of extract to set times 4464b88c807SRodney W. Grimes * and modes to agree with those stored in the archive. However 4474b88c807SRodney W. Grimes * to allow extract to continue, we may have to also set owner 4484b88c807SRodney W. Grimes * rights. This allows nodes in the archive that are children 4494b88c807SRodney W. Grimes * of this directory to be extracted without failure. Both time 4504b88c807SRodney W. Grimes * and modes will be fixed after the entire archive is read and 4514b88c807SRodney W. Grimes * before pax exits. 4524b88c807SRodney W. Grimes */ 4534b88c807SRodney W. Grimes if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 4544b88c807SRodney W. Grimes if (lstat(arcn->name, &sb) < 0) { 455778766feSKris Kennaway syswarn(0, errno,"Could not access %s (stat)", 4564b88c807SRodney W. Grimes arcn->name); 4574b88c807SRodney W. Grimes set_pmode(arcn->name,file_mode | S_IRWXU); 4584b88c807SRodney W. Grimes } else { 4594b88c807SRodney W. Grimes /* 4604b88c807SRodney W. Grimes * We have to add rights to the dir, so we make 4614b88c807SRodney W. Grimes * sure to restore the mode. The mode must be 4624b88c807SRodney W. Grimes * restored AS CREATED and not as stored if 4634b88c807SRodney W. Grimes * pmode is not set. 4644b88c807SRodney W. Grimes */ 4654b88c807SRodney W. Grimes set_pmode(arcn->name, 4664b88c807SRodney W. Grimes ((sb.st_mode & FILEBITS) | S_IRWXU)); 4674b88c807SRodney W. Grimes if (!pmode) 4684b88c807SRodney W. Grimes arcn->sb.st_mode = sb.st_mode; 4694b88c807SRodney W. Grimes } 4704b88c807SRodney W. Grimes 4714b88c807SRodney W. Grimes /* 4724b88c807SRodney W. Grimes * we have to force the mode to what was set here, 4734b88c807SRodney W. Grimes * since we changed it from the default as created. 4744b88c807SRodney W. Grimes */ 4754b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 4764b88c807SRodney W. Grimes } else if (pmode || patime || pmtime) 4774b88c807SRodney W. Grimes add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 4784b88c807SRodney W. Grimes } 4794b88c807SRodney W. Grimes 4804b88c807SRodney W. Grimes if (patime || pmtime) 4814b88c807SRodney W. Grimes set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 4824b88c807SRodney W. Grimes return(0); 4834b88c807SRodney W. Grimes } 4844b88c807SRodney W. Grimes 4854b88c807SRodney W. Grimes /* 4864b88c807SRodney W. Grimes * unlnk_exist() 4874b88c807SRodney W. Grimes * Remove node from file system with the specified name. We pass the type 4884b88c807SRodney W. Grimes * of the node that is going to replace it. When we try to create a 4894b88c807SRodney W. Grimes * directory and find that it already exists, we allow processing to 4904b88c807SRodney W. Grimes * continue as proper modes etc will always be set for it later on. 4914b88c807SRodney W. Grimes * Return: 4924b88c807SRodney W. Grimes * 0 is ok to proceed, no file with the specified name exists 4934b88c807SRodney W. Grimes * -1 we were unable to remove the node, or we should not remove it (-k) 4944b88c807SRodney W. Grimes * 1 we found a directory and we were going to create a directory. 4954b88c807SRodney W. Grimes */ 4964b88c807SRodney W. Grimes 4974b88c807SRodney W. Grimes int 498f789b261SWarner Losh unlnk_exist(char *name, int type) 4994b88c807SRodney W. Grimes { 5004b88c807SRodney W. Grimes struct stat sb; 5014b88c807SRodney W. Grimes 5024b88c807SRodney W. Grimes /* 5034b88c807SRodney W. Grimes * the file does not exist, or -k we are done 5044b88c807SRodney W. Grimes */ 5054b88c807SRodney W. Grimes if (lstat(name, &sb) < 0) 5064b88c807SRodney W. Grimes return(0); 5074b88c807SRodney W. Grimes if (kflag) 5084b88c807SRodney W. Grimes return(-1); 5094b88c807SRodney W. Grimes 5104b88c807SRodney W. Grimes if (S_ISDIR(sb.st_mode)) { 5114b88c807SRodney W. Grimes /* 5124b88c807SRodney W. Grimes * try to remove a directory, if it fails and we were going to 5134b88c807SRodney W. Grimes * create a directory anyway, tell the caller (return a 1) 5144b88c807SRodney W. Grimes */ 5154b88c807SRodney W. Grimes if (rmdir(name) < 0) { 5164b88c807SRodney W. Grimes if (type == PAX_DIR) 5174b88c807SRodney W. Grimes return(1); 518778766feSKris Kennaway syswarn(1,errno,"Unable to remove directory %s", name); 5194b88c807SRodney W. Grimes return(-1); 5204b88c807SRodney W. Grimes } 5214b88c807SRodney W. Grimes return(0); 5224b88c807SRodney W. Grimes } 5234b88c807SRodney W. Grimes 5244b88c807SRodney W. Grimes /* 5254b88c807SRodney W. Grimes * try to get rid of all non-directory type nodes 5264b88c807SRodney W. Grimes */ 5274b88c807SRodney W. Grimes if (unlink(name) < 0) { 528778766feSKris Kennaway syswarn(1, errno, "Could not unlink %s", name); 5294b88c807SRodney W. Grimes return(-1); 5304b88c807SRodney W. Grimes } 5314b88c807SRodney W. Grimes return(0); 5324b88c807SRodney W. Grimes } 5334b88c807SRodney W. Grimes 5344b88c807SRodney W. Grimes /* 5354b88c807SRodney W. Grimes * chk_path() 5364b88c807SRodney W. Grimes * We were trying to create some kind of node in the file system and it 5374b88c807SRodney W. Grimes * failed. chk_path() makes sure the path up to the node exists and is 5384b88c807SRodney W. Grimes * writeable. When we have to create a directory that is missing along the 5394b88c807SRodney W. Grimes * path somewhere, the directory we create will be set to the same 5404b88c807SRodney W. Grimes * uid/gid as the file has (when uid and gid are being preserved). 5414b88c807SRodney W. Grimes * NOTE: this routine is a real performance loss. It is only used as a 5424b88c807SRodney W. Grimes * last resort when trying to create entries in the file system. 5434b88c807SRodney W. Grimes * Return: 5444b88c807SRodney W. Grimes * -1 when it could find nothing it is allowed to fix. 5454b88c807SRodney W. Grimes * 0 otherwise 5464b88c807SRodney W. Grimes */ 5474b88c807SRodney W. Grimes 5484b88c807SRodney W. Grimes int 549f789b261SWarner Losh chk_path( char *name, uid_t st_uid, gid_t st_gid) 5504b88c807SRodney W. Grimes { 551f789b261SWarner Losh char *spt = name; 5524b88c807SRodney W. Grimes struct stat sb; 5534b88c807SRodney W. Grimes int retval = -1; 5544b88c807SRodney W. Grimes 5554b88c807SRodney W. Grimes /* 5564b88c807SRodney W. Grimes * watch out for paths with nodes stored directly in / (e.g. /bozo) 5574b88c807SRodney W. Grimes */ 5584b88c807SRodney W. Grimes if (*spt == '/') 5594b88c807SRodney W. Grimes ++spt; 5604b88c807SRodney W. Grimes 5614b88c807SRodney W. Grimes for(;;) { 5624b88c807SRodney W. Grimes /* 56301c99176SUlrich Spörlein * work forward from the first / and check each part of the path 5644b88c807SRodney W. Grimes */ 5654b88c807SRodney W. Grimes spt = strchr(spt, '/'); 5664b88c807SRodney W. Grimes if (spt == NULL) 5674b88c807SRodney W. Grimes break; 5684b88c807SRodney W. Grimes *spt = '\0'; 5694b88c807SRodney W. Grimes 5704b88c807SRodney W. Grimes /* 5714b88c807SRodney W. Grimes * if it exists we assume it is a directory, it is not within 5724b88c807SRodney W. Grimes * the spec (at least it seems to read that way) to alter the 5734b88c807SRodney W. Grimes * file system for nodes NOT EXPLICITLY stored on the archive. 5744b88c807SRodney W. Grimes * If that assumption is changed, you would test the node here 5754b88c807SRodney W. Grimes * and figure out how to get rid of it (probably like some 5764b88c807SRodney W. Grimes * recursive unlink()) or fix up the directory permissions if 5774b88c807SRodney W. Grimes * required (do an access()). 5784b88c807SRodney W. Grimes */ 5794b88c807SRodney W. Grimes if (lstat(name, &sb) == 0) { 5804b88c807SRodney W. Grimes *(spt++) = '/'; 5814b88c807SRodney W. Grimes continue; 5824b88c807SRodney W. Grimes } 5834b88c807SRodney W. Grimes 5844b88c807SRodney W. Grimes /* 5854b88c807SRodney W. Grimes * the path fails at this point, see if we can create the 5864b88c807SRodney W. Grimes * needed directory and continue on 5874b88c807SRodney W. Grimes */ 5884b88c807SRodney W. Grimes if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 5894b88c807SRodney W. Grimes *spt = '/'; 5904b88c807SRodney W. Grimes retval = -1; 5914b88c807SRodney W. Grimes break; 5924b88c807SRodney W. Grimes } 5934b88c807SRodney W. Grimes 5944b88c807SRodney W. Grimes /* 5954b88c807SRodney W. Grimes * we were able to create the directory. We will tell the 5964b88c807SRodney W. Grimes * caller that we found something to fix, and it is ok to try 5974b88c807SRodney W. Grimes * and create the node again. 5984b88c807SRodney W. Grimes */ 5994b88c807SRodney W. Grimes retval = 0; 6004b88c807SRodney W. Grimes if (pids) 6014b88c807SRodney W. Grimes (void)set_ids(name, st_uid, st_gid); 6024b88c807SRodney W. Grimes 6034b88c807SRodney W. Grimes /* 60401c99176SUlrich Spörlein * make sure the user doesn't have some strange umask that 6054b88c807SRodney W. Grimes * causes this newly created directory to be unusable. We fix 6064b88c807SRodney W. Grimes * the modes and restore them back to the creation default at 6074b88c807SRodney W. Grimes * the end of pax 6084b88c807SRodney W. Grimes */ 6094b88c807SRodney W. Grimes if ((access(name, R_OK | W_OK | X_OK) < 0) && 6104b88c807SRodney W. Grimes (lstat(name, &sb) == 0)) { 6114b88c807SRodney W. Grimes set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 6124b88c807SRodney W. Grimes add_dir(name, spt - name, &sb, 1); 6134b88c807SRodney W. Grimes } 6144b88c807SRodney W. Grimes *(spt++) = '/'; 6154b88c807SRodney W. Grimes continue; 6164b88c807SRodney W. Grimes } 6174b88c807SRodney W. Grimes return(retval); 6184b88c807SRodney W. Grimes } 6194b88c807SRodney W. Grimes 6204b88c807SRodney W. Grimes /* 6214b88c807SRodney W. Grimes * set_ftime() 6224b88c807SRodney W. Grimes * Set the access time and modification time for a named file. If frc is 623778766feSKris Kennaway * non-zero we force these times to be set even if the user did not 6244b88c807SRodney W. Grimes * request access and/or modification time preservation (this is also 6254b88c807SRodney W. Grimes * used by -t to reset access times). 6264b88c807SRodney W. Grimes * When ign is zero, only those times the user has asked for are set, the 6274b88c807SRodney W. Grimes * other ones are left alone. We do not assume the un-documented feature 628db77c5b9STim Kientzle * of many lutimes() implementations that consider a 0 time value as a do 6294b88c807SRodney W. Grimes * not set request. 6304b88c807SRodney W. Grimes */ 6314b88c807SRodney W. Grimes 6324b88c807SRodney W. Grimes void 6334b88c807SRodney W. Grimes set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 6344b88c807SRodney W. Grimes { 6354b88c807SRodney W. Grimes static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 6364b88c807SRodney W. Grimes struct stat sb; 6374b88c807SRodney W. Grimes 6386e63a104SMatthew Dillon tv[0].tv_sec = atime; 6396e63a104SMatthew Dillon tv[1].tv_sec = mtime; 6404b88c807SRodney W. Grimes if (!frc && (!patime || !pmtime)) { 6414b88c807SRodney W. Grimes /* 6424b88c807SRodney W. Grimes * if we are not forcing, only set those times the user wants 6434b88c807SRodney W. Grimes * set. We get the current values of the times if we need them. 6444b88c807SRodney W. Grimes */ 6454b88c807SRodney W. Grimes if (lstat(fnm, &sb) == 0) { 6464b88c807SRodney W. Grimes if (!patime) 6476e63a104SMatthew Dillon tv[0].tv_sec = sb.st_atime; 6484b88c807SRodney W. Grimes if (!pmtime) 6496e63a104SMatthew Dillon tv[1].tv_sec = sb.st_mtime; 6504b88c807SRodney W. Grimes } else 651778766feSKris Kennaway syswarn(0,errno,"Unable to obtain file stats %s", fnm); 6524b88c807SRodney W. Grimes } 6534b88c807SRodney W. Grimes 6544b88c807SRodney W. Grimes /* 6554b88c807SRodney W. Grimes * set the times 6564b88c807SRodney W. Grimes */ 657db77c5b9STim Kientzle if (lutimes(fnm, tv) < 0) 658778766feSKris Kennaway syswarn(1, errno, "Access/modification time set failed on: %s", 6594b88c807SRodney W. Grimes fnm); 6604b88c807SRodney W. Grimes return; 6614b88c807SRodney W. Grimes } 6624b88c807SRodney W. Grimes 6634b88c807SRodney W. Grimes /* 6644b88c807SRodney W. Grimes * set_ids() 6654b88c807SRodney W. Grimes * set the uid and gid of a file system node 6664b88c807SRodney W. Grimes * Return: 6674b88c807SRodney W. Grimes * 0 when set, -1 on failure 6684b88c807SRodney W. Grimes */ 6694b88c807SRodney W. Grimes 6704b88c807SRodney W. Grimes int 6714b88c807SRodney W. Grimes set_ids(char *fnm, uid_t uid, gid_t gid) 6724b88c807SRodney W. Grimes { 673b1787decSKris Kennaway if (lchown(fnm, uid, gid) < 0) { 674b1787decSKris Kennaway /* 675b1787decSKris Kennaway * ignore EPERM unless in verbose mode or being run by root. 676b1787decSKris Kennaway * if running as pax, POSIX requires a warning. 677b1787decSKris Kennaway */ 678b1787decSKris Kennaway if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 679b1787decSKris Kennaway geteuid() == 0) 680b1787decSKris Kennaway syswarn(1, errno, "Unable to set file uid/gid of %s", 681b1787decSKris Kennaway fnm); 6824b88c807SRodney W. Grimes return(-1); 6834b88c807SRodney W. Grimes } 6844b88c807SRodney W. Grimes return(0); 6854b88c807SRodney W. Grimes } 6864b88c807SRodney W. Grimes 6874b88c807SRodney W. Grimes /* 6884b88c807SRodney W. Grimes * set_pmode() 6894b88c807SRodney W. Grimes * Set file access mode 6904b88c807SRodney W. Grimes */ 6914b88c807SRodney W. Grimes 6924b88c807SRodney W. Grimes void 6934b88c807SRodney W. Grimes set_pmode(char *fnm, mode_t mode) 6944b88c807SRodney W. Grimes { 6954b88c807SRodney W. Grimes mode &= ABITS; 696db77c5b9STim Kientzle if (lchmod(fnm, mode) < 0) 697778766feSKris Kennaway syswarn(1, errno, "Could not set permissions on %s", fnm); 6984b88c807SRodney W. Grimes return; 6994b88c807SRodney W. Grimes } 7004b88c807SRodney W. Grimes 7014b88c807SRodney W. Grimes /* 7024b88c807SRodney W. Grimes * file_write() 7034b88c807SRodney W. Grimes * Write/copy a file (during copy or archive extract). This routine knows 7044b88c807SRodney W. Grimes * how to copy files with lseek holes in it. (Which are read as file 7054b88c807SRodney W. Grimes * blocks containing all 0's but do not have any file blocks associated 7064b88c807SRodney W. Grimes * with the data). Typical examples of these are files created by dbm 7074b88c807SRodney W. Grimes * variants (.pag files). While the file size of these files are huge, the 7084b88c807SRodney W. Grimes * actual storage is quite small (the files are sparse). The problem is 7094b88c807SRodney W. Grimes * the holes read as all zeros so are probably stored on the archive that 7104b88c807SRodney W. Grimes * way (there is no way to determine if the file block is really a hole, 7114b88c807SRodney W. Grimes * we only know that a file block of all zero's can be a hole). 7124b88c807SRodney W. Grimes * At this writing, no major archive format knows how to archive files 7134b88c807SRodney W. Grimes * with holes. However, on extraction (or during copy, -rw) we have to 7144b88c807SRodney W. Grimes * deal with these files. Without detecting the holes, the files can 7154b88c807SRodney W. Grimes * consume a lot of file space if just written to disk. This replacement 7164b88c807SRodney W. Grimes * for write when passed the basic allocation size of a file system block, 7174b88c807SRodney W. Grimes * uses lseek whenever it detects the input data is all 0 within that 7184b88c807SRodney W. Grimes * file block. In more detail, the strategy is as follows: 7194b88c807SRodney W. Grimes * While the input is all zero keep doing an lseek. Keep track of when we 72001c99176SUlrich Spörlein * pass over file block boundaries. Only write when we hit a non zero 7214b88c807SRodney W. Grimes * input. once we have written a file block, we continue to write it to 7224b88c807SRodney W. Grimes * the end (we stop looking at the input). When we reach the start of the 7234b88c807SRodney W. Grimes * next file block, start checking for zero blocks again. Working on file 72401c99176SUlrich Spörlein * block boundaries significantly reduces the overhead when copying files 7254b88c807SRodney W. Grimes * that are NOT very sparse. This overhead (when compared to a write) is 7264b88c807SRodney W. Grimes * almost below the measurement resolution on many systems. Without it, 7274b88c807SRodney W. Grimes * files with holes cannot be safely copied. It does has a side effect as 7284b88c807SRodney W. Grimes * it can put holes into files that did not have them before, but that is 7294b88c807SRodney W. Grimes * not a problem since the file contents are unchanged (in fact it saves 7304b88c807SRodney W. Grimes * file space). (Except on paging files for diskless clients. But since we 7314b88c807SRodney W. Grimes * cannot determine one of those file from here, we ignore them). If this 7324b88c807SRodney W. Grimes * ever ends up on a system where CTG files are supported and the holes 7334b88c807SRodney W. Grimes * are not desired, just do a conditional test in those routines that 7344b88c807SRodney W. Grimes * call file_write() and have it call write() instead. BEFORE CLOSING THE 7354b88c807SRodney W. Grimes * FILE, make sure to call file_flush() when the last write finishes with 7364b88c807SRodney W. Grimes * an empty block. A lot of file systems will not create an lseek hole at 7374b88c807SRodney W. Grimes * the end. In this case we drop a single 0 at the end to force the 7384b88c807SRodney W. Grimes * trailing 0's in the file. 7394b88c807SRodney W. Grimes * ---Parameters--- 7404b88c807SRodney W. Grimes * rem: how many bytes left in this file system block 7414b88c807SRodney W. Grimes * isempt: have we written to the file block yet (is it empty) 7424b88c807SRodney W. Grimes * sz: basic file block allocation size 7434b88c807SRodney W. Grimes * cnt: number of bytes on this write 7444b88c807SRodney W. Grimes * str: buffer to write 7454b88c807SRodney W. Grimes * Return: 7464b88c807SRodney W. Grimes * number of bytes written, -1 on write (or lseek) error. 7474b88c807SRodney W. Grimes */ 7484b88c807SRodney W. Grimes 7494b88c807SRodney W. Grimes int 750f789b261SWarner Losh file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 7514b88c807SRodney W. Grimes char *name) 7524b88c807SRodney W. Grimes { 753f789b261SWarner Losh char *pt; 754f789b261SWarner Losh char *end; 755f789b261SWarner Losh int wcnt; 756f789b261SWarner Losh char *st = str; 7574b88c807SRodney W. Grimes 7584b88c807SRodney W. Grimes /* 7594b88c807SRodney W. Grimes * while we have data to process 7604b88c807SRodney W. Grimes */ 7614b88c807SRodney W. Grimes while (cnt) { 7624b88c807SRodney W. Grimes if (!*rem) { 7634b88c807SRodney W. Grimes /* 7644b88c807SRodney W. Grimes * We are now at the start of file system block again 7654b88c807SRodney W. Grimes * (or what we think one is...). start looking for 7664b88c807SRodney W. Grimes * empty blocks again 7674b88c807SRodney W. Grimes */ 7684b88c807SRodney W. Grimes *isempt = 1; 7694b88c807SRodney W. Grimes *rem = sz; 7704b88c807SRodney W. Grimes } 7714b88c807SRodney W. Grimes 7724b88c807SRodney W. Grimes /* 7734b88c807SRodney W. Grimes * only examine up to the end of the current file block or 7744b88c807SRodney W. Grimes * remaining characters to write, whatever is smaller 7754b88c807SRodney W. Grimes */ 7764b88c807SRodney W. Grimes wcnt = MIN(cnt, *rem); 7774b88c807SRodney W. Grimes cnt -= wcnt; 7784b88c807SRodney W. Grimes *rem -= wcnt; 7794b88c807SRodney W. Grimes if (*isempt) { 7804b88c807SRodney W. Grimes /* 7814b88c807SRodney W. Grimes * have not written to this block yet, so we keep 7824b88c807SRodney W. Grimes * looking for zero's 7834b88c807SRodney W. Grimes */ 7844b88c807SRodney W. Grimes pt = st; 7854b88c807SRodney W. Grimes end = st + wcnt; 7864b88c807SRodney W. Grimes 7874b88c807SRodney W. Grimes /* 7884b88c807SRodney W. Grimes * look for a zero filled buffer 7894b88c807SRodney W. Grimes */ 7904b88c807SRodney W. Grimes while ((pt < end) && (*pt == '\0')) 7914b88c807SRodney W. Grimes ++pt; 7924b88c807SRodney W. Grimes 7934b88c807SRodney W. Grimes if (pt == end) { 7944b88c807SRodney W. Grimes /* 7954b88c807SRodney W. Grimes * skip, buf is empty so far 7964b88c807SRodney W. Grimes */ 7974b88c807SRodney W. Grimes if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 798778766feSKris Kennaway syswarn(1,errno,"File seek on %s", 7994b88c807SRodney W. Grimes name); 8004b88c807SRodney W. Grimes return(-1); 8014b88c807SRodney W. Grimes } 8024b88c807SRodney W. Grimes st = pt; 8034b88c807SRodney W. Grimes continue; 8044b88c807SRodney W. Grimes } 8054b88c807SRodney W. Grimes /* 8064b88c807SRodney W. Grimes * drat, the buf is not zero filled 8074b88c807SRodney W. Grimes */ 8084b88c807SRodney W. Grimes *isempt = 0; 8094b88c807SRodney W. Grimes } 8104b88c807SRodney W. Grimes 8114b88c807SRodney W. Grimes /* 8124b88c807SRodney W. Grimes * have non-zero data in this file system block, have to write 8134b88c807SRodney W. Grimes */ 8144b88c807SRodney W. Grimes if (write(fd, st, wcnt) != wcnt) { 815778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", name); 8164b88c807SRodney W. Grimes return(-1); 8174b88c807SRodney W. Grimes } 8184b88c807SRodney W. Grimes st += wcnt; 8194b88c807SRodney W. Grimes } 8204b88c807SRodney W. Grimes return(st - str); 8214b88c807SRodney W. Grimes } 8224b88c807SRodney W. Grimes 8234b88c807SRodney W. Grimes /* 8244b88c807SRodney W. Grimes * file_flush() 8254b88c807SRodney W. Grimes * when the last file block in a file is zero, many file systems will not 8264b88c807SRodney W. Grimes * let us create a hole at the end. To get the last block with zeros, we 8274b88c807SRodney W. Grimes * write the last BYTE with a zero (back up one byte and write a zero). 8284b88c807SRodney W. Grimes */ 8294b88c807SRodney W. Grimes 8304b88c807SRodney W. Grimes void 8314b88c807SRodney W. Grimes file_flush(int fd, char *fname, int isempt) 8324b88c807SRodney W. Grimes { 8334b88c807SRodney W. Grimes static char blnk[] = "\0"; 8344b88c807SRodney W. Grimes 8354b88c807SRodney W. Grimes /* 8364b88c807SRodney W. Grimes * silly test, but make sure we are only called when the last block is 8374b88c807SRodney W. Grimes * filled with all zeros. 8384b88c807SRodney W. Grimes */ 8394b88c807SRodney W. Grimes if (!isempt) 8404b88c807SRodney W. Grimes return; 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes /* 8434b88c807SRodney W. Grimes * move back one byte and write a zero 8444b88c807SRodney W. Grimes */ 8454b88c807SRodney W. Grimes if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 846778766feSKris Kennaway syswarn(1, errno, "Failed seek on file %s", fname); 8474b88c807SRodney W. Grimes return; 8484b88c807SRodney W. Grimes } 8494b88c807SRodney W. Grimes 8504b88c807SRodney W. Grimes if (write(fd, blnk, 1) < 0) 851778766feSKris Kennaway syswarn(1, errno, "Failed write to file %s", fname); 8524b88c807SRodney W. Grimes return; 8534b88c807SRodney W. Grimes } 8544b88c807SRodney W. Grimes 8554b88c807SRodney W. Grimes /* 8564b88c807SRodney W. Grimes * rdfile_close() 8574b88c807SRodney W. Grimes * close a file we have beed reading (to copy or archive). If we have to 8584b88c807SRodney W. Grimes * reset access time (tflag) do so (the times are stored in arcn). 8594b88c807SRodney W. Grimes */ 8604b88c807SRodney W. Grimes 8614b88c807SRodney W. Grimes void 862f789b261SWarner Losh rdfile_close(ARCHD *arcn, int *fd) 8634b88c807SRodney W. Grimes { 8644b88c807SRodney W. Grimes /* 8654b88c807SRodney W. Grimes * make sure the file is open 8664b88c807SRodney W. Grimes */ 8674b88c807SRodney W. Grimes if (*fd < 0) 8684b88c807SRodney W. Grimes return; 8694b88c807SRodney W. Grimes 8704b88c807SRodney W. Grimes (void)close(*fd); 8714b88c807SRodney W. Grimes *fd = -1; 8724b88c807SRodney W. Grimes if (!tflag) 8734b88c807SRodney W. Grimes return; 8744b88c807SRodney W. Grimes 8754b88c807SRodney W. Grimes /* 8764b88c807SRodney W. Grimes * user wants last access time reset 8774b88c807SRodney W. Grimes */ 8784b88c807SRodney W. Grimes set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 8794b88c807SRodney W. Grimes return; 8804b88c807SRodney W. Grimes } 8814b88c807SRodney W. Grimes 8824b88c807SRodney W. Grimes /* 8834b88c807SRodney W. Grimes * set_crc() 8844b88c807SRodney W. Grimes * read a file to calculate its crc. This is a real drag. Archive formats 8854b88c807SRodney W. Grimes * that have this, end up reading the file twice (we have to write the 8864b88c807SRodney W. Grimes * header WITH the crc before writing the file contents. Oh well... 8874b88c807SRodney W. Grimes * Return: 8884b88c807SRodney W. Grimes * 0 if was able to calculate the crc, -1 otherwise 8894b88c807SRodney W. Grimes */ 8904b88c807SRodney W. Grimes 8914b88c807SRodney W. Grimes int 892f789b261SWarner Losh set_crc(ARCHD *arcn, int fd) 8934b88c807SRodney W. Grimes { 894f789b261SWarner Losh int i; 895f789b261SWarner Losh int res; 8964b88c807SRodney W. Grimes off_t cpcnt = 0L; 8974b88c807SRodney W. Grimes u_long size; 8984b88c807SRodney W. Grimes unsigned long crc = 0L; 8994b88c807SRodney W. Grimes char tbuf[FILEBLK]; 9004b88c807SRodney W. Grimes struct stat sb; 9014b88c807SRodney W. Grimes 9024b88c807SRodney W. Grimes if (fd < 0) { 9034b88c807SRodney W. Grimes /* 9044b88c807SRodney W. Grimes * hmm, no fd, should never happen. well no crc then. 9054b88c807SRodney W. Grimes */ 9064b88c807SRodney W. Grimes arcn->crc = 0L; 9074b88c807SRodney W. Grimes return(0); 9084b88c807SRodney W. Grimes } 9094b88c807SRodney W. Grimes 9104b88c807SRodney W. Grimes if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 9114b88c807SRodney W. Grimes size = (u_long)sizeof(tbuf); 9124b88c807SRodney W. Grimes 9134b88c807SRodney W. Grimes /* 9144b88c807SRodney W. Grimes * read all the bytes we think that there are in the file. If the user 9154b88c807SRodney W. Grimes * is trying to archive an active file, forget this file. 9164b88c807SRodney W. Grimes */ 9174b88c807SRodney W. Grimes for(;;) { 9184b88c807SRodney W. Grimes if ((res = read(fd, tbuf, size)) <= 0) 9194b88c807SRodney W. Grimes break; 9204b88c807SRodney W. Grimes cpcnt += res; 9214b88c807SRodney W. Grimes for (i = 0; i < res; ++i) 9224b88c807SRodney W. Grimes crc += (tbuf[i] & 0xff); 9234b88c807SRodney W. Grimes } 9244b88c807SRodney W. Grimes 9254b88c807SRodney W. Grimes /* 9264b88c807SRodney W. Grimes * safety check. we want to avoid archiving files that are active as 92701c99176SUlrich Spörlein * they can create inconsistent archive copies. 9284b88c807SRodney W. Grimes */ 9294b88c807SRodney W. Grimes if (cpcnt != arcn->sb.st_size) 930778766feSKris Kennaway paxwarn(1, "File changed size %s", arcn->org_name); 9314b88c807SRodney W. Grimes else if (fstat(fd, &sb) < 0) 932778766feSKris Kennaway syswarn(1, errno, "Failed stat on %s", arcn->org_name); 9334b88c807SRodney W. Grimes else if (arcn->sb.st_mtime != sb.st_mtime) 934778766feSKris Kennaway paxwarn(1, "File %s was modified during read", arcn->org_name); 9354b88c807SRodney W. Grimes else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 936778766feSKris Kennaway syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 9374b88c807SRodney W. Grimes else { 9384b88c807SRodney W. Grimes arcn->crc = crc; 9394b88c807SRodney W. Grimes return(0); 9404b88c807SRodney W. Grimes } 9414b88c807SRodney W. Grimes return(-1); 9424b88c807SRodney W. Grimes } 943