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