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