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