source: freewrt/tools/paxmirabilis/src/ar_subs.c@ 7417b08

freewrt_1_0 freewrt_2_0
Last change on this file since 7417b08 was a569125, checked in by Thorsten Glaser <tg@…>, 14 years ago

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

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