aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shishkin <virtuoso@slind.org>2010-03-15 15:38:09 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-15 15:38:09 +0100
commit535584c750820dd6c36011697f9ef89fe0a0698c (patch)
tree6c682a7a6ca7d740529201c759fa4e64d9622797
parent814da220a5d451c036c9871094253366372676e0 (diff)
downloadbusybox-w32-535584c750820dd6c36011697f9ef89fe0a0698c.tar.gz
busybox-w32-535584c750820dd6c36011697f9ef89fe0a0698c.tar.bz2
busybox-w32-535584c750820dd6c36011697f9ef89fe0a0698c.zip
ar: add archive creation support
function old new delta ar_main 184 542 +358 output_ar_header - 166 +166 copy_data - 54 +54 filter_replaceable - 19 +19 get_header_ar 409 414 +5 header_verbose_list_ar 85 88 +3 static.msg_unsupported_err 28 - -28 ------------------------------------------------------------------------------ (add/remove: 3/1 grow/shrink: 3/0 up/down: 605/-28) Total: 577 bytes Signed-off-by: Alexander Shishkin <virtuoso@slind.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/Config.in7
-rw-r--r--archival/ar.c221
-rw-r--r--archival/libunarchive/get_header_ar.c13
-rw-r--r--archival/libunarchive/unpack_ar_archive.c7
-rw-r--r--include/ar.h26
-rw-r--r--include/unarchive.h4
-rwxr-xr-xtestsuite/ar.tests27
7 files changed, 263 insertions, 42 deletions
diff --git a/archival/Config.in b/archival/Config.in
index fb79c7bca..9e2e88c01 100644
--- a/archival/Config.in
+++ b/archival/Config.in
@@ -62,6 +62,13 @@ config FEATURE_AR_LONG_FILENAMES
62 It supports the GNU ar long filename method which moves multiple long 62 It supports the GNU ar long filename method which moves multiple long
63 filenames into a the data section of a new ar entry. 63 filenames into a the data section of a new ar entry.
64 64
65config FEATURE_AR_CREATE
66 bool "Support archive creation"
67 default n
68 depends on AR
69 help
70 This enables archive creation (-c and -r) with busybox ar.
71
65config BUNZIP2 72config BUNZIP2
66 bool "bunzip2" 73 bool "bunzip2"
67 default n 74 default n
diff --git a/archival/ar.c b/archival/ar.c
index fd7f8e4db..90397474d 100644
--- a/archival/ar.c
+++ b/archival/ar.c
@@ -8,6 +8,10 @@
8 * 8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * 10 *
11 * Archive creation support:
12 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
13 * Written by Alexander Shishkin.
14 *
11 * There is no single standard to adhere to so ar may not portable 15 * There is no single standard to adhere to so ar may not portable
12 * between different systems 16 * between different systems
13 * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html 17 * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html
@@ -15,6 +19,150 @@
15 19
16#include "libbb.h" 20#include "libbb.h"
17#include "unarchive.h" 21#include "unarchive.h"
22#include "ar.h"
23
24#if ENABLE_FEATURE_AR_CREATE
25/* filter out entries with same names as specified on the command line */
26static char FAST_FUNC filter_replaceable(archive_handle_t *handle)
27{
28 if (find_list_entry(handle->accept, handle->file_header->name))
29 return EXIT_FAILURE;
30
31 return EXIT_SUCCESS;
32}
33
34static void output_ar_header(archive_handle_t *handle)
35{
36 /* GNU ar 2.19.51.0.14 creates malformed archives
37 * if input files are >10G. It also truncates files >4GB
38 * (uses "size mod 4G"). We abort in this case:
39 * We could add support for up to 10G files, but this is unlikely to be useful.
40 * Note that unpacking side limits all fields to "unsigned int" data type,
41 * and treats "all ones" as an error indicator. Thus max we allow here is UINT_MAX-1.
42 */
43 enum {
44 /* for 2nd field: mtime */
45 MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
46 /* for last field: filesize */
47 MAX10CHARS = UINT_MAX > 0xffffffff ? (unsigned)9999999999 : UINT_MAX-1,
48 };
49
50 struct file_header_t *fh = handle->file_header;
51
52 if (handle->offset & 1) {
53 xwrite(handle->src_fd, "\n", 1);
54 handle->offset++;
55 }
56
57 /* Careful! The widths should be exact. Fields must be separated */
58 if (sizeof(off_t) > 4 && fh->size > (off_t)MAX10CHARS) {
59 bb_error_msg_and_die("'%s' is bigger than ar can handle", fh->name);
60 }
61 fdprintf(handle->src_fd, "%-16.16s%-12lu%-6u%-6u%-8o%-10"OFF_FMT"u`\n",
62 fh->name,
63 (sizeof(time_t) > 4 && fh->mtime > MAX11CHARS) ? (long)0 : (long)fh->mtime,
64 fh->uid > 99999 ? 0 : (int)fh->uid,
65 fh->gid > 99999 ? 0 : (int)fh->gid,
66 (int)fh->mode & 07777777,
67 fh->size
68 );
69
70 handle->offset += AR_HEADER_LEN;
71}
72
73/*
74 * when replacing files in an existing archive, copy from the the
75 * original archive those files that are to be left intact
76 */
77static void FAST_FUNC copy_data(archive_handle_t *handle)
78{
79 archive_handle_t *out_handle = handle->ar__out;
80 struct file_header_t *fh = handle->file_header;
81
82 out_handle->file_header = fh;
83 output_ar_header(out_handle);
84
85 bb_copyfd_exact_size(handle->src_fd, out_handle->src_fd, fh->size);
86 out_handle->offset += fh->size;
87}
88
89static int write_ar_header(archive_handle_t *handle)
90{
91 char *fn;
92 char fn_h[17]; /* 15 + "/" + NUL */
93 struct stat st;
94 int fd;
95
96 fn = llist_pop(&handle->accept);
97 if (!fn)
98 return -1;
99
100 xstat(fn, &st);
101
102 handle->file_header->mtime = st.st_mtime;
103 handle->file_header->uid = st.st_uid;
104 handle->file_header->gid = st.st_gid;
105 handle->file_header->mode = st.st_mode;
106 handle->file_header->size = st.st_size;
107 handle->file_header->name = fn_h;
108//TODO: if ENABLE_FEATURE_AR_LONG_FILENAMES...
109 sprintf(fn_h, "%.15s/", bb_basename(fn));
110
111 output_ar_header(handle);
112
113 fd = xopen(fn, O_RDONLY);
114 bb_copyfd_exact_size(fd, handle->src_fd, st.st_size);
115 close(fd);
116 handle->offset += st.st_size;
117
118 return 0;
119}
120
121static int write_ar_archive(archive_handle_t *handle)
122{
123 struct stat st;
124 archive_handle_t *out_handle;
125
126 if (fstat(handle->src_fd, &st) == -1)
127 bb_simple_perror_msg_and_die(handle->ar__name);
128
129 /* if archive exists, create a new handle for output.
130 * we create it in place of the old one.
131 */
132 if (st.st_size != 0) {
133 out_handle = init_handle();
134 xunlink(handle->ar__name);
135 out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
136 out_handle->accept = handle->accept;
137 } else {
138 out_handle = handle;
139 }
140
141 handle->ar__out = out_handle;
142
143 xwrite(out_handle->src_fd, AR_MAGIC "\n", AR_MAGIC_LEN + 1);
144 out_handle->offset += AR_MAGIC_LEN + 1;
145
146 /* skip to the end of the archive if we have to append stuff */
147 if (st.st_size != 0) {
148 handle->filter = filter_replaceable;
149 handle->action_data = copy_data;
150 unpack_ar_archive(handle);
151 }
152
153 while (write_ar_header(out_handle) == 0)
154 continue;
155
156 /* optional, since we exit right after we return */
157 if (ENABLE_FEATURE_CLEAN_UP) {
158 close(handle->src_fd);
159 if (out_handle->src_fd != handle->src_fd)
160 close(out_handle->src_fd);
161 }
162
163 return EXIT_SUCCESS;
164}
165#endif /* FEATURE_AR_CREATE */
18 166
19static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header) 167static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
20{ 168{
@@ -25,41 +173,51 @@ static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
25 mtime[16] = ' '; 173 mtime[16] = ' ';
26 memmove(&mtime[17], &mtime[20], 4); 174 memmove(&mtime[17], &mtime[20], 4);
27 mtime[21] = '\0'; 175 mtime[21] = '\0';
28 printf("%s %d/%d%7d %s %s\n", &mode[1], file_header->uid, file_header->gid, 176 printf("%s %u/%u%7"OFF_FMT"u %s %s\n", &mode[1],
29 (int) file_header->size, &mtime[4], file_header->name); 177 (int)file_header->uid, (int)file_header->gid,
178 file_header->size,
179 &mtime[4], file_header->name
180 );
30} 181}
31 182
32#define AR_CTX_PRINT 0x01 183#define AR_OPT_VERBOSE (1 << 0)
33#define AR_CTX_LIST 0x02 184#define AR_OPT_PRESERVE_DATE (1 << 1)
34#define AR_CTX_EXTRACT 0x04 185/* "ar r" implies create, but warns about it. c suppresses warning.
35#define AR_OPT_PRESERVE_DATE 0x08 186 * bbox accepts but ignores it: */
36#define AR_OPT_VERBOSE 0x10 187#define AR_OPT_CREATE (1 << 2)
37#define AR_OPT_CREATE 0x20 188
38#define AR_OPT_INSERT 0x40 189#define AR_CMD_PRINT (1 << 3)
190#define FIRST_CMD AR_CMD_PRINT
191#define AR_CMD_LIST (1 << 4)
192#define AR_CMD_EXTRACT (1 << 5)
193#define AR_CMD_INSERT (1 << 6)
39 194
40int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 195int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41int ar_main(int argc UNUSED_PARAM, char **argv) 196int ar_main(int argc UNUSED_PARAM, char **argv)
42{ 197{
43 static const char msg_unsupported_err[] ALIGN1 =
44 "archive %s is not supported";
45
46 archive_handle_t *archive_handle; 198 archive_handle_t *archive_handle;
47 unsigned opt; 199 unsigned opt, t;
48 200
49 archive_handle = init_handle(); 201 archive_handle = init_handle();
50 202
51 /* Prepend '-' to the first argument if required */ 203 /* --: prepend '-' to the first argument if required */
52 opt_complementary = "--:p:t:x:-1:p--tx:t--px:x--pt"; 204 /* -1: at least one param is reqd */
53 opt = getopt32(argv, "ptxovcr"); 205 /* one of p,t,x[,r] is required */
206 opt_complementary = "--:-1:p:t:x"IF_FEATURE_AR_CREATE(":r");
207 opt = getopt32(argv, "voc""ptx"IF_FEATURE_AR_CREATE("r"));
54 argv += optind; 208 argv += optind;
55 209
56 if (opt & AR_CTX_PRINT) { 210 t = opt / FIRST_CMD;
211 if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
212 bb_show_usage();
213
214 if (opt & AR_CMD_PRINT) {
57 archive_handle->action_data = data_extract_to_stdout; 215 archive_handle->action_data = data_extract_to_stdout;
58 } 216 }
59 if (opt & AR_CTX_LIST) { 217 if (opt & AR_CMD_LIST) {
60 archive_handle->action_header = header_list; 218 archive_handle->action_header = header_list;
61 } 219 }
62 if (opt & AR_CTX_EXTRACT) { 220 if (opt & AR_CMD_EXTRACT) {
63 archive_handle->action_data = data_extract_all; 221 archive_handle->action_data = data_extract_all;
64 } 222 }
65 if (opt & AR_OPT_PRESERVE_DATE) { 223 if (opt & AR_OPT_PRESERVE_DATE) {
@@ -68,20 +226,25 @@ int ar_main(int argc UNUSED_PARAM, char **argv)
68 if (opt & AR_OPT_VERBOSE) { 226 if (opt & AR_OPT_VERBOSE) {
69 archive_handle->action_header = header_verbose_list_ar; 227 archive_handle->action_header = header_verbose_list_ar;
70 } 228 }
71 if (opt & AR_OPT_CREATE) { 229#if ENABLE_FEATURE_AR_CREATE
72 bb_error_msg_and_die(msg_unsupported_err, "creation"); 230 archive_handle->ar__name = *argv;
73 } 231#endif
74 if (opt & AR_OPT_INSERT) { 232 archive_handle->src_fd = xopen(*argv++,
75 bb_error_msg_and_die(msg_unsupported_err, "insertion"); 233 (opt & AR_CMD_INSERT)
76 } 234 ? O_RDWR | O_CREAT
77 235 : O_RDONLY
78 archive_handle->src_fd = xopen(*argv++, O_RDONLY); 236 );
79 237
238 archive_handle->filter = filter_accept_list;
80 while (*argv) { 239 while (*argv) {
81 archive_handle->filter = filter_accept_list; 240 llist_add_to_end(&archive_handle->accept, *argv++);
82 llist_add_to(&archive_handle->accept, *argv++);
83 } 241 }
84 242
243#if ENABLE_FEATURE_AR_CREATE
244 if (opt & AR_CMD_INSERT)
245 return write_ar_archive(archive_handle);
246#endif
247
85 unpack_ar_archive(archive_handle); 248 unpack_ar_archive(archive_handle);
86 249
87 return EXIT_SUCCESS; 250 return EXIT_SUCCESS;
diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c
index 2f38279cc..dbc5ec004 100644
--- a/archival/libunarchive/get_header_ar.c
+++ b/archival/libunarchive/get_header_ar.c
@@ -6,6 +6,7 @@
6 6
7#include "libbb.h" 7#include "libbb.h"
8#include "unarchive.h" 8#include "unarchive.h"
9#include "ar.h"
9 10
10static unsigned read_num(const char *str, int base) 11static unsigned read_num(const char *str, int base)
11{ 12{
@@ -23,15 +24,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
23 unsigned size; 24 unsigned size;
24 union { 25 union {
25 char raw[60]; 26 char raw[60];
26 struct { 27 struct ar_header formatted;
27 char name[16];
28 char date[12];
29 char uid[6];
30 char gid[6];
31 char mode[8];
32 char size[10];
33 char magic[2];
34 } formatted;
35 } ar; 28 } ar;
36#if ENABLE_FEATURE_AR_LONG_FILENAMES 29#if ENABLE_FEATURE_AR_LONG_FILENAMES
37 static char *ar_long_names; 30 static char *ar_long_names;
@@ -55,13 +48,13 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
55 } 48 }
56 archive_handle->offset += 60; 49 archive_handle->offset += 60;
57 50
58 /* align the headers based on the header magic */
59 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n') 51 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
60 bb_error_msg_and_die("invalid ar header"); 52 bb_error_msg_and_die("invalid ar header");
61 53
62 /* FIXME: more thorough routine would be in order here 54 /* FIXME: more thorough routine would be in order here
63 * (we have something like that in tar) 55 * (we have something like that in tar)
64 * but for now we are lax. */ 56 * but for now we are lax. */
57 ar.formatted.magic[0] = '\0'; /* else 4G-2 file will have size="4294967294`\n..." */
65 typed->size = size = read_num(ar.formatted.size, 10); 58 typed->size = size = read_num(ar.formatted.size, 10);
66 59
67 /* special filenames have '/' as the first character */ 60 /* special filenames have '/' as the first character */
diff --git a/archival/libunarchive/unpack_ar_archive.c b/archival/libunarchive/unpack_ar_archive.c
index dc2eec223..300d10e48 100644
--- a/archival/libunarchive/unpack_ar_archive.c
+++ b/archival/libunarchive/unpack_ar_archive.c
@@ -5,16 +5,17 @@
5 5
6#include "libbb.h" 6#include "libbb.h"
7#include "unarchive.h" 7#include "unarchive.h"
8#include "ar.h"
8 9
9void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive) 10void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive)
10{ 11{
11 char magic[7]; 12 char magic[7];
12 13
13 xread(ar_archive->src_fd, magic, 7); 14 xread(ar_archive->src_fd, magic, AR_MAGIC_LEN);
14 if (strncmp(magic, "!<arch>", 7) != 0) { 15 if (strncmp(magic, AR_MAGIC, AR_MAGIC_LEN) != 0) {
15 bb_error_msg_and_die("invalid ar magic"); 16 bb_error_msg_and_die("invalid ar magic");
16 } 17 }
17 ar_archive->offset += 7; 18 ar_archive->offset += AR_MAGIC_LEN;
18 19
19 while (get_header_ar(ar_archive) == EXIT_SUCCESS) 20 while (get_header_ar(ar_archive) == EXIT_SUCCESS)
20 continue; 21 continue;
diff --git a/include/ar.h b/include/ar.h
new file mode 100644
index 000000000..26678895a
--- /dev/null
+++ b/include/ar.h
@@ -0,0 +1,26 @@
1/*
2 * busybox ar archive data structures
3 * Licensed under the GPL v2 or later, see the file LICENSE in this source tree.
4 */
5#ifndef AR_H
6#define AR_H
7
8PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
9
10struct ar_header {
11 char name[16];
12 char date[12];
13 char uid[6];
14 char gid[6];
15 char mode[8];
16 char size[10];
17 char magic[2];
18};
19
20#define AR_HEADER_LEN sizeof(struct ar_header)
21#define AR_MAGIC "!<arch>"
22#define AR_MAGIC_LEN 7
23
24POP_SAVED_FUNCTION_VISIBILITY
25
26#endif
diff --git a/include/unarchive.h b/include/unarchive.h
index 8bfc92cbe..92ebd6565 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -73,6 +73,10 @@ typedef struct archive_handle_t {
73 /* Contains the handle to a sub archive */ 73 /* Contains the handle to a sub archive */
74 struct archive_handle_t *dpkg__sub_archive; 74 struct archive_handle_t *dpkg__sub_archive;
75#endif 75#endif
76#if ENABLE_FEATURE_AR_CREATE
77 const char *ar__name;
78 struct archive_handle_t *ar__out;
79#endif
76} archive_handle_t; 80} archive_handle_t;
77/* bits in ah_flags */ 81/* bits in ah_flags */
78#define ARCHIVE_RESTORE_DATE (1 << 0) 82#define ARCHIVE_RESTORE_DATE (1 << 0)
diff --git a/testsuite/ar.tests b/testsuite/ar.tests
new file mode 100755
index 000000000..f112eb64a
--- /dev/null
+++ b/testsuite/ar.tests
@@ -0,0 +1,27 @@
1#!/bin/sh
2# Copyright 2010 Nokia Corporation
3# Written by Alexander Shishkin
4# Licensed under GPL v2 or later, see file LICENSE for details
5
6. ./testing.sh
7
8# testing "test name" "command(s)" "expected result" "file input" "stdin"
9
10optional FEATURE_AR_CREATE
11
12rm test.a 2>/dev/null
13testing "ar creates archives" \
14 "ar rc test.a README && ar p test.a README | md5sum" \
15 "$(md5sum <README)\n" \
16 "" \
17 ""
18rm test.a
19
20testing "ar replaces things in archives" \
21 "echo 'blah!' >file1 && echo 'blast!'> file2 && ar cr test.a README file1 file2 && mv file2 file1 && ar cr test.a file1 && ar p test.a file1" \
22 "blast!\n" \
23 "" \
24 ""
25rm test.a
26
27exit $FAILCOUNT