1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61#include "libbb.h"
62#include "bb_archive.h"
63#include "ar.h"
64
65#if ENABLE_FEATURE_AR_CREATE
66
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
78
79
80
81
82
83
84 enum {
85
86 MAX11CHARS = UINT_MAX > 0xffffffff ? (unsigned)99999999999 : UINT_MAX-1,
87
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
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
115
116
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];
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
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
167 xfstat(handle->src_fd, &st, handle->ar__name);
168
169
170
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
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
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
206
207static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
208{
209 const char *mode = bb_mode_string(file_header->mode);
210 char *mtime;
211
212 mtime = ctime(&file_header->mtime);
213 mtime[16] = ' ';
214 memmove(&mtime[17], &mtime[20], 4);
215 mtime[21] = '\0';
216 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 );
221}
222
223#define AR_OPT_VERBOSE (1 << 0)
224#define AR_OPT_PRESERVE_DATE (1 << 1)
225
226
227#define AR_OPT_CREATE (1 << 2)
228
229#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)
234
235int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
236int ar_main(int argc UNUSED_PARAM, char **argv)
237{
238 archive_handle_t *archive_handle;
239 unsigned opt, t;
240
241 archive_handle = init_handle();
242
243
244 if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0')
245 argv[1] = xasprintf("-%s", argv[1]);
246 opt = getopt32(argv, "^"
247 "voc""ptx"IF_FEATURE_AR_CREATE("r")
248 "\0"
249
250
251 "-1:p:t:x"IF_FEATURE_AR_CREATE(":r")
252 );
253 argv += optind;
254
255 t = opt / FIRST_CMD;
256 if (t & (t-1))
257 bb_show_usage();
258
259 if (opt & AR_CMD_PRINT) {
260 archive_handle->action_data = data_extract_to_stdout;
261 }
262 if (opt & AR_CMD_LIST) {
263 archive_handle->action_header = header_list;
264 }
265 if (opt & AR_CMD_EXTRACT) {
266 archive_handle->action_data = data_extract_all;
267 }
268 if (opt & AR_OPT_PRESERVE_DATE) {
269 archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
270 }
271 if (opt & AR_OPT_VERBOSE) {
272 archive_handle->action_header = header_verbose_list_ar;
273 }
274#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 );
282
283 if (*argv)
284 archive_handle->filter = filter_accept_list;
285 while (*argv) {
286 llist_add_to_end(&archive_handle->accept, *argv++);
287 }
288
289#if ENABLE_FEATURE_AR_CREATE
290 if (opt & AR_CMD_INSERT)
291 return write_ar_archive(archive_handle);
292#endif
293
294 unpack_ar_archive(archive_handle);
295
296 return EXIT_SUCCESS;
297}
298