1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2007 Kai Wang 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #define BSDAR_VERSION "1.1.0" 32 33 /* 34 * ar(1) options. 35 */ 36 #define AR_A 0x0001 /* position-after */ 37 #define AR_B 0x0002 /* position-before */ 38 #define AR_C 0x0004 /* creating new archive */ 39 #define AR_CC 0x0008 /* do not overwrite when extracting */ 40 #define AR_J 0x0010 /* bzip2 compression */ 41 #define AR_O 0x0020 /* preserve original mtime when extracting */ 42 #define AR_S 0x0040 /* write archive symbol table */ 43 #define AR_SS 0x0080 /* do not write archive symbol table */ 44 #define AR_TR 0x0100 /* only keep first 15 chars for member name */ 45 #define AR_U 0x0200 /* only extract or update newer members.*/ 46 #define AR_V 0x0400 /* verbose mode */ 47 #define AR_Z 0x0800 /* gzip compression */ 48 #define AR_D 0x1000 /* insert dummy mode, mtime, uid and gid */ 49 50 #define DEF_BLKSZ 10240 /* default block size */ 51 52 /* 53 * Convenient wrapper for general libarchive error handling. 54 */ 55 #define AC(CALL) do { \ 56 if ((CALL)) \ 57 bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s", \ 58 archive_error_string(a)); \ 59 } while (0) 60 61 /* 62 * In-memory representation of archive member(object). 63 */ 64 struct ar_obj { 65 char *name; /* member name */ 66 void *maddr; /* mmap start address */ 67 uid_t uid; /* user id */ 68 gid_t gid; /* group id */ 69 mode_t md; /* octal file permissions */ 70 size_t size; /* member size */ 71 time_t mtime; /* modification time */ 72 int fd; /* file descriptor */ 73 dev_t dev; /* inode's device */ 74 ino_t ino; /* inode's number */ 75 76 TAILQ_ENTRY(ar_obj) objs; 77 }; 78 79 /* 80 * Structure encapsulates the "global" data for "ar" program. 81 */ 82 struct bsdar { 83 const char *filename; /* archive name. */ 84 const char *addlib; /* target of ADDLIB. */ 85 const char *posarg; /* position arg for modifiers -a, -b. */ 86 char mode; /* program mode */ 87 int options; /* command line options */ 88 89 const char *progname; /* program name */ 90 int argc; 91 char **argv; 92 93 /* 94 * Fields for the archive string table. 95 */ 96 char *as; /* buffer for archive string table. */ 97 size_t as_sz; /* current size of as table. */ 98 size_t as_cap; /* capacity of as table buffer. */ 99 100 /* 101 * Fields for the archive symbol table. 102 */ 103 uint32_t s_cnt; /* current number of symbols. */ 104 uint32_t *s_so; /* symbol offset table. */ 105 size_t s_so_cap; /* capacity of so table buffer. */ 106 char *s_sn; /* symbol name table */ 107 size_t s_sn_cap; /* capacity of sn table buffer. */ 108 size_t s_sn_sz; /* current size of sn table. */ 109 /* Current member's offset (relative to the end of pseudo members.) */ 110 off_t rela_off; 111 112 TAILQ_HEAD(, ar_obj) v_obj; /* object(member) list */ 113 }; 114 115 void bsdar_errc(struct bsdar *, int _eval, int _code, 116 const char *fmt, ...) __dead2; 117 void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); 118 void ar_mode_d(struct bsdar *bsdar); 119 void ar_mode_m(struct bsdar *bsdar); 120 void ar_mode_p(struct bsdar *bsdar); 121 void ar_mode_q(struct bsdar *bsdar); 122 void ar_mode_r(struct bsdar *bsdar); 123 void ar_mode_s(struct bsdar *bsdar); 124 void ar_mode_t(struct bsdar *bsdar); 125 void ar_mode_x(struct bsdar *bsdar); 126 void ar_mode_A(struct bsdar *bsdar); 127 void ar_mode_script(struct bsdar *ar); 128