Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * Mini ar implementation for busybox |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2000 by Glenn McGrath |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 7 | * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar. |
| 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 10 | * |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 11 | * Archive creation support: |
| 12 | * Copyright (C) 2010 Nokia Corporation. All rights reserved. |
| 13 | * Written by Alexander Shishkin. |
| 14 | * |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 15 | * There is no single standard to adhere to so ar may not portable |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 16 | * between different systems |
| 17 | * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 18 | */ |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 19 | |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 20 | //config:config AR |
| 21 | //config: bool "ar" |
| 22 | //config: default n # needs to be improved to be able to replace binutils ar |
| 23 | //config: help |
| 24 | //config: ar is an archival utility program used to create, modify, and |
Denys Vlasenko | db70033 | 2015-10-25 20:36:03 +0100 | [diff] [blame] | 25 | //config: extract contents from archives. In practice, it is used exclusively |
| 26 | //config: for object module archives used by compilers. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 27 | //config: |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 28 | //config: On an x86 system, the ar applet adds about 1K. |
| 29 | //config: |
| 30 | //config: Unless you have a specific application which requires ar, you should |
Denys Vlasenko | db70033 | 2015-10-25 20:36:03 +0100 | [diff] [blame] | 31 | //config: probably say N here: most compilers come with their own ar utility. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 32 | //config: |
| 33 | //config:config FEATURE_AR_LONG_FILENAMES |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 34 | //config: bool "Support long filenames (not needed for debs)" |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 35 | //config: default y |
| 36 | //config: depends on AR |
| 37 | //config: help |
| 38 | //config: By default the ar format can only store the first 15 characters |
| 39 | //config: of the filename, this option removes that limitation. |
| 40 | //config: It supports the GNU ar long filename method which moves multiple long |
| 41 | //config: filenames into a the data section of a new ar entry. |
| 42 | //config: |
| 43 | //config:config FEATURE_AR_CREATE |
| 44 | //config: bool "Support archive creation" |
| 45 | //config: default y |
| 46 | //config: depends on AR |
| 47 | //config: help |
| 48 | //config: This enables archive creation (-c and -r) with busybox ar. |
| 49 | |
Denys Vlasenko | 36184a4 | 2013-11-14 09:54:24 +0100 | [diff] [blame] | 50 | //applet:IF_AR(APPLET(ar, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 51 | //kbuild:lib-$(CONFIG_AR) += ar.o |
| 52 | |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 53 | //usage:#define ar_trivial_usage |
| 54 | //usage: "[-o] [-v] [-p] [-t] [-x] ARCHIVE FILES" |
| 55 | //usage:#define ar_full_usage "\n\n" |
| 56 | //usage: "Extract or list FILES from an ar archive\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 57 | //usage: "\n -o Preserve original dates" |
| 58 | //usage: "\n -p Extract to stdout" |
| 59 | //usage: "\n -t List" |
| 60 | //usage: "\n -x Extract" |
| 61 | //usage: "\n -v Verbose" |
| 62 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 63 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 64 | #include "bb_archive.h" |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 65 | #include "ar.h" |
| 66 | |
| 67 | #if ENABLE_FEATURE_AR_CREATE |
| 68 | /* filter out entries with same names as specified on the command line */ |
| 69 | static char FAST_FUNC filter_replaceable(archive_handle_t *handle) |
| 70 | { |
| 71 | if (find_list_entry(handle->accept, handle->file_header->name)) |
| 72 | return EXIT_FAILURE; |
| 73 | |
| 74 | return EXIT_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | static void output_ar_header(archive_handle_t *handle) |
| 78 | { |
| 79 | /* GNU ar 2.19.51.0.14 creates malformed archives |
| 80 | * if input files are >10G. It also truncates files >4GB |
| 81 | * (uses "size mod 4G"). We abort in this case: |
| 82 | * We could add support for up to 10G files, but this is unlikely to be useful. |
| 83 | * Note that unpacking side limits all fields to "unsigned int" data type, |
| 84 | * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1. |
| 85 | */ |
| 86 | enum { |
| 87 | /* for 2nd field: mtime */ |
| 88 | MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1, |
| 89 | /* for last field: filesize */ |
| 90 | MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1, |
| 91 | }; |
| 92 | |
| 93 | struct file_header_t *fh = handle->file_header; |
| 94 | |
| 95 | if (handle->offset & 1) { |
| 96 | xwrite(handle->src_fd, "\n", 1); |
| 97 | handle->offset++; |
| 98 | } |
| 99 | |
| 100 | /* Careful! The widths should be exact. Fields must be separated */ |
| 101 | if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) { |
| 102 | bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name); |
| 103 | } |
| 104 | fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n", |
| 105 | fh->name, |
| 106 | (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime, |
| 107 | fh->uid > 99999 ? 0 : (int)fh->uid, |
| 108 | fh->gid > 99999 ? 0 : (int)fh->gid, |
| 109 | (int)fh->mode & 07777777, |
| 110 | fh->size |
| 111 | ); |
| 112 | |
| 113 | handle->offset += AR_HEADER_LEN; |
| 114 | } |
| 115 | |
| 116 | /* |
Marek Polacek | b0b8884 | 2011-04-16 17:33:43 +0200 | [diff] [blame] | 117 | * when replacing files in an existing archive, copy from the |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 118 | * original archive those files that are to be left intact |
| 119 | */ |
| 120 | static void FAST_FUNC copy_data(archive_handle_t *handle) |
| 121 | { |
| 122 | archive_handle_t *out_handle = handle->ar__out; |
| 123 | struct file_header_t *fh = handle->file_header; |
| 124 | |
| 125 | out_handle->file_header = fh; |
| 126 | output_ar_header(out_handle); |
| 127 | |
| 128 | bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size); |
| 129 | out_handle->offset += fh->size; |
| 130 | } |
| 131 | |
| 132 | static int write_ar_header(archive_handle_t *handle) |
| 133 | { |
| 134 | char *fn; |
| 135 | char fn_h[17]; /* 15 + "/" + NUL */ |
| 136 | struct stat st; |
| 137 | int fd; |
| 138 | |
| 139 | fn = llist_pop(&handle->accept); |
| 140 | if (!fn) |
| 141 | return -1; |
| 142 | |
| 143 | xstat(fn, &st); |
| 144 | |
| 145 | handle->file_header->mtime = st.st_mtime; |
| 146 | handle->file_header->uid = st.st_uid; |
| 147 | handle->file_header->gid = st.st_gid; |
| 148 | handle->file_header->mode = st.st_mode; |
| 149 | handle->file_header->size = st.st_size; |
| 150 | handle->file_header->name = fn_h; |
| 151 | //TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES... |
| 152 | sprintf(fn_h, "%.15s/", bb_basename(fn)); |
| 153 | |
| 154 | output_ar_header(handle); |
| 155 | |
| 156 | fd = xopen(fn, O_RDONLY); |
| 157 | bb_copyfd_exact_size(fd, handle->src_fd, st.st_size); |
| 158 | close(fd); |
| 159 | handle->offset += st.st_size; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int write_ar_archive(archive_handle_t *handle) |
| 165 | { |
| 166 | struct stat st; |
| 167 | archive_handle_t *out_handle; |
| 168 | |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 169 | xfstat(handle->src_fd, &st, handle->ar__name); |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 170 | |
| 171 | /* if archive exists, create a new handle for output. |
| 172 | * we create it in place of the old one. |
| 173 | */ |
| 174 | if (st.st_size != 0) { |
| 175 | out_handle = init_handle(); |
| 176 | xunlink(handle->ar__name); |
| 177 | out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC); |
| 178 | out_handle->accept = handle->accept; |
| 179 | } else { |
| 180 | out_handle = handle; |
| 181 | } |
| 182 | |
| 183 | handle->ar__out = out_handle; |
| 184 | |
| 185 | xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1); |
| 186 | out_handle->offset += AR_MAGIC_LEN + 1; |
| 187 | |
| 188 | /* skip to the end of the archive if we have to append stuff */ |
| 189 | if (st.st_size != 0) { |
| 190 | handle->filter = filter_replaceable; |
| 191 | handle->action_data = copy_data; |
| 192 | unpack_ar_archive(handle); |
| 193 | } |
| 194 | |
| 195 | while (write_ar_header(out_handle) == 0) |
| 196 | continue; |
| 197 | |
| 198 | /* optional, since we exit right after we return */ |
| 199 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 200 | close(handle->src_fd); |
| 201 | if (out_handle->src_fd != handle->src_fd) |
| 202 | close(out_handle->src_fd); |
| 203 | } |
| 204 | |
| 205 | return EXIT_SUCCESS; |
| 206 | } |
| 207 | #endif /* FEATURE_AR_CREATE */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 209 | static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 210 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 211 | const char *mode = bb_mode_string(file_header->mode); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 212 | char *mtime; |
| 213 | |
| 214 | mtime = ctime(&file_header->mtime); |
| 215 | mtime[16] = ' '; |
| 216 | memmove(&mtime[17], &mtime[20], 4); |
| 217 | mtime[21] = '\0'; |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 218 | printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1], |
| 219 | (int)file_header->uid, (int)file_header->gid, |
| 220 | file_header->size, |
| 221 | &mtime[4], file_header->name |
| 222 | ); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 225 | #define AR_OPT_VERBOSE (1 << 0) |
| 226 | #define AR_OPT_PRESERVE_DATE (1 << 1) |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 227 | /* "ar r" implies create, but warns about it. c suppresses warning. |
| 228 | * bbox accepts but ignores it: */ |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 229 | #define AR_OPT_CREATE (1 << 2) |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 230 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 231 | #define AR_CMD_PRINT (1 << 3) |
| 232 | #define FIRST_CMD AR_CMD_PRINT |
| 233 | #define AR_CMD_LIST (1 << 4) |
| 234 | #define AR_CMD_EXTRACT (1 << 5) |
| 235 | #define AR_CMD_INSERT (1 << 6) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 236 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 237 | int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 238 | int ar_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 239 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 240 | archive_handle_t *archive_handle; |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 241 | unsigned opt, t; |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 242 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 243 | archive_handle = init_handle(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 244 | |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 245 | /* --: prepend '-' to the first argument if required */ |
| 246 | /* -1: at least one param is reqd */ |
| 247 | /* one of p,t,x[,r] is required */ |
| 248 | opt_complementary = "--:-1:p:t:x"IF_FEATURE_AR_CREATE(":r"); |
| 249 | opt = getopt32(argv, "voc""ptx"IF_FEATURE_AR_CREATE("r")); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 250 | argv += optind; |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 251 | |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 252 | t = opt / FIRST_CMD; |
| 253 | if (t & (t-1)) /* more than one of p,t,x[,r] are specified */ |
| 254 | bb_show_usage(); |
| 255 | |
| 256 | if (opt & AR_CMD_PRINT) { |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 257 | archive_handle->action_data = data_extract_to_stdout; |
| 258 | } |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 259 | if (opt & AR_CMD_LIST) { |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 260 | archive_handle->action_header = header_list; |
| 261 | } |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 262 | if (opt & AR_CMD_EXTRACT) { |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 263 | archive_handle->action_data = data_extract_all; |
| 264 | } |
| 265 | if (opt & AR_OPT_PRESERVE_DATE) { |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 266 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE; |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 267 | } |
| 268 | if (opt & AR_OPT_VERBOSE) { |
| 269 | archive_handle->action_header = header_verbose_list_ar; |
| 270 | } |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 271 | #if ENABLE_FEATURE_AR_CREATE |
| 272 | archive_handle->ar__name = *argv; |
| 273 | #endif |
| 274 | archive_handle->src_fd = xopen(*argv++, |
| 275 | (opt & AR_CMD_INSERT) |
| 276 | ? O_RDWR | O_CREAT |
| 277 | : O_RDONLY |
| 278 | ); |
Glenn L McGrath | 930453b | 2004-01-04 10:28:22 +0000 | [diff] [blame] | 279 | |
Nguyễn Thái Ngọc Duy | 36dcf4e | 2010-04-15 16:55:04 -0700 | [diff] [blame] | 280 | if (*argv) |
| 281 | archive_handle->filter = filter_accept_list; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 282 | while (*argv) { |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 283 | llist_add_to_end(&archive_handle->accept, *argv++); |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 284 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 285 | |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 286 | #if ENABLE_FEATURE_AR_CREATE |
| 287 | if (opt & AR_CMD_INSERT) |
| 288 | return write_ar_archive(archive_handle); |
| 289 | #endif |
| 290 | |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 291 | unpack_ar_archive(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 292 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 293 | return EXIT_SUCCESS; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 294 | } |