diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-07-25 14:16:10 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-07-25 14:20:48 +0200 |
commit | f8346498604f417415c131e3c6dbf66e6643254e (patch) | |
tree | 8f9de79c9d4d59bfa9481a632064afde5afa2d39 | |
parent | db9187c8d21e9f08b20899e3e14c1938db7b79fb (diff) |
Replace reads in squashfs with positional reads
In most cases, we know exactely where the data that we want to read is
on disk, so instead of using read() on the squashfs (or lseek + read),
the code can in many places be cleaned up to use the pread wrapper
read_data_at instead.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | lib/comp/compressor.c | 4 | ||||
-rw-r--r-- | lib/sqfs/data_reader.c | 17 | ||||
-rw-r--r-- | lib/sqfs/frag_reader.c | 19 | ||||
-rw-r--r-- | lib/sqfs/id_table_read.c | 10 | ||||
-rw-r--r-- | lib/sqfs/meta_reader.c | 11 | ||||
-rw-r--r-- | lib/sqfs/read_super.c | 8 |
6 files changed, 24 insertions, 45 deletions
diff --git a/lib/comp/compressor.c b/lib/comp/compressor.c index ce88bdd..fd67257 100644 --- a/lib/comp/compressor.c +++ b/lib/comp/compressor.c @@ -77,8 +77,8 @@ int generic_read_options(int fd, void *data, size_t size) { uint8_t buffer[size + 2]; - if (read_data("reading compressor options", - fd, buffer, sizeof(buffer))) { + if (read_data_at("reading compressor options", sizeof(sqfs_super_t), + fd, buffer, sizeof(buffer))) { return -1; } diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c index 4fbd97a..e03a436 100644 --- a/lib/sqfs/data_reader.c +++ b/lib/sqfs/data_reader.c @@ -65,6 +65,7 @@ int data_reader_dump_file(data_reader_t *data, file_info_t *fi, int outfd, bool allow_sparse) { size_t i, count, fragsz, unpackedsz; + off_t sqfs_location; uint64_t filesz = 0; bool compressed; uint32_t bs; @@ -81,8 +82,7 @@ int data_reader_dump_file(data_reader_t *data, file_info_t *fi, int outfd, } if (count > 0) { - if (lseek(data->sqfsfd, fi->startblock, SEEK_SET) == (off_t)-1) - goto fail_seek; + sqfs_location = fi->startblock; for (i = 0; i < count; ++i) { bs = fi->blocksizes[i]; @@ -112,9 +112,13 @@ int data_reader_dump_file(data_reader_t *data, file_info_t *fi, int outfd, if (bs == 0) { memset(data->buffer, 0, unpackedsz); compressed = false; - } else if (read_data("reading data block", - data->sqfsfd, data->buffer, bs)) { - return -1; + } else { + if (read_data_at("reading data block", + sqfs_location, data->sqfsfd, + data->buffer, bs)) { + return -1; + } + sqfs_location += bs; } if (compressed) { @@ -157,9 +161,6 @@ int data_reader_dump_file(data_reader_t *data, file_info_t *fi, int outfd, fail_sparse: perror("creating sparse output file"); return -1; -fail_seek: - perror("seek on squashfs"); - return -1; fail_bs: fputs("found compressed block larger than block size\n", stderr); return -1; diff --git a/lib/sqfs/frag_reader.c b/lib/sqfs/frag_reader.c index 87e4c35..721286c 100644 --- a/lib/sqfs/frag_reader.c +++ b/lib/sqfs/frag_reader.c @@ -31,11 +31,6 @@ static int precache_block(frag_reader_t *f, size_t i) if (i == f->current_index) return 0; - if (lseek(f->fd, f->tbl[i].start_offset, SEEK_SET) == (off_t)-1) { - perror("seeking to fragment location"); - return -1; - } - compressed = (f->tbl[i].size & (1 << 24)) == 0; size = f->tbl[i].size & ((1 << 24) - 1); @@ -44,8 +39,10 @@ static int precache_block(frag_reader_t *f, size_t i) return -1; } - if (read_data("reading fragment", f->fd, f->buffer, size)) + if (read_data_at("reading fragment", f->tbl[i].start_offset, + f->fd, f->buffer, size)) { return -1; + } if (compressed) { ret = f->cmp->do_block(f->cmp, f->buffer, size, @@ -95,11 +92,8 @@ frag_reader_t *frag_reader_create(sqfs_super_t *super, int fd, goto fail_rd; /* read the meta block offset table */ - if (lseek(fd, super->fragment_table_start, SEEK_SET) == (off_t)-1) - goto fail_seek; - - if (read_data("reading fragment table", fd, locations, - blockcount * sizeof(locations[0]))) { + if (read_data_at("reading fragment table", super->fragment_table_start, + fd, locations, blockcount * sizeof(locations[0]))) { goto fail; } @@ -139,9 +133,6 @@ frag_reader_t *frag_reader_create(sqfs_super_t *super, int fd, f->block_size = super->block_size; f->current_index = count; return f; -fail_seek: - perror("seek to fragment table"); - goto fail; fail_rd: perror("reading fragment table"); goto fail; diff --git a/lib/sqfs/id_table_read.c b/lib/sqfs/id_table_read.c index 7147f2e..1e92f75 100644 --- a/lib/sqfs/id_table_read.c +++ b/lib/sqfs/id_table_read.c @@ -38,15 +38,12 @@ int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super, tbl->num_ids = super->id_count; tbl->max_ids = super->id_count; - if (lseek(fd, super->id_table_start, SEEK_SET) == (off_t)-1) - goto fail_seek; - block_count = super->id_count / 2048; if (super->id_count % 2048) ++block_count; - if (read_data("reading ID table", fd, blocks, - sizeof(blocks[0]) * block_count)) { + if (read_data_at("reading ID table", super->id_table_start, fd, + blocks, sizeof(blocks[0]) * block_count)) { return -1; } @@ -82,7 +79,4 @@ int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super, fail_meta: meta_reader_destroy(m); return -1; -fail_seek: - perror("seeking to ID table"); - return -1; } diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index 96b59fd..2c6ec97 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -68,14 +68,11 @@ int meta_reader_seek(meta_reader_t *m, uint64_t block_start, size_t offset) return 0; } - if (lseek(m->fd, block_start, SEEK_SET) == (off_t)-1) { - perror("seek on image file"); + if (read_data_at("reading meta data header", block_start, + m->fd, &header, 2)) { return -1; } - if (read_data("reading meta data header", m->fd, &header, 2)) - return -1; - header = le16toh(header); compressed = (header & 0x8000) == 0; size = header & 0x7FFF; @@ -92,8 +89,10 @@ int meta_reader_seek(meta_reader_t *m, uint64_t block_start, size_t offset) memset(m->data, 0, sizeof(m->data)); - if (read_data("reading meta data block", m->fd, m->data, size)) + if (read_data_at("reading meta data block", block_start + 2, + m->fd, m->data, size)) { return -1; + } if (compressed) { ret = m->cmp->do_block(m->cmp, m->data, size, diff --git a/lib/sqfs/read_super.c b/lib/sqfs/read_super.c index 1a84ff9..fff3d50 100644 --- a/lib/sqfs/read_super.c +++ b/lib/sqfs/read_super.c @@ -15,10 +15,7 @@ int sqfs_super_read(sqfs_super_t *super, int fd) sqfs_super_t temp; int i; - if (lseek(fd, 0, SEEK_SET) == (off_t)-1) - goto fail_seek; - - if (read_data("reading super block", fd, &temp, sizeof(temp))) + if (read_data_at("reading super block", 0, fd, &temp, sizeof(temp))) return -1; temp.magic = le32toh(temp.magic); @@ -87,7 +84,4 @@ int sqfs_super_read(sqfs_super_t *super, int fd) memcpy(super, &temp, sizeof(temp)); return 0; -fail_seek: - perror("squashfs writing super block: seek on output file"); - return -1; } |