file_subs.c revision 1.15.2.2 1 1.15.2.2 mycroft /* $NetBSD: file_subs.c,v 1.15.2.2 1999/11/07 15:48:25 mycroft Exp $ */
2 1.15.2.2 mycroft
3 1.15.2.2 mycroft /*-
4 1.15.2.2 mycroft * Copyright (c) 1992 Keith Muller.
5 1.15.2.2 mycroft * Copyright (c) 1992, 1993
6 1.15.2.2 mycroft * The Regents of the University of California. All rights reserved.
7 1.15.2.2 mycroft *
8 1.15.2.2 mycroft * This code is derived from software contributed to Berkeley by
9 1.15.2.2 mycroft * Keith Muller of the University of California, San Diego.
10 1.15.2.2 mycroft *
11 1.15.2.2 mycroft * Redistribution and use in source and binary forms, with or without
12 1.15.2.2 mycroft * modification, are permitted provided that the following conditions
13 1.15.2.2 mycroft * are met:
14 1.15.2.2 mycroft * 1. Redistributions of source code must retain the above copyright
15 1.15.2.2 mycroft * notice, this list of conditions and the following disclaimer.
16 1.15.2.2 mycroft * 2. Redistributions in binary form must reproduce the above copyright
17 1.15.2.2 mycroft * notice, this list of conditions and the following disclaimer in the
18 1.15.2.2 mycroft * documentation and/or other materials provided with the distribution.
19 1.15.2.2 mycroft * 3. All advertising materials mentioning features or use of this software
20 1.15.2.2 mycroft * must display the following acknowledgement:
21 1.15.2.2 mycroft * This product includes software developed by the University of
22 1.15.2.2 mycroft * California, Berkeley and its contributors.
23 1.15.2.2 mycroft * 4. Neither the name of the University nor the names of its contributors
24 1.15.2.2 mycroft * may be used to endorse or promote products derived from this software
25 1.15.2.2 mycroft * without specific prior written permission.
26 1.15.2.2 mycroft *
27 1.15.2.2 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.15.2.2 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.15.2.2 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.15.2.2 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.15.2.2 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.15.2.2 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.15.2.2 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.15.2.2 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.15.2.2 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.15.2.2 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.15.2.2 mycroft * SUCH DAMAGE.
38 1.15.2.2 mycroft */
39 1.15.2.2 mycroft
40 1.15.2.2 mycroft #include <sys/cdefs.h>
41 1.15.2.2 mycroft #ifndef lint
42 1.15.2.2 mycroft #if 0
43 1.15.2.2 mycroft static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93";
44 1.15.2.2 mycroft #else
45 1.15.2.2 mycroft __RCSID("$NetBSD: file_subs.c,v 1.15.2.2 1999/11/07 15:48:25 mycroft Exp $");
46 1.15.2.2 mycroft #endif
47 1.15.2.2 mycroft #endif /* not lint */
48 1.15.2.2 mycroft
49 1.15.2.2 mycroft #include <sys/types.h>
50 1.15.2.2 mycroft #include <sys/time.h>
51 1.15.2.2 mycroft #include <sys/stat.h>
52 1.15.2.2 mycroft #include <unistd.h>
53 1.15.2.2 mycroft #include <sys/param.h>
54 1.15.2.2 mycroft #include <err.h>
55 1.15.2.2 mycroft #include <fcntl.h>
56 1.15.2.2 mycroft #include <string.h>
57 1.15.2.2 mycroft #include <stdio.h>
58 1.15.2.2 mycroft #include <ctype.h>
59 1.15.2.2 mycroft #include <errno.h>
60 1.15.2.2 mycroft #include <sys/uio.h>
61 1.15.2.2 mycroft #include <stdlib.h>
62 1.15.2.2 mycroft #include "pax.h"
63 1.15.2.2 mycroft #include "extern.h"
64 1.15.2.2 mycroft
65 1.15.2.2 mycroft static int
66 1.15.2.2 mycroft mk_link __P((char *,struct stat *,char *, int));
67 1.15.2.2 mycroft
68 1.15.2.2 mycroft /*
69 1.15.2.2 mycroft * routines that deal with file operations such as: creating, removing;
70 1.15.2.2 mycroft * and setting access modes, uid/gid and times of files
71 1.15.2.2 mycroft */
72 1.15.2.2 mycroft
73 1.15.2.2 mycroft #define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
74 1.15.2.2 mycroft #define SETBITS (S_ISUID | S_ISGID)
75 1.15.2.2 mycroft #define ABITS (FILEBITS | SETBITS)
76 1.15.2.2 mycroft
77 1.15.2.2 mycroft /*
78 1.15.2.2 mycroft * file_creat()
79 1.15.2.2 mycroft * Create and open a file.
80 1.15.2.2 mycroft * Return:
81 1.15.2.2 mycroft * file descriptor or -1 for failure
82 1.15.2.2 mycroft */
83 1.15.2.2 mycroft
84 1.15.2.2 mycroft #if __STDC__
85 1.15.2.2 mycroft int
86 1.15.2.2 mycroft file_creat(ARCHD *arcn)
87 1.15.2.2 mycroft #else
88 1.15.2.2 mycroft int
89 1.15.2.2 mycroft file_creat(arcn)
90 1.15.2.2 mycroft ARCHD *arcn;
91 1.15.2.2 mycroft #endif
92 1.15.2.2 mycroft {
93 1.15.2.2 mycroft int fd = -1;
94 1.15.2.2 mycroft mode_t file_mode;
95 1.15.2.2 mycroft int oerrno;
96 1.15.2.2 mycroft
97 1.15.2.2 mycroft /*
98 1.15.2.2 mycroft * assume file doesn't exist, so just try to create it, most times this
99 1.15.2.2 mycroft * works. We have to take special handling when the file does exist. To
100 1.15.2.2 mycroft * detect this, we use O_EXCL. For example when trying to create a
101 1.15.2.2 mycroft * file and a character device or fifo exists with the same name, we
102 1.15.2.2 mycroft * can accidently open the device by mistake (or block waiting to open)
103 1.15.2.2 mycroft * If we find that the open has failed, then figure spend the effore to
104 1.15.2.2 mycroft * figure out why. This strategy was found to have better average
105 1.15.2.2 mycroft * performance in common use than checking the file (and the path)
106 1.15.2.2 mycroft * first with lstat.
107 1.15.2.2 mycroft */
108 1.15.2.2 mycroft file_mode = arcn->sb.st_mode & FILEBITS;
109 1.15.2.2 mycroft if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
110 1.15.2.2 mycroft file_mode)) >= 0)
111 1.15.2.2 mycroft return(fd);
112 1.15.2.2 mycroft
113 1.15.2.2 mycroft /*
114 1.15.2.2 mycroft * the file seems to exist. First we try to get rid of it (found to be
115 1.15.2.2 mycroft * the second most common failure when traced). If this fails, only
116 1.15.2.2 mycroft * then we go to the expense to check and create the path to the file
117 1.15.2.2 mycroft */
118 1.15.2.2 mycroft if (unlnk_exist(arcn->name, arcn->type) != 0)
119 1.15.2.2 mycroft return(-1);
120 1.15.2.2 mycroft
121 1.15.2.2 mycroft for (;;) {
122 1.15.2.2 mycroft /*
123 1.15.2.2 mycroft * try to open it again, if this fails, check all the nodes in
124 1.15.2.2 mycroft * the path and give it a final try. if chk_path() finds that
125 1.15.2.2 mycroft * it cannot fix anything, we will skip the last attempt
126 1.15.2.2 mycroft */
127 1.15.2.2 mycroft if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
128 1.15.2.2 mycroft file_mode)) >= 0)
129 1.15.2.2 mycroft break;
130 1.15.2.2 mycroft oerrno = errno;
131 1.15.2.2 mycroft if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
132 1.15.2.2 mycroft syswarn(1, oerrno, "Unable to create %s", arcn->name);
133 1.15.2.2 mycroft return(-1);
134 1.15.2.2 mycroft }
135 1.15.2.2 mycroft }
136 1.15.2.2 mycroft return(fd);
137 1.15.2.2 mycroft }
138 1.15.2.2 mycroft
139 1.15.2.2 mycroft /*
140 1.15.2.2 mycroft * file_close()
141 1.15.2.2 mycroft * Close file descriptor to a file just created by pax. Sets modes,
142 1.15.2.2 mycroft * ownership and times as required.
143 1.15.2.2 mycroft * Return:
144 1.15.2.2 mycroft * 0 for success, -1 for failure
145 1.15.2.2 mycroft */
146 1.15.2.2 mycroft
147 1.15.2.2 mycroft #if __STDC__
148 1.15.2.2 mycroft void
149 1.15.2.2 mycroft file_close(ARCHD *arcn, int fd)
150 1.15.2.2 mycroft #else
151 1.15.2.2 mycroft void
152 1.15.2.2 mycroft file_close(arcn, fd)
153 1.15.2.2 mycroft ARCHD *arcn;
154 1.15.2.2 mycroft int fd;
155 1.15.2.2 mycroft #endif
156 1.15.2.2 mycroft {
157 1.15.2.2 mycroft int res = 0;
158 1.15.2.2 mycroft
159 1.15.2.2 mycroft if (fd < 0)
160 1.15.2.2 mycroft return;
161 1.15.2.2 mycroft if (close(fd) < 0)
162 1.15.2.2 mycroft syswarn(0, errno, "Unable to close file descriptor on %s",
163 1.15.2.2 mycroft arcn->name);
164 1.15.2.2 mycroft
165 1.15.2.2 mycroft /*
166 1.15.2.2 mycroft * set owner/groups first as this may strip off mode bits we want
167 1.15.2.2 mycroft * then set file permission modes. Then set file access and
168 1.15.2.2 mycroft * modification times.
169 1.15.2.2 mycroft */
170 1.15.2.2 mycroft if (pids)
171 1.15.2.2 mycroft res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
172 1.15.2.2 mycroft
173 1.15.2.2 mycroft /*
174 1.15.2.2 mycroft * IMPORTANT SECURITY NOTE:
175 1.15.2.2 mycroft * if not preserving mode or we cannot set uid/gid, then PROHIBIT
176 1.15.2.2 mycroft * set uid/gid bits
177 1.15.2.2 mycroft */
178 1.15.2.2 mycroft if (!pmode || res)
179 1.15.2.2 mycroft arcn->sb.st_mode &= ~(SETBITS);
180 1.15.2.2 mycroft if (pmode)
181 1.15.2.2 mycroft set_pmode(arcn->name, arcn->sb.st_mode);
182 1.15.2.2 mycroft if (patime || pmtime)
183 1.15.2.2 mycroft set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
184 1.15.2.2 mycroft if (pfflags && arcn->type != PAX_SLK)
185 1.15.2.2 mycroft set_chflags(arcn->name, arcn->sb.st_flags);
186 1.15.2.2 mycroft }
187 1.15.2.2 mycroft
188 1.15.2.2 mycroft /*
189 1.15.2.2 mycroft * lnk_creat()
190 1.15.2.2 mycroft * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
191 1.15.2.2 mycroft * must exist;
192 1.15.2.2 mycroft * Return:
193 1.15.2.2 mycroft * 0 if ok, -1 otherwise
194 1.15.2.2 mycroft */
195 1.15.2.2 mycroft
196 1.15.2.2 mycroft #if __STDC__
197 1.15.2.2 mycroft int
198 1.15.2.2 mycroft lnk_creat(ARCHD *arcn)
199 1.15.2.2 mycroft #else
200 1.15.2.2 mycroft int
201 1.15.2.2 mycroft lnk_creat(arcn)
202 1.15.2.2 mycroft ARCHD *arcn;
203 1.15.2.2 mycroft #endif
204 1.15.2.2 mycroft {
205 1.15.2.2 mycroft struct stat sb;
206 1.15.2.2 mycroft
207 1.15.2.2 mycroft /*
208 1.15.2.2 mycroft * we may be running as root, so we have to be sure that link target
209 1.15.2.2 mycroft * is not a directory, so we lstat and check
210 1.15.2.2 mycroft */
211 1.15.2.2 mycroft if (lstat(arcn->ln_name, &sb) < 0) {
212 1.15.2.2 mycroft syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
213 1.15.2.2 mycroft arcn->name);
214 1.15.2.2 mycroft return(-1);
215 1.15.2.2 mycroft }
216 1.15.2.2 mycroft
217 1.15.2.2 mycroft if (S_ISDIR(sb.st_mode)) {
218 1.15.2.2 mycroft tty_warn(1, "A hard link to the directory %s is not allowed",
219 1.15.2.2 mycroft arcn->ln_name);
220 1.15.2.2 mycroft return(-1);
221 1.15.2.2 mycroft }
222 1.15.2.2 mycroft
223 1.15.2.2 mycroft return(mk_link(arcn->ln_name, &sb, arcn->name, 0));
224 1.15.2.2 mycroft }
225 1.15.2.2 mycroft
226 1.15.2.2 mycroft /*
227 1.15.2.2 mycroft * cross_lnk()
228 1.15.2.2 mycroft * Create a hard link to arcn->org_name from arcn->name. Only used in copy
229 1.15.2.2 mycroft * with the -l flag. No warning or error if this does not succeed (we will
230 1.15.2.2 mycroft * then just create the file)
231 1.15.2.2 mycroft * Return:
232 1.15.2.2 mycroft * 1 if copy() should try to create this file node
233 1.15.2.2 mycroft * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
234 1.15.2.2 mycroft */
235 1.15.2.2 mycroft
236 1.15.2.2 mycroft #if __STDC__
237 1.15.2.2 mycroft int
238 1.15.2.2 mycroft cross_lnk(ARCHD *arcn)
239 1.15.2.2 mycroft #else
240 1.15.2.2 mycroft int
241 1.15.2.2 mycroft cross_lnk(arcn)
242 1.15.2.2 mycroft ARCHD *arcn;
243 1.15.2.2 mycroft #endif
244 1.15.2.2 mycroft {
245 1.15.2.2 mycroft /*
246 1.15.2.2 mycroft * try to make a link to orginal file (-l flag in copy mode). make sure
247 1.15.2.2 mycroft * we do not try to link to directories in case we are running as root
248 1.15.2.2 mycroft * (and it might succeed).
249 1.15.2.2 mycroft */
250 1.15.2.2 mycroft if (arcn->type == PAX_DIR)
251 1.15.2.2 mycroft return(1);
252 1.15.2.2 mycroft return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
253 1.15.2.2 mycroft }
254 1.15.2.2 mycroft
255 1.15.2.2 mycroft /*
256 1.15.2.2 mycroft * chk_same()
257 1.15.2.2 mycroft * In copy mode if we are not trying to make hard links between the src
258 1.15.2.2 mycroft * and destinations, make sure we are not going to overwrite ourselves by
259 1.15.2.2 mycroft * accident. This slows things down a little, but we have to protect all
260 1.15.2.2 mycroft * those people who make typing errors.
261 1.15.2.2 mycroft * Return:
262 1.15.2.2 mycroft * 1 the target does not exist, go ahead and copy
263 1.15.2.2 mycroft * 0 skip it file exists (-k) or may be the same as source file
264 1.15.2.2 mycroft */
265 1.15.2.2 mycroft
266 1.15.2.2 mycroft #if __STDC__
267 1.15.2.2 mycroft int
268 1.15.2.2 mycroft chk_same(ARCHD *arcn)
269 1.15.2.2 mycroft #else
270 1.15.2.2 mycroft int
271 1.15.2.2 mycroft chk_same(arcn)
272 1.15.2.2 mycroft ARCHD *arcn;
273 1.15.2.2 mycroft #endif
274 1.15.2.2 mycroft {
275 1.15.2.2 mycroft struct stat sb;
276 1.15.2.2 mycroft
277 1.15.2.2 mycroft /*
278 1.15.2.2 mycroft * if file does not exist, return. if file exists and -k, skip it
279 1.15.2.2 mycroft * quietly
280 1.15.2.2 mycroft */
281 1.15.2.2 mycroft if (lstat(arcn->name, &sb) < 0)
282 1.15.2.2 mycroft return(1);
283 1.15.2.2 mycroft if (kflag)
284 1.15.2.2 mycroft return(0);
285 1.15.2.2 mycroft
286 1.15.2.2 mycroft /*
287 1.15.2.2 mycroft * better make sure the user does not have src == dest by mistake
288 1.15.2.2 mycroft */
289 1.15.2.2 mycroft if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
290 1.15.2.2 mycroft tty_warn(1, "Unable to copy %s, file would overwrite itself",
291 1.15.2.2 mycroft arcn->name);
292 1.15.2.2 mycroft return(0);
293 1.15.2.2 mycroft }
294 1.15.2.2 mycroft return(1);
295 1.15.2.2 mycroft }
296 1.15.2.2 mycroft
297 1.15.2.2 mycroft /*
298 1.15.2.2 mycroft * mk_link()
299 1.15.2.2 mycroft * try to make a hard link between two files. if ign set, we do not
300 1.15.2.2 mycroft * complain.
301 1.15.2.2 mycroft * Return:
302 1.15.2.2 mycroft * 0 if successful (or we are done with this file but no error, such as
303 1.15.2.2 mycroft * finding the from file exists and the user has set -k).
304 1.15.2.2 mycroft * 1 when ign was set to indicates we could not make the link but we
305 1.15.2.2 mycroft * should try to copy/extract the file as that might work (and is an
306 1.15.2.2 mycroft * allowed option). -1 an error occurred.
307 1.15.2.2 mycroft */
308 1.15.2.2 mycroft
309 1.15.2.2 mycroft #if __STDC__
310 1.15.2.2 mycroft static int
311 1.15.2.2 mycroft mk_link(char *to, struct stat *to_sb, char *from,
312 1.15.2.2 mycroft int ign)
313 1.15.2.2 mycroft #else
314 1.15.2.2 mycroft static int
315 1.15.2.2 mycroft mk_link(to, to_sb, from, ign)
316 1.15.2.2 mycroft char *to;
317 1.15.2.2 mycroft struct stat *to_sb;
318 1.15.2.2 mycroft char *from;
319 1.15.2.2 mycroft int ign;
320 1.15.2.2 mycroft #endif
321 1.15.2.2 mycroft {
322 1.15.2.2 mycroft struct stat sb;
323 1.15.2.2 mycroft int oerrno;
324 1.15.2.2 mycroft
325 1.15.2.2 mycroft /*
326 1.15.2.2 mycroft * if from file exists, it has to be unlinked to make the link. If the
327 1.15.2.2 mycroft * file exists and -k is set, skip it quietly
328 1.15.2.2 mycroft */
329 1.15.2.2 mycroft if (lstat(from, &sb) == 0) {
330 1.15.2.2 mycroft if (kflag)
331 1.15.2.2 mycroft return(0);
332 1.15.2.2 mycroft
333 1.15.2.2 mycroft /*
334 1.15.2.2 mycroft * make sure it is not the same file, protect the user
335 1.15.2.2 mycroft */
336 1.15.2.2 mycroft if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
337 1.15.2.2 mycroft tty_warn(1, "Unable to link file %s to itself", to);
338 1.15.2.2 mycroft return(-1);;
339 1.15.2.2 mycroft }
340 1.15.2.2 mycroft
341 1.15.2.2 mycroft /*
342 1.15.2.2 mycroft * try to get rid of the file, based on the type
343 1.15.2.2 mycroft */
344 1.15.2.2 mycroft if (S_ISDIR(sb.st_mode)) {
345 1.15.2.2 mycroft if (rmdir(from) < 0) {
346 1.15.2.2 mycroft syswarn(1, errno, "Unable to remove %s", from);
347 1.15.2.2 mycroft return(-1);
348 1.15.2.2 mycroft }
349 1.15.2.2 mycroft } else if (unlink(from) < 0) {
350 1.15.2.2 mycroft if (!ign) {
351 1.15.2.2 mycroft syswarn(1, errno, "Unable to remove %s", from);
352 1.15.2.2 mycroft return(-1);
353 1.15.2.2 mycroft }
354 1.15.2.2 mycroft return(1);
355 1.15.2.2 mycroft }
356 1.15.2.2 mycroft }
357 1.15.2.2 mycroft
358 1.15.2.2 mycroft /*
359 1.15.2.2 mycroft * from file is gone (or did not exist), try to make the hard link.
360 1.15.2.2 mycroft * if it fails, check the path and try it again (if chk_path() says to
361 1.15.2.2 mycroft * try again)
362 1.15.2.2 mycroft */
363 1.15.2.2 mycroft for (;;) {
364 1.15.2.2 mycroft if (link(to, from) == 0)
365 1.15.2.2 mycroft break;
366 1.15.2.2 mycroft oerrno = errno;
367 1.15.2.2 mycroft if (chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0)
368 1.15.2.2 mycroft continue;
369 1.15.2.2 mycroft if (!ign) {
370 1.15.2.2 mycroft syswarn(1, oerrno, "Could not link to %s from %s", to,
371 1.15.2.2 mycroft from);
372 1.15.2.2 mycroft return(-1);
373 1.15.2.2 mycroft }
374 1.15.2.2 mycroft return(1);
375 1.15.2.2 mycroft }
376 1.15.2.2 mycroft
377 1.15.2.2 mycroft /*
378 1.15.2.2 mycroft * all right the link was made
379 1.15.2.2 mycroft */
380 1.15.2.2 mycroft return(0);
381 1.15.2.2 mycroft }
382 1.15.2.2 mycroft
383 1.15.2.2 mycroft /*
384 1.15.2.2 mycroft * node_creat()
385 1.15.2.2 mycroft * create an entry in the file system (other than a file or hard link).
386 1.15.2.2 mycroft * If successful, sets uid/gid modes and times as required.
387 1.15.2.2 mycroft * Return:
388 1.15.2.2 mycroft * 0 if ok, -1 otherwise
389 1.15.2.2 mycroft */
390 1.15.2.2 mycroft
391 1.15.2.2 mycroft #if __STDC__
392 1.15.2.2 mycroft int
393 1.15.2.2 mycroft node_creat(ARCHD *arcn)
394 1.15.2.2 mycroft #else
395 1.15.2.2 mycroft int
396 1.15.2.2 mycroft node_creat(arcn)
397 1.15.2.2 mycroft ARCHD *arcn;
398 1.15.2.2 mycroft #endif
399 1.15.2.2 mycroft {
400 1.15.2.2 mycroft int res;
401 1.15.2.2 mycroft int ign = 0;
402 1.15.2.2 mycroft int oerrno;
403 1.15.2.2 mycroft int pass = 0;
404 1.15.2.2 mycroft mode_t file_mode;
405 1.15.2.2 mycroft struct stat sb;
406 1.15.2.2 mycroft
407 1.15.2.2 mycroft /*
408 1.15.2.2 mycroft * create node based on type, if that fails try to unlink the node and
409 1.15.2.2 mycroft * try again. finally check the path and try again. As noted in the
410 1.15.2.2 mycroft * file and link creation routines, this method seems to exhibit the
411 1.15.2.2 mycroft * best performance in general use workloads.
412 1.15.2.2 mycroft */
413 1.15.2.2 mycroft file_mode = arcn->sb.st_mode & FILEBITS;
414 1.15.2.2 mycroft
415 1.15.2.2 mycroft for (;;) {
416 1.15.2.2 mycroft switch(arcn->type) {
417 1.15.2.2 mycroft case PAX_DIR:
418 1.15.2.2 mycroft res = mkdir(arcn->name, file_mode);
419 1.15.2.2 mycroft if (ign)
420 1.15.2.2 mycroft res = 0;
421 1.15.2.2 mycroft break;
422 1.15.2.2 mycroft case PAX_CHR:
423 1.15.2.2 mycroft file_mode |= S_IFCHR;
424 1.15.2.2 mycroft res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
425 1.15.2.2 mycroft break;
426 1.15.2.2 mycroft case PAX_BLK:
427 1.15.2.2 mycroft file_mode |= S_IFBLK;
428 1.15.2.2 mycroft res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
429 1.15.2.2 mycroft break;
430 1.15.2.2 mycroft case PAX_FIF:
431 1.15.2.2 mycroft res = mkfifo(arcn->name, file_mode);
432 1.15.2.2 mycroft break;
433 1.15.2.2 mycroft case PAX_SCK:
434 1.15.2.2 mycroft /*
435 1.15.2.2 mycroft * Skip sockets, operation has no meaning under BSD
436 1.15.2.2 mycroft */
437 1.15.2.2 mycroft tty_warn(0,
438 1.15.2.2 mycroft "%s skipped. Sockets cannot be copied or extracted",
439 1.15.2.2 mycroft arcn->name);
440 1.15.2.2 mycroft return(-1);
441 1.15.2.2 mycroft case PAX_SLK:
442 1.15.2.2 mycroft res = symlink(arcn->ln_name, arcn->name);
443 1.15.2.2 mycroft break;
444 1.15.2.2 mycroft case PAX_CTG:
445 1.15.2.2 mycroft case PAX_HLK:
446 1.15.2.2 mycroft case PAX_HRG:
447 1.15.2.2 mycroft case PAX_REG:
448 1.15.2.2 mycroft default:
449 1.15.2.2 mycroft /*
450 1.15.2.2 mycroft * we should never get here
451 1.15.2.2 mycroft */
452 1.15.2.2 mycroft tty_warn(0, "%s has an unknown file type, skipping",
453 1.15.2.2 mycroft arcn->name);
454 1.15.2.2 mycroft return(-1);
455 1.15.2.2 mycroft }
456 1.15.2.2 mycroft
457 1.15.2.2 mycroft /*
458 1.15.2.2 mycroft * if we were able to create the node break out of the loop,
459 1.15.2.2 mycroft * otherwise try to unlink the node and try again. if that
460 1.15.2.2 mycroft * fails check the full path and try a final time.
461 1.15.2.2 mycroft */
462 1.15.2.2 mycroft if (res == 0)
463 1.15.2.2 mycroft break;
464 1.15.2.2 mycroft
465 1.15.2.2 mycroft /*
466 1.15.2.2 mycroft * we failed to make the node
467 1.15.2.2 mycroft */
468 1.15.2.2 mycroft oerrno = errno;
469 1.15.2.2 mycroft if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0)
470 1.15.2.2 mycroft return(-1);
471 1.15.2.2 mycroft
472 1.15.2.2 mycroft if (++pass <= 1)
473 1.15.2.2 mycroft continue;
474 1.15.2.2 mycroft
475 1.15.2.2 mycroft if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
476 1.15.2.2 mycroft syswarn(1, oerrno, "Could not create: %s", arcn->name);
477 1.15.2.2 mycroft return(-1);
478 1.15.2.2 mycroft }
479 1.15.2.2 mycroft }
480 1.15.2.2 mycroft
481 1.15.2.2 mycroft /*
482 1.15.2.2 mycroft * we were able to create the node. set uid/gid, modes and times
483 1.15.2.2 mycroft */
484 1.15.2.2 mycroft if (pids)
485 1.15.2.2 mycroft res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
486 1.15.2.2 mycroft else
487 1.15.2.2 mycroft res = 0;
488 1.15.2.2 mycroft
489 1.15.2.2 mycroft /*
490 1.15.2.2 mycroft * IMPORTANT SECURITY NOTE:
491 1.15.2.2 mycroft * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
492 1.15.2.2 mycroft * set uid/gid bits
493 1.15.2.2 mycroft */
494 1.15.2.2 mycroft if (!pmode || res)
495 1.15.2.2 mycroft arcn->sb.st_mode &= ~(SETBITS);
496 1.15.2.2 mycroft if (pmode)
497 1.15.2.2 mycroft set_pmode(arcn->name, arcn->sb.st_mode);
498 1.15.2.2 mycroft
499 1.15.2.2 mycroft if (arcn->type == PAX_DIR) {
500 1.15.2.2 mycroft /*
501 1.15.2.2 mycroft * Dirs must be processed again at end of extract to set times
502 1.15.2.2 mycroft * and modes to agree with those stored in the archive. However
503 1.15.2.2 mycroft * to allow extract to continue, we may have to also set owner
504 1.15.2.2 mycroft * rights. This allows nodes in the archive that are children
505 1.15.2.2 mycroft * of this directory to be extracted without failure. Both time
506 1.15.2.2 mycroft * and modes will be fixed after the entire archive is read and
507 1.15.2.2 mycroft * before pax exits.
508 1.15.2.2 mycroft */
509 1.15.2.2 mycroft if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
510 1.15.2.2 mycroft if (lstat(arcn->name, &sb) < 0) {
511 1.15.2.2 mycroft syswarn(0, errno,"Could not access %s (stat)",
512 1.15.2.2 mycroft arcn->name);
513 1.15.2.2 mycroft set_pmode(arcn->name,file_mode | S_IRWXU);
514 1.15.2.2 mycroft } else {
515 1.15.2.2 mycroft /*
516 1.15.2.2 mycroft * We have to add rights to the dir, so we make
517 1.15.2.2 mycroft * sure to restore the mode. The mode must be
518 1.15.2.2 mycroft * restored AS CREATED and not as stored if
519 1.15.2.2 mycroft * pmode is not set.
520 1.15.2.2 mycroft */
521 1.15.2.2 mycroft set_pmode(arcn->name,
522 1.15.2.2 mycroft ((sb.st_mode & FILEBITS) | S_IRWXU));
523 1.15.2.2 mycroft if (!pmode)
524 1.15.2.2 mycroft arcn->sb.st_mode = sb.st_mode;
525 1.15.2.2 mycroft }
526 1.15.2.2 mycroft
527 1.15.2.2 mycroft /*
528 1.15.2.2 mycroft * we have to force the mode to what was set here,
529 1.15.2.2 mycroft * since we changed it from the default as created.
530 1.15.2.2 mycroft */
531 1.15.2.2 mycroft add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
532 1.15.2.2 mycroft } else if (pmode || patime || pmtime)
533 1.15.2.2 mycroft add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
534 1.15.2.2 mycroft }
535 1.15.2.2 mycroft
536 1.15.2.2 mycroft if (patime || pmtime)
537 1.15.2.2 mycroft set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
538 1.15.2.2 mycroft if (pfflags && arcn->type != PAX_SLK)
539 1.15.2.2 mycroft set_chflags(arcn->name, arcn->sb.st_flags);
540 1.15.2.2 mycroft return(0);
541 1.15.2.2 mycroft }
542 1.15.2.2 mycroft
543 1.15.2.2 mycroft /*
544 1.15.2.2 mycroft * unlnk_exist()
545 1.15.2.2 mycroft * Remove node from file system with the specified name. We pass the type
546 1.15.2.2 mycroft * of the node that is going to replace it. When we try to create a
547 1.15.2.2 mycroft * directory and find that it already exists, we allow processing to
548 1.15.2.2 mycroft * continue as proper modes etc will always be set for it later on.
549 1.15.2.2 mycroft * Return:
550 1.15.2.2 mycroft * 0 is ok to proceed, no file with the specified name exists
551 1.15.2.2 mycroft * -1 we were unable to remove the node, or we should not remove it (-k)
552 1.15.2.2 mycroft * 1 we found a directory and we were going to create a directory.
553 1.15.2.2 mycroft */
554 1.15.2.2 mycroft
555 1.15.2.2 mycroft #if __STDC__
556 1.15.2.2 mycroft int
557 1.15.2.2 mycroft unlnk_exist(char *name, int type)
558 1.15.2.2 mycroft #else
559 1.15.2.2 mycroft int
560 1.15.2.2 mycroft unlnk_exist(name, type)
561 1.15.2.2 mycroft char *name;
562 1.15.2.2 mycroft int type;
563 1.15.2.2 mycroft #endif
564 1.15.2.2 mycroft {
565 1.15.2.2 mycroft struct stat sb;
566 1.15.2.2 mycroft
567 1.15.2.2 mycroft /*
568 1.15.2.2 mycroft * the file does not exist, or -k we are done
569 1.15.2.2 mycroft */
570 1.15.2.2 mycroft if (lstat(name, &sb) < 0)
571 1.15.2.2 mycroft return(0);
572 1.15.2.2 mycroft if (kflag)
573 1.15.2.2 mycroft return(-1);
574 1.15.2.2 mycroft
575 1.15.2.2 mycroft if (S_ISDIR(sb.st_mode)) {
576 1.15.2.2 mycroft /*
577 1.15.2.2 mycroft * try to remove a directory, if it fails and we were going to
578 1.15.2.2 mycroft * create a directory anyway, tell the caller (return a 1)
579 1.15.2.2 mycroft */
580 1.15.2.2 mycroft if (rmdir(name) < 0) {
581 1.15.2.2 mycroft if (type == PAX_DIR)
582 1.15.2.2 mycroft return(1);
583 1.15.2.2 mycroft syswarn(1,errno,"Unable to remove directory %s", name);
584 1.15.2.2 mycroft return(-1);
585 1.15.2.2 mycroft }
586 1.15.2.2 mycroft return(0);
587 1.15.2.2 mycroft }
588 1.15.2.2 mycroft
589 1.15.2.2 mycroft /*
590 1.15.2.2 mycroft * try to get rid of all non-directory type nodes
591 1.15.2.2 mycroft */
592 1.15.2.2 mycroft if (unlink(name) < 0) {
593 1.15.2.2 mycroft syswarn(1, errno, "Could not unlink %s", name);
594 1.15.2.2 mycroft return(-1);
595 1.15.2.2 mycroft }
596 1.15.2.2 mycroft return(0);
597 1.15.2.2 mycroft }
598 1.15.2.2 mycroft
599 1.15.2.2 mycroft /*
600 1.15.2.2 mycroft * chk_path()
601 1.15.2.2 mycroft * We were trying to create some kind of node in the file system and it
602 1.15.2.2 mycroft * failed. chk_path() makes sure the path up to the node exists and is
603 1.15.2.2 mycroft * writeable. When we have to create a directory that is missing along the
604 1.15.2.2 mycroft * path somewhere, the directory we create will be set to the same
605 1.15.2.2 mycroft * uid/gid as the file has (when uid and gid are being preserved).
606 1.15.2.2 mycroft * NOTE: this routine is a real performance loss. It is only used as a
607 1.15.2.2 mycroft * last resort when trying to create entries in the file system.
608 1.15.2.2 mycroft * Return:
609 1.15.2.2 mycroft * -1 when it could find nothing it is allowed to fix.
610 1.15.2.2 mycroft * 0 otherwise
611 1.15.2.2 mycroft */
612 1.15.2.2 mycroft
613 1.15.2.2 mycroft #if __STDC__
614 1.15.2.2 mycroft int
615 1.15.2.2 mycroft chk_path( char *name, uid_t st_uid, gid_t st_gid)
616 1.15.2.2 mycroft #else
617 1.15.2.2 mycroft int
618 1.15.2.2 mycroft chk_path(name, st_uid, st_gid)
619 1.15.2.2 mycroft char *name;
620 1.15.2.2 mycroft uid_t st_uid;
621 1.15.2.2 mycroft gid_t st_gid;
622 1.15.2.2 mycroft #endif
623 1.15.2.2 mycroft {
624 1.15.2.2 mycroft char *spt = name;
625 1.15.2.2 mycroft struct stat sb;
626 1.15.2.2 mycroft int retval = -1;
627 1.15.2.2 mycroft
628 1.15.2.2 mycroft /*
629 1.15.2.2 mycroft * watch out for paths with nodes stored directly in / (e.g. /bozo)
630 1.15.2.2 mycroft */
631 1.15.2.2 mycroft if (*spt == '/')
632 1.15.2.2 mycroft ++spt;
633 1.15.2.2 mycroft
634 1.15.2.2 mycroft for(;;) {
635 1.15.2.2 mycroft /*
636 1.15.2.2 mycroft * work foward from the first / and check each part of the path
637 1.15.2.2 mycroft */
638 1.15.2.2 mycroft spt = strchr(spt, '/');
639 1.15.2.2 mycroft if (spt == NULL)
640 1.15.2.2 mycroft break;
641 1.15.2.2 mycroft *spt = '\0';
642 1.15.2.2 mycroft
643 1.15.2.2 mycroft /*
644 1.15.2.2 mycroft * if it exists we assume it is a directory, it is not within
645 1.15.2.2 mycroft * the spec (at least it seems to read that way) to alter the
646 1.15.2.2 mycroft * file system for nodes NOT EXPLICITLY stored on the archive.
647 1.15.2.2 mycroft * If that assumption is changed, you would test the node here
648 1.15.2.2 mycroft * and figure out how to get rid of it (probably like some
649 1.15.2.2 mycroft * recursive unlink()) or fix up the directory permissions if
650 1.15.2.2 mycroft * required (do an access()).
651 1.15.2.2 mycroft */
652 1.15.2.2 mycroft if (lstat(name, &sb) == 0) {
653 1.15.2.2 mycroft *(spt++) = '/';
654 1.15.2.2 mycroft continue;
655 1.15.2.2 mycroft }
656 1.15.2.2 mycroft
657 1.15.2.2 mycroft /*
658 1.15.2.2 mycroft * the path fails at this point, see if we can create the
659 1.15.2.2 mycroft * needed directory and continue on
660 1.15.2.2 mycroft */
661 1.15.2.2 mycroft if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
662 1.15.2.2 mycroft *spt = '/';
663 1.15.2.2 mycroft retval = -1;
664 1.15.2.2 mycroft break;
665 1.15.2.2 mycroft }
666 1.15.2.2 mycroft
667 1.15.2.2 mycroft /*
668 1.15.2.2 mycroft * we were able to create the directory. We will tell the
669 1.15.2.2 mycroft * caller that we found something to fix, and it is ok to try
670 1.15.2.2 mycroft * and create the node again.
671 1.15.2.2 mycroft */
672 1.15.2.2 mycroft retval = 0;
673 1.15.2.2 mycroft if (pids)
674 1.15.2.2 mycroft (void)set_ids(name, st_uid, st_gid);
675 1.15.2.2 mycroft
676 1.15.2.2 mycroft /*
677 1.15.2.2 mycroft * make sure the user doen't have some strange umask that
678 1.15.2.2 mycroft * causes this newly created directory to be unusable. We fix
679 1.15.2.2 mycroft * the modes and restore them back to the creation default at
680 1.15.2.2 mycroft * the end of pax
681 1.15.2.2 mycroft */
682 1.15.2.2 mycroft if ((access(name, R_OK | W_OK | X_OK) < 0) &&
683 1.15.2.2 mycroft (lstat(name, &sb) == 0)) {
684 1.15.2.2 mycroft set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
685 1.15.2.2 mycroft add_dir(name, spt - name, &sb, 1);
686 1.15.2.2 mycroft }
687 1.15.2.2 mycroft *(spt++) = '/';
688 1.15.2.2 mycroft continue;
689 1.15.2.2 mycroft }
690 1.15.2.2 mycroft return(retval);
691 1.15.2.2 mycroft }
692 1.15.2.2 mycroft
693 1.15.2.2 mycroft /*
694 1.15.2.2 mycroft * set_ftime()
695 1.15.2.2 mycroft * Set the access time and modification time for a named file. If frc is
696 1.15.2.2 mycroft * non-zero we force these times to be set even if the the user did not
697 1.15.2.2 mycroft * request access and/or modification time preservation (this is also
698 1.15.2.2 mycroft * used by -t to reset access times).
699 1.15.2.2 mycroft * When ign is zero, only those times the user has asked for are set, the
700 1.15.2.2 mycroft * other ones are left alone. We do not assume the un-documented feature
701 1.15.2.2 mycroft * of many utimes() implementations that consider a 0 time value as a do
702 1.15.2.2 mycroft * not set request.
703 1.15.2.2 mycroft */
704 1.15.2.2 mycroft
705 1.15.2.2 mycroft #if __STDC__
706 1.15.2.2 mycroft void
707 1.15.2.2 mycroft set_ftime(char *fnm, time_t mtime, time_t atime, int frc)
708 1.15.2.2 mycroft #else
709 1.15.2.2 mycroft void
710 1.15.2.2 mycroft set_ftime(fnm, mtime, atime, frc)
711 1.15.2.2 mycroft char *fnm;
712 1.15.2.2 mycroft time_t mtime;
713 1.15.2.2 mycroft time_t atime;
714 1.15.2.2 mycroft int frc;
715 1.15.2.2 mycroft #endif
716 1.15.2.2 mycroft {
717 1.15.2.2 mycroft struct timeval tv[2];
718 1.15.2.2 mycroft struct stat sb;
719 1.15.2.2 mycroft
720 1.15.2.2 mycroft tv[0].tv_sec = (long)atime;
721 1.15.2.2 mycroft tv[0].tv_usec = 0;
722 1.15.2.2 mycroft tv[1].tv_sec = (long)mtime;
723 1.15.2.2 mycroft tv[1].tv_usec = 0;
724 1.15.2.2 mycroft if (!frc && (!patime || !pmtime)) {
725 1.15.2.2 mycroft /*
726 1.15.2.2 mycroft * if we are not forcing, only set those times the user wants
727 1.15.2.2 mycroft * set. We get the current values of the times if we need them.
728 1.15.2.2 mycroft */
729 1.15.2.2 mycroft if (lstat(fnm, &sb) == 0) {
730 1.15.2.2 mycroft if (!patime)
731 1.15.2.2 mycroft TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
732 1.15.2.2 mycroft if (!pmtime)
733 1.15.2.2 mycroft TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
734 1.15.2.2 mycroft } else
735 1.15.2.2 mycroft syswarn(0,errno,"Unable to obtain file stats %s", fnm);
736 1.15.2.2 mycroft }
737 1.15.2.2 mycroft
738 1.15.2.2 mycroft /*
739 1.15.2.2 mycroft * set the times
740 1.15.2.2 mycroft */
741 1.15.2.2 mycroft if (lutimes(fnm, tv) < 0)
742 1.15.2.2 mycroft syswarn(1, errno, "Access/modification time set failed on: %s",
743 1.15.2.2 mycroft fnm);
744 1.15.2.2 mycroft return;
745 1.15.2.2 mycroft }
746 1.15.2.2 mycroft
747 1.15.2.2 mycroft /*
748 1.15.2.2 mycroft * set_ids()
749 1.15.2.2 mycroft * set the uid and gid of a file system node
750 1.15.2.2 mycroft * Return:
751 1.15.2.2 mycroft * 0 when set, -1 on failure
752 1.15.2.2 mycroft */
753 1.15.2.2 mycroft
754 1.15.2.2 mycroft #if __STDC__
755 1.15.2.2 mycroft int
756 1.15.2.2 mycroft set_ids(char *fnm, uid_t uid, gid_t gid)
757 1.15.2.2 mycroft #else
758 1.15.2.2 mycroft int
759 1.15.2.2 mycroft set_ids(fnm, uid, gid)
760 1.15.2.2 mycroft char *fnm;
761 1.15.2.2 mycroft uid_t uid;
762 1.15.2.2 mycroft gid_t gid;
763 1.15.2.2 mycroft #endif
764 1.15.2.2 mycroft {
765 1.15.2.2 mycroft if (lchown(fnm, uid, gid) < 0) {
766 1.15.2.2 mycroft syswarn(1, errno, "Unable to set file uid/gid of %s", fnm);
767 1.15.2.2 mycroft return(-1);
768 1.15.2.2 mycroft }
769 1.15.2.2 mycroft return(0);
770 1.15.2.2 mycroft }
771 1.15.2.2 mycroft
772 1.15.2.2 mycroft /*
773 1.15.2.2 mycroft * set_pmode()
774 1.15.2.2 mycroft * Set file access mode
775 1.15.2.2 mycroft */
776 1.15.2.2 mycroft
777 1.15.2.2 mycroft #if __STDC__
778 1.15.2.2 mycroft void
779 1.15.2.2 mycroft set_pmode(char *fnm, mode_t mode)
780 1.15.2.2 mycroft #else
781 1.15.2.2 mycroft void
782 1.15.2.2 mycroft set_pmode(fnm, mode)
783 1.15.2.2 mycroft char *fnm;
784 1.15.2.2 mycroft mode_t mode;
785 1.15.2.2 mycroft #endif
786 1.15.2.2 mycroft {
787 1.15.2.2 mycroft mode &= ABITS;
788 1.15.2.2 mycroft if (lchmod(fnm, mode) < 0)
789 1.15.2.2 mycroft syswarn(1, errno, "Could not set permissions on %s", fnm);
790 1.15.2.2 mycroft return;
791 1.15.2.2 mycroft }
792 1.15.2.2 mycroft
793 1.15.2.2 mycroft /*
794 1.15.2.2 mycroft * set_chflags()
795 1.15.2.2 mycroft * Set 4.4BSD file flags
796 1.15.2.2 mycroft */
797 1.15.2.2 mycroft #if __STDC__
798 1.15.2.2 mycroft void
799 1.15.2.2 mycroft set_chflags(char *fnm, u_int32_t flags)
800 1.15.2.2 mycroft #else
801 1.15.2.2 mycroft void
802 1.15.2.2 mycroft set_chflags(fnm, flags)
803 1.15.2.2 mycroft char *fnm;
804 1.15.2.2 mycroft u_int32_t flags;
805 1.15.2.2 mycroft #endif
806 1.15.2.2 mycroft {
807 1.15.2.2 mycroft #if 0
808 1.15.2.2 mycroft if (chflags(fnm, flags) < 0)
809 1.15.2.2 mycroft syswarn(1, errno, "Could not set file flags on %s", fnm);
810 1.15.2.2 mycroft #endif
811 1.15.2.2 mycroft return;
812 1.15.2.2 mycroft }
813 1.15.2.2 mycroft
814 1.15.2.2 mycroft /*
815 1.15.2.2 mycroft * file_write()
816 1.15.2.2 mycroft * Write/copy a file (during copy or archive extract). This routine knows
817 1.15.2.2 mycroft * how to copy files with lseek holes in it. (Which are read as file
818 1.15.2.2 mycroft * blocks containing all 0's but do not have any file blocks associated
819 1.15.2.2 mycroft * with the data). Typical examples of these are files created by dbm
820 1.15.2.2 mycroft * variants (.pag files). While the file size of these files are huge, the
821 1.15.2.2 mycroft * actual storage is quite small (the files are sparse). The problem is
822 1.15.2.2 mycroft * the holes read as all zeros so are probably stored on the archive that
823 1.15.2.2 mycroft * way (there is no way to determine if the file block is really a hole,
824 1.15.2.2 mycroft * we only know that a file block of all zero's can be a hole).
825 1.15.2.2 mycroft * At this writing, no major archive format knows how to archive files
826 1.15.2.2 mycroft * with holes. However, on extraction (or during copy, -rw) we have to
827 1.15.2.2 mycroft * deal with these files. Without detecting the holes, the files can
828 1.15.2.2 mycroft * consume a lot of file space if just written to disk. This replacement
829 1.15.2.2 mycroft * for write when passed the basic allocation size of a file system block,
830 1.15.2.2 mycroft * uses lseek whenever it detects the input data is all 0 within that
831 1.15.2.2 mycroft * file block. In more detail, the strategy is as follows:
832 1.15.2.2 mycroft * While the input is all zero keep doing an lseek. Keep track of when we
833 1.15.2.2 mycroft * pass over file block boundries. Only write when we hit a non zero
834 1.15.2.2 mycroft * input. once we have written a file block, we continue to write it to
835 1.15.2.2 mycroft * the end (we stop looking at the input). When we reach the start of the
836 1.15.2.2 mycroft * next file block, start checking for zero blocks again. Working on file
837 1.15.2.2 mycroft * block boundries significantly reduces the overhead when copying files
838 1.15.2.2 mycroft * that are NOT very sparse. This overhead (when compared to a write) is
839 1.15.2.2 mycroft * almost below the measurement resolution on many systems. Without it,
840 1.15.2.2 mycroft * files with holes cannot be safely copied. It does has a side effect as
841 1.15.2.2 mycroft * it can put holes into files that did not have them before, but that is
842 1.15.2.2 mycroft * not a problem since the file contents are unchanged (in fact it saves
843 1.15.2.2 mycroft * file space). (Except on paging files for diskless clients. But since we
844 1.15.2.2 mycroft * cannot determine one of those file from here, we ignore them). If this
845 1.15.2.2 mycroft * ever ends up on a system where CTG files are supported and the holes
846 1.15.2.2 mycroft * are not desired, just do a conditional test in those routines that
847 1.15.2.2 mycroft * call file_write() and have it call write() instead. BEFORE CLOSING THE
848 1.15.2.2 mycroft * FILE, make sure to call file_flush() when the last write finishes with
849 1.15.2.2 mycroft * an empty block. A lot of file systems will not create an lseek hole at
850 1.15.2.2 mycroft * the end. In this case we drop a single 0 at the end to force the
851 1.15.2.2 mycroft * trailing 0's in the file.
852 1.15.2.2 mycroft * ---Parameters---
853 1.15.2.2 mycroft * rem: how many bytes left in this file system block
854 1.15.2.2 mycroft * isempt: have we written to the file block yet (is it empty)
855 1.15.2.2 mycroft * sz: basic file block allocation size
856 1.15.2.2 mycroft * cnt: number of bytes on this write
857 1.15.2.2 mycroft * str: buffer to write
858 1.15.2.2 mycroft * Return:
859 1.15.2.2 mycroft * number of bytes written, -1 on write (or lseek) error.
860 1.15.2.2 mycroft */
861 1.15.2.2 mycroft
862 1.15.2.2 mycroft #if __STDC__
863 1.15.2.2 mycroft int
864 1.15.2.2 mycroft file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
865 1.15.2.2 mycroft char *name)
866 1.15.2.2 mycroft #else
867 1.15.2.2 mycroft int
868 1.15.2.2 mycroft file_write(fd, str, cnt, rem, isempt, sz, name)
869 1.15.2.2 mycroft int fd;
870 1.15.2.2 mycroft char *str;
871 1.15.2.2 mycroft int cnt;
872 1.15.2.2 mycroft int *rem;
873 1.15.2.2 mycroft int *isempt;
874 1.15.2.2 mycroft int sz;
875 1.15.2.2 mycroft char *name;
876 1.15.2.2 mycroft #endif
877 1.15.2.2 mycroft {
878 1.15.2.2 mycroft char *pt;
879 1.15.2.2 mycroft char *end;
880 1.15.2.2 mycroft int wcnt;
881 1.15.2.2 mycroft char *st = str;
882 1.15.2.2 mycroft
883 1.15.2.2 mycroft /*
884 1.15.2.2 mycroft * while we have data to process
885 1.15.2.2 mycroft */
886 1.15.2.2 mycroft while (cnt) {
887 1.15.2.2 mycroft if (!*rem) {
888 1.15.2.2 mycroft /*
889 1.15.2.2 mycroft * We are now at the start of file system block again
890 1.15.2.2 mycroft * (or what we think one is...). start looking for
891 1.15.2.2 mycroft * empty blocks again
892 1.15.2.2 mycroft */
893 1.15.2.2 mycroft *isempt = 1;
894 1.15.2.2 mycroft *rem = sz;
895 1.15.2.2 mycroft }
896 1.15.2.2 mycroft
897 1.15.2.2 mycroft /*
898 1.15.2.2 mycroft * only examine up to the end of the current file block or
899 1.15.2.2 mycroft * remaining characters to write, whatever is smaller
900 1.15.2.2 mycroft */
901 1.15.2.2 mycroft wcnt = MIN(cnt, *rem);
902 1.15.2.2 mycroft cnt -= wcnt;
903 1.15.2.2 mycroft *rem -= wcnt;
904 1.15.2.2 mycroft if (*isempt) {
905 1.15.2.2 mycroft /*
906 1.15.2.2 mycroft * have not written to this block yet, so we keep
907 1.15.2.2 mycroft * looking for zero's
908 1.15.2.2 mycroft */
909 1.15.2.2 mycroft pt = st;
910 1.15.2.2 mycroft end = st + wcnt;
911 1.15.2.2 mycroft
912 1.15.2.2 mycroft /*
913 1.15.2.2 mycroft * look for a zero filled buffer
914 1.15.2.2 mycroft */
915 1.15.2.2 mycroft while ((pt < end) && (*pt == '\0'))
916 1.15.2.2 mycroft ++pt;
917 1.15.2.2 mycroft
918 1.15.2.2 mycroft if (pt == end) {
919 1.15.2.2 mycroft /*
920 1.15.2.2 mycroft * skip, buf is empty so far
921 1.15.2.2 mycroft */
922 1.15.2.2 mycroft if (fd > -1 &&
923 1.15.2.2 mycroft lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
924 1.15.2.2 mycroft syswarn(1,errno,"File seek on %s",
925 1.15.2.2 mycroft name);
926 1.15.2.2 mycroft return(-1);
927 1.15.2.2 mycroft }
928 1.15.2.2 mycroft st = pt;
929 1.15.2.2 mycroft continue;
930 1.15.2.2 mycroft }
931 1.15.2.2 mycroft /*
932 1.15.2.2 mycroft * drat, the buf is not zero filled
933 1.15.2.2 mycroft */
934 1.15.2.2 mycroft *isempt = 0;
935 1.15.2.2 mycroft }
936 1.15.2.2 mycroft
937 1.15.2.2 mycroft /*
938 1.15.2.2 mycroft * have non-zero data in this file system block, have to write
939 1.15.2.2 mycroft */
940 1.15.2.2 mycroft if (fd == -1) {
941 1.15.2.2 mycroft /* GNU hack */
942 1.15.2.2 mycroft if (gnu_hack_string)
943 1.15.2.2 mycroft err(1, "WARNING! Major Internal Error! GNU hack Failing!");
944 1.15.2.2 mycroft gnu_hack_string = malloc(wcnt + 1);
945 1.15.2.2 mycroft if (gnu_hack_string == NULL) {
946 1.15.2.2 mycroft tty_warn(1, "Out of memory");
947 1.15.2.2 mycroft return(-1);
948 1.15.2.2 mycroft }
949 1.15.2.2 mycroft strncpy(gnu_hack_string, st, wcnt);
950 1.15.2.2 mycroft gnu_hack_string[wcnt] = 0;
951 1.15.2.2 mycroft } else if (write(fd, st, wcnt) != wcnt) {
952 1.15.2.2 mycroft syswarn(1, errno, "Failed write to file %s", name);
953 1.15.2.2 mycroft return(-1);
954 1.15.2.2 mycroft }
955 1.15.2.2 mycroft st += wcnt;
956 1.15.2.2 mycroft }
957 1.15.2.2 mycroft return(st - str);
958 1.15.2.2 mycroft }
959 1.15.2.2 mycroft
960 1.15.2.2 mycroft /*
961 1.15.2.2 mycroft * file_flush()
962 1.15.2.2 mycroft * when the last file block in a file is zero, many file systems will not
963 1.15.2.2 mycroft * let us create a hole at the end. To get the last block with zeros, we
964 1.15.2.2 mycroft * write the last BYTE with a zero (back up one byte and write a zero).
965 1.15.2.2 mycroft */
966 1.15.2.2 mycroft
967 1.15.2.2 mycroft #if __STDC__
968 1.15.2.2 mycroft void
969 1.15.2.2 mycroft file_flush(int fd, char *fname, int isempt)
970 1.15.2.2 mycroft #else
971 1.15.2.2 mycroft void
972 1.15.2.2 mycroft file_flush(fd, fname, isempt)
973 1.15.2.2 mycroft int fd;
974 1.15.2.2 mycroft char *fname;
975 1.15.2.2 mycroft int isempt;
976 1.15.2.2 mycroft #endif
977 1.15.2.2 mycroft {
978 1.15.2.2 mycroft static char blnk[] = "\0";
979 1.15.2.2 mycroft
980 1.15.2.2 mycroft /*
981 1.15.2.2 mycroft * silly test, but make sure we are only called when the last block is
982 1.15.2.2 mycroft * filled with all zeros.
983 1.15.2.2 mycroft */
984 1.15.2.2 mycroft if (!isempt)
985 1.15.2.2 mycroft return;
986 1.15.2.2 mycroft
987 1.15.2.2 mycroft /*
988 1.15.2.2 mycroft * move back one byte and write a zero
989 1.15.2.2 mycroft */
990 1.15.2.2 mycroft if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
991 1.15.2.2 mycroft syswarn(1, errno, "Failed seek on file %s", fname);
992 1.15.2.2 mycroft return;
993 1.15.2.2 mycroft }
994 1.15.2.2 mycroft
995 1.15.2.2 mycroft if (write(fd, blnk, 1) < 0)
996 1.15.2.2 mycroft syswarn(1, errno, "Failed write to file %s", fname);
997 1.15.2.2 mycroft return;
998 1.15.2.2 mycroft }
999 1.15.2.2 mycroft
1000 1.15.2.2 mycroft /*
1001 1.15.2.2 mycroft * rdfile_close()
1002 1.15.2.2 mycroft * close a file we have been reading (to copy or archive). If we have to
1003 1.15.2.2 mycroft * reset access time (tflag) do so (the times are stored in arcn).
1004 1.15.2.2 mycroft */
1005 1.15.2.2 mycroft
1006 1.15.2.2 mycroft #if __STDC__
1007 1.15.2.2 mycroft void
1008 1.15.2.2 mycroft rdfile_close(ARCHD *arcn, int *fd)
1009 1.15.2.2 mycroft #else
1010 1.15.2.2 mycroft void
1011 1.15.2.2 mycroft rdfile_close(arcn, fd)
1012 1.15.2.2 mycroft ARCHD *arcn;
1013 1.15.2.2 mycroft int *fd;
1014 1.15.2.2 mycroft #endif
1015 1.15.2.2 mycroft {
1016 1.15.2.2 mycroft /*
1017 1.15.2.2 mycroft * make sure the file is open
1018 1.15.2.2 mycroft */
1019 1.15.2.2 mycroft if (*fd < 0)
1020 1.15.2.2 mycroft return;
1021 1.15.2.2 mycroft
1022 1.15.2.2 mycroft (void)close(*fd);
1023 1.15.2.2 mycroft *fd = -1;
1024 1.15.2.2 mycroft if (!tflag)
1025 1.15.2.2 mycroft return;
1026 1.15.2.2 mycroft
1027 1.15.2.2 mycroft /*
1028 1.15.2.2 mycroft * user wants last access time reset
1029 1.15.2.2 mycroft */
1030 1.15.2.2 mycroft set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
1031 1.15.2.2 mycroft return;
1032 1.15.2.2 mycroft }
1033 1.15.2.2 mycroft
1034 1.15.2.2 mycroft /*
1035 1.15.2.2 mycroft * set_crc()
1036 1.15.2.2 mycroft * read a file to calculate its crc. This is a real drag. Archive formats
1037 1.15.2.2 mycroft * that have this, end up reading the file twice (we have to write the
1038 1.15.2.2 mycroft * header WITH the crc before writing the file contents. Oh well...
1039 1.15.2.2 mycroft * Return:
1040 1.15.2.2 mycroft * 0 if was able to calculate the crc, -1 otherwise
1041 1.15.2.2 mycroft */
1042 1.15.2.2 mycroft
1043 1.15.2.2 mycroft #if __STDC__
1044 1.15.2.2 mycroft int
1045 1.15.2.2 mycroft set_crc(ARCHD *arcn, int fd)
1046 1.15.2.2 mycroft #else
1047 1.15.2.2 mycroft int
1048 1.15.2.2 mycroft set_crc(arcn, fd)
1049 1.15.2.2 mycroft ARCHD *arcn;
1050 1.15.2.2 mycroft int fd;
1051 1.15.2.2 mycroft #endif
1052 1.15.2.2 mycroft {
1053 1.15.2.2 mycroft int i;
1054 1.15.2.2 mycroft int res;
1055 1.15.2.2 mycroft off_t cpcnt = 0L;
1056 1.15.2.2 mycroft u_long size;
1057 1.15.2.2 mycroft unsigned long crc = 0L;
1058 1.15.2.2 mycroft char tbuf[FILEBLK];
1059 1.15.2.2 mycroft struct stat sb;
1060 1.15.2.2 mycroft
1061 1.15.2.2 mycroft if (fd < 0) {
1062 1.15.2.2 mycroft /*
1063 1.15.2.2 mycroft * hmm, no fd, should never happen. well no crc then.
1064 1.15.2.2 mycroft */
1065 1.15.2.2 mycroft arcn->crc = 0L;
1066 1.15.2.2 mycroft return(0);
1067 1.15.2.2 mycroft }
1068 1.15.2.2 mycroft
1069 1.15.2.2 mycroft if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
1070 1.15.2.2 mycroft size = (u_long)sizeof(tbuf);
1071 1.15.2.2 mycroft
1072 1.15.2.2 mycroft /*
1073 1.15.2.2 mycroft * read all the bytes we think that there are in the file. If the user
1074 1.15.2.2 mycroft * is trying to archive an active file, forget this file.
1075 1.15.2.2 mycroft */
1076 1.15.2.2 mycroft for(;;) {
1077 1.15.2.2 mycroft if ((res = read(fd, tbuf, size)) <= 0)
1078 1.15.2.2 mycroft break;
1079 1.15.2.2 mycroft cpcnt += res;
1080 1.15.2.2 mycroft for (i = 0; i < res; ++i)
1081 1.15.2.2 mycroft crc += (tbuf[i] & 0xff);
1082 1.15.2.2 mycroft }
1083 1.15.2.2 mycroft
1084 1.15.2.2 mycroft /*
1085 1.15.2.2 mycroft * safety check. we want to avoid archiving files that are active as
1086 1.15.2.2 mycroft * they can create inconsistant archive copies.
1087 1.15.2.2 mycroft */
1088 1.15.2.2 mycroft if (cpcnt != arcn->sb.st_size)
1089 1.15.2.2 mycroft tty_warn(1, "File changed size %s", arcn->org_name);
1090 1.15.2.2 mycroft else if (fstat(fd, &sb) < 0)
1091 1.15.2.2 mycroft syswarn(1, errno, "Failed stat on %s", arcn->org_name);
1092 1.15.2.2 mycroft else if (arcn->sb.st_mtime != sb.st_mtime)
1093 1.15.2.2 mycroft tty_warn(1, "File %s was modified during read", arcn->org_name);
1094 1.15.2.2 mycroft else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
1095 1.15.2.2 mycroft syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
1096 1.15.2.2 mycroft else {
1097 1.15.2.2 mycroft arcn->crc = crc;
1098 1.15.2.2 mycroft return(0);
1099 1.15.2.2 mycroft }
1100 1.15.2.2 mycroft return(-1);
1101 1.15.2.2 mycroft }
1102