Electroneum
util.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, 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 <system_error>
37 #include <csignal>
38 #include <cstdio>
39 #include <functional>
40 #include <memory>
41 #include <string>
42 
43 #include "crypto/hash.h"
44 
50 namespace tools
51 {
53  struct close_file
54  {
55  void operator()(std::FILE* handle) const noexcept
56  {
57  if (handle)
58  {
59  std::fclose(handle);
60  }
61  }
62  };
63 
65  std::unique_ptr<std::FILE, close_file> create_private_file(const std::string& filename);
66 
77  std::string get_default_data_dir();
78 
79 #ifdef WIN32
80 
88  std::string get_special_folder_path(int nfolder, bool iscreate);
89 #endif
90 
97  std::string get_os_version_string();
98 
104  bool create_directories_if_necessary(const std::string& path);
107  std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name);
108 
109  bool sanitize_locale();
110 
114  {
115  public:
117  template<typename T>
118  static bool install(T t)
119  {
120 #if defined(WIN32)
121  bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE);
122  if (r)
123  {
124  m_handler = t;
125  }
126  return r;
127 #else
128  /* Only blocks SIGINT and SIGTERM */
129  signal(SIGINT, posix_handler);
130  signal(SIGTERM, posix_handler);
131  m_handler = t;
132  return true;
133 #endif
134  }
135 
136  private:
137 #if defined(WIN32)
138 
139  static BOOL WINAPI win_handler(DWORD type)
140  {
141  if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type)
142  {
144  }
145  else
146  {
147  MGINFO_RED("Got control signal " << type << ". Exiting without saving...");
148  return FALSE;
149  }
150  return TRUE;
151  }
152 #else
153 
154  static void posix_handler(int type)
155  {
157  }
158 #endif
159 
161  static void handle_signal(int type)
162  {
163  static boost::mutex m_mutex;
164  boost::unique_lock<boost::mutex> lock(m_mutex);
165  m_handler(type);
166  }
167 
169  static std::function<void(int)> m_handler;
170  };
171 
172  void set_strict_default_file_permissions(bool strict);
173 
174  void set_max_concurrency(unsigned n);
175  unsigned get_max_concurrency();
176 
177  bool is_local_address(const std::string &address);
178  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
179 
180  bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
181  bool sha256sum(const std::string &filename, crypto::hash &hash);
182 
184 
185  std::string glob_to_regex(const std::string &val);
186 }
int display_simple_progress_spinner(int x)
Definition: util.cpp:642
const uint32_t T[512]
Definition: groestl_tables.h:34
bool is_local_address(const std::string &address)
Definition: util.cpp:539
std::string get_os_version_string()
Returns the OS version string.
Definition: util.cpp:381
void set_max_concurrency(unsigned n)
Definition: util.cpp:522
std::string glob_to_regex(const std::string &val)
Definition: util.cpp:659
#define v1(p)
Definition: aesb.c:116
std::error_code replace_file(const std::string &replacement_name, const std::string &replaced_name)
std::rename wrapper for nix and something strange for windows.
Definition: util.cpp:464
unsigned get_max_concurrency()
Definition: util.cpp:533
std::string get_default_data_dir()
Returns the default data directory.
Definition: util.cpp:408
Defines a signal handler for win32 and *nix.
Definition: util.h:113
static std::function< void(int)> m_handler
where the installed handler is stored
Definition: util.h:169
std::unique_ptr< std::FILE, tools::close_file > create_private_file(const std::string &name)
Definition: util.cpp:57
static void handle_signal(int type)
calles m_handler
Definition: util.h:161
Various Tools.
Definition: base58.cpp:43
type
Definition: json.h:74
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash)
Definition: util.cpp:598
static void posix_handler(int type)
handler for NIX
Definition: util.h:154
static bool install(T t)
installs a signal handler
Definition: util.h:118
void operator()(std::FILE *handle) const noexcept
Definition: util.h:55
void set_strict_default_file_permissions(bool strict)
Definition: util.cpp:506
#define v0(p)
Definition: aesb.c:115
bool sanitize_locale()
Definition: util.cpp:484
bool create_directories_if_necessary(const std::string &path)
creates directories for a path
Definition: util.cpp:441
POD_CLASS hash
Definition: hash.h:46
Functional class for closing C file handles.
Definition: util.h:53
int vercmp(const char *v0, const char *v1)
Definition: util.cpp:580