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