summaryrefslogtreecommitdiff
path: root/lib
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
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')
-rw-r--r--lib/common/data_reader_dump.c57
-rw-r--r--lib/tar/Makemodule.am3
-rw-r--r--lib/tar/padd_file.c6
-rw-r--r--lib/tar/write_header.c30
-rw-r--r--lib/tar/write_retry.c35
5 files changed, 28 insertions, 103 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;
}
diff --git a/lib/tar/Makemodule.am b/lib/tar/Makemodule.am
index fe18895..7ba0454 100644
--- a/lib/tar/Makemodule.am
+++ b/lib/tar/Makemodule.am
@@ -3,8 +3,7 @@ libtar_a_SOURCES += lib/tar/number.c lib/tar/checksum.c lib/tar/cleanup.c
libtar_a_SOURCES += lib/tar/read_sparse_map.c lib/tar/read_sparse_map_old.c
libtar_a_SOURCES += lib/tar/base64.c lib/tar/urldecode.c lib/tar/internal.h
libtar_a_SOURCES += lib/tar/padd_file.c lib/tar/read_retry.c include/tar.h
-libtar_a_SOURCES += lib/tar/write_retry.c lib/tar/pax_header.c
-libtar_a_SOURCES += lib/tar/read_sparse_map_new.c
+libtar_a_SOURCES += lib/tar/pax_header.c lib/tar/read_sparse_map_new.c
libtar_a_CFLAGS = $(AM_CFLAGS)
libtar_a_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/lib/tar/padd_file.c b/lib/tar/padd_file.c
index dd945a3..1173096 100644
--- a/lib/tar/padd_file.c
+++ b/lib/tar/padd_file.c
@@ -10,7 +10,7 @@
#include <stdlib.h>
#include <stdio.h>
-int padd_file(FILE *fp, sqfs_u64 size)
+int padd_file(ostream_t *fp, sqfs_u64 size)
{
size_t padd_sz = size % TAR_RECORD_SIZE;
int status = -1;
@@ -25,10 +25,8 @@ int padd_file(FILE *fp, sqfs_u64 size)
if (buffer == NULL)
goto fail_errno;
- if (write_retry("padding output file to block size",
- fp, buffer, padd_sz)) {
+ if (ostream_append(fp, buffer, padd_sz))
goto out;
- }
status = 0;
out:
diff --git a/lib/tar/write_header.c b/lib/tar/write_header.c
index aaf9f08..3caa1b3 100644
--- a/lib/tar/write_header.c
+++ b/lib/tar/write_header.c
@@ -53,7 +53,7 @@ static void write_number_signed(char *dst, sqfs_s64 value, int digits)
}
}
-static int write_header(FILE *fp, const struct stat *sb, const char *name,
+static int write_header(ostream_t *fp, const struct stat *sb, const char *name,
const char *slink_target, int type)
{
int maj = 0, min = 0;
@@ -88,10 +88,10 @@ static int write_header(FILE *fp, const struct stat *sb, const char *name,
update_checksum(&hdr);
- return write_retry("writing tar header record", fp, &hdr, sizeof(hdr));
+ return ostream_append(fp, &hdr, sizeof(hdr));
}
-static int write_gnu_header(FILE *fp, const struct stat *orig,
+static int write_gnu_header(ostream_t *fp, const struct stat *orig,
const char *payload, size_t payload_len,
int type, const char *name)
{
@@ -104,10 +104,8 @@ static int write_gnu_header(FILE *fp, const struct stat *orig,
if (write_header(fp, &sb, name, NULL, type))
return -1;
- if (write_retry("writing GNU extension header",
- fp, payload, payload_len)) {
+ if (ostream_append(fp, payload, payload_len))
return -1;
- }
return padd_file(fp, payload_len);
}
@@ -136,7 +134,7 @@ static size_t prefix_digit_len(size_t len)
return ndigit;
}
-static int write_schily_xattr(FILE *fp, const struct stat *orig,
+static int write_schily_xattr(ostream_t *fp, const struct stat *orig,
const char *name, const tar_xattr_t *xattr)
{
static const char *prefix = "SCHILY.xattr.";
@@ -161,15 +159,20 @@ static int write_schily_xattr(FILE *fp, const struct stat *orig,
len = strlen(prefix) + strlen(it->key) + it->value_len + 3;
len += prefix_digit_len(len);
- fprintf(fp, PRI_SZ " %s%s=", len, prefix, it->key);
- fwrite(it->value, 1, it->value_len, fp);
- fputc('\n', fp);
+ if (ostream_printf(fp, PRI_SZ " %s%s=",
+ len, prefix, it->key) < 0) {
+ return -1;
+ }
+ if (ostream_append(fp, it->value, it->value_len))
+ return -1;
+ if (ostream_append(fp, "\n", 1))
+ return -1;
}
return padd_file(fp, total_size);
}
-int write_tar_header(FILE *fp, const struct stat *sb, const char *name,
+int write_tar_header(ostream_t *fp, const struct stat *sb, const char *name,
const char *slink_target, const tar_xattr_t *xattr,
unsigned int counter)
{
@@ -228,7 +231,7 @@ out_skip:
return 1;
}
-int write_hard_link(FILE *fp, const struct stat *sb, const char *name,
+int write_hard_link(ostream_t *fp, const struct stat *sb, const char *name,
const char *target, unsigned int counter)
{
tar_header_t hdr;
@@ -274,6 +277,5 @@ int write_hard_link(FILE *fp, const struct stat *sb, const char *name,
write_number(hdr.devminor, 0, sizeof(hdr.devminor));
update_checksum(&hdr);
- return write_retry("writing tar hard link record",
- fp, &hdr, sizeof(hdr));
+ return ostream_append(fp, &hdr, sizeof(hdr));
}
diff --git a/lib/tar/write_retry.c b/lib/tar/write_retry.c
deleted file mode 100644
index f4f3166..0000000
--- a/lib/tar/write_retry.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/* SPDX-License-Identifier: LGPL-3.0-or-later */
-/*
- * write_retry.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "config.h"
-
-#include <errno.h>
-#include <stdio.h>
-
-#include "tar.h"
-
-int write_retry(const char *errstr, FILE *fp, const void *data, size_t size)
-{
- size_t ret;
-
- while (size > 0) {
- if (feof(fp)) {
- fprintf(stderr, "%s: write truncated\n", errstr);
- return -1;
- }
-
- if (ferror(fp)) {
- fprintf(stderr, "%s: error writing to file\n", errstr);
- return -1;
- }
-
- ret = fwrite(data, 1, size, fp);
- data = (const char *)data + ret;
- size -= ret;
- }
-
- return 0;
-}