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