blob: b58982b325646d89c032a6b4e09aad34f37942b1 [file] [log] [blame]
Denis Vlasenkoea620772006-10-14 02:23:43 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000011
12#define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
13 || ENABLE_FEATURE_SEAMLESS_BZ2 \
14 || ENABLE_FEATURE_SEAMLESS_GZ \
15 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
16)
17
18#if ZIPPED
Denis Vlasenkob6052722008-07-10 17:43:01 +000019#include "unarchive.h"
20#endif
Denis Vlasenkoea620772006-10-14 02:23:43 +000021
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000022ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
Denis Vlasenkoea620772006-10-14 02:23:43 +000023{
24 ssize_t n;
25
26 do {
27 n = read(fd, buf, count);
28 } while (n < 0 && errno == EINTR);
29
30 return n;
31}
32
Denis Vlasenkoe376d452008-02-20 22:23:24 +000033/* 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?
37 *
38 * *** SURPRISE! It will affect the parent too! ***
39 * *** BIG SURPRISE! It stays even after child exits! ***
40 *
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.
45 *
46 * In general,
47 * fd2 = dup(fd1);
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.
51 *
52 * We need
53 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK);
54 * (note SETFD, not SETFL!) but such thing doesn't exist.
55 *
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);
59 * "MSG_DONTWAIT:
60 * Enables non-blocking operation; if the operation
61 * would block, EAGAIN is returned."
62 * but recv() works only for sockets!
63 *
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.
Denis Vlasenko081efd12008-02-20 22:57:24 +000067 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
Denis Vlasenkoe376d452008-02-20 22:23:24 +000068 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000069ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
Denis Vlasenkoe376d452008-02-20 22:23:24 +000070{
71 struct pollfd pfd[1];
72 ssize_t n;
73
74 while (1) {
75 n = safe_read(fd, buf, count);
76 if (n >= 0 || errno != EAGAIN)
77 return n;
78 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
79 pfd[0].fd = fd;
80 pfd[0].events = POLLIN;
81 safe_poll(pfd, 1, -1);
82 }
83}
84
Denis Vlasenkoea620772006-10-14 02:23:43 +000085/*
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.
90 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000091ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
Denis Vlasenkoea620772006-10-14 02:23:43 +000092{
93 ssize_t cc;
94 ssize_t total;
95
96 total = 0;
97
98 while (len) {
99 cc = safe_read(fd, buf, len);
100
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000101 if (cc < 0) {
102 if (total) {
103 /* we already have some! */
104 /* user can do another read to know the error code */
105 return total;
106 }
107 return cc; /* read() returns -1 on failure. */
108 }
Denis Vlasenkoea620772006-10-14 02:23:43 +0000109 if (cc == 0)
110 break;
Denis Vlasenkoea620772006-10-14 02:23:43 +0000111 buf = ((char *)buf) + cc;
112 total += cc;
113 len -= cc;
114 }
115
116 return total;
117}
118
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000119/* Die with an error message if we can't read the entire buffer. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000120void FAST_FUNC xread(int fd, void *buf, size_t count)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000121{
122 if (count) {
123 ssize_t size = full_read(fd, buf, count);
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000124 if ((size_t)size != count)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000125 bb_error_msg_and_die("short read");
126 }
127}
128
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000129/* Die with an error message if we can't read one character. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000130unsigned char FAST_FUNC xread_char(int fd)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000131{
132 char tmp;
Denis Vlasenkoea620772006-10-14 02:23:43 +0000133 xread(fd, &tmp, 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000134 return tmp;
135}
136
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000137// 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.
Denis Vlasenkoe376d452008-02-20 22:23:24 +0000139// Bytes are appended to pfx (which must be malloced, or NULL).
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000140char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000141{
142 char *p;
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000143 size_t sz = buf ? strlen(buf) : 0;
Denis Vlasenko9f57cf62009-03-18 17:32:44 +0000144 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000145
146 goto jump_in;
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000147 while (sz < maxsz) {
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000148 if ((size_t)(p - buf) == sz) {
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000149 jump_in:
150 buf = xrealloc(buf, sz + 128);
151 p = buf + sz;
152 sz += 128;
153 }
Denis Vlasenkoe376d452008-02-20 22:23:24 +0000154 /* nonblock_safe_read() because we are used by e.g. shells */
155 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000156 if (p == buf) { /* we read nothing */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000157 free(buf);
158 return NULL;
159 }
160 break;
161 }
162 if (*p == '\n')
163 break;
164 p++;
165 }
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000166 *p = '\0';
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000167 if (maxsz_p)
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000168 *maxsz_p = p - buf;
169 p++;
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000170 return xrealloc(buf, p - buf);
171}
172
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000173ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000174{
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000175 /*int e;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000176 size = full_read(fd, buf, size);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000177 /*e = errno;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000178 close(fd);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000179 /*errno = e;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000180 return size;
181}
182
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000183ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000184{
185 int fd = open(filename, O_RDONLY);
186 if (fd < 0)
187 return fd;
188 return read_close(fd, buf, size);
189}
190
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000191
Denis Vlasenkofe733a92008-06-24 16:08:22 +0000192// Read (potentially big) files in one go. File size is estimated
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000193// by stat. Extra '\0' byte is appended.
Denis Vlasenkob6052722008-07-10 17:43:01 +0000194void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
Denis Vlasenkoea620772006-10-14 02:23:43 +0000195{
196 char *buf;
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000197 size_t size, rd_size, total;
Denis Vlasenkob6052722008-07-10 17:43:01 +0000198 size_t to_read;
Denis Vlasenko0a144622008-04-20 14:45:43 +0000199 struct stat st;
200
Denis Vlasenko9f57cf62009-03-18 17:32:44 +0000201 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
Denis Vlasenko0a144622008-04-20 14:45:43 +0000202
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000203 /* Estimate file size */
204 st.st_size = 0; /* in case fstat fails, assume 0 */
Denis Vlasenko0a144622008-04-20 14:45:43 +0000205 fstat(fd, &st);
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000206 /* /proc/N/stat files report st_size 0 */
Denis Vlasenko0a144622008-04-20 14:45:43 +0000207 /* In order to make such files readable, we add small const */
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000208 size = (st.st_size | 0x3ff) + 1;
209
210 total = 0;
211 buf = NULL;
212 while (1) {
213 if (to_read < size)
214 size = to_read;
215 buf = xrealloc(buf, total + size + 1);
216 rd_size = full_read(fd, buf + total, size);
Denis Vlasenkob6052722008-07-10 17:43:01 +0000217 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000218 free(buf);
219 return NULL;
220 }
221 total += rd_size;
222 if (rd_size < size) /* EOF */
223 break;
Denis Vlasenkob6052722008-07-10 17:43:01 +0000224 if (to_read <= rd_size)
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000225 break;
Denis Vlasenkob6052722008-07-10 17:43:01 +0000226 to_read -= rd_size;
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000227 /* grow by 1/8, but in [1k..64k] bounds */
228 size = ((total / 8) | 0x3ff) + 1;
229 if (size > 64*1024)
230 size = 64*1024;
Denis Vlasenko0a144622008-04-20 14:45:43 +0000231 }
Denys Vlasenko071ede12009-05-15 23:23:23 +0200232 buf = xrealloc(buf, total + 1);
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000233 buf[total] = '\0';
Denis Vlasenko0a144622008-04-20 14:45:43 +0000234
Denis Vlasenkob6052722008-07-10 17:43:01 +0000235 if (maxsz_p)
236 *maxsz_p = total;
Denis Vlasenko0a144622008-04-20 14:45:43 +0000237 return buf;
238}
239
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) */
245
246// Read (potentially big) files in one go. File size is estimated by
247// lseek to end.
Denis Vlasenkob6052722008-07-10 17:43:01 +0000248void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
Denis Vlasenko0a144622008-04-20 14:45:43 +0000249{
250 char *buf;
251 size_t size;
252 int fd;
253 off_t len;
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000254
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000255 fd = open(filename, O_RDONLY);
256 if (fd < 0)
257 return NULL;
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000258
Denis Vlasenkob131b272006-12-17 17:30:01 +0000259 /* /proc/N/stat files report len 0 here */
260 /* In order to make such files readable, we add small const */
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000261 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);
Denis Vlasenko9f57cf62009-03-18 17:32:44 +0000265 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000266 if (len < size)
267 size = len;
268 }
269
Denis Vlasenkob131b272006-12-17 17:30:01 +0000270 buf = xmalloc(size + 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000271 size = read_close(fd, buf, size);
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000272 if ((ssize_t)size < 0) {
273 free(buf);
274 return NULL;
275 }
Denys Vlasenko071ede12009-05-15 23:23:23 +0200276 buf = xrealloc(buf, size + 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000277 buf[size] = '\0';
Denis Vlasenko25cfe492008-04-20 01:27:59 +0000278
Denis Vlasenkob6052722008-07-10 17:43:01 +0000279 if (maxsz_p)
280 *maxsz_p = size;
Denis Vlasenkoea620772006-10-14 02:23:43 +0000281 return buf;
282}
Denis Vlasenko0a144622008-04-20 14:45:43 +0000283#endif
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000284
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000285// Read (potentially big) files in one go. File size is estimated
286// by stat.
Denis Vlasenkob6052722008-07-10 17:43:01 +0000287void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000288{
289 char *buf;
290 int fd;
291
292 fd = open(filename, O_RDONLY);
293 if (fd < 0)
294 return NULL;
295
Denis Vlasenkob6052722008-07-10 17:43:01 +0000296 buf = xmalloc_read(fd, maxsz_p);
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +0000297 close(fd);
298 return buf;
299}
300
Denis Vlasenkob6052722008-07-10 17:43:01 +0000301void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000302{
Denis Vlasenkob6052722008-07-10 17:43:01 +0000303 void *buf = xmalloc_open_read_close(filename, maxsz_p);
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000304 if (!buf)
305 bb_perror_msg_and_die("can't read '%s'", filename);
306 return buf;
307}
Denis Vlasenkob6052722008-07-10 17:43:01 +0000308
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000309int FAST_FUNC open_zipped(const char *fname)
310{
311#if !ZIPPED
312 return open(fname, O_RDONLY);
313#else
314 unsigned char magic[2];
315 char *sfx;
316 int fd;
317#if BB_MMU
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000318 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000319 enum { xformer_prog = 0 };
320#else
321 enum { xformer = 0 };
322 const char *xformer_prog;
323#endif
324
325 fd = open(fname, O_RDONLY);
326 if (fd < 0)
327 return fd;
328
329 sfx = strrchr(fname, '.');
330 if (sfx) {
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");
334 else
335 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, ".gz") == 0)
336 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, ".bz2") == 0)
337 ) {
338 /* .gz and .bz2 both have 2-byte signature, and their
339 * unpack_XXX_stream want this header skipped. */
340 xread(fd, &magic, 2);
Denis Vlasenkoec73d302008-08-05 17:43:29 +0000341#if ENABLE_FEATURE_SEAMLESS_GZ
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000342#if BB_MMU
343 xformer = unpack_gz_stream;
344#else
345 xformer_prog = "gunzip";
346#endif
Denis Vlasenkoec73d302008-08-05 17:43:29 +0000347#endif
348 if (!ENABLE_FEATURE_SEAMLESS_GZ
349 || magic[0] != 0x1f || magic[1] != 0x8b
350 ) {
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000351 if (!ENABLE_FEATURE_SEAMLESS_BZ2
352 || magic[0] != 'B' || magic[1] != 'Z'
353 ) {
354 bb_error_msg_and_die("no gzip"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000355 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000356 " magic");
357 }
358#if BB_MMU
359 xformer = unpack_bz2_stream;
360#else
361 xformer_prog = "bunzip2";
362#endif
363 } else {
364#if !BB_MMU
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);
369#endif
370 }
371 open_transformer(fd, xformer, xformer_prog);
372 }
373 }
374
375 return fd;
376#endif
377}
378
Denis Vlasenkob6052722008-07-10 17:43:01 +0000379void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
380{
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000381 int fd;
Denis Vlasenkob6052722008-07-10 17:43:01 +0000382 char *image;
Denis Vlasenkob6052722008-07-10 17:43:01 +0000383
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000384 fd = open_zipped(fname);
Denis Vlasenkob6052722008-07-10 17:43:01 +0000385 if (fd < 0)
386 return NULL;
387
Denis Vlasenkob6052722008-07-10 17:43:01 +0000388 image = xmalloc_read(fd, maxsz_p);
389 if (!image)
390 bb_perror_msg("read error from '%s'", fname);
391 close(fd);
392
393 return image;
394}