Skip to content

Commit

Permalink
engine: Replace boost::filesystem with std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Jun 3, 2024
1 parent eb2d595 commit df2c734
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
29 changes: 14 additions & 15 deletions engine/src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@

#include "simulation.hpp"

#include <cstdint> // for uint64_t
#include <fstream> // for ofstream
#include <future> // for future<>, async
#include <sstream> // for stringstream
#include <string> // for string
#include <thread> // for sleep_for

#include <boost/filesystem.hpp> // for is_directory, is_regular_file, ...
#include <cstdint> // for uint64_t
#include <filesystem> // for filesystem::path
#include <fstream> // for ofstream
#include <future> // for future<>, async
#include <sstream> // for stringstream
#include <string> // for string
#include <thread> // for sleep_for

#include <cloe/controller.hpp> // for Controller
#include <cloe/core/abort.hpp> // for AsyncAbort
Expand Down Expand Up @@ -1608,7 +1607,7 @@ size_t Simulation::write_output(const SimulationResult& r) const {
return;
}

boost::filesystem::path filepath = r.get_output_filepath(*filename);
std::filesystem::path filepath = r.get_output_filepath(*filename);
if (write_output_file(filepath, output)) {
files_written++;
}
Expand All @@ -1624,7 +1623,7 @@ size_t Simulation::write_output(const SimulationResult& r) const {
return files_written;
}

