[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini ar implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2000 by Glenn McGrath
|
---|
| 6 | *
|
---|
| 7 | * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
|
---|
| 8 | *
|
---|
[2725] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 10 | *
|
---|
[2725] | 11 | * Archive creation support:
|
---|
| 12 | * Copyright (C) 2010 Nokia Corporation. All rights reserved.
|
---|
| 13 | * Written by Alexander Shishkin.
|
---|
| 14 | *
|
---|
[821] | 15 | * There is no single standard to adhere to so ar may not portable
|
---|
| 16 | * between different systems
|
---|
| 17 | * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html
|
---|
| 18 | */
|
---|
| 19 |
|
---|
[3232] | 20 | //usage:#define ar_trivial_usage
|
---|
| 21 | //usage: "[-o] [-v] [-p] [-t] [-x] ARCHIVE FILES"
|
---|
| 22 | //usage:#define ar_full_usage "\n\n"
|
---|
| 23 | //usage: "Extract or list FILES from an ar archive\n"
|
---|
| 24 | //usage: "\n -o Preserve original dates"
|
---|
| 25 | //usage: "\n -p Extract to stdout"
|
---|
| 26 | //usage: "\n -t List"
|
---|
| 27 | //usage: "\n -x Extract"
|
---|
| 28 | //usage: "\n -v Verbose"
|
---|
| 29 |
|
---|
[1765] | 30 | #include "libbb.h"
|
---|
[3232] | 31 | #include "bb_archive.h"
|
---|
[2725] | 32 | #include "ar.h"
|
---|
[821] | 33 |
|
---|
[2725] | 34 | #if ENABLE_FEATURE_AR_CREATE
|
---|
| 35 | /* filter out entries with same names as specified on the command line */
|
---|
| 36 | static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
|
---|
[821] | 37 | {
|
---|
[2725] | 38 | if (find_list_entry(handle->accept, handle->file_header->name))
|
---|
| 39 | return EXIT_FAILURE;
|
---|
| 40 |
|
---|
| 41 | return EXIT_SUCCESS;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | static void output_ar_header(archive_handle_t *handle)
|
---|
| 45 | {
|
---|
| 46 | /* GNU ar 2.19.51.0.14 creates malformed archives
|
---|
| 47 | * if input files are >10G. It also truncates files >4GB
|
---|
| 48 | * (uses "size mod 4G"). We abort in this case:
|
---|
| 49 | * We could add support for up to 10G files, but this is unlikely to be useful.
|
---|
| 50 | * Note that unpacking side limits all fields to "unsigned int" data type,
|
---|
| 51 | * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
|
---|
| 52 | */
|
---|
| 53 | enum {
|
---|
| 54 | /* for 2nd field: mtime */
|
---|
| 55 | MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
|
---|
| 56 | /* for last field: filesize */
|
---|
| 57 | MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | struct file_header_t *fh = handle->file_header;
|
---|
| 61 |
|
---|
| 62 | if (handle->offset & 1) {
|
---|
| 63 | xwrite(handle->src_fd, "\n", 1);
|
---|
| 64 | handle->offset++;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /* Careful! The widths should be exact. Fields must be separated */
|
---|
| 68 | if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
|
---|
| 69 | bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
|
---|
| 70 | }
|
---|
| 71 | fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
|
---|
| 72 | fh->name,
|
---|
| 73 | (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
|
---|
| 74 | fh->uid > 99999 ? 0 : (int)fh->uid,
|
---|
| 75 | fh->gid > 99999 ? 0 : (int)fh->gid,
|
---|
| 76 | (int)fh->mode & 07777777,
|
---|
| 77 | fh->size
|
---|
| 78 | );
|
---|
| 79 |
|
---|
| 80 | handle->offset += AR_HEADER_LEN;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /*
|
---|
[3232] | 84 | * when replacing files in an existing archive, copy from the
|
---|
[2725] | 85 | * original archive those files that are to be left intact
|
---|
| 86 | */
|
---|
| 87 | static void FAST_FUNC copy_data(archive_handle_t *handle)
|
---|
| 88 | {
|
---|
| 89 | archive_handle_t *out_handle = handle->ar__out;
|
---|
| 90 | struct file_header_t *fh = handle->file_header;
|
---|
| 91 |
|
---|
| 92 | out_handle->file_header = fh;
|
---|
| 93 | output_ar_header(out_handle);
|
---|
| 94 |
|
---|
| 95 | bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
|
---|
| 96 | out_handle->offset += fh->size;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | static int write_ar_header(archive_handle_t *handle)
|
---|
| 100 | {
|
---|
| 101 | char *fn;
|
---|
| 102 | char fn_h[17]; /* 15 + "/" + NUL */
|
---|
| 103 | struct stat st;
|
---|
| 104 | int fd;
|
---|
| 105 |
|
---|
| 106 | fn = llist_pop(&handle->accept);
|
---|
| 107 | if (!fn)
|
---|
| 108 | return -1;
|
---|
| 109 |
|
---|
| 110 | xstat(fn, &st);
|
---|
| 111 |
|
---|
| 112 | handle->file_header->mtime = st.st_mtime;
|
---|
| 113 | handle->file_header->uid = st.st_uid;
|
---|
| 114 | handle->file_header->gid = st.st_gid;
|
---|
| 115 | handle->file_header->mode = st.st_mode;
|
---|
| 116 | handle->file_header->size = st.st_size;
|
---|
| 117 | handle->file_header->name = fn_h;
|
---|
| 118 | //TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
|
---|
| 119 | sprintf(fn_h, "%.15s/", bb_basename(fn));
|
---|
| 120 |
|
---|
| 121 | output_ar_header(handle);
|
---|
| 122 |
|
---|
| 123 | fd = xopen(fn, O_RDONLY);
|
---|
| 124 | bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
|
---|
| 125 | close(fd);
|
---|
| 126 | handle->offset += st.st_size;
|
---|
| 127 |
|
---|
| 128 | return 0;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static int write_ar_archive(archive_handle_t *handle)
|
---|
| 132 | {
|
---|
| 133 | struct stat st;
|
---|
| 134 | archive_handle_t *out_handle;
|
---|
| 135 |
|
---|
| 136 | xfstat(handle->src_fd, &st, handle->ar__name);
|
---|
| 137 |
|
---|
| 138 | /* if archive exists, create a new handle for output.
|
---|
| 139 | * we create it in place of the old one.
|
---|
| 140 | */
|
---|
| 141 | if (st.st_size != 0) {
|
---|
| 142 | out_handle = init_handle();
|
---|
| 143 | xunlink(handle->ar__name);
|
---|
| 144 | out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
|
---|
| 145 | out_handle->accept = handle->accept;
|
---|
| 146 | } else {
|
---|
| 147 | out_handle = handle;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | handle->ar__out = out_handle;
|
---|
| 151 |
|
---|
| 152 | xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
|
---|
| 153 | out_handle->offset += AR_MAGIC_LEN + 1;
|
---|
| 154 |
|
---|
| 155 | /* skip to the end of the archive if we have to append stuff */
|
---|
| 156 | if (st.st_size != 0) {
|
---|
| 157 | handle->filter = filter_replaceable;
|
---|
| 158 | handle->action_data = copy_data;
|
---|
| 159 | unpack_ar_archive(handle);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | while (write_ar_header(out_handle) == 0)
|
---|
| 163 | continue;
|
---|
| 164 |
|
---|
| 165 | /* optional, since we exit right after we return */
|
---|
| 166 | if (ENABLE_FEATURE_CLEAN_UP) {
|
---|
| 167 | close(handle->src_fd);
|
---|
| 168 | if (out_handle->src_fd != handle->src_fd)
|
---|
| 169 | close(out_handle->src_fd);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | return EXIT_SUCCESS;
|
---|
| 173 | }
|
---|
| 174 | #endif /* FEATURE_AR_CREATE */
|
---|
| 175 |
|
---|
| 176 | static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
|
---|
| 177 | {
|
---|
[821] | 178 | const char *mode = bb_mode_string(file_header->mode);
|
---|
| 179 | char *mtime;
|
---|
| 180 |
|
---|
| 181 | mtime = ctime(&file_header->mtime);
|
---|
| 182 | mtime[16] = ' ';
|
---|
| 183 | memmove(&mtime[17], &mtime[20], 4);
|
---|
| 184 | mtime[21] = '\0';
|
---|
[2725] | 185 | printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
|
---|
| 186 | (int)file_header->uid, (int)file_header->gid,
|
---|
| 187 | file_header->size,
|
---|
| 188 | &mtime[4], file_header->name
|
---|
| 189 | );
|
---|
[821] | 190 | }
|
---|
| 191 |
|
---|
[2725] | 192 | #define AR_OPT_VERBOSE (1 << 0)
|
---|
| 193 | #define AR_OPT_PRESERVE_DATE (1 << 1)
|
---|
| 194 | /* "ar r" implies create, but warns about it. c suppresses warning.
|
---|
| 195 | * bbox accepts but ignores it: */
|
---|
| 196 | #define AR_OPT_CREATE (1 << 2)
|
---|
[821] | 197 |
|
---|
[2725] | 198 | #define AR_CMD_PRINT (1 << 3)
|
---|
| 199 | #define FIRST_CMD AR_CMD_PRINT
|
---|
| 200 | #define AR_CMD_LIST (1 << 4)
|
---|
| 201 | #define AR_CMD_EXTRACT (1 << 5)
|
---|
| 202 | #define AR_CMD_INSERT (1 << 6)
|
---|
| 203 |
|
---|
| 204 | int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 205 | int ar_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 206 | {
|
---|
| 207 | archive_handle_t *archive_handle;
|
---|
[2725] | 208 | unsigned opt, t;
|
---|
[821] | 209 |
|
---|
| 210 | archive_handle = init_handle();
|
---|
| 211 |
|
---|
[2725] | 212 | /* --: prepend '-' to the first argument if required */
|
---|
| 213 | /* -1: at least one param is reqd */
|
---|
| 214 | /* one of p,t,x[,r] is required */
|
---|
| 215 | opt_complementary = "--:-1:p:t:x"IF_FEATURE_AR_CREATE(":r");
|
---|
| 216 | opt = getopt32(argv, "voc""ptx"IF_FEATURE_AR_CREATE("r"));
|
---|
| 217 | argv += optind;
|
---|
[821] | 218 |
|
---|
[2725] | 219 | t = opt / FIRST_CMD;
|
---|
| 220 | if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
|
---|
| 221 | bb_show_usage();
|
---|
| 222 |
|
---|
| 223 | if (opt & AR_CMD_PRINT) {
|
---|
[821] | 224 | archive_handle->action_data = data_extract_to_stdout;
|
---|
| 225 | }
|
---|
[2725] | 226 | if (opt & AR_CMD_LIST) {
|
---|
[821] | 227 | archive_handle->action_header = header_list;
|
---|
| 228 | }
|
---|
[2725] | 229 | if (opt & AR_CMD_EXTRACT) {
|
---|
[821] | 230 | archive_handle->action_data = data_extract_all;
|
---|
| 231 | }
|
---|
| 232 | if (opt & AR_OPT_PRESERVE_DATE) {
|
---|
[2725] | 233 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
|
---|
[821] | 234 | }
|
---|
| 235 | if (opt & AR_OPT_VERBOSE) {
|
---|
| 236 | archive_handle->action_header = header_verbose_list_ar;
|
---|
| 237 | }
|
---|
[2725] | 238 | #if ENABLE_FEATURE_AR_CREATE
|
---|
| 239 | archive_handle->ar__name = *argv;
|
---|
| 240 | #endif
|
---|
| 241 | archive_handle->src_fd = xopen(*argv++,
|
---|
| 242 | (opt & AR_CMD_INSERT)
|
---|
| 243 | ? O_RDWR | O_CREAT
|
---|
| 244 | : O_RDONLY
|
---|
| 245 | );
|
---|
[821] | 246 |
|
---|
[2725] | 247 | if (*argv)
|
---|
[821] | 248 | archive_handle->filter = filter_accept_list;
|
---|
[2725] | 249 | while (*argv) {
|
---|
| 250 | llist_add_to_end(&archive_handle->accept, *argv++);
|
---|
[821] | 251 | }
|
---|
| 252 |
|
---|
[2725] | 253 | #if ENABLE_FEATURE_AR_CREATE
|
---|
| 254 | if (opt & AR_CMD_INSERT)
|
---|
| 255 | return write_ar_archive(archive_handle);
|
---|
| 256 | #endif
|
---|
[821] | 257 |
|
---|
[2725] | 258 | unpack_ar_archive(archive_handle);
|
---|
[821] | 259 |
|
---|
| 260 | return EXIT_SUCCESS;
|
---|
| 261 | }
|
---|