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,22 +1038,28 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
std::unique_ptr<std::ostream> os;
if (!options.debug_filter_function) {
if (std::filesystem::exists(output) && !force_overwrite) {
iol.err
<< "error: output file already exists, use --force to overwrite\n";
return 1;
if (output != "-") {
if (std::filesystem::exists(output) && !force_overwrite) {
iol.err
<< "error: output file already exists, use --force to overwrite\n";
return 1;
}
auto ofs = std::make_unique<std::ofstream>(output, std::ios::binary |
std::ios::trunc);
if (ofs->bad() || !ofs->is_open()) {
iol.err << "error: cannot open output file '" << output
<< "': " << ::strerror(errno) << "\n";
return 1;
}
os = std::move(ofs);
} else {
#ifdef _WIN32
::_setmode(::_fileno(stdout), _O_BINARY);
#endif
}
auto ofs = std::make_unique<std::ofstream>(output, std::ios::binary |
std::ios::trunc);
if (ofs->bad() || !ofs->is_open()) {
iol.err << "error: cannot open output file '" << output
<< "': " << ::strerror(errno) << "\n";
return 1;
}
os = std::move(ofs);
} 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,22 +1247,24 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) {
<< time_with_unit(wg_compress.get_cpu_time());
}
if (auto ofs = dynamic_cast<std::ofstream*>(os.get())) {
ofs->close();
if (os) {
if (auto ofs = dynamic_cast<std::ofstream*>(os.get())) {
ofs->close();
if (ofs->bad()) {
LOG_ERROR << "failed to close output file '" << output
<< "': " << strerror(errno);
return 1;
if (ofs->bad()) {
LOG_ERROR << "failed to close output file '" << output
<< "': " << strerror(errno);
return 1;
}
} else if (auto oss [[maybe_unused]] =
dynamic_cast<std::ostringstream*>(os.get())) {
assert(oss->str().empty());
} else {
assert(false);
}
} else if (auto oss [[maybe_unused]] =
dynamic_cast<std::ostringstream*>(os.get())) {
assert(oss->str().empty());
} else {
assert(false);
}
os.reset();
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));