Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 1 | /* 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 | */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 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 Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 18 | # include "unarchive.h" |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 19 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 21 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 22 | { |
| 23 | ssize_t n; |
| 24 | |
| 25 | do { |
| 26 | n = read(fd, buf, count); |
| 27 | } while (n < 0 && errno == EINTR); |
| 28 | |
| 29 | return n; |
| 30 | } |
| 31 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 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? |
| 36 | * |
| 37 | * *** SURPRISE! It will affect the parent too! *** |
| 38 | * *** BIG SURPRISE! It stays even after child exits! *** |
| 39 | * |
| 40 | * This is a design bug in UNIX API. |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 41 | * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 42 | * will set nonblocking mode not only on _your_ stdin, but |
| 43 | * also on stdin of your parent, etc. |
| 44 | * |
| 45 | * In general, |
| 46 | * fd2 = dup(fd1); |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 47 | * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK); |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 48 | * sets both fd1 and fd2 to O_NONBLOCK. This includes cases |
| 49 | * where duping is done implicitly by fork() etc. |
| 50 | * |
| 51 | * We need |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 52 | * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK); |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 53 | * (note SETFD, not SETFL!) but such thing doesn't exist. |
| 54 | * |
| 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); |
| 58 | * "MSG_DONTWAIT: |
| 59 | * Enables non-blocking operation; if the operation |
| 60 | * would block, EAGAIN is returned." |
| 61 | * but recv() works only for sockets! |
| 62 | * |
| 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. |
Denis Vlasenko | 081efd1 | 2008-02-20 22:57:24 +0000 | [diff] [blame] | 66 | * Thankfully, poll() doesn't care about O_NONBLOCK flag. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 67 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 68 | ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 69 | { |
| 70 | struct pollfd pfd[1]; |
| 71 | ssize_t n; |
| 72 | |
| 73 | while (1) { |
| 74 | n = safe_read(fd, buf, count); |
| 75 | if (n >= 0 || errno != EAGAIN) |
| 76 | return n; |
| 77 | /* fd is in O_NONBLOCK mode. Wait using poll and repeat */ |
| 78 | pfd[0].fd = fd; |
| 79 | pfd[0].events = POLLIN; |
| 80 | safe_poll(pfd, 1, -1); |
| 81 | } |
| 82 | } |
| 83 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 84 | /* |
| 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. |
| 89 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 90 | ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 91 | { |
| 92 | ssize_t cc; |
| 93 | ssize_t total; |
| 94 | |
| 95 | total = 0; |
| 96 | |
| 97 | while (len) { |
| 98 | cc = safe_read(fd, buf, len); |
| 99 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 100 | if (cc < 0) { |
| 101 | if (total) { |
| 102 | /* we already have some! */ |
| 103 | /* user can do another read to know the error code */ |
| 104 | return total; |
| 105 | } |
| 106 | return cc; /* read() returns -1 on failure. */ |
| 107 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 108 | if (cc == 0) |
| 109 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 110 | buf = ((char *)buf) + cc; |
| 111 | total += cc; |
| 112 | len -= cc; |
| 113 | } |
| 114 | |
| 115 | return total; |
| 116 | } |
| 117 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 118 | /* Die with an error message if we can't read the entire buffer. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 119 | void FAST_FUNC xread(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 120 | { |
| 121 | if (count) { |
| 122 | ssize_t size = full_read(fd, buf, count); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 123 | if ((size_t)size != count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 124 | bb_error_msg_and_die("short read"); |
| 125 | } |
| 126 | } |
| 127 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 128 | /* Die with an error message if we can't read one character. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 129 | unsigned char FAST_FUNC xread_char(int fd) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 130 | { |
| 131 | char tmp; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 132 | xread(fd, &tmp, 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 133 | return tmp; |
| 134 | } |
| 135 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 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. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 138 | // Bytes are appended to pfx (which must be malloced, or NULL). |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 139 | char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p) |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 140 | { |
| 141 | char *p; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 142 | size_t sz = buf ? strlen(buf) : 0; |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 143 | size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 144 | |
| 145 | goto jump_in; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 146 | while (sz < maxsz) { |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 147 | if ((size_t)(p - buf) == sz) { |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 148 | jump_in: |
| 149 | buf = xrealloc(buf, sz + 128); |
| 150 | p = buf + sz; |
| 151 | sz += 128; |
| 152 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 153 | /* nonblock_safe_read() because we are used by e.g. shells */ |
| 154 | if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */ |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 155 | if (p == buf) { /* we read nothing */ |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 156 | free(buf); |
| 157 | return NULL; |
| 158 | } |
| 159 | break; |
| 160 | } |
| 161 | if (*p == '\n') |
| 162 | break; |
| 163 | p++; |
| 164 | } |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 165 | *p = '\0'; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 166 | if (maxsz_p) |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 167 | *maxsz_p = p - buf; |
| 168 | p++; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 169 | return xrealloc(buf, p - buf); |
| 170 | } |
| 171 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 172 | ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 173 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 174 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 175 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 176 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 177 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 178 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 179 | return size; |
| 180 | } |
| 181 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 182 | ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 183 | { |
| 184 | int fd = open(filename, O_RDONLY); |
| 185 | if (fd < 0) |
| 186 | return fd; |
| 187 | return read_close(fd, buf, size); |
| 188 | } |
| 189 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | fe733a9 | 2008-06-24 16:08:22 +0000 | [diff] [blame] | 191 | // Read (potentially big) files in one go. File size is estimated |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 192 | // by stat. Extra '\0' byte is appended. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 193 | void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 194 | { |
| 195 | char *buf; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 196 | size_t size, rd_size, total; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 197 | size_t to_read; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 198 | struct stat st; |
| 199 | |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 200 | to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 202 | /* Estimate file size */ |
| 203 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 204 | fstat(fd, &st); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 205 | /* /proc/N/stat files report st_size 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 206 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 207 | size = (st.st_size | 0x3ff) + 1; |
| 208 | |
| 209 | total = 0; |
| 210 | buf = NULL; |
| 211 | while (1) { |
| 212 | if (to_read < size) |
| 213 | size = to_read; |
| 214 | buf = xrealloc(buf, total + size + 1); |
| 215 | rd_size = full_read(fd, buf + total, size); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 216 | if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 217 | free(buf); |
| 218 | return NULL; |
| 219 | } |
| 220 | total += rd_size; |
| 221 | if (rd_size < size) /* EOF */ |
| 222 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 223 | if (to_read <= rd_size) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 224 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 225 | to_read -= rd_size; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 226 | /* grow by 1/8, but in [1k..64k] bounds */ |
| 227 | size = ((total / 8) | 0x3ff) + 1; |
| 228 | if (size > 64*1024) |
| 229 | size = 64*1024; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 230 | } |
Denys Vlasenko | 071ede1 | 2009-05-15 23:23:23 +0200 | [diff] [blame] | 231 | buf = xrealloc(buf, total + 1); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 232 | buf[total] = '\0'; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 234 | if (maxsz_p) |
| 235 | *maxsz_p = total; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 236 | return buf; |
| 237 | } |
| 238 | |
| 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) */ |
| 244 | |
| 245 | // Read (potentially big) files in one go. File size is estimated by |
| 246 | // lseek to end. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 247 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 248 | { |
| 249 | char *buf; |
| 250 | size_t size; |
| 251 | int fd; |
| 252 | off_t len; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 254 | fd = open(filename, O_RDONLY); |
| 255 | if (fd < 0) |
| 256 | return NULL; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 258 | /* /proc/N/stat files report len 0 here */ |
| 259 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 264 | size = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 265 | if (len < size) |
| 266 | size = len; |
| 267 | } |
| 268 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 269 | buf = xmalloc(size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 270 | size = read_close(fd, buf, size); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 271 | if ((ssize_t)size < 0) { |
| 272 | free(buf); |
| 273 | return NULL; |
| 274 | } |
Denys Vlasenko | 071ede1 | 2009-05-15 23:23:23 +0200 | [diff] [blame] | 275 | buf = xrealloc(buf, size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 276 | buf[size] = '\0'; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 278 | if (maxsz_p) |
| 279 | *maxsz_p = size; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 280 | return buf; |
| 281 | } |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 282 | #endif |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 284 | // Read (potentially big) files in one go. File size is estimated |
| 285 | // by stat. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 286 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 287 | { |
| 288 | char *buf; |
| 289 | int fd; |
| 290 | |
| 291 | fd = open(filename, O_RDONLY); |
| 292 | if (fd < 0) |
| 293 | return NULL; |
| 294 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 295 | buf = xmalloc_read(fd, maxsz_p); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 296 | close(fd); |
| 297 | return buf; |
| 298 | } |
| 299 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 300 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 301 | { |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 302 | void *buf = xmalloc_open_read_close(filename, maxsz_p); |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 303 | if (!buf) |
| 304 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 305 | return buf; |
| 306 | } |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 307 | |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 308 | #if ZIPPED |
| 309 | int FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 310 | { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 311 | const int fail_if_not_detected = 1; |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 312 | unsigned char magic[2]; |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 313 | #if BB_MMU |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 314 | IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 315 | enum { xformer_prog = 0 }; |
| 316 | #else |
| 317 | enum { xformer = 0 }; |
| 318 | const char *xformer_prog; |
| 319 | #endif |
| 320 | |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 321 | /* .gz and .bz2 both have 2-byte signature, and their |
| 322 | * unpack_XXX_stream wants this header skipped. */ |
| 323 | xread(fd, &magic, 2); |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 324 | if (ENABLE_FEATURE_SEAMLESS_GZ |
| 325 | && magic[0] == 0x1f && magic[1] == 0x8b |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 326 | ) { |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 327 | # if BB_MMU |
| 328 | xformer = unpack_gz_stream; |
| 329 | # else |
| 330 | xformer_prog = "gunzip"; |
| 331 | # endif |
| 332 | goto found_magic; |
| 333 | } |
| 334 | if (ENABLE_FEATURE_SEAMLESS_BZ2 |
| 335 | && magic[0] == 'B' && magic[1] == 'Z' |
| 336 | ) { |
| 337 | # if BB_MMU |
| 338 | xformer = unpack_bz2_stream; |
| 339 | # else |
| 340 | xformer_prog = "bunzip2"; |
| 341 | # endif |
| 342 | goto found_magic; |
| 343 | } |
Denys Vlasenko | c2345fa | 2010-05-06 17:05:22 +0200 | [diff] [blame] | 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 |
| 347 | |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 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") |
| 352 | " magic"); |
| 353 | xlseek(fd, -2, SEEK_CUR); |
| 354 | return fd; |
| 355 | |
| 356 | found_magic: |
| 357 | # if !BB_MMU |
| 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); |
| 362 | # endif |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 363 | open_transformer(fd, xformer, xformer_prog); |
| 364 | |
| 365 | return fd; |
| 366 | } |
| 367 | #endif /* ZIPPED */ |
| 368 | |
| 369 | int FAST_FUNC open_zipped(const char *fname) |
| 370 | { |
| 371 | #if !ZIPPED |
| 372 | return open(fname, O_RDONLY); |
| 373 | #else |
| 374 | char *sfx; |
| 375 | int fd; |
| 376 | |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 377 | fd = open(fname, O_RDONLY); |
| 378 | if (fd < 0) |
| 379 | return fd; |
| 380 | |
| 381 | sfx = strrchr(fname, '.'); |
| 382 | if (sfx) { |
| 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"); |
| 386 | else |
| 387 | if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, ".gz") == 0) |
| 388 | || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, ".bz2") == 0) |
| 389 | ) { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 390 | setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/); |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | return fd; |
| 395 | #endif |
| 396 | } |
| 397 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 398 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) |
| 399 | { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 400 | int fd; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 401 | char *image; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 402 | |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 403 | fd = open_zipped(fname); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 404 | if (fd < 0) |
| 405 | return NULL; |
| 406 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 407 | image = xmalloc_read(fd, maxsz_p); |
| 408 | if (!image) |
| 409 | bb_perror_msg("read error from '%s'", fname); |
| 410 | close(fd); |
| 411 | |
| 412 | return image; |
| 413 | } |