aboutsummaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/data_reader_dump.c57
1 files changed, 9 insertions, 48 deletions
diff --git a/lib/common/data_reader_dump.c b/lib/common/data_reader_dump.c
index d832388..7902c25 100644
--- a/lib/common/data_reader_dump.c
+++ b/lib/common/data_reader_dump.c
@@ -11,33 +11,9 @@
#include <stdio.h>
#include <errno.h>
-static int append_block(FILE *fp, const sqfs_u8 *data, size_t size)
-{
- const sqfs_u8 *ptr = data;
- size_t ret;
-
- while (size > 0) {
- if (ferror(fp)) {
- fputs("writing data block: error writing to file\n",
- stderr);
- }
-
- if (feof(fp)) {
- fputs("writing data block: unexpected end of file\n",
- stderr);
- }
-
- ret = fwrite(ptr, 1, size, fp);
- ptr += ret;
- size -= ret;
- }
-
- return 0;
-}
-
int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data,
const sqfs_inode_generic_t *inode,
- FILE *fp, size_t block_size, bool allow_sparse)
+ ostream_t *fp, size_t block_size)
{
size_t i, diff, chunk_size;
sqfs_u64 filesz;
@@ -46,23 +22,12 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data,
sqfs_inode_get_file_size(inode, &filesz);
-#if defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L)
- if (allow_sparse) {
- int fd = fileno(fp);
-
- if (ftruncate(fd, filesz))
- goto fail_sparse;
- }
-#else
- allow_sparse = false;
-#endif
-
for (i = 0; i < sqfs_inode_get_file_block_count(inode); ++i) {
diff = (filesz < block_size) ? filesz : block_size;
- if (SQFS_IS_SPARSE_BLOCK(inode->extra[i]) && allow_sparse) {
- if (fseek(fp, diff, SEEK_CUR) < 0)
- goto fail_sparse;
+ if (SQFS_IS_SPARSE_BLOCK(inode->extra[i])) {
+ if (ostream_append_sparse(fp, diff))
+ return -1;
} else {
err = sqfs_data_reader_get_block(data, inode, i,
&chunk_size, &chunk);
@@ -71,7 +36,7 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data,
return -1;
}
- err = append_block(fp, chunk, chunk_size);
+ err = ostream_append(fp, chunk, chunk_size);
free(chunk);
if (err)
@@ -89,16 +54,12 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data,
return -1;
}
- if (append_block(fp, chunk, chunk_size)) {
- free(chunk);
- return -1;
- }
-
+ err = ostream_append(fp, chunk, chunk_size);
free(chunk);
+
+ if (err)
+ return -1;
}
return 0;
-fail_sparse:
- perror("creating sparse output file");
- return -1;
}