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