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