Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 8 | */ |
| 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 Vlasenko | 833d4e7 | 2010-11-03 02:38:31 +0100 | [diff] [blame] | 18 | # include "archive.h" |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 19 | #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 Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame^] | 58 | ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count) |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 59 | { |
| 60 | struct pollfd pfd[1]; |
| 61 | ssize_t n; |
| 62 | |
| 63 | while (1) { |
| 64 | n = safe_read(fd, buf, count); |
| 65 | 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; |
| 70 | safe_poll(pfd, 1, -1); /* note: this pulls in printf */ |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Reads one line a-la fgets (but doesn't save terminating '\n'). |
| 75 | // Reads byte-by-byte. Useful when it is important to not read ahead. |
| 76 | // Bytes are appended to pfx (which must be malloced, or NULL). |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame^] | 77 | char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p) |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 78 | { |
| 79 | char *p; |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame^] | 80 | char *buf = NULL; |
| 81 | size_t sz = 0; |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 82 | size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
| 83 | |
| 84 | goto jump_in; |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame^] | 85 | |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 86 | while (sz < maxsz) { |
| 87 | if ((size_t)(p - buf) == sz) { |
| 88 | jump_in: |
| 89 | buf = xrealloc(buf, sz + 128); |
| 90 | p = buf + sz; |
| 91 | sz += 128; |
| 92 | } |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame^] | 93 | if (nonblock_immune_read(fd, p, 1) != 1) { |
| 94 | /* EOF/error */ |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 95 | if (p == buf) { /* we read nothing */ |
| 96 | free(buf); |
| 97 | return NULL; |
| 98 | } |
| 99 | break; |
| 100 | } |
| 101 | if (*p == '\n') |
| 102 | break; |
| 103 | p++; |
| 104 | } |
| 105 | *p = '\0'; |
| 106 | if (maxsz_p) |
| 107 | *maxsz_p = p - buf; |
| 108 | p++; |
| 109 | return xrealloc(buf, p - buf); |
| 110 | } |
| 111 | |
| 112 | // Read (potentially big) files in one go. File size is estimated |
| 113 | // by stat. Extra '\0' byte is appended. |
| 114 | void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) |
| 115 | { |
| 116 | char *buf; |
| 117 | size_t size, rd_size, total; |
| 118 | size_t to_read; |
| 119 | struct stat st; |
| 120 | |
| 121 | to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */ |
| 122 | |
| 123 | /* Estimate file size */ |
| 124 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
| 125 | fstat(fd, &st); |
| 126 | /* /proc/N/stat files report st_size 0 */ |
| 127 | /* In order to make such files readable, we add small const */ |
| 128 | size = (st.st_size | 0x3ff) + 1; |
| 129 | |
| 130 | total = 0; |
| 131 | buf = NULL; |
| 132 | while (1) { |
| 133 | if (to_read < size) |
| 134 | size = to_read; |
| 135 | buf = xrealloc(buf, total + size + 1); |
| 136 | rd_size = full_read(fd, buf + total, size); |
| 137 | if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */ |
| 138 | free(buf); |
| 139 | return NULL; |
| 140 | } |
| 141 | total += rd_size; |
| 142 | if (rd_size < size) /* EOF */ |
| 143 | break; |
| 144 | if (to_read <= rd_size) |
| 145 | break; |
| 146 | to_read -= rd_size; |
| 147 | /* grow by 1/8, but in [1k..64k] bounds */ |
| 148 | size = ((total / 8) | 0x3ff) + 1; |
| 149 | if (size > 64*1024) |
| 150 | size = 64*1024; |
| 151 | } |
| 152 | buf = xrealloc(buf, total + 1); |
| 153 | buf[total] = '\0'; |
| 154 | |
| 155 | if (maxsz_p) |
| 156 | *maxsz_p = total; |
| 157 | return buf; |
| 158 | } |
| 159 | |
| 160 | #ifdef USING_LSEEK_TO_GET_SIZE |
| 161 | /* Alternatively, file size can be obtained by lseek to the end. |
| 162 | * The code is slightly bigger. Retained in case fstat approach |
| 163 | * will not work for some weird cases (/proc, block devices, etc). |
| 164 | * (NB: lseek also can fail to work for some weird files) */ |
| 165 | |
| 166 | // Read (potentially big) files in one go. File size is estimated by |
| 167 | // lseek to end. |
| 168 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
| 169 | { |
| 170 | char *buf; |
| 171 | size_t size; |
| 172 | int fd; |
| 173 | off_t len; |
| 174 | |
| 175 | fd = open(filename, O_RDONLY); |
| 176 | if (fd < 0) |
| 177 | return NULL; |
| 178 | |
| 179 | /* /proc/N/stat files report len 0 here */ |
| 180 | /* In order to make such files readable, we add small const */ |
| 181 | size = 0x3ff; /* read only 1k on unseekable files */ |
| 182 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 183 | if (len != (off_t)-1) { |
| 184 | xlseek(fd, 0, SEEK_SET); |
| 185 | size = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
| 186 | if (len < size) |
| 187 | size = len; |
| 188 | } |
| 189 | |
| 190 | buf = xmalloc(size + 1); |
| 191 | size = read_close(fd, buf, size); |
| 192 | if ((ssize_t)size < 0) { |
| 193 | free(buf); |
| 194 | return NULL; |
| 195 | } |
| 196 | buf = xrealloc(buf, size + 1); |
| 197 | buf[size] = '\0'; |
| 198 | |
| 199 | if (maxsz_p) |
| 200 | *maxsz_p = size; |
| 201 | return buf; |
| 202 | } |
| 203 | #endif |
| 204 | |
| 205 | // Read (potentially big) files in one go. File size is estimated |
| 206 | // by stat. |
| 207 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
| 208 | { |
| 209 | char *buf; |
| 210 | int fd; |
| 211 | |
| 212 | fd = open(filename, O_RDONLY); |
| 213 | if (fd < 0) |
| 214 | return NULL; |
| 215 | |
| 216 | buf = xmalloc_read(fd, maxsz_p); |
| 217 | close(fd); |
| 218 | return buf; |
| 219 | } |
| 220 | |
| 221 | /* Die with an error message if we can't read the entire buffer. */ |
| 222 | void FAST_FUNC xread(int fd, void *buf, size_t count) |
| 223 | { |
| 224 | if (count) { |
| 225 | ssize_t size = full_read(fd, buf, count); |
| 226 | if ((size_t)size != count) |
| 227 | bb_error_msg_and_die("short read"); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* Die with an error message if we can't read one character. */ |
| 232 | unsigned char FAST_FUNC xread_char(int fd) |
| 233 | { |
| 234 | char tmp; |
| 235 | xread(fd, &tmp, 1); |
| 236 | return tmp; |
| 237 | } |
| 238 | |
| 239 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) |
| 240 | { |
| 241 | void *buf = xmalloc_open_read_close(filename, maxsz_p); |
| 242 | if (!buf) |
| 243 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 244 | return buf; |
| 245 | } |
| 246 | |
| 247 | /* Used by e.g. rpm which gives us a fd without filename, |
| 248 | * thus we can't guess the format from filename's extension. |
| 249 | */ |
| 250 | #if ZIPPED |
| 251 | void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) |
| 252 | { |
| 253 | const int fail_if_not_detected = 1; |
| 254 | union { |
| 255 | uint8_t b[4]; |
| 256 | uint16_t b16[2]; |
| 257 | uint32_t b32[1]; |
| 258 | } magic; |
| 259 | int offset = -2; |
| 260 | # if BB_MMU |
| 261 | IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); |
| 262 | enum { xformer_prog = 0 }; |
| 263 | # else |
| 264 | enum { xformer = 0 }; |
| 265 | const char *xformer_prog; |
| 266 | # endif |
| 267 | |
| 268 | /* .gz and .bz2 both have 2-byte signature, and their |
| 269 | * unpack_XXX_stream wants this header skipped. */ |
Denys Vlasenko | 894fa0a | 2010-06-26 05:01:16 +0200 | [diff] [blame] | 270 | xread(fd, magic.b16, sizeof(magic.b16[0])); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 271 | if (ENABLE_FEATURE_SEAMLESS_GZ |
| 272 | && magic.b16[0] == GZIP_MAGIC |
| 273 | ) { |
| 274 | # if BB_MMU |
| 275 | xformer = unpack_gz_stream; |
| 276 | # else |
| 277 | xformer_prog = "gunzip"; |
| 278 | # endif |
| 279 | goto found_magic; |
| 280 | } |
| 281 | if (ENABLE_FEATURE_SEAMLESS_BZ2 |
| 282 | && magic.b16[0] == BZIP2_MAGIC |
| 283 | ) { |
| 284 | # if BB_MMU |
| 285 | xformer = unpack_bz2_stream; |
| 286 | # else |
| 287 | xformer_prog = "bunzip2"; |
| 288 | # endif |
| 289 | goto found_magic; |
| 290 | } |
| 291 | if (ENABLE_FEATURE_SEAMLESS_XZ |
| 292 | && magic.b16[0] == XZ_MAGIC1 |
| 293 | ) { |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 294 | offset = -6; |
Denys Vlasenko | 894fa0a | 2010-06-26 05:01:16 +0200 | [diff] [blame] | 295 | xread(fd, magic.b32, sizeof(magic.b32[0])); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 296 | if (magic.b32[0] == XZ_MAGIC2) { |
| 297 | # if BB_MMU |
| 298 | xformer = unpack_xz_stream; |
Denys Vlasenko | 45f6616 | 2010-07-01 05:12:28 +0200 | [diff] [blame] | 299 | /* unpack_xz_stream wants fd at position 6, no need to seek */ |
| 300 | //xlseek(fd, offset, SEEK_CUR); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 301 | # else |
| 302 | xformer_prog = "unxz"; |
| 303 | # endif |
| 304 | goto found_magic; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* No known magic seen */ |
| 309 | if (fail_if_not_detected) |
| 310 | bb_error_msg_and_die("no gzip" |
| 311 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 312 | IF_FEATURE_SEAMLESS_XZ("/xz") |
| 313 | " magic"); |
| 314 | xlseek(fd, offset, SEEK_CUR); |
| 315 | return; |
| 316 | |
| 317 | found_magic: |
| 318 | # if !BB_MMU |
| 319 | /* NOMMU version of open_transformer execs |
| 320 | * an external unzipper that wants |
| 321 | * file position at the start of the file */ |
| 322 | xlseek(fd, offset, SEEK_CUR); |
| 323 | # endif |
| 324 | open_transformer(fd, xformer, xformer_prog); |
| 325 | } |
| 326 | #endif /* ZIPPED */ |
| 327 | |
| 328 | int FAST_FUNC open_zipped(const char *fname) |
| 329 | { |
| 330 | #if !ZIPPED |
| 331 | return open(fname, O_RDONLY); |
| 332 | #else |
| 333 | char *sfx; |
| 334 | int fd; |
| 335 | |
| 336 | fd = open(fname, O_RDONLY); |
| 337 | if (fd < 0) |
| 338 | return fd; |
| 339 | |
| 340 | sfx = strrchr(fname, '.'); |
| 341 | if (sfx) { |
| 342 | sfx++; |
| 343 | if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0) |
| 344 | /* .lzma has no header/signature, just trust it */ |
| 345 | open_transformer(fd, unpack_lzma_stream, "unlzma"); |
| 346 | else |
| 347 | if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0) |
| 348 | || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0) |
| 349 | || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0) |
| 350 | ) { |
| 351 | setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return fd; |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) |
| 360 | { |
| 361 | int fd; |
| 362 | char *image; |
| 363 | |
| 364 | fd = open_zipped(fname); |
| 365 | if (fd < 0) |
| 366 | return NULL; |
| 367 | |
| 368 | image = xmalloc_read(fd, maxsz_p); |
| 369 | if (!image) |
| 370 | bb_perror_msg("read error from '%s'", fname); |
| 371 | close(fd); |
| 372 | |
| 373 | return image; |
| 374 | } |