ar_subs.c revision 1.55 1 1.55 lukem /* $NetBSD: ar_subs.c,v 1.55 2009/02/14 08:10:06 lukem Exp $ */
2 1.5 cgd
3 1.1 jtc /*-
4 1.28 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.27 agc * 3. Neither the name of the University nor the names of its contributors
20 1.27 agc * may be used to endorse or promote products derived from this software
21 1.27 agc * without specific prior written permission.
22 1.27 agc *
23 1.27 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.27 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.27 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.27 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.27 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.27 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.27 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.27 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.27 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.27 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.27 agc * SUCH DAMAGE.
34 1.27 agc */
35 1.27 agc
36 1.29 lukem #if HAVE_NBTOOL_CONFIG_H
37 1.29 lukem #include "nbtool_config.h"
38 1.29 lukem #endif
39 1.29 lukem
40 1.7 christos #include <sys/cdefs.h>
41 1.29 lukem #if !defined(lint)
42 1.5 cgd #if 0
43 1.5 cgd static char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
44 1.5 cgd #else
45 1.55 lukem __RCSID("$NetBSD: ar_subs.c,v 1.55 2009/02/14 08:10:06 lukem Exp $");
46 1.5 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 <sys/param.h>
53 1.1 jtc #include <signal.h>
54 1.1 jtc #include <string.h>
55 1.1 jtc #include <stdio.h>
56 1.1 jtc #include <ctype.h>
57 1.1 jtc #include <fcntl.h>
58 1.1 jtc #include <errno.h>
59 1.8 kleink #include <time.h>
60 1.1 jtc #include <unistd.h>
61 1.1 jtc #include <stdlib.h>
62 1.1 jtc #include "pax.h"
63 1.54 christos #include "pat_rep.h"
64 1.1 jtc #include "extern.h"
65 1.1 jtc
66 1.38 christos static int path_check(ARCHD *, int);
67 1.49 dsl static int wr_archive(ARCHD *, int is_app);
68 1.15 lukem static int get_arc(void);
69 1.15 lukem static int next_head(ARCHD *);
70 1.42 christos #if !HAVE_NBTOOL_CONFIG_H
71 1.39 christos static int fdochroot(int);
72 1.42 christos #endif
73 1.1 jtc extern sigset_t s_mask;
74 1.1 jtc
75 1.1 jtc /*
76 1.1 jtc * Routines which control the overall operation modes of pax as specified by
77 1.1 jtc * the user: list, append, read ...
78 1.1 jtc */
79 1.1 jtc
80 1.14 itohy static char hdbuf[BLKMULT]; /* space for archive header on read */
81 1.1 jtc u_long flcnt; /* number of files processed */
82 1.11 christos ARCHD archd;
83 1.1 jtc
84 1.37 christos static char cwdpath[MAXPATHLEN]; /* current working directory path */
85 1.37 christos static size_t cwdpathlen; /* current working directory path len */
86 1.37 christos
87 1.37 christos int
88 1.37 christos updatepath(void)
89 1.37 christos {
90 1.37 christos if (getcwd(cwdpath, sizeof(cwdpath)) == NULL) {
91 1.37 christos syswarn(1, errno, "Cannot get working directory");
92 1.37 christos return -1;
93 1.37 christos }
94 1.37 christos cwdpathlen = strlen(cwdpath);
95 1.37 christos return 0;
96 1.37 christos }
97 1.37 christos
98 1.37 christos int
99 1.39 christos fdochdir(int fcwd)
100 1.37 christos {
101 1.39 christos if (fchdir(fcwd) == -1) {
102 1.37 christos syswarn(1, errno, "Cannot chdir to `.'");
103 1.37 christos return -1;
104 1.37 christos }
105 1.37 christos return updatepath();
106 1.37 christos }
107 1.37 christos
108 1.37 christos int
109 1.37 christos dochdir(const char *name)
110 1.37 christos {
111 1.37 christos if (chdir(name) == -1)
112 1.37 christos syswarn(1, errno, "Cannot chdir to `%s'", name);
113 1.37 christos return updatepath();
114 1.37 christos }
115 1.37 christos
116 1.40 jmc #if !HAVE_NBTOOL_CONFIG_H
117 1.37 christos static int
118 1.39 christos fdochroot(int fcwd)
119 1.39 christos {
120 1.39 christos if (fchroot(fcwd) != 0) {
121 1.39 christos syswarn(1, errno, "Can't fchroot to \".\"");
122 1.39 christos return -1;
123 1.39 christos }
124 1.39 christos return updatepath();
125 1.39 christos }
126 1.40 jmc #endif
127 1.39 christos
128 1.43 christos /*
129 1.43 christos * mkdir(), but if we failed, check if someone else made it for us
130 1.43 christos * already and don't error out.
131 1.43 christos */
132 1.43 christos int
133 1.43 christos domkdir(const char *fname, mode_t mode)
134 1.43 christos {
135 1.43 christos int error;
136 1.43 christos struct stat sb;
137 1.43 christos
138 1.44 christos if ((error = mkdir(fname, mode)) != -1)
139 1.43 christos return error;
140 1.43 christos
141 1.44 christos switch (errno) {
142 1.44 christos case EISDIR:
143 1.43 christos return 0;
144 1.44 christos case EEXIST:
145 1.46 christos case EACCES:
146 1.47 christos case ENOSYS: /* Grr Solaris */
147 1.52 rillig case EROFS:
148 1.44 christos error = errno;
149 1.44 christos if (stat(fname, &sb) != -1 && S_ISDIR(sb.st_mode))
150 1.44 christos return 0;
151 1.44 christos errno = error;
152 1.44 christos /*FALLTHROUGH*/
153 1.44 christos default:
154 1.44 christos return -1;
155 1.44 christos }
156 1.43 christos }
157 1.43 christos
158 1.39 christos static int
159 1.38 christos path_check(ARCHD *arcn, int level)
160 1.37 christos {
161 1.37 christos char buf[MAXPATHLEN];
162 1.41 christos char *p;
163 1.41 christos
164 1.41 christos if ((p = strrchr(arcn->name, '/')) == NULL)
165 1.41 christos return 0;
166 1.41 christos *p = '\0';
167 1.37 christos
168 1.37 christos if (realpath(arcn->name, buf) == NULL) {
169 1.38 christos int error;
170 1.38 christos error = path_check(arcn, level + 1);
171 1.38 christos *p = '/';
172 1.38 christos if (error == 0)
173 1.38 christos return 0;
174 1.38 christos if (level == 0)
175 1.38 christos syswarn(1, 0, "Cannot resolve `%s'", arcn->name);
176 1.37 christos return -1;
177 1.37 christos }
178 1.37 christos if (strncmp(buf, cwdpath, cwdpathlen) != 0) {
179 1.41 christos *p = '/';
180 1.41 christos syswarn(1, 0, "Attempt to write file `%s' that resolves into "
181 1.41 christos "`%s/%s' outside current working directory `%s' ignored",
182 1.41 christos arcn->name, buf, p + 1, cwdpath);
183 1.37 christos return -1;
184 1.37 christos }
185 1.41 christos *p = '/';
186 1.37 christos return 0;
187 1.37 christos }
188 1.37 christos
189 1.1 jtc /*
190 1.1 jtc * list()
191 1.1 jtc * list the contents of an archive which match user supplied pattern(s)
192 1.22 wiz * (if no pattern is supplied, list entire contents).
193 1.1 jtc */
194 1.1 jtc
195 1.49 dsl int
196 1.1 jtc list(void)
197 1.1 jtc {
198 1.6 tls ARCHD *arcn;
199 1.6 tls int res;
200 1.1 jtc time_t now;
201 1.1 jtc
202 1.1 jtc arcn = &archd;
203 1.1 jtc /*
204 1.1 jtc * figure out archive type; pass any format specific options to the
205 1.1 jtc * archive option processing routine; call the format init routine. We
206 1.1 jtc * also save current time for ls_list() so we do not make a system
207 1.1 jtc * call for each file we need to print. If verbose (vflag) start up
208 1.1 jtc * the name and group caches.
209 1.1 jtc */
210 1.1 jtc if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
211 1.1 jtc ((*frmt->st_rd)() < 0))
212 1.49 dsl return 1;
213 1.1 jtc
214 1.1 jtc now = time((time_t *)NULL);
215 1.1 jtc
216 1.1 jtc /*
217 1.1 jtc * step through the archive until the format says it is done
218 1.1 jtc */
219 1.1 jtc while (next_head(arcn) == 0) {
220 1.18 christos if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
221 1.18 christos /*
222 1.18 christos * we need to read, to get the real filename
223 1.18 christos */
224 1.18 christos off_t cnt;
225 1.36 christos if (!(*frmt->rd_data)(arcn, -arcn->type, &cnt))
226 1.18 christos (void)rd_skip(cnt + arcn->pad);
227 1.18 christos continue;
228 1.18 christos }
229 1.18 christos
230 1.1 jtc /*
231 1.1 jtc * check for pattern, and user specified options match.
232 1.1 jtc * When all patterns are matched we are done.
233 1.1 jtc */
234 1.1 jtc if ((res = pat_match(arcn)) < 0)
235 1.1 jtc break;
236 1.1 jtc
237 1.1 jtc if ((res == 0) && (sel_chk(arcn) == 0)) {
238 1.1 jtc /*
239 1.1 jtc * pattern resulted in a selected file
240 1.1 jtc */
241 1.1 jtc if (pat_sel(arcn) < 0)
242 1.1 jtc break;
243 1.1 jtc
244 1.1 jtc /*
245 1.1 jtc * modify the name as requested by the user if name
246 1.1 jtc * survives modification, do a listing of the file
247 1.1 jtc */
248 1.54 christos if ((res = mod_name(arcn, RENM)) < 0)
249 1.1 jtc break;
250 1.35 jmc if (res == 0) {
251 1.35 jmc if (arcn->name[0] == '/' && !check_Aflag()) {
252 1.35 jmc memmove(arcn->name, arcn->name + 1,
253 1.35 jmc strlen(arcn->name));
254 1.35 jmc }
255 1.17 christos ls_list(arcn, now, stdout);
256 1.35 jmc }
257 1.32 christos /*
258 1.32 christos * if there's an error writing to stdout then we must
259 1.32 christos * stop now -- we're probably writing to a pipe that
260 1.32 christos * has been closed by the reader.
261 1.32 christos */
262 1.32 christos if (ferror(stdout)) {
263 1.32 christos syswarn(1, errno, "Listing incomplete.");
264 1.32 christos break;
265 1.32 christos }
266 1.1 jtc }
267 1.1 jtc /*
268 1.1 jtc * skip to next archive format header using values calculated
269 1.1 jtc * by the format header read routine
270 1.1 jtc */
271 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
272 1.1 jtc break;
273 1.1 jtc }
274 1.1 jtc
275 1.1 jtc /*
276 1.1 jtc * all done, let format have a chance to cleanup, and make sure that
277 1.1 jtc * the patterns supplied by the user were all matched
278 1.1 jtc */
279 1.1 jtc (void)(*frmt->end_rd)();
280 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
281 1.1 jtc ar_close();
282 1.1 jtc pat_chk();
283 1.49 dsl
284 1.49 dsl return 0;
285 1.1 jtc }
286 1.1 jtc
287 1.1 jtc /*
288 1.1 jtc * extract()
289 1.1 jtc * extract the member(s) of an archive as specified by user supplied
290 1.1 jtc * pattern(s) (no patterns extracts all members)
291 1.1 jtc */
292 1.1 jtc
293 1.49 dsl int
294 1.1 jtc extract(void)
295 1.1 jtc {
296 1.6 tls ARCHD *arcn;
297 1.6 tls int res;
298 1.1 jtc off_t cnt;
299 1.1 jtc struct stat sb;
300 1.1 jtc int fd;
301 1.17 christos time_t now;
302 1.1 jtc
303 1.1 jtc arcn = &archd;
304 1.1 jtc /*
305 1.1 jtc * figure out archive type; pass any format specific options to the
306 1.1 jtc * archive option processing routine; call the format init routine;
307 1.1 jtc * start up the directory modification time and access mode database
308 1.1 jtc */
309 1.1 jtc if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
310 1.1 jtc ((*frmt->st_rd)() < 0) || (dir_start() < 0))
311 1.49 dsl return 1;
312 1.1 jtc
313 1.17 christos now = time((time_t *)NULL);
314 1.34 jmc #if !HAVE_NBTOOL_CONFIG_H
315 1.39 christos if (do_chroot)
316 1.39 christos (void)fdochroot(cwdfd);
317 1.34 jmc #endif
318 1.17 christos
319 1.1 jtc /*
320 1.1 jtc * When we are doing interactive rename, we store the mapping of names
321 1.1 jtc * so we can fix up hard links files later in the archive.
322 1.1 jtc */
323 1.1 jtc if (iflag && (name_start() < 0))
324 1.49 dsl return 1;
325 1.1 jtc
326 1.1 jtc /*
327 1.1 jtc * step through each entry on the archive until the format read routine
328 1.1 jtc * says it is done
329 1.1 jtc */
330 1.1 jtc while (next_head(arcn) == 0) {
331 1.31 tron int write_to_hard_link = 0;
332 1.31 tron
333 1.18 christos if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
334 1.18 christos /*
335 1.18 christos * we need to read, to get the real filename
336 1.18 christos */
337 1.36 christos if (!(*frmt->rd_data)(arcn, -arcn->type, &cnt))
338 1.18 christos (void)rd_skip(cnt + arcn->pad);
339 1.18 christos continue;
340 1.18 christos }
341 1.1 jtc
342 1.1 jtc /*
343 1.1 jtc * check for pattern, and user specified options match. When
344 1.1 jtc * all the patterns are matched we are done
345 1.1 jtc */
346 1.18 christos if ((res = pat_match(arcn)) < 0)
347 1.18 christos break;
348 1.1 jtc
349 1.18 christos if ((res > 0) || (sel_chk(arcn) != 0)) {
350 1.18 christos /*
351 1.18 christos * file is not selected. skip past any file
352 1.18 christos * data and padding and go back for the next
353 1.18 christos * archive member
354 1.18 christos */
355 1.18 christos (void)rd_skip(arcn->skip + arcn->pad);
356 1.18 christos continue;
357 1.1 jtc }
358 1.1 jtc
359 1.45 christos if (kflag && (lstat(arcn->name, &sb) == 0)) {
360 1.45 christos (void)rd_skip(arcn->skip + arcn->pad);
361 1.45 christos continue;
362 1.45 christos }
363 1.45 christos
364 1.1 jtc /*
365 1.1 jtc * with -u or -D only extract when the archive member is newer
366 1.22 wiz * than the file with the same name in the file system (no
367 1.1 jtc * test of being the same type is required).
368 1.1 jtc * NOTE: this test is done BEFORE name modifications as
369 1.1 jtc * specified by pax. this operation can be confusing to the
370 1.1 jtc * user who might expect the test to be done on an existing
371 1.1 jtc * file AFTER the name mod. In honesty the pax spec is probably
372 1.13 mrg * flawed in this respect. ignore this for GNU long links.
373 1.1 jtc */
374 1.18 christos if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
375 1.1 jtc if (uflag && Dflag) {
376 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
377 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime)) {
378 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
379 1.1 jtc continue;
380 1.1 jtc }
381 1.1 jtc } else if (Dflag) {
382 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime) {
383 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
384 1.1 jtc continue;
385 1.1 jtc }
386 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime) {
387 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
388 1.1 jtc continue;
389 1.1 jtc }
390 1.1 jtc }
391 1.1 jtc
392 1.1 jtc /*
393 1.1 jtc * this archive member is now been selected. modify the name.
394 1.1 jtc */
395 1.54 christos if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn, RENM)) < 0))
396 1.1 jtc break;
397 1.1 jtc if (res > 0) {
398 1.1 jtc /*
399 1.1 jtc * a bad name mod, skip and purge name from link table
400 1.1 jtc */
401 1.1 jtc purg_lnk(arcn);
402 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
403 1.1 jtc continue;
404 1.1 jtc }
405 1.1 jtc
406 1.35 jmc if (arcn->name[0] == '/' && !check_Aflag()) {
407 1.35 jmc memmove(arcn->name, arcn->name + 1, strlen(arcn->name));
408 1.35 jmc }
409 1.1 jtc /*
410 1.14 itohy * Non standard -Y and -Z flag. When the existing file is
411 1.13 mrg * same age or newer skip; ignore this for GNU long links.
412 1.1 jtc */
413 1.18 christos if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
414 1.1 jtc if (Yflag && Zflag) {
415 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
416 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime)) {
417 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
418 1.1 jtc continue;
419 1.1 jtc }
420 1.1 jtc } else if (Yflag) {
421 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime) {
422 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
423 1.1 jtc continue;
424 1.1 jtc }
425 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime) {
426 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
427 1.1 jtc continue;
428 1.1 jtc }
429 1.1 jtc }
430 1.1 jtc
431 1.1 jtc if (vflag) {
432 1.17 christos if (vflag > 1)
433 1.17 christos ls_list(arcn, now, listf);
434 1.17 christos else {
435 1.17 christos (void)safe_print(arcn->name, listf);
436 1.17 christos vfpart = 1;
437 1.17 christos }
438 1.1 jtc }
439 1.1 jtc
440 1.1 jtc /*
441 1.17 christos * if required, chdir around.
442 1.17 christos */
443 1.26 simonb if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL) &&
444 1.26 simonb !to_stdout)
445 1.37 christos dochdir(arcn->pat->chdname);
446 1.37 christos
447 1.38 christos if (secure && path_check(arcn, 0) != 0) {
448 1.37 christos (void)rd_skip(arcn->skip + arcn->pad);
449 1.37 christos continue;
450 1.37 christos }
451 1.37 christos
452 1.37 christos
453 1.17 christos /*
454 1.1 jtc * all ok, extract this member based on type
455 1.1 jtc */
456 1.31 tron if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
457 1.1 jtc /*
458 1.1 jtc * process archive members that are not regular files.
459 1.1 jtc * throw out padding and any data that might follow the
460 1.1 jtc * header (as determined by the format).
461 1.1 jtc */
462 1.31 tron if ((arcn->type == PAX_HLK) ||
463 1.31 tron (arcn->type == PAX_HRG))
464 1.31 tron res = lnk_creat(arcn, &write_to_hard_link);
465 1.1 jtc else
466 1.1 jtc res = node_creat(arcn);
467 1.1 jtc
468 1.31 tron if (!write_to_hard_link) {
469 1.31 tron (void)rd_skip(arcn->skip + arcn->pad);
470 1.31 tron if (res < 0)
471 1.31 tron purg_lnk(arcn);
472 1.1 jtc
473 1.31 tron if (vflag && vfpart) {
474 1.31 tron (void)putc('\n', listf);
475 1.31 tron vfpart = 0;
476 1.31 tron }
477 1.31 tron continue;
478 1.1 jtc }
479 1.1 jtc }
480 1.26 simonb if (to_stdout)
481 1.26 simonb fd = STDOUT_FILENO;
482 1.26 simonb else {
483 1.26 simonb /*
484 1.26 simonb * We have a file with data here. If we cannot create
485 1.26 simonb * it, skip over the data and purge the name from hard
486 1.26 simonb * link table.
487 1.26 simonb */
488 1.31 tron if ((fd = file_creat(arcn, write_to_hard_link)) < 0) {
489 1.26 simonb (void)fflush(listf);
490 1.26 simonb (void)rd_skip(arcn->skip + arcn->pad);
491 1.26 simonb purg_lnk(arcn);
492 1.26 simonb continue;
493 1.26 simonb }
494 1.1 jtc }
495 1.1 jtc /*
496 1.1 jtc * extract the file from the archive and skip over padding and
497 1.1 jtc * any unprocessed data
498 1.1 jtc */
499 1.1 jtc res = (*frmt->rd_data)(arcn, fd, &cnt);
500 1.26 simonb if (!to_stdout)
501 1.26 simonb file_close(arcn, fd);
502 1.1 jtc if (vflag && vfpart) {
503 1.17 christos (void)putc('\n', listf);
504 1.1 jtc vfpart = 0;
505 1.1 jtc }
506 1.1 jtc if (!res)
507 1.1 jtc (void)rd_skip(cnt + arcn->pad);
508 1.17 christos
509 1.17 christos /*
510 1.17 christos * if required, chdir around.
511 1.17 christos */
512 1.17 christos if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
513 1.37 christos fdochdir(cwdfd);
514 1.1 jtc }
515 1.1 jtc
516 1.1 jtc /*
517 1.1 jtc * all done, restore directory modes and times as required; make sure
518 1.1 jtc * all patterns supplied by the user were matched; block off signals
519 1.1 jtc * to avoid chance for multiple entry into the cleanup code.
520 1.1 jtc */
521 1.1 jtc (void)(*frmt->end_rd)();
522 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
523 1.1 jtc ar_close();
524 1.1 jtc proc_dir();
525 1.1 jtc pat_chk();
526 1.49 dsl
527 1.49 dsl return 0;
528 1.1 jtc }
529 1.1 jtc
530 1.1 jtc /*
531 1.1 jtc * wr_archive()
532 1.1 jtc * Write an archive. used in both creating a new archive and appends on
533 1.1 jtc * previously written archive.
534 1.1 jtc */
535 1.1 jtc
536 1.49 dsl static int
537 1.6 tls wr_archive(ARCHD *arcn, int is_app)
538 1.1 jtc {
539 1.6 tls int res;
540 1.6 tls int hlk;
541 1.6 tls int wr_one;
542 1.1 jtc off_t cnt;
543 1.15 lukem int (*wrf)(ARCHD *);
544 1.1 jtc int fd = -1;
545 1.17 christos time_t now;
546 1.1 jtc
547 1.1 jtc /*
548 1.1 jtc * if this format supports hard link storage, start up the database
549 1.1 jtc * that detects them.
550 1.1 jtc */
551 1.1 jtc if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
552 1.49 dsl return 1;
553 1.1 jtc
554 1.1 jtc /*
555 1.1 jtc * start up the file traversal code and format specific write
556 1.1 jtc */
557 1.1 jtc if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
558 1.49 dsl return 1;
559 1.1 jtc wrf = frmt->wr;
560 1.1 jtc
561 1.17 christos now = time((time_t *)NULL);
562 1.17 christos
563 1.1 jtc /*
564 1.1 jtc * When we are doing interactive rename, we store the mapping of names
565 1.1 jtc * so we can fix up hard links files later in the archive.
566 1.1 jtc */
567 1.1 jtc if (iflag && (name_start() < 0))
568 1.49 dsl return 1;
569 1.1 jtc
570 1.1 jtc /*
571 1.22 wiz * if this is not append, and there are no files, we do no write a trailer
572 1.1 jtc */
573 1.1 jtc wr_one = is_app;
574 1.1 jtc
575 1.1 jtc /*
576 1.1 jtc * while there are files to archive, process them one at at time
577 1.1 jtc */
578 1.1 jtc while (next_file(arcn) == 0) {
579 1.1 jtc /*
580 1.1 jtc * check if this file meets user specified options match.
581 1.1 jtc */
582 1.1 jtc if (sel_chk(arcn) != 0)
583 1.1 jtc continue;
584 1.54 christos /*
585 1.54 christos * Here we handle the exclusion -X gnu style patterns which
586 1.54 christos * are implemented like a pattern list. We don't modify the
587 1.54 christos * name as this will be done below again, and we don't want
588 1.54 christos * to double modify it.
589 1.54 christos */
590 1.54 christos if ((res = mod_name(arcn, 0)) < 0)
591 1.51 christos break;
592 1.51 christos if (res == 1)
593 1.51 christos continue;
594 1.1 jtc fd = -1;
595 1.1 jtc if (uflag) {
596 1.1 jtc /*
597 1.1 jtc * only archive if this file is newer than a file with
598 1.1 jtc * the same name that is already stored on the archive
599 1.1 jtc */
600 1.1 jtc if ((res = chk_ftime(arcn)) < 0)
601 1.1 jtc break;
602 1.1 jtc if (res > 0)
603 1.1 jtc continue;
604 1.1 jtc }
605 1.1 jtc
606 1.1 jtc /*
607 1.1 jtc * this file is considered selected now. see if this is a hard
608 1.1 jtc * link to a file already stored
609 1.1 jtc */
610 1.1 jtc ftree_sel(arcn);
611 1.1 jtc if (hlk && (chk_lnk(arcn) < 0))
612 1.1 jtc break;
613 1.1 jtc
614 1.1 jtc if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
615 1.1 jtc (arcn->type == PAX_CTG)) {
616 1.1 jtc /*
617 1.1 jtc * we will have to read this file. by opening it now we
618 1.1 jtc * can avoid writing a header to the archive for a file
619 1.1 jtc * we were later unable to read (we also purge it from
620 1.1 jtc * the link table).
621 1.1 jtc */
622 1.1 jtc if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
623 1.23 grant syswarn(1, errno, "Unable to open %s to read",
624 1.1 jtc arcn->org_name);
625 1.1 jtc purg_lnk(arcn);
626 1.1 jtc continue;
627 1.1 jtc }
628 1.1 jtc }
629 1.1 jtc
630 1.1 jtc /*
631 1.1 jtc * Now modify the name as requested by the user
632 1.1 jtc */
633 1.54 christos if ((res = mod_name(arcn, RENM)) < 0) {
634 1.1 jtc /*
635 1.1 jtc * name modification says to skip this file, close the
636 1.1 jtc * file and purge link table entry
637 1.1 jtc */
638 1.1 jtc rdfile_close(arcn, &fd);
639 1.1 jtc purg_lnk(arcn);
640 1.1 jtc break;
641 1.1 jtc }
642 1.1 jtc
643 1.35 jmc if (arcn->name[0] == '/' && !check_Aflag()) {
644 1.35 jmc memmove(arcn->name, arcn->name + 1, strlen(arcn->name));
645 1.35 jmc }
646 1.35 jmc
647 1.1 jtc if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
648 1.1 jtc /*
649 1.1 jtc * unable to obtain the crc we need, close the file,
650 1.14 itohy * purge link table entry
651 1.1 jtc */
652 1.1 jtc rdfile_close(arcn, &fd);
653 1.1 jtc purg_lnk(arcn);
654 1.1 jtc continue;
655 1.1 jtc }
656 1.1 jtc
657 1.1 jtc if (vflag) {
658 1.17 christos if (vflag > 1)
659 1.17 christos ls_list(arcn, now, listf);
660 1.17 christos else {
661 1.17 christos (void)safe_print(arcn->name, listf);
662 1.17 christos vfpart = 1;
663 1.17 christos }
664 1.1 jtc }
665 1.1 jtc ++flcnt;
666 1.1 jtc
667 1.1 jtc /*
668 1.1 jtc * looks safe to store the file, have the format specific
669 1.1 jtc * routine write routine store the file header on the archive
670 1.1 jtc */
671 1.1 jtc if ((res = (*wrf)(arcn)) < 0) {
672 1.1 jtc rdfile_close(arcn, &fd);
673 1.1 jtc break;
674 1.1 jtc }
675 1.1 jtc wr_one = 1;
676 1.1 jtc if (res > 0) {
677 1.14 itohy /*
678 1.1 jtc * format write says no file data needs to be stored
679 1.1 jtc * so we are done messing with this file
680 1.1 jtc */
681 1.1 jtc if (vflag && vfpart) {
682 1.17 christos (void)putc('\n', listf);
683 1.1 jtc vfpart = 0;
684 1.1 jtc }
685 1.1 jtc rdfile_close(arcn, &fd);
686 1.1 jtc continue;
687 1.1 jtc }
688 1.1 jtc
689 1.1 jtc /*
690 1.1 jtc * Add file data to the archive, quit on write error. if we
691 1.1 jtc * cannot write the entire file contents to the archive we
692 1.1 jtc * must pad the archive to replace the missing file data
693 1.1 jtc * (otherwise during an extract the file header for the file
694 1.1 jtc * which FOLLOWS this one will not be where we expect it to
695 1.1 jtc * be).
696 1.1 jtc */
697 1.1 jtc res = (*frmt->wr_data)(arcn, fd, &cnt);
698 1.1 jtc rdfile_close(arcn, &fd);
699 1.1 jtc if (vflag && vfpart) {
700 1.17 christos (void)putc('\n', listf);
701 1.1 jtc vfpart = 0;
702 1.1 jtc }
703 1.1 jtc if (res < 0)
704 1.1 jtc break;
705 1.1 jtc
706 1.1 jtc /*
707 1.1 jtc * pad as required, cnt is number of bytes not written
708 1.1 jtc */
709 1.1 jtc if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
710 1.1 jtc ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
711 1.1 jtc break;
712 1.1 jtc }
713 1.1 jtc
714 1.1 jtc /*
715 1.14 itohy * tell format to write trailer; pad to block boundary; reset directory
716 1.1 jtc * mode/access times, and check if all patterns supplied by the user
717 1.1 jtc * were matched. block off signals to avoid chance for multiple entry
718 1.1 jtc * into the cleanup code
719 1.1 jtc */
720 1.1 jtc if (wr_one) {
721 1.1 jtc (*frmt->end_wr)();
722 1.1 jtc wr_fin();
723 1.1 jtc }
724 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
725 1.1 jtc ar_close();
726 1.1 jtc if (tflag)
727 1.1 jtc proc_dir();
728 1.1 jtc ftree_chk();
729 1.49 dsl
730 1.49 dsl return 0;
731 1.1 jtc }
732 1.1 jtc
733 1.1 jtc /*
734 1.1 jtc * append()
735 1.1 jtc * Add file to previously written archive. Archive format specified by the
736 1.1 jtc * user must agree with archive. The archive is read first to collect
737 1.1 jtc * modification times (if -u) and locate the archive trailer. The archive
738 1.1 jtc * is positioned in front of the record with the trailer and wr_archive()
739 1.1 jtc * is called to add the new members.
740 1.1 jtc * PAX IMPLEMENTATION DETAIL NOTE:
741 1.1 jtc * -u is implemented by adding the new members to the end of the archive.
742 1.14 itohy * Care is taken so that these do not end up as links to the older
743 1.1 jtc * version of the same file already stored in the archive. It is expected
744 1.1 jtc * when extraction occurs these newer versions will over-write the older
745 1.1 jtc * ones stored "earlier" in the archive (this may be a bad assumption as
746 1.1 jtc * it depends on the implementation of the program doing the extraction).
747 1.1 jtc * It is really difficult to splice in members without either re-writing
748 1.1 jtc * the entire archive (from the point were the old version was), or having
749 1.1 jtc * assistance of the format specification in terms of a special update
750 1.1 jtc * header that invalidates a previous archive record. The posix spec left
751 1.1 jtc * the method used to implement -u unspecified. This pax is able to
752 1.1 jtc * over write existing files that it creates.
753 1.1 jtc */
754 1.1 jtc
755 1.49 dsl int
756 1.1 jtc append(void)
757 1.1 jtc {
758 1.6 tls ARCHD *arcn;
759 1.6 tls int res;
760 1.1 jtc FSUB *orgfrmt;
761 1.1 jtc int udev;
762 1.1 jtc off_t tlen;
763 1.1 jtc
764 1.1 jtc arcn = &archd;
765 1.1 jtc orgfrmt = frmt;
766 1.1 jtc
767 1.1 jtc /*
768 1.1 jtc * Do not allow an append operation if the actual archive is of a
769 1.14 itohy * different format than the user specified format.
770 1.1 jtc */
771 1.1 jtc if (get_arc() < 0)
772 1.49 dsl return 1;
773 1.1 jtc if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
774 1.7 christos tty_warn(1, "Cannot mix current archive format %s with %s",
775 1.1 jtc frmt->name, orgfrmt->name);
776 1.49 dsl return 1;
777 1.1 jtc }
778 1.1 jtc
779 1.1 jtc /*
780 1.1 jtc * pass the format any options and start up format
781 1.1 jtc */
782 1.1 jtc if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
783 1.49 dsl return 1;
784 1.1 jtc
785 1.1 jtc /*
786 1.1 jtc * if we only are adding members that are newer, we need to save the
787 1.1 jtc * mod times for all files we see.
788 1.1 jtc */
789 1.1 jtc if (uflag && (ftime_start() < 0))
790 1.49 dsl return 1;
791 1.1 jtc
792 1.1 jtc /*
793 1.1 jtc * some archive formats encode hard links by recording the device and
794 1.1 jtc * file serial number (inode) but copy the file anyway (multiple times)
795 1.1 jtc * to the archive. When we append, we run the risk that newly added
796 1.1 jtc * files may have the same device and inode numbers as those recorded
797 1.1 jtc * on the archive but during a previous run. If this happens, when the
798 1.1 jtc * archive is extracted we get INCORRECT hard links. We avoid this by
799 1.1 jtc * remapping the device numbers so that newly added files will never
800 1.1 jtc * use the same device number as one found on the archive. remapping
801 1.1 jtc * allows new members to safely have links among themselves. remapping
802 1.1 jtc * also avoids problems with file inode (serial number) truncations
803 1.1 jtc * when the inode number is larger than storage space in the archive
804 1.1 jtc * header. See the remap routines for more details.
805 1.1 jtc */
806 1.1 jtc if ((udev = frmt->udev) && (dev_start() < 0))
807 1.49 dsl return 1;
808 1.1 jtc
809 1.1 jtc /*
810 1.1 jtc * reading the archive may take a long time. If verbose tell the user
811 1.1 jtc */
812 1.53 christos if (vflag || Vflag) {
813 1.17 christos (void)fprintf(listf,
814 1.1 jtc "%s: Reading archive to position at the end...", argv0);
815 1.1 jtc vfpart = 1;
816 1.1 jtc }
817 1.1 jtc
818 1.1 jtc /*
819 1.1 jtc * step through the archive until the format says it is done
820 1.1 jtc */
821 1.1 jtc while (next_head(arcn) == 0) {
822 1.1 jtc /*
823 1.1 jtc * check if this file meets user specified options.
824 1.1 jtc */
825 1.1 jtc if (sel_chk(arcn) != 0) {
826 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
827 1.1 jtc break;
828 1.1 jtc continue;
829 1.1 jtc }
830 1.1 jtc
831 1.1 jtc if (uflag) {
832 1.1 jtc /*
833 1.1 jtc * see if this is the newest version of this file has
834 1.1 jtc * already been seen, if so skip.
835 1.1 jtc */
836 1.1 jtc if ((res = chk_ftime(arcn)) < 0)
837 1.1 jtc break;
838 1.1 jtc if (res > 0) {
839 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
840 1.1 jtc break;
841 1.1 jtc continue;
842 1.1 jtc }
843 1.1 jtc }
844 1.1 jtc
845 1.1 jtc /*
846 1.1 jtc * Store this device number. Device numbers seen during the
847 1.1 jtc * read phase of append will cause newly appended files with a
848 1.1 jtc * device number seen in the old part of the archive to be
849 1.1 jtc * remapped to an unused device number.
850 1.1 jtc */
851 1.1 jtc if ((udev && (add_dev(arcn) < 0)) ||
852 1.1 jtc (rd_skip(arcn->skip + arcn->pad) == 1))
853 1.1 jtc break;
854 1.1 jtc }
855 1.1 jtc
856 1.1 jtc /*
857 1.1 jtc * done, finish up read and get the number of bytes to back up so we
858 1.1 jtc * can add new members. The format might have used the hard link table,
859 1.1 jtc * purge it.
860 1.1 jtc */
861 1.1 jtc tlen = (*frmt->end_rd)();
862 1.1 jtc lnk_end();
863 1.1 jtc
864 1.1 jtc /*
865 1.14 itohy * try to position for write, if this fails quit. if any error occurs,
866 1.1 jtc * we will refuse to write
867 1.1 jtc */
868 1.1 jtc if (appnd_start(tlen) < 0)
869 1.49 dsl return 1;
870 1.1 jtc
871 1.1 jtc /*
872 1.1 jtc * tell the user we are done reading.
873 1.1 jtc */
874 1.53 christos if ((vflag || Vflag) && vfpart) {
875 1.17 christos (void)safe_print("done.\n", listf);
876 1.1 jtc vfpart = 0;
877 1.1 jtc }
878 1.14 itohy
879 1.1 jtc /*
880 1.1 jtc * go to the writing phase to add the new members
881 1.1 jtc */
882 1.50 elad res = wr_archive(arcn, 1);
883 1.50 elad if (res == 1) {
884 1.50 elad /*
885 1.50 elad * wr_archive failed in some way, but before any files were
886 1.50 elad * added. These are the only steps needed to cleanup (and
887 1.50 elad * not truncate the archive).
888 1.50 elad */
889 1.50 elad wr_fin();
890 1.50 elad (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
891 1.50 elad ar_close();
892 1.50 elad }
893 1.50 elad return res;
894 1.1 jtc }
895 1.1 jtc
896 1.1 jtc /*
897 1.1 jtc * archive()
898 1.1 jtc * write a new archive
899 1.1 jtc */
900 1.1 jtc
901 1.49 dsl int
902 1.1 jtc archive(void)
903 1.1 jtc {
904 1.1 jtc
905 1.1 jtc /*
906 1.1 jtc * if we only are adding members that are newer, we need to save the
907 1.1 jtc * mod times for all files; set up for writing; pass the format any
908 1.1 jtc * options write the archive
909 1.1 jtc */
910 1.1 jtc if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
911 1.49 dsl return 1;
912 1.1 jtc if ((*frmt->options)() < 0)
913 1.49 dsl return 1;
914 1.1 jtc
915 1.49 dsl return wr_archive(&archd, 0);
916 1.1 jtc }
917 1.1 jtc
918 1.1 jtc /*
919 1.1 jtc * copy()
920 1.1 jtc * copy files from one part of the file system to another. this does not
921 1.1 jtc * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
922 1.1 jtc * archive was written and then extracted in the destination directory
923 1.1 jtc * (except the files are forced to be under the destination directory).
924 1.1 jtc */
925 1.1 jtc
926 1.49 dsl int
927 1.1 jtc copy(void)
928 1.1 jtc {
929 1.6 tls ARCHD *arcn;
930 1.6 tls int res;
931 1.6 tls int fddest;
932 1.6 tls char *dest_pt;
933 1.55 lukem size_t dlen;
934 1.55 lukem size_t drem;
935 1.1 jtc int fdsrc = -1;
936 1.1 jtc struct stat sb;
937 1.1 jtc char dirbuf[PAXPATHLEN+1];
938 1.1 jtc
939 1.1 jtc arcn = &archd;
940 1.1 jtc /*
941 1.1 jtc * set up the destination dir path and make sure it is a directory. We
942 1.1 jtc * make sure we have a trailing / on the destination
943 1.1 jtc */
944 1.17 christos dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
945 1.17 christos if (dlen >= sizeof(dirbuf) ||
946 1.17 christos (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
947 1.17 christos tty_warn(1, "directory name is too long %s", dirptr);
948 1.49 dsl return 1;
949 1.17 christos }
950 1.1 jtc dest_pt = dirbuf + dlen;
951 1.1 jtc if (*(dest_pt-1) != '/') {
952 1.1 jtc *dest_pt++ = '/';
953 1.1 jtc ++dlen;
954 1.1 jtc }
955 1.1 jtc *dest_pt = '\0';
956 1.1 jtc drem = PAXPATHLEN - dlen;
957 1.1 jtc
958 1.1 jtc if (stat(dirptr, &sb) < 0) {
959 1.1 jtc syswarn(1, errno, "Cannot access destination directory %s",
960 1.1 jtc dirptr);
961 1.49 dsl return 1;
962 1.1 jtc }
963 1.1 jtc if (!S_ISDIR(sb.st_mode)) {
964 1.7 christos tty_warn(1, "Destination is not a directory %s", dirptr);
965 1.49 dsl return 1;
966 1.1 jtc }
967 1.1 jtc
968 1.1 jtc /*
969 1.1 jtc * start up the hard link table; file traversal routines and the
970 1.14 itohy * modification time and access mode database
971 1.1 jtc */
972 1.1 jtc if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
973 1.49 dsl return 1;
974 1.1 jtc
975 1.1 jtc /*
976 1.1 jtc * When we are doing interactive rename, we store the mapping of names
977 1.1 jtc * so we can fix up hard links files later in the archive.
978 1.1 jtc */
979 1.1 jtc if (iflag && (name_start() < 0))
980 1.49 dsl return 1;
981 1.1 jtc
982 1.1 jtc /*
983 1.1 jtc * set up to cp file trees
984 1.1 jtc */
985 1.1 jtc cp_start();
986 1.1 jtc
987 1.1 jtc /*
988 1.1 jtc * while there are files to archive, process them
989 1.1 jtc */
990 1.1 jtc while (next_file(arcn) == 0) {
991 1.1 jtc fdsrc = -1;
992 1.1 jtc
993 1.1 jtc /*
994 1.1 jtc * check if this file meets user specified options
995 1.1 jtc */
996 1.1 jtc if (sel_chk(arcn) != 0)
997 1.1 jtc continue;
998 1.1 jtc
999 1.1 jtc /*
1000 1.1 jtc * if there is already a file in the destination directory with
1001 1.1 jtc * the same name and it is newer, skip the one stored on the
1002 1.1 jtc * archive.
1003 1.1 jtc * NOTE: this test is done BEFORE name modifications as
1004 1.1 jtc * specified by pax. this can be confusing to the user who
1005 1.1 jtc * might expect the test to be done on an existing file AFTER
1006 1.1 jtc * the name mod. In honesty the pax spec is probably flawed in
1007 1.1 jtc * this respect
1008 1.1 jtc */
1009 1.1 jtc if (uflag || Dflag) {
1010 1.1 jtc /*
1011 1.1 jtc * create the destination name
1012 1.1 jtc */
1013 1.17 christos if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
1014 1.17 christos drem + 1) > drem) {
1015 1.7 christos tty_warn(1, "Destination pathname too long %s",
1016 1.1 jtc arcn->name);
1017 1.1 jtc continue;
1018 1.1 jtc }
1019 1.1 jtc
1020 1.1 jtc /*
1021 1.1 jtc * if existing file is same age or newer skip
1022 1.1 jtc */
1023 1.1 jtc res = lstat(dirbuf, &sb);
1024 1.1 jtc *dest_pt = '\0';
1025 1.1 jtc
1026 1.14 itohy if (res == 0) {
1027 1.1 jtc if (uflag && Dflag) {
1028 1.1 jtc if ((arcn->sb.st_mtime<=sb.st_mtime) &&
1029 1.14 itohy (arcn->sb.st_ctime<=sb.st_ctime))
1030 1.1 jtc continue;
1031 1.1 jtc } else if (Dflag) {
1032 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime)
1033 1.1 jtc continue;
1034 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime)
1035 1.1 jtc continue;
1036 1.1 jtc }
1037 1.1 jtc }
1038 1.1 jtc
1039 1.1 jtc /*
1040 1.1 jtc * this file is considered selected. See if this is a hard link
1041 1.1 jtc * to a previous file; modify the name as requested by the
1042 1.1 jtc * user; set the final destination.
1043 1.1 jtc */
1044 1.1 jtc ftree_sel(arcn);
1045 1.54 christos if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn, RENM)) < 0))
1046 1.1 jtc break;
1047 1.1 jtc if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
1048 1.1 jtc /*
1049 1.1 jtc * skip file, purge from link table
1050 1.1 jtc */
1051 1.1 jtc purg_lnk(arcn);
1052 1.1 jtc continue;
1053 1.1 jtc }
1054 1.1 jtc
1055 1.1 jtc /*
1056 1.1 jtc * Non standard -Y and -Z flag. When the exisiting file is
1057 1.1 jtc * same age or newer skip
1058 1.1 jtc */
1059 1.1 jtc if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
1060 1.1 jtc if (Yflag && Zflag) {
1061 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
1062 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime))
1063 1.1 jtc continue;
1064 1.1 jtc } else if (Yflag) {
1065 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime)
1066 1.1 jtc continue;
1067 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime)
1068 1.1 jtc continue;
1069 1.1 jtc }
1070 1.1 jtc
1071 1.1 jtc if (vflag) {
1072 1.17 christos (void)safe_print(arcn->name, listf);
1073 1.1 jtc vfpart = 1;
1074 1.1 jtc }
1075 1.1 jtc ++flcnt;
1076 1.1 jtc
1077 1.1 jtc /*
1078 1.1 jtc * try to create a hard link to the src file if requested
1079 1.1 jtc * but make sure we are not trying to overwrite ourselves.
1080 1.1 jtc */
1081 1.14 itohy if (lflag)
1082 1.1 jtc res = cross_lnk(arcn);
1083 1.1 jtc else
1084 1.1 jtc res = chk_same(arcn);
1085 1.1 jtc if (res <= 0) {
1086 1.1 jtc if (vflag && vfpart) {
1087 1.17 christos (void)putc('\n', listf);
1088 1.1 jtc vfpart = 0;
1089 1.1 jtc }
1090 1.1 jtc continue;
1091 1.1 jtc }
1092 1.1 jtc
1093 1.1 jtc /*
1094 1.1 jtc * have to create a new file
1095 1.1 jtc */
1096 1.1 jtc if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
1097 1.1 jtc /*
1098 1.1 jtc * create a link or special file
1099 1.1 jtc */
1100 1.31 tron if ((arcn->type == PAX_HLK) ||
1101 1.31 tron (arcn->type == PAX_HRG)) {
1102 1.31 tron int payload;
1103 1.31 tron
1104 1.31 tron res = lnk_creat(arcn, &payload);
1105 1.31 tron } else {
1106 1.1 jtc res = node_creat(arcn);
1107 1.31 tron }
1108 1.1 jtc if (res < 0)
1109 1.1 jtc purg_lnk(arcn);
1110 1.1 jtc if (vflag && vfpart) {
1111 1.17 christos (void)putc('\n', listf);
1112 1.1 jtc vfpart = 0;
1113 1.1 jtc }
1114 1.1 jtc continue;
1115 1.1 jtc }
1116 1.1 jtc
1117 1.1 jtc /*
1118 1.1 jtc * have to copy a regular file to the destination directory.
1119 1.1 jtc * first open source file and then create the destination file
1120 1.1 jtc */
1121 1.1 jtc if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
1122 1.1 jtc syswarn(1, errno, "Unable to open %s to read",
1123 1.1 jtc arcn->org_name);
1124 1.1 jtc purg_lnk(arcn);
1125 1.1 jtc continue;
1126 1.1 jtc }
1127 1.31 tron if ((fddest = file_creat(arcn, 0)) < 0) {
1128 1.1 jtc rdfile_close(arcn, &fdsrc);
1129 1.1 jtc purg_lnk(arcn);
1130 1.1 jtc continue;
1131 1.1 jtc }
1132 1.1 jtc
1133 1.1 jtc /*
1134 1.1 jtc * copy source file data to the destination file
1135 1.1 jtc */
1136 1.1 jtc cp_file(arcn, fdsrc, fddest);
1137 1.1 jtc file_close(arcn, fddest);
1138 1.1 jtc rdfile_close(arcn, &fdsrc);
1139 1.1 jtc
1140 1.1 jtc if (vflag && vfpart) {
1141 1.17 christos (void)putc('\n', listf);
1142 1.1 jtc vfpart = 0;
1143 1.1 jtc }
1144 1.1 jtc }
1145 1.1 jtc
1146 1.1 jtc /*
1147 1.1 jtc * restore directory modes and times as required; make sure all
1148 1.1 jtc * patterns were selected block off signals to avoid chance for
1149 1.1 jtc * multiple entry into the cleanup code.
1150 1.1 jtc */
1151 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
1152 1.1 jtc ar_close();
1153 1.1 jtc proc_dir();
1154 1.1 jtc ftree_chk();
1155 1.49 dsl
1156 1.49 dsl return 0;
1157 1.1 jtc }
1158 1.1 jtc
1159 1.1 jtc /*
1160 1.1 jtc * next_head()
1161 1.1 jtc * try to find a valid header in the archive. Uses format specific
1162 1.1 jtc * routines to extract the header and id the trailer. Trailers may be
1163 1.1 jtc * located within a valid header or in an invalid header (the location
1164 1.1 jtc * is format specific. The inhead field from the option table tells us
1165 1.1 jtc * where to look for the trailer).
1166 1.1 jtc * We keep reading (and resyncing) until we get enough contiguous data
1167 1.1 jtc * to check for a header. If we cannot find one, we shift by a byte
1168 1.1 jtc * add a new byte from the archive to the end of the buffer and try again.
1169 1.1 jtc * If we get a read error, we throw out what we have (as we must have
1170 1.1 jtc * contiguous data) and start over again.
1171 1.1 jtc * ASSUMED: headers fit within a BLKMULT header.
1172 1.1 jtc * Return:
1173 1.1 jtc * 0 if we got a header, -1 if we are unable to ever find another one
1174 1.1 jtc * (we reached the end of input, or we reached the limit on retries. see
1175 1.1 jtc * the specs for rd_wrbuf() for more details)
1176 1.1 jtc */
1177 1.1 jtc
1178 1.1 jtc static int
1179 1.6 tls next_head(ARCHD *arcn)
1180 1.1 jtc {
1181 1.6 tls int ret;
1182 1.6 tls char *hdend;
1183 1.6 tls int res;
1184 1.6 tls int shftsz;
1185 1.6 tls int hsz;
1186 1.14 itohy int in_resync = 0; /* set when we are in resync mode */
1187 1.1 jtc int cnt = 0; /* counter for trailer function */
1188 1.17 christos int first = 1; /* on 1st read, EOF isn't premature. */
1189 1.14 itohy
1190 1.1 jtc /*
1191 1.1 jtc * set up initial conditions, we want a whole frmt->hsz block as we
1192 1.1 jtc * have no data yet.
1193 1.1 jtc */
1194 1.1 jtc res = hsz = frmt->hsz;
1195 1.1 jtc hdend = hdbuf;
1196 1.1 jtc shftsz = hsz - 1;
1197 1.1 jtc for(;;) {
1198 1.1 jtc /*
1199 1.1 jtc * keep looping until we get a contiguous FULL buffer
1200 1.1 jtc * (frmt->hsz is the proper size)
1201 1.1 jtc */
1202 1.1 jtc for (;;) {
1203 1.1 jtc if ((ret = rd_wrbuf(hdend, res)) == res)
1204 1.1 jtc break;
1205 1.17 christos
1206 1.17 christos /*
1207 1.17 christos * If we read 0 bytes (EOF) from an archive when we
1208 1.17 christos * expect to find a header, we have stepped upon
1209 1.17 christos * an archive without the customary block of zeroes
1210 1.17 christos * end marker. It's just stupid to error out on
1211 1.17 christos * them, so exit gracefully.
1212 1.17 christos */
1213 1.17 christos if (first && ret == 0)
1214 1.48 dsl return -1;
1215 1.17 christos first = 0;
1216 1.1 jtc
1217 1.1 jtc /*
1218 1.1 jtc * some kind of archive read problem, try to resync the
1219 1.1 jtc * storage device, better give the user the bad news.
1220 1.1 jtc */
1221 1.1 jtc if ((ret == 0) || (rd_sync() < 0)) {
1222 1.24 christos tty_warn(1,
1223 1.24 christos "Premature end of file on archive read");
1224 1.48 dsl return -1;
1225 1.1 jtc }
1226 1.1 jtc if (!in_resync) {
1227 1.1 jtc if (act == APPND) {
1228 1.7 christos tty_warn(1,
1229 1.14 itohy "Archive I/O error, cannot continue");
1230 1.48 dsl return -1;
1231 1.1 jtc }
1232 1.7 christos tty_warn(1,
1233 1.7 christos "Archive I/O error. Trying to recover.");
1234 1.1 jtc ++in_resync;
1235 1.1 jtc }
1236 1.1 jtc
1237 1.1 jtc /*
1238 1.1 jtc * oh well, throw it all out and start over
1239 1.1 jtc */
1240 1.1 jtc res = hsz;
1241 1.1 jtc hdend = hdbuf;
1242 1.1 jtc }
1243 1.1 jtc
1244 1.1 jtc /*
1245 1.1 jtc * ok we have a contiguous buffer of the right size. Call the
1246 1.1 jtc * format read routine. If this was not a valid header and this
1247 1.1 jtc * format stores trailers outside of the header, call the
1248 1.1 jtc * format specific trailer routine to check for a trailer. We
1249 1.1 jtc * have to watch out that we do not mis-identify file data or
1250 1.1 jtc * block padding as a header or trailer. Format specific
1251 1.1 jtc * trailer functions must NOT check for the trailer while we
1252 1.1 jtc * are running in resync mode. Some trailer functions may tell
1253 1.1 jtc * us that this block cannot contain a valid header either, so
1254 1.1 jtc * we then throw out the entire block and start over.
1255 1.1 jtc */
1256 1.1 jtc if ((*frmt->rd)(arcn, hdbuf) == 0)
1257 1.1 jtc break;
1258 1.1 jtc
1259 1.1 jtc if (!frmt->inhead) {
1260 1.1 jtc /*
1261 1.1 jtc * this format has trailers outside of valid headers
1262 1.1 jtc */
1263 1.1 jtc if ((ret = (*frmt->trail)(hdbuf,in_resync,&cnt)) == 0){
1264 1.1 jtc /*
1265 1.1 jtc * valid trailer found, drain input as required
1266 1.1 jtc */
1267 1.1 jtc ar_drain();
1268 1.48 dsl return -1;
1269 1.1 jtc }
1270 1.1 jtc
1271 1.1 jtc if (ret == 1) {
1272 1.1 jtc /*
1273 1.1 jtc * we are in resync and we were told to throw
1274 1.1 jtc * the whole block out because none of the
1275 1.1 jtc * bytes in this block can be used to form a
1276 1.1 jtc * valid header
1277 1.1 jtc */
1278 1.1 jtc res = hsz;
1279 1.1 jtc hdend = hdbuf;
1280 1.1 jtc continue;
1281 1.1 jtc }
1282 1.1 jtc }
1283 1.1 jtc
1284 1.1 jtc /*
1285 1.1 jtc * Brute force section.
1286 1.1 jtc * not a valid header. We may be able to find a header yet. So
1287 1.1 jtc * we shift over by one byte, and set up to read one byte at a
1288 1.1 jtc * time from the archive and place it at the end of the buffer.
1289 1.1 jtc * We will keep moving byte at a time until we find a header or
1290 1.1 jtc * get a read error and have to start over.
1291 1.1 jtc */
1292 1.1 jtc if (!in_resync) {
1293 1.1 jtc if (act == APPND) {
1294 1.7 christos tty_warn(1,
1295 1.7 christos "Unable to append, archive header flaw");
1296 1.48 dsl return -1;
1297 1.1 jtc }
1298 1.7 christos tty_warn(1,
1299 1.7 christos "Invalid header, starting valid header search.");
1300 1.1 jtc ++in_resync;
1301 1.1 jtc }
1302 1.4 mycroft memmove(hdbuf, hdbuf+1, shftsz);
1303 1.1 jtc res = 1;
1304 1.1 jtc hdend = hdbuf + shftsz;
1305 1.1 jtc }
1306 1.1 jtc
1307 1.1 jtc /*
1308 1.1 jtc * ok got a valid header, check for trailer if format encodes it in the
1309 1.1 jtc * the header. NOTE: the parameters are different than trailer routines
1310 1.1 jtc * which encode trailers outside of the header!
1311 1.1 jtc */
1312 1.7 christos if (frmt->inhead && ((*frmt->subtrail)(arcn) == 0)) {
1313 1.1 jtc /*
1314 1.1 jtc * valid trailer found, drain input as required
1315 1.1 jtc */
1316 1.1 jtc ar_drain();
1317 1.48 dsl return -1;
1318 1.1 jtc }
1319 1.1 jtc
1320 1.1 jtc ++flcnt;
1321 1.48 dsl return 0;
1322 1.1 jtc }
1323 1.1 jtc
1324 1.1 jtc /*
1325 1.1 jtc * get_arc()
1326 1.1 jtc * Figure out what format an archive is. Handles archive with flaws by
1327 1.1 jtc * brute force searches for a legal header in any supported format. The
1328 1.1 jtc * format id routines have to be careful to NOT mis-identify a format.
1329 1.1 jtc * ASSUMED: headers fit within a BLKMULT header.
1330 1.1 jtc * Return:
1331 1.1 jtc * 0 if archive found -1 otherwise
1332 1.1 jtc */
1333 1.1 jtc
1334 1.1 jtc static int
1335 1.1 jtc get_arc(void)
1336 1.1 jtc {
1337 1.6 tls int i;
1338 1.6 tls int hdsz = 0;
1339 1.6 tls int res;
1340 1.6 tls int minhd = BLKMULT;
1341 1.1 jtc char *hdend;
1342 1.1 jtc int notice = 0;
1343 1.14 itohy
1344 1.1 jtc /*
1345 1.1 jtc * find the smallest header size in all archive formats and then set up
1346 1.1 jtc * to read the archive.
1347 1.1 jtc */
1348 1.1 jtc for (i = 0; ford[i] >= 0; ++i) {
1349 1.1 jtc if (fsub[ford[i]].hsz < minhd)
1350 1.1 jtc minhd = fsub[ford[i]].hsz;
1351 1.1 jtc }
1352 1.1 jtc if (rd_start() < 0)
1353 1.48 dsl return -1;
1354 1.1 jtc res = BLKMULT;
1355 1.1 jtc hdsz = 0;
1356 1.1 jtc hdend = hdbuf;
1357 1.1 jtc for(;;) {
1358 1.1 jtc for (;;) {
1359 1.1 jtc /*
1360 1.1 jtc * fill the buffer with at least the smallest header
1361 1.1 jtc */
1362 1.1 jtc i = rd_wrbuf(hdend, res);
1363 1.1 jtc if (i > 0)
1364 1.1 jtc hdsz += i;
1365 1.1 jtc if (hdsz >= minhd)
1366 1.1 jtc break;
1367 1.1 jtc
1368 1.1 jtc /*
1369 1.1 jtc * if we cannot recover from a read error quit
1370 1.1 jtc */
1371 1.1 jtc if ((i == 0) || (rd_sync() < 0))
1372 1.1 jtc goto out;
1373 1.1 jtc
1374 1.1 jtc /*
1375 1.1 jtc * when we get an error none of the data we already
1376 1.1 jtc * have can be used to create a legal header (we just
1377 1.1 jtc * got an error in the middle), so we throw it all out
1378 1.1 jtc * and refill the buffer with fresh data.
1379 1.1 jtc */
1380 1.1 jtc res = BLKMULT;
1381 1.1 jtc hdsz = 0;
1382 1.1 jtc hdend = hdbuf;
1383 1.1 jtc if (!notice) {
1384 1.1 jtc if (act == APPND)
1385 1.48 dsl return -1;
1386 1.7 christos tty_warn(1,
1387 1.7 christos "Cannot identify format. Searching...");
1388 1.1 jtc ++notice;
1389 1.1 jtc }
1390 1.1 jtc }
1391 1.1 jtc
1392 1.1 jtc /*
1393 1.1 jtc * we have at least the size of the smallest header in any
1394 1.1 jtc * archive format. Look to see if we have a match. The array
1395 1.1 jtc * ford[] is used to specify the header id order to reduce the
1396 1.1 jtc * chance of incorrectly id'ing a valid header (some formats
1397 1.1 jtc * may be subsets of each other and the order would then be
1398 1.1 jtc * important).
1399 1.1 jtc */
1400 1.1 jtc for (i = 0; ford[i] >= 0; ++i) {
1401 1.1 jtc if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1402 1.1 jtc continue;
1403 1.1 jtc frmt = &(fsub[ford[i]]);
1404 1.14 itohy /*
1405 1.1 jtc * yuck, to avoid slow special case code in the extract
1406 1.1 jtc * routines, just push this header back as if it was
1407 1.1 jtc * not seen. We have left extra space at start of the
1408 1.1 jtc * buffer for this purpose. This is a bit ugly, but
1409 1.1 jtc * adding all the special case code is far worse.
1410 1.1 jtc */
1411 1.1 jtc pback(hdbuf, hdsz);
1412 1.48 dsl return 0;
1413 1.1 jtc }
1414 1.1 jtc
1415 1.1 jtc /*
1416 1.1 jtc * We have a flawed archive, no match. we start searching, but
1417 1.1 jtc * we never allow additions to flawed archives
1418 1.1 jtc */
1419 1.1 jtc if (!notice) {
1420 1.1 jtc if (act == APPND)
1421 1.48 dsl return -1;
1422 1.7 christos tty_warn(1, "Cannot identify format. Searching...");
1423 1.1 jtc ++notice;
1424 1.1 jtc }
1425 1.1 jtc
1426 1.1 jtc /*
1427 1.1 jtc * brute force search for a header that we can id.
1428 1.1 jtc * we shift through byte at a time. this is slow, but we cannot
1429 1.1 jtc * determine the nature of the flaw in the archive in a
1430 1.1 jtc * portable manner
1431 1.1 jtc */
1432 1.1 jtc if (--hdsz > 0) {
1433 1.4 mycroft memmove(hdbuf, hdbuf+1, hdsz);
1434 1.1 jtc res = BLKMULT - hdsz;
1435 1.1 jtc hdend = hdbuf + hdsz;
1436 1.1 jtc } else {
1437 1.1 jtc res = BLKMULT;
1438 1.1 jtc hdend = hdbuf;
1439 1.1 jtc hdsz = 0;
1440 1.1 jtc }
1441 1.1 jtc }
1442 1.1 jtc
1443 1.1 jtc out:
1444 1.1 jtc /*
1445 1.1 jtc * we cannot find a header, bow, apologize and quit
1446 1.1 jtc */
1447 1.21 christos tty_warn(1, "Sorry, unable to determine archive format.");
1448 1.48 dsl return -1;
1449 1.1 jtc }
1450