From 0a0cbefc6ebb6174aad3e6f0b8a6dea87aed49da Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 4 Sep 2020 19:26:31 +0200 Subject: 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 --- bin/rdsquashfs/Makemodule.am | 2 +- bin/rdsquashfs/fill_files.c | 30 ++++++++++++++++-------------- bin/rdsquashfs/rdsquashfs.c | 14 ++++++++++++-- bin/sqfs2tar/Makemodule.am | 5 +++-- bin/sqfs2tar/sqfs2tar.c | 21 +++++++++------------ bin/sqfs2tar/sqfs2tar.h | 2 +- bin/sqfs2tar/write_tree.c | 2 +- bin/sqfsdiff/Makemodule.am | 4 ++-- bin/sqfsdiff/extract.c | 13 +++++++------ 9 files changed, 52 insertions(+), 41 deletions(-) (limited to 'bin') diff --git a/bin/rdsquashfs/Makemodule.am b/bin/rdsquashfs/Makemodule.am index e4f3b96..094771c 100644 --- a/bin/rdsquashfs/Makemodule.am +++ b/bin/rdsquashfs/Makemodule.am @@ -4,7 +4,7 @@ rdsquashfs_SOURCES += bin/rdsquashfs/restore_fstree.c bin/rdsquashfs/describe.c rdsquashfs_SOURCES += bin/rdsquashfs/fill_files.c bin/rdsquashfs/dump_xattrs.c rdsquashfs_SOURCES += bin/rdsquashfs/stat.c rdsquashfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -rdsquashfs_LDADD = libcommon.a libcompat.a libsquashfs.la +rdsquashfs_LDADD = libcommon.a libfstream.a libcompat.a libsquashfs.la rdsquashfs_LDADD += libfstree.a $(LZO_LIBS) $(PTHREAD_LIBS) dist_man1_MANS += bin/rdsquashfs/rdsquashfs.1 diff --git a/bin/rdsquashfs/fill_files.c b/bin/rdsquashfs/fill_files.c index b75afbf..63ad640 100644 --- a/bin/rdsquashfs/fill_files.c +++ b/bin/rdsquashfs/fill_files.c @@ -135,29 +135,31 @@ static int gen_file_list_dfs(const sqfs_tree_node_t *n) static int fill_files(sqfs_data_reader_t *data, int flags) { + int ret, openflags; + ostream_t *fp; size_t i; - FILE *fp; + + openflags = OSTREAM_OPEN_OVERWRITE; + + if (flags & UNPACK_NO_SPARSE) + openflags |= OSTREAM_OPEN_SPARSE; for (i = 0; i < num_files; ++i) { - fp = fopen(files[i].path, "wb"); - if (fp == NULL) { - fprintf(stderr, "unpacking %s: %s\n", - files[i].path, strerror(errno)); + fp = ostream_open_file(files[i].path, openflags); + if (fp == NULL) return -1; - } if (!(flags & UNPACK_QUIET)) printf("unpacking %s\n", files[i].path); - if (sqfs_data_reader_dump(files[i].path, data, files[i].inode, - fp, block_size, - (flags & UNPACK_NO_SPARSE) == 0)) { - fclose(fp); - return -1; - } + ret = sqfs_data_reader_dump(files[i].path, data, files[i].inode, + fp, block_size); + if (ret == 0) + ret = ostream_flush(fp); - fflush(fp); - fclose(fp); + sqfs_destroy(fp); + if (ret) + return -1; } return 0; diff --git a/bin/rdsquashfs/rdsquashfs.c b/bin/rdsquashfs/rdsquashfs.c index 1c11afa..5b6ce19 100644 --- a/bin/rdsquashfs/rdsquashfs.c +++ b/bin/rdsquashfs/rdsquashfs.c @@ -115,18 +115,28 @@ int main(int argc, char **argv) if (stat_file(n)) goto out; break; - case OP_CAT: + case OP_CAT: { + ostream_t *fp; + if (!S_ISREG(n->inode->base.mode)) { fprintf(stderr, "/%s: not a regular file\n", opt.cmdpath); goto out; } + fp = ostream_open_stdout(); + if (fp == NULL) + goto out; + if (sqfs_data_reader_dump(opt.cmdpath, data, n->inode, - stdout, super.block_size, false)) { + fp, super.block_size)) { + sqfs_destroy(fp); goto out; } + + sqfs_destroy(fp); break; + } case OP_UNPACK: if (opt.unpack_root != NULL) { if (mkdir_p(opt.unpack_root)) diff --git a/bin/sqfs2tar/Makemodule.am b/bin/sqfs2tar/Makemodule.am index f8d95cc..cd00508 100644 --- a/bin/sqfs2tar/Makemodule.am +++ b/bin/sqfs2tar/Makemodule.am @@ -2,8 +2,9 @@ sqfs2tar_SOURCES = bin/sqfs2tar/sqfs2tar.c bin/sqfs2tar/sqfs2tar.h sqfs2tar_SOURCES += bin/sqfs2tar/options.c bin/sqfs2tar/write_tree.c sqfs2tar_SOURCES += bin/sqfs2tar/xattr.c sqfs2tar_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -sqfs2tar_LDADD = libcommon.a libutil.a libsquashfs.la libtar.a libcompat.a -sqfs2tar_LDADD += libfstree.a $(LZO_LIBS) $(PTHREAD_LIBS) +sqfs2tar_LDADD = libcommon.a libutil.a libsquashfs.la libtar.a +sqfs2tar_LDADD += libfstream.a libcompat.a libfstree.a +sqfs2tar_LDADD += $(LZO_LIBS) $(PTHREAD_LIBS) dist_man1_MANS += bin/sqfs2tar/sqfs2tar.1 bin_PROGRAMS += sqfs2tar diff --git a/bin/sqfs2tar/sqfs2tar.c b/bin/sqfs2tar/sqfs2tar.c index 8c5f631..c5d1201 100644 --- a/bin/sqfs2tar/sqfs2tar.c +++ b/bin/sqfs2tar/sqfs2tar.c @@ -9,7 +9,7 @@ sqfs_xattr_reader_t *xr; sqfs_data_reader_t *data; sqfs_super_t super; -FILE *out_file = NULL; +ostream_t *out_file = NULL; static sqfs_file_t *file; @@ -63,8 +63,7 @@ static int terminate_archive(void) memset(buffer, '\0', sizeof(buffer)); - return write_retry("adding archive terminator", out_file, - buffer, sizeof(buffer)); + return ostream_append(out_file, buffer, sizeof(buffer)); } static sqfs_tree_node_t *tree_merge(sqfs_tree_node_t *lhs, @@ -118,13 +117,7 @@ int main(int argc, char **argv) process_args(argc, argv); -#ifdef _WIN32 - _setmode(_fileno(stdout), _O_BINARY); - out_file = stdout; -#else - out_file = freopen(NULL, "wb", stdout); -#endif - + out_file = ostream_open_stdout(); if (out_file == NULL) { perror("changing stdout to binary mode"); goto out_dirs; @@ -133,7 +126,7 @@ int main(int argc, char **argv) file = sqfs_open_file(filename, SQFS_FILE_OPEN_READ_ONLY); if (file == NULL) { perror(filename); - goto out_dirs; + goto out_ostrm; } ret = sqfs_super_read(&super, file); @@ -244,8 +237,10 @@ int main(int argc, char **argv) if (terminate_archive()) goto out; + if (ostream_flush(out_file)) + goto out; + status = EXIT_SUCCESS; - fflush(out_file); out: if (root != NULL) sqfs_dir_tree_destroy(root); @@ -262,6 +257,8 @@ out_cmp: sqfs_destroy(cmp); out_fd: sqfs_destroy(file); +out_ostrm: + sqfs_destroy(out_file); out_dirs: for (i = 0; i < num_subdirs; ++i) free(subdirs[i]); diff --git a/bin/sqfs2tar/sqfs2tar.h b/bin/sqfs2tar/sqfs2tar.h index 6eee2b1..70f51ef 100644 --- a/bin/sqfs2tar/sqfs2tar.h +++ b/bin/sqfs2tar/sqfs2tar.h @@ -37,7 +37,7 @@ void process_args(int argc, char **argv); extern sqfs_xattr_reader_t *xr; extern sqfs_data_reader_t *data; extern sqfs_super_t super; -extern FILE *out_file; +extern ostream_t *out_file; char *assemble_tar_path(char *name, bool is_dir); diff --git a/bin/sqfs2tar/write_tree.c b/bin/sqfs2tar/write_tree.c index 28e1cf0..e460481 100644 --- a/bin/sqfs2tar/write_tree.c +++ b/bin/sqfs2tar/write_tree.c @@ -105,7 +105,7 @@ static int write_tree_dfs(const sqfs_tree_node_t *n) if (S_ISREG(sb.st_mode)) { if (sqfs_data_reader_dump(name, data, n->inode, out_file, - super.block_size, false)) { + super.block_size)) { free(name); return -1; } diff --git a/bin/sqfsdiff/Makemodule.am b/bin/sqfsdiff/Makemodule.am index b5de502..8331f25 100644 --- a/bin/sqfsdiff/Makemodule.am +++ b/bin/sqfsdiff/Makemodule.am @@ -4,8 +4,8 @@ sqfsdiff_SOURCES += bin/sqfsdiff/compare_dir.c bin/sqfsdiff/node_compare.c sqfsdiff_SOURCES += bin/sqfsdiff/compare_files.c bin/sqfsdiff/super.c sqfsdiff_SOURCES += bin/sqfsdiff/extract.c sqfsdiff_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -sqfsdiff_LDADD = libcommon.a libsquashfs.la libcompat.a $(LZO_LIBS) libfstree.a -sqfsdiff_LDADD += $(PTHREAD_LIBS) +sqfsdiff_LDADD = libcommon.a libsquashfs.la libfstream.a libcompat.a +sqfsdiff_LDADD += $(LZO_LIBS) libfstree.a $(PTHREAD_LIBS) dist_man1_MANS += bin/sqfsdiff/sqfsdiff.1 bin_PROGRAMS += sqfsdiff diff --git a/bin/sqfsdiff/extract.c b/bin/sqfsdiff/extract.c index 979572a..0c68918 100644 --- a/bin/sqfsdiff/extract.c +++ b/bin/sqfsdiff/extract.c @@ -10,7 +10,7 @@ static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, const char *prefix, const char *path, size_t block_size) { char *ptr, *temp; - FILE *fp; + ostream_t *fp; temp = alloca(strlen(prefix) + strlen(path) + 2); sprintf(temp, "%s/%s", prefix, path); @@ -21,19 +21,20 @@ static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, return -1; *ptr = '/'; - fp = fopen(temp, "wb"); + fp = ostream_open_file(temp, OSTREAM_OPEN_OVERWRITE | + OSTREAM_OPEN_SPARSE); if (fp == NULL) { perror(temp); return -1; } - if (sqfs_data_reader_dump(path, data, inode, fp, block_size, true)) { - fclose(fp); + if (sqfs_data_reader_dump(path, data, inode, fp, block_size)) { + sqfs_destroy(fp); return -1; } - fflush(fp); - fclose(fp); + ostream_flush(fp); + sqfs_destroy(fp); return 0; } -- cgit v1.2.3