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.
12 #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
13 || ENABLE_FEATURE_SEAMLESS_BZ2 \
14 || ENABLE_FEATURE_SEAMLESS_GZ \
15 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
19 #include "unarchive.h"
22 ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
27 n = read(fd, buf, count);
28 } while (n < 0 && errno == EINTR);
33 /* Suppose that you are a shell. You start child processes.
34 * They work and eventually exit. You want to get user input.
35 * You read stdin. But what happens if last child switched
36 * its stdin into O_NONBLOCK mode?
38 * *** SURPRISE! It will affect the parent too! ***
39 * *** BIG SURPRISE! It stays even after child exits! ***
41 * This is a design bug in UNIX API.
42 * fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
43 * will set nonblocking mode not only on _your_ stdin, but
44 * also on stdin of your parent, etc.
48 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL, 0) | O_NONBLOCK);
49 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
50 * where duping is done implicitly by fork() etc.
53 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK);
54 * (note SETFD, not SETFL!) but such thing doesn't exist.
56 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
57 * require O_NONBLOCK dance at all. Actually, it exists:
58 * n = recv(fd, buf, len, MSG_DONTWAIT);
60 * Enables non-blocking operation; if the operation
61 * would block, EAGAIN is returned."
62 * but recv() works only for sockets!
64 * So far I don't see any good solution, I can only propose
65 * that affected readers should be careful and use this routine,
66 * which detects EAGAIN and uses poll() to wait on the fd.
67 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
69 ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
75 n = safe_read(fd, buf, count);
76 if (n >= 0 || errno != EAGAIN)
78 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
80 pfd[0].events = POLLIN;
81 safe_poll(pfd, 1, -1);
86 * Read all of the supplied buffer from a file.
87 * This does multiple reads as necessary.
88 * Returns the amount read, or -1 on an error.
89 * A short read is returned on an end of file.
91 ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
99 cc = safe_read(fd, buf, len);
103 /* we already have some! */
104 /* user can do another read to know the error code */
107 return cc; /* read() returns -1 on failure. */
111 buf = ((char *)buf) + cc;
119 /* Die with an error message if we can't read the entire buffer. */
120 void FAST_FUNC xread(int fd, void *buf, size_t count)
123 ssize_t size = full_read(fd, buf, count);
124 if ((size_t)size != count)
125 bb_error_msg_and_die("short read");
129 /* Die with an error message if we can't read one character. */
130 unsigned char FAST_FUNC xread_char(int fd)
137 // Reads one line a-la fgets (but doesn't save terminating '\n').
138 // Reads byte-by-byte. Useful when it is important to not read ahead.
139 // Bytes are appended to pfx (which must be malloced, or NULL).
140 char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
143 size_t sz = buf ? strlen(buf) : 0;
144 size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t);
148 if ((size_t)(p - buf) == sz) {
150 buf = xrealloc(buf, sz + 128);
154 /* nonblock_safe_read() because we are used by e.g. shells */
155 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
156 if (p == buf) { /* we read nothing */
170 return xrealloc(buf, p - buf);
173 ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
176 size = full_read(fd, buf, size);
183 ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
185 int fd = open(filename, O_RDONLY);
188 return read_close(fd, buf, size);
192 // Read (potentially big) files in one go. File size is estimated
193 // by stat. Extra '\0' byte is appended.
194 void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
197 size_t size, rd_size, total;
201 to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */
203 /* Estimate file size */
204 st.st_size = 0; /* in case fstat fails, assume 0 */
206 /* /proc/N/stat files report st_size 0 */
207 /* In order to make such files readable, we add small const */
208 size = (st.st_size | 0x3ff) + 1;
215 buf = xrealloc(buf, total + size + 1);
216 rd_size = full_read(fd, buf + total, size);
217 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
222 if (rd_size < size) /* EOF */
224 if (to_read <= rd_size)
227 /* grow by 1/8, but in [1k..64k] bounds */
228 size = ((total / 8) | 0x3ff) + 1;
232 xrealloc(buf, total + 1);
240 #ifdef USING_LSEEK_TO_GET_SIZE
241 /* Alternatively, file size can be obtained by lseek to the end.
242 * The code is slightly bigger. Retained in case fstat approach
243 * will not work for some weird cases (/proc, block devices, etc).
244 * (NB: lseek also can fail to work for some weird files) */
246 // Read (potentially big) files in one go. File size is estimated by
248 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
255 fd = open(filename, O_RDONLY);
259 /* /proc/N/stat files report len 0 here */
260 /* In order to make such files readable, we add small const */
261 size = 0x3ff; /* read only 1k on unseekable files */
262 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
263 if (len != (off_t)-1) {
264 xlseek(fd, 0, SEEK_SET);
265 size = maxsz_p ? *maxsz_p : INT_MAX;
270 buf = xmalloc(size + 1);
271 size = read_close(fd, buf, size);
272 if ((ssize_t)size < 0) {
276 xrealloc(buf, size + 1);
285 // Read (potentially big) files in one go. File size is estimated
287 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
292 fd = open(filename, O_RDONLY);
296 buf = xmalloc_read(fd, maxsz_p);
301 void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
303 void *buf = xmalloc_open_read_close(filename, maxsz_p);
305 bb_perror_msg_and_die("can't read '%s'", filename);
309 int FAST_FUNC open_zipped(const char *fname)
312 return open(fname, O_RDONLY);
314 unsigned char magic[2];
318 USE_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
319 enum { xformer_prog = 0 };
321 enum { xformer = 0 };
322 const char *xformer_prog;
325 fd = open(fname, O_RDONLY);
329 sfx = strrchr(fname, '.');
331 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, ".lzma") == 0)
332 /* .lzma has no header/signature, just trust it */
333 open_transformer(fd, unpack_lzma_stream, "unlzma");
335 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, ".gz") == 0)
336 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, ".bz2") == 0)
338 /* .gz and .bz2 both have 2-byte signature, and their
339 * unpack_XXX_stream want this header skipped. */
340 xread(fd, &magic, 2);
341 #if ENABLE_FEATURE_SEAMLESS_GZ
343 xformer = unpack_gz_stream;
345 xformer_prog = "gunzip";
348 if (!ENABLE_FEATURE_SEAMLESS_GZ
349 || magic[0] != 0x1f || magic[1] != 0x8b
351 if (!ENABLE_FEATURE_SEAMLESS_BZ2
352 || magic[0] != 'B' || magic[1] != 'Z'
354 bb_error_msg_and_die("no gzip"
355 USE_FEATURE_SEAMLESS_BZ2("/bzip2")
359 xformer = unpack_bz2_stream;
361 xformer_prog = "bunzip2";
365 /* NOMMU version of open_transformer execs
366 * an external unzipper that wants
367 * file position at the start of the file */
368 xlseek(fd, 0, SEEK_SET);
371 open_transformer(fd, xformer, xformer_prog);
379 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
384 fd = open_zipped(fname);
388 image = xmalloc_read(fd, maxsz_p);
390 bb_perror_msg("read error from '%s'", fname);