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