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