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