diff options
author | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-20 10:23:09 +0800 |
---|---|---|
committer | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-20 10:23:09 +0800 |
commit | 75fad3d9443170d81624060a7ff73149c84e06b0 (patch) | |
tree | 7fcc7ffadb946c54b1a4ac86a9605e433c41f48e /lib/extract.c | |
parent | 7c01202076019f13e39d3b0cc7c306bf3d0ce59d (diff) | |
download | nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar.gz nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar.bz2 nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar.lz nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar.xz nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.tar.zst nfcollect-75fad3d9443170d81624060a7ff73149c84e06b0.zip |
nfextract: Check number of available entries
Diffstat (limited to 'lib/extract.c')
-rw-r--r-- | lib/extract.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/extract.c b/lib/extract.c index e5fa681..11381d7 100644 --- a/lib/extract.c +++ b/lib/extract.c @@ -29,9 +29,10 @@ static int nfl_verify_header(nfl_header_t *header) { } static int nfl_extract_default(FILE *f, nfl_state_t *state) { - fread(state->store, state->header->n_entries, sizeof(nfl_entry_t), f); + int entries = + fread(state->store, sizeof(nfl_entry_t), state->header->n_entries, f); WARN_RETURN(ferror(f), "%s", strerror(errno)); - return 0; + return entries; } static int nfl_extract_zstd(FILE *f, nfl_state_t *state) { @@ -40,6 +41,9 @@ static int nfl_extract_zstd(FILE *f, nfl_state_t *state) { expected_decom_size = state->header->n_entries * sizeof(nfl_entry_t); + // It's possible that data or header is not written due to broken commit + WARN_RETURN(compressed_size <= 0, "%s", "zstd: no data in this trunk"); + ERR(!(buf = malloc(compressed_size)), "zstd: cannot malloc"); fread(buf, compressed_size, 1, f); WARN_RETURN(ferror(f), "%s", strerror(errno)); @@ -61,7 +65,7 @@ static int nfl_extract_zstd(FILE *f, nfl_state_t *state) { } free(buf); - return 0; + return actual_decom_size / sizeof(nfl_entry_t); } static int nfl_extract_lz4(FILE *f, nfl_state_t *state) { @@ -94,16 +98,18 @@ int nfl_extract_worker(const char *filename, nfl_state_t *state) { "extract malloc store"); switch (h->compression_opt) { case COMPRESS_NONE: - debug("Extract worker #%u: extract without compression\n", h->id) - nfl_extract_default(f, state); + debug("Extract worker #%u: extract without compression\n", h->id); + ret = nfl_extract_default(f, state); break; case COMPRESS_LZ4: debug("Extract worker #%u: extract with compression algorithm: lz4", - h->id) nfl_extract_lz4(f, state); + h->id); + ret = nfl_extract_lz4(f, state); break; case COMPRESS_ZSTD: debug("Extract worker #%u: extract with compression algorithm: zstd", - h->id) nfl_extract_zstd(f, state); + h->id); + ret = nfl_extract_zstd(f, state); break; // Must not reach here ... default: |