Bitcoin Core  23.99.0
P2P Digital Currency
fs.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_FS_H
6 #define BITCOIN_FS_H
7 
8 #include <tinyformat.h>
9 
10 #include <cstdio>
11 #include <filesystem>
12 #include <functional>
13 #include <iomanip>
14 #include <ios>
15 #include <ostream>
16 #include <string>
17 #include <utility>
18 
20 namespace fs {
21 
22 using namespace std::filesystem;
23 
30 class path : public std::filesystem::path
31 {
32 public:
33  using std::filesystem::path::path;
34 
35  // Allow path objects arguments for compatibility.
36  path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
37  path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
38  path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(std::move(path)); return *this; }
39 
40  // Allow literal string arguments, which are safe as long as the literals are ASCII.
41  path(const char* c) : std::filesystem::path(c) {}
42  path& operator=(const char* c) { std::filesystem::path::operator=(c); return *this; }
43  path& operator/=(const char* c) { std::filesystem::path::operator/=(c); return *this; }
44  path& append(const char* c) { std::filesystem::path::append(c); return *this; }
45 
46  // Disallow std::string arguments to avoid locale-dependent decoding on windows.
47  path(std::string) = delete;
48  path& operator=(std::string) = delete;
49  path& operator/=(std::string) = delete;
50  path& append(std::string) = delete;
51 
52  // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
53  std::string string() const = delete;
54 
55  std::string u8string() const
56  {
57  const auto& utf8_str{std::filesystem::path::u8string()};
58  // utf8_str might either be std::string (C++17) or std::u8string
59  // (C++20). Convert both to std::string. This method can be removed
60  // after switching to C++20.
61  return std::string{utf8_str.begin(), utf8_str.end()};
62  }
63 
64  // Required for path overloads in <fstream>.
65  // See https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96e0367ead5d8dcac3bec2865582e76e2fbab190
66  path& make_preferred() { std::filesystem::path::make_preferred(); return *this; }
67  path filename() const { return std::filesystem::path::filename(); }
68 };
69 
70 static inline path u8path(const std::string& utf8_str)
71 {
72  return std::filesystem::u8path(utf8_str);
73 }
74 
75 // Disallow implicit std::string conversion for absolute to avoid
76 // locale-dependent encoding on windows.
77 static inline path absolute(const path& p)
78 {
79  return std::filesystem::absolute(p);
80 }
81 
82 // Disallow implicit std::string conversion for exists to avoid
83 // locale-dependent encoding on windows.
84 static inline bool exists(const path& p)
85 {
86  return std::filesystem::exists(p);
87 }
88 
89 // Allow explicit quoted stream I/O.
90 static inline auto quoted(const std::string& s)
91 {
92  return std::quoted(s, '"', '&');
93 }
94 
95 // Allow safe path append operations.
96 static inline path operator/(path p1, path p2)
97 {
98  p1 /= std::move(p2);
99  return p1;
100 }
101 static inline path operator/(path p1, const char* p2)
102 {
103  p1 /= p2;
104  return p1;
105 }
106 static inline path operator+(path p1, const char* p2)
107 {
108  p1 += p2;
109  return p1;
110 }
111 static inline path operator+(path p1, path::value_type p2)
112 {
113  p1 += p2;
114  return p1;
115 }
116 
117 // Disallow unsafe path append operations.
118 template<typename T> static inline path operator/(path p1, T p2) = delete;
119 template<typename T> static inline path operator+(path p1, T p2) = delete;
120 
121 // Disallow implicit std::string conversion for copy_file
122 // to avoid locale-dependent encoding on Windows.
123 static inline bool copy_file(const path& from, const path& to, copy_options options)
124 {
125  return std::filesystem::copy_file(from, to, options);
126 }
127 
146 static inline std::string PathToString(const path& path)
147 {
148  // Implementation note: On Windows, the std::filesystem::path(string)
149  // constructor and std::filesystem::path::string() method are not safe to
150  // use here, because these methods encode the path using C++'s narrow
151  // multibyte encoding, which on Windows corresponds to the current "code
152  // page", which is unpredictable and typically not able to represent all
153  // valid paths. So fs::path::u8string() and
154  // fs::u8path() functions are used instead on Windows. On
155  // POSIX, u8string/u8path functions are not safe to use because paths are
156  // not always valid UTF-8, so plain string methods which do not transform
157  // the path there are used.
158 #ifdef WIN32
159  return path.u8string();
160 #else
161  static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
162  return path.std::filesystem::path::string();
163 #endif
164 }
165 
169 static inline path PathFromString(const std::string& string)
170 {
171 #ifdef WIN32
172  return u8path(string);
173 #else
174  return std::filesystem::path(string);
175 #endif
176 }
177 
184 static inline bool create_directories(const std::filesystem::path& p)
185 {
186  if (std::filesystem::is_symlink(p) && std::filesystem::is_directory(p)) {
187  return false;
188  }
190 }
191 
197 bool create_directories(const std::filesystem::path& p, std::error_code& ec) = delete;
198 
199 } // namespace fs
200 
202 namespace fsbridge {
203  using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
204  FILE *fopen(const fs::path& p, const char *mode);
205 
215  fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
216 
217  class FileLock
218  {
219  public:
220  FileLock() = delete;
221  FileLock(const FileLock&) = delete;
222  FileLock(FileLock&&) = delete;
223  explicit FileLock(const fs::path& file);
224  ~FileLock();
225  bool TryLock();
226  std::string GetReason() { return reason; }
227 
228  private:
229  std::string reason;
230 #ifndef WIN32
231  int fd = -1;
232 #else
233  void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
234 #endif
235  };
236 
237  std::string get_filesystem_error_message(const fs::filesystem_error& e);
238 };
239 
240 // Disallow path operator<< formatting in tinyformat to avoid locale-dependent
241 // encoding on windows.
242 namespace tinyformat {
243 template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
244 template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
245 } // namespace tinyformat
246 
247 #endif // BITCOIN_FS_H
tinyformat
Definition: fs.h:242
fs::path::filename
path filename() const
Definition: fs.h:67
fs::exists
static bool exists(const path &p)
Definition: fs.h:84
fs::create_directories
bool create_directories(const std::filesystem::path &p, std::error_code &ec)=delete
This variant is not used.
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
fsbridge::get_filesystem_error_message
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:138
fs::path::make_preferred
path & make_preferred()
Definition: fs.h:66
fs::quoted
static auto quoted(const std::string &s)
Definition: fs.h:90
fs::operator+
static path operator+(path p1, const char *p2)
Definition: fs.h:106
fsbridge::AbsPathJoin
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:35
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:146
fsbridge::FileLock::GetReason
std::string GetReason()
Definition: fs.h:226
fs::path::append
path & append(const char *c)
Definition: fs.h:44
fs::path::operator/=
path & operator/=(const char *c)
Definition: fs.h:43
tinyformat.h
fs::operator/
static path operator/(path p1, path p2)
Definition: fs.h:96
fs::path::operator/=
path & operator/=(std::filesystem::path path)
Definition: fs.h:38
fs::u8path
static path u8path(const std::string &utf8_str)
Definition: fs.h:70
fsbridge::FopenFn
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:203
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
fs::path::operator=
path & operator=(const char *c)
Definition: fs.h:42
fs::PathFromString
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:169
fsbridge::FileLock
Definition: fs.h:217
fs::path::path
path(std::filesystem::path path)
Definition: fs.h:36
fs::path::u8string
std::string u8string() const
Definition: fs.h:55
fs
Filesystem operations and types.
Definition: fs.h:20
std
Definition: setup_common.h:33
fs::create_directories
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:184
fs::absolute
static path absolute(const path &p)
Definition: fs.h:77
fs::copy_file
static bool copy_file(const path &from, const path &to, copy_options options)
Definition: fs.h:123
tinyformat::formatValue
void formatValue(std::ostream &, const char *, const char *, int, const fs::path &)=delete
fs::path::operator=
path & operator=(std::filesystem::path path)
Definition: fs.h:37
fsbridge::FileLock::reason
std::string reason
Definition: fs.h:229
fsbridge
Bridge operations to C stdio.
Definition: fs.cpp:23
fs::path::path
path(const char *c)
Definition: fs.h:41