summaryrefslogtreecommitdiff
path: root/lib/common/data_reader_dump.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-04 19:26:31 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-16 09:34:35 +0200
commit0a0cbefc6ebb6174aad3e6f0b8a6dea87aed49da (patch)
tree7b1a69382f7480e442620b6176c1673abcf81b3c /lib/common/data_reader_dump.c
parent4b994ac359757098ebc09263fff9e2290a58de71 (diff)
Remodel file extraction tools to use libfstream
This commit rewrites the libtar write paths to use libfstream insead of a FILE pointer. Also, the libcommon file extraction function is remodeled to use libfstream. In accordance, rdsquashfs, sqfs2tar and sqfsdiff have some minor adjustments made to work with the ported libtar and libcommon. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common/data_reader_dump.c')
-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;
}