00001 /*- 00002 * Copyright (c) 1992 Keith Muller. 00003 * Copyright (c) 1992, 1993 00004 * The Regents of the University of California. All rights reserved. 00005 * 00006 * This code is derived from software contributed to Berkeley by 00007 * Keith Muller of the University of California, San Diego. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 2. Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the distribution. 00017 * 4. Neither the name of the University nor the names of its contributors 00018 * may be used to endorse or promote products derived from this software 00019 * without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 */ 00033 00034 #ifndef lint 00035 #if 0 00036 static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 00037 #endif 00038 #endif /* not lint */ 00039 00040 #include <sys/types.h> 00041 #include <sys/time.h> 00042 #include <sys/stat.h> 00043 #include <unistd.h> 00044 #include <fcntl.h> 00045 #include <string.h> 00046 #include <stdio.h> 00047 #include <errno.h> 00048 #include <utime.h> 00049 #include <sys/uio.h> 00050 #include <stdlib.h> 00051 #include "pax.h" 00052 #include "options.h" 00053 #include "extern.h" 00054 00055 static int 00056 mk_link(char *,struct stat *,char *, int); 00057 00058 /* 00059 * routines that deal with file operations such as: creating, removing; 00060 * and setting access modes, uid/gid and times of files 00061 */ 00062 00063 #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 00064 #define SETBITS (S_ISUID | S_ISGID) 00065 #define ABITS (FILEBITS | SETBITS) 00066 00067 /* 00068 * file_creat() 00069 * Create and open a file. 00070 * Return: 00071 * file descriptor or -1 for failure 00072 */ 00073 00074 int 00075 file_creat(ARCHD *arcn) 00076 { 00077 int fd = -1; 00078 mode_t file_mode; 00079 int oerrno; 00080 00081 /* 00082 * assume file doesn't exist, so just try to create it, most times this 00083 * works. We have to take special handling when the file does exist. To 00084 * detect this, we use O_EXCL. For example when trying to create a 00085 * file and a character device or fifo exists with the same name, we 00086 * can accidently open the device by mistake (or block waiting to open) 00087 * If we find that the open has failed, then figure spend the effort to 00088 * figure out why. This strategy was found to have better average 00089 * performance in common use than checking the file (and the path) 00090 * first with lstat. 00091 */ 00092 file_mode = arcn->sb.st_mode & FILEBITS; 00093 if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 00094 file_mode)) >= 0) 00095 return(fd); 00096 00097 /* 00098 * the file seems to exist. First we try to get rid of it (found to be 00099 * the second most common failure when traced). If this fails, only 00100 * then we go to the expense to check and create the path to the file 00101 */ 00102 if (unlnk_exist(arcn->name, arcn->type) != 0) 00103 return(-1); 00104 00105 for (;;) { 00106 /* 00107 * try to open it again, if this fails, check all the nodes in 00108 * the path and give it a final try. if chk_path() finds that 00109 * it cannot fix anything, we will skip the last attempt 00110 */ 00111 if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, 00112 file_mode)) >= 0) 00113 break; 00114 oerrno = errno; 00115 if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 00116 syswarn(1, oerrno, "Unable to create %s", arcn->name); 00117 return(-1); 00118 } 00119 } 00120 return(fd); 00121 } 00122 00123 /* 00124 * file_close() 00125 * Close file descriptor to a file just created by pax. Sets modes, 00126 * ownership and times as required. 00127 * Return: 00128 * 0 for success, -1 for failure 00129 */ 00130 00131 void 00132 file_close(ARCHD *arcn, int fd) 00133 { 00134 int res = 0; 00135 00136 if (fd < 0) 00137 return; 00138 if (close(fd) < 0) 00139 syswarn(0, errno, "Unable to close file descriptor on %s", 00140 arcn->name); 00141 00142 /* 00143 * set owner/groups first as this may strip off mode bits we want 00144 * then set file permission modes. Then set file access and 00145 * modification times. 00146 */ 00147 if (pids) 00148 res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid); 00149 00150 /* 00151 * IMPORTANT SECURITY NOTE: 00152 * if not preserving mode or we cannot set uid/gid, then PROHIBIT 00153 * set uid/gid bits 00154 */ 00155 if (!pmode || res) 00156 arcn->sb.st_mode &= ~(SETBITS); 00157 if (pmode) 00158 set_pmode(arcn->name, arcn->sb.st_mode); 00159 if (patime || pmtime) 00160 set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 00161 } 00162 00163 /* 00164 * lnk_creat() 00165 * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 00166 * must exist; 00167 * Return: 00168 * 0 if ok, -1 otherwise 00169 */ 00170 00171 int 00172 lnk_creat(ARCHD *arcn) 00173 { 00174 struct stat sb; 00175 00176 /* 00177 * we may be running as root, so we have to be sure that link target 00178 * is not a directory, so we lstat and check 00179 */ 00180 if (lstat(arcn->ln_name, &sb) < 0) { 00181 syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, 00182 arcn->name); 00183 return(-1); 00184 } 00185 00186 if (S_ISDIR(sb.st_mode)) { 00187 paxwarn(1, "A hard link to the directory %s is not allowed", 00188 arcn->ln_name); 00189 return(-1); 00190 } 00191 00192 return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 00193 } 00194 00195 /* 00196 * cross_lnk() 00197 * Create a hard link to arcn->org_name from arcn->name. Only used in copy 00198 * with the -l flag. No warning or error if this does not succeed (we will 00199 * then just create the file) 00200 * Return: 00201 * 1 if copy() should try to create this file node 00202 * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 00203 */ 00204 00205 int 00206 cross_lnk(ARCHD *arcn) 00207 { 00208 /* 00209 * try to make a link to original file (-l flag in copy mode). make sure 00210 * we do not try to link to directories in case we are running as root 00211 * (and it might succeed). 00212 */ 00213 if (arcn->type == PAX_DIR) 00214 return(1); 00215 return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 00216 } 00217 00218 /* 00219 * chk_same() 00220 * In copy mode if we are not trying to make hard links between the src 00221 * and destinations, make sure we are not going to overwrite ourselves by 00222 * accident. This slows things down a little, but we have to protect all 00223 * those people who make typing errors. 00224 * Return: 00225 * 1 the target does not exist, go ahead and copy 00226 * 0 skip it file exists (-k) or may be the same as source file 00227 */ 00228 00229 int 00230 chk_same(ARCHD *arcn) 00231 { 00232 struct stat sb; 00233 00234 /* 00235 * if file does not exist, return. if file exists and -k, skip it 00236 * quietly 00237 */ 00238 if (lstat(arcn->name, &sb) < 0) 00239 return(1); 00240 if (kflag) 00241 return(0); 00242 00243 /* 00244 * better make sure the user does not have src == dest by mistake 00245 */ 00246 if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 00247 paxwarn(1, "Unable to copy %s, file would overwrite itself", 00248 arcn->name); 00249 return(0); 00250 } 00251 return(1); 00252 } 00253 00254 /* 00255 * mk_link() 00256 * try to make a hard link between two files. if ign set, we do not 00257 * complain. 00258 * Return: 00259 * 0 if successful (or we are done with this file but no error, such as 00260 * finding the from file exists and the user has set -k). 00261 * 1 when ign was set to indicates we could not make the link but we 00262 * should try to copy/extract the file as that might work (and is an 00263 * allowed option). -1 an error occurred. 00264 */ 00265 00266 static int 00267 mk_link(char *to, struct stat *to_sb, char *from, 00268 int ign) 00269 { 00270 struct stat sb; 00271 int oerrno; 00272 00273 /* 00274 * if from file exists, it has to be unlinked to make the link. If the 00275 * file exists and -k is set, skip it quietly 00276 */ 00277 if (lstat(from, &sb) == 0) { 00278 if (kflag) 00279 return(0); 00280 00281 /* 00282 * make sure it is not the same file, protect the user 00283 */ 00284 if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 00285 paxwarn(1, "Unable to link file %s to itself", to); 00286 return(-1);; 00287 } 00288 00289 /* 00290 * try to get rid of the file, based on the type 00291 */ 00292 if (S_ISDIR(sb.st_mode)) { 00293 if (rmdir(from) < 0) { 00294 syswarn(1, errno, "Unable to remove %s", from); 00295 return(-1); 00296 } 00297 } else if (unlink(from) < 0) { 00298 if (!ign) { 00299 syswarn(1, errno, "Unable to remove %s", from); 00300 return(-1); 00301 } 00302 return(1); 00303 } 00304 } 00305 00306 /* 00307 * from file is gone (or did not exist), try to make the hard link. 00308 * if it fails, check the path and try it again (if chk_path() says to 00309 * try again) 00310 */ 00311 for (;;) { 00312 if (link(to, from) == 0) 00313 break; 00314 oerrno = errno; 00315 if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 00316 continue; 00317 if (!ign) { 00318 syswarn(1, oerrno, "Could not link to %s from %s", to, 00319 from); 00320 return(-1); 00321 } 00322 return(1); 00323 } 00324 00325 /* 00326 * all right the link was made 00327 */ 00328 return(0); 00329 } 00330 00331 /* 00332 * node_creat() 00333 * create an entry in the file system (other than a file or hard link). 00334 * If successful, sets uid/gid modes and times as required. 00335 * Return: 00336 * 0 if ok, -1 otherwise 00337 */ 00338 00339 int 00340 node_creat(ARCHD *arcn) 00341 { 00342 int res; 00343 int ign = 0; 00344 int oerrno; 00345 int pass = 0; 00346 mode_t file_mode; 00347 struct stat sb; 00348 00349 /* 00350 * create node based on type, if that fails try to unlink the node and 00351 * try again. finally check the path and try again. As noted in the 00352 * file and link creation routines, this method seems to exhibit the 00353 * best performance in general use workloads. 00354 */ 00355 file_mode = arcn->sb.st_mode & FILEBITS; 00356 00357 for (;;) { 00358 switch(arcn->type) { 00359 case PAX_DIR: 00360 res = mkdir(arcn->name, file_mode); 00361 if (ign) 00362 res = 0; 00363 break; 00364 case PAX_CHR: 00365 file_mode |= S_IFCHR; 00366 res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 00367 break; 00368 case PAX_BLK: 00369 file_mode |= S_IFBLK; 00370 res = mknod(arcn->name, file_mode, arcn->sb.st_rdev); 00371 break; 00372 case PAX_FIF: 00373 res = mkfifo(arcn->name, file_mode); 00374 break; 00375 case PAX_SCK: 00376 /* 00377 * Skip sockets, operation has no meaning under BSD 00378 */ 00379 paxwarn(0, 00380 "%s skipped. Sockets cannot be copied or extracted", 00381 arcn->name); 00382 return(-1); 00383 case PAX_SLK: 00384 res = symlink(arcn->ln_name, arcn->name); 00385 break; 00386 case PAX_CTG: 00387 case PAX_HLK: 00388 case PAX_HRG: 00389 case PAX_REG: 00390 default: 00391 /* 00392 * we should never get here 00393 */ 00394 paxwarn(0, "%s has an unknown file type, skipping", 00395 arcn->name); 00396 return(-1); 00397 } 00398 00399 /* 00400 * if we were able to create the node break out of the loop, 00401 * otherwise try to unlink the node and try again. if that 00402 * fails check the full path and try a final time. 00403 */ 00404 if (res == 0) 00405 break; 00406 00407 /* 00408 * we failed to make the node 00409 */ 00410 oerrno = errno; 00411 if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0) 00412 return(-1); 00413 00414 if (++pass <= 1) 00415 continue; 00416 00417 if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 00418 syswarn(1, oerrno, "Could not create: %s", arcn->name); 00419 return(-1); 00420 } 00421 } 00422 00423 /* 00424 * we were able to create the node. set uid/gid, modes and times 00425 */ 00426 if (pids) 00427 res = ((arcn->type == PAX_SLK) ? 00428 set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) : 00429 set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid)); 00430 else 00431 res = 0; 00432 00433 /* 00434 * symlinks are done now. 00435 */ 00436 if (arcn->type == PAX_SLK) 00437 return(0); 00438 00439 /* 00440 * IMPORTANT SECURITY NOTE: 00441 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 00442 * set uid/gid bits 00443 */ 00444 if (!pmode || res) 00445 arcn->sb.st_mode &= ~(SETBITS); 00446 if (pmode) 00447 set_pmode(arcn->name, arcn->sb.st_mode); 00448 00449 if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 00450 /* 00451 * Dirs must be processed again at end of extract to set times 00452 * and modes to agree with those stored in the archive. However 00453 * to allow extract to continue, we may have to also set owner 00454 * rights. This allows nodes in the archive that are children 00455 * of this directory to be extracted without failure. Both time 00456 * and modes will be fixed after the entire archive is read and 00457 * before pax exits. 00458 */ 00459 if (access(arcn->name, R_OK | W_OK | X_OK) < 0) { 00460 if (lstat(arcn->name, &sb) < 0) { 00461 syswarn(0, errno,"Could not access %s (stat)", 00462 arcn->name); 00463 set_pmode(arcn->name,file_mode | S_IRWXU); 00464 } else { 00465 /* 00466 * We have to add rights to the dir, so we make 00467 * sure to restore the mode. The mode must be 00468 * restored AS CREATED and not as stored if 00469 * pmode is not set. 00470 */ 00471 set_pmode(arcn->name, 00472 ((sb.st_mode & FILEBITS) | S_IRWXU)); 00473 if (!pmode) 00474 arcn->sb.st_mode = sb.st_mode; 00475 } 00476 00477 /* 00478 * we have to force the mode to what was set here, 00479 * since we changed it from the default as created. 00480 */ 00481 add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1); 00482 } else if (pmode || patime || pmtime) 00483 add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0); 00484 } 00485 00486 if (patime || pmtime) 00487 set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 00488 return(0); 00489 } 00490 00491 /* 00492 * unlnk_exist() 00493 * Remove node from file system with the specified name. We pass the type 00494 * of the node that is going to replace it. When we try to create a 00495 * directory and find that it already exists, we allow processing to 00496 * continue as proper modes etc will always be set for it later on. 00497 * Return: 00498 * 0 is ok to proceed, no file with the specified name exists 00499 * -1 we were unable to remove the node, or we should not remove it (-k) 00500 * 1 we found a directory and we were going to create a directory. 00501 */ 00502 00503 int 00504 unlnk_exist(char *name, int type) 00505 { 00506 struct stat sb; 00507 00508 /* 00509 * the file does not exist, or -k we are done 00510 */ 00511 if (lstat(name, &sb) < 0) 00512 return(0); 00513 if (kflag) 00514 return(-1); 00515 00516 if (S_ISDIR(sb.st_mode)) { 00517 /* 00518 * try to remove a directory, if it fails and we were going to 00519 * create a directory anyway, tell the caller (return a 1) 00520 */ 00521 if (rmdir(name) < 0) { 00522 if (type == PAX_DIR) 00523 return(1); 00524 syswarn(1,errno,"Unable to remove directory %s", name); 00525 return(-1); 00526 } 00527 return(0); 00528 } 00529 00530 /* 00531 * try to get rid of all non-directory type nodes 00532 */ 00533 if (unlink(name) < 0) { 00534 syswarn(1, errno, "Could not unlink %s", name); 00535 return(-1); 00536 } 00537 return(0); 00538 } 00539 00540 /* 00541 * chk_path() 00542 * We were trying to create some kind of node in the file system and it 00543 * failed. chk_path() makes sure the path up to the node exists and is 00544 * writeable. When we have to create a directory that is missing along the 00545 * path somewhere, the directory we create will be set to the same 00546 * uid/gid as the file has (when uid and gid are being preserved). 00547 * NOTE: this routine is a real performance loss. It is only used as a 00548 * last resort when trying to create entries in the file system. 00549 * Return: 00550 * -1 when it could find nothing it is allowed to fix. 00551 * 0 otherwise 00552 */ 00553 00554 int 00555 chk_path( char *name, uid_t st_uid, gid_t st_gid) 00556 { 00557 char *spt = name; 00558 struct stat sb; 00559 int retval = -1; 00560 00561 /* 00562 * watch out for paths with nodes stored directly in / (e.g. /bozo) 00563 */ 00564 if (*spt == '/') 00565 ++spt; 00566 00567 for(;;) { 00568 /* 00569 * work foward from the first / and check each part of the path 00570 */ 00571 spt = strchr(spt, '/'); 00572 if (spt == NULL) 00573 break; 00574 *spt = '\0'; 00575 00576 /* 00577 * if it exists we assume it is a directory, it is not within 00578 * the spec (at least it seems to read that way) to alter the 00579 * file system for nodes NOT EXPLICITLY stored on the archive. 00580 * If that assumption is changed, you would test the node here 00581 * and figure out how to get rid of it (probably like some 00582 * recursive unlink()) or fix up the directory permissions if 00583 * required (do an access()). 00584 */ 00585 if (lstat(name, &sb) == 0) { 00586 *(spt++) = '/'; 00587 continue; 00588 } 00589 00590 /* 00591 * the path fails at this point, see if we can create the 00592 * needed directory and continue on 00593 */ 00594 if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 00595 *spt = '/'; 00596 retval = -1; 00597 break; 00598 } 00599 00600 /* 00601 * we were able to create the directory. We will tell the 00602 * caller that we found something to fix, and it is ok to try 00603 * and create the node again. 00604 */ 00605 retval = 0; 00606 if (pids) 00607 (void)set_ids(name, st_uid, st_gid); 00608 00609 /* 00610 * make sure the user doen't have some strange umask that 00611 * causes this newly created directory to be unusable. We fix 00612 * the modes and restore them back to the creation default at 00613 * the end of pax 00614 */ 00615 if ((access(name, R_OK | W_OK | X_OK) < 0) && 00616 (lstat(name, &sb) == 0)) { 00617 set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 00618 add_dir(name, spt - name, &sb, 1); 00619 } 00620 *(spt++) = '/'; 00621 continue; 00622 } 00623 return(retval); 00624 } 00625 00626 /* 00627 * set_ftime() 00628 * Set the access time and modification time for a named file. If frc is 00629 * non-zero we force these times to be set even if the user did not 00630 * request access and/or modification time preservation (this is also 00631 * used by -t to reset access times). 00632 * When ign is zero, only those times the user has asked for are set, the 00633 * other ones are left alone. We do not assume the un-documented feature 00634 * of many utimes() implementations that consider a 0 time value as a do 00635 * not set request. 00636 */ 00637 00638 void 00639 set_ftime(char *fnm, time_t mtime, time_t atime, int frc) 00640 { 00641 #if 0 00642 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}}; 00643 #endif 00644 struct stat sb; 00645 struct utimbuf ut; 00646 00647 #if 0 00648 tv[0].tv_sec = atime; 00649 tv[1].tv_sec = mtime; 00650 #endif 00651 00652 ut.actime = atime; 00653 ut.modtime = mtime; 00654 00655 if (!frc && (!patime || !pmtime)) { 00656 /* 00657 * if we are not forcing, only set those times the user wants 00658 * set. We get the current values of the times if we need them. 00659 */ 00660 if (lstat(fnm, &sb) == 0) { 00661 if (!patime) 00662 ut.actime = sb.st_atime; 00663 if (!pmtime) 00664 ut.modtime = sb.st_mtime; 00665 } else 00666 syswarn(0,errno,"Unable to obtain file stats %s", fnm); 00667 } 00668 00669 /* 00670 * set the times 00671 */ 00672 if (utime(fnm, &ut) < 0) 00673 syswarn(1, errno, "Access/modification time set failed on: %s", 00674 fnm); 00675 return; 00676 } 00677 00678 /* 00679 * set_ids() 00680 * set the uid and gid of a file system node 00681 * Return: 00682 * 0 when set, -1 on failure 00683 */ 00684 00685 int 00686 set_ids(char *fnm, uid_t uid, gid_t gid) 00687 { 00688 if (chown(fnm, uid, gid) < 0) { 00689 /* 00690 * ignore EPERM unless in verbose mode or being run by root. 00691 * if running as pax, POSIX requires a warning. 00692 */ 00693 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 00694 geteuid() == 0) 00695 syswarn(1, errno, "Unable to set file uid/gid of %s", 00696 fnm); 00697 return(-1); 00698 } 00699 return(0); 00700 } 00701 00702 /* 00703 * set_lids() 00704 * set the uid and gid of a file system node 00705 * Return: 00706 * 0 when set, -1 on failure 00707 */ 00708 00709 int 00710 set_lids(char *fnm, uid_t uid, gid_t gid) 00711 { 00712 00713 #if 0 00714 if (lchown(fnm, uid, gid) < 0) { 00715 /* 00716 * ignore EPERM unless in verbose mode or being run by root. 00717 * if running as pax, POSIX requires a warning. 00718 */ 00719 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || 00720 geteuid() == 0) 00721 syswarn(1, errno, "Unable to set file uid/gid of %s", 00722 fnm); 00723 return(-1); 00724 } 00725 #else 00726 return(-1); /* No lchown() in minix. */ 00727 #endif 00728 return(0); 00729 } 00730 00731 /* 00732 * set_pmode() 00733 * Set file access mode 00734 */ 00735 00736 void 00737 set_pmode(char *fnm, mode_t mode) 00738 { 00739 mode &= ABITS; 00740 if (chmod(fnm, mode) < 0) 00741 syswarn(1, errno, "Could not set permissions on %s", fnm); 00742 return; 00743 } 00744 00745 /* 00746 * file_write() 00747 * Write/copy a file (during copy or archive extract). This routine knows 00748 * how to copy files with lseek holes in it. (Which are read as file 00749 * blocks containing all 0's but do not have any file blocks associated 00750 * with the data). Typical examples of these are files created by dbm 00751 * variants (.pag files). While the file size of these files are huge, the 00752 * actual storage is quite small (the files are sparse). The problem is 00753 * the holes read as all zeros so are probably stored on the archive that 00754 * way (there is no way to determine if the file block is really a hole, 00755 * we only know that a file block of all zero's can be a hole). 00756 * At this writing, no major archive format knows how to archive files 00757 * with holes. However, on extraction (or during copy, -rw) we have to 00758 * deal with these files. Without detecting the holes, the files can 00759 * consume a lot of file space if just written to disk. This replacement 00760 * for write when passed the basic allocation size of a file system block, 00761 * uses lseek whenever it detects the input data is all 0 within that 00762 * file block. In more detail, the strategy is as follows: 00763 * While the input is all zero keep doing an lseek. Keep track of when we 00764 * pass over file block boundries. Only write when we hit a non zero 00765 * input. once we have written a file block, we continue to write it to 00766 * the end (we stop looking at the input). When we reach the start of the 00767 * next file block, start checking for zero blocks again. Working on file 00768 * block boundries significantly reduces the overhead when copying files 00769 * that are NOT very sparse. This overhead (when compared to a write) is 00770 * almost below the measurement resolution on many systems. Without it, 00771 * files with holes cannot be safely copied. It does has a side effect as 00772 * it can put holes into files that did not have them before, but that is 00773 * not a problem since the file contents are unchanged (in fact it saves 00774 * file space). (Except on paging files for diskless clients. But since we 00775 * cannot determine one of those file from here, we ignore them). If this 00776 * ever ends up on a system where CTG files are supported and the holes 00777 * are not desired, just do a conditional test in those routines that 00778 * call file_write() and have it call write() instead. BEFORE CLOSING THE 00779 * FILE, make sure to call file_flush() when the last write finishes with 00780 * an empty block. A lot of file systems will not create an lseek hole at 00781 * the end. In this case we drop a single 0 at the end to force the 00782 * trailing 0's in the file. 00783 * ---Parameters--- 00784 * rem: how many bytes left in this file system block 00785 * isempt: have we written to the file block yet (is it empty) 00786 * sz: basic file block allocation size 00787 * cnt: number of bytes on this write 00788 * str: buffer to write 00789 * Return: 00790 * number of bytes written, -1 on write (or lseek) error. 00791 */ 00792 00793 int 00794 file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 00795 char *name) 00796 { 00797 char *pt; 00798 char *end; 00799 int wcnt; 00800 char *st = str; 00801 00802 /* 00803 * while we have data to process 00804 */ 00805 while (cnt) { 00806 if (!*rem) { 00807 /* 00808 * We are now at the start of file system block again 00809 * (or what we think one is...). start looking for 00810 * empty blocks again 00811 */ 00812 *isempt = 1; 00813 *rem = sz; 00814 } 00815 00816 /* 00817 * only examine up to the end of the current file block or 00818 * remaining characters to write, whatever is smaller 00819 */ 00820 wcnt = MIN(cnt, *rem); 00821 cnt -= wcnt; 00822 *rem -= wcnt; 00823 if (*isempt) { 00824 /* 00825 * have not written to this block yet, so we keep 00826 * looking for zero's 00827 */ 00828 pt = st; 00829 end = st + wcnt; 00830 00831 /* 00832 * look for a zero filled buffer 00833 */ 00834 while ((pt < end) && (*pt == '\0')) 00835 ++pt; 00836 00837 if (pt == end) { 00838 /* 00839 * skip, buf is empty so far 00840 */ 00841 if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 00842 syswarn(1,errno,"File seek on %s", 00843 name); 00844 return(-1); 00845 } 00846 st = pt; 00847 continue; 00848 } 00849 /* 00850 * drat, the buf is not zero filled 00851 */ 00852 *isempt = 0; 00853 } 00854 00855 /* 00856 * have non-zero data in this file system block, have to write 00857 */ 00858 if (write(fd, st, wcnt) != wcnt) { 00859 syswarn(1, errno, "Failed write to file %s", name); 00860 return(-1); 00861 } 00862 st += wcnt; 00863 } 00864 return(st - str); 00865 } 00866 00867 /* 00868 * file_flush() 00869 * when the last file block in a file is zero, many file systems will not 00870 * let us create a hole at the end. To get the last block with zeros, we 00871 * write the last BYTE with a zero (back up one byte and write a zero). 00872 */ 00873 00874 void 00875 file_flush(int fd, char *fname, int isempt) 00876 { 00877 static char blnk[] = "\0"; 00878 00879 /* 00880 * silly test, but make sure we are only called when the last block is 00881 * filled with all zeros. 00882 */ 00883 if (!isempt) 00884 return; 00885 00886 /* 00887 * move back one byte and write a zero 00888 */ 00889 if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 00890 syswarn(1, errno, "Failed seek on file %s", fname); 00891 return; 00892 } 00893 00894 if (write(fd, blnk, 1) < 0) 00895 syswarn(1, errno, "Failed write to file %s", fname); 00896 return; 00897 } 00898 00899 /* 00900 * rdfile_close() 00901 * close a file we have beed reading (to copy or archive). If we have to 00902 * reset access time (tflag) do so (the times are stored in arcn). 00903 */ 00904 00905 void 00906 rdfile_close(ARCHD *arcn, int *fd) 00907 { 00908 /* 00909 * make sure the file is open 00910 */ 00911 if (*fd < 0) 00912 return; 00913 00914 (void)close(*fd); 00915 *fd = -1; 00916 if (!tflag) 00917 return; 00918 00919 /* 00920 * user wants last access time reset 00921 */ 00922 set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 00923 return; 00924 } 00925 00926 /* 00927 * set_crc() 00928 * read a file to calculate its crc. This is a real drag. Archive formats 00929 * that have this, end up reading the file twice (we have to write the 00930 * header WITH the crc before writing the file contents. Oh well... 00931 * Return: 00932 * 0 if was able to calculate the crc, -1 otherwise 00933 */ 00934 00935 int 00936 set_crc(ARCHD *arcn, int fd) 00937 { 00938 int i; 00939 int res; 00940 off_t cpcnt = 0L; 00941 u_long size; 00942 unsigned long crc = 0L; 00943 char tbuf[FILEBLK]; 00944 struct stat sb; 00945 00946 if (fd < 0) { 00947 /* 00948 * hmm, no fd, should never happen. well no crc then. 00949 */ 00950 arcn->crc = 0L; 00951 return(0); 00952 } 00953 00954 #if 0 00955 /* not in minix */ 00956 if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 00957 size = (u_long)sizeof(tbuf); 00958 #endif 00959 00960 /* 00961 * read all the bytes we think that there are in the file. If the user 00962 * is trying to archive an active file, forget this file. 00963 */ 00964 for(;;) { 00965 if ((res = read(fd, tbuf, size)) <= 0) 00966 break; 00967 cpcnt += res; 00968 for (i = 0; i < res; ++i) 00969 crc += (tbuf[i] & 0xff); 00970 } 00971 00972 /* 00973 * safety check. we want to avoid archiving files that are active as 00974 * they can create inconsistant archive copies. 00975 */ 00976 if (cpcnt != arcn->sb.st_size) 00977 paxwarn(1, "File changed size %s", arcn->org_name); 00978 else if (fstat(fd, &sb) < 0) 00979 syswarn(1, errno, "Failed stat on %s", arcn->org_name); 00980 else if (arcn->sb.st_mtime != sb.st_mtime) 00981 paxwarn(1, "File %s was modified during read", arcn->org_name); 00982 else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 00983 syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 00984 else { 00985 arcn->crc = crc; 00986 return(0); 00987 } 00988 return(-1); 00989 }
1.4.6