feat(mkdwarfs): support writing file system to stdout

This commit is contained in:
Marcus Holland-Moritz 2023-12-25 22:02:28 +01:00
parent df09b171c2
commit 2bee3ec702
2 changed files with 55 additions and 31 deletions

View File

@ -39,6 +39,10 @@
#include <utility>
#include <vector>
#ifdef _WIN32
#include <io.h>
#endif
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
@ -1034,6 +1038,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
std::unique_ptr<std::ostream> os;
if (!options.debug_filter_function) {
if (output != "-") {
if (std::filesystem::exists(output) && !force_overwrite) {
iol.err
<< "error: output file already exists, use --force to overwrite\n";
@ -1051,6 +1056,11 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
os = std::move(ofs);
} else {
#ifdef _WIN32
::_setmode(::_fileno(stdout), _O_BINARY);
#endif
}
} else {
os = std::make_unique<std::ostringstream>();
}
@ -1164,8 +1174,8 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
try {
fsw = std::make_unique<filesystem_writer>(
*os, lgr, wg_compress, prog, schema_bc, metadata_bc, history_bc,
fswopts, header_ifs.get());
os ? *os : iol.out, lgr, wg_compress, prog, schema_bc, metadata_bc,
history_bc, fswopts, header_ifs.get());
categorized_option<block_compressor> compression_opt;
contextual_option_parser cop("--compression", compression_opt, cp,
@ -1237,6 +1247,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
<< time_with_unit(wg_compress.get_cpu_time());
}
if (os) {
if (auto ofs = dynamic_cast<std::ofstream*>(os.get())) {
ofs->close();
@ -1253,6 +1264,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
}
os.reset();
}
if (!options.debug_filter_function) {
std::ostringstream err;

View File

@ -709,11 +709,23 @@ TEST_P(tools_test, end_to_end) {
EXPECT_EQ(unicode_file_contents, "unicode\n");
ASSERT_TRUE(subprocess::check_run(*mkdwarfs_test_bin, mkdwarfs_tool_arg, "-i",
fsdata_dir, "-o", image, "--no-progress"));
fsdata_dir, "-o", image, "--no-progress",
"--no-history", "--no-create-timestamp"));
ASSERT_TRUE(fs::exists(image));
ASSERT_GT(fs::file_size(image), 1000);
{
auto out = subprocess::check_run(
*mkdwarfs_test_bin, mkdwarfs_tool_arg, "-i", fsdata_dir, "-o", "-",
"--no-progress", "--no-history", "--no-create-timestamp");
ASSERT_TRUE(out);
std::string ref;
ASSERT_TRUE(read_file(image, ref));
EXPECT_EQ(ref.size(), out->size());
EXPECT_EQ(ref, *out);
}
ASSERT_TRUE(subprocess::check_run(
*mkdwarfs_test_bin, mkdwarfs_tool_arg, "-i", image, "-o", image_hdr,
"--no-progress", "--recompress=none", "--header", header_data));