Skip to content

Commit

Permalink
refactor(mkdwarfs): abstract out file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Dec 28, 2023
1 parent f358397 commit 73cd7d8
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 27 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ list(
src/dwarfs/entry.cpp
src/dwarfs/error.cpp
src/dwarfs/features.cpp
src/dwarfs/file_access_generic.cpp
src/dwarfs/file_scanner.cpp
src/dwarfs/file_stat.cpp
src/dwarfs/file_type.cpp
Expand Down
49 changes: 49 additions & 0 deletions include/dwarfs/file_access.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz ([email protected])
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <filesystem>
#include <iosfwd>
#include <memory>
#include <system_error>

namespace dwarfs {

class output_stream {
public:
virtual ~output_stream() = default;

virtual std::ostream& os() = 0;
virtual void close(std::error_code& ec) = 0;
};

class file_access {
public:
virtual ~file_access() = default;

virtual bool exists(std::filesystem::path const& path) const = 0;
virtual std::unique_ptr<output_stream>
open_output_binary(std::filesystem::path const& path,
std::error_code& ec) const = 0;
};

} // namespace dwarfs
32 changes: 32 additions & 0 deletions include/dwarfs/file_access_generic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz ([email protected])
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <memory>

namespace dwarfs {

class file_access;

std::unique_ptr<file_access const> create_file_access_generic();

} // namespace dwarfs
2 changes: 2 additions & 0 deletions include/dwarfs/iolayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace dwarfs {

class file_access;
class os_access;
class terminal;

Expand All @@ -34,6 +35,7 @@ struct iolayer {

std::shared_ptr<os_access const> os;
std::shared_ptr<terminal const> term;
std::shared_ptr<file_access const> file;
std::istream& in;
std::ostream& out;
std::ostream& err;
Expand Down
91 changes: 91 additions & 0 deletions src/dwarfs/file_access_generic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz ([email protected])
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/

#include <cerrno>
#include <filesystem>
#include <fstream>

#ifdef _WIN32
#include <folly/portability/Windows.h>
#endif

#include "dwarfs/file_access.h"
#include "dwarfs/file_access_generic.h"
#include "dwarfs/util.h"

namespace dwarfs {

namespace {

void assign_error_code(std::error_code& ec) {
#ifdef _WIN32
ec.assign(::GetLastError(), std::system_category());
#else
ec.assign(errno, std::generic_category());
#endif
}

class file_output_stream : public output_stream {
public:
file_output_stream(std::filesystem::path const& path, std::error_code& ec)
: os_{path.string().c_str(), std::ios::binary | std::ios::trunc} {
if (os_.bad() || os_.fail() || !os_.is_open()) {
assign_error_code(ec);
}
}

std::ostream& os() override { return os_; }

void close(std::error_code& ec) override {
os_.close();
if (os_.bad()) {
assign_error_code(ec);
}
}

private:
std::ofstream os_;
};

class file_access_generic : public file_access {
public:
bool exists(std::filesystem::path const& path) const override {
return std::filesystem::exists(path);
}

std::unique_ptr<output_stream>
open_output_binary(std::filesystem::path const& path,
std::error_code& ec) const override {
auto rv = std::make_unique<file_output_stream>(path, ec);
if (ec) {
rv.reset();
}
return rv;
}
};

} // namespace

std::unique_ptr<file_access const> create_file_access_generic() {
return std::make_unique<file_access_generic>();
}

} // namespace dwarfs
3 changes: 3 additions & 0 deletions src/dwarfs/iolayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <iostream>

#include "dwarfs/file_access.h"
#include "dwarfs/file_access_generic.h"
#include "dwarfs/iolayer.h"
#include "dwarfs/os_access_generic.h"
#include "dwarfs/terminal.h"
Expand All @@ -31,6 +33,7 @@ iolayer const& iolayer::system_default() {
static iolayer const iol{
.os = std::make_shared<os_access_generic>(),
.term = terminal::create(),
.file = create_file_access_generic(),
.in = std::cin,
.out = std::cout,
.err = std::cerr,
Expand Down
78 changes: 51 additions & 27 deletions src/mkdwarfs_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <string_view>
#include <thread>
#include <utility>
#include <variant>
#include <vector>

#ifdef _WIN32
Expand All @@ -63,6 +64,7 @@
#include "dwarfs/console_writer.h"
#include "dwarfs/entry.h"
#include "dwarfs/error.h"
#include "dwarfs/file_access.h"
#include "dwarfs/filesystem_block_category_resolver.h"
#include "dwarfs/filesystem_v2.h"
#include "dwarfs/filesystem_writer.h"
Expand All @@ -74,6 +76,7 @@
#include "dwarfs/mmap.h"
#include "dwarfs/options.h"
#include "dwarfs/options_interface.h"
#include "dwarfs/overloaded.h"
#include "dwarfs/program_options_helpers.h"
#include "dwarfs/progress.h"
#include "dwarfs/scanner.h"
Expand Down Expand Up @@ -1035,33 +1038,37 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {

std::filesystem::path output(output_str);

std::unique_ptr<std::ostream> os;
std::variant<std::monostate, std::unique_ptr<output_stream>,
std::ostringstream>
os;

if (!options.debug_filter_function) {
if (output != "-") {
if (std::filesystem::exists(output) && !force_overwrite) {
if (iol.file->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);
std::error_code ec;
auto stream = iol.file->open_output_binary(output, ec);

if (ofs->bad() || !ofs->is_open()) {
if (ec) {
iol.err << "error: cannot open output file '" << output
<< "': " << ::strerror(errno) << "\n";
<< "': " << ec.message() << "\n";
return 1;
}

os = std::move(ofs);
assert(stream);

os.emplace<std::unique_ptr<output_stream>>(std::move(stream));
} else {
#ifdef _WIN32
::_setmode(::_fileno(stdout), _O_BINARY);
#endif
}
} else {
os = std::make_unique<std::ostringstream>();
os.emplace<std::ostringstream>();
}

options.enable_history = !no_history;
Expand Down Expand Up @@ -1173,9 +1180,18 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
std::unique_ptr<filesystem_writer> fsw;

try {
std::ostream& fsw_os = std::visit(
overloaded(
[&](std::monostate) -> std::ostream& { return iol.out; },
[&](std::unique_ptr<output_stream>& os) -> std::ostream& {
return os->os();
},
[&](std::ostringstream& oss) -> std::ostream& { return oss; }),
os);

fsw = std::make_unique<filesystem_writer>(
os ? *os : iol.out, lgr, wg_compress, prog, schema_bc, metadata_bc,
history_bc, fswopts, header_ifs.get());
fsw_os, 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,
Expand Down Expand Up @@ -1245,21 +1261,34 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
if (!options.debug_filter_function) {
LOG_INFO << "compression CPU time: "
<< time_with_unit(wg_compress.get_cpu_time());
}

if (os) {
if (auto ofs = dynamic_cast<std::ofstream*>(os.get())) {
ofs->close();
}

if (os->bad()) {
LOG_ERROR << "failed to close output file '" << output
<< "': " << strerror(errno);
return 1;
}

os.reset();
{
auto ec = std::visit(
overloaded([](std::monostate) -> int { return 0; },
[&](std::unique_ptr<output_stream>& os) -> int {
std::error_code ec;
os->close(ec);
if (ec) {
LOG_ERROR << "failed to close output file '" << output
<< "': " << ec.message();
return 1;
}
os.reset();
return 0;
},
[](std::ostringstream& oss [[maybe_unused]]) -> int {
assert(oss.str().empty());
return 0;
}),
os);

if (ec != 0) {
return ec;
}
}

if (!options.debug_filter_function) {
std::ostringstream err;

if (prog.errors) {
Expand All @@ -1273,11 +1302,6 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {

ti << "filesystem " << (recompress ? "rewritten " : "created ")
<< err.str();
} else {
assert(os);
auto oss [[maybe_unused]] = dynamic_cast<std::ostringstream*>(os.get());
assert(oss);
assert(oss->str().empty());
}

return prog.errors > 0;
Expand Down

0 comments on commit 73cd7d8

Please sign in to comment.