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