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