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