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