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