Electroneum
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#pragma once
33
34#include <boost/thread/locks.hpp>
35#include <boost/thread/mutex.hpp>
36#include <boost/optional.hpp>
37#include <system_error>
38#include <csignal>
39#include <cstdio>
40#include <functional>
41#include <memory>
42#include <string>
43
44#ifdef _WIN32
45#include "windows.h"
46#include "misc_log_ex.h"
47#endif
48
49#include "crypto/hash.h"
50
56namespace tools
57{
60 {
61 void operator()(std::FILE* handle) const noexcept
62 {
63 if (handle)
64 {
65 std::fclose(handle);
66 }
67 }
68 };
69
71 class private_file {
72 std::unique_ptr<std::FILE, close_file> m_handle;
73 std::string m_filename;
74
75 private_file(std::FILE* handle, std::string&& filename) noexcept;
76 public:
77
79 private_file() noexcept;
80
83 static private_file create(std::string filename);
84
85 private_file(private_file&&) = default;
86 private_file& operator=(private_file&&) = default;
87
89 ~private_file() noexcept;
90
91 std::FILE* handle() const noexcept { return m_handle.get(); }
92 const std::string& filename() const noexcept { return m_filename; }
93 };
94
96 {
97 public:
98 file_locker(const std::string &filename);
100 bool locked() const;
101 private:
102#ifdef WIN32
103 HANDLE m_fd;
104#else
105 int m_fd;
106#endif
107 };
108
119 std::string get_default_data_dir();
120
121#ifdef WIN32
130 std::string get_special_folder_path(int nfolder, bool iscreate);
131#endif
132
139 std::string get_os_version_string();
140
146 bool create_directories_if_necessary(const std::string& path);
149 std::error_code replace_file(const std::string& old_name, const std::string& new_name);
150
151 bool sanitize_locale();
152
153 bool disable_core_dumps();
154
155 bool on_startup();
156
160 {
161 public:
163 template<typename T>
164 static bool install(T t)
165 {
166#if defined(WIN32)
167 bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE);
168 if (r)
169 {
170 m_handler = t;
171 }
172 return r;
173#else
174 static struct sigaction sa;
175 memset(&sa, 0, sizeof(struct sigaction));
176 sa.sa_handler = posix_handler;
177 sa.sa_flags = 0;
178 /* Only blocks SIGINT, SIGTERM and SIGPIPE */
179 sigaction(SIGINT, &sa, NULL);
180 signal(SIGTERM, posix_handler);
181 signal(SIGPIPE, SIG_IGN);
182 m_handler = t;
183 return true;
184#endif
185 }
186
187 private:
188#if defined(WIN32)
190 static BOOL WINAPI win_handler(DWORD type)
191 {
192 if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type)
193 {
194 handle_signal(type);
195 }
196 else
197 {
198 MGINFO_RED("Got control signal " << type << ". Exiting without saving...");
199 return FALSE;
200 }
201 return TRUE;
202 }
203#else
205 static void posix_handler(int type)
206 {
207 handle_signal(type);
208 }
209#endif
210
212 static void handle_signal(int type)
213 {
214 static boost::mutex m_mutex;
215 boost::unique_lock<boost::mutex> lock(m_mutex);
216 m_handler(type);
217 }
218
220 static std::function<void(int)> m_handler;
221 };
222
223 void set_strict_default_file_permissions(bool strict);
224
225 ssize_t get_lockable_memory();
226
227 void set_max_concurrency(unsigned n);
228 unsigned get_max_concurrency();
229
230 bool is_local_address(const std::string &address);
231 int vercmp(const char *v0, const char *v1); // returns < 0, 0, > 0, similar to strcmp, but more human friendly than lexical - does not attempt to validate
232
233 bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
234 bool sha256sum(const std::string &filename, crypto::hash &hash);
235
236 boost::optional<bool> is_hdd(const char *path);
237
238 boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str);
239
240 std::string glob_to_regex(const std::string &val);
241#ifdef _WIN32
242 std::string input_line_win();
243#endif
244
245 void closefrom(int fd);
246
248
249 std::string get_human_readable_bytes(uint64_t bytes);
250
252}
file_locker(const std::string &filename)
Definition util.cpp:238
bool locked() const
Definition util.cpp:296
const std::string & filename() const noexcept
Definition util.h:92
private_file(private_file &&)=default
static private_file create(std::string filename)
Definition util.cpp:125
~private_file() noexcept
Deletes filename() and closes handle().
Definition util.cpp:228
private_file & operator=(private_file &&)=default
private_file() noexcept
handle() == nullptr && filename.empty().
Definition util.cpp:120
std::FILE * handle() const noexcept
Definition util.h:91
Defines a signal handler for win32 and *nix.
Definition util.h:160
static bool install(T t)
installs a signal handler
Definition util.h:164
#define MGINFO_RED(x)
Definition misc_log_ex.h:81
POD_CLASS hash
Definition hash.h:50
STL namespace.
Various Tools.
Definition tools.cpp:31
std::string get_human_readable_timestamp(uint64_t ts)
Definition util.cpp:1077
void closefrom(int fd)
Definition util.cpp:1058
int vercmp(const char *v0, const char *v1)
Definition util.cpp:915
bool disable_core_dumps()
Definition util.cpp:748
void set_max_concurrency(unsigned n)
Definition util.cpp:857
int display_simple_progress_spinner(int x)
Definition util.cpp:995
boost::optional< bool > is_hdd(const char *file_path)
Definition util.cpp:813
void set_strict_default_file_permissions(bool strict)
Definition util.cpp:803
std::error_code replace_file(const std::string &old_name, const std::string &new_name)
std::rename wrapper for nix and something strange for windows.
Definition util.cpp:648
bool sanitize_locale()
Definition util.cpp:690
std::string glob_to_regex(const std::string &val)
Definition util.cpp:1012
boost::optional< std::pair< uint32_t, uint32_t > > parse_subaddress_lookahead(const std::string &str)
Definition util.cpp:977
bool create_directories_if_necessary(const std::string &path)
creates directories for a path
Definition util.cpp:625
bool is_local_address(const std::string &address)
Definition util.cpp:874
ssize_t get_lockable_memory()
Definition util.cpp:763
bool on_startup()
Definition util.cpp:778
std::string get_human_readable_bytes(uint64_t bytes)
Definition util.cpp:1089
unsigned get_max_concurrency()
Definition util.cpp:868
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash)
Definition util.cpp:933
std::string get_os_version_string()
Returns the OS version string.
Definition util.cpp:566
std::string get_default_data_dir()
Returns the default data directory.
Definition util.cpp:600
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
Functional class for closing C file handles.
Definition util.h:60
void operator()(std::FILE *handle) const noexcept
Definition util.h:61
const char * address
Definition multisig.cpp:37
#define T(x)