sync
[bitrig.git] / bin / pax / ar_subs.c
blob11f437ebf6533979db488c337152bc66e163b245
1 /* $OpenBSD: ar_subs.c,v 1.34 2012/12/04 02:24:45 deraadt Exp $ */
2 /* $NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $ */
4 /*-
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <signal.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include "pax.h"
48 #include "extern.h"
50 static void wr_archive(ARCHD *, int is_app);
51 static int get_arc(void);
52 static int next_head(ARCHD *);
53 extern sigset_t s_mask;
56 * Routines which control the overall operation modes of pax as specified by
57 * the user: list, append, read ...
60 static char hdbuf[BLKMULT]; /* space for archive header on read */
61 u_long flcnt; /* number of files processed */
64 * list()
65 * list the contents of an archive which match user supplied pattern(s)
66 * (no pattern matches all).
69 void
70 list(void)
72 ARCHD *arcn;
73 int res;
74 ARCHD archd;
75 time_t now;
77 arcn = &archd;
79 * figure out archive type; pass any format specific options to the
80 * archive option processing routine; call the format init routine. We
81 * also save current time for ls_list() so we do not make a system
82 * call for each file we need to print. If verbose (vflag) start up
83 * the name and group caches.
85 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
86 ((*frmt->st_rd)() < 0))
87 return;
89 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
90 return;
92 now = time(NULL);
95 * step through the archive until the format says it is done
97 while (next_head(arcn) == 0) {
98 if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
100 * we need to read, to get the real filename
102 off_t cnt;
103 if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF
104 ? -1 : -2, &cnt))
105 (void)rd_skip(cnt + arcn->pad);
106 continue;
110 * check for pattern, and user specified options match.
111 * When all patterns are matched we are done.
113 if ((res = pat_match(arcn)) < 0)
114 break;
116 if ((res == 0) && (sel_chk(arcn) == 0)) {
118 * pattern resulted in a selected file
120 if (pat_sel(arcn) < 0)
121 break;
124 * modify the name as requested by the user if name
125 * survives modification, do a listing of the file
127 if ((res = mod_name(arcn)) < 0)
128 break;
129 if (res == 0)
130 ls_list(arcn, now, stdout);
134 * skip to next archive format header using values calculated
135 * by the format header read routine
137 if (rd_skip(arcn->skip + arcn->pad) == 1)
138 break;
142 * all done, let format have a chance to cleanup, and make sure that
143 * the patterns supplied by the user were all matched
145 (void)(*frmt->end_rd)();
146 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
147 ar_close();
148 pat_chk();
152 * extract()
153 * extract the member(s) of an archive as specified by user supplied
154 * pattern(s) (no patterns extracts all members)
157 void
158 extract(void)
160 ARCHD *arcn;
161 int res;
162 off_t cnt;
163 ARCHD archd;
164 struct stat sb;
165 int fd;
166 time_t now;
168 arcn = &archd;
170 * figure out archive type; pass any format specific options to the
171 * archive option processing routine; call the format init routine;
172 * start up the directory modification time and access mode database
174 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
175 ((*frmt->st_rd)() < 0) || (dir_start() < 0))
176 return;
179 * When we are doing interactive rename, we store the mapping of names
180 * so we can fix up hard links files later in the archive.
182 if (iflag && (name_start() < 0))
183 return;
185 now = time(NULL);
188 * step through each entry on the archive until the format read routine
189 * says it is done
191 while (next_head(arcn) == 0) {
192 if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
194 * we need to read, to get the real filename
196 if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF
197 ? -1 : -2, &cnt))
198 (void)rd_skip(cnt + arcn->pad);
199 continue;
203 * check for pattern, and user specified options match. When
204 * all the patterns are matched we are done
206 if ((res = pat_match(arcn)) < 0)
207 break;
209 if ((res > 0) || (sel_chk(arcn) != 0)) {
211 * file is not selected. skip past any file data and
212 * padding and go back for the next archive member
214 (void)rd_skip(arcn->skip + arcn->pad);
215 continue;
219 * with -u or -D only extract when the archive member is newer
220 * than the file with the same name in the file system (no
221 * test of being the same type is required).
222 * NOTE: this test is done BEFORE name modifications as
223 * specified by pax. this operation can be confusing to the
224 * user who might expect the test to be done on an existing
225 * file AFTER the name mod. In honesty the pax spec is probably
226 * flawed in this respect.
228 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
229 if (uflag && Dflag) {
230 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
231 (arcn->sb.st_ctime <= sb.st_ctime)) {
232 (void)rd_skip(arcn->skip + arcn->pad);
233 continue;
235 } else if (Dflag) {
236 if (arcn->sb.st_ctime <= sb.st_ctime) {
237 (void)rd_skip(arcn->skip + arcn->pad);
238 continue;
240 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
241 (void)rd_skip(arcn->skip + arcn->pad);
242 continue;
247 * this archive member is now been selected. modify the name.
249 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
250 break;
251 if (res > 0) {
253 * a bad name mod, skip and purge name from link table
255 purg_lnk(arcn);
256 (void)rd_skip(arcn->skip + arcn->pad);
257 continue;
261 * Non standard -Y and -Z flag. When the existing file is
262 * same age or newer skip
264 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
265 if (Yflag && Zflag) {
266 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
267 (arcn->sb.st_ctime <= sb.st_ctime)) {
268 (void)rd_skip(arcn->skip + arcn->pad);
269 continue;
271 } else if (Yflag) {
272 if (arcn->sb.st_ctime <= sb.st_ctime) {
273 (void)rd_skip(arcn->skip + arcn->pad);
274 continue;
276 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
277 (void)rd_skip(arcn->skip + arcn->pad);
278 continue;
282 if (vflag) {
283 if (vflag > 1)
284 ls_list(arcn, now, listf);
285 else {
286 (void)safe_print(arcn->name, listf);
287 vfpart = 1;
292 * if required, chdir around.
294 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
295 if (chdir(arcn->pat->chdname) != 0)
296 syswarn(1, errno, "Cannot chdir to %s",
297 arcn->pat->chdname);
300 * all ok, extract this member based on type
302 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
304 * process archive members that are not regular files.
305 * throw out padding and any data that might follow the
306 * header (as determined by the format).
308 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
309 res = lnk_creat(arcn);
310 else
311 res = node_creat(arcn);
313 (void)rd_skip(arcn->skip + arcn->pad);
314 if (res < 0)
315 purg_lnk(arcn);
317 if (vflag && vfpart) {
318 (void)putc('\n', listf);
319 vfpart = 0;
321 goto popd;
324 * we have a file with data here. If we can not create it, skip
325 * over the data and purge the name from hard link table
327 if ((fd = file_creat(arcn)) < 0) {
328 (void)rd_skip(arcn->skip + arcn->pad);
329 purg_lnk(arcn);
330 goto popd;
333 * extract the file from the archive and skip over padding and
334 * any unprocessed data
336 res = (*frmt->rd_data)(arcn, fd, &cnt);
337 file_close(arcn, fd);
338 if (vflag && vfpart) {
339 (void)putc('\n', listf);
340 vfpart = 0;
342 if (!res)
343 (void)rd_skip(cnt + arcn->pad);
345 popd:
347 * if required, chdir around.
349 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
350 if (fchdir(cwdfd) != 0)
351 syswarn(1, errno,
352 "Can't fchdir to starting directory");
356 * all done, restore directory modes and times as required; make sure
357 * all patterns supplied by the user were matched; block off signals
358 * to avoid chance for multiple entry into the cleanup code.
360 (void)(*frmt->end_rd)();
361 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
362 ar_close();
363 proc_dir();
364 pat_chk();
368 * wr_archive()
369 * Write an archive. used in both creating a new archive and appends on
370 * previously written archive.
373 static void
374 wr_archive(ARCHD *arcn, int is_app)
376 int res;
377 int hlk;
378 int wr_one;
379 off_t cnt;
380 int (*wrf)(ARCHD *);
381 int fd = -1;
382 time_t now;
385 * if this format supports hard link storage, start up the database
386 * that detects them.
388 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
389 return;
392 * if this is not append, and there are no files, we do not write a
393 * trailer
395 wr_one = is_app;
398 * start up the file traversal code and format specific write
400 if (ftree_start() < 0) {
401 if (is_app)
402 goto trailer;
403 return;
404 } else if (((*frmt->st_wr)() < 0))
405 return;
407 wrf = frmt->wr;
410 * When we are doing interactive rename, we store the mapping of names
411 * so we can fix up hard links files later in the archive.
413 if (iflag && (name_start() < 0))
414 return;
416 now = time(NULL);
419 * while there are files to archive, process them one at at time
421 while (next_file(arcn) == 0) {
423 * check if this file meets user specified options match.
425 if (sel_chk(arcn) != 0)
426 continue;
427 fd = -1;
428 if (uflag) {
430 * only archive if this file is newer than a file with
431 * the same name that is already stored on the archive
433 if ((res = chk_ftime(arcn)) < 0)
434 break;
435 if (res > 0) {
436 ftree_skipped_newer(arcn);
437 continue;
442 * this file is considered selected now. see if this is a hard
443 * link to a file already stored
445 ftree_sel(arcn);
446 if (hlk && (chk_lnk(arcn) < 0))
447 break;
449 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
450 (arcn->type == PAX_CTG)) {
452 * we will have to read this file. by opening it now we
453 * can avoid writing a header to the archive for a file
454 * we were later unable to read (we also purge it from
455 * the link table).
457 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
458 syswarn(1,errno, "Unable to open %s to read",
459 arcn->org_name);
460 purg_lnk(arcn);
461 continue;
466 * Now modify the name as requested by the user
468 if ((res = mod_name(arcn)) < 0) {
470 * name modification says to skip this file, close the
471 * file and purge link table entry
473 rdfile_close(arcn, &fd);
474 purg_lnk(arcn);
475 break;
478 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
480 * unable to obtain the crc we need, close the file,
481 * purge link table entry
483 rdfile_close(arcn, &fd);
484 purg_lnk(arcn);
485 continue;
488 if (vflag) {
489 if (vflag > 1)
490 ls_list(arcn, now, listf);
491 else {
492 (void)safe_print(arcn->name, listf);
493 vfpart = 1;
496 ++flcnt;
499 * looks safe to store the file, have the format specific
500 * routine write routine store the file header on the archive
502 if ((res = (*wrf)(arcn)) < 0) {
503 rdfile_close(arcn, &fd);
504 break;
506 wr_one = 1;
507 if (res > 0) {
509 * format write says no file data needs to be stored
510 * so we are done messing with this file
512 if (vflag && vfpart) {
513 (void)putc('\n', listf);
514 vfpart = 0;
516 rdfile_close(arcn, &fd);
517 continue;
521 * Add file data to the archive, quit on write error. if we
522 * cannot write the entire file contents to the archive we
523 * must pad the archive to replace the missing file data
524 * (otherwise during an extract the file header for the file
525 * which FOLLOWS this one will not be where we expect it to
526 * be).
528 res = (*frmt->wr_data)(arcn, fd, &cnt);
529 rdfile_close(arcn, &fd);
530 if (vflag && vfpart) {
531 (void)putc('\n', listf);
532 vfpart = 0;
534 if (res < 0)
535 break;
538 * pad as required, cnt is number of bytes not written
540 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
541 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
542 break;
545 trailer:
547 * tell format to write trailer; pad to block boundary; reset directory
548 * mode/access times, and check if all patterns supplied by the user
549 * were matched. block off signals to avoid chance for multiple entry
550 * into the cleanup code
552 if (wr_one) {
553 (*frmt->end_wr)();
554 wr_fin();
556 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
557 ar_close();
558 if (tflag)
559 proc_dir();
560 ftree_chk();
564 * append()
565 * Add file to previously written archive. Archive format specified by the
566 * user must agree with archive. The archive is read first to collect
567 * modification times (if -u) and locate the archive trailer. The archive
568 * is positioned in front of the record with the trailer and wr_archive()
569 * is called to add the new members.
570 * PAX IMPLEMENTATION DETAIL NOTE:
571 * -u is implemented by adding the new members to the end of the archive.
572 * Care is taken so that these do not end up as links to the older
573 * version of the same file already stored in the archive. It is expected
574 * when extraction occurs these newer versions will over-write the older
575 * ones stored "earlier" in the archive (this may be a bad assumption as
576 * it depends on the implementation of the program doing the extraction).
577 * It is really difficult to splice in members without either re-writing
578 * the entire archive (from the point were the old version was), or having
579 * assistance of the format specification in terms of a special update
580 * header that invalidates a previous archive record. The posix spec left
581 * the method used to implement -u unspecified. This pax is able to
582 * over write existing files that it creates.
585 void
586 append(void)
588 ARCHD *arcn;
589 int res;
590 ARCHD archd;
591 FSUB *orgfrmt;
592 int udev;
593 off_t tlen;
595 arcn = &archd;
596 orgfrmt = frmt;
599 * Do not allow an append operation if the actual archive is of a
600 * different format than the user specified format.
602 if (get_arc() < 0)
603 return;
604 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
605 paxwarn(1, "Cannot mix current archive format %s with %s",
606 frmt->name, orgfrmt->name);
607 return;
611 * pass the format any options and start up format
613 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
614 return;
617 * if we only are adding members that are newer, we need to save the
618 * mod times for all files we see.
620 if (uflag && (ftime_start() < 0))
621 return;
624 * some archive formats encode hard links by recording the device and
625 * file serial number (inode) but copy the file anyway (multiple times)
626 * to the archive. When we append, we run the risk that newly added
627 * files may have the same device and inode numbers as those recorded
628 * on the archive but during a previous run. If this happens, when the
629 * archive is extracted we get INCORRECT hard links. We avoid this by
630 * remapping the device numbers so that newly added files will never
631 * use the same device number as one found on the archive. remapping
632 * allows new members to safely have links among themselves. remapping
633 * also avoids problems with file inode (serial number) truncations
634 * when the inode number is larger than storage space in the archive
635 * header. See the remap routines for more details.
637 if ((udev = frmt->udev) && (dev_start() < 0))
638 return;
641 * reading the archive may take a long time. If verbose tell the user
643 if (vflag) {
644 (void)fprintf(listf,
645 "%s: Reading archive to position at the end...", argv0);
646 vfpart = 1;
650 * step through the archive until the format says it is done
652 while (next_head(arcn) == 0) {
654 * check if this file meets user specified options.
656 if (sel_chk(arcn) != 0) {
657 if (rd_skip(arcn->skip + arcn->pad) == 1)
658 break;
659 continue;
662 if (uflag) {
664 * see if this is the newest version of this file has
665 * already been seen, if so skip.
667 if ((res = chk_ftime(arcn)) < 0)
668 break;
669 if (res > 0) {
670 if (rd_skip(arcn->skip + arcn->pad) == 1)
671 break;
672 continue;
677 * Store this device number. Device numbers seen during the
678 * read phase of append will cause newly appended files with a
679 * device number seen in the old part of the archive to be
680 * remapped to an unused device number.
682 if ((udev && (add_dev(arcn) < 0)) ||
683 (rd_skip(arcn->skip + arcn->pad) == 1))
684 break;
688 * done, finish up read and get the number of bytes to back up so we
689 * can add new members. The format might have used the hard link table,
690 * purge it.
692 tlen = (*frmt->end_rd)();
693 lnk_end();
696 * try to position for write, if this fails quit. if any error occurs,
697 * we will refuse to write
699 if (appnd_start(tlen) < 0)
700 return;
703 * tell the user we are done reading.
705 if (vflag && vfpart) {
706 (void)fputs("done.\n", listf);
707 vfpart = 0;
711 * go to the writing phase to add the new members
713 wr_archive(arcn, 1);
717 * archive()
718 * write a new archive
721 void
722 archive(void)
724 ARCHD archd;
727 * if we only are adding members that are newer, we need to save the
728 * mod times for all files; set up for writing; pass the format any
729 * options write the archive
731 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
732 return;
733 if ((*frmt->options)() < 0)
734 return;
736 wr_archive(&archd, 0);
740 * copy()
741 * copy files from one part of the file system to another. this does not
742 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
743 * archive was written and then extracted in the destination directory
744 * (except the files are forced to be under the destination directory).
747 void
748 copy(void)
750 ARCHD *arcn;
751 int res;
752 int fddest;
753 char *dest_pt;
754 int dlen;
755 int drem;
756 int fdsrc = -1;
757 struct stat sb;
758 ARCHD archd;
759 char dirbuf[PAXPATHLEN+1];
761 arcn = &archd;
763 * set up the destination dir path and make sure it is a directory. We
764 * make sure we have a trailing / on the destination
766 dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
767 if (dlen >= sizeof(dirbuf) ||
768 (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
769 paxwarn(1, "directory name is too long %s", dirptr);
770 return;
772 dest_pt = dirbuf + dlen;
773 if (*(dest_pt-1) != '/') {
774 *dest_pt++ = '/';
775 *dest_pt = '\0';
776 ++dlen;
778 drem = PAXPATHLEN - dlen;
780 if (stat(dirptr, &sb) < 0) {
781 syswarn(1, errno, "Cannot access destination directory %s",
782 dirptr);
783 return;
785 if (!S_ISDIR(sb.st_mode)) {
786 paxwarn(1, "Destination is not a directory %s", dirptr);
787 return;
791 * start up the hard link table; file traversal routines and the
792 * modification time and access mode database
794 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
795 return;
798 * When we are doing interactive rename, we store the mapping of names
799 * so we can fix up hard links files later in the archive.
801 if (iflag && (name_start() < 0))
802 return;
805 * set up to cp file trees
807 cp_start();
810 * while there are files to archive, process them
812 while (next_file(arcn) == 0) {
813 fdsrc = -1;
816 * check if this file meets user specified options
818 if (sel_chk(arcn) != 0)
819 continue;
822 * if there is already a file in the destination directory with
823 * the same name and it is newer, skip the one stored on the
824 * archive.
825 * NOTE: this test is done BEFORE name modifications as
826 * specified by pax. this can be confusing to the user who
827 * might expect the test to be done on an existing file AFTER
828 * the name mod. In honesty the pax spec is probably flawed in
829 * this respect
831 if (uflag || Dflag) {
833 * create the destination name
835 if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
836 drem + 1) > drem) {
837 paxwarn(1, "Destination pathname too long %s",
838 arcn->name);
839 continue;
843 * if existing file is same age or newer skip
845 res = lstat(dirbuf, &sb);
846 *dest_pt = '\0';
848 if (res == 0) {
849 ftree_skipped_newer(arcn);
850 if (uflag && Dflag) {
851 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
852 (arcn->sb.st_ctime<=sb.st_ctime))
853 continue;
854 } else if (Dflag) {
855 if (arcn->sb.st_ctime <= sb.st_ctime)
856 continue;
857 } else if (arcn->sb.st_mtime <= sb.st_mtime)
858 continue;
863 * this file is considered selected. See if this is a hard link
864 * to a previous file; modify the name as requested by the
865 * user; set the final destination.
867 ftree_sel(arcn);
868 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
869 break;
870 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
872 * skip file, purge from link table
874 purg_lnk(arcn);
875 continue;
879 * Non standard -Y and -Z flag. When the existing file is
880 * same age or newer skip
882 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
883 if (Yflag && Zflag) {
884 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
885 (arcn->sb.st_ctime <= sb.st_ctime))
886 continue;
887 } else if (Yflag) {
888 if (arcn->sb.st_ctime <= sb.st_ctime)
889 continue;
890 } else if (arcn->sb.st_mtime <= sb.st_mtime)
891 continue;
894 if (vflag) {
895 (void)safe_print(arcn->name, listf);
896 vfpart = 1;
898 ++flcnt;
901 * try to create a hard link to the src file if requested
902 * but make sure we are not trying to overwrite ourselves.
904 if (lflag)
905 res = cross_lnk(arcn);
906 else
907 res = chk_same(arcn);
908 if (res <= 0) {
909 if (vflag && vfpart) {
910 (void)putc('\n', listf);
911 vfpart = 0;
913 continue;
917 * have to create a new file
919 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
921 * create a link or special file
923 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
924 res = lnk_creat(arcn);
925 else
926 res = node_creat(arcn);
927 if (res < 0)
928 purg_lnk(arcn);
929 if (vflag && vfpart) {
930 (void)putc('\n', listf);
931 vfpart = 0;
933 continue;
937 * have to copy a regular file to the destination directory.
938 * first open source file and then create the destination file
940 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
941 syswarn(1, errno, "Unable to open %s to read",
942 arcn->org_name);
943 purg_lnk(arcn);
944 continue;
946 if ((fddest = file_creat(arcn)) < 0) {
947 rdfile_close(arcn, &fdsrc);
948 purg_lnk(arcn);
949 continue;
953 * copy source file data to the destination file
955 cp_file(arcn, fdsrc, fddest);
956 file_close(arcn, fddest);
957 rdfile_close(arcn, &fdsrc);
959 if (vflag && vfpart) {
960 (void)putc('\n', listf);
961 vfpart = 0;
966 * restore directory modes and times as required; make sure all
967 * patterns were selected block off signals to avoid chance for
968 * multiple entry into the cleanup code.
970 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
971 ar_close();
972 proc_dir();
973 ftree_chk();
977 * next_head()
978 * try to find a valid header in the archive. Uses format specific
979 * routines to extract the header and id the trailer. Trailers may be
980 * located within a valid header or in an invalid header (the location
981 * is format specific. The inhead field from the option table tells us
982 * where to look for the trailer).
983 * We keep reading (and resyncing) until we get enough contiguous data
984 * to check for a header. If we cannot find one, we shift by a byte
985 * add a new byte from the archive to the end of the buffer and try again.
986 * If we get a read error, we throw out what we have (as we must have
987 * contiguous data) and start over again.
988 * ASSUMED: headers fit within a BLKMULT header.
989 * Return:
990 * 0 if we got a header, -1 if we are unable to ever find another one
991 * (we reached the end of input, or we reached the limit on retries. see
992 * the specs for rd_wrbuf() for more details)
995 static int
996 next_head(ARCHD *arcn)
998 int ret;
999 char *hdend;
1000 int res;
1001 int shftsz;
1002 int hsz;
1003 int in_resync = 0; /* set when we are in resync mode */
1004 int cnt = 0; /* counter for trailer function */
1005 int first = 1; /* on 1st read, EOF isn't premature. */
1008 * set up initial conditions, we want a whole frmt->hsz block as we
1009 * have no data yet.
1011 res = hsz = frmt->hsz;
1012 hdend = hdbuf;
1013 shftsz = hsz - 1;
1014 for (;;) {
1016 * keep looping until we get a contiguous FULL buffer
1017 * (frmt->hsz is the proper size)
1019 for (;;) {
1020 if ((ret = rd_wrbuf(hdend, res)) == res)
1021 break;
1024 * If we read 0 bytes (EOF) from an archive when we
1025 * expect to find a header, we have stepped upon
1026 * an archive without the customary block of zeroes
1027 * end marker. It's just stupid to error out on
1028 * them, so exit gracefully.
1030 if (first && ret == 0)
1031 return(-1);
1032 first = 0;
1035 * some kind of archive read problem, try to resync the
1036 * storage device, better give the user the bad news.
1038 if ((ret == 0) || (rd_sync() < 0)) {
1039 paxwarn(1,"Premature end of file on archive read");
1040 return(-1);
1042 if (!in_resync) {
1043 if (act == APPND) {
1044 paxwarn(1,
1045 "Archive I/O error, cannot continue");
1046 return(-1);
1048 paxwarn(1,"Archive I/O error. Trying to recover.");
1049 ++in_resync;
1053 * oh well, throw it all out and start over
1055 res = hsz;
1056 hdend = hdbuf;
1060 * ok we have a contiguous buffer of the right size. Call the
1061 * format read routine. If this was not a valid header and this
1062 * format stores trailers outside of the header, call the
1063 * format specific trailer routine to check for a trailer. We
1064 * have to watch out that we do not mis-identify file data or
1065 * block padding as a header or trailer. Format specific
1066 * trailer functions must NOT check for the trailer while we
1067 * are running in resync mode. Some trailer functions may tell
1068 * us that this block cannot contain a valid header either, so
1069 * we then throw out the entire block and start over.
1071 if ((*frmt->rd)(arcn, hdbuf) == 0)
1072 break;
1074 if (!frmt->inhead) {
1076 * this format has trailers outside of valid headers
1078 if ((ret = (*frmt->trail)(arcn,hdbuf,in_resync,&cnt)) == 0){
1080 * valid trailer found, drain input as required
1082 ar_drain();
1083 return(-1);
1086 if (ret == 1) {
1088 * we are in resync and we were told to throw
1089 * the whole block out because none of the
1090 * bytes in this block can be used to form a
1091 * valid header
1093 res = hsz;
1094 hdend = hdbuf;
1095 continue;
1100 * Brute force section.
1101 * not a valid header. We may be able to find a header yet. So
1102 * we shift over by one byte, and set up to read one byte at a
1103 * time from the archive and place it at the end of the buffer.
1104 * We will keep moving byte at a time until we find a header or
1105 * get a read error and have to start over.
1107 if (!in_resync) {
1108 if (act == APPND) {
1109 paxwarn(1,"Unable to append, archive header flaw");
1110 return(-1);
1112 paxwarn(1,"Invalid header, starting valid header search.");
1113 ++in_resync;
1115 memmove(hdbuf, hdbuf+1, shftsz);
1116 res = 1;
1117 hdend = hdbuf + shftsz;
1121 * ok got a valid header, check for trailer if format encodes it in the
1122 * the header. NOTE: the parameters are different than trailer routines
1123 * which encode trailers outside of the header!
1125 if (frmt->inhead && ((*frmt->trail)(arcn,NULL,0,NULL) == 0)) {
1127 * valid trailer found, drain input as required
1129 ar_drain();
1130 return(-1);
1133 ++flcnt;
1134 return(0);
1138 * get_arc()
1139 * Figure out what format an archive is. Handles archive with flaws by
1140 * brute force searches for a legal header in any supported format. The
1141 * format id routines have to be careful to NOT mis-identify a format.
1142 * ASSUMED: headers fit within a BLKMULT header.
1143 * Return:
1144 * 0 if archive found -1 otherwise
1147 static int
1148 get_arc(void)
1150 int i;
1151 int hdsz = 0;
1152 int res;
1153 int minhd = BLKMULT;
1154 char *hdend;
1155 int notice = 0;
1158 * find the smallest header size in all archive formats and then set up
1159 * to read the archive.
1161 for (i = 0; ford[i] >= 0; ++i) {
1162 if (fsub[ford[i]].hsz < minhd)
1163 minhd = fsub[ford[i]].hsz;
1165 if (rd_start() < 0)
1166 return(-1);
1167 res = BLKMULT;
1168 hdsz = 0;
1169 hdend = hdbuf;
1170 for (;;) {
1171 for (;;) {
1173 * fill the buffer with at least the smallest header
1175 i = rd_wrbuf(hdend, res);
1176 if (i > 0)
1177 hdsz += i;
1178 if (hdsz >= minhd)
1179 break;
1182 * if we cannot recover from a read error quit
1184 if ((i == 0) || (rd_sync() < 0))
1185 goto out;
1188 * when we get an error none of the data we already
1189 * have can be used to create a legal header (we just
1190 * got an error in the middle), so we throw it all out
1191 * and refill the buffer with fresh data.
1193 res = BLKMULT;
1194 hdsz = 0;
1195 hdend = hdbuf;
1196 if (!notice) {
1197 if (act == APPND)
1198 return(-1);
1199 paxwarn(1,"Cannot identify format. Searching...");
1200 ++notice;
1205 * we have at least the size of the smallest header in any
1206 * archive format. Look to see if we have a match. The array
1207 * ford[] is used to specify the header id order to reduce the
1208 * chance of incorrectly id'ing a valid header (some formats
1209 * may be subsets of each other and the order would then be
1210 * important).
1212 for (i = 0; ford[i] >= 0; ++i) {
1213 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1214 continue;
1215 frmt = &(fsub[ford[i]]);
1217 * yuck, to avoid slow special case code in the extract
1218 * routines, just push this header back as if it was
1219 * not seen. We have left extra space at start of the
1220 * buffer for this purpose. This is a bit ugly, but
1221 * adding all the special case code is far worse.
1223 pback(hdbuf, hdsz);
1224 return(0);
1228 * We have a flawed archive, no match. we start searching, but
1229 * we never allow additions to flawed archives
1231 if (!notice) {
1232 if (act == APPND)
1233 return(-1);
1234 paxwarn(1, "Cannot identify format. Searching...");
1235 ++notice;
1239 * brute force search for a header that we can id.
1240 * we shift through byte at a time. this is slow, but we cannot
1241 * determine the nature of the flaw in the archive in a
1242 * portable manner
1244 if (--hdsz > 0) {
1245 memmove(hdbuf, hdbuf+1, hdsz);
1246 res = BLKMULT - hdsz;
1247 hdend = hdbuf + hdsz;
1248 } else {
1249 res = BLKMULT;
1250 hdend = hdbuf;
1251 hdsz = 0;
1255 out:
1257 * we cannot find a header, bow, apologize and quit
1259 paxwarn(1, "Sorry, unable to determine archive format.");
1260 return(-1);