blob: 192f83d6e4e338d4c2572f25958a409aa1b11325 [file] [log] [blame]
Denys Vlasenko19ced5c2010-06-06 21:53:09 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko19ced5c2010-06-06 21:53:09 +02008 */
9#include "libbb.h"
10
11#define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
12 || ENABLE_FEATURE_SEAMLESS_BZ2 \
13 || ENABLE_FEATURE_SEAMLESS_GZ \
14 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
15)
16
17#if ZIPPED
Denys Vlasenko833d4e72010-11-03 02:38:31 +010018# include "archive.h"
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020019#endif
20
21
22/* Suppose that you are a shell. You start child processes.
23 * They work and eventually exit. You want to get user input.
24 * You read stdin. But what happens if last child switched
25 * its stdin into O_NONBLOCK mode?
26 *
27 * *** SURPRISE! It will affect the parent too! ***
28 * *** BIG SURPRISE! It stays even after child exits! ***
29 *
30 * This is a design bug in UNIX API.
31 * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
32 * will set nonblocking mode not only on _your_ stdin, but
33 * also on stdin of your parent, etc.
34 *
35 * In general,
36 * fd2 = dup(fd1);
37 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
38 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
39 * where duping is done implicitly by fork() etc.
40 *
41 * We need
42 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
43 * (note SETFD, not SETFL!) but such thing doesn't exist.
44 *
45 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
46 * require O_NONBLOCK dance at all. Actually, it exists:
47 * n = recv(fd, buf, len, MSG_DONTWAIT);
48 * "MSG_DONTWAIT:
49 * Enables non-blocking operation; if the operation
50 * would block, EAGAIN is returned."
51 * but recv() works only for sockets!
52 *
53 * So far I don't see any good solution, I can only propose
54 * that affected readers should be careful and use this routine,
55 * which detects EAGAIN and uses poll() to wait on the fd.
56 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
57 */
Denys Vlasenko80542ba2011-05-08 21:23:43 +020058ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR)
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020059{
60 struct pollfd pfd[1];
61 ssize_t n;
62
63 while (1) {
Denys Vlasenko80542ba2011-05-08 21:23:43 +020064 n = loop_on_EINTR ? safe_read(fd, buf, count) : read(fd, buf, count);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020065 if (n >= 0 || errno != EAGAIN)
66 return n;
67 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
68 pfd[0].fd = fd;
69 pfd[0].events = POLLIN;
Denys Vlasenko80542ba2011-05-08 21:23:43 +020070 /* note: safe_poll pulls in printf */
71 loop_on_EINTR ? safe_poll(pfd, 1, -1) : poll(pfd, 1, -1);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020072 }
73}
74
75// Reads one line a-la fgets (but doesn't save terminating '\n').
76// Reads byte-by-byte. Useful when it is important to not read ahead.
77// Bytes are appended to pfx (which must be malloced, or NULL).
Denys Vlasenko80c5b682011-05-08 21:21:10 +020078char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020079{
80 char *p;
Denys Vlasenko80c5b682011-05-08 21:21:10 +020081 char *buf = NULL;
82 size_t sz = 0;
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020083 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
84
85 goto jump_in;
Denys Vlasenko80c5b682011-05-08 21:21:10 +020086
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020087 while (sz < maxsz) {
88 if ((size_t)(p - buf) == sz) {
89 jump_in:
90 buf = xrealloc(buf, sz + 128);
91 p = buf + sz;
92 sz += 128;
93 }
Denys Vlasenko80542ba2011-05-08 21:23:43 +020094 if (nonblock_immune_read(fd, p, 1, /*loop_on_EINTR:*/ 1) != 1) {
Denys Vlasenko80c5b682011-05-08 21:21:10 +020095 /* EOF/error */
Denys Vlasenko19ced5c2010-06-06 21:53:09 +020096 if (p == buf) { /* we read nothing */
97 free(buf);
98 return NULL;
99 }
100 break;
101 }
102 if (*p == '\n')
103 break;
104 p++;
105 }
106 *p = '\0';
107 if (maxsz_p)
108 *maxsz_p = p - buf;
109 p++;
110 return xrealloc(buf, p - buf);
111}
112
113// Read (potentially big) files in one go. File size is estimated
114// by stat. Extra '\0' byte is appended.
115void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
116{
117 char *buf;
118 size_t size, rd_size, total;
119 size_t to_read;
120 struct stat st;
121
122 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
123
124 /* Estimate file size */
125 st.st_size = 0; /* in case fstat fails, assume 0 */
126 fstat(fd, &st);
127 /* /proc/N/stat files report st_size 0 */
128 /* In order to make such files readable, we add small const */
129 size = (st.st_size | 0x3ff) + 1;
130
131 total = 0;
132 buf = NULL;
133 while (1) {
134 if (to_read < size)
135 size = to_read;
136 buf = xrealloc(buf, total + size + 1);
137 rd_size = full_read(fd, buf + total, size);
138 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
139 free(buf);
140 return NULL;
141 }
142 total += rd_size;
143 if (rd_size < size) /* EOF */
144 break;
145 if (to_read <= rd_size)
146 break;
147 to_read -= rd_size;
148 /* grow by 1/8, but in [1k..64k] bounds */
149 size = ((total / 8) | 0x3ff) + 1;
150 if (size > 64*1024)
151 size = 64*1024;
152 }
153 buf = xrealloc(buf, total + 1);
154 buf[total] = '\0';
155
156 if (maxsz_p)
157 *maxsz_p = total;
158 return buf;
159}
160
161#ifdef USING_LSEEK_TO_GET_SIZE
162/* Alternatively, file size can be obtained by lseek to the end.
163 * The code is slightly bigger. Retained in case fstat approach
164 * will not work for some weird cases (/proc, block devices, etc).
165 * (NB: lseek also can fail to work for some weird files) */
166
167// Read (potentially big) files in one go. File size is estimated by
168// lseek to end.
169void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
170{
171 char *buf;
172 size_t size;
173 int fd;
174 off_t len;
175
176 fd = open(filename, O_RDONLY);
177 if (fd < 0)
178 return NULL;
179
180 /* /proc/N/stat files report len 0 here */
181 /* In order to make such files readable, we add small const */
182 size = 0x3ff; /* read only 1k on unseekable files */
183 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
184 if (len != (off_t)-1) {
185 xlseek(fd, 0, SEEK_SET);
186 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
187 if (len < size)
188 size = len;
189 }
190
191 buf = xmalloc(size + 1);
192 size = read_close(fd, buf, size);
193 if ((ssize_t)size < 0) {
194 free(buf);
195 return NULL;
196 }
197 buf = xrealloc(buf, size + 1);
198 buf[size] = '\0';
199
200 if (maxsz_p)
201 *maxsz_p = size;
202 return buf;
203}
204#endif
205
206// Read (potentially big) files in one go. File size is estimated
207// by stat.
208void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
209{
210 char *buf;
211 int fd;
212
213 fd = open(filename, O_RDONLY);
214 if (fd < 0)
215 return NULL;
216
217 buf = xmalloc_read(fd, maxsz_p);
218 close(fd);
219 return buf;
220}
221
222/* Die with an error message if we can't read the entire buffer. */
223void FAST_FUNC xread(int fd, void *buf, size_t count)
224{
225 if (count) {
226 ssize_t size = full_read(fd, buf, count);
227 if ((size_t)size != count)
228 bb_error_msg_and_die("short read");
229 }
230}
231
232/* Die with an error message if we can't read one character. */
233unsigned char FAST_FUNC xread_char(int fd)
234{
235 char tmp;
236 xread(fd, &tmp, 1);
237 return tmp;
238}
239
240void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
241{
242 void *buf = xmalloc_open_read_close(filename, maxsz_p);
243 if (!buf)
244 bb_perror_msg_and_die("can't read '%s'", filename);
245 return buf;
246}
247
248/* Used by e.g. rpm which gives us a fd without filename,
249 * thus we can't guess the format from filename's extension.
250 */
251#if ZIPPED
252void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
253{
254 const int fail_if_not_detected = 1;
255 union {
256 uint8_t b[4];
257 uint16_t b16[2];
258 uint32_t b32[1];
259 } magic;
260 int offset = -2;
261# if BB_MMU
262 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
263 enum { xformer_prog = 0 };
264# else
265 enum { xformer = 0 };
266 const char *xformer_prog;
267# endif
268
269 /* .gz and .bz2 both have 2-byte signature, and their
270 * unpack_XXX_stream wants this header skipped. */
Denys Vlasenko894fa0a2010-06-26 05:01:16 +0200271 xread(fd, magic.b16, sizeof(magic.b16[0]));
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200272 if (ENABLE_FEATURE_SEAMLESS_GZ
273 && magic.b16[0] == GZIP_MAGIC
274 ) {
275# if BB_MMU
276 xformer = unpack_gz_stream;
277# else
278 xformer_prog = "gunzip";
279# endif
280 goto found_magic;
281 }
282 if (ENABLE_FEATURE_SEAMLESS_BZ2
283 && magic.b16[0] == BZIP2_MAGIC
284 ) {
285# if BB_MMU
286 xformer = unpack_bz2_stream;
287# else
288 xformer_prog = "bunzip2";
289# endif
290 goto found_magic;
291 }
292 if (ENABLE_FEATURE_SEAMLESS_XZ
293 && magic.b16[0] == XZ_MAGIC1
294 ) {
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200295 offset = -6;
Denys Vlasenko894fa0a2010-06-26 05:01:16 +0200296 xread(fd, magic.b32, sizeof(magic.b32[0]));
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200297 if (magic.b32[0] == XZ_MAGIC2) {
298# if BB_MMU
299 xformer = unpack_xz_stream;
Denys Vlasenko45f66162010-07-01 05:12:28 +0200300 /* unpack_xz_stream wants fd at position 6, no need to seek */
301 //xlseek(fd, offset, SEEK_CUR);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200302# else
303 xformer_prog = "unxz";
304# endif
305 goto found_magic;
306 }
307 }
308
309 /* No known magic seen */
310 if (fail_if_not_detected)
311 bb_error_msg_and_die("no gzip"
312 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
313 IF_FEATURE_SEAMLESS_XZ("/xz")
314 " magic");
315 xlseek(fd, offset, SEEK_CUR);
316 return;
317
318 found_magic:
319# if !BB_MMU
320 /* NOMMU version of open_transformer execs
321 * an external unzipper that wants
322 * file position at the start of the file */
323 xlseek(fd, offset, SEEK_CUR);
324# endif
325 open_transformer(fd, xformer, xformer_prog);
326}
327#endif /* ZIPPED */
328
329int FAST_FUNC open_zipped(const char *fname)
330{
331#if !ZIPPED
332 return open(fname, O_RDONLY);
333#else
334 char *sfx;
335 int fd;
336
337 fd = open(fname, O_RDONLY);
338 if (fd < 0)
339 return fd;
340
341 sfx = strrchr(fname, '.');
342 if (sfx) {
343 sfx++;
344 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
345 /* .lzma has no header/signature, just trust it */
346 open_transformer(fd, unpack_lzma_stream, "unlzma");
347 else
348 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
349 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
350 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
351 ) {
352 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
353 }
354 }
355
356 return fd;
357#endif
358}
359
360void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
361{
362 int fd;
363 char *image;
364
365 fd = open_zipped(fname);
366 if (fd < 0)
367 return NULL;
368
369 image = xmalloc_read(fd, maxsz_p);
370 if (!image)
371 bb_perror_msg("read error from '%s'", fname);
372 close(fd);
373
374 return image;
375}