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