diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 1b085c394e..f39ad1e9f4 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/core_test/entry.cpp b/nano/core_test/entry.cpp index 7c51ce85b9..929544a834 100644 --- a/nano/core_test/entry.cpp +++ b/nano/core_test/entry.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/nano/core_test/utility.cpp b/nano/core_test/utility.cpp index 30ee29544e..1c12eee0c5 100644 --- a/nano/core_test/utility.cpp +++ b/nano/core_test/utility.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/nano/lib/CMakeLists.txt b/nano/lib/CMakeLists.txt index 0caefa9f74..9ef472a1b4 100644 --- a/nano/lib/CMakeLists.txt +++ b/nano/lib/CMakeLists.txt @@ -51,6 +51,8 @@ add_library( epochs.hpp errors.hpp errors.cpp + files.hpp + files.cpp fwd.hpp id_dispenser.hpp interval.hpp diff --git a/nano/lib/assert.cpp b/nano/lib/assert.cpp index 7d8652efb9..cc2a9bb32d 100644 --- a/nano/lib/assert.cpp +++ b/nano/lib/assert.cpp @@ -1,6 +1,10 @@ #include +#include #include +#include + +#include #include /* diff --git a/nano/lib/files.cpp b/nano/lib/files.cpp new file mode 100644 index 0000000000..434a32c8f3 --- /dev/null +++ b/nano/lib/files.cpp @@ -0,0 +1,85 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 +#include +#endif + +std::size_t nano::get_file_descriptor_limit () +{ + std::size_t fd_limit = std::numeric_limits::max (); +#ifndef _WIN32 + rlimit limit{}; + if (getrlimit (RLIMIT_NOFILE, &limit) == 0) + { + fd_limit = static_cast (limit.rlim_cur); + } +#endif + return fd_limit; +} + +void nano::set_file_descriptor_limit (std::size_t limit) +{ +#ifndef _WIN32 + rlimit fd_limit{}; + if (-1 == getrlimit (RLIMIT_NOFILE, &fd_limit)) + { + std::cerr << "WARNING: Unable to get current limits for the number of open file descriptors: " << std::strerror (errno); + return; + } + + if (fd_limit.rlim_cur >= limit) + { + return; + } + + fd_limit.rlim_cur = std::min (static_cast (limit), fd_limit.rlim_max); + if (-1 == setrlimit (RLIMIT_NOFILE, &fd_limit)) + { + std::cerr << "WARNING: Unable to set limits for the number of open file descriptors: " << std::strerror (errno); + return; + } +#endif +} + +void nano::initialize_file_descriptor_limit () +{ + nano::set_file_descriptor_limit (DEFAULT_FILE_DESCRIPTOR_LIMIT); + auto limit = nano::get_file_descriptor_limit (); + if (limit < DEFAULT_FILE_DESCRIPTOR_LIMIT) + { + std::cerr << "WARNING: Current file descriptor limit of " << limit << " is lower than the " << DEFAULT_FILE_DESCRIPTOR_LIMIT << " recommended. Node was unable to change it." << std::endl; + } +} + +void nano::remove_all_files_in_dir (std::filesystem::path const & dir) +{ + for (auto & p : std::filesystem::directory_iterator (dir)) + { + auto path = p.path (); + if (std::filesystem::is_regular_file (path)) + { + std::filesystem::remove (path); + } + } +} + +void nano::move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to) +{ + for (auto & p : std::filesystem::directory_iterator (from)) + { + auto path = p.path (); + if (std::filesystem::is_regular_file (path)) + { + std::filesystem::rename (path, to / path.filename ()); + } + } +} \ No newline at end of file diff --git a/nano/lib/files.hpp b/nano/lib/files.hpp new file mode 100644 index 0000000000..b0ed9dcaa6 --- /dev/null +++ b/nano/lib/files.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include + +namespace nano +{ +/* + * Functions for managing filesystem permissions, platform specific + */ +void set_umask (); +void set_secure_perm_directory (std::filesystem::path const & path); +void set_secure_perm_directory (std::filesystem::path const & path, std::error_code & ec); +void set_secure_perm_file (std::filesystem::path const & path); +void set_secure_perm_file (std::filesystem::path const & path, std::error_code & ec); + +/* + * Function to check if running Windows as an administrator + */ +bool is_windows_elevated (); + +/* + * Function to check if the Windows Event log registry key exists + */ +bool event_log_reg_entry_exists (); + +/* + * Create the load memory addresses for the executable and shared libraries. + */ +void create_load_memory_address_files (); + +/** + * Some systems, especially in virtualized environments, may have very low file descriptor limits, + * causing the node to fail. This function attempts to query the limit and returns the value. If the + * limit cannot be queried, or running on a Windows system, this returns max-value of std::size_t. + * Increasing the limit programmatically can be done only for the soft limit, the hard one requiring + * super user permissions to modify. + */ +std::size_t get_file_descriptor_limit (); +void set_file_descriptor_limit (std::size_t limit); +/** + * This should be called from entry points. It sets the file descriptor limit to the maximum allowed and logs any errors. + */ +constexpr std::size_t DEFAULT_FILE_DESCRIPTOR_LIMIT = 16384; +void initialize_file_descriptor_limit (); + +void remove_all_files_in_dir (std::filesystem::path const & dir); +void move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to); +} \ No newline at end of file diff --git a/nano/lib/jsonconfig.cpp b/nano/lib/jsonconfig.cpp index 74523154c2..a92ac7ca00 100644 --- a/nano/lib/jsonconfig.cpp +++ b/nano/lib/jsonconfig.cpp @@ -1,4 +1,5 @@ #include +#include #include #include diff --git a/nano/lib/jsonconfig.hpp b/nano/lib/jsonconfig.hpp index 27827dfc72..d7ca5fc33f 100644 --- a/nano/lib/jsonconfig.hpp +++ b/nano/lib/jsonconfig.hpp @@ -7,6 +7,7 @@ #include #include +#include #include namespace boost diff --git a/nano/lib/plat/default/debugging.cpp b/nano/lib/plat/default/debugging.cpp index 1489fa80ac..3ca179f024 100644 --- a/nano/lib/plat/default/debugging.cpp +++ b/nano/lib/plat/default/debugging.cpp @@ -1,3 +1,4 @@ +#include #include void nano::create_load_memory_address_files () diff --git a/nano/lib/plat/linux/debugging.cpp b/nano/lib/plat/linux/debugging.cpp index caee1b910e..e9f4207a88 100644 --- a/nano/lib/plat/linux/debugging.cpp +++ b/nano/lib/plat/linux/debugging.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/nano/lib/plat/posix/perms.cpp b/nano/lib/plat/posix/perms.cpp index 5512c4546b..2544451af6 100644 --- a/nano/lib/plat/posix/perms.cpp +++ b/nano/lib/plat/posix/perms.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/nano/lib/plat/windows/perms.cpp b/nano/lib/plat/windows/perms.cpp index dc5122b7f2..ac2ff14840 100644 --- a/nano/lib/plat/windows/perms.cpp +++ b/nano/lib/plat/windows/perms.cpp @@ -1,3 +1,4 @@ +#include #include // clang-format off diff --git a/nano/lib/plat/windows/priority.cpp b/nano/lib/plat/windows/priority.cpp index e9945b0b0f..4a87451708 100644 --- a/nano/lib/plat/windows/priority.cpp +++ b/nano/lib/plat/windows/priority.cpp @@ -1,4 +1,5 @@ #include + namespace nano { void work_thread_reprioritize () diff --git a/nano/lib/tomlconfig.cpp b/nano/lib/tomlconfig.cpp index ff998dbab9..4922d67fcf 100644 --- a/nano/lib/tomlconfig.cpp +++ b/nano/lib/tomlconfig.cpp @@ -1,4 +1,5 @@ #include +#include #include nano::tomlconfig::tomlconfig () : diff --git a/nano/lib/tomlconfig.hpp b/nano/lib/tomlconfig.hpp index a56524c021..bf32806cdd 100644 --- a/nano/lib/tomlconfig.hpp +++ b/nano/lib/tomlconfig.hpp @@ -6,6 +6,8 @@ #include #include +#include + #include namespace boost diff --git a/nano/lib/utility.cpp b/nano/lib/utility.cpp index 6eaf6fa6c1..9fa56e9589 100644 --- a/nano/lib/utility.cpp +++ b/nano/lib/utility.cpp @@ -1,92 +1,7 @@ -#include #include -#include #include -#include -#include -#include -#include -#include -#include -#include - -#ifndef _WIN32 -#include -#endif - -std::size_t nano::get_file_descriptor_limit () -{ - std::size_t fd_limit = std::numeric_limits::max (); -#ifndef _WIN32 - rlimit limit{}; - if (getrlimit (RLIMIT_NOFILE, &limit) == 0) - { - fd_limit = static_cast (limit.rlim_cur); - } -#endif - return fd_limit; -} - -void nano::set_file_descriptor_limit (std::size_t limit) -{ -#ifndef _WIN32 - rlimit fd_limit{}; - if (-1 == getrlimit (RLIMIT_NOFILE, &fd_limit)) - { - std::cerr << "WARNING: Unable to get current limits for the number of open file descriptors: " << std::strerror (errno); - return; - } - - if (fd_limit.rlim_cur >= limit) - { - return; - } - - fd_limit.rlim_cur = std::min (static_cast (limit), fd_limit.rlim_max); - if (-1 == setrlimit (RLIMIT_NOFILE, &fd_limit)) - { - std::cerr << "WARNING: Unable to set limits for the number of open file descriptors: " << std::strerror (errno); - return; - } -#endif -} - -void nano::initialize_file_descriptor_limit () -{ - nano::set_file_descriptor_limit (DEFAULT_FILE_DESCRIPTOR_LIMIT); - auto limit = nano::get_file_descriptor_limit (); - if (limit < DEFAULT_FILE_DESCRIPTOR_LIMIT) - { - std::cerr << "WARNING: Current file descriptor limit of " << limit << " is lower than the " << DEFAULT_FILE_DESCRIPTOR_LIMIT << " recommended. Node was unable to change it." << std::endl; - } -} - -void nano::remove_all_files_in_dir (std::filesystem::path const & dir) -{ - for (auto & p : std::filesystem::directory_iterator (dir)) - { - auto path = p.path (); - if (std::filesystem::is_regular_file (path)) - { - std::filesystem::remove (path); - } - } -} - -void nano::move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to) -{ - for (auto & p : std::filesystem::directory_iterator (from)) - { - auto path = p.path (); - if (std::filesystem::is_regular_file (path)) - { - std::filesystem::rename (path, to / path.filename ()); - } - } -} - // Issue #3748 void nano::sort_options_description (const boost::program_options::options_description & source, boost::program_options::options_description & target) { diff --git a/nano/lib/utility.hpp b/nano/lib/utility.hpp index d4ea83ba9a..ef31b01e35 100644 --- a/nano/lib/utility.hpp +++ b/nano/lib/utility.hpp @@ -6,10 +6,7 @@ #include -#include -#include #include -#include #include #include @@ -31,48 +28,6 @@ namespace nano // Lower priority of calling work generating thread void work_thread_reprioritize (); -/* - * Functions for managing filesystem permissions, platform specific - */ -void set_umask (); -void set_secure_perm_directory (std::filesystem::path const & path); -void set_secure_perm_directory (std::filesystem::path const & path, std::error_code & ec); -void set_secure_perm_file (std::filesystem::path const & path); -void set_secure_perm_file (std::filesystem::path const & path, std::error_code & ec); - -/* - * Function to check if running Windows as an administrator - */ -bool is_windows_elevated (); - -/* - * Function to check if the Windows Event log registry key exists - */ -bool event_log_reg_entry_exists (); - -/* - * Create the load memory addresses for the executable and shared libraries. - */ -void create_load_memory_address_files (); - -/** - * Some systems, especially in virtualized environments, may have very low file descriptor limits, - * causing the node to fail. This function attempts to query the limit and returns the value. If the - * limit cannot be queried, or running on a Windows system, this returns max-value of std::size_t. - * Increasing the limit programmatically can be done only for the soft limit, the hard one requiring - * super user permissions to modify. - */ -std::size_t get_file_descriptor_limit (); -void set_file_descriptor_limit (std::size_t limit); -/** - * This should be called from entry points. It sets the file descriptor limit to the maximum allowed and logs any errors. - */ -constexpr std::size_t DEFAULT_FILE_DESCRIPTOR_LIMIT = 16384; -void initialize_file_descriptor_limit (); - -void remove_all_files_in_dir (std::filesystem::path const & dir); -void move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to); - template void transform_if (InputIt first, InputIt last, OutputIt dest, Pred pred, Func transform) { diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 45eaea5509..2922b729de 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 5477769e7d..740916bb29 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index c54b335869..3b59879335 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 4f149aa51d..a8ce90d34b 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/node/cli.cpp b/nano/node/cli.cpp index 6128a1c3e5..4f060f4bf3 100644 --- a/nano/node/cli.cpp +++ b/nano/node/cli.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/node/node.cpp b/nano/node/node.cpp index af513eae49..c264265a12 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/node/node_wrapper.cpp b/nano/node/node_wrapper.cpp index 5f4f1a17b9..a49fd759bb 100644 --- a/nano/node/node_wrapper.cpp +++ b/nano/node/node_wrapper.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index ba65fe0dc5..c2c2101c70 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/qt_test/entry.cpp b/nano/qt_test/entry.cpp index 1847ff850d..e0a4255078 100644 --- a/nano/qt_test/entry.cpp +++ b/nano/qt_test/entry.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/nano/rpc_test/entry.cpp b/nano/rpc_test/entry.cpp index e16a3f4e5f..4cfa6e0b3d 100644 --- a/nano/rpc_test/entry.cpp +++ b/nano/rpc_test/entry.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index 4533eddccd..7ca03aa573 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/slow_test/entry.cpp b/nano/slow_test/entry.cpp index 83d12130de..0659075b55 100644 --- a/nano/slow_test/entry.cpp +++ b/nano/slow_test/entry.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index b30718960f..eeae96d584 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/nano/store/rocksdb/rocksdb.cpp b/nano/store/rocksdb/rocksdb.cpp index 75f3e51497..b356064f42 100644 --- a/nano/store/rocksdb/rocksdb.cpp +++ b/nano/store/rocksdb/rocksdb.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include