blob: f4edeb087c2cf7aa8661d06687e58aadb4170b07 [file] [log] [blame]
Eric Andersen86ab8a32000-06-02 03:21:42 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Mini ar implementation for busybox
Eric Andersen86ab8a32000-06-02 03:21:42 +00004 *
5 * Copyright (C) 2000 by Glenn McGrath
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 *
Eric Andersen86ab8a32000-06-02 03:21:42 +00007 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen86ab8a32000-06-02 03:21:42 +000010 *
Alexander Shishkin535584c2010-03-15 15:38:09 +010011 * Archive creation support:
12 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
13 * Written by Alexander Shishkin.
14 *
Glenn L McGrath930453b2004-01-04 10:28:22 +000015 * There is no single standard to adhere to so ar may not portable
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000016 * between different systems
17 * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html
Eric Andersen86ab8a32000-06-02 03:21:42 +000018 */
Denys Vlasenkof6beef62013-11-14 11:39:00 +010019//config:config AR
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020020//config: bool "ar (9.5 kb)"
Denys Vlasenkof6beef62013-11-14 11:39:00 +010021//config: default n # needs to be improved to be able to replace binutils ar
22//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: ar is an archival utility program used to create, modify, and
24//config: extract contents from archives. In practice, it is used exclusively
25//config: for object module archives used by compilers.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010026//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Unless you have a specific application which requires ar, you should
28//config: probably say N here: most compilers come with their own ar utility.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010029//config:
30//config:config FEATURE_AR_LONG_FILENAMES
Denys Vlasenkof5604222017-01-10 14:58:54 +010031//config: bool "Support long filenames (not needed for debs)"
Denys Vlasenkof6beef62013-11-14 11:39:00 +010032//config: default y
33//config: depends on AR
34//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: By default the ar format can only store the first 15 characters
36//config: of the filename, this option removes that limitation.
37//config: It supports the GNU ar long filename method which moves multiple long
38//config: filenames into a the data section of a new ar entry.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010039//config:
40//config:config FEATURE_AR_CREATE
41//config: bool "Support archive creation"
42//config: default y
43//config: depends on AR
44//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020045//config: This enables archive creation (-c and -r) with busybox ar.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010046
Denys Vlasenko36184a42013-11-14 09:54:24 +010047//applet:IF_AR(APPLET(ar, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020048
Denys Vlasenko66620fa2013-11-14 09:53:52 +010049//kbuild:lib-$(CONFIG_AR) += ar.o
50
Pere Orga1f4447b2011-03-27 22:40:30 +020051//usage:#define ar_trivial_usage
52//usage: "[-o] [-v] [-p] [-t] [-x] ARCHIVE FILES"
53//usage:#define ar_full_usage "\n\n"
54//usage: "Extract or list FILES from an ar archive\n"
Pere Orga1f4447b2011-03-27 22:40:30 +020055//usage: "\n -o Preserve original dates"
56//usage: "\n -p Extract to stdout"
57//usage: "\n -t List"
58//usage: "\n -x Extract"
59//usage: "\n -v Verbose"
60
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000061#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020062#include "bb_archive.h"
Alexander Shishkin535584c2010-03-15 15:38:09 +010063#include "ar.h"
64
65#if ENABLE_FEATURE_AR_CREATE
66/* filter out entries with same names as specified on the command line */
67static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
68{
69 if (find_list_entry(handle->accept, handle->file_header->name))
70 return EXIT_FAILURE;
71
72 return EXIT_SUCCESS;
73}
74
75static void output_ar_header(archive_handle_t *handle)
76{
77 /* GNU ar 2.19.51.0.14 creates malformed archives
78 * if input files are >10G. It also truncates files >4GB
79 * (uses "size mod 4G"). We abort in this case:
80 * We could add support for up to 10G files, but this is unlikely to be useful.
81 * Note that unpacking side limits all fields to "unsigned int" data type,
82 * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
83 */
84 enum {
85 /* for 2nd field: mtime */
86 MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
87 /* for last field: filesize */
88 MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
89 };
90
91 struct file_header_t *fh = handle->file_header;
92
93 if (handle->offset & 1) {
94 xwrite(handle->src_fd, "\n", 1);
95 handle->offset++;
96 }
97
98 /* Careful! The widths should be exact. Fields must be separated */
99 if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
100 bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
101 }
102 fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
103 fh->name,
104 (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
105 fh->uid > 99999 ? 0 : (int)fh->uid,
106 fh->gid > 99999 ? 0 : (int)fh->gid,
107 (int)fh->mode & 07777777,
108 fh->size
109 );
110
111 handle->offset += AR_HEADER_LEN;
112}
113
114/*
Marek Polacekb0b88842011-04-16 17:33:43 +0200115 * when replacing files in an existing archive, copy from the
Alexander Shishkin535584c2010-03-15 15:38:09 +0100116 * original archive those files that are to be left intact
117 */
118static void FAST_FUNC copy_data(archive_handle_t *handle)
119{
120 archive_handle_t *out_handle = handle->ar__out;
121 struct file_header_t *fh = handle->file_header;
122
123 out_handle->file_header = fh;
124 output_ar_header(out_handle);
125
126 bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
127 out_handle->offset += fh->size;
128}
129
130static int write_ar_header(archive_handle_t *handle)
131{
132 char *fn;
133 char fn_h[17]; /* 15 + "/" + NUL */
134 struct stat st;
135 int fd;
136
137 fn = llist_pop(&handle->accept);
138 if (!fn)
139 return -1;
140
141 xstat(fn, &st);
142
143 handle->file_header->mtime = st.st_mtime;
144 handle->file_header->uid = st.st_uid;
145 handle->file_header->gid = st.st_gid;
146 handle->file_header->mode = st.st_mode;
147 handle->file_header->size = st.st_size;
148 handle->file_header->name = fn_h;
149//TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
150 sprintf(fn_h, "%.15s/", bb_basename(fn));
151
152 output_ar_header(handle);
153
154 fd = xopen(fn, O_RDONLY);
155 bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
156 close(fd);
157 handle->offset += st.st_size;
158
159 return 0;
160}
161
162static int write_ar_archive(archive_handle_t *handle)
163{
164 struct stat st;
165 archive_handle_t *out_handle;
166
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200167 xfstat(handle->src_fd, &st, handle->ar__name);
Alexander Shishkin535584c2010-03-15 15:38:09 +0100168
169 /* if archive exists, create a new handle for output.
170 * we create it in place of the old one.
171 */
172 if (st.st_size != 0) {
173 out_handle = init_handle();
174 xunlink(handle->ar__name);
175 out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
176 out_handle->accept = handle->accept;
177 } else {
178 out_handle = handle;
179 }
180
181 handle->ar__out = out_handle;
182
183 xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
184 out_handle->offset += AR_MAGIC_LEN + 1;
185
186 /* skip to the end of the archive if we have to append stuff */
187 if (st.st_size != 0) {
188 handle->filter = filter_replaceable;
189 handle->action_data = copy_data;
190 unpack_ar_archive(handle);
191 }
192
193 while (write_ar_header(out_handle) == 0)
194 continue;
195
196 /* optional, since we exit right after we return */
197 if (ENABLE_FEATURE_CLEAN_UP) {
198 close(handle->src_fd);
199 if (out_handle->src_fd != handle->src_fd)
200 close(out_handle->src_fd);
201 }
202
203 return EXIT_SUCCESS;
204}
205#endif /* FEATURE_AR_CREATE */
Eric Andersen86ab8a32000-06-02 03:21:42 +0000206
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000207static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000208{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 const char *mode = bb_mode_string(file_header->mode);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000210 char *mtime;
211
212 mtime = ctime(&file_header->mtime);
213 mtime[16] = ' ';
214 memmove(&mtime[17], &mtime[20], 4);
215 mtime[21] = '\0';
Alexander Shishkin535584c2010-03-15 15:38:09 +0100216 printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
217 (int)file_header->uid, (int)file_header->gid,
218 file_header->size,
219 &mtime[4], file_header->name
220 );
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000221}
222
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200223#define AR_OPT_VERBOSE (1 << 0)
224#define AR_OPT_PRESERVE_DATE (1 << 1)
Alexander Shishkin535584c2010-03-15 15:38:09 +0100225/* "ar r" implies create, but warns about it. c suppresses warning.
226 * bbox accepts but ignores it: */
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200227#define AR_OPT_CREATE (1 << 2)
Alexander Shishkin535584c2010-03-15 15:38:09 +0100228
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200229#define AR_CMD_PRINT (1 << 3)
230#define FIRST_CMD AR_CMD_PRINT
231#define AR_CMD_LIST (1 << 4)
232#define AR_CMD_EXTRACT (1 << 5)
233#define AR_CMD_INSERT (1 << 6)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000234
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000235int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100236int ar_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000237{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000238 archive_handle_t *archive_handle;
Alexander Shishkin535584c2010-03-15 15:38:09 +0100239 unsigned opt, t;
Glenn L McGrath930453b2004-01-04 10:28:22 +0000240
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000241 archive_handle = init_handle();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000242
Denys Vlasenkodd5a4022017-08-04 16:46:17 +0200243 /* prepend '-' to the first argument if required */
244 if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0')
245 argv[1] = xasprintf("-%s", argv[1]);
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200246 opt = getopt32(argv, "^"
247 "voc""ptx"IF_FEATURE_AR_CREATE("r")
248 "\0"
249 /* -1: at least one arg is reqd */
250 /* one of p,t,x[,r] is required */
251 "-1:p:t:x"IF_FEATURE_AR_CREATE(":r")
252 );
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100253 argv += optind;
Glenn L McGrath930453b2004-01-04 10:28:22 +0000254
Alexander Shishkin535584c2010-03-15 15:38:09 +0100255 t = opt / FIRST_CMD;
256 if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
257 bb_show_usage();
258
259 if (opt & AR_CMD_PRINT) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000260 archive_handle->action_data = data_extract_to_stdout;
261 }
Alexander Shishkin535584c2010-03-15 15:38:09 +0100262 if (opt & AR_CMD_LIST) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000263 archive_handle->action_header = header_list;
264 }
Alexander Shishkin535584c2010-03-15 15:38:09 +0100265 if (opt & AR_CMD_EXTRACT) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000266 archive_handle->action_data = data_extract_all;
267 }
268 if (opt & AR_OPT_PRESERVE_DATE) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200269 archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
Glenn L McGrath930453b2004-01-04 10:28:22 +0000270 }
271 if (opt & AR_OPT_VERBOSE) {
272 archive_handle->action_header = header_verbose_list_ar;
273 }
Alexander Shishkin535584c2010-03-15 15:38:09 +0100274#if ENABLE_FEATURE_AR_CREATE
275 archive_handle->ar__name = *argv;
276#endif
277 archive_handle->src_fd = xopen(*argv++,
278 (opt & AR_CMD_INSERT)
279 ? O_RDWR | O_CREAT
280 : O_RDONLY
281 );
Glenn L McGrath930453b2004-01-04 10:28:22 +0000282
Nguyễn Thái Ngọc Duy36dcf4e2010-04-15 16:55:04 -0700283 if (*argv)
284 archive_handle->filter = filter_accept_list;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100285 while (*argv) {
Alexander Shishkin535584c2010-03-15 15:38:09 +0100286 llist_add_to_end(&archive_handle->accept, *argv++);
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000287 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000288
Alexander Shishkin535584c2010-03-15 15:38:09 +0100289#if ENABLE_FEATURE_AR_CREATE
290 if (opt & AR_CMD_INSERT)
291 return write_ar_archive(archive_handle);
292#endif
293
Denis Vlasenko0381d422008-07-10 23:06:00 +0000294 unpack_ar_archive(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000295
Matt Kraai3e856ce2000-12-01 02:55:13 +0000296 return EXIT_SUCCESS;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000297}