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