readelf: report ARM program and section header types
[freebsd-src.git] / bin / pax / ar_subs.c
blobbcab2fce24cb66f8d2069eb9d1613223ceeb67a5
1 /*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include "pax.h"
52 #include "extern.h"
54 static void wr_archive(ARCHD *, int is_app);
55 static int get_arc(void);
56 static int next_head(ARCHD *);
59 * Routines which control the overall operation modes of pax as specified by
60 * the user: list, append, read ...
63 static char hdbuf[BLKMULT]; /* space for archive header on read */
64 u_long flcnt; /* number of files processed */
67 * list()
68 * list the contents of an archive which match user supplied pattern(s)
69 * (no pattern matches all).
72 void
73 list(void)
75 ARCHD *arcn;
76 int res;
77 ARCHD archd;
78 time_t now;
80 arcn = &archd;
82 * figure out archive type; pass any format specific options to the
83 * archive option processing routine; call the format init routine. We
84 * also save current time for ls_list() so we do not make a system
85 * call for each file we need to print. If verbose (vflag) start up
86 * the name and group caches.
88 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
89 ((*frmt->st_rd)() < 0))
90 return;
92 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
93 return;
95 now = time(NULL);
98 * step through the archive until the format says it is done
100 while (next_head(arcn) == 0) {
102 * check for pattern, and user specified options match.
103 * When all patterns are matched we are done.
105 if ((res = pat_match(arcn)) < 0)
106 break;
108 if ((res == 0) && (sel_chk(arcn) == 0)) {
110 * pattern resulted in a selected file
112 if (pat_sel(arcn) < 0)
113 break;
116 * modify the name as requested by the user if name
117 * survives modification, do a listing of the file
119 if ((res = mod_name(arcn)) < 0)
120 break;
121 if (res == 0)
122 ls_list(arcn, now, stdout);
126 * skip to next archive format header using values calculated
127 * by the format header read routine
129 if (rd_skip(arcn->skip + arcn->pad) == 1)
130 break;
134 * all done, let format have a chance to cleanup, and make sure that
135 * the patterns supplied by the user were all matched
137 (void)(*frmt->end_rd)();
138 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
139 ar_close();
140 pat_chk();
144 * extract()
145 * extract the member(s) of an archive as specified by user supplied
146 * pattern(s) (no patterns extracts all members)
149 void
150 extract(void)
152 ARCHD *arcn;
153 int res;
154 off_t cnt;
155 ARCHD archd;
156 struct stat sb;
157 int fd;
158 time_t now;
160 arcn = &archd;
162 * figure out archive type; pass any format specific options to the
163 * archive option processing routine; call the format init routine;
164 * start up the directory modification time and access mode database
166 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
167 ((*frmt->st_rd)() < 0) || (dir_start() < 0))
168 return;
171 * When we are doing interactive rename, we store the mapping of names
172 * so we can fix up hard links files later in the archive.
174 if (iflag && (name_start() < 0))
175 return;
177 now = time(NULL);
180 * step through each entry on the archive until the format read routine
181 * says it is done
183 while (next_head(arcn) == 0) {
186 * check for pattern, and user specified options match. When
187 * all the patterns are matched we are done
189 if ((res = pat_match(arcn)) < 0)
190 break;
192 if ((res > 0) || (sel_chk(arcn) != 0)) {
194 * file is not selected. skip past any file data and
195 * padding and go back for the next archive member
197 (void)rd_skip(arcn->skip + arcn->pad);
198 continue;
202 * with -u or -D only extract when the archive member is newer
203 * than the file with the same name in the file system (nos
204 * test of being the same type is required).
205 * NOTE: this test is done BEFORE name modifications as
206 * specified by pax. this operation can be confusing to the
207 * user who might expect the test to be done on an existing
208 * file AFTER the name mod. In honesty the pax spec is probably
209 * flawed in this respect.
211 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
212 if (uflag && Dflag) {
213 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
214 (arcn->sb.st_ctime <= sb.st_ctime)) {
215 (void)rd_skip(arcn->skip + arcn->pad);
216 continue;
218 } else if (Dflag) {
219 if (arcn->sb.st_ctime <= sb.st_ctime) {
220 (void)rd_skip(arcn->skip + arcn->pad);
221 continue;
223 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
224 (void)rd_skip(arcn->skip + arcn->pad);
225 continue;
230 * this archive member is now been selected. modify the name.
232 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
233 break;
234 if (res > 0) {
236 * a bad name mod, skip and purge name from link table
238 purg_lnk(arcn);
239 (void)rd_skip(arcn->skip + arcn->pad);
240 continue;
244 * Non standard -Y and -Z flag. When the existing file is
245 * same age or newer skip
247 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
248 if (Yflag && Zflag) {
249 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
250 (arcn->sb.st_ctime <= sb.st_ctime)) {
251 (void)rd_skip(arcn->skip + arcn->pad);
252 continue;
254 } else if (Yflag) {
255 if (arcn->sb.st_ctime <= sb.st_ctime) {
256 (void)rd_skip(arcn->skip + arcn->pad);
257 continue;
259 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
260 (void)rd_skip(arcn->skip + arcn->pad);
261 continue;
265 if (vflag) {
266 if (vflag > 1)
267 ls_list(arcn, now, listf);
268 else {
269 (void)fputs(arcn->name, listf);
270 vfpart = 1;
275 * if required, chdir around.
277 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
278 if (chdir(arcn->pat->chdname) != 0)
279 syswarn(1, errno, "Cannot chdir to %s",
280 arcn->pat->chdname);
283 * all ok, extract this member based on type
285 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
287 * process archive members that are not regular files.
288 * throw out padding and any data that might follow the
289 * header (as determined by the format).
291 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
292 res = lnk_creat(arcn);
293 else
294 res = node_creat(arcn);
296 (void)rd_skip(arcn->skip + arcn->pad);
297 if (res < 0)
298 purg_lnk(arcn);
300 if (vflag && vfpart) {
301 (void)putc('\n', listf);
302 vfpart = 0;
304 continue;
307 * we have a file with data here. If we can not create it, skip
308 * over the data and purge the name from hard link table
310 if ((fd = file_creat(arcn)) < 0) {
311 (void)rd_skip(arcn->skip + arcn->pad);
312 purg_lnk(arcn);
313 continue;
316 * extract the file from the archive and skip over padding and
317 * any unprocessed data
319 res = (*frmt->rd_data)(arcn, fd, &cnt);
320 file_close(arcn, fd);
321 if (vflag && vfpart) {
322 (void)putc('\n', listf);
323 vfpart = 0;
325 if (!res)
326 (void)rd_skip(cnt + arcn->pad);
329 * if required, chdir around.
331 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
332 if (fchdir(cwdfd) != 0)
333 syswarn(1, errno,
334 "Can't fchdir to starting directory");
338 * all done, restore directory modes and times as required; make sure
339 * all patterns supplied by the user were matched; block off signals
340 * to avoid chance for multiple entry into the cleanup code.
342 (void)(*frmt->end_rd)();
343 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
344 ar_close();
345 proc_dir();
346 pat_chk();
350 * wr_archive()
351 * Write an archive. used in both creating a new archive and appends on
352 * previously written archive.
355 static void
356 wr_archive(ARCHD *arcn, int is_app)
358 int res;
359 int hlk;
360 int wr_one;
361 off_t cnt;
362 int (*wrf)(ARCHD *);
363 int fd = -1;
364 time_t now;
367 * if this format supports hard link storage, start up the database
368 * that detects them.
370 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
371 return;
374 * start up the file traversal code and format specific write
376 if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
377 return;
378 wrf = frmt->wr;
381 * When we are doing interactive rename, we store the mapping of names
382 * so we can fix up hard links files later in the archive.
384 if (iflag && (name_start() < 0))
385 return;
388 * if this not append, and there are no files, we do no write a trailer
390 wr_one = is_app;
392 now = time(NULL);
395 * while there are files to archive, process them one at at time
397 while (next_file(arcn) == 0) {
399 * check if this file meets user specified options match.
401 if (sel_chk(arcn) != 0) {
402 ftree_notsel();
403 continue;
405 fd = -1;
406 if (uflag) {
408 * only archive if this file is newer than a file with
409 * the same name that is already stored on the archive
411 if ((res = chk_ftime(arcn)) < 0)
412 break;
413 if (res > 0)
414 continue;
418 * this file is considered selected now. see if this is a hard
419 * link to a file already stored
421 ftree_sel(arcn);
422 if (hlk && (chk_lnk(arcn) < 0))
423 break;
425 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
426 (arcn->type == PAX_CTG)) {
428 * we will have to read this file. by opening it now we
429 * can avoid writing a header to the archive for a file
430 * we were later unable to read (we also purge it from
431 * the link table).
433 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
434 syswarn(1,errno, "Unable to open %s to read",
435 arcn->org_name);
436 purg_lnk(arcn);
437 continue;
442 * Now modify the name as requested by the user
444 if ((res = mod_name(arcn)) < 0) {
446 * name modification says to skip this file, close the
447 * file and purge link table entry
449 rdfile_close(arcn, &fd);
450 purg_lnk(arcn);
451 break;
454 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
456 * unable to obtain the crc we need, close the file,
457 * purge link table entry
459 rdfile_close(arcn, &fd);
460 purg_lnk(arcn);
461 continue;
464 if (vflag) {
465 if (vflag > 1)
466 ls_list(arcn, now, listf);
467 else {
468 (void)fputs(arcn->name, listf);
469 vfpart = 1;
472 ++flcnt;
475 * looks safe to store the file, have the format specific
476 * routine write routine store the file header on the archive
478 if ((res = (*wrf)(arcn)) < 0) {
479 rdfile_close(arcn, &fd);
480 break;
482 wr_one = 1;
483 if (res > 0) {
485 * format write says no file data needs to be stored
486 * so we are done messing with this file
488 if (vflag && vfpart) {
489 (void)putc('\n', listf);
490 vfpart = 0;
492 rdfile_close(arcn, &fd);
493 continue;
497 * Add file data to the archive, quit on write error. if we
498 * cannot write the entire file contents to the archive we
499 * must pad the archive to replace the missing file data
500 * (otherwise during an extract the file header for the file
501 * which FOLLOWS this one will not be where we expect it to
502 * be).
504 res = (*frmt->wr_data)(arcn, fd, &cnt);
505 rdfile_close(arcn, &fd);
506 if (vflag && vfpart) {
507 (void)putc('\n', listf);
508 vfpart = 0;
510 if (res < 0)
511 break;
514 * pad as required, cnt is number of bytes not written
516 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
517 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
518 break;
522 * tell format to write trailer; pad to block boundary; reset directory
523 * mode/access times, and check if all patterns supplied by the user
524 * were matched. block off signals to avoid chance for multiple entry
525 * into the cleanup code
527 if (wr_one) {
528 (*frmt->end_wr)();
529 wr_fin();
531 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
532 ar_close();
533 if (tflag)
534 proc_dir();
535 ftree_chk();
539 * append()
540 * Add file to previously written archive. Archive format specified by the
541 * user must agree with archive. The archive is read first to collect
542 * modification times (if -u) and locate the archive trailer. The archive
543 * is positioned in front of the record with the trailer and wr_archive()
544 * is called to add the new members.
545 * PAX IMPLEMENTATION DETAIL NOTE:
546 * -u is implemented by adding the new members to the end of the archive.
547 * Care is taken so that these do not end up as links to the older
548 * version of the same file already stored in the archive. It is expected
549 * when extraction occurs these newer versions will over-write the older
550 * ones stored "earlier" in the archive (this may be a bad assumption as
551 * it depends on the implementation of the program doing the extraction).
552 * It is really difficult to splice in members without either re-writing
553 * the entire archive (from the point were the old version was), or having
554 * assistance of the format specification in terms of a special update
555 * header that invalidates a previous archive record. The POSIX spec left
556 * the method used to implement -u unspecified. This pax is able to
557 * over write existing files that it creates.
560 void
561 append(void)
563 ARCHD *arcn;
564 int res;
565 ARCHD archd;
566 FSUB *orgfrmt;
567 int udev;
568 off_t tlen;
570 arcn = &archd;
571 orgfrmt = frmt;
574 * Do not allow an append operation if the actual archive is of a
575 * different format than the user specified format.
577 if (get_arc() < 0)
578 return;
579 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
580 paxwarn(1, "Cannot mix current archive format %s with %s",
581 frmt->name, orgfrmt->name);
582 return;
586 * pass the format any options and start up format
588 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
589 return;
592 * if we only are adding members that are newer, we need to save the
593 * mod times for all files we see.
595 if (uflag && (ftime_start() < 0))
596 return;
599 * some archive formats encode hard links by recording the device and
600 * file serial number (inode) but copy the file anyway (multiple times)
601 * to the archive. When we append, we run the risk that newly added
602 * files may have the same device and inode numbers as those recorded
603 * on the archive but during a previous run. If this happens, when the
604 * archive is extracted we get INCORRECT hard links. We avoid this by
605 * remapping the device numbers so that newly added files will never
606 * use the same device number as one found on the archive. remapping
607 * allows new members to safely have links among themselves. remapping
608 * also avoids problems with file inode (serial number) truncations
609 * when the inode number is larger than storage space in the archive
610 * header. See the remap routines for more details.
612 if ((udev = frmt->udev) && (dev_start() < 0))
613 return;
616 * reading the archive may take a long time. If verbose tell the user
618 if (vflag) {
619 (void)fprintf(listf,
620 "%s: Reading archive to position at the end...", argv0);
621 vfpart = 1;
625 * step through the archive until the format says it is done
627 while (next_head(arcn) == 0) {
629 * check if this file meets user specified options.
631 if (sel_chk(arcn) != 0) {
632 if (rd_skip(arcn->skip + arcn->pad) == 1)
633 break;
634 continue;
637 if (uflag) {
639 * see if this is the newest version of this file has
640 * already been seen, if so skip.
642 if ((res = chk_ftime(arcn)) < 0)
643 break;
644 if (res > 0) {
645 if (rd_skip(arcn->skip + arcn->pad) == 1)
646 break;
647 continue;
652 * Store this device number. Device numbers seen during the
653 * read phase of append will cause newly appended files with a
654 * device number seen in the old part of the archive to be
655 * remapped to an unused device number.
657 if ((udev && (add_dev(arcn) < 0)) ||
658 (rd_skip(arcn->skip + arcn->pad) == 1))
659 break;
663 * done, finish up read and get the number of bytes to back up so we
664 * can add new members. The format might have used the hard link table,
665 * purge it.
667 tlen = (*frmt->end_rd)();
668 lnk_end();
671 * try to position for write, if this fails quit. if any error occurs,
672 * we will refuse to write
674 if (appnd_start(tlen) < 0)
675 return;
678 * tell the user we are done reading.
680 if (vflag && vfpart) {
681 (void)fputs("done.\n", listf);
682 vfpart = 0;
686 * go to the writing phase to add the new members
688 wr_archive(arcn, 1);
692 * archive()
693 * write a new archive
696 void
697 archive(void)
699 ARCHD archd;
702 * if we only are adding members that are newer, we need to save the
703 * mod times for all files; set up for writing; pass the format any
704 * options write the archive
706 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
707 return;
708 if ((*frmt->options)() < 0)
709 return;
711 wr_archive(&archd, 0);
715 * copy()
716 * copy files from one part of the file system to another. this does not
717 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
718 * archive was written and then extracted in the destination directory
719 * (except the files are forced to be under the destination directory).
722 void
723 copy(void)
725 ARCHD *arcn;
726 int res;
727 int fddest;
728 char *dest_pt;
729 int dlen;
730 int drem;
731 int fdsrc = -1;
732 struct stat sb;
733 ARCHD archd;
734 char dirbuf[PAXPATHLEN+1];
736 arcn = &archd;
738 * set up the destination dir path and make sure it is a directory. We
739 * make sure we have a trailing / on the destination
741 dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
742 dest_pt = dirbuf + dlen;
743 if (*(dest_pt-1) != '/') {
744 *dest_pt++ = '/';
745 ++dlen;
747 *dest_pt = '\0';
748 drem = PAXPATHLEN - dlen;
750 if (stat(dirptr, &sb) < 0) {
751 syswarn(1, errno, "Cannot access destination directory %s",
752 dirptr);
753 return;
755 if (!S_ISDIR(sb.st_mode)) {
756 paxwarn(1, "Destination is not a directory %s", dirptr);
757 return;
761 * start up the hard link table; file traversal routines and the
762 * modification time and access mode database
764 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
765 return;
768 * When we are doing interactive rename, we store the mapping of names
769 * so we can fix up hard links files later in the archive.
771 if (iflag && (name_start() < 0))
772 return;
775 * set up to cp file trees
777 cp_start();
780 * while there are files to archive, process them
782 while (next_file(arcn) == 0) {
783 fdsrc = -1;
786 * check if this file meets user specified options
788 if (sel_chk(arcn) != 0) {
789 ftree_notsel();
790 continue;
794 * if there is already a file in the destination directory with
795 * the same name and it is newer, skip the one stored on the
796 * archive.
797 * NOTE: this test is done BEFORE name modifications as
798 * specified by pax. this can be confusing to the user who
799 * might expect the test to be done on an existing file AFTER
800 * the name mod. In honesty the pax spec is probably flawed in
801 * this respect
803 if (uflag || Dflag) {
805 * create the destination name
807 if (*(arcn->name) == '/')
808 res = 1;
809 else
810 res = 0;
811 if ((arcn->nlen - res) > drem) {
812 paxwarn(1, "Destination pathname too long %s",
813 arcn->name);
814 continue;
816 (void)strncpy(dest_pt, arcn->name + res, drem);
817 dirbuf[PAXPATHLEN] = '\0';
820 * if existing file is same age or newer skip
822 res = lstat(dirbuf, &sb);
823 *dest_pt = '\0';
825 if (res == 0) {
826 if (uflag && Dflag) {
827 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
828 (arcn->sb.st_ctime<=sb.st_ctime))
829 continue;
830 } else if (Dflag) {
831 if (arcn->sb.st_ctime <= sb.st_ctime)
832 continue;
833 } else if (arcn->sb.st_mtime <= sb.st_mtime)
834 continue;
839 * this file is considered selected. See if this is a hard link
840 * to a previous file; modify the name as requested by the
841 * user; set the final destination.
843 ftree_sel(arcn);
844 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
845 break;
846 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
848 * skip file, purge from link table
850 purg_lnk(arcn);
851 continue;
855 * Non standard -Y and -Z flag. When the existing file is
856 * same age or newer skip
858 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
859 if (Yflag && Zflag) {
860 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
861 (arcn->sb.st_ctime <= sb.st_ctime))
862 continue;
863 } else if (Yflag) {
864 if (arcn->sb.st_ctime <= sb.st_ctime)
865 continue;
866 } else if (arcn->sb.st_mtime <= sb.st_mtime)
867 continue;
870 if (vflag) {
871 (void)fputs(arcn->name, listf);
872 vfpart = 1;
874 ++flcnt;
877 * try to create a hard link to the src file if requested
878 * but make sure we are not trying to overwrite ourselves.
880 if (lflag)
881 res = cross_lnk(arcn);
882 else
883 res = chk_same(arcn);
884 if (res <= 0) {
885 if (vflag && vfpart) {
886 (void)putc('\n', listf);
887 vfpart = 0;
889 continue;
893 * have to create a new file
895 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
897 * create a link or special file
899 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
900 res = lnk_creat(arcn);
901 else
902 res = node_creat(arcn);
903 if (res < 0)
904 purg_lnk(arcn);
905 if (vflag && vfpart) {
906 (void)putc('\n', listf);
907 vfpart = 0;
909 continue;
913 * have to copy a regular file to the destination directory.
914 * first open source file and then create the destination file
916 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
917 syswarn(1, errno, "Unable to open %s to read",
918 arcn->org_name);
919 purg_lnk(arcn);
920 continue;
922 if ((fddest = file_creat(arcn)) < 0) {
923 rdfile_close(arcn, &fdsrc);
924 purg_lnk(arcn);
925 continue;
929 * copy source file data to the destination file
931 cp_file(arcn, fdsrc, fddest);
932 file_close(arcn, fddest);
933 rdfile_close(arcn, &fdsrc);
935 if (vflag && vfpart) {
936 (void)putc('\n', listf);
937 vfpart = 0;
942 * restore directory modes and times as required; make sure all
943 * patterns were selected block off signals to avoid chance for
944 * multiple entry into the cleanup code.
946 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
947 ar_close();
948 proc_dir();
949 ftree_chk();
953 * next_head()
954 * try to find a valid header in the archive. Uses format specific
955 * routines to extract the header and id the trailer. Trailers may be
956 * located within a valid header or in an invalid header (the location
957 * is format specific. The inhead field from the option table tells us
958 * where to look for the trailer).
959 * We keep reading (and resyncing) until we get enough contiguous data
960 * to check for a header. If we cannot find one, we shift by a byte
961 * add a new byte from the archive to the end of the buffer and try again.
962 * If we get a read error, we throw out what we have (as we must have
963 * contiguous data) and start over again.
964 * ASSUMED: headers fit within a BLKMULT header.
965 * Return:
966 * 0 if we got a header, -1 if we are unable to ever find another one
967 * (we reached the end of input, or we reached the limit on retries. see
968 * the specs for rd_wrbuf() for more details)
971 static int
972 next_head(ARCHD *arcn)
974 int ret;
975 char *hdend;
976 int res;
977 int shftsz;
978 int hsz;
979 int in_resync = 0; /* set when we are in resync mode */
980 int cnt = 0; /* counter for trailer function */
981 int first = 1; /* on 1st read, EOF isn't premature. */
984 * set up initial conditions, we want a whole frmt->hsz block as we
985 * have no data yet.
987 res = hsz = frmt->hsz;
988 hdend = hdbuf;
989 shftsz = hsz - 1;
990 for(;;) {
992 * keep looping until we get a contiguous FULL buffer
993 * (frmt->hsz is the proper size)
995 for (;;) {
996 if ((ret = rd_wrbuf(hdend, res)) == res)
997 break;
1000 * If we read 0 bytes (EOF) from an archive when we
1001 * expect to find a header, we have stepped upon
1002 * an archive without the customary block of zeroes
1003 * end marker. It's just stupid to error out on
1004 * them, so exit gracefully.
1006 if (first && ret == 0)
1007 return(-1);
1008 first = 0;
1011 * some kind of archive read problem, try to resync the
1012 * storage device, better give the user the bad news.
1014 if ((ret == 0) || (rd_sync() < 0)) {
1015 paxwarn(1,"Premature end of file on archive read");
1016 return(-1);
1018 if (!in_resync) {
1019 if (act == APPND) {
1020 paxwarn(1,
1021 "Archive I/O error, cannot continue");
1022 return(-1);
1024 paxwarn(1,"Archive I/O error. Trying to recover.");
1025 ++in_resync;
1029 * oh well, throw it all out and start over
1031 res = hsz;
1032 hdend = hdbuf;
1036 * ok we have a contiguous buffer of the right size. Call the
1037 * format read routine. If this was not a valid header and this
1038 * format stores trailers outside of the header, call the
1039 * format specific trailer routine to check for a trailer. We
1040 * have to watch out that we do not mis-identify file data or
1041 * block padding as a header or trailer. Format specific
1042 * trailer functions must NOT check for the trailer while we
1043 * are running in resync mode. Some trailer functions may tell
1044 * us that this block cannot contain a valid header either, so
1045 * we then throw out the entire block and start over.
1047 if ((*frmt->rd)(arcn, hdbuf) == 0)
1048 break;
1050 if (!frmt->inhead) {
1052 * this format has trailers outside of valid headers
1054 if ((ret = (*frmt->trail_tar)(hdbuf,in_resync,&cnt)) == 0){
1056 * valid trailer found, drain input as required
1058 ar_drain();
1059 return(-1);
1062 if (ret == 1) {
1064 * we are in resync and we were told to throw
1065 * the whole block out because none of the
1066 * bytes in this block can be used to form a
1067 * valid header
1069 res = hsz;
1070 hdend = hdbuf;
1071 continue;
1076 * Brute force section.
1077 * not a valid header. We may be able to find a header yet. So
1078 * we shift over by one byte, and set up to read one byte at a
1079 * time from the archive and place it at the end of the buffer.
1080 * We will keep moving byte at a time until we find a header or
1081 * get a read error and have to start over.
1083 if (!in_resync) {
1084 if (act == APPND) {
1085 paxwarn(1,"Unable to append, archive header flaw");
1086 return(-1);
1088 paxwarn(1,"Invalid header, starting valid header search.");
1089 ++in_resync;
1091 memmove(hdbuf, hdbuf+1, shftsz);
1092 res = 1;
1093 hdend = hdbuf + shftsz;
1097 * ok got a valid header, check for trailer if format encodes it in
1098 * the header.
1100 if (frmt->inhead && ((*frmt->trail_cpio)(arcn) == 0)) {
1102 * valid trailer found, drain input as required
1104 ar_drain();
1105 return(-1);
1108 ++flcnt;
1109 return(0);
1113 * get_arc()
1114 * Figure out what format an archive is. Handles archive with flaws by
1115 * brute force searches for a legal header in any supported format. The
1116 * format id routines have to be careful to NOT mis-identify a format.
1117 * ASSUMED: headers fit within a BLKMULT header.
1118 * Return:
1119 * 0 if archive found -1 otherwise
1122 static int
1123 get_arc(void)
1125 int i;
1126 int hdsz = 0;
1127 int res;
1128 int minhd = BLKMULT;
1129 char *hdend;
1130 int notice = 0;
1133 * find the smallest header size in all archive formats and then set up
1134 * to read the archive.
1136 for (i = 0; ford[i] >= 0; ++i) {
1137 if (fsub[ford[i]].hsz < minhd)
1138 minhd = fsub[ford[i]].hsz;
1140 if (rd_start() < 0)
1141 return(-1);
1142 res = BLKMULT;
1143 hdsz = 0;
1144 hdend = hdbuf;
1145 for(;;) {
1146 for (;;) {
1148 * fill the buffer with at least the smallest header
1150 i = rd_wrbuf(hdend, res);
1151 if (i > 0)
1152 hdsz += i;
1153 if (hdsz >= minhd)
1154 break;
1157 * if we cannot recover from a read error quit
1159 if ((i == 0) || (rd_sync() < 0))
1160 goto out;
1163 * when we get an error none of the data we already
1164 * have can be used to create a legal header (we just
1165 * got an error in the middle), so we throw it all out
1166 * and refill the buffer with fresh data.
1168 res = BLKMULT;
1169 hdsz = 0;
1170 hdend = hdbuf;
1171 if (!notice) {
1172 if (act == APPND)
1173 return(-1);
1174 paxwarn(1,"Cannot identify format. Searching...");
1175 ++notice;
1180 * we have at least the size of the smallest header in any
1181 * archive format. Look to see if we have a match. The array
1182 * ford[] is used to specify the header id order to reduce the
1183 * chance of incorrectly id'ing a valid header (some formats
1184 * may be subsets of each other and the order would then be
1185 * important).
1187 for (i = 0; ford[i] >= 0; ++i) {
1188 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1189 continue;
1190 frmt = &(fsub[ford[i]]);
1192 * yuck, to avoid slow special case code in the extract
1193 * routines, just push this header back as if it was
1194 * not seen. We have left extra space at start of the
1195 * buffer for this purpose. This is a bit ugly, but
1196 * adding all the special case code is far worse.
1198 pback(hdbuf, hdsz);
1199 return(0);
1203 * We have a flawed archive, no match. we start searching, but
1204 * we never allow additions to flawed archives
1206 if (!notice) {
1207 if (act == APPND)
1208 return(-1);
1209 paxwarn(1, "Cannot identify format. Searching...");
1210 ++notice;
1214 * brute force search for a header that we can id.
1215 * we shift through byte at a time. this is slow, but we cannot
1216 * determine the nature of the flaw in the archive in a
1217 * portable manner
1219 if (--hdsz > 0) {
1220 memmove(hdbuf, hdbuf+1, hdsz);
1221 res = BLKMULT - hdsz;
1222 hdend = hdbuf + hdsz;
1223 } else {
1224 res = BLKMULT;
1225 hdend = hdbuf;
1226 hdsz = 0;
1230 out:
1232 * we cannot find a header, bow, apologize and quit
1234 paxwarn(1, "Sorry, unable to determine archive format.");
1235 return(-1);