2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)file_subs.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/bin/pax/file_subs.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
39 * $DragonFly: src/bin/pax/file_subs.c,v 1.8 2006/09/27 21:58:08 pavalos Exp $
42 #include <sys/types.h>
57 mk_link (char *,struct stat
*,char *, int);
60 * routines that deal with file operations such as: creating, removing;
61 * and setting access modes, uid/gid and times of files
64 #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
65 #define SETBITS (S_ISUID | S_ISGID)
66 #define ABITS (FILEBITS | SETBITS)
70 * Create and open a file.
72 * file descriptor or -1 for failure
76 file_creat(ARCHD
*arcn
)
83 * Assume file doesn't exist, so just try to create it, most times this
84 * works. We have to take special handling when the file does exist. To
85 * detect this, we use O_EXCL. For example when trying to create a
86 * file and a character device or fifo exists with the same name, we
87 * can accidently open the device by mistake (or block waiting to open).
88 * If we find that the open has failed, then spend the effort to
89 * figure out why. This strategy was found to have better average
90 * performance in common use than checking the file (and the path)
93 file_mode
= arcn
->sb
.st_mode
& FILEBITS
;
94 if ((fd
= open(arcn
->name
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
,
99 * the file seems to exist. First we try to get rid of it (found to be
100 * the second most common failure when traced). If this fails, only
101 * then we go to the expense to check and create the path to the file
103 if (unlnk_exist(arcn
->name
, arcn
->type
) != 0)
108 * try to open it again, if this fails, check all the nodes in
109 * the path and give it a final try. if chk_path() finds that
110 * it cannot fix anything, we will skip the last attempt
112 if ((fd
= open(arcn
->name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
116 if (nodirs
|| chk_path(arcn
->name
,arcn
->sb
.st_uid
,arcn
->sb
.st_gid
) < 0) {
117 syswarn(1, oerrno
, "Unable to create %s", arcn
->name
);
126 * Close file descriptor to a file just created by pax. Sets modes,
127 * ownership and times as required.
129 * 0 for success, -1 for failure
133 file_close(ARCHD
*arcn
, int fd
)
140 syswarn(0, errno
, "Unable to close file descriptor on %s",
144 * set owner/groups first as this may strip off mode bits we want
145 * then set file permission modes. Then set file access and
146 * modification times.
149 res
= set_ids(arcn
->name
, arcn
->sb
.st_uid
, arcn
->sb
.st_gid
);
152 * IMPORTANT SECURITY NOTE:
153 * if not preserving mode or we cannot set uid/gid, then PROHIBIT
157 arcn
->sb
.st_mode
&= ~(SETBITS
);
159 set_pmode(arcn
->name
, arcn
->sb
.st_mode
);
160 if (patime
|| pmtime
)
161 set_ftime(arcn
->name
, arcn
->sb
.st_mtime
, arcn
->sb
.st_atime
, 0);
166 * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
169 * 0 if ok, -1 otherwise
173 lnk_creat(ARCHD
*arcn
)
178 * we may be running as root, so we have to be sure that link target
179 * is not a directory, so we lstat and check
181 if (lstat(arcn
->ln_name
, &sb
) < 0) {
182 syswarn(1,errno
,"Unable to link to %s from %s", arcn
->ln_name
,
187 if (S_ISDIR(sb
.st_mode
)) {
188 paxwarn(1, "A hard link to the directory %s is not allowed",
193 return(mk_link(arcn
->ln_name
, &sb
, arcn
->name
, 0));
198 * Create a hard link to arcn->org_name from arcn->name. Only used in copy
199 * with the -l flag. No warning or error if this does not succeed (we will
200 * then just create the file)
202 * 1 if copy() should try to create this file node
203 * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
207 cross_lnk(ARCHD
*arcn
)
210 * try to make a link to original file (-l flag in copy mode). make sure
211 * we do not try to link to directories in case we are running as root
212 * (and it might succeed).
214 if (arcn
->type
== PAX_DIR
)
216 return(mk_link(arcn
->org_name
, &(arcn
->sb
), arcn
->name
, 1));
221 * In copy mode if we are not trying to make hard links between the src
222 * and destinations, make sure we are not going to overwrite ourselves by
223 * accident. This slows things down a little, but we have to protect all
224 * those people who make typing errors.
226 * 1 the target does not exist, go ahead and copy
227 * 0 skip it file exists (-k) or may be the same as source file
231 chk_same(ARCHD
*arcn
)
236 * if file does not exist, return. if file exists and -k, skip it
239 if (lstat(arcn
->name
, &sb
) < 0)
245 * better make sure the user does not have src == dest by mistake
247 if ((arcn
->sb
.st_dev
== sb
.st_dev
) && (arcn
->sb
.st_ino
== sb
.st_ino
)) {
248 paxwarn(1, "Unable to copy %s, file would overwrite itself",
257 * try to make a hard link between two files. if ign set, we do not
260 * 0 if successful (or we are done with this file but no error, such as
261 * finding the from file exists and the user has set -k).
262 * 1 when ign was set to indicates we could not make the link but we
263 * should try to copy/extract the file as that might work (and is an
264 * allowed option). -1 an error occurred.
268 mk_link(char *to
, struct stat
*to_sb
, char *from
,
275 * if from file exists, it has to be unlinked to make the link. If the
276 * file exists and -k is set, skip it quietly
278 if (lstat(from
, &sb
) == 0) {
283 * make sure it is not the same file, protect the user
285 if ((to_sb
->st_dev
==sb
.st_dev
)&&(to_sb
->st_ino
== sb
.st_ino
)) {
286 paxwarn(1, "Unable to link file %s to itself", to
);
291 * try to get rid of the file, based on the type
293 if (S_ISDIR(sb
.st_mode
)) {
294 if (rmdir(from
) < 0) {
295 syswarn(1, errno
, "Unable to remove %s", from
);
298 } else if (unlink(from
) < 0) {
300 syswarn(1, errno
, "Unable to remove %s", from
);
308 * from file is gone (or did not exist), try to make the hard link.
309 * if it fails, check the path and try it again (if chk_path() says to
313 if (link(to
, from
) == 0)
316 if (!nodirs
&& chk_path(from
, to_sb
->st_uid
, to_sb
->st_gid
) == 0)
319 syswarn(1, oerrno
, "Could not link to %s from %s", to
,
327 * all right the link was made
334 * create an entry in the file system (other than a file or hard link).
335 * If successful, sets uid/gid modes and times as required.
337 * 0 if ok, -1 otherwise
341 node_creat(ARCHD
*arcn
)
351 * create node based on type, if that fails try to unlink the node and
352 * try again. finally check the path and try again. As noted in the
353 * file and link creation routines, this method seems to exhibit the
354 * best performance in general use workloads.
356 file_mode
= arcn
->sb
.st_mode
& FILEBITS
;
361 res
= mkdir(arcn
->name
, file_mode
);
366 file_mode
|= S_IFCHR
;
367 res
= mknod(arcn
->name
, file_mode
, arcn
->sb
.st_rdev
);
370 file_mode
|= S_IFBLK
;
371 res
= mknod(arcn
->name
, file_mode
, arcn
->sb
.st_rdev
);
374 res
= mkfifo(arcn
->name
, file_mode
);
378 * Skip sockets, operation has no meaning under BSD
381 "%s skipped. Sockets cannot be copied or extracted",
385 res
= symlink(arcn
->ln_name
, arcn
->name
);
393 * we should never get here
395 paxwarn(0, "%s has an unknown file type, skipping",
401 * if we were able to create the node break out of the loop,
402 * otherwise try to unlink the node and try again. if that
403 * fails check the full path and try a final time.
409 * we failed to make the node
412 if ((ign
= unlnk_exist(arcn
->name
, arcn
->type
)) < 0)
418 if (nodirs
|| chk_path(arcn
->name
,arcn
->sb
.st_uid
,arcn
->sb
.st_gid
) < 0) {
419 syswarn(1, oerrno
, "Could not create: %s", arcn
->name
);
425 * we were able to create the node. set uid/gid, modes and times
428 res
= ((arcn
->type
== PAX_SLK
) ?
429 set_lids(arcn
->name
, arcn
->sb
.st_uid
, arcn
->sb
.st_gid
) :
430 set_ids(arcn
->name
, arcn
->sb
.st_uid
, arcn
->sb
.st_gid
));
435 * symlinks are done now.
437 if (arcn
->type
== PAX_SLK
)
441 * IMPORTANT SECURITY NOTE:
442 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
446 arcn
->sb
.st_mode
&= ~(SETBITS
);
448 set_pmode(arcn
->name
, arcn
->sb
.st_mode
);
450 if (arcn
->type
== PAX_DIR
&& strcmp(NM_CPIO
, argv0
) != 0) {
452 * Dirs must be processed again at end of extract to set times
453 * and modes to agree with those stored in the archive. However
454 * to allow extract to continue, we may have to also set owner
455 * rights. This allows nodes in the archive that are children
456 * of this directory to be extracted without failure. Both time
457 * and modes will be fixed after the entire archive is read and
460 if (access(arcn
->name
, R_OK
| W_OK
| X_OK
) < 0) {
461 if (lstat(arcn
->name
, &sb
) < 0) {
462 syswarn(0, errno
,"Could not access %s (stat)",
464 set_pmode(arcn
->name
,file_mode
| S_IRWXU
);
467 * We have to add rights to the dir, so we make
468 * sure to restore the mode. The mode must be
469 * restored AS CREATED and not as stored if
472 set_pmode(arcn
->name
,
473 ((sb
.st_mode
& FILEBITS
) | S_IRWXU
));
475 arcn
->sb
.st_mode
= sb
.st_mode
;
479 * we have to force the mode to what was set here,
480 * since we changed it from the default as created.
482 add_dir(arcn
->name
, arcn
->nlen
, &(arcn
->sb
), 1);
483 } else if (pmode
|| patime
|| pmtime
)
484 add_dir(arcn
->name
, arcn
->nlen
, &(arcn
->sb
), 0);
487 if (patime
|| pmtime
)
488 set_ftime(arcn
->name
, arcn
->sb
.st_mtime
, arcn
->sb
.st_atime
, 0);
494 * Remove node from file system with the specified name. We pass the type
495 * of the node that is going to replace it. When we try to create a
496 * directory and find that it already exists, we allow processing to
497 * continue as proper modes etc will always be set for it later on.
499 * 0 is ok to proceed, no file with the specified name exists
500 * -1 we were unable to remove the node, or we should not remove it (-k)
501 * 1 we found a directory and we were going to create a directory.
505 unlnk_exist(char *name
, int type
)
510 * the file does not exist, or -k we are done
512 if (lstat(name
, &sb
) < 0)
517 if (S_ISDIR(sb
.st_mode
)) {
519 * try to remove a directory, if it fails and we were going to
520 * create a directory anyway, tell the caller (return a 1)
522 if (rmdir(name
) < 0) {
525 syswarn(1,errno
,"Unable to remove directory %s", name
);
532 * try to get rid of all non-directory type nodes
534 if (unlink(name
) < 0) {
535 syswarn(1, errno
, "Could not unlink %s", name
);
543 * We were trying to create some kind of node in the file system and it
544 * failed. chk_path() makes sure the path up to the node exists and is
545 * writeable. When we have to create a directory that is missing along the
546 * path somewhere, the directory we create will be set to the same
547 * uid/gid as the file has (when uid and gid are being preserved).
548 * NOTE: this routine is a real performance loss. It is only used as a
549 * last resort when trying to create entries in the file system.
551 * -1 when it could find nothing it is allowed to fix.
556 chk_path( char *name
, uid_t st_uid
, gid_t st_gid
)
563 * watch out for paths with nodes stored directly in / (e.g. /bozo)
570 * work forward from the first / and check each part of the path
572 spt
= strchr(spt
, '/');
578 * if it exists we assume it is a directory, it is not within
579 * the spec (at least it seems to read that way) to alter the
580 * file system for nodes NOT EXPLICITLY stored on the archive.
581 * If that assumption is changed, you would test the node here
582 * and figure out how to get rid of it (probably like some
583 * recursive unlink()) or fix up the directory permissions if
584 * required (do an access()).
586 if (lstat(name
, &sb
) == 0) {
592 * the path fails at this point, see if we can create the
593 * needed directory and continue on
595 if (mkdir(name
, S_IRWXU
| S_IRWXG
| S_IRWXO
) < 0) {
602 * we were able to create the directory. We will tell the
603 * caller that we found something to fix, and it is ok to try
604 * and create the node again.
608 set_ids(name
, st_uid
, st_gid
);
611 * make sure the user doesn't have some strange umask that
612 * causes this newly created directory to be unusable. We fix
613 * the modes and restore them back to the creation default at
616 if ((access(name
, R_OK
| W_OK
| X_OK
) < 0) &&
617 (lstat(name
, &sb
) == 0)) {
618 set_pmode(name
, ((sb
.st_mode
& FILEBITS
) | S_IRWXU
));
619 add_dir(name
, spt
- name
, &sb
, 1);
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
640 set_ftime(char *fnm
, time_t mtime
, time_t atime
, int frc
)
642 static struct timeval tv
[2] = {{0L, 0L}, {0L, 0L}};
645 tv
[0].tv_sec
= (long)atime
;
646 tv
[1].tv_sec
= (long)mtime
;
647 if (!frc
&& (!patime
|| !pmtime
)) {
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.
652 if (lstat(fnm
, &sb
) == 0) {
654 tv
[0].tv_sec
= (long)sb
.st_atime
;
656 tv
[1].tv_sec
= (long)sb
.st_mtime
;
658 syswarn(0,errno
,"Unable to obtain file stats %s", fnm
);
664 if (utimes(fnm
, tv
) < 0)
665 syswarn(1, errno
, "Access/modification time set failed on: %s",
672 * set the uid and gid of a file system node
674 * 0 when set, -1 on failure
678 set_ids(char *fnm
, uid_t uid
, gid_t gid
)
680 if (chown(fnm
, uid
, gid
) < 0) {
682 * ignore EPERM unless in verbose mode or being run by root.
683 * if running as pax, POSIX requires a warning.
685 if (strcmp(NM_PAX
, argv0
) == 0 || errno
!= EPERM
|| vflag
||
687 syswarn(1, errno
, "Unable to set file uid/gid of %s",
696 * set the uid and gid of a file system node
698 * 0 when set, -1 on failure
702 set_lids(char *fnm
, uid_t uid
, gid_t gid
)
704 if (lchown(fnm
, uid
, gid
) < 0) {
706 * ignore EPERM unless in verbose mode or being run by root.
707 * if running as pax, POSIX requires a warning.
709 if (strcmp(NM_PAX
, argv0
) == 0 || errno
!= EPERM
|| vflag
||
711 syswarn(1, errno
, "Unable to set file uid/gid of %s",
720 * Set file access mode
724 set_pmode(char *fnm
, mode_t mode
)
727 if (chmod(fnm
, mode
) < 0)
728 syswarn(1, errno
, "Could not set permissions on %s", fnm
);
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 boundaries. 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 boundaries 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.
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
777 * number of bytes written, -1 on write (or lseek) error.
781 file_write(int fd
, char *str
, int cnt
, int *rem
, int *isempt
, int sz
,
790 * while we have data to process
795 * We are now at the start of file system block again
796 * (or what we think one is...). start looking for
804 * only examine up to the end of the current file block or
805 * remaining characters to write, whatever is smaller
807 wcnt
= MIN(cnt
, *rem
);
812 * have not written to this block yet, so we keep
819 * look for a zero filled buffer
821 while ((pt
< end
) && (*pt
== '\0'))
826 * skip, buf is empty so far
828 if (lseek(fd
, (off_t
)wcnt
, SEEK_CUR
) < 0) {
829 syswarn(1,errno
,"File seek on %s",
837 * drat, the buf is not zero filled
843 * have non-zero data in this file system block, have to write
845 if (write(fd
, st
, wcnt
) != wcnt
) {
846 syswarn(1, errno
, "Failed write to file %s", name
);
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).
862 file_flush(int fd
, char *fname
, int isempt
)
864 static char blnk
[] = "\0";
867 * silly test, but make sure we are only called when the last block is
868 * filled with all zeros.
874 * move back one byte and write a zero
876 if (lseek(fd
, (off_t
)-1, SEEK_CUR
) < 0) {
877 syswarn(1, errno
, "Failed seek on file %s", fname
);
881 if (write(fd
, blnk
, 1) < 0)
882 syswarn(1, errno
, "Failed write to file %s", fname
);
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).
893 rdfile_close(ARCHD
*arcn
, int *fd
)
896 * make sure the file is open
907 * user wants last access time reset
909 set_ftime(arcn
->org_name
, arcn
->sb
.st_mtime
, arcn
->sb
.st_atime
, 1);
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...
919 * 0 if was able to calculate the crc, -1 otherwise
923 set_crc(ARCHD
*arcn
, int fd
)
929 unsigned long crc
= 0L;
935 * hmm, no fd, should never happen. well no crc then.
941 if ((size
= (u_long
)arcn
->sb
.st_blksize
) > (u_long
)sizeof(tbuf
))
942 size
= (u_long
)sizeof(tbuf
);
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.
949 if ((res
= read(fd
, tbuf
, size
)) <= 0)
952 for (i
= 0; i
< res
; ++i
)
953 crc
+= (tbuf
[i
] & 0xff);
957 * safety check. we want to avoid archiving files that are active as
958 * they can create inconsistent archive copies.
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
);