Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/libbb/read_printf.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/libbb/read_printf.c
r2725 r3232 8 8 */ 9 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 ZIPPED18 # include "archive.h"19 #endif20 10 21 11 … … 56 46 * Thankfully, poll() doesn't care about O_NONBLOCK flag. 57 47 */ 58 ssize_t FAST_FUNC nonblock_ safe_read(int fd, void *buf, size_t count)48 ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR) 59 49 { 60 50 struct pollfd pfd[1]; … … 62 52 63 53 while (1) { 64 n = safe_read(fd, buf, count);54 n = loop_on_EINTR ? safe_read(fd, buf, count) : read(fd, buf, count); 65 55 if (n >= 0 || errno != EAGAIN) 66 56 return n; … … 68 58 pfd[0].fd = fd; 69 59 pfd[0].events = POLLIN; 70 safe_poll(pfd, 1, -1); /* note: this pulls in printf */ 60 /* note: safe_poll pulls in printf */ 61 loop_on_EINTR ? safe_poll(pfd, 1, -1) : poll(pfd, 1, -1); 71 62 } 72 63 } … … 75 66 // Reads byte-by-byte. Useful when it is important to not read ahead. 76 67 // Bytes are appended to pfx (which must be malloced, or NULL). 77 char* FAST_FUNC xmalloc_reads(int fd, char *buf,size_t *maxsz_p)68 char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p) 78 69 { 79 70 char *p; 80 size_t sz = buf ? strlen(buf) : 0; 71 char *buf = NULL; 72 size_t sz = 0; 81 73 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095); 82 74 83 75 goto jump_in; 76 84 77 while (sz < maxsz) { 85 78 if ((size_t)(p - buf) == sz) { … … 89 82 sz += 128; 90 83 } 91 /* nonblock_safe_read() because we are used by e.g. shells */92 if (nonblock_safe_read(fd, p, 1) != 1) {/* EOF/error */84 if (nonblock_immune_read(fd, p, 1, /*loop_on_EINTR:*/ 1) != 1) { 85 /* EOF/error */ 93 86 if (p == buf) { /* we read nothing */ 94 87 free(buf); … … 242 235 return buf; 243 236 } 244 245 /* Used by e.g. rpm which gives us a fd without filename,246 * thus we can't guess the format from filename's extension.247 */248 #if ZIPPED249 void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)250 {251 const int fail_if_not_detected = 1;252 union {253 uint8_t b[4];254 uint16_t b16[2];255 uint32_t b32[1];256 } magic;257 int offset = -2;258 # if BB_MMU259 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);260 enum { xformer_prog = 0 };261 # else262 enum { xformer = 0 };263 const char *xformer_prog;264 # endif265 266 /* .gz and .bz2 both have 2-byte signature, and their267 * unpack_XXX_stream wants this header skipped. */268 xread(fd, magic.b16, sizeof(magic.b16[0]));269 if (ENABLE_FEATURE_SEAMLESS_GZ270 && magic.b16[0] == GZIP_MAGIC271 ) {272 # if BB_MMU273 xformer = unpack_gz_stream;274 # else275 xformer_prog = "gunzip";276 # endif277 goto found_magic;278 }279 if (ENABLE_FEATURE_SEAMLESS_BZ2280 && magic.b16[0] == BZIP2_MAGIC281 ) {282 # if BB_MMU283 xformer = unpack_bz2_stream;284 # else285 xformer_prog = "bunzip2";286 # endif287 goto found_magic;288 }289 if (ENABLE_FEATURE_SEAMLESS_XZ290 && magic.b16[0] == XZ_MAGIC1291 ) {292 offset = -6;293 xread(fd, magic.b32, sizeof(magic.b32[0]));294 if (magic.b32[0] == XZ_MAGIC2) {295 # if BB_MMU296 xformer = unpack_xz_stream;297 /* unpack_xz_stream wants fd at position 6, no need to seek */298 //xlseek(fd, offset, SEEK_CUR);299 # else300 xformer_prog = "unxz";301 # endif302 goto found_magic;303 }304 }305 306 /* No known magic seen */307 if (fail_if_not_detected)308 bb_error_msg_and_die("no gzip"309 IF_FEATURE_SEAMLESS_BZ2("/bzip2")310 IF_FEATURE_SEAMLESS_XZ("/xz")311 " magic");312 xlseek(fd, offset, SEEK_CUR);313 return;314 315 found_magic:316 # if !BB_MMU317 /* NOMMU version of open_transformer execs318 * an external unzipper that wants319 * file position at the start of the file */320 xlseek(fd, offset, SEEK_CUR);321 # endif322 open_transformer(fd, xformer, xformer_prog);323 }324 #endif /* ZIPPED */325 326 int FAST_FUNC open_zipped(const char *fname)327 {328 #if !ZIPPED329 return open(fname, O_RDONLY);330 #else331 char *sfx;332 int fd;333 334 fd = open(fname, O_RDONLY);335 if (fd < 0)336 return fd;337 338 sfx = strrchr(fname, '.');339 if (sfx) {340 sfx++;341 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)342 /* .lzma has no header/signature, just trust it */343 open_transformer(fd, unpack_lzma_stream, "unlzma");344 else345 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)346 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)347 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)348 ) {349 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);350 }351 }352 353 return fd;354 #endif355 }356 357 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)358 {359 int fd;360 char *image;361 362 fd = open_zipped(fname);363 if (fd < 0)364 return NULL;365 366 image = xmalloc_read(fd, maxsz_p);367 if (!image)368 bb_perror_msg("read error from '%s'", fname);369 close(fd);370 371 return image;372 }
Note:
See TracChangeset
for help on using the changeset viewer.