diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index e1e2be08..12917723 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -39,6 +39,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + #include #include @@ -1034,22 +1038,28 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) { std::unique_ptr 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(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(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(); } @@ -1164,8 +1174,8 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer& iol) { try { fsw = std::make_unique( - *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 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(os.get())) { - ofs->close(); + if (os) { + if (auto ofs = dynamic_cast(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(os.get())) { + assert(oss->str().empty()); + } else { + assert(false); } - } else if (auto oss [[maybe_unused]] = - dynamic_cast(os.get())) { - assert(oss->str().empty()); - } else { - assert(false); - } - os.reset(); + os.reset(); + } if (!options.debug_filter_function) { std::ostringstream err; diff --git a/test/tools_test.cpp b/test/tools_test.cpp index d8332c61..bbff4822 100644 --- a/test/tools_test.cpp +++ b/test/tools_test.cpp @@ -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));