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