1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
12 || ENABLE_FEATURE_SEAMLESS_BZ2 \
13 || ENABLE_FEATURE_SEAMLESS_GZ \
14 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
18 # include "unarchive.h"
21 ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
26 n = read(fd, buf, count);
27 } while (n < 0 && errno == EINTR);
32 /* Suppose that you are a shell. You start child processes.
33 * They work and eventually exit. You want to get user input.
34 * You read stdin. But what happens if last child switched
35 * its stdin into O_NONBLOCK mode?
37 * *** SURPRISE! It will affect the parent too! ***
38 * *** BIG SURPRISE! It stays even after child exits! ***
40 * This is a design bug in UNIX API.
41 * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
42 * will set nonblocking mode not only on _your_ stdin, but
43 * also on stdin of your parent, etc.
47 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
48 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
49 * where duping is done implicitly by fork() etc.
52 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
53 * (note SETFD, not SETFL!) but such thing doesn't exist.
55 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
56 * require O_NONBLOCK dance at all. Actually, it exists:
57 * n = recv(fd, buf, len, MSG_DONTWAIT);
59 * Enables non-blocking operation; if the operation
60 * would block, EAGAIN is returned."
61 * but recv() works only for sockets!
63 * So far I don't see any good solution, I can only propose
64 * that affected readers should be careful and use this routine,
65 * which detects EAGAIN and uses poll() to wait on the fd.
66 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
68 ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
74 n = safe_read(fd, buf, count);
75 if (n >= 0 || errno != EAGAIN)
77 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
79 pfd[0].events = POLLIN;
80 safe_poll(pfd, 1, -1);
85 * Read all of the supplied buffer from a file.
86 * This does multiple reads as necessary.
87 * Returns the amount read, or -1 on an error.
88 * A short read is returned on an end of file.
90 ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
98 cc = safe_read(fd, buf, len);
102 /* we already have some! */
103 /* user can do another read to know the error code */
106 return cc; /* read() returns -1 on failure. */
110 buf = ((char *)buf) + cc;
118 /* Die with an error message if we can't read the entire buffer. */
119 void FAST_FUNC xread(int fd, void *buf, size_t count)
122 ssize_t size = full_read(fd, buf, count);
123 if ((size_t)size != count)
124 bb_error_msg_and_die("short read");
128 /* Die with an error message if we can't read one character. */
129 unsigned char FAST_FUNC xread_char(int fd)
136 // Reads one line a-la fgets (but doesn't save terminating '\n').
137 // Reads byte-by-byte. Useful when it is important to not read ahead.
138 // Bytes are appended to pfx (which must be malloced, or NULL).
139 char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
142 size_t sz = buf ? strlen(buf) : 0;
143 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
147 if ((size_t)(p - buf) == sz) {
149 buf = xrealloc(buf, sz + 128);
153 /* nonblock_safe_read() because we are used by e.g. shells */
154 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
155 if (p == buf) { /* we read nothing */
169 return xrealloc(buf, p - buf);
172 ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
175 size = full_read(fd, buf, size);
182 ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
184 int fd = open(filename, O_RDONLY);
187 return read_close(fd, buf, size);
191 // Read (potentially big) files in one go. File size is estimated
192 // by stat. Extra '\0' byte is appended.
193 void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
196 size_t size, rd_size, total;
200 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
202 /* Estimate file size */
203 st.st_size = 0; /* in case fstat fails, assume 0 */
205 /* /proc/N/stat files report st_size 0 */
206 /* In order to make such files readable, we add small const */
207 size = (st.st_size | 0x3ff) + 1;
214 buf = xrealloc(buf, total + size + 1);
215 rd_size = full_read(fd, buf + total, size);
216 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
221 if (rd_size < size) /* EOF */
223 if (to_read <= rd_size)
226 /* grow by 1/8, but in [1k..64k] bounds */
227 size = ((total / 8) | 0x3ff) + 1;
231 buf = xrealloc(buf, total + 1);
239 #ifdef USING_LSEEK_TO_GET_SIZE
240 /* Alternatively, file size can be obtained by lseek to the end.
241 * The code is slightly bigger. Retained in case fstat approach
242 * will not work for some weird cases (/proc, block devices, etc).
243 * (NB: lseek also can fail to work for some weird files) */
245 // Read (potentially big) files in one go. File size is estimated by
247 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
254 fd = open(filename, O_RDONLY);
258 /* /proc/N/stat files report len 0 here */
259 /* In order to make such files readable, we add small const */
260 size = 0x3ff; /* read only 1k on unseekable files */
261 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
262 if (len != (off_t)-1) {
263 xlseek(fd, 0, SEEK_SET);
264 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
269 buf = xmalloc(size + 1);
270 size = read_close(fd, buf, size);
271 if ((ssize_t)size < 0) {
275 buf = xrealloc(buf, size + 1);
284 // Read (potentially big) files in one go. File size is estimated
286 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
291 fd = open(filename, O_RDONLY);
295 buf = xmalloc_read(fd, maxsz_p);
300 void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
302 void *buf = xmalloc_open_read_close(filename, maxsz_p);
304 bb_perror_msg_and_die("can't read '%s'", filename);
309 int FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
311 const int fail_if_not_detected = 1;
312 unsigned char magic[2];
314 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
315 enum { xformer_prog = 0 };
317 enum { xformer = 0 };
318 const char *xformer_prog;
321 /* .gz and .bz2 both have 2-byte signature, and their
322 * unpack_XXX_stream wants this header skipped. */
323 xread(fd, &magic, 2);
324 if (ENABLE_FEATURE_SEAMLESS_GZ
325 && magic[0] == 0x1f && magic[1] == 0x8b
328 xformer = unpack_gz_stream;
330 xformer_prog = "gunzip";
334 if (ENABLE_FEATURE_SEAMLESS_BZ2
335 && magic[0] == 'B' && magic[1] == 'Z'
338 xformer = unpack_bz2_stream;
340 xformer_prog = "bunzip2";
344 // TODO: xz format support. rpm adopted it, "rpm -i FILE.rpm" badly needs this.
345 // Signature: 0xFD, '7', 'z', 'X', 'Z', 0x00
346 // More info at: http://tukaani.org/xz/xz-file-format.txt
348 /* No known magic seen */
349 if (fail_if_not_detected)
350 bb_error_msg_and_die("no gzip"
351 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
353 xlseek(fd, -2, SEEK_CUR);
358 /* NOMMU version of open_transformer execs
359 * an external unzipper that wants
360 * file position at the start of the file */
361 xlseek(fd, -2, SEEK_CUR);
363 open_transformer(fd, xformer, xformer_prog);
369 int FAST_FUNC open_zipped(const char *fname)
372 return open(fname, O_RDONLY);
377 fd = open(fname, O_RDONLY);
381 sfx = strrchr(fname, '.');
383 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, ".lzma") == 0)
384 /* .lzma has no header/signature, just trust it */
385 open_transformer(fd, unpack_lzma_stream, "unlzma");
387 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, ".gz") == 0)
388 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, ".bz2") == 0)
390 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
398 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
403 fd = open_zipped(fname);
407 image = xmalloc_read(fd, maxsz_p);
409 bb_perror_msg("read error from '%s'", fname);