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