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