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