1*c7d97141Sjca /* $OpenBSD: file_subs.c,v 1.57 2024/07/14 14:32:02 jca Exp $ */
2df930be7Sderaadt /* $NetBSD: file_subs.c,v 1.4 1995/03/21 09:07:18 cgd Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1992 Keith Muller.
6df930be7Sderaadt * Copyright (c) 1992, 1993
7df930be7Sderaadt * The Regents of the University of California. All rights reserved.
8df930be7Sderaadt *
9df930be7Sderaadt * This code is derived from software contributed to Berkeley by
10df930be7Sderaadt * Keith Muller of the University of California, San Diego.
11df930be7Sderaadt *
12df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
13df930be7Sderaadt * modification, are permitted provided that the following conditions
14df930be7Sderaadt * are met:
15df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
16df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
17df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
18df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
19df930be7Sderaadt * documentation and/or other materials provided with the distribution.
2029295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt * may be used to endorse or promote products derived from this software
22df930be7Sderaadt * without specific prior written permission.
23df930be7Sderaadt *
24df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt * SUCH DAMAGE.
35df930be7Sderaadt */
36df930be7Sderaadt
37df930be7Sderaadt #include <sys/stat.h>
38da60b202Smillert #include <err.h>
39da60b202Smillert #include <errno.h>
40da60b202Smillert #include <fcntl.h>
4108961bb1Sguenther #include <limits.h>
42da60b202Smillert #include <stdio.h>
43df930be7Sderaadt #include <stdlib.h>
44da60b202Smillert #include <string.h>
45da60b202Smillert #include <unistd.h>
46df930be7Sderaadt #include "pax.h"
47df930be7Sderaadt #include "extern.h"
48df930be7Sderaadt
49c01bd743Sespie static int fset_ids(char *, int, uid_t, gid_t);
50c01bd743Sespie static int unlnk_exist(char *, int);
51c01bd743Sespie static int chk_path(char *, uid_t, gid_t, int);
52c01bd743Sespie static int mk_link(char *, struct stat *, char *, int);
53c01bd743Sespie static void fset_ftime(const char *, int, const struct timespec *,
54c01bd743Sespie const struct timespec *, int);
55c01bd743Sespie static void fset_pmode(char *, int, mode_t);
56df930be7Sderaadt
57df930be7Sderaadt /*
58df930be7Sderaadt * routines that deal with file operations such as: creating, removing;
59df930be7Sderaadt * and setting access modes, uid/gid and times of files
60df930be7Sderaadt */
61df930be7Sderaadt
62df930be7Sderaadt /*
63df930be7Sderaadt * file_creat()
64df930be7Sderaadt * Create and open a file.
65df930be7Sderaadt * Return:
66df930be7Sderaadt * file descriptor or -1 for failure
67df930be7Sderaadt */
68df930be7Sderaadt
69df930be7Sderaadt int
file_creat(ARCHD * arcn)70be87792eSmillert file_creat(ARCHD *arcn)
71df930be7Sderaadt {
72df930be7Sderaadt int fd = -1;
73df930be7Sderaadt mode_t file_mode;
74df930be7Sderaadt int oerrno;
75df930be7Sderaadt
76df930be7Sderaadt /*
774eb0b000Smillert * Assume file doesn't exist, so just try to create it, most times this
78df930be7Sderaadt * works. We have to take special handling when the file does exist. To
79df930be7Sderaadt * detect this, we use O_EXCL. For example when trying to create a
80df930be7Sderaadt * file and a character device or fifo exists with the same name, we
814eb0b000Smillert * can accidently open the device by mistake (or block waiting to open).
82f0f82a05Sjmc * If we find that the open has failed, then spend the effort to
83df930be7Sderaadt * figure out why. This strategy was found to have better average
84df930be7Sderaadt * performance in common use than checking the file (and the path)
85df930be7Sderaadt * first with lstat.
86df930be7Sderaadt */
87df930be7Sderaadt file_mode = arcn->sb.st_mode & FILEBITS;
88217c04afSguenther if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_EXCL,
89df930be7Sderaadt file_mode)) >= 0)
90df930be7Sderaadt return(fd);
91df930be7Sderaadt
92df930be7Sderaadt /*
93df930be7Sderaadt * the file seems to exist. First we try to get rid of it (found to be
94df930be7Sderaadt * the second most common failure when traced). If this fails, only
95df930be7Sderaadt * then we go to the expense to check and create the path to the file
96df930be7Sderaadt */
97df930be7Sderaadt if (unlnk_exist(arcn->name, arcn->type) != 0)
98df930be7Sderaadt return(-1);
99df930be7Sderaadt
100df930be7Sderaadt for (;;) {
101df930be7Sderaadt /*
102df930be7Sderaadt * try to open it again, if this fails, check all the nodes in
103df930be7Sderaadt * the path and give it a final try. if chk_path() finds that
104df930be7Sderaadt * it cannot fix anything, we will skip the last attempt
105df930be7Sderaadt */
106df930be7Sderaadt if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
107df930be7Sderaadt file_mode)) >= 0)
108df930be7Sderaadt break;
109df930be7Sderaadt oerrno = errno;
1108cc78d45Sespie if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid, 0) < 0) {
111df930be7Sderaadt syswarn(1, oerrno, "Unable to create %s", arcn->name);
112df930be7Sderaadt return(-1);
113df930be7Sderaadt }
114df930be7Sderaadt }
115df930be7Sderaadt return(fd);
116df930be7Sderaadt }
117df930be7Sderaadt
118df930be7Sderaadt /*
119df930be7Sderaadt * file_close()
120df930be7Sderaadt * Close file descriptor to a file just created by pax. Sets modes,
121df930be7Sderaadt * ownership and times as required.
122df930be7Sderaadt * Return:
123df930be7Sderaadt * 0 for success, -1 for failure
124df930be7Sderaadt */
125df930be7Sderaadt
126df930be7Sderaadt void
file_close(ARCHD * arcn,int fd)127be87792eSmillert file_close(ARCHD *arcn, int fd)
128df930be7Sderaadt {
129df930be7Sderaadt int res = 0;
130df930be7Sderaadt
131df930be7Sderaadt if (fd < 0)
132df930be7Sderaadt return;
133df930be7Sderaadt
134df930be7Sderaadt /*
135df930be7Sderaadt * set owner/groups first as this may strip off mode bits we want
136df930be7Sderaadt * then set file permission modes. Then set file access and
137df930be7Sderaadt * modification times.
138df930be7Sderaadt */
139df930be7Sderaadt if (pids)
140f95f8796Sotto res = fset_ids(arcn->name, fd, arcn->sb.st_uid,
141f95f8796Sotto arcn->sb.st_gid);
142df930be7Sderaadt
143df930be7Sderaadt /*
144df930be7Sderaadt * IMPORTANT SECURITY NOTE:
145df930be7Sderaadt * if not preserving mode or we cannot set uid/gid, then PROHIBIT
146df930be7Sderaadt * set uid/gid bits
147df930be7Sderaadt */
148df930be7Sderaadt if (!pmode || res)
149df930be7Sderaadt arcn->sb.st_mode &= ~(SETBITS);
150df930be7Sderaadt if (pmode)
151f95f8796Sotto fset_pmode(arcn->name, fd, arcn->sb.st_mode);
152df930be7Sderaadt if (patime || pmtime)
1538b72bc25Sguenther fset_ftime(arcn->name, fd, &arcn->sb.st_mtim,
1548b72bc25Sguenther &arcn->sb.st_atim, 0);
1553aaa63ebSderaadt if (close(fd) == -1)
156f95f8796Sotto syswarn(0, errno, "Unable to close file descriptor on %s",
157f95f8796Sotto arcn->name);
158df930be7Sderaadt }
159df930be7Sderaadt
160df930be7Sderaadt /*
161df930be7Sderaadt * lnk_creat()
162df930be7Sderaadt * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
163df930be7Sderaadt * must exist;
164df930be7Sderaadt * Return:
165df930be7Sderaadt * 0 if ok, -1 otherwise
166df930be7Sderaadt */
167df930be7Sderaadt
168df930be7Sderaadt int
lnk_creat(ARCHD * arcn)169be87792eSmillert lnk_creat(ARCHD *arcn)
170df930be7Sderaadt {
171df930be7Sderaadt struct stat sb;
1722dbd6dc5Sguenther int res;
173df930be7Sderaadt
174df930be7Sderaadt /*
175df930be7Sderaadt * we may be running as root, so we have to be sure that link target
176df930be7Sderaadt * is not a directory, so we lstat and check
177df930be7Sderaadt */
1783aaa63ebSderaadt if (lstat(arcn->ln_name, &sb) == -1) {
179df930be7Sderaadt syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
180df930be7Sderaadt arcn->name);
181df930be7Sderaadt return(-1);
182df930be7Sderaadt }
183df930be7Sderaadt
184df930be7Sderaadt if (S_ISDIR(sb.st_mode)) {
18542cf9836Stholo paxwarn(1, "A hard link to the directory %s is not allowed",
186df930be7Sderaadt arcn->ln_name);
187df930be7Sderaadt return(-1);
188df930be7Sderaadt }
189df930be7Sderaadt
1902dbd6dc5Sguenther res = mk_link(arcn->ln_name, &sb, arcn->name, 0);
1912dbd6dc5Sguenther if (res == 0) {
1922dbd6dc5Sguenther /* check for a hardlink to a placeholder symlink */
1932dbd6dc5Sguenther res = sltab_add_link(arcn->name, &sb);
1942dbd6dc5Sguenther
1952dbd6dc5Sguenther if (res < 0) {
1962dbd6dc5Sguenther /* arrgh, it failed, clean up */
1972dbd6dc5Sguenther unlink(arcn->name);
1982dbd6dc5Sguenther }
1992dbd6dc5Sguenther }
2002dbd6dc5Sguenther
2012dbd6dc5Sguenther return (res);
202df930be7Sderaadt }
203df930be7Sderaadt
204df930be7Sderaadt /*
205df930be7Sderaadt * cross_lnk()
206df930be7Sderaadt * Create a hard link to arcn->org_name from arcn->name. Only used in copy
207df930be7Sderaadt * with the -l flag. No warning or error if this does not succeed (we will
208df930be7Sderaadt * then just create the file)
209df930be7Sderaadt * Return:
210df930be7Sderaadt * 1 if copy() should try to create this file node
211df930be7Sderaadt * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
212df930be7Sderaadt */
213df930be7Sderaadt
214df930be7Sderaadt int
cross_lnk(ARCHD * arcn)215be87792eSmillert cross_lnk(ARCHD *arcn)
216df930be7Sderaadt {
217df930be7Sderaadt /*
21808cab283Sjmc * try to make a link to original file (-l flag in copy mode). make
2194eb0b000Smillert * sure we do not try to link to directories in case we are running as
2204eb0b000Smillert * root (and it might succeed).
221df930be7Sderaadt */
222df930be7Sderaadt if (arcn->type == PAX_DIR)
223df930be7Sderaadt return(1);
224df930be7Sderaadt return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
225df930be7Sderaadt }
226df930be7Sderaadt
227df930be7Sderaadt /*
228df930be7Sderaadt * chk_same()
229df930be7Sderaadt * In copy mode if we are not trying to make hard links between the src
230df930be7Sderaadt * and destinations, make sure we are not going to overwrite ourselves by
231df930be7Sderaadt * accident. This slows things down a little, but we have to protect all
232df930be7Sderaadt * those people who make typing errors.
233df930be7Sderaadt * Return:
234df930be7Sderaadt * 1 the target does not exist, go ahead and copy
235df930be7Sderaadt * 0 skip it file exists (-k) or may be the same as source file
236df930be7Sderaadt */
237df930be7Sderaadt
238df930be7Sderaadt int
chk_same(ARCHD * arcn)239be87792eSmillert chk_same(ARCHD *arcn)
240df930be7Sderaadt {
241df930be7Sderaadt struct stat sb;
242df930be7Sderaadt
243df930be7Sderaadt /*
244df930be7Sderaadt * if file does not exist, return. if file exists and -k, skip it
245df930be7Sderaadt * quietly
246df930be7Sderaadt */
2473aaa63ebSderaadt if (lstat(arcn->name, &sb) == -1)
248df930be7Sderaadt return(1);
249df930be7Sderaadt if (kflag)
250df930be7Sderaadt return(0);
251df930be7Sderaadt
252df930be7Sderaadt /*
253df930be7Sderaadt * better make sure the user does not have src == dest by mistake
254df930be7Sderaadt */
255df930be7Sderaadt if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
25642cf9836Stholo paxwarn(1, "Unable to copy %s, file would overwrite itself",
257df930be7Sderaadt arcn->name);
258df930be7Sderaadt return(0);
259df930be7Sderaadt }
260df930be7Sderaadt return(1);
261df930be7Sderaadt }
262df930be7Sderaadt
263df930be7Sderaadt /*
264df930be7Sderaadt * mk_link()
265df930be7Sderaadt * try to make a hard link between two files. if ign set, we do not
266df930be7Sderaadt * complain.
267df930be7Sderaadt * Return:
268df930be7Sderaadt * 0 if successful (or we are done with this file but no error, such as
269df930be7Sderaadt * finding the from file exists and the user has set -k).
270df930be7Sderaadt * 1 when ign was set to indicates we could not make the link but we
271df930be7Sderaadt * should try to copy/extract the file as that might work (and is an
272df930be7Sderaadt * allowed option). -1 an error occurred.
273df930be7Sderaadt */
274df930be7Sderaadt
275df930be7Sderaadt static int
mk_link(char * to,struct stat * to_sb,char * from,int ign)276be87792eSmillert mk_link(char *to, struct stat *to_sb, char *from, int ign)
277df930be7Sderaadt {
278df930be7Sderaadt struct stat sb;
279df930be7Sderaadt int oerrno;
280df930be7Sderaadt
281df930be7Sderaadt /*
282df930be7Sderaadt * if from file exists, it has to be unlinked to make the link. If the
283df930be7Sderaadt * file exists and -k is set, skip it quietly
284df930be7Sderaadt */
285df930be7Sderaadt if (lstat(from, &sb) == 0) {
286df930be7Sderaadt if (kflag)
287df930be7Sderaadt return(0);
288df930be7Sderaadt
289df930be7Sderaadt /*
290df930be7Sderaadt * make sure it is not the same file, protect the user
291df930be7Sderaadt */
292df930be7Sderaadt if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
29342cf9836Stholo paxwarn(1, "Unable to link file %s to itself", to);
2947b425235Smillert return(-1);
295df930be7Sderaadt }
296df930be7Sderaadt
297df930be7Sderaadt /*
298df930be7Sderaadt * try to get rid of the file, based on the type
299df930be7Sderaadt */
300df930be7Sderaadt if (S_ISDIR(sb.st_mode)) {
3013aaa63ebSderaadt if (rmdir(from) == -1) {
302df930be7Sderaadt syswarn(1, errno, "Unable to remove %s", from);
303df930be7Sderaadt return(-1);
304df930be7Sderaadt }
3052dbd6dc5Sguenther delete_dir(sb.st_dev, sb.st_ino);
3063aaa63ebSderaadt } else if (unlink(from) == -1) {
307df930be7Sderaadt if (!ign) {
308df930be7Sderaadt syswarn(1, errno, "Unable to remove %s", from);
309df930be7Sderaadt return(-1);
310df930be7Sderaadt }
311df930be7Sderaadt return(1);
312df930be7Sderaadt }
313df930be7Sderaadt }
314df930be7Sderaadt
315df930be7Sderaadt /*
316df930be7Sderaadt * from file is gone (or did not exist), try to make the hard link.
317df930be7Sderaadt * if it fails, check the path and try it again (if chk_path() says to
318df930be7Sderaadt * try again)
319df930be7Sderaadt */
320df930be7Sderaadt for (;;) {
3210cc9193bSguenther if (linkat(AT_FDCWD, to, AT_FDCWD, from, 0) == 0)
322df930be7Sderaadt break;
323df930be7Sderaadt oerrno = errno;
3248cc78d45Sespie if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid, ign) == 0)
325df930be7Sderaadt continue;
326df930be7Sderaadt if (!ign) {
327df930be7Sderaadt syswarn(1, oerrno, "Could not link to %s from %s", to,
328df930be7Sderaadt from);
329df930be7Sderaadt return(-1);
330df930be7Sderaadt }
331df930be7Sderaadt return(1);
332df930be7Sderaadt }
333df930be7Sderaadt
334df930be7Sderaadt /*
335df930be7Sderaadt * all right the link was made
336df930be7Sderaadt */
337df930be7Sderaadt return(0);
338df930be7Sderaadt }
339df930be7Sderaadt
340df930be7Sderaadt /*
341df930be7Sderaadt * node_creat()
342df930be7Sderaadt * create an entry in the file system (other than a file or hard link).
343df930be7Sderaadt * If successful, sets uid/gid modes and times as required.
344df930be7Sderaadt * Return:
345df930be7Sderaadt * 0 if ok, -1 otherwise
346df930be7Sderaadt */
347df930be7Sderaadt
348df930be7Sderaadt int
node_creat(ARCHD * arcn)349be87792eSmillert node_creat(ARCHD *arcn)
350df930be7Sderaadt {
351be87792eSmillert int res;
352be87792eSmillert int ign = 0;
353be87792eSmillert int oerrno;
354be87792eSmillert int pass = 0;
355df930be7Sderaadt mode_t file_mode;
356df930be7Sderaadt struct stat sb;
35708961bb1Sguenther char target[PATH_MAX];
35833b137a4Sniklas char *nm = arcn->name;
3592dbd6dc5Sguenther int len, defer_pmode = 0;
360df930be7Sderaadt
361df930be7Sderaadt /*
362df930be7Sderaadt * create node based on type, if that fails try to unlink the node and
363df930be7Sderaadt * try again. finally check the path and try again. As noted in the
364df930be7Sderaadt * file and link creation routines, this method seems to exhibit the
365df930be7Sderaadt * best performance in general use workloads.
366df930be7Sderaadt */
367df930be7Sderaadt file_mode = arcn->sb.st_mode & FILEBITS;
368df930be7Sderaadt
369df930be7Sderaadt for (;;) {
370df930be7Sderaadt switch (arcn->type) {
371df930be7Sderaadt case PAX_DIR:
37233b137a4Sniklas /*
37333b137a4Sniklas * If -h (or -L) was given in tar-mode, follow the
37433b137a4Sniklas * potential symlink chain before trying to create the
37533b137a4Sniklas * directory.
37633b137a4Sniklas */
3773228b364Sguenther if (op_mode == OP_TAR && Lflag) {
37833b137a4Sniklas while (lstat(nm, &sb) == 0 &&
37933b137a4Sniklas S_ISLNK(sb.st_mode)) {
38033b137a4Sniklas len = readlink(nm, target,
38133b137a4Sniklas sizeof target - 1);
38233b137a4Sniklas if (len == -1) {
38333b137a4Sniklas syswarn(0, errno,
38433b137a4Sniklas "cannot follow symlink %s in chain for %s",
38533b137a4Sniklas nm, arcn->name);
38633b137a4Sniklas res = -1;
38733b137a4Sniklas goto badlink;
38833b137a4Sniklas }
38933b137a4Sniklas target[len] = '\0';
39033b137a4Sniklas nm = target;
39133b137a4Sniklas }
39233b137a4Sniklas }
39333b137a4Sniklas res = mkdir(nm, file_mode);
39433b137a4Sniklas
39533b137a4Sniklas badlink:
396df930be7Sderaadt if (ign)
397df930be7Sderaadt res = 0;
398df930be7Sderaadt break;
399df930be7Sderaadt case PAX_CHR:
400df930be7Sderaadt file_mode |= S_IFCHR;
40133b137a4Sniklas res = mknod(nm, file_mode, arcn->sb.st_rdev);
402df930be7Sderaadt break;
403df930be7Sderaadt case PAX_BLK:
404df930be7Sderaadt file_mode |= S_IFBLK;
40533b137a4Sniklas res = mknod(nm, file_mode, arcn->sb.st_rdev);
406df930be7Sderaadt break;
407df930be7Sderaadt case PAX_FIF:
40833b137a4Sniklas res = mkfifo(nm, file_mode);
409df930be7Sderaadt break;
410df930be7Sderaadt case PAX_SCK:
411df930be7Sderaadt /*
412df930be7Sderaadt * Skip sockets, operation has no meaning under BSD
413df930be7Sderaadt */
41442cf9836Stholo paxwarn(0,
415df930be7Sderaadt "%s skipped. Sockets cannot be copied or extracted",
41633b137a4Sniklas nm);
417df930be7Sderaadt return(-1);
418df930be7Sderaadt case PAX_SLK:
4192dbd6dc5Sguenther if (arcn->ln_name[0] != '/' &&
4202dbd6dc5Sguenther !has_dotdot(arcn->ln_name))
42133b137a4Sniklas res = symlink(arcn->ln_name, nm);
4222dbd6dc5Sguenther else {
4232dbd6dc5Sguenther /*
4242dbd6dc5Sguenther * absolute symlinks and symlinks with ".."
4252dbd6dc5Sguenther * have to be deferred to prevent the archive
4262dbd6dc5Sguenther * from bootstrapping itself to outside the
4272dbd6dc5Sguenther * working directory.
4282dbd6dc5Sguenther */
4292dbd6dc5Sguenther res = sltab_add_sym(nm, arcn->ln_name,
4302dbd6dc5Sguenther arcn->sb.st_mode);
4312dbd6dc5Sguenther if (res == 0)
4322dbd6dc5Sguenther defer_pmode = 1;
4332dbd6dc5Sguenther }
434df930be7Sderaadt break;
435df930be7Sderaadt case PAX_CTG:
436df930be7Sderaadt case PAX_HLK:
437df930be7Sderaadt case PAX_HRG:
438df930be7Sderaadt case PAX_REG:
439df930be7Sderaadt default:
440df930be7Sderaadt /*
441df930be7Sderaadt * we should never get here
442df930be7Sderaadt */
44342cf9836Stholo paxwarn(0, "%s has an unknown file type, skipping",
44433b137a4Sniklas nm);
445df930be7Sderaadt return(-1);
446df930be7Sderaadt }
447df930be7Sderaadt
448df930be7Sderaadt /*
449df930be7Sderaadt * if we were able to create the node break out of the loop,
450df930be7Sderaadt * otherwise try to unlink the node and try again. if that
451df930be7Sderaadt * fails check the full path and try a final time.
452df930be7Sderaadt */
453df930be7Sderaadt if (res == 0)
454df930be7Sderaadt break;
455df930be7Sderaadt
456df930be7Sderaadt /*
457df930be7Sderaadt * we failed to make the node
458df930be7Sderaadt */
459df930be7Sderaadt oerrno = errno;
46033b137a4Sniklas if ((ign = unlnk_exist(nm, arcn->type)) < 0)
461df930be7Sderaadt return(-1);
462df930be7Sderaadt
463df930be7Sderaadt if (++pass <= 1)
464df930be7Sderaadt continue;
465df930be7Sderaadt
4668cc78d45Sespie if (nodirs || chk_path(nm,arcn->sb.st_uid,arcn->sb.st_gid, 0) < 0) {
46733b137a4Sniklas syswarn(1, oerrno, "Could not create: %s", nm);
468df930be7Sderaadt return(-1);
469df930be7Sderaadt }
470df930be7Sderaadt }
471df930be7Sderaadt
472df930be7Sderaadt /*
473df930be7Sderaadt * we were able to create the node. set uid/gid, modes and times
474df930be7Sderaadt */
475df930be7Sderaadt if (pids)
476007bef9eSguenther res = set_ids(nm, arcn->sb.st_uid, arcn->sb.st_gid);
477df930be7Sderaadt else
478df930be7Sderaadt res = 0;
479df930be7Sderaadt
480df930be7Sderaadt /*
481df930be7Sderaadt * IMPORTANT SECURITY NOTE:
482df930be7Sderaadt * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
483df930be7Sderaadt * set uid/gid bits
484df930be7Sderaadt */
485df930be7Sderaadt if (!pmode || res)
486df930be7Sderaadt arcn->sb.st_mode &= ~(SETBITS);
4872dbd6dc5Sguenther if (pmode && !defer_pmode)
48833b137a4Sniklas set_pmode(nm, arcn->sb.st_mode);
489df930be7Sderaadt
4903228b364Sguenther if (arcn->type == PAX_DIR && op_mode != OP_CPIO) {
491df930be7Sderaadt /*
492df930be7Sderaadt * Dirs must be processed again at end of extract to set times
493df930be7Sderaadt * and modes to agree with those stored in the archive. However
494df930be7Sderaadt * to allow extract to continue, we may have to also set owner
495df930be7Sderaadt * rights. This allows nodes in the archive that are children
496df930be7Sderaadt * of this directory to be extracted without failure. Both time
497df930be7Sderaadt * and modes will be fixed after the entire archive is read and
4982dbd6dc5Sguenther * before pax exits. To do that safely, we want the dev+ino
4992dbd6dc5Sguenther * of the directory we created.
500df930be7Sderaadt */
5013aaa63ebSderaadt if (lstat(nm, &sb) == -1) {
5022dbd6dc5Sguenther syswarn(0, errno,"Could not access %s (stat)", nm);
5033aaa63ebSderaadt } else if (access(nm, R_OK | W_OK | X_OK) == -1) {
504df930be7Sderaadt /*
505df930be7Sderaadt * We have to add rights to the dir, so we make
506df930be7Sderaadt * sure to restore the mode. The mode must be
507df930be7Sderaadt * restored AS CREATED and not as stored if
508df930be7Sderaadt * pmode is not set.
509df930be7Sderaadt */
51033b137a4Sniklas set_pmode(nm,
511df930be7Sderaadt ((sb.st_mode & FILEBITS) | S_IRWXU));
512df930be7Sderaadt if (!pmode)
513df930be7Sderaadt arcn->sb.st_mode = sb.st_mode;
514df930be7Sderaadt
515df930be7Sderaadt /*
5162dbd6dc5Sguenther * we have to force the mode to what was set
5172dbd6dc5Sguenther * here, since we changed it from the default
5182dbd6dc5Sguenther * as created.
519df930be7Sderaadt */
5202dbd6dc5Sguenther arcn->sb.st_dev = sb.st_dev;
5212dbd6dc5Sguenther arcn->sb.st_ino = sb.st_ino;
5229116436cSotto add_dir(nm, &(arcn->sb), 1);
5232dbd6dc5Sguenther } else if (pmode || patime || pmtime) {
5242dbd6dc5Sguenther arcn->sb.st_dev = sb.st_dev;
5252dbd6dc5Sguenther arcn->sb.st_ino = sb.st_ino;
5269116436cSotto add_dir(nm, &(arcn->sb), 0);
527df930be7Sderaadt }
528d4d3b6b2Sguenther } else if (patime || pmtime)
5298b72bc25Sguenther set_ftime(nm, &arcn->sb.st_mtim, &arcn->sb.st_atim, 0);
530df930be7Sderaadt return(0);
531df930be7Sderaadt }
532df930be7Sderaadt
533df930be7Sderaadt /*
534df930be7Sderaadt * unlnk_exist()
535df930be7Sderaadt * Remove node from file system with the specified name. We pass the type
536df930be7Sderaadt * of the node that is going to replace it. When we try to create a
537df930be7Sderaadt * directory and find that it already exists, we allow processing to
538df930be7Sderaadt * continue as proper modes etc will always be set for it later on.
539df930be7Sderaadt * Return:
540df930be7Sderaadt * 0 is ok to proceed, no file with the specified name exists
541df930be7Sderaadt * -1 we were unable to remove the node, or we should not remove it (-k)
542df930be7Sderaadt * 1 we found a directory and we were going to create a directory.
543df930be7Sderaadt */
544df930be7Sderaadt
545c01bd743Sespie static int
unlnk_exist(char * name,int type)546be87792eSmillert unlnk_exist(char *name, int type)
547df930be7Sderaadt {
548df930be7Sderaadt struct stat sb;
549df930be7Sderaadt
550df930be7Sderaadt /*
551df930be7Sderaadt * the file does not exist, or -k we are done
552df930be7Sderaadt */
5533aaa63ebSderaadt if (lstat(name, &sb) == -1)
554df930be7Sderaadt return(0);
555df930be7Sderaadt if (kflag)
556df930be7Sderaadt return(-1);
557df930be7Sderaadt
558df930be7Sderaadt if (S_ISDIR(sb.st_mode)) {
559df930be7Sderaadt /*
560df930be7Sderaadt * try to remove a directory, if it fails and we were going to
561df930be7Sderaadt * create a directory anyway, tell the caller (return a 1)
562df930be7Sderaadt */
5633aaa63ebSderaadt if (rmdir(name) == -1) {
564df930be7Sderaadt if (type == PAX_DIR)
565df930be7Sderaadt return(1);
566df930be7Sderaadt syswarn(1,errno,"Unable to remove directory %s", name);
567df930be7Sderaadt return(-1);
568df930be7Sderaadt }
5692dbd6dc5Sguenther delete_dir(sb.st_dev, sb.st_ino);
570df930be7Sderaadt return(0);
571df930be7Sderaadt }
572df930be7Sderaadt
573df930be7Sderaadt /*
574df930be7Sderaadt * try to get rid of all non-directory type nodes
575df930be7Sderaadt */
5763aaa63ebSderaadt if (unlink(name) == -1) {
577df930be7Sderaadt syswarn(1, errno, "Could not unlink %s", name);
578df930be7Sderaadt return(-1);
579df930be7Sderaadt }
580df930be7Sderaadt return(0);
581df930be7Sderaadt }
582df930be7Sderaadt
583df930be7Sderaadt /*
584df930be7Sderaadt * chk_path()
585df930be7Sderaadt * We were trying to create some kind of node in the file system and it
586df930be7Sderaadt * failed. chk_path() makes sure the path up to the node exists and is
587df930be7Sderaadt * writeable. When we have to create a directory that is missing along the
588df930be7Sderaadt * path somewhere, the directory we create will be set to the same
589df930be7Sderaadt * uid/gid as the file has (when uid and gid are being preserved).
590df930be7Sderaadt * NOTE: this routine is a real performance loss. It is only used as a
591df930be7Sderaadt * last resort when trying to create entries in the file system.
592df930be7Sderaadt * Return:
593df930be7Sderaadt * -1 when it could find nothing it is allowed to fix.
594df930be7Sderaadt * 0 otherwise
595df930be7Sderaadt */
596df930be7Sderaadt
597df930be7Sderaadt int
chk_path(char * name,uid_t st_uid,gid_t st_gid,int ign)5988cc78d45Sespie chk_path(char *name, uid_t st_uid, gid_t st_gid, int ign)
599df930be7Sderaadt {
600be87792eSmillert char *spt = name;
601be528732Sguenther char *next;
602df930be7Sderaadt struct stat sb;
603df930be7Sderaadt int retval = -1;
604df930be7Sderaadt
605df930be7Sderaadt /*
606df930be7Sderaadt * watch out for paths with nodes stored directly in / (e.g. /bozo)
607df930be7Sderaadt */
608be528732Sguenther while (*spt == '/')
609df930be7Sderaadt ++spt;
610df930be7Sderaadt
611df930be7Sderaadt for (;;) {
612df930be7Sderaadt /*
6134eb0b000Smillert * work forward from the first / and check each part of the path
614df930be7Sderaadt */
615df930be7Sderaadt spt = strchr(spt, '/');
616df930be7Sderaadt if (spt == NULL)
617df930be7Sderaadt break;
618be528732Sguenther
619be528732Sguenther /*
620be528732Sguenther * skip over duplicate slashes; stop if there're only
621be528732Sguenther * trailing slashes left
622be528732Sguenther */
623be528732Sguenther next = spt + 1;
624be528732Sguenther while (*next == '/')
625be528732Sguenther next++;
626be528732Sguenther if (*next == '\0')
627be528732Sguenther break;
628be528732Sguenther
629df930be7Sderaadt *spt = '\0';
630df930be7Sderaadt
631df930be7Sderaadt /*
632df930be7Sderaadt * if it exists we assume it is a directory, it is not within
633df930be7Sderaadt * the spec (at least it seems to read that way) to alter the
634df930be7Sderaadt * file system for nodes NOT EXPLICITLY stored on the archive.
635df930be7Sderaadt * If that assumption is changed, you would test the node here
636df930be7Sderaadt * and figure out how to get rid of it (probably like some
637df930be7Sderaadt * recursive unlink()) or fix up the directory permissions if
638df930be7Sderaadt * required (do an access()).
639df930be7Sderaadt */
640df930be7Sderaadt if (lstat(name, &sb) == 0) {
641be528732Sguenther *spt = '/';
642be528732Sguenther spt = next;
643df930be7Sderaadt continue;
644df930be7Sderaadt }
645df930be7Sderaadt
646df930be7Sderaadt /*
647df930be7Sderaadt * the path fails at this point, see if we can create the
648df930be7Sderaadt * needed directory and continue on
649df930be7Sderaadt */
6503aaa63ebSderaadt if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) == -1) {
6518cc78d45Sespie if (!ign)
6528cc78d45Sespie syswarn(1, errno, "Unable to mkdir %s", name);
653df930be7Sderaadt *spt = '/';
654df930be7Sderaadt retval = -1;
655df930be7Sderaadt break;
656df930be7Sderaadt }
657df930be7Sderaadt
658df930be7Sderaadt /*
659df930be7Sderaadt * we were able to create the directory. We will tell the
660df930be7Sderaadt * caller that we found something to fix, and it is ok to try
661df930be7Sderaadt * and create the node again.
662df930be7Sderaadt */
663df930be7Sderaadt retval = 0;
664df930be7Sderaadt if (pids)
665df930be7Sderaadt (void)set_ids(name, st_uid, st_gid);
666df930be7Sderaadt
667df930be7Sderaadt /*
6684eb0b000Smillert * make sure the user doesn't have some strange umask that
669df930be7Sderaadt * causes this newly created directory to be unusable. We fix
670df930be7Sderaadt * the modes and restore them back to the creation default at
671df930be7Sderaadt * the end of pax
672df930be7Sderaadt */
6733aaa63ebSderaadt if ((access(name, R_OK | W_OK | X_OK) == -1) &&
674df930be7Sderaadt (lstat(name, &sb) == 0)) {
675df930be7Sderaadt set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
6769116436cSotto add_dir(name, &sb, 1);
677df930be7Sderaadt }
678be528732Sguenther *spt = '/';
679be528732Sguenther spt = next;
680df930be7Sderaadt continue;
681df930be7Sderaadt }
682df930be7Sderaadt return(retval);
683df930be7Sderaadt }
684df930be7Sderaadt
685df930be7Sderaadt /*
686df930be7Sderaadt * set_ftime()
6874eb0b000Smillert * Set the access time and modification time for a named file. If frc
6884eb0b000Smillert * is non-zero we force these times to be set even if the user did not
689df930be7Sderaadt * request access and/or modification time preservation (this is also
690df930be7Sderaadt * used by -t to reset access times).
691df930be7Sderaadt * When ign is zero, only those times the user has asked for are set, the
6922e9755b3Smillert * other ones are left alone.
693df930be7Sderaadt */
694df930be7Sderaadt
695df930be7Sderaadt void
set_ftime(const char * fnm,const struct timespec * mtimp,const struct timespec * atimp,int frc)6968b72bc25Sguenther set_ftime(const char *fnm, const struct timespec *mtimp,
6978b72bc25Sguenther const struct timespec *atimp, int frc)
698df930be7Sderaadt {
6992e9755b3Smillert struct timespec tv[2];
700df930be7Sderaadt
7018b72bc25Sguenther tv[0] = *atimp;
7028b72bc25Sguenther tv[1] = *mtimp;
7038b72bc25Sguenther
704007bef9eSguenther if (!frc) {
705df930be7Sderaadt /*
706df930be7Sderaadt * if we are not forcing, only set those times the user wants
7072e9755b3Smillert * set.
708df930be7Sderaadt */
709df930be7Sderaadt if (!patime)
7102e9755b3Smillert tv[0].tv_nsec = UTIME_OMIT;
711df930be7Sderaadt if (!pmtime)
7122e9755b3Smillert tv[1].tv_nsec = UTIME_OMIT;
713df930be7Sderaadt }
714df930be7Sderaadt
715df930be7Sderaadt /*
716df930be7Sderaadt * set the times
717df930be7Sderaadt */
718007bef9eSguenther if (utimensat(AT_FDCWD, fnm, tv, AT_SYMLINK_NOFOLLOW) < 0)
719df930be7Sderaadt syswarn(1, errno, "Access/modification time set failed on: %s",
720df930be7Sderaadt fnm);
721df930be7Sderaadt }
722df930be7Sderaadt
723c01bd743Sespie static void
fset_ftime(const char * fnm,int fd,const struct timespec * mtimp,const struct timespec * atimp,int frc)7248b72bc25Sguenther fset_ftime(const char *fnm, int fd, const struct timespec *mtimp,
7258b72bc25Sguenther const struct timespec *atimp, int frc)
726f95f8796Sotto {
7272e9755b3Smillert struct timespec tv[2];
728f95f8796Sotto
7298b72bc25Sguenther
7308b72bc25Sguenther tv[0] = *atimp;
7318b72bc25Sguenther tv[1] = *mtimp;
7328b72bc25Sguenther
733007bef9eSguenther if (!frc) {
734f95f8796Sotto /*
735f95f8796Sotto * if we are not forcing, only set those times the user wants
7362e9755b3Smillert * set.
737f95f8796Sotto */
738f95f8796Sotto if (!patime)
7392e9755b3Smillert tv[0].tv_nsec = UTIME_OMIT;
740f95f8796Sotto if (!pmtime)
7412e9755b3Smillert tv[1].tv_nsec = UTIME_OMIT;
742f95f8796Sotto }
743f95f8796Sotto /*
744f95f8796Sotto * set the times
745f95f8796Sotto */
7463aaa63ebSderaadt if (futimens(fd, tv) == -1)
747f95f8796Sotto syswarn(1, errno, "Access/modification time set failed on: %s",
748f95f8796Sotto fnm);
749f95f8796Sotto }
750f95f8796Sotto
751df930be7Sderaadt /*
752df930be7Sderaadt * set_ids()
753df930be7Sderaadt * set the uid and gid of a file system node
754df930be7Sderaadt * Return:
755df930be7Sderaadt * 0 when set, -1 on failure
756df930be7Sderaadt */
757df930be7Sderaadt
758df930be7Sderaadt int
set_ids(char * fnm,uid_t uid,gid_t gid)759df930be7Sderaadt set_ids(char *fnm, uid_t uid, gid_t gid)
760df930be7Sderaadt {
7613aaa63ebSderaadt if (fchownat(AT_FDCWD, fnm, uid, gid, AT_SYMLINK_NOFOLLOW) == -1) {
7623c75bef4Smillert /*
7633c75bef4Smillert * ignore EPERM unless in verbose mode or being run by root.
764ef8af512Smillert * if running as pax, POSIX requires a warning.
7653c75bef4Smillert */
7663228b364Sguenther if (op_mode == OP_PAX || errno != EPERM || vflag ||
767ef8af512Smillert geteuid() == 0)
7686df3e62cSmillert syswarn(1, errno, "Unable to set file uid/gid of %s",
7693c75bef4Smillert fnm);
770df930be7Sderaadt return(-1);
771df930be7Sderaadt }
772df930be7Sderaadt return(0);
773df930be7Sderaadt }
774df930be7Sderaadt
775f95f8796Sotto int
fset_ids(char * fnm,int fd,uid_t uid,gid_t gid)776f95f8796Sotto fset_ids(char *fnm, int fd, uid_t uid, gid_t gid)
777f95f8796Sotto {
7783aaa63ebSderaadt if (fchown(fd, uid, gid) == -1) {
779f95f8796Sotto /*
780f95f8796Sotto * ignore EPERM unless in verbose mode or being run by root.
781f95f8796Sotto * if running as pax, POSIX requires a warning.
782f95f8796Sotto */
7833228b364Sguenther if (op_mode == OP_PAX || errno != EPERM || vflag ||
784f95f8796Sotto geteuid() == 0)
785f95f8796Sotto syswarn(1, errno, "Unable to set file uid/gid of %s",
786f95f8796Sotto fnm);
787f95f8796Sotto return(-1);
788f95f8796Sotto }
789f95f8796Sotto return(0);
790f95f8796Sotto }
791f95f8796Sotto
792df930be7Sderaadt /*
793df930be7Sderaadt * set_pmode()
794df930be7Sderaadt * Set file access mode
795df930be7Sderaadt */
796df930be7Sderaadt
797df930be7Sderaadt void
set_pmode(char * fnm,mode_t mode)798df930be7Sderaadt set_pmode(char *fnm, mode_t mode)
799df930be7Sderaadt {
800df930be7Sderaadt mode &= ABITS;
8013aaa63ebSderaadt if (fchmodat(AT_FDCWD, fnm, mode, AT_SYMLINK_NOFOLLOW) == -1)
802df930be7Sderaadt syswarn(1, errno, "Could not set permissions on %s", fnm);
803df930be7Sderaadt }
804df930be7Sderaadt
805c01bd743Sespie static void
fset_pmode(char * fnm,int fd,mode_t mode)806f95f8796Sotto fset_pmode(char *fnm, int fd, mode_t mode)
807f95f8796Sotto {
808f95f8796Sotto mode &= ABITS;
8093aaa63ebSderaadt if (fchmod(fd, mode) == -1)
810f95f8796Sotto syswarn(1, errno, "Could not set permissions on %s", fnm);
811f95f8796Sotto }
812f95f8796Sotto
813df930be7Sderaadt /*
8142dbd6dc5Sguenther * set_attr()
8152dbd6dc5Sguenther * Given a DIRDATA, restore the mode and times as indicated, but
8162dbd6dc5Sguenther * only after verifying that it's the directory that we wanted.
8172dbd6dc5Sguenther */
8182dbd6dc5Sguenther int
set_attr(const struct file_times * ft,int force_times,mode_t mode,int do_mode,int in_sig)8192dbd6dc5Sguenther set_attr(const struct file_times *ft, int force_times, mode_t mode,
8202dbd6dc5Sguenther int do_mode, int in_sig)
8212dbd6dc5Sguenther {
8222dbd6dc5Sguenther struct stat sb;
8232dbd6dc5Sguenther int fd, r;
8242dbd6dc5Sguenther
8252dbd6dc5Sguenther if (!do_mode && !force_times && !patime && !pmtime)
8262dbd6dc5Sguenther return (0);
8272dbd6dc5Sguenther
8282dbd6dc5Sguenther /*
8292dbd6dc5Sguenther * We could legitimately go through a symlink here,
8302dbd6dc5Sguenther * so do *not* use O_NOFOLLOW. The dev+ino check will
8312dbd6dc5Sguenther * protect us from evil.
8322dbd6dc5Sguenther */
8332dbd6dc5Sguenther fd = open(ft->ft_name, O_RDONLY | O_DIRECTORY);
8342dbd6dc5Sguenther if (fd == -1) {
8352dbd6dc5Sguenther if (!in_sig)
8362dbd6dc5Sguenther syswarn(1, errno, "Unable to restore mode and times"
8372dbd6dc5Sguenther " for directory: %s", ft->ft_name);
8382dbd6dc5Sguenther return (-1);
8392dbd6dc5Sguenther }
8402dbd6dc5Sguenther
8412dbd6dc5Sguenther if (fstat(fd, &sb) == -1) {
8422dbd6dc5Sguenther if (!in_sig)
8432dbd6dc5Sguenther syswarn(1, errno, "Unable to stat directory: %s",
8442dbd6dc5Sguenther ft->ft_name);
8452dbd6dc5Sguenther r = -1;
8462dbd6dc5Sguenther } else if (ft->ft_ino != sb.st_ino || ft->ft_dev != sb.st_dev) {
8472dbd6dc5Sguenther if (!in_sig)
8482dbd6dc5Sguenther paxwarn(1, "Directory vanished before restoring"
8492dbd6dc5Sguenther " mode and times: %s", ft->ft_name);
8502dbd6dc5Sguenther r = -1;
8512dbd6dc5Sguenther } else {
8522dbd6dc5Sguenther /* Whew, it's a match! Is there anything to change? */
8532dbd6dc5Sguenther if (do_mode && (mode & ABITS) != (sb.st_mode & ABITS))
8542dbd6dc5Sguenther fset_pmode(ft->ft_name, fd, mode);
8558b72bc25Sguenther if (((force_times || patime) &&
8568b72bc25Sguenther timespeccmp(&ft->ft_atim, &sb.st_atim, !=)) ||
8578b72bc25Sguenther ((force_times || pmtime) &&
8588b72bc25Sguenther timespeccmp(&ft->ft_mtim, &sb.st_mtim, !=)))
8598b72bc25Sguenther fset_ftime(ft->ft_name, fd, &ft->ft_mtim,
8608b72bc25Sguenther &ft->ft_atim, force_times);
8612dbd6dc5Sguenther r = 0;
8622dbd6dc5Sguenther }
8632dbd6dc5Sguenther close(fd);
8642dbd6dc5Sguenther
8652dbd6dc5Sguenther return (r);
8662dbd6dc5Sguenther }
8672dbd6dc5Sguenther
8682dbd6dc5Sguenther
8692dbd6dc5Sguenther /*
870df930be7Sderaadt * file_write()
871df930be7Sderaadt * Write/copy a file (during copy or archive extract). This routine knows
872df930be7Sderaadt * how to copy files with lseek holes in it. (Which are read as file
873df930be7Sderaadt * blocks containing all 0's but do not have any file blocks associated
874df930be7Sderaadt * with the data). Typical examples of these are files created by dbm
875df930be7Sderaadt * variants (.pag files). While the file size of these files are huge, the
876df930be7Sderaadt * actual storage is quite small (the files are sparse). The problem is
877df930be7Sderaadt * the holes read as all zeros so are probably stored on the archive that
878df930be7Sderaadt * way (there is no way to determine if the file block is really a hole,
879df930be7Sderaadt * we only know that a file block of all zero's can be a hole).
880df930be7Sderaadt * At this writing, no major archive format knows how to archive files
881df930be7Sderaadt * with holes. However, on extraction (or during copy, -rw) we have to
882df930be7Sderaadt * deal with these files. Without detecting the holes, the files can
883df930be7Sderaadt * consume a lot of file space if just written to disk. This replacement
884df930be7Sderaadt * for write when passed the basic allocation size of a file system block,
885df930be7Sderaadt * uses lseek whenever it detects the input data is all 0 within that
886df930be7Sderaadt * file block. In more detail, the strategy is as follows:
887df930be7Sderaadt * While the input is all zero keep doing an lseek. Keep track of when we
8884eb0b000Smillert * pass over file block boundaries. Only write when we hit a non zero
889df930be7Sderaadt * input. once we have written a file block, we continue to write it to
890df930be7Sderaadt * the end (we stop looking at the input). When we reach the start of the
891df930be7Sderaadt * next file block, start checking for zero blocks again. Working on file
8924eb0b000Smillert * block boundaries significantly reduces the overhead when copying files
893df930be7Sderaadt * that are NOT very sparse. This overhead (when compared to a write) is
894df930be7Sderaadt * almost below the measurement resolution on many systems. Without it,
895df930be7Sderaadt * files with holes cannot be safely copied. It does has a side effect as
896df930be7Sderaadt * it can put holes into files that did not have them before, but that is
897df930be7Sderaadt * not a problem since the file contents are unchanged (in fact it saves
898df930be7Sderaadt * file space). (Except on paging files for diskless clients. But since we
899df930be7Sderaadt * cannot determine one of those file from here, we ignore them). If this
900df930be7Sderaadt * ever ends up on a system where CTG files are supported and the holes
901df930be7Sderaadt * are not desired, just do a conditional test in those routines that
902df930be7Sderaadt * call file_write() and have it call write() instead. BEFORE CLOSING THE
903df930be7Sderaadt * FILE, make sure to call file_flush() when the last write finishes with
904df930be7Sderaadt * an empty block. A lot of file systems will not create an lseek hole at
905df930be7Sderaadt * the end. In this case we drop a single 0 at the end to force the
906df930be7Sderaadt * trailing 0's in the file.
907df930be7Sderaadt * ---Parameters---
908df930be7Sderaadt * rem: how many bytes left in this file system block
909df930be7Sderaadt * isempt: have we written to the file block yet (is it empty)
910df930be7Sderaadt * sz: basic file block allocation size
911df930be7Sderaadt * cnt: number of bytes on this write
912df930be7Sderaadt * str: buffer to write
913df930be7Sderaadt * Return:
914df930be7Sderaadt * number of bytes written, -1 on write (or lseek) error.
915df930be7Sderaadt */
916df930be7Sderaadt
917df930be7Sderaadt int
file_write(int fd,char * str,int cnt,int * rem,int * isempt,int sz,char * name)918be87792eSmillert file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
919df930be7Sderaadt char *name)
920df930be7Sderaadt {
921be87792eSmillert char *pt;
922be87792eSmillert char *end;
923be87792eSmillert int wcnt;
924be87792eSmillert char *st = str;
925a79a0570Smillert char **strp;
926df930be7Sderaadt
927df930be7Sderaadt /*
928df930be7Sderaadt * while we have data to process
929df930be7Sderaadt */
930df930be7Sderaadt while (cnt) {
931df930be7Sderaadt if (!*rem) {
932df930be7Sderaadt /*
933df930be7Sderaadt * We are now at the start of file system block again
934df930be7Sderaadt * (or what we think one is...). start looking for
935df930be7Sderaadt * empty blocks again
936df930be7Sderaadt */
937df930be7Sderaadt *isempt = 1;
938df930be7Sderaadt *rem = sz;
939df930be7Sderaadt }
940df930be7Sderaadt
941df930be7Sderaadt /*
942df930be7Sderaadt * only examine up to the end of the current file block or
943df930be7Sderaadt * remaining characters to write, whatever is smaller
944df930be7Sderaadt */
945b9fc9a72Sderaadt wcnt = MINIMUM(cnt, *rem);
946df930be7Sderaadt cnt -= wcnt;
947df930be7Sderaadt *rem -= wcnt;
948df930be7Sderaadt if (*isempt) {
949df930be7Sderaadt /*
950df930be7Sderaadt * have not written to this block yet, so we keep
951df930be7Sderaadt * looking for zero's
952df930be7Sderaadt */
953df930be7Sderaadt pt = st;
954df930be7Sderaadt end = st + wcnt;
955df930be7Sderaadt
956df930be7Sderaadt /*
957df930be7Sderaadt * look for a zero filled buffer
958df930be7Sderaadt */
959df930be7Sderaadt while ((pt < end) && (*pt == '\0'))
960df930be7Sderaadt ++pt;
961df930be7Sderaadt
962df930be7Sderaadt if (pt == end) {
963df930be7Sderaadt /*
964df930be7Sderaadt * skip, buf is empty so far
965df930be7Sderaadt */
966da60b202Smillert if (fd > -1 &&
9679a58b8c6Sguenther lseek(fd, wcnt, SEEK_CUR) < 0) {
968df930be7Sderaadt syswarn(1,errno,"File seek on %s",
969df930be7Sderaadt name);
970df930be7Sderaadt return(-1);
971df930be7Sderaadt }
972df930be7Sderaadt st = pt;
973df930be7Sderaadt continue;
974df930be7Sderaadt }
975df930be7Sderaadt /*
976df930be7Sderaadt * drat, the buf is not zero filled
977df930be7Sderaadt */
978df930be7Sderaadt *isempt = 0;
979df930be7Sderaadt }
980df930be7Sderaadt
981df930be7Sderaadt /*
982df930be7Sderaadt * have non-zero data in this file system block, have to write
983df930be7Sderaadt */
984a79a0570Smillert switch (fd) {
985a79a0570Smillert case -1:
986a79a0570Smillert strp = &gnu_name_string;
987a79a0570Smillert break;
988a79a0570Smillert case -2:
989a79a0570Smillert strp = &gnu_link_string;
990a79a0570Smillert break;
991a79a0570Smillert default:
992a79a0570Smillert strp = NULL;
993a79a0570Smillert break;
994a79a0570Smillert }
995a79a0570Smillert if (strp) {
996a79a0570Smillert if (*strp)
997da60b202Smillert err(1, "WARNING! Major Internal Error! GNU hack Failing!");
998a79a0570Smillert *strp = malloc(wcnt + 1);
999a79a0570Smillert if (*strp == NULL) {
1000da60b202Smillert paxwarn(1, "Out of memory");
1001da60b202Smillert return(-1);
1002da60b202Smillert }
1003a79a0570Smillert memcpy(*strp, st, wcnt);
1004a79a0570Smillert (*strp)[wcnt] = '\0';
1005a79a0570Smillert break;
1006da60b202Smillert } else if (write(fd, st, wcnt) != wcnt) {
1007df930be7Sderaadt syswarn(1, errno, "Failed write to file %s", name);
1008df930be7Sderaadt return(-1);
1009df930be7Sderaadt }
1010df930be7Sderaadt st += wcnt;
1011df930be7Sderaadt }
1012df930be7Sderaadt return(st - str);
1013df930be7Sderaadt }
1014df930be7Sderaadt
1015df930be7Sderaadt /*
1016df930be7Sderaadt * file_flush()
1017df930be7Sderaadt * when the last file block in a file is zero, many file systems will not
1018df930be7Sderaadt * let us create a hole at the end. To get the last block with zeros, we
1019df930be7Sderaadt * write the last BYTE with a zero (back up one byte and write a zero).
1020df930be7Sderaadt */
1021df930be7Sderaadt
1022df930be7Sderaadt void
file_flush(int fd,char * fname,int isempt)1023df930be7Sderaadt file_flush(int fd, char *fname, int isempt)
1024df930be7Sderaadt {
1025df930be7Sderaadt static char blnk[] = "\0";
1026df930be7Sderaadt
1027df930be7Sderaadt /*
1028df930be7Sderaadt * silly test, but make sure we are only called when the last block is
1029df930be7Sderaadt * filled with all zeros.
1030df930be7Sderaadt */
1031df930be7Sderaadt if (!isempt)
1032df930be7Sderaadt return;
1033df930be7Sderaadt
1034df930be7Sderaadt /*
1035df930be7Sderaadt * move back one byte and write a zero
1036df930be7Sderaadt */
10379a58b8c6Sguenther if (lseek(fd, -1, SEEK_CUR) < 0) {
1038df930be7Sderaadt syswarn(1, errno, "Failed seek on file %s", fname);
1039df930be7Sderaadt return;
1040df930be7Sderaadt }
1041df930be7Sderaadt
10423aaa63ebSderaadt if (write(fd, blnk, 1) == -1)
1043df930be7Sderaadt syswarn(1, errno, "Failed write to file %s", fname);
1044df930be7Sderaadt }
1045df930be7Sderaadt
1046df930be7Sderaadt /*
1047df930be7Sderaadt * rdfile_close()
1048220d81c0Sjasper * close a file we have been reading (to copy or archive). If we have to
1049df930be7Sderaadt * reset access time (tflag) do so (the times are stored in arcn).
1050df930be7Sderaadt */
1051df930be7Sderaadt
1052df930be7Sderaadt void
rdfile_close(ARCHD * arcn,int * fd)1053be87792eSmillert rdfile_close(ARCHD *arcn, int *fd)
1054df930be7Sderaadt {
1055df930be7Sderaadt /*
1056df930be7Sderaadt * make sure the file is open
1057df930be7Sderaadt */
1058df930be7Sderaadt if (*fd < 0)
1059df930be7Sderaadt return;
1060df930be7Sderaadt
1061df930be7Sderaadt /*
1062df930be7Sderaadt * user wants last access time reset
1063df930be7Sderaadt */
10642dbd6dc5Sguenther if (tflag)
10658b72bc25Sguenther fset_ftime(arcn->org_name, *fd, &arcn->sb.st_mtim,
10668b72bc25Sguenther &arcn->sb.st_atim, 1);
10672dbd6dc5Sguenther
10682dbd6dc5Sguenther (void)close(*fd);
10692dbd6dc5Sguenther *fd = -1;
1070df930be7Sderaadt }
1071df930be7Sderaadt
1072df930be7Sderaadt /*
1073df930be7Sderaadt * set_crc()
1074df930be7Sderaadt * read a file to calculate its crc. This is a real drag. Archive formats
1075df930be7Sderaadt * that have this, end up reading the file twice (we have to write the
1076df930be7Sderaadt * header WITH the crc before writing the file contents. Oh well...
1077df930be7Sderaadt * Return:
1078df930be7Sderaadt * 0 if was able to calculate the crc, -1 otherwise
1079df930be7Sderaadt */
1080df930be7Sderaadt
1081df930be7Sderaadt int
set_crc(ARCHD * arcn,int fd)1082be87792eSmillert set_crc(ARCHD *arcn, int fd)
1083df930be7Sderaadt {
1084be87792eSmillert int i;
1085be87792eSmillert int res;
10869a58b8c6Sguenther off_t cpcnt = 0;
10879a58b8c6Sguenther size_t size;
10884a51f016Sotto u_int32_t crc = 0;
1089df930be7Sderaadt char tbuf[FILEBLK];
1090df930be7Sderaadt struct stat sb;
1091df930be7Sderaadt
1092df930be7Sderaadt if (fd < 0) {
1093df930be7Sderaadt /*
1094df930be7Sderaadt * hmm, no fd, should never happen. well no crc then.
1095df930be7Sderaadt */
10969a58b8c6Sguenther arcn->crc = 0;
1097df930be7Sderaadt return(0);
1098df930be7Sderaadt }
1099df930be7Sderaadt
11009a58b8c6Sguenther if ((size = arcn->sb.st_blksize) > sizeof(tbuf))
11019a58b8c6Sguenther size = sizeof(tbuf);
1102df930be7Sderaadt
1103df930be7Sderaadt /*
1104df930be7Sderaadt * read all the bytes we think that there are in the file. If the user
1105df930be7Sderaadt * is trying to archive an active file, forget this file.
1106df930be7Sderaadt */
1107df930be7Sderaadt for (;;) {
1108df930be7Sderaadt if ((res = read(fd, tbuf, size)) <= 0)
1109df930be7Sderaadt break;
1110df930be7Sderaadt cpcnt += res;
1111df930be7Sderaadt for (i = 0; i < res; ++i)
1112df930be7Sderaadt crc += (tbuf[i] & 0xff);
1113df930be7Sderaadt }
1114df930be7Sderaadt
1115df930be7Sderaadt /*
1116df930be7Sderaadt * safety check. we want to avoid archiving files that are active as
111708cab283Sjmc * they can create inconsistent archive copies.
1118df930be7Sderaadt */
1119df930be7Sderaadt if (cpcnt != arcn->sb.st_size)
112042cf9836Stholo paxwarn(1, "File changed size %s", arcn->org_name);
11213aaa63ebSderaadt else if (fstat(fd, &sb) == -1)
1122df930be7Sderaadt syswarn(1, errno, "Failed stat on %s", arcn->org_name);
11238b72bc25Sguenther else if (timespeccmp(&arcn->sb.st_mtim, &sb.st_mtim, !=))
112442cf9836Stholo paxwarn(1, "File %s was modified during read", arcn->org_name);
11259a58b8c6Sguenther else if (lseek(fd, 0, SEEK_SET) < 0)
1126df930be7Sderaadt syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
1127df930be7Sderaadt else {
1128df930be7Sderaadt arcn->crc = crc;
1129df930be7Sderaadt return(0);
1130df930be7Sderaadt }
1131df930be7Sderaadt return(-1);
1132df930be7Sderaadt }
1133