blob: 320cbae727febe10828342c5fdc1fb302556cc9f [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000051#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020052#include "bb_archive.h"
Joachim Nilssonbd46f642020-05-09 13:31:33 +020053#include "ar_.h"
Alexander Shishkin535584c2010-03-15 15:38:09 +010054
55#if ENABLE_FEATURE_AR_CREATE
56/* filter out entries with same names as specified on the command line */
57static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
58{
59 if (find_list_entry(handle->accept, handle->file_header->name))
60 return EXIT_FAILURE;
61
62 return EXIT_SUCCESS;
63}
64
65static void output_ar_header(archive_handle_t *handle)
66{
67 /* GNU ar 2.19.51.0.14 creates malformed archives
68 * if input files are >10G. It also truncates files >4GB
69 * (uses "size mod 4G"). We abort in this case:
70 * We could add support for up to 10G files, but this is unlikely to be useful.
71 * Note that unpacking side limits all fields to "unsigned int" data type,
72 * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
73 */
74 enum {
75 /* for 2nd field: mtime */
76 MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
77 /* for last field: filesize */
78 MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
79 };
80
81 struct file_header_t *fh = handle->file_header;
82
83 if (handle->offset & 1) {
84 xwrite(handle->src_fd, "\n", 1);
85 handle->offset++;
86 }
87
88 /* Careful! The widths should be exact. Fields must be separated */
89 if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
90 bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
91 }
92 fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
93 fh->name,
94 (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
95 fh->uid > 99999 ? 0 : (int)fh->uid,
96 fh->gid > 99999 ? 0 : (int)fh->gid,
97 (int)fh->mode & 07777777,
98 fh->size
99 );
100
101 handle->offset += AR_HEADER_LEN;
102}
103
104/*
Marek Polacekb0b88842011-04-16 17:33:43 +0200105 * when replacing files in an existing archive, copy from the
Alexander Shishkin535584c2010-03-15 15:38:09 +0100106 * original archive those files that are to be left intact
107 */
108static void FAST_FUNC copy_data(archive_handle_t *handle)
109{
110 archive_handle_t *out_handle = handle->ar__out;
111 struct file_header_t *fh = handle->file_header;
112
113 out_handle->file_header = fh;
114 output_ar_header(out_handle);
115
116 bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
117 out_handle->offset += fh->size;
118}
119
120static int write_ar_header(archive_handle_t *handle)
121{
122 char *fn;
123 char fn_h[17]; /* 15 + "/" + NUL */
124 struct stat st;
125 int fd;
126
127 fn = llist_pop(&handle->accept);
128 if (!fn)
129 return -1;
130
131 xstat(fn, &st);
132
133 handle->file_header->mtime = st.st_mtime;
134 handle->file_header->uid = st.st_uid;
135 handle->file_header->gid = st.st_gid;
136 handle->file_header->mode = st.st_mode;
137 handle->file_header->size = st.st_size;
138 handle->file_header->name = fn_h;
139//TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
140 sprintf(fn_h, "%.15s/", bb_basename(fn));
141
142 output_ar_header(handle);
143
144 fd = xopen(fn, O_RDONLY);
145 bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
146 close(fd);
147 handle->offset += st.st_size;
148
149 return 0;
150}
151
152static int write_ar_archive(archive_handle_t *handle)
153{
154 struct stat st;
155 archive_handle_t *out_handle;
156
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200157 xfstat(handle->src_fd, &st, handle->ar__name);
Alexander Shishkin535584c2010-03-15 15:38:09 +0100158
159 /* if archive exists, create a new handle for output.
160 * we create it in place of the old one.
161 */
162 if (st.st_size != 0) {
163 out_handle = init_handle();
164 xunlink(handle->ar__name);
165 out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
166 out_handle->accept = handle->accept;
167 } else {
168 out_handle = handle;
169 }
170
171 handle->ar__out = out_handle;
172
173 xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
174 out_handle->offset += AR_MAGIC_LEN + 1;
175
176 /* skip to the end of the archive if we have to append stuff */
177 if (st.st_size != 0) {
178 handle->filter = filter_replaceable;
179 handle->action_data = copy_data;
180 unpack_ar_archive(handle);
181 }
182
183 while (write_ar_header(out_handle) == 0)
184 continue;
185
186 /* optional, since we exit right after we return */
187 if (ENABLE_FEATURE_CLEAN_UP) {
188 close(handle->src_fd);
189 if (out_handle->src_fd != handle->src_fd)
190 close(out_handle->src_fd);
191 }
192
193 return EXIT_SUCCESS;
194}
195#endif /* FEATURE_AR_CREATE */
Eric Andersen86ab8a32000-06-02 03:21:42 +0000196
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000197static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000198{
Denys Vlasenko59ac4672021-09-17 01:13:58 +0200199 char mode[12];
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000200 char *mtime;
201
Denys Vlasenko59ac4672021-09-17 01:13:58 +0200202 bb_mode_string(mode, file_header->mode);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000203 mtime = ctime(&file_header->mtime);
204 mtime[16] = ' ';
205 memmove(&mtime[17], &mtime[20], 4);
206 mtime[21] = '\0';
Alexander Shishkin535584c2010-03-15 15:38:09 +0100207 printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
208 (int)file_header->uid, (int)file_header->gid,
209 file_header->size,
210 &mtime[4], file_header->name
211 );
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000212}
213
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100214//usage:#define ar_trivial_usage
215//usage: "x|p|t"IF_FEATURE_AR_CREATE("|r")" [-ov] ARCHIVE [FILE]..."
216//usage:#define ar_full_usage "\n\n"
217//usage: "Extract or list FILEs from an ar archive"IF_FEATURE_AR_CREATE(", or create it")"\n"
218//usage: "\n x Extract"
219//usage: "\n p Extract to stdout"
220//usage: "\n t List"
221//usage: IF_FEATURE_AR_CREATE(
222//usage: "\n r Create"
223//usage: )
224//usage: "\n -o Restore mtime"
225//usage: "\n -v Verbose"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000226
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000227int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100228int ar_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000229{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000230 archive_handle_t *archive_handle;
Alexander Shishkin535584c2010-03-15 15:38:09 +0100231 unsigned opt, t;
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100232 enum {
233 OPT_VERBOSE = (1 << 0),
234 OPT_PRESERVE_DATE = (1 << 1),
235 /* "ar r" implies create, but warns about it. c suppresses warning.
236 * bbox accepts but ignores it: */
237 OPT_CREATE = (1 << 2),
238 CMD_PRINT = (1 << 3),
239 FIRST_CMD = CMD_PRINT,
240 CMD_LIST = (1 << 4),
241 CMD_EXTRACT = (1 << 5),
242 CMD_INSERT = ((1 << 6) * ENABLE_FEATURE_AR_CREATE),
243 };
Glenn L McGrath930453b2004-01-04 10:28:22 +0000244
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000245 archive_handle = init_handle();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000246
Denys Vlasenkodd5a4022017-08-04 16:46:17 +0200247 /* prepend '-' to the first argument if required */
248 if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0')
249 argv[1] = xasprintf("-%s", argv[1]);
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200250 opt = getopt32(argv, "^"
251 "voc""ptx"IF_FEATURE_AR_CREATE("r")
252 "\0"
253 /* -1: at least one arg is reqd */
254 /* one of p,t,x[,r] is required */
255 "-1:p:t:x"IF_FEATURE_AR_CREATE(":r")
256 );
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100257 argv += optind;
Glenn L McGrath930453b2004-01-04 10:28:22 +0000258
Alexander Shishkin535584c2010-03-15 15:38:09 +0100259 t = opt / FIRST_CMD;
260 if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
261 bb_show_usage();
262
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100263 if (opt & CMD_PRINT) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000264 archive_handle->action_data = data_extract_to_stdout;
265 }
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100266 if (opt & CMD_LIST) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000267 archive_handle->action_header = header_list;
268 }
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100269 if (opt & CMD_EXTRACT) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000270 archive_handle->action_data = data_extract_all;
271 }
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100272 if (opt & OPT_PRESERVE_DATE) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200273 archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
Glenn L McGrath930453b2004-01-04 10:28:22 +0000274 }
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100275 if (opt & OPT_VERBOSE) {
Glenn L McGrath930453b2004-01-04 10:28:22 +0000276 archive_handle->action_header = header_verbose_list_ar;
277 }
Alexander Shishkin535584c2010-03-15 15:38:09 +0100278#if ENABLE_FEATURE_AR_CREATE
279 archive_handle->ar__name = *argv;
280#endif
281 archive_handle->src_fd = xopen(*argv++,
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100282 (opt & CMD_INSERT)
Alexander Shishkin535584c2010-03-15 15:38:09 +0100283 ? O_RDWR | O_CREAT
284 : O_RDONLY
285 );
Glenn L McGrath930453b2004-01-04 10:28:22 +0000286
Nguyễn Thái Ngọc Duy36dcf4e2010-04-15 16:55:04 -0700287 if (*argv)
288 archive_handle->filter = filter_accept_list;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100289 while (*argv) {
Alexander Shishkin535584c2010-03-15 15:38:09 +0100290 llist_add_to_end(&archive_handle->accept, *argv++);
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000291 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000292
Alexander Shishkin535584c2010-03-15 15:38:09 +0100293#if ENABLE_FEATURE_AR_CREATE
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100294 if (opt & CMD_INSERT)
Alexander Shishkin535584c2010-03-15 15:38:09 +0100295 return write_ar_archive(archive_handle);
296#endif
297
Denis Vlasenko0381d422008-07-10 23:06:00 +0000298 unpack_ar_archive(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000299
Matt Kraai3e856ce2000-12-01 02:55:13 +0000300 return EXIT_SUCCESS;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000301}