summaryrefslogtreecommitdiff
path: root/lib/sqfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-16 21:02:58 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-16 22:48:00 +0200
commite3ef871d6a80d72db02c9ab1ef492e8f58c2ddeb (patch)
treeec28b205b651e83c795e1e264aacfe5bcb307bc4 /lib/sqfs
parentbfd876dbf151df164b4d87de20aec39b24f205f9 (diff)
cleanup: move error handling into read_retry
If read_retry fails to read the expected amount of data (EOF or otherwise), it is almost always an error. This commit renames read_retry to read_data and moves error handling into the function, making a lot of error handling code redundant. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r--lib/sqfs/data_reader.c16
-rw-r--r--lib/sqfs/data_writer.c31
-rw-r--r--lib/sqfs/frag_reader.c27
-rw-r--r--lib/sqfs/id_table_read.c17
-rw-r--r--lib/sqfs/meta_reader.c22
-rw-r--r--lib/sqfs/read_super.c12
6 files changed, 25 insertions, 100 deletions
diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c
index 7f857b2..96ccb39 100644
--- a/lib/sqfs/data_reader.c
+++ b/lib/sqfs/data_reader.c
@@ -110,13 +110,9 @@ 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 {
- ret = read_retry(data->sqfsfd, data->buffer, bs);
- if (ret < 0)
- goto fail_rd;
-
- if ((size_t)ret < bs)
- goto fail_trunc;
+ } else if (read_data("reading data block",
+ data->sqfsfd, data->buffer, bs)) {
+ return -1;
}
if (compressed) {
@@ -163,12 +159,6 @@ fail_sparse:
fail_seek:
perror("seek on squashfs");
return -1;
-fail_rd:
- perror("reading from squashfs");
- return -1;
-fail_trunc:
- fputs("reading from squashfs: unexpected end of file\n", stderr);
- return -1;
fail_bs:
fputs("found compressed block larger than block size\n", stderr);
return -1;
diff --git a/lib/sqfs/data_writer.c b/lib/sqfs/data_writer.c
index eebd2a4..64e27fa 100644
--- a/lib/sqfs/data_writer.c
+++ b/lib/sqfs/data_writer.c
@@ -160,7 +160,6 @@ int write_data_from_fd(data_writer_t *data, file_info_t *fi,
int infd, int flags)
{
uint64_t count;
- ssize_t ret;
size_t diff;
if ((flags & DW_ALLIGN_DEVBLK) && allign_file(data) != 0)
@@ -174,11 +173,8 @@ int write_data_from_fd(data_writer_t *data, file_info_t *fi,
diff = count > (uint64_t)data->super->block_size ?
data->super->block_size : count;
- ret = read_retry(infd, data->block, diff);
- if (ret < 0)
- goto fail_read;
- if ((size_t)ret < diff)
- goto fail_trunc;
+ if (read_data(fi->input_file, infd, data->block, diff))
+ return -1;
if (flush_data_block(data, diff, fi, flags))
return -1;
@@ -188,12 +184,6 @@ int write_data_from_fd(data_writer_t *data, file_info_t *fi,
return -1;
return 0;
-fail_read:
- perror(fi->input_file);
- return -1;
-fail_trunc:
- fprintf(stderr, "%s: truncated read\n", fi->input_file);
- return -1;
}
int write_data_from_fd_condensed(data_writer_t *data, file_info_t *fi,
@@ -202,7 +192,6 @@ int write_data_from_fd_condensed(data_writer_t *data, file_info_t *fi,
size_t start, count, diff;
sparse_map_t *m;
uint64_t offset;
- ssize_t ret;
if ((flags & DW_ALLIGN_DEVBLK) && allign_file(data) != 0)
return -1;
@@ -246,12 +235,10 @@ int write_data_from_fd_condensed(data_writer_t *data, file_info_t *fi,
if (start + count > diff)
count = diff - start;
- ret = read_retry(infd, (char *)data->block + start,
- count);
- if (ret < 0)
- goto fail_read;
- if ((size_t)ret < count)
- goto fail_trunc;
+ if (read_data(fi->input_file, infd,
+ (char *)data->block + start, count)) {
+ return -1;
+ }
map = map->next;
}
@@ -273,12 +260,6 @@ fail_map:
"%s: sparse file map is unordered or self overlapping\n",
fi->input_file);
return -1;
-fail_read:
- perror(fi->input_file);
- return -1;
-fail_trunc:
- fprintf(stderr, "%s: truncated read\n", fi->input_file);
- return -1;
}
data_writer_t *data_writer_create(sqfs_super_t *super, compressor_t *cmp,
diff --git a/lib/sqfs/frag_reader.c b/lib/sqfs/frag_reader.c
index 0d13bcd..6fa160b 100644
--- a/lib/sqfs/frag_reader.c
+++ b/lib/sqfs/frag_reader.c
@@ -42,16 +42,8 @@ static int precache_block(frag_reader_t *f, size_t i)
return -1;
}
- ret = read_retry(f->fd, f->buffer, size);
- if (ret < 0) {
- perror("reading fragment");
+ if (read_data("reading fragment", f->fd, f->buffer, size))
return -1;
- }
-
- if ((size_t)ret < size) {
- fputs("reading fragment: unexpected end of file\n", stderr);
- return -1;
- }
if (compressed) {
ret = f->cmp->do_block(f->cmp, f->buffer, size,
@@ -62,11 +54,12 @@ static int precache_block(frag_reader_t *f, size_t i)
return -1;
}
+ size = ret;
memmove(f->buffer, f->buffer + f->block_size, ret);
}
f->current_index = i;
- f->used = ret;
+ f->used = size;
return 0;
}
@@ -79,7 +72,6 @@ frag_reader_t *frag_reader_create(sqfs_super_t *super, int fd,
uint64_t *locations = NULL;
meta_reader_t *m = NULL;
frag_reader_t *f = NULL;
- ssize_t ret;
count = super->fragment_entry_count;
blockcount = count / (SQFS_META_BLOCK_SIZE / sizeof(tbl[0]));
@@ -104,12 +96,10 @@ frag_reader_t *frag_reader_create(sqfs_super_t *super, int fd,
if (lseek(fd, super->fragment_table_start, SEEK_SET) == (off_t)-1)
goto fail_seek;
- ret = read_retry(fd, locations, blockcount * sizeof(locations[0]));
- if (ret < 0)
- goto fail_rd;
-
- if ((size_t)ret < (blockcount * sizeof(locations[0])))
- goto fail_trunc;
+ if (read_data("reading fragment table", fd, locations,
+ blockcount * sizeof(locations[0]))) {
+ goto fail;
+ }
for (i = 0; i < blockcount; ++i)
locations[i] = le64toh(locations[i]);
@@ -150,9 +140,6 @@ frag_reader_t *frag_reader_create(sqfs_super_t *super, int fd,
fail_seek:
perror("seek to fragment table");
goto fail;
-fail_trunc:
- fputs("reading fragment table: unexpected end of file\n", stderr);
- 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 58b0cf9..e13f3b1 100644
--- a/lib/sqfs/id_table_read.c
+++ b/lib/sqfs/id_table_read.c
@@ -14,7 +14,6 @@ int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super,
uint64_t blocks[32];
meta_reader_t *m;
uint32_t *ptr;
- ssize_t ret;
if (tbl->ids != NULL) {
free(tbl->ids);
@@ -44,12 +43,10 @@ int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super,
if (super->id_count % 2048)
++block_count;
- ret = read_retry(fd, blocks, sizeof(blocks[0]) * block_count);
- if (ret < 0)
- goto fail_rd;
-
- if ((size_t)ret < sizeof(blocks[0]) * block_count)
- goto fail_trunc;
+ if (read_data("reading ID table", fd, blocks,
+ sizeof(blocks[0]) * block_count)) {
+ return -1;
+ }
for (i = 0; i < block_count; ++i)
blocks[i] = le64toh(blocks[i]);
@@ -83,12 +80,6 @@ int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super,
fail_meta:
meta_reader_destroy(m);
return -1;
-fail_trunc:
- fputs("reading ID table: unexpected end of file\n", stderr);
- return -1;
-fail_rd:
- perror("reading ID table");
- 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 e9b599f..c85d063 100644
--- a/lib/sqfs/meta_reader.c
+++ b/lib/sqfs/meta_reader.c
@@ -71,12 +71,8 @@ int meta_reader_seek(meta_reader_t *m, uint64_t block_start, size_t offset)
return -1;
}
- ret = read_retry(m->fd, &header, 2);
- if (ret < 0)
- goto fail_rd;
-
- if ((size_t)ret < 2)
- goto fail_trunc;
+ if (read_data("reading meta data header", m->fd, &header, 2))
+ return -1;
header = le16toh(header);
compressed = (header & 0x8000) == 0;
@@ -94,12 +90,8 @@ int meta_reader_seek(meta_reader_t *m, uint64_t block_start, size_t offset)
memset(m->data, 0, sizeof(m->data));
- ret = read_retry(m->fd, m->data, size);
- if (ret < 0)
- goto fail_rd;
-
- if ((size_t)ret < size)
- goto fail_trunc;
+ if (read_data("reading meta data block", m->fd, m->data, size))
+ return -1;
if (compressed) {
ret = m->cmp->do_block(m->cmp, m->data, size,
@@ -117,12 +109,6 @@ int meta_reader_seek(meta_reader_t *m, uint64_t block_start, size_t offset)
}
return 0;
-fail_trunc:
- fputs("reading meta data: unexpected end of file\n", stderr);
- return -1;
-fail_rd:
- perror("reading meta data");
- return -1;
}
int meta_reader_read(meta_reader_t *m, void *data, size_t size)
diff --git a/lib/sqfs/read_super.c b/lib/sqfs/read_super.c
index 7b738ea..8c43fe0 100644
--- a/lib/sqfs/read_super.c
+++ b/lib/sqfs/read_super.c
@@ -11,23 +11,13 @@ int sqfs_super_read(sqfs_super_t *super, int fd)
{
size_t block_size = 0;
sqfs_super_t temp;
- ssize_t ret;
int i;
if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
goto fail_seek;
- ret = read_retry(fd, &temp, sizeof(temp));
-
- if (ret < 0) {
- perror("reading super block");
+ if (read_data("reading super block", fd, &temp, sizeof(temp)))
return -1;
- }
-
- if ((size_t)ret < sizeof(temp)) {
- fputs("reading super block: unexpected end of file\n", stderr);
- return -1;
- }
temp.magic = le32toh(temp.magic);
temp.inode_count = le32toh(temp.inode_count);