Skip to content

Commit

Permalink
Use c++ std::filesystem instead of boost::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano authored Oct 18, 2023
1 parent 4b7f91f commit 81178d0
Show file tree
Hide file tree
Showing 58 changed files with 246 additions and 335 deletions.
8 changes: 3 additions & 5 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include <gtest/gtest.h>

#include <boost/filesystem.hpp>

#include <cstdlib>
#include <fstream>
#include <unordered_set>
Expand Down Expand Up @@ -680,7 +678,7 @@ TEST (mdb_block_store, bad_path)
{
std::ofstream stream (path.c_str ());
}
boost::filesystem::permissions (path, boost::filesystem::perms::no_perms);
std::filesystem::permissions (path, std::filesystem::perms::none);
nano::store::lmdb::component store (logger, path, nano::dev::constants);
}
catch (std::runtime_error &)
Expand All @@ -693,7 +691,7 @@ TEST (mdb_block_store, bad_path)
TEST (block_store, DISABLED_already_open) // File can be shared
{
auto path (nano::unique_path ());
boost::filesystem::create_directories (path.parent_path ());
std::filesystem::create_directories (path.parent_path ());
nano::set_secure_perm_directory (path.parent_path ());
std::ofstream file;
file.open (path.string ().c_str ());
Expand Down Expand Up @@ -1457,7 +1455,7 @@ TEST (mdb_block_store, upgrade_backup)
GTEST_SKIP ();
}
auto dir (nano::unique_path ());
namespace fs = boost::filesystem;
namespace fs = std::filesystem;
fs::create_directory (dir);
auto path = dir / "data.ldb";
/** Returns 'dir' if backup file cannot be found */
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ TEST (logger, stable_filename)
auto log_file = path / "log" / "node.log";

#if BOOST_VERSION >= 107000
EXPECT_TRUE (boost::filesystem::exists (log_file));
EXPECT_TRUE (std::filesystem::exists (log_file));
// Try opening it again
logging.release_file_sink ();
logging.init (path);
logger.always_log ("stable2");
#else
// When using Boost < 1.70 , behavior is reverted to not using the stable filename
EXPECT_FALSE (boost::filesystem::exists (log_file));
EXPECT_FALSE (std::filesystem::exists (log_file));
#endif

// Reset the logger
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4312,12 +4312,12 @@ TEST (node_config, node_id_private_key_persistence)

// create the directory and the file
auto path = nano::unique_path ();
ASSERT_TRUE (boost::filesystem::create_directories (path));
ASSERT_TRUE (std::filesystem::create_directories (path));
auto priv_key_filename = path / "node_id_private.key";

// check that the key generated is random when the key does not exist
nano::keypair kp1 = nano::load_or_create_node_id (path, logger);
boost::filesystem::remove (priv_key_filename);
std::filesystem::remove (priv_key_filename);
nano::keypair kp2 = nano::load_or_create_node_id (path, logger);
ASSERT_NE (kp1.prv, kp2.prv);

