summaryrefslogtreecommitdiff
path: root/lib/sqfs/data_reader.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-05 23:55:04 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-07 10:41:49 +0200
commit1fff8f3a1326bd82f8140a61d969994e635834fe (patch)
tree817c148152a38acd0b1110d1e3d55788047d078c /lib/sqfs/data_reader.c
parentae4193162e1607b75ef34e4e9a33e2b54f03af8a (diff)
Add pread(2) like function to data_reader
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/data_reader.c')
-rw-r--r--lib/sqfs/data_reader.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c
index e33ffbb..27a8302 100644
--- a/lib/sqfs/data_reader.c
+++ b/lib/sqfs/data_reader.c
@@ -225,3 +225,84 @@ fail_sparse:
perror("creating sparse output file");
return -1;
}
+
+ssize_t data_reader_read(data_reader_t *data, file_info_t *fi,
+ uint64_t offset, void *buffer, size_t size)
+{
+ size_t i, diff, fragsz, count, total = 0;
+ off_t off;
+ char *ptr;
+
+ /* work out block count and fragment size */
+ fragsz = fi->size % data->block_size;
+ count = fi->size / data->block_size;
+
+ if (fragsz != 0 && !(fi->flags & FILE_FLAG_HAS_FRAGMENT)) {
+ fragsz = 0;
+ ++count;
+ }
+
+ /* work out block index and on-disk location */
+ off = fi->startblock;
+ i = 0;
+
+ while (offset > data->block_size && i < count) {
+ off += SQFS_ON_DISK_BLOCK_SIZE(fi->blocks[i++].size);
+ offset -= data->block_size;
+ }
+
+ /* copy data from blocks */
+ while (i < count && size > 0) {
+ diff = data->block_size - offset;
+ if (size < diff)
+ size = diff;
+
+ if (SQFS_IS_SPARSE_BLOCK(fi->blocks[i].size)) {
+ memset(buffer, 0, diff);
+ } else {
+ if (precache_data_block(data, off, fi->blocks[i].size))
+ return -1;
+
+ memcpy(buffer, (char *)data->block + offset, diff);
+ off += SQFS_ON_DISK_BLOCK_SIZE(fi->blocks[i].size);
+ }
+
+ ++i;
+ offset = 0;
+ size -= diff;
+ total += diff;
+ buffer = (char *)buffer + diff;
+ }
+
+ /* copy from fragment */
+ if (i == count && size > 0 && fragsz > 0) {
+ if (precache_fragment_block(data, fi->fragment))
+ return -1;
+
+ if (fi->fragment_offset >= data->frag_used)
+ goto fail_range;
+
+ if ((fi->fragment_offset + fragsz - 1) >= data->frag_used)
+ goto fail_range;
+
+ ptr = (char *)data->frag_block + fi->fragment_offset;
+ ptr += offset;
+
+ if (offset >= fragsz) {
+ offset = 0;
+ size = 0;
+ }
+
+ if (offset + size > fragsz)
+ size = fragsz - offset;
+
+ if (size > 0) {
+ memcpy(buffer, ptr + offset, size);
+ total += size;
+ }
+ }
+ return total;
+fail_range:
+ fputs("attempted to read past fragment block limits\n", stderr);
+ return -1;
+}