Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/libbb/read_printf.c

    r2725 r3232  
    88 */
    99#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
    18 # include "archive.h"
    19 #endif
    2010
    2111
     
    5646 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
    5747 */
    58 ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
     48ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR)
    5949{
    6050    struct pollfd pfd[1];
     
    6252
    6353    while (1) {
    64         n = safe_read(fd, buf, count);
     54        n = loop_on_EINTR ? safe_read(fd, buf, count) : read(fd, buf, count);
    6555        if (n >= 0 || errno != EAGAIN)
    6656            return n;
     
    6858        pfd[0].fd = fd;
    6959        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);
    7162    }
    7263}
     
    7566// Reads byte-by-byte. Useful when it is important to not read ahead.
    7667// 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)
     68char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
    7869{
    7970    char *p;
    80     size_t sz = buf ? strlen(buf) : 0;
     71    char *buf = NULL;
     72    size_t sz = 0;
    8173    size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
    8274
    8375    goto jump_in;
     76
    8477    while (sz < maxsz) {
    8578        if ((size_t)(p - buf) == sz) {
     
    8982            sz += 128;
    9083        }
    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 */
    9386            if (p == buf) { /* we read nothing */
    9487                free(buf);
     
    242235    return buf;
    243236}
    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 ZIPPED
    249 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_MMU
    259     IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
    260     enum { xformer_prog = 0 };
    261 # else
    262     enum { xformer = 0 };
    263     const char *xformer_prog;
    264 # endif
    265 
    266     /* .gz and .bz2 both have 2-byte signature, and their
    267      * unpack_XXX_stream wants this header skipped. */
    268     xread(fd, magic.b16, sizeof(magic.b16[0]));
    269     if (ENABLE_FEATURE_SEAMLESS_GZ
    270      && magic.b16[0] == GZIP_MAGIC
    271     ) {
    272 # if BB_MMU
    273         xformer = unpack_gz_stream;
    274 # else
    275         xformer_prog = "gunzip";
    276 # endif
    277         goto found_magic;
    278     }
    279     if (ENABLE_FEATURE_SEAMLESS_BZ2
    280      && magic.b16[0] == BZIP2_MAGIC
    281     ) {
    282 # if BB_MMU
    283         xformer = unpack_bz2_stream;
    284 # else
    285         xformer_prog = "bunzip2";
    286 # endif
    287         goto found_magic;
    288     }
    289     if (ENABLE_FEATURE_SEAMLESS_XZ
    290      && magic.b16[0] == XZ_MAGIC1
    291     ) {
    292         offset = -6;
    293         xread(fd, magic.b32, sizeof(magic.b32[0]));
    294         if (magic.b32[0] == XZ_MAGIC2) {
    295 # if BB_MMU
    296             xformer = unpack_xz_stream;
    297             /* unpack_xz_stream wants fd at position 6, no need to seek */
    298             //xlseek(fd, offset, SEEK_CUR);
    299 # else
    300             xformer_prog = "unxz";
    301 # endif
    302             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_MMU
    317     /* NOMMU version of open_transformer execs
    318      * an external unzipper that wants
    319      * file position at the start of the file */
    320     xlseek(fd, offset, SEEK_CUR);
    321 # endif
    322     open_transformer(fd, xformer, xformer_prog);
    323 }
    324 #endif /* ZIPPED */
    325 
    326 int FAST_FUNC open_zipped(const char *fname)
    327 {
    328 #if !ZIPPED
    329     return open(fname, O_RDONLY);
    330 #else
    331     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         else
    345         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 #endif
    355 }
    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.