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