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