source: freewrt/tools/paxmirabilis/src/ar_subs.c@ fdd4f59

freewrt_1_0 freewrt_2_0
Last change on this file since fdd4f59 was dbbcad4, checked in by Thorsten Glaser <tg@…>, 19 years ago

sync with MirCpio: introduce "extract to stdout" option

"schön" wbx@

git-svn-id: svn://www.freewrt.org/trunk/freewrt@207 afb5a338-a214-0410-bd46-81f09a774fd1

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