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