bool Simulation::write_output_file(const boost::filesystem::path& filepath,
bool Simulation::write_output_file(const std::filesystem::path& filepath,
const cloe::Json& j) const {
if (!is_writable(filepath)) {
return false;
Expand All @@ -1641,24 +1640,24 @@ bool Simulation::write_output_file(const boost::filesystem::path& filepath,
return true;
}

bool Simulation::is_writable(const boost::filesystem::path& filepath) const {
bool Simulation::is_writable(const std::filesystem::path& filepath) const {
// Make sure we're not clobbering anything if we shouldn't.
auto native = filepath.native();
if (boost::filesystem::exists(filepath)) {
if (std::filesystem::exists(filepath)) {
if (!config_.engine.output_clobber_files) {
logger()->error("Will not clobber file: {}", native);
return false;
}
if (!boost::filesystem::is_regular_file(filepath)) {
if (!std::filesystem::is_regular_file(filepath)) {
logger()->error("Cannot clobber non-regular file: {}", native);
return false;
}
}

// Make sure the directory exists.
auto dirpath = filepath.parent_path();
if (!boost::filesystem::is_directory(dirpath)) {
bool ok = boost::filesystem::create_directories(dirpath);
if (!std::filesystem::is_directory(dirpath)) {
bool ok = std::filesystem::create_directories(dirpath);
if (!ok) {
logger()->error("Error creating leading directories: {}", dirpath.native());
return false;
Expand Down
10 changes: 5 additions & 5 deletions engine/src/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct SimulationResult {
cloe::Json signals; // dump of all signals in DataBroker right before the simulation started
std::vector<std::string>
signals_autocompletion; // pseudo lua file used for vscode autocompletion
std::optional<boost::filesystem::path> output_dir;
std::optional<std::filesystem::path> output_dir;

public:
/**
Expand All @@ -65,8 +65,8 @@ struct SimulationResult {
* and output path are set automatically. Thus, if they are empty, then
* that is because the user explicitly set them so.
*/
boost::filesystem::path get_output_filepath(const boost::filesystem::path& filename) const {
boost::filesystem::path filepath;
std::filesystem::path get_output_filepath(const std::filesystem::path& filename) const {
std::filesystem::path filepath;
if (filename.is_absolute()) {
filepath = filename;
} else if (output_dir) {
Expand Down Expand Up @@ -136,12 +136,12 @@ class Simulation {
/**
* Write the given JSON output into the file. Return true if successful.
*/
bool write_output_file(const boost::filesystem::path& filepath, const cloe::Json& j) const;
bool write_output_file(const std::filesystem::path& filepath, const cloe::Json& j) const;

/**
* Check if the given filepath may be opened, respecting clobber options.
*/
bool is_writable(const boost::filesystem::path& filepath) const;
bool is_writable(const std::filesystem::path& filepath) const;

/**
* Set whether simulation progress should be reported.
Expand Down
20 changes: 10 additions & 10 deletions engine/src/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

#include "stack.hpp"

#include <algorithm> // for transform, swap
#include <map> // for map<>
#include <memory> // for shared_ptr<>
#include <set> // for set<>
#include <string> // for string
#include <algorithm> // for transform, swap
#include <filesystem> // for filesystem
#include <map> // for map<>
#include <memory> // for shared_ptr<>
#include <set> // for set<>
#include <string> // for string

#include <boost/algorithm/string/predicate.hpp> // for starts_with
#include <boost/filesystem.hpp> // for path
namespace fs = boost::filesystem;
namespace fs = std::filesystem;

#include <cloe/utility/std_extensions.hpp> // for join_vector
#include <fable/utility.hpp> // for indent_string
#include <fable/utility.hpp> // for indent_string
#include <fable/utility/string.hpp> // for starts_with

namespace cloe {

Expand Down Expand Up @@ -87,7 +87,7 @@ void LoggingConf::apply() const {

std::string PluginConf::canonical() const {
// Handle builtins specially, these are in a URI form.
if (boost::starts_with(plugin_path.string(), "builtin://")) {
if (fable::starts_with(plugin_path.string(), "builtin://")) {
return plugin_path.string();
}

Expand Down
44 changes: 21 additions & 23 deletions engine/src/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@

#pragma once

#include <map> // for map<>
#include <memory> // for shared_ptr<>
#include <set> // for set<>
#include <string> // for string
#include <utility> // for move
#include <vector> // for vector<>
#include <optional> // for optional<>

#include <boost/filesystem/path.hpp> // for path
#include <fable/schema/boost_optional.hpp> // for Optional<>
#include <fable/schema/boost_path.hpp> // for Path
#include <filesystem> // for filesystem::path
#include <map> // for map<>
#include <memory> // for shared_ptr<>
#include <optional> // for optional<>
#include <set> // for set<>
#include <string> // for string
#include <utility> // for move
#include <vector> // for vector<>

#include <fable/schema/custom.hpp> // for CustomDeserializer
#include <fable/schema/factory.hpp> // for Factory

Expand Down Expand Up @@ -83,7 +81,7 @@ inline auto id_path_prototype(std::string desc = "") {
* IncludeConf is a relative or absolute filepath that should be included in
* the stack configuration.
*/
using IncludeConf = boost::filesystem::path;
using IncludeConf = std::filesystem::path;
using IncludeSchema = decltype(schema::make_schema(static_cast<IncludeConf*>(nullptr), ""));
using IncludesSchema = schema::Vector<IncludeConf, IncludeSchema>;

Expand Down Expand Up @@ -202,7 +200,7 @@ struct ServerConf : public Confable {
*/
struct PluginConf : public PersistentConfable {
/** Filesystem path to file or directory. */
boost::filesystem::path plugin_path{};
std::filesystem::path plugin_path{};

/** Name to give plugin if path is to a single file. */
std::optional<std::string> plugin_name{};
Expand Down Expand Up @@ -309,14 +307,14 @@ struct EngineConf : public Confable {
bool triggers_ignore_source{false};

// Output:
std::optional<boost::filesystem::path> registry_path{CLOE_DATA_HOME "/registry"};
std::optional<boost::filesystem::path> output_path{"${CLOE_SIMULATION_UUID}"};
std::optional<boost::filesystem::path> output_file_config{"config.json"};
std::optional<boost::filesystem::path> output_file_result{"result.json"};
std::optional<boost::filesystem::path> output_file_triggers{"triggers.json"};
std::optional<boost::filesystem::path> output_file_signals{"signals.json"};
std::optional<boost::filesystem::path> output_file_signals_autocompletion;
std::optional<boost::filesystem::path> output_file_data_stream;
std::optional<std::filesystem::path> registry_path{CLOE_DATA_HOME "/registry"};
std::optional<std::filesystem::path> output_path{"${CLOE_SIMULATION_UUID}"};
std::optional<std::filesystem::path> output_file_config{"config.json"};
std::optional<std::filesystem::path> output_file_result{"result.json"};
std::optional<std::filesystem::path> output_file_triggers{"triggers.json"};
std::optional<std::filesystem::path> output_file_signals{"signals.json"};
std::optional<std::filesystem::path> output_file_signals_autocompletion;
std::optional<std::filesystem::path> output_file_data_stream;
bool output_clobber_files{true};

/**
Expand Down Expand Up @@ -390,8 +388,8 @@ struct EngineConf : public Confable {
CONFABLE_SCHEMA(EngineConf) {
// clang-format off
using namespace schema; // NOLINT(build/namespaces)
auto dir_proto = []() { return make_prototype<boost::filesystem::path>().not_file(); };
auto file_proto = []() { return make_prototype<boost::filesystem::path>().not_dir().resolve(false); };
auto dir_proto = []() { return make_prototype<std::filesystem::path>().not_file(); };
auto file_proto = []() { return make_prototype<std::filesystem::path>().not_dir().resolve(false); };
return Struct{
{"ignore", make_schema(&ignore_sections, "JSON pointers to sections that should be ignored").extend(true)},
{"security", Struct{
Expand Down
17 changes: 12 additions & 5 deletions engine/src/stack_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <string> // for string
#include <vector> // for vector<>

#include <boost/filesystem.hpp> // for path

#include <cloe/utility/std_extensions.hpp> // for split_string
#include <cloe/utility/xdg.hpp> // for merge_config
#include <fable/environment.hpp> // for Environment
Expand All @@ -49,7 +47,16 @@ Conf read_conf(const StackOptions& opt, const std::string& filepath) {
// Prepare environment with extra variables:
fable::Environment env(*opt.environment);
if (!filepath.empty() && filepath != "-") {
std::string dirpath = boost::filesystem::canonical(filepath).parent_path().native();
// We use weakly_canonical() because otherwise
// we get an error when calling cloe-engine like:
//
// cloe-engine run <(cat file.json)
std::string dirpath;
if (std::filesystem::is_other(filepath)) {
dirpath = std::filesystem::path(filepath).parent_path().native();
} else {
dirpath = std::filesystem::weakly_canonical(filepath).parent_path().native();
}
env.set("THIS_STACKFILE_FILE", filepath);
env.set("THIS_STACKFILE_DIR", dirpath);
}
Expand Down Expand Up @@ -106,7 +113,7 @@ Stack new_stack(const StackOptions& opt) {

// Interpolate known variables, if requested.
if (opt.interpolate_vars) {
auto interpolate_path = [&opt](std::optional<boost::filesystem::path>& p) {
auto interpolate_path = [&opt](std::optional<std::filesystem::path>& p) {
p = fable::interpolate_vars(p->native(), opt.environment.get());
};
interpolate_path(s.engine.registry_path);
Expand Down Expand Up @@ -150,7 +157,7 @@ Stack new_stack(const StackOptions& opt) {

// Merge system configurations:
if (!opt.no_system_confs) {
auto mergefn = [&](const boost::filesystem::path& file) -> bool {
auto mergefn = [&](const std::filesystem::path& file) -> bool {
s.logger()->info("Include conf {}", file.native());
merge_stack(opt, s, file.native());
return true;
Expand Down
4 changes: 2 additions & 2 deletions engine/src/utility/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CommandResult CommandExecuter::run_and_release(const cloe::Command& cmd) const {
}
try {
if (!cmd.is_sync()) {
r.child = bp::child(cmd.executable(), cmd.args());
r.child = bp::child(cmd.executable().native(), cmd.args());

if (cmd.is_detach()) {
r.child->detach();
Expand All @@ -59,7 +59,7 @@ CommandResult CommandExecuter::run_and_release(const cloe::Command& cmd) const {

// The syntax `(bp::std_out & bp::std_err) > is` is valid and works, but
// was only found by rummaging through the source code. Ridiculous.
r.child = bp::child(cmd.executable(), cmd.args(), (bp::std_out & bp::std_err) > is);
r.child = bp::child(cmd.executable().native(), cmd.args(), (bp::std_out & bp::std_err) > is);

std::string line;
// After finished running output the rest of the lines.
Expand Down
21 changes: 11 additions & 10 deletions engine/src/utility/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <optional> // for optional<>
#include <string> // for string
#include <system_error> // for system_error
#include <utility> // for move
#include <vector> // for vector<>

#include <boost/process/child.hpp> // for child
Expand All @@ -48,12 +49,12 @@ struct CommandResult {
class CommandExecuter {
public:
explicit CommandExecuter(cloe::Logger logger, bool enabled = true)
: logger_(logger), enabled_(enabled) {}
: logger_(std::move(logger)), enabled_(enabled) {}

bool is_enabled() const { return enabled_; }
[[nodiscard]] bool is_enabled() const { return enabled_; }
void set_enabled(bool v) { enabled_ = v; }

CommandResult run_and_release(const cloe::Command&) const;
CommandResult run_and_release(const cloe::Command&) const; // NOLINT

void run(const cloe::Command&);

Expand All @@ -65,7 +66,7 @@ class CommandExecuter {

std::vector<CommandResult> release_all();

cloe::Logger logger() const { return logger_; }
[[nodiscard]] cloe::Logger logger() const { return logger_; }

private:
std::vector<CommandResult> handles_;
Expand All @@ -77,16 +78,16 @@ namespace actions {

class Command : public cloe::Action {
public:
Command(const std::string& name, const cloe::Command& cmd, CommandExecuter* exec)
: Action(name), command_(cmd), executer_(exec) {
Command(const std::string& name, cloe::Command cmd, CommandExecuter* exec)
: Action(name), command_(std::move(cmd)), executer_(exec) {
assert(executer_ != nullptr);
}

cloe::ActionPtr clone() const override {
return std::make_unique<Command>(name(), command_, executer_);
}

cloe::CallbackResult operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;
cloe::CallbackResult operator()(const cloe::Sync& sync, cloe::TriggerRegistrar& registrar) override;

protected:
void to_json(cloe::Json& j) const override;
Expand All @@ -103,9 +104,9 @@ class CommandFactory : public cloe::ActionFactory {
: cloe::ActionFactory("command", "run a system command"), executer_(exec) {
assert(executer_ != nullptr);
}
cloe::TriggerSchema schema() const override;
cloe::ActionPtr make(const cloe::Conf& c) const override;
cloe::ActionPtr make(const std::string& s) const override;
[[nodiscard]] cloe::TriggerSchema schema() const override;
[[nodiscard]] cloe::ActionPtr make(const cloe::Conf& c) const override;
[[nodiscard]] cloe::ActionPtr make(const std::string& s) const override;

private:
CommandExecuter* executer_{nullptr};
Expand Down

0 comments on commit df2c734

Please sign in to comment.