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