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