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