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