file_subs.c revision 127958
1214501Srpaulo/*- 2214501Srpaulo * Copyright (c) 1992 Keith Muller. 3214501Srpaulo * Copyright (c) 1992, 1993 4214501Srpaulo * The Regents of the University of California. All rights reserved. 5252726Srpaulo * 6252726Srpaulo * This code is derived from software contributed to Berkeley by 7214501Srpaulo * Keith Muller of the University of California, San Diego. 8214501Srpaulo * 9214501Srpaulo * Redistribution and use in source and binary forms, with or without 10214501Srpaulo * modification, are permitted provided that the following conditions 11214501Srpaulo * are met: 12252726Srpaulo * 1. Redistributions of source code must retain the above copyright 13214501Srpaulo * notice, this list of conditions and the following disclaimer. 14214501Srpaulo * 2. Redistributions in binary form must reproduce the above copyright 15214501Srpaulo * notice, this list of conditions and the following disclaimer in the 16214501Srpaulo * documentation and/or other materials provided with the distribution. 17214501Srpaulo * 4. Neither the name of the University nor the names of its contributors 18214501Srpaulo * may be used to endorse or promote products derived from this software 19214501Srpaulo * without specific prior written permission. 20214501Srpaulo * 21214501Srpaulo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22214501Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23214501Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24214501Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25214501Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26214501Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27214501Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28214501Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29214501Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30214501Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31214501Srpaulo * SUCH DAMAGE. 32214501Srpaulo */ 33214501Srpaulo 34214501Srpaulo#ifndef lint 35214501Srpaulo#if 0 36214501Srpaulostatic char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 37214501Srpaulo#endif 38214501Srpaulo#endif /* not lint */ 39214501Srpaulo#include <sys/cdefs.h> 40214501Srpaulo__FBSDID("$FreeBSD: head/bin/pax/file_subs.c 127958 2004-04-06 20:06:54Z markm $"); 41214501Srpaulo 42214501Srpaulo#include <sys/types.h> 43214501Srpaulo#include <sys/time.h> 44214501Srpaulo#include <sys/stat.h> 45214501Srpaulo#include <unistd.h> 46214501Srpaulo#include <fcntl.h> 47214501Srpaulo#include <string.h> 48214501Srpaulo#include <stdio.h> 49214501Srpaulo#include <errno.h> 50214501Srpaulo#include <sys/uio.h> 51214501Srpaulo#include <stdlib.h> 52214501Srpaulo#include "pax.h" 53214501Srpaulo#include "options.h" 54214501Srpaulo#include "extern.h" 55214501Srpaulo 56214501Srpaulostatic int 57214501Srpaulomk_link(char *,struct stat *,char *, int); 58214501Srpaulo 59214501Srpaulo/* 60214501Srpaulo * routines that deal with file operations such as: creating, removing; 61214501Srpaulo * and setting access modes, uid/gid and times of files 62214501Srpaulo */ 63214501Srpaulo 64214501Srpaulo#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 65214501Srpaulo#define SETBITS (S_ISUID | S_ISGID) 66214501Srpaulo#define ABITS (FILEBITS | SETBITS) 67214501Srpaulo 68214501Srpaulo/* 69214501Srpaulo * file_creat() 70214501Srpaulo * Create and open a file. 71214501Srpaulo * Return: 72214501Srpaulo * file descriptor or -1 for failure 73214501Srpaulo */ 74214501Srpaulo 75214501Srpauloint 76214501Srpaulofile_creat(ARCHD *arcn) 77214501Srpaulo{ 78214501Srpaulo int fd = -1; 79214501Srpaulo mode_t file_mode; 80214501Srpaulo int oerrno; 81214501Srpaulo 82214501Srpaulo /* 83214501Srpaulo * assume file doesn't exist, so just try to create it, most times this 84214501Srpaulo * works. We have to take special handling when the file does exist. To 85214501Srpaulo * detect this, we use O_EXCL. For example when trying to create a 86214501Srpaulo * file and a character device or fifo exists with the same name, we 87214501Srpaulo * can accidently open the device by mistake (or block waiting to open) 88214501Srpaulo * If we find that the open has failed, then figure spend the effort to 89214501Srpaulo * figure out why. This strategy was found to have better average 90214501Srpaulo * performance in common use than checking the file (and the path) 91214501Srpaulo * first with lstat. 92214501Srpaulo */ 93214501Srpaulo file_mode = arcn->sb.st_mode & FILEBITS; 94214501Srpaulo if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 95214501Srpaulo file_mode)) >= 0) 96214501Srpaulo return(fd); 97214501Srpaulo 98214501Srpaulo /* 99214501Srpaulo * the file seems to exist. First we try to get rid of it (found to be 100214501Srpaulo * the second most common failure when traced). If this fails, only 101214501Srpaulo * then we go to the expense to check and create the path to the file 102214501Srpaulo */ 103214501Srpaulo if (unlnk_exist(arcn->name, arcn->type) != 0) 104214501Srpaulo return(-1); 105214501Srpaulo 106214501Srpaulo for (;;) { 107214501Srpaulo /* 108214501Srpaulo * try to open it again, if this fails, check all the nodes in 109214501Srpaulo * the path and give it a final try. if chk_path() finds that 110214501Srpaulo * it cannot fix anything, we will skip the last attempt 111214501Srpaulo */ 112214501Srpaulo if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 113214501Srpaulo file_mode)) >= 0) 114214501Srpaulo break; 115214501Srpaulo oerrno = errno; 116214501Srpaulo if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 117214501Srpaulo syswarn(1, oerrno, "Unable to create %s", arcn->name); 118252726Srpaulo return(-1); 119214501Srpaulo } 120214501Srpaulo } 121214501Srpaulo return(fd); 122214501Srpaulo} 123214501Srpaulo 124214501Srpaulo/* 125214501Srpaulo * file_close() 126214501Srpaulo * Close file descriptor to a file just created by pax. Sets modes, 127214501Srpaulo * ownership and times as required. 128214501Srpaulo * Return: 129214501Srpaulo * 0 for success, -1 for failure 130214501Srpaulo */ 131214501Srpaulo 132214501Srpaulovoid 133214501Srpaulofile_close(ARCHD *arcn, int fd) 134214501Srpaulo{ 135214501Srpaulo int res = 0; 136214501Srpaulo 137214501Srpaulo if (fd < 0) 138214501Srpaulo return; 139214501Srpaulo if (close(fd) < 0) 140214501Srpaulo syswarn(0, errno, "Unable to close file descriptor on %s", 141214501Srpaulo arcn->name); 142214501Srpaulo 143214501Srpaulo /* 144214501Srpaulo * set owner/groups first as this may strip off mode bits we want 145214501Srpaulo * then set file permission modes. Then set file access and 146214501Srpaulo * modification times. 147214501Srpaulo */ 148214501Srpaulo if (pids) 149214501Srpaulo res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 150214501Srpaulo 151214501Srpaulo /* 152214501Srpaulo * IMPORTANT SECURITY NOTE: 153214501Srpaulo * if not preserving mode or we cannot set uid/gid, then PROHIBIT 154214501Srpaulo * set uid/gid bits 155214501Srpaulo */ 156214501Srpaulo if (!pmode || res) 157214501Srpaulo arcn->sb.st_mode &= ~(SETBITS); 158214501Srpaulo if (pmode) 159214501Srpaulo set_pmode(arcn->name, arcn->sb.st_mode); 160214501Srpaulo if (patime || pmtime) 161214501Srpaulo set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 162214501Srpaulo} 163214501Srpaulo 164214501Srpaulo/* 165214501Srpaulo * lnk_creat() 166214501Srpaulo * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 167214501Srpaulo * must exist; 168214501Srpaulo * Return: 169214501Srpaulo * 0 if ok, -1 otherwise 170214501Srpaulo */ 171214501Srpaulo 172214501Srpauloint 173214501Srpaulolnk_creat(ARCHD *arcn) 174214501Srpaulo{ 175214501Srpaulo struct stat sb; 176214501Srpaulo 177214501Srpaulo /* 178214501Srpaulo * we may be running as root, so we have to be sure that link target 179214501Srpaulo * is not a directory, so we lstat and check 180214501Srpaulo */ 181214501Srpaulo if (lstat(arcn->ln_name, &sb) < 0) { 182214501Srpaulo syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 183214501Srpaulo arcn->name); 184214501Srpaulo return(-1); 185214501Srpaulo } 186214501Srpaulo 187214501Srpaulo if (S_ISDIR(sb.st_mode)) { 188214501Srpaulo paxwarn(1, "A hard link to the directory %s is not allowed", 189214501Srpaulo arcn->ln_name); 190214501Srpaulo return(-1); 191214501Srpaulo } 192214501Srpaulo 193214501Srpaulo return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 194214501Srpaulo} 195214501Srpaulo 196214501Srpaulo/* 197214501Srpaulo * cross_lnk() 198214501Srpaulo * Create a hard link to arcn->org_name from arcn->name. Only used in copy 199214501Srpaulo * with the -l flag. No warning or error if this does not succeed (we will 200214501Srpaulo * then just create the file) 201214501Srpaulo * Return: 202214501Srpaulo * 1 if copy() should try to create this file node 203214501Srpaulo * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 204214501Srpaulo */ 205214501Srpaulo 206214501Srpauloint 207214501Srpaulocross_lnk(ARCHD *arcn) 208214501Srpaulo{ 209214501Srpaulo /* 210214501Srpaulo * try to make a link to original file (-l flag in copy mode). make sure 211214501Srpaulo * we do not try to link to directories in case we are running as root 212214501Srpaulo * (and it might succeed). 213214501Srpaulo */ 214214501Srpaulo if (arcn->type == PAX_DIR) 215214501Srpaulo return(1); 216214501Srpaulo return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 217214501Srpaulo} 218214501Srpaulo 219214501Srpaulo/* 220214501Srpaulo * chk_same() 221214501Srpaulo * In copy mode if we are not trying to make hard links between the src 222214501Srpaulo * and destinations, make sure we are not going to overwrite ourselves by 223214501Srpaulo * accident. This slows things down a little, but we have to protect all 224214501Srpaulo * those people who make typing errors. 225214501Srpaulo * Return: 226214501Srpaulo * 1 the target does not exist, go ahead and copy 227214501Srpaulo * 0 skip it file exists (-k) or may be the same as source file 228214501Srpaulo */ 229214501Srpaulo 230214501Srpauloint 231214501Srpaulochk_same(ARCHD *arcn) 232214501Srpaulo{ 233214501Srpaulo struct stat sb; 234214501Srpaulo 235214501Srpaulo /* 236214501Srpaulo * if file does not exist, return. if file exists and -k, skip it 237214501Srpaulo * quietly 238214501Srpaulo */ 239214501Srpaulo if (lstat(arcn->name, &sb) < 0) 240214501Srpaulo return(1); 241214501Srpaulo if (kflag) 242214501Srpaulo return(0); 243214501Srpaulo 244214501Srpaulo /* 245214501Srpaulo * better make sure the user does not have src == dest by mistake 246214501Srpaulo */ 247214501Srpaulo if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 248214501Srpaulo paxwarn(1, "Unable to copy %s, file would overwrite itself", 249214501Srpaulo arcn->name); 250214501Srpaulo return(0); 251214501Srpaulo } 252214501Srpaulo return(1); 253214501Srpaulo} 254214501Srpaulo 255214501Srpaulo/* 256214501Srpaulo * mk_link() 257214501Srpaulo * try to make a hard link between two files. if ign set, we do not 258214501Srpaulo * complain. 259214501Srpaulo * Return: 260214501Srpaulo * 0 if successful (or we are done with this file but no error, such as 261214501Srpaulo * finding the from file exists and the user has set -k). 262214501Srpaulo * 1 when ign was set to indicates we could not make the link but we 263214501Srpaulo * should try to copy/extract the file as that might work (and is an 264214501Srpaulo * allowed option). -1 an error occurred. 265214501Srpaulo */ 266214501Srpaulo 267214501Srpaulostatic int 268214501Srpaulomk_link(char *to, struct stat *to_sb, char *from, 269214501Srpaulo int ign) 270214501Srpaulo{ 271214501Srpaulo struct stat sb; 272214501Srpaulo int oerrno; 273214501Srpaulo 274214501Srpaulo /* 275214501Srpaulo * if from file exists, it has to be unlinked to make the link. If the 276214501Srpaulo * file exists and -k is set, skip it quietly 277214501Srpaulo */ 278214501Srpaulo if (lstat(from, &sb) == 0) { 279214501Srpaulo if (kflag) 280214501Srpaulo return(0); 281214501Srpaulo 282214501Srpaulo /* 283214501Srpaulo * make sure it is not the same file, protect the user 284214501Srpaulo */ 285214501Srpaulo if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 286214501Srpaulo paxwarn(1, "Unable to link file %s to itself", to); 287214501Srpaulo return(-1);; 288214501Srpaulo } 289214501Srpaulo 290214501Srpaulo /* 291214501Srpaulo * try to get rid of the file, based on the type 292214501Srpaulo */ 293214501Srpaulo if (S_ISDIR(sb.st_mode)) { 294214501Srpaulo if (rmdir(from) < 0) { 295214501Srpaulo syswarn(1, errno, "Unable to remove %s", from); 296214501Srpaulo return(-1); 297214501Srpaulo } 298214501Srpaulo } else if (unlink(from) < 0) { 299214501Srpaulo if (!ign) { 300214501Srpaulo syswarn(1, errno, "Unable to remove %s", from); 301214501Srpaulo return(-1); 302214501Srpaulo } 303214501Srpaulo return(1); 304214501Srpaulo } 305214501Srpaulo } 306214501Srpaulo 307214501Srpaulo /* 308214501Srpaulo * from file is gone (or did not exist), try to make the hard link. 309214501Srpaulo * if it fails, check the path and try it again (if chk_path() says to 310214501Srpaulo * try again) 311214501Srpaulo */ 312214501Srpaulo for (;;) { 313214501Srpaulo if (link(to, from) == 0) 314214501Srpaulo break; 315214501Srpaulo oerrno = errno; 316214501Srpaulo if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 317214501Srpaulo continue; 318214501Srpaulo if (!ign) { 319214501Srpaulo syswarn(1, oerrno, "Could not link to %s from %s", to, 320214501Srpaulo from); 321214501Srpaulo return(-1); 322214501Srpaulo } 323214501Srpaulo return(1); 324214501Srpaulo } 325214501Srpaulo 326214501Srpaulo /* 327214501Srpaulo * all right the link was made 328214501Srpaulo */ 329214501Srpaulo return(0); 330214501Srpaulo} 331214501Srpaulo 332214501Srpaulo/* 333214501Srpaulo * node_creat() 334214501Srpaulo * create an entry in the file system (other than a file or hard link). 335214501Srpaulo * If successful, sets uid/gid modes and times as required. 336214501Srpaulo * Return: 337214501Srpaulo * 0 if ok, -1 otherwise 338214501Srpaulo */ 339214501Srpaulo 340214501Srpauloint 341214501Srpaulonode_creat(ARCHD *arcn) 342214501Srpaulo{ 343214501Srpaulo int res; 344214501Srpaulo int ign = 0; 345214501Srpaulo int oerrno; 346214501Srpaulo int pass = 0; 347214501Srpaulo mode_t file_mode; 348214501Srpaulo struct stat sb; 349214501Srpaulo 350214501Srpaulo /* 351214501Srpaulo * create node based on type, if that fails try to unlink the node and 352214501Srpaulo * try again. finally check the path and try again. As noted in the 353214501Srpaulo * file and link creation routines, this method seems to exhibit the 354214501Srpaulo * best performance in general use workloads. 355214501Srpaulo */ 356214501Srpaulo file_mode = arcn->sb.st_mode & FILEBITS; 357214501Srpaulo 358214501Srpaulo for (;;) { 359214501Srpaulo switch(arcn->type) { 360214501Srpaulo case PAX_DIR: 361214501Srpaulo res = mkdir(arcn->name, file_mode); 362214501Srpaulo if (ign) 363214501Srpaulo res = 0; 364214501Srpaulo break; 365214501Srpaulo case PAX_CHR: 366214501Srpaulo file_mode |= S_IFCHR; 367214501Srpaulo res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 368214501Srpaulo break; 369214501Srpaulo case PAX_BLK: 370214501Srpaulo file_mode |= S_IFBLK; 371214501Srpaulo res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 372214501Srpaulo break; 373214501Srpaulo case PAX_FIF: 374214501Srpaulo res = mkfifo(arcn->name, file_mode); 375214501Srpaulo break; 376214501Srpaulo case PAX_SCK: 377214501Srpaulo /* 378214501Srpaulo * Skip sockets, operation has no meaning under BSD 379214501Srpaulo */ 380214501Srpaulo paxwarn(0, 381214501Srpaulo "%s skipped. Sockets cannot be copied or extracted", 382214501Srpaulo arcn->name); 383214501Srpaulo return(-1); 384214501Srpaulo case PAX_SLK: 385214501Srpaulo res = symlink(arcn->ln_name, arcn->name); 386214501Srpaulo break; 387214501Srpaulo case PAX_CTG: 388214501Srpaulo case PAX_HLK: 389214501Srpaulo case PAX_HRG: 390214501Srpaulo case PAX_REG: 391214501Srpaulo default: 392214501Srpaulo /* 393214501Srpaulo * we should never get here 394214501Srpaulo */ 395214501Srpaulo paxwarn(0, "%s has an unknown file type, skipping", 396214501Srpaulo arcn->name); 397214501Srpaulo return(-1); 398214501Srpaulo } 399214501Srpaulo 400214501Srpaulo /* 401214501Srpaulo * if we were able to create the node break out of the loop, 402214501Srpaulo * otherwise try to unlink the node and try again. if that 403214501Srpaulo * fails check the full path and try a final time. 404214501Srpaulo */ 405214501Srpaulo if (res == 0) 406214501Srpaulo break; 407214501Srpaulo 408214501Srpaulo /* 409214501Srpaulo * we failed to make the node 410214501Srpaulo */ 411214501Srpaulo oerrno = errno; 412214501Srpaulo if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 413214501Srpaulo return(-1); 414214501Srpaulo 415214501Srpaulo if (++pass <= 1) 416214501Srpaulo continue; 417214501Srpaulo 418214501Srpaulo if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 419214501Srpaulo syswarn(1, oerrno, "Could not create: %s", arcn->name); 420214501Srpaulo return(-1); 421214501Srpaulo } 422214501Srpaulo } 423214501Srpaulo 424214501Srpaulo /* 425214501Srpaulo * we were able to create the node. set uid/gid, modes and times 426214501Srpaulo */ 427214501Srpaulo if (pids) 428214501Srpaulo res = ((arcn->type == PAX_SLK) ? 429214501Srpaulo set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) : 430214501Srpaulo set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid)); 431214501Srpaulo else 432214501Srpaulo res = 0; 433214501Srpaulo 434214501Srpaulo /* 435214501Srpaulo * symlinks are done now. 436214501Srpaulo */ 437214501Srpaulo if (arcn->type == PAX_SLK) 438214501Srpaulo return(0); 439214501Srpaulo 440214501Srpaulo /* 441214501Srpaulo * IMPORTANT SECURITY NOTE: 442214501Srpaulo * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 443214501Srpaulo * set uid/gid bits 444214501Srpaulo */ 445214501Srpaulo if (!pmode || res) 446214501Srpaulo arcn->sb.st_mode &= ~(SETBITS); 447214501Srpaulo if (pmode) 448214501Srpaulo set_pmode(arcn->name, arcn->sb.st_mode); 449214501Srpaulo 450214501Srpaulo if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 451214501Srpaulo /* 452214501Srpaulo * Dirs must be processed again at end of extract to set times 453214501Srpaulo * and modes to agree with those stored in the archive. However 454214501Srpaulo * to allow extract to continue, we may have to also set owner 455214501Srpaulo * rights. This allows nodes in the archive that are children 456214501Srpaulo * of this directory to be extracted without failure. Both time 457214501Srpaulo * and modes will be fixed after the entire archive is read and 458214501Srpaulo * before pax exits. 459214501Srpaulo */ 460214501Srpaulo if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 461214501Srpaulo if (lstat(arcn->name, &sb) < 0) { 462214501Srpaulo syswarn(0, errno,"Could not access %s (stat)", 463214501Srpaulo arcn->name); 464214501Srpaulo set_pmode(arcn->name,file_mode | S_IRWXU); 465214501Srpaulo } else { 466214501Srpaulo /* 467214501Srpaulo * We have to add rights to the dir, so we make 468214501Srpaulo * sure to restore the mode. The mode must be 469214501Srpaulo * restored AS CREATED and not as stored if 470214501Srpaulo * pmode is not set. 471214501Srpaulo */ 472214501Srpaulo set_pmode(arcn->name, 473214501Srpaulo ((sb.st_mode & FILEBITS) | S_IRWXU)); 474214501Srpaulo if (!pmode) 475214501Srpaulo arcn->sb.st_mode = sb.st_mode; 476214501Srpaulo } 477214501Srpaulo 478214501Srpaulo /* 479214501Srpaulo * we have to force the mode to what was set here, 480214501Srpaulo * since we changed it from the default as created. 481214501Srpaulo */ 482214501Srpaulo add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 483214501Srpaulo } else if (pmode || patime || pmtime) 484214501Srpaulo add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 485214501Srpaulo } 486214501Srpaulo 487214501Srpaulo if (patime || pmtime) 488214501Srpaulo set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 489214501Srpaulo return(0); 490214501Srpaulo} 491214501Srpaulo 492214501Srpaulo/* 493214501Srpaulo * unlnk_exist() 494214501Srpaulo * Remove node from file system with the specified name. We pass the type 495214501Srpaulo * of the node that is going to replace it. When we try to create a 496214501Srpaulo * directory and find that it already exists, we allow processing to 497214501Srpaulo * continue as proper modes etc will always be set for it later on. 498214501Srpaulo * Return: 499214501Srpaulo * 0 is ok to proceed, no file with the specified name exists 500214501Srpaulo * -1 we were unable to remove the node, or we should not remove it (-k) 501214501Srpaulo * 1 we found a directory and we were going to create a directory. 502214501Srpaulo */ 503214501Srpaulo 504214501Srpauloint 505214501Srpaulounlnk_exist(char *name, int type) 506214501Srpaulo{ 507214501Srpaulo struct stat sb; 508214501Srpaulo 509214501Srpaulo /* 510214501Srpaulo * the file does not exist, or -k we are done 511214501Srpaulo */ 512214501Srpaulo if (lstat(name, &sb) < 0) 513214501Srpaulo return(0); 514214501Srpaulo if (kflag) 515214501Srpaulo return(-1); 516214501Srpaulo 517214501Srpaulo if (S_ISDIR(sb.st_mode)) { 518214501Srpaulo /* 519214501Srpaulo * try to remove a directory, if it fails and we were going to 520214501Srpaulo * create a directory anyway, tell the caller (return a 1) 521214501Srpaulo */ 522214501Srpaulo if (rmdir(name) < 0) { 523214501Srpaulo if (type == PAX_DIR) 524214501Srpaulo return(1); 525214501Srpaulo syswarn(1,errno,"Unable to remove directory %s", name); 526214501Srpaulo return(-1); 527214501Srpaulo } 528214501Srpaulo return(0); 529214501Srpaulo } 530214501Srpaulo 531214501Srpaulo /* 532214501Srpaulo * try to get rid of all non-directory type nodes 533214501Srpaulo */ 534214501Srpaulo if (unlink(name) < 0) { 535214501Srpaulo syswarn(1, errno, "Could not unlink %s", name); 536214501Srpaulo return(-1); 537214501Srpaulo } 538214501Srpaulo return(0); 539214501Srpaulo} 540214501Srpaulo 541214501Srpaulo/* 542214501Srpaulo * chk_path() 543214501Srpaulo * We were trying to create some kind of node in the file system and it 544214501Srpaulo * failed. chk_path() makes sure the path up to the node exists and is 545214501Srpaulo * writeable. When we have to create a directory that is missing along the 546214501Srpaulo * path somewhere, the directory we create will be set to the same 547214501Srpaulo * uid/gid as the file has (when uid and gid are being preserved). 548214501Srpaulo * NOTE: this routine is a real performance loss. It is only used as a 549214501Srpaulo * last resort when trying to create entries in the file system. 550214501Srpaulo * Return: 551214501Srpaulo * -1 when it could find nothing it is allowed to fix. 552214501Srpaulo * 0 otherwise 553214501Srpaulo */ 554214501Srpaulo 555214501Srpauloint 556214501Srpaulochk_path( char *name, uid_t st_uid, gid_t st_gid) 557214501Srpaulo{ 558214501Srpaulo char *spt = name; 559214501Srpaulo struct stat sb; 560214501Srpaulo int retval = -1; 561214501Srpaulo 562214501Srpaulo /* 563214501Srpaulo * watch out for paths with nodes stored directly in / (e.g. /bozo) 564214501Srpaulo */ 565214501Srpaulo if (*spt == '/') 566214501Srpaulo ++spt; 567214501Srpaulo 568214501Srpaulo for(;;) { 569214501Srpaulo /* 570214501Srpaulo * work foward from the first / and check each part of the path 571214501Srpaulo */ 572214501Srpaulo spt = strchr(spt, '/'); 573214501Srpaulo if (spt == NULL) 574214501Srpaulo break; 575214501Srpaulo *spt = '\0'; 576214501Srpaulo 577214501Srpaulo /* 578214501Srpaulo * if it exists we assume it is a directory, it is not within 579214501Srpaulo * the spec (at least it seems to read that way) to alter the 580214501Srpaulo * file system for nodes NOT EXPLICITLY stored on the archive. 581214501Srpaulo * If that assumption is changed, you would test the node here 582214501Srpaulo * and figure out how to get rid of it (probably like some 583214501Srpaulo * recursive unlink()) or fix up the directory permissions if 584214501Srpaulo * required (do an access()). 585214501Srpaulo */ 586214501Srpaulo if (lstat(name, &sb) == 0) { 587214501Srpaulo *(spt++) = '/'; 588214501Srpaulo continue; 589214501Srpaulo } 590214501Srpaulo 591214501Srpaulo /* 592214501Srpaulo * the path fails at this point, see if we can create the 593214501Srpaulo * needed directory and continue on 594214501Srpaulo */ 595214501Srpaulo if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 596214501Srpaulo *spt = '/'; 597214501Srpaulo retval = -1; 598214501Srpaulo break; 599214501Srpaulo } 600214501Srpaulo 601214501Srpaulo /* 602214501Srpaulo * we were able to create the directory. We will tell the 603214501Srpaulo * caller that we found something to fix, and it is ok to try 604214501Srpaulo * and create the node again. 605214501Srpaulo */ 606214501Srpaulo retval = 0; 607214501Srpaulo if (pids) 608214501Srpaulo (void)set_ids(name, st_uid, st_gid); 609214501Srpaulo 610214501Srpaulo /* 611214501Srpaulo * make sure the user doen't have some strange umask that 612214501Srpaulo * causes this newly created directory to be unusable. We fix 613214501Srpaulo * the modes and restore them back to the creation default at 614214501Srpaulo * the end of pax 615214501Srpaulo */ 616214501Srpaulo if ((access(name, R_OK | W_OK | X_OK) < 0) && 617214501Srpaulo (lstat(name, &sb) == 0)) { 618214501Srpaulo set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 619214501Srpaulo add_dir(name, spt - name, &sb, 1); 620214501Srpaulo } 621214501Srpaulo *(spt++) = '/'; 622214501Srpaulo continue; 623214501Srpaulo } 624214501Srpaulo return(retval); 625214501Srpaulo} 626214501Srpaulo 627214501Srpaulo/* 628214501Srpaulo * set_ftime() 629 * Set the access time and modification time for a named file. If frc is 630 * non-zero we force these times to be set even if the user did not 631 * request access and/or modification time preservation (this is also 632 * used by -t to reset access times). 633 * When ign is zero, only those times the user has asked for are set, the 634 * other ones are left alone. We do not assume the un-documented feature 635 * of many utimes() implementations that consider a 0 time value as a do 636 * not set request. 637 */ 638 639void 640set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 641{ 642 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 643 struct stat sb; 644 645 tv[0].tv_sec = atime; 646 tv[1].tv_sec = mtime; 647 if (!frc && (!patime || !pmtime)) { 648 /* 649 * if we are not forcing, only set those times the user wants 650 * set. We get the current values of the times if we need them. 651 */ 652 if (lstat(fnm, &sb) == 0) { 653 if (!patime) 654 tv[0].tv_sec = sb.st_atime; 655 if (!pmtime) 656 tv[1].tv_sec = sb.st_mtime; 657 } else 658 syswarn(0,errno,"Unable to obtain file stats %s", fnm); 659 } 660 661 /* 662 * set the times 663 */ 664 if (utimes(fnm, tv) < 0) 665 syswarn(1, errno, "Access/modification time set failed on: %s", 666 fnm); 667 return; 668} 669 670/* 671 * set_ids() 672 * set the uid and gid of a file system node 673 * Return: 674 * 0 when set, -1 on failure 675 */ 676 677int 678set_ids(char *fnm, uid_t uid, gid_t gid) 679{ 680 if (chown(fnm, uid, gid) < 0) { 681 /* 682 * ignore EPERM unless in verbose mode or being run by root. 683 * if running as pax, POSIX requires a warning. 684 */ 685 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 686 geteuid() == 0) 687 syswarn(1, errno, "Unable to set file uid/gid of %s", 688 fnm); 689 return(-1); 690 } 691 return(0); 692} 693 694/* 695 * set_lids() 696 * set the uid and gid of a file system node 697 * Return: 698 * 0 when set, -1 on failure 699 */ 700 701int 702set_lids(char *fnm, uid_t uid, gid_t gid) 703{ 704 if (lchown(fnm, uid, gid) < 0) { 705 /* 706 * ignore EPERM unless in verbose mode or being run by root. 707 * if running as pax, POSIX requires a warning. 708 */ 709 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 710 geteuid() == 0) 711 syswarn(1, errno, "Unable to set file uid/gid of %s", 712 fnm); 713 return(-1); 714 } 715 return(0); 716} 717 718/* 719 * set_pmode() 720 * Set file access mode 721 */ 722 723void 724set_pmode(char *fnm, mode_t mode) 725{ 726 mode &= ABITS; 727 if (chmod(fnm, mode) < 0) 728 syswarn(1, errno, "Could not set permissions on %s", fnm); 729 return; 730} 731 732/* 733 * file_write() 734 * Write/copy a file (during copy or archive extract). This routine knows 735 * how to copy files with lseek holes in it. (Which are read as file 736 * blocks containing all 0's but do not have any file blocks associated 737 * with the data). Typical examples of these are files created by dbm 738 * variants (.pag files). While the file size of these files are huge, the 739 * actual storage is quite small (the files are sparse). The problem is 740 * the holes read as all zeros so are probably stored on the archive that 741 * way (there is no way to determine if the file block is really a hole, 742 * we only know that a file block of all zero's can be a hole). 743 * At this writing, no major archive format knows how to archive files 744 * with holes. However, on extraction (or during copy, -rw) we have to 745 * deal with these files. Without detecting the holes, the files can 746 * consume a lot of file space if just written to disk. This replacement 747 * for write when passed the basic allocation size of a file system block, 748 * uses lseek whenever it detects the input data is all 0 within that 749 * file block. In more detail, the strategy is as follows: 750 * While the input is all zero keep doing an lseek. Keep track of when we 751 * pass over file block boundries. Only write when we hit a non zero 752 * input. once we have written a file block, we continue to write it to 753 * the end (we stop looking at the input). When we reach the start of the 754 * next file block, start checking for zero blocks again. Working on file 755 * block boundries significantly reduces the overhead when copying files 756 * that are NOT very sparse. This overhead (when compared to a write) is 757 * almost below the measurement resolution on many systems. Without it, 758 * files with holes cannot be safely copied. It does has a side effect as 759 * it can put holes into files that did not have them before, but that is 760 * not a problem since the file contents are unchanged (in fact it saves 761 * file space). (Except on paging files for diskless clients. But since we 762 * cannot determine one of those file from here, we ignore them). If this 763 * ever ends up on a system where CTG files are supported and the holes 764 * are not desired, just do a conditional test in those routines that 765 * call file_write() and have it call write() instead. BEFORE CLOSING THE 766 * FILE, make sure to call file_flush() when the last write finishes with 767 * an empty block. A lot of file systems will not create an lseek hole at 768 * the end. In this case we drop a single 0 at the end to force the 769 * trailing 0's in the file. 770 * ---Parameters--- 771 * rem: how many bytes left in this file system block 772 * isempt: have we written to the file block yet (is it empty) 773 * sz: basic file block allocation size 774 * cnt: number of bytes on this write 775 * str: buffer to write 776 * Return: 777 * number of bytes written, -1 on write (or lseek) error. 778 */ 779 780int 781file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 782 char *name) 783{ 784 char *pt; 785 char *end; 786 int wcnt; 787 char *st = str; 788 789 /* 790 * while we have data to process 791 */ 792 while (cnt) { 793 if (!*rem) { 794 /* 795 * We are now at the start of file system block again 796 * (or what we think one is...). start looking for 797 * empty blocks again 798 */ 799 *isempt = 1; 800 *rem = sz; 801 } 802 803 /* 804 * only examine up to the end of the current file block or 805 * remaining characters to write, whatever is smaller 806 */ 807 wcnt = MIN(cnt, *rem); 808 cnt -= wcnt; 809 *rem -= wcnt; 810 if (*isempt) { 811 /* 812 * have not written to this block yet, so we keep 813 * looking for zero's 814 */ 815 pt = st; 816 end = st + wcnt; 817 818 /* 819 * look for a zero filled buffer 820 */ 821 while ((pt < end) && (*pt == '\0')) 822 ++pt; 823 824 if (pt == end) { 825 /* 826 * skip, buf is empty so far 827 */ 828 if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 829 syswarn(1,errno,"File seek on %s", 830 name); 831 return(-1); 832 } 833 st = pt; 834 continue; 835 } 836 /* 837 * drat, the buf is not zero filled 838 */ 839 *isempt = 0; 840 } 841 842 /* 843 * have non-zero data in this file system block, have to write 844 */ 845 if (write(fd, st, wcnt) != wcnt) { 846 syswarn(1, errno, "Failed write to file %s", name); 847 return(-1); 848 } 849 st += wcnt; 850 } 851 return(st - str); 852} 853 854/* 855 * file_flush() 856 * when the last file block in a file is zero, many file systems will not 857 * let us create a hole at the end. To get the last block with zeros, we 858 * write the last BYTE with a zero (back up one byte and write a zero). 859 */ 860 861void 862file_flush(int fd, char *fname, int isempt) 863{ 864 static char blnk[] = "\0"; 865 866 /* 867 * silly test, but make sure we are only called when the last block is 868 * filled with all zeros. 869 */ 870 if (!isempt) 871 return; 872 873 /* 874 * move back one byte and write a zero 875 */ 876 if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 877 syswarn(1, errno, "Failed seek on file %s", fname); 878 return; 879 } 880 881 if (write(fd, blnk, 1) < 0) 882 syswarn(1, errno, "Failed write to file %s", fname); 883 return; 884} 885 886/* 887 * rdfile_close() 888 * close a file we have beed reading (to copy or archive). If we have to 889 * reset access time (tflag) do so (the times are stored in arcn). 890 */ 891 892void 893rdfile_close(ARCHD *arcn, int *fd) 894{ 895 /* 896 * make sure the file is open 897 */ 898 if (*fd < 0) 899 return; 900 901 (void)close(*fd); 902 *fd = -1; 903 if (!tflag) 904 return; 905 906 /* 907 * user wants last access time reset 908 */ 909 set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 910 return; 911} 912 913/* 914 * set_crc() 915 * read a file to calculate its crc. This is a real drag. Archive formats 916 * that have this, end up reading the file twice (we have to write the 917 * header WITH the crc before writing the file contents. Oh well... 918 * Return: 919 * 0 if was able to calculate the crc, -1 otherwise 920 */ 921 922int 923set_crc(ARCHD *arcn, int fd) 924{ 925 int i; 926 int res; 927 off_t cpcnt = 0L; 928 u_long size; 929 unsigned long crc = 0L; 930 char tbuf[FILEBLK]; 931 struct stat sb; 932 933 if (fd < 0) { 934 /* 935 * hmm, no fd, should never happen. well no crc then. 936 */ 937 arcn->crc = 0L; 938 return(0); 939 } 940 941 if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 942 size = (u_long)sizeof(tbuf); 943 944 /* 945 * read all the bytes we think that there are in the file. If the user 946 * is trying to archive an active file, forget this file. 947 */ 948 for(;;) { 949 if ((res = read(fd, tbuf, size)) <= 0) 950 break; 951 cpcnt += res; 952 for (i = 0; i < res; ++i) 953 crc += (tbuf[i] & 0xff); 954 } 955 956 /* 957 * safety check. we want to avoid archiving files that are active as 958 * they can create inconsistant archive copies. 959 */ 960 if (cpcnt != arcn->sb.st_size) 961 paxwarn(1, "File changed size %s", arcn->org_name); 962 else if (fstat(fd, &sb) < 0) 963 syswarn(1, errno, "Failed stat on %s", arcn->org_name); 964 else if (arcn->sb.st_mtime != sb.st_mtime) 965 paxwarn(1, "File %s was modified during read", arcn->org_name); 966 else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 967 syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 968 else { 969 arcn->crc = crc; 970 return(0); 971 } 972 return(-1); 973} 974