-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move file related utilities to
files.hpp
- Loading branch information
1 parent
731acc2
commit 401a3fd
Showing
32 changed files
with
165 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <nano/lib/files.hpp> | ||
|
||
#include <cstddef> | ||
#include <cstring> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <limits> | ||
#include <sstream> | ||
#include <string_view> | ||
#include <thread> | ||
|
||
#ifndef _WIN32 | ||
#include <sys/resource.h> | ||
#endif | ||
|
||
std::size_t nano::get_file_descriptor_limit () | ||
{ | ||
std::size_t fd_limit = std::numeric_limits<std::size_t>::max (); | ||
#ifndef _WIN32 | ||
rlimit limit{}; | ||
if (getrlimit (RLIMIT_NOFILE, &limit) == 0) | ||
{ | ||
fd_limit = static_cast<std::size_t> (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<rlim_t> (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 ()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <nano/lib/files.hpp> | ||
#include <nano/lib/utility.hpp> | ||
|
||
void nano::create_load_memory_address_files () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <nano/lib/files.hpp> | ||
#include <nano/lib/utility.hpp> | ||
|
||
#include <cstring> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <nano/lib/files.hpp> | ||
#include <nano/lib/utility.hpp> | ||
|
||
#include <sys/stat.h> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <nano/lib/files.hpp> | ||
#include <nano/lib/utility.hpp> | ||
|
||
// clang-format off | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include <windows.h> | ||
|
||
namespace nano | ||
{ | ||
void work_thread_reprioritize () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.