Expand Down
8 changes: 4 additions & 4 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TEST (toml, diff_equal)
TEST (toml, daemon_config_update_array)
{
nano::tomlconfig t;
boost::filesystem::path data_path (".");
std::filesystem::path data_path (".");
nano::daemon_config c{ data_path, nano::dev::network_params };
c.node.preconfigured_peers.push_back ("dev-peer.org");
c.serialize_toml (t);
Expand Down Expand Up @@ -874,7 +874,7 @@ TEST (toml, daemon_config_deserialize_errors)
TEST (toml, daemon_read_config)
{
auto path (nano::unique_path ());
boost::filesystem::create_directories (path);
std::filesystem::create_directories (path);
nano::daemon_config config;
std::vector<std::string> invalid_overrides1{ "node.max_work_generate_multiplier=0" };
std::string expected_message1{ "max_work_generate_multiplier must be greater than or equal to 1" };
Expand All @@ -883,7 +883,7 @@ TEST (toml, daemon_read_config)
std::string expected_message2{ "Value must follow after a '=' at line 2" };

// Reading when there is no config file
ASSERT_FALSE (boost::filesystem::exists (nano::get_node_toml_config_path (path)));
ASSERT_FALSE (std::filesystem::exists (nano::get_node_toml_config_path (path)));
ASSERT_FALSE (nano::read_node_config_toml (path, config));
{
auto error = nano::read_node_config_toml (path, config, invalid_overrides1);
Expand All @@ -901,7 +901,7 @@ TEST (toml, daemon_read_config)
toml.write (nano::get_node_toml_config_path (path));

// Reading when there is a config file
ASSERT_TRUE (boost::filesystem::exists (nano::get_node_toml_config_path (path)));
ASSERT_TRUE (std::filesystem::exists (nano::get_node_toml_config_path (path)));
ASSERT_FALSE (nano::read_node_config_toml (path, config));
{
auto error = nano::read_node_config_toml (path, config, invalid_overrides1);
Expand Down
32 changes: 16 additions & 16 deletions nano/core_test/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,50 +234,50 @@ TEST (filesystem, remove_all_files)
{
auto path = nano::unique_path ();
auto dummy_directory = path / "tmp";
boost::filesystem::create_directories (dummy_directory);
std::filesystem::create_directories (dummy_directory);

auto dummy_file1 = path / "my_file1.txt";
auto dummy_file2 = path / "my_file2.txt";
std::ofstream (dummy_file1.string ());
std::ofstream (dummy_file2.string ());

// Check all exist
ASSERT_TRUE (boost::filesystem::exists (dummy_directory));
ASSERT_TRUE (boost::filesystem::exists (dummy_file1));
ASSERT_TRUE (boost::filesystem::exists (dummy_file2));
ASSERT_TRUE (std::filesystem::exists (dummy_directory));
ASSERT_TRUE (std::filesystem::exists (dummy_file1));
ASSERT_TRUE (std::filesystem::exists (dummy_file2));

// Should remove only the files
nano::remove_all_files_in_dir (path);

ASSERT_TRUE (boost::filesystem::exists (dummy_directory));
ASSERT_FALSE (boost::filesystem::exists (dummy_file1));
ASSERT_FALSE (boost::filesystem::exists (dummy_file2));
ASSERT_TRUE (std::filesystem::exists (dummy_directory));
ASSERT_FALSE (std::filesystem::exists (dummy_file1));
ASSERT_FALSE (std::filesystem::exists (dummy_file2));
}

TEST (filesystem, move_all_files)
{
auto path = nano::unique_path ();
auto dummy_directory = path / "tmp";
boost::filesystem::create_directories (dummy_directory);
std::filesystem::create_directories (dummy_directory);

auto dummy_file1 = dummy_directory / "my_file1.txt";
auto dummy_file2 = dummy_directory / "my_file2.txt";
std::ofstream (dummy_file1.string ());
std::ofstream (dummy_file2.string ());

// Check all exist
ASSERT_TRUE (boost::filesystem::exists (dummy_directory));
ASSERT_TRUE (boost::filesystem::exists (dummy_file1));
ASSERT_TRUE (boost::filesystem::exists (dummy_file2));
ASSERT_TRUE (std::filesystem::exists (dummy_directory));
ASSERT_TRUE (std::filesystem::exists (dummy_file1));
ASSERT_TRUE (std::filesystem::exists (dummy_file2));

// Should move only the files
nano::move_all_files_to_dir (dummy_directory, path);

ASSERT_TRUE (boost::filesystem::exists (dummy_directory));
ASSERT_TRUE (boost::filesystem::exists (path / "my_file1.txt"));
ASSERT_TRUE (boost::filesystem::exists (path / "my_file2.txt"));
ASSERT_FALSE (boost::filesystem::exists (dummy_file1));
ASSERT_FALSE (boost::filesystem::exists (dummy_file2));
ASSERT_TRUE (std::filesystem::exists (dummy_directory));
ASSERT_TRUE (std::filesystem::exists (path / "my_file1.txt"));
ASSERT_TRUE (std::filesystem::exists (path / "my_file2.txt"));
ASSERT_FALSE (std::filesystem::exists (dummy_file1));
ASSERT_FALSE (std::filesystem::exists (dummy_file2));
}

TEST (relaxed_atomic_integral, basic)
Expand Down
11 changes: 5 additions & 6 deletions nano/lib/config.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/config.hpp>

#include <boost/filesystem/path.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>

Expand Down Expand Up @@ -290,27 +289,27 @@ bool is_sanitizer_build ()
return is_asan_build () || is_tsan_build ();
}

std::string get_node_toml_config_path (boost::filesystem::path const & data_path)
std::string get_node_toml_config_path (std::filesystem::path const & data_path)
{
return (data_path / "config-node.toml").string ();
}

std::string get_rpc_toml_config_path (boost::filesystem::path const & data_path)
std::string get_rpc_toml_config_path (std::filesystem::path const & data_path)
{
return (data_path / "config-rpc.toml").string ();
}

std::string get_qtwallet_toml_config_path (boost::filesystem::path const & data_path)
std::string get_qtwallet_toml_config_path (std::filesystem::path const & data_path)
{
return (data_path / "config-qtwallet.toml").string ();
}

std::string get_access_toml_config_path (boost::filesystem::path const & data_path)
std::string get_access_toml_config_path (std::filesystem::path const & data_path)
{
return (data_path / "config-access.toml").string ();
}

std::string get_tls_toml_config_path (boost::filesystem::path const & data_path)
std::string get_tls_toml_config_path (std::filesystem::path const & data_path)
{
return (data_path / "config-tls.toml").string ();
}
Expand Down
19 changes: 6 additions & 13 deletions nano/lib/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@
#include <algorithm>
#include <array>
#include <chrono>
#include <filesystem>
#include <optional>
#include <string>

using namespace std::chrono_literals;

namespace boost
{
namespace filesystem
{
class path;
}
}

#define xstr(a) ver_str (a)
#define ver_str(a) #a

Expand Down Expand Up @@ -387,11 +380,11 @@ class network_constants
uint8_t const bootstrap_protocol_version_min = 0x13;
};

std::string get_node_toml_config_path (boost::filesystem::path const & data_path);
std::string get_rpc_toml_config_path (boost::filesystem::path const & data_path);
std::string get_access_toml_config_path (boost::filesystem::path const & data_path);
std::string get_qtwallet_toml_config_path (boost::filesystem::path const & data_path);
std::string get_tls_toml_config_path (boost::filesystem::path const & data_path);
std::string get_node_toml_config_path (std::filesystem::path const & data_path);
std::string get_rpc_toml_config_path (std::filesystem::path const & data_path);
std::string get_access_toml_config_path (std::filesystem::path const & data_path);
std::string get_qtwallet_toml_config_path (std::filesystem::path const & data_path);
std::string get_tls_toml_config_path (std::filesystem::path const & data_path);

/** Checks if we are running inside a valgrind instance */
bool running_within_valgrind ();
Expand Down
11 changes: 5 additions & 6 deletions nano/lib/jsonconfig.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <nano/boost/asio/ip/address_v6.hpp>
#include <nano/lib/jsonconfig.hpp>

#include <boost/filesystem/convenience.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <cstddef>
Expand All @@ -25,7 +24,7 @@ nano::jsonconfig::jsonconfig (boost::property_tree::ptree & tree_a, std::shared_
* Reads a json object from the stream
* @return nano::error&, including a descriptive error message if the config file is malformed.
*/
nano::error & nano::jsonconfig::read (boost::filesystem::path const & path_a)
nano::error & nano::jsonconfig::read (std::filesystem::path const & path_a)
{
std::fstream stream;
open_or_create (stream, path_a.string ());
Expand All @@ -48,7 +47,7 @@ nano::error & nano::jsonconfig::read (boost::filesystem::path const & path_a)
return *error;
}

void nano::jsonconfig::write (boost::filesystem::path const & path_a)
void nano::jsonconfig::write (std::filesystem::path const & path_a)
{
std::fstream stream;
open_or_create (stream, path_a.string ());
Expand All @@ -68,7 +67,7 @@ void nano::jsonconfig::read (std::istream & stream_a)
/** Open configuration file, create if necessary */
void nano::jsonconfig::open_or_create (std::fstream & stream_a, std::string const & path_a)
{
if (!boost::filesystem::exists (path_a))
if (!std::filesystem::exists (path_a))
{
// Create temp stream to first create the file
std::ofstream stream (path_a);
Expand All @@ -81,7 +80,7 @@ void nano::jsonconfig::open_or_create (std::fstream & stream_a, std::string cons
}

/** Takes a filepath, appends '_backup_<timestamp>' to the end (but before any extension) and saves that file in the same directory */
void nano::jsonconfig::create_backup_file (boost::filesystem::path const & filepath_a)
void nano::jsonconfig::create_backup_file (std::filesystem::path const & filepath_a)
{
auto extension = filepath_a.extension ();
auto filename_without_extension = filepath_a.filename ().replace_extension ("");
Expand All @@ -93,7 +92,7 @@ void nano::jsonconfig::create_backup_file (boost::filesystem::path const & filep
backup_filename += extension;
auto backup_filepath = backup_path / backup_filename;

boost::filesystem::copy_file (filepath_a, backup_filepath);
std::filesystem::copy_file (filepath_a, backup_filepath);
}

/** Returns the boost property node managed by this instance */
Expand Down
7 changes: 3 additions & 4 deletions nano/lib/jsonconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <nano/lib/errors.hpp>
#include <nano/lib/utility.hpp>

#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>

Expand All @@ -29,12 +28,12 @@ class jsonconfig : public nano::configbase
public:
jsonconfig ();
jsonconfig (boost::property_tree::ptree & tree_a, std::shared_ptr<nano::error> const & error_a = nullptr);
nano::error & read (boost::filesystem::path const & path_a);
void write (boost::filesystem::path const & path_a);
nano::error & read (std::filesystem::path const & path_a);
void write (std::filesystem::path const & path_a);
void write (std::ostream & stream_a) const;
void read (std::istream & stream_a);
void open_or_create (std::fstream & stream_a, std::string const & path_a);
void create_backup_file (boost::filesystem::path const & filepath_a);
void create_backup_file (std::filesystem::path const & filepath_a);
boost::property_tree::ptree const & get_tree ();
bool empty () const;
boost::optional<jsonconfig> get_optional_child (std::string const & key_a);
Expand Down
18 changes: 8 additions & 10 deletions nano/lib/plat/posix/perms.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <nano/lib/utility.hpp>

#include <boost/filesystem.hpp>

#include <sys/stat.h>
#include <sys/types.h>

Expand All @@ -10,22 +8,22 @@ void nano::set_umask ()
umask (077);
}

void nano::set_secure_perm_directory (boost::filesystem::path const & path)
void nano::set_secure_perm_directory (std::filesystem::path const & path)
{
boost::filesystem::permissions (path, boost::filesystem::owner_all);
std::filesystem::permissions (path, std::filesystem::perms::owner_all);
}

void nano::set_secure_perm_directory (boost::filesystem::path const & path, boost::system::error_code & ec)
void nano::set_secure_perm_directory (std::filesystem::path const & path, std::error_code & ec)
{
boost::filesystem::permissions (path, boost::filesystem::owner_all, ec);
std::filesystem::permissions (path, std::filesystem::perms::owner_all, ec);
}

void nano::set_secure_perm_file (boost::filesystem::path const & path)
void nano::set_secure_perm_file (std::filesystem::path const & path)
{
boost::filesystem::permissions (path, boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_write);
std::filesystem::permissions (path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write);
}

void nano::set_secure_perm_file (boost::filesystem::path const & path, boost::system::error_code & ec)
void nano::set_secure_perm_file (std::filesystem::path const & path, std::error_code & ec)
{
boost::filesystem::permissions (path, boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_write, ec);
std::filesystem::permissions (path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write, ec);
}
Loading

0 comments on commit 81178d0

Please sign in to comment.