Debian Codegraph @

monero/src/common/util.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #pragma once
32
33 #include <boost/thread/locks.hpp>
34 #include <boost/thread/mutex.hpp>
35 #include <boost/optional.hpp>
36 #include <system_error>
37 #include <csignal>
38 #include <cstdio>
39 #include <functional>
40 #include <memory>
41 #include <string>
42
43 #ifdef _WIN32
44 #include "windows.h"
45 #include "misc_log_ex.h"
46 #endif
47
48 #include "crypto/hash.h"
49
50 /*! \brief Various Tools
51 *
52 *
53 *
54 */
55 namespace tools
56 {
57 //! Functional class for closing C file handles.
58 struct close_file
59 {
60 void operator()(std::FILE* handle) const noexcept
61 {
62 if (handle)
63 {
64 std::fclose(handle);
65 }
66 }
67 };
68
69 void copy_file(const std::string& from, const std::string& to);
70
71 //! A file restricted to process owner AND process. Deletes file on destruction.
72 class private_file {
73 std::unique_ptr<std::FILE, close_file> m_handle;
74 std::string m_filename;
75
76 private_file(std::FILE* handle, std::string&& filename) noexcept;
77 public:
78
79 //! `handle() == nullptr && filename.empty()`.
80 private_file() noexcept;
81
82 /*! \return File only readable by owner and only used by this process
83 OR `private_file{}` on error. */
84 static private_file create(std::string filename, uint32_t extra_flags = 0);
85
86 /*! \return Drop and create file only readable by owner and only used
87 by this process OR `private_file{}` on error. */
88 static private_file drop_and_recreate(std::string filename);
89
90 private_file(private_file&&) = default;
91 private_file& operator=(private_file&&) = default;
92
93 //! Deletes `filename()` and closes `handle()`.
94 ~private_file() noexcept;
95
96 std::FILE* handle() const noexcept { return m_handle.get(); }
97 const std::string& filename() const noexcept { return m_filename; }
98 };
99
100 class file_locker
101 {
102 public:
103 file_locker(const std::string &filename);
104 ~file_locker();
105 bool locked() const;
106 private:
107 #ifdef WIN32
108 HANDLE m_fd;
109 #else
110 int m_fd;
111 #endif
112 };
113
114 /*! \brief Returns the default data directory.
115 *
116 * \details Windows < Vista: C:\\Documents and Settings\\Username\\Application Data\\CRYPTONOTE_NAME
117 *
118 * Windows >= Vista: C:\\Users\\Username\\AppData\\Roaming\\CRYPTONOTE_NAME
119 *
120 * Mac: ~/Library/Application Support/CRYPTONOTE_NAME
121 *
122 * Unix: ~/.CRYPTONOTE_NAME
123 */
124 std::string get_default_data_dir();
125
126 #ifdef WIN32
127 /**
128 * @brief
129 *
130 * @param nfolder
131 * @param iscreate
132 *
133 * @return
134 */
135 std::string get_special_folder_path(int nfolder, bool iscreate);
136 #endif
137
138 /*! \brief Returns the OS version string
139 *
140 * \details This is a wrapper around the primitives
141 * get_windows_version_display_string() and
142 * get_nix_version_display_string()
143 */
144 std::string get_os_version_string();
145
146 /*! \brief creates directories for a path
147 *
148 * wrapper around boost::filesyste::create_directories.
149 * (ensure-directory-exists): greenspun's tenth rule in action!
150 */
151 bool create_directories_if_necessary(const std::string& path);
152 /*! \brief std::rename wrapper for nix and something strange for windows.
153 */
154 std::error_code replace_file(const std::string& old_name, const std::string& new_name);
155
156 bool sanitize_locale();
157
158 bool disable_core_dumps();
159
160 bool on_startup();
161
162 /*! \brief Defines a signal handler for win32 and *nix
163 */
164 class signal_handler
165 {
166 public:
167 /*! \brief installs a signal handler */
168 template<typename T>
169 static bool install(T t)
170 {
171 #if defined(WIN32)
172 bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE);
173 if (r)
174 {
175 m_handler = t;
176 }
177 return r;
178 #else
179 static struct sigaction sa;
180 memset(&sa, 0, sizeof(struct sigaction));
181 sa.sa_handler = posix_handler;
182 sa.sa_flags = 0;
183 /* Only blocks SIGINT, SIGTERM and SIGPIPE */
184 sigaction(SIGINT, &sa, NULL);
185 signal(SIGTERM, posix_handler);
186 signal(SIGPIPE, SIG_IGN);
187 m_handler = t;
188 return true;
189 #endif
190 }
191
192 private:
193 #if defined(WIN32)
194 /*! \brief Handler for win */
195 static BOOL WINAPI win_handler(DWORD type)
196 {
197 if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type)
198 {
199 handle_signal(type);
200 }
201 else
202 {
203 MGINFO_RED("Got control signal " << type << ". Exiting without saving...");
204 return FALSE;
205 }
206 return TRUE;
207 }
208 #else
209 /*! \brief handler for NIX */
210 static void posix_handler(int type)
211 {
212 handle_signal(type);
213 }
214 #endif
215
216 /*! \brief calles m_handler */
217 static void handle_signal(int type)
218 {
219 static boost::mutex m_mutex;
220 boost::unique_lock<boost::mutex> lock(m_mutex);
221 m_handler(type);
222 }
223
224 /*! \brief where the installed handler is stored */
225 static std::function<void(int)> m_handler;
226 };
227
228 void set_strict_default_file_permissions(bool strict);
229
230 ssize_t get_lockable_memory();
231
232 void set_max_concurrency(unsigned n);
233 unsigned get_max_concurrency();
234
235 bool is_local_address(const std::string &address);
236 bool is_privacy_preserving_network(const std::string &address);
237 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
238
239 /**
240 * \brief Creates a SHA-256 digest of a data buffer
241 *
242 * \param[in] data pointer to the buffer
243 * \param[in] len size of the buffer in bytes
244 * \param[out] hash where message digest will be written to
245 *
246 * \returns true if successful, false otherwise
247 */
248 bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
249
250 /**
251 * \brief Creates a SHA-256 digest of a file's contents, equivalent to the sha256sum command in Linux
252 *
253 * \param[in] filename path to target file
254 * \param[out] hash where message digest will be written to
255 *
256 * \returns true if successful, false if the file can not be opened or there is an OpenSSL failure
257 *
258 * \throws ios_base::failure if after the file is successfully opened, an error occurs during reading
259 */
260 bool sha256sum(const std::string &filename, crypto::hash &hash);
261
262 boost::optional<bool> is_hdd(const char *path);
263
264 boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str);
265
266 std::string glob_to_regex(const std::string &val);
267 #ifdef _WIN32
268 std::string input_line_win();
269 #endif
270
271 void closefrom(int fd);
272
273 std::string get_human_readable_timestamp(uint64_t ts);
274
275 std::string get_human_readable_timespan(uint64_t seconds);
276
277 std::string get_human_readable_bytes(uint64_t bytes);
278
279 void clear_screen();
280
281 std::vector<std::pair<std::string, size_t>> split_string_by_width(const std::string &s, size_t columns);
282 }