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