From f8346498604f417415c131e3c6dbf66e6643254e Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 25 Jul 2019 14:16:10 +0200 Subject: 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 --- lib/sqfs/data_reader.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/sqfs/data_reader.c') 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; -- cgit v1.2.3