Bitcoin Core 30.99.0
P2P Digital Currency
fs.h
Go to the documentation of this file.
1// Copyright (c) 2017-present 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_UTIL_FS_H
6#define BITCOIN_UTIL_FS_H
7
8#include <tinyformat.h>
9
10#include <cstdio>
11#include <filesystem> // IWYU pragma: export
12#include <functional>
13#include <iomanip>
14#include <ios>
15#include <ostream>
16#include <string>
17#include <string_view>
18#include <system_error>
19#include <type_traits>
20#include <utility>
21
23namespace fs {
24
25using namespace std::filesystem;
26
33class path : public std::filesystem::path
34{
35public:
36 using std::filesystem::path::path;
37
38 // Convenience method for accessing standard path type without needing a cast.
39 std::filesystem::path& std_path() { return *this; }
40 const std::filesystem::path& std_path() const { return *this; }
41
42 // Allow path objects arguments for compatibility.
43 path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
44 path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
45 path& operator/=(const std::filesystem::path& path) { std::filesystem::path::operator/=(path); return *this; }
46
47 // Allow literal string arguments, which are safe as long as the literals are ASCII.
48 path(const char* c) : std::filesystem::path(c) {}
49 path& operator=(const char* c) { std::filesystem::path::operator=(c); return *this; }
50 path& operator/=(const char* c) { std::filesystem::path::operator/=(c); return *this; }
51 path& append(const char* c) { std::filesystem::path::append(c); return *this; }
52
53 // Disallow std::string arguments to avoid locale-dependent decoding on windows.
54 path(std::string) = delete;
55 path& operator=(std::string) = delete;
56 path& operator/=(std::string) = delete;
57 path& append(std::string) = delete;
58
59 // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
60 std::string string() const = delete;
61
62 // Disallow implicit string conversion to ensure code is portable.
63 // `string_type` may be `string` or `wstring` depending on the platform, so
64 // using this conversion could result in code that compiles on unix but
65 // fails to compile on windows, or vice versa.
66 operator string_type() const = delete;
67
74 std::string utf8string() const
75 {
76 const std::u8string& utf8_str{std::filesystem::path::u8string()};
77 return std::string{utf8_str.begin(), utf8_str.end()};
78 }
79};
80
81static inline path u8path(std::string_view utf8_str)
82{
83 return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
84}
85
86// Disallow implicit std::string conversion for absolute to avoid
87// locale-dependent encoding on windows.
88static inline path absolute(const path& p)
89{
91}
92
93// Disallow implicit std::string conversion for exists to avoid
94// locale-dependent encoding on windows.
95static inline bool exists(const path& p)
96{
98}
99
100// Allow explicit quoted stream I/O.
101static inline auto quoted(const std::string& s)
102{
103 return std::quoted(s, '"', '&');
104}
105
106// Allow safe path append operations.
107static inline path operator/(path p1, const path& p2)
108{
109 p1 /= p2;
110 return p1;
111}
112static inline path operator/(path p1, const char* p2)
113{
114 p1 /= p2;
115 return p1;
116}
117static inline path operator+(path p1, const char* p2)
118{
119 p1 += p2;
120 return p1;
121}
122static inline path operator+(path p1, path::value_type p2)
123{
124 p1 += p2;
125 return p1;
126}
127
128// Disallow unsafe path append operations.
129template<typename T> static inline path operator/(path p1, T p2) = delete;
130template<typename T> static inline path operator+(path p1, T p2) = delete;
131
132// Disallow implicit std::string conversion for copy_file
133// to avoid locale-dependent encoding on Windows.
134static inline bool copy_file(const path& from, const path& to, copy_options options)
135{
136 return std::filesystem::copy_file(from, to, options);
137}
138
157static inline std::string PathToString(const path& path)
158{
159 // Implementation note: On Windows, the std::filesystem::path(string)
160 // constructor and std::filesystem::path::string() method are not safe to
161 // use here, because these methods encode the path using C++'s narrow
162 // multibyte encoding, which on Windows corresponds to the current "code
163 // page", which is unpredictable and typically not able to represent all
164 // valid paths. So fs::path::utf8string() and
165 // fs::u8path() functions are used instead on Windows. On
166 // POSIX, u8string/utf8string/u8path functions are not safe to use because paths are
167 // not always valid UTF-8, so plain string methods which do not transform
168 // the path there are used.
169#ifdef WIN32
170 return path.utf8string();
171#else
172 static_assert(std::is_same_v<path::string_type, std::string>, "PathToString not implemented on this platform");
173 return path.std::filesystem::path::string();
174#endif
175}
176
180static inline path PathFromString(const std::string& string)
181{
182#ifdef WIN32
183 return u8path(string);
184#else
185 return std::filesystem::path(string);
186#endif
187}
188
196static inline bool create_directories(const std::filesystem::path& p)
197{
198 if (std::filesystem::is_symlink(p) && std::filesystem::is_directory(p)) {
199 return false;
200 }
202}
203
209bool create_directories(const std::filesystem::path& p, std::error_code& ec) = delete;
210
211} // namespace fs
212
214namespace fsbridge {
215 using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
216 FILE *fopen(const fs::path& p, const char *mode);
217
227 fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
228
230 {
231 public:
232 FileLock() = delete;
233 FileLock(const FileLock&) = delete;
234 FileLock(FileLock&&) = delete;
235 explicit FileLock(const fs::path& file);
236 ~FileLock();
237 bool TryLock();
238 std::string GetReason() { return reason; }
239
240 private:
241 std::string reason;
242#ifndef WIN32
243 int fd = -1;
244#else
245 void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
246#endif
247 };
248};
249
250// Disallow path operator<< formatting in tinyformat to avoid locale-dependent
251// encoding on windows.
252namespace tinyformat {
253template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
254template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
255} // namespace tinyformat
256
257#endif // BITCOIN_UTIL_FS_H
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:34
const std::filesystem::path & std_path() const
Definition: fs.h:40
path(std::filesystem::path path)
Definition: fs.h:43
path & operator=(std::string)=delete
std::filesystem::path & std_path()
Definition: fs.h:39
std::string string() const =delete
path & operator/=(const std::filesystem::path &path)
Definition: fs.h:45
path & operator/=(std::string)=delete
std::string utf8string() const
Return a UTF-8 representation of the path as a std::string, for compatibility with code using std::st...
Definition: fs.h:74
path & append(std::string)=delete
path & operator=(std::filesystem::path path)
Definition: fs.h:44
path & append(const char *c)
Definition: fs.h:51
path & operator=(const char *c)
Definition: fs.h:49
path(std::string)=delete
path & operator/=(const char *c)
Definition: fs.h:50
path(const char *c)
Definition: fs.h:48
FileLock(FileLock &&)=delete
FileLock(const FileLock &)=delete
std::string reason
Definition: fs.h:241
std::string GetReason()
Definition: fs.h:238
bool TryLock()
Definition: fs.cpp:62
static path absolute(const path &p)
Definition: fs.h:88
static path u8path(std::string_view utf8_str)
Definition: fs.h:81
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:196
static auto quoted(const std::string &s)
Definition: fs.h:101
static bool exists(const path &p)
Definition: fs.h:95
static bool copy_file(const path &from, const path &to, copy_options options)
Definition: fs.h:134
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:157
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:180
static path operator/(path p1, const path &p2)
Definition: fs.h:107
Bridge operations to C stdio.
Definition: fs.cpp:23
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:34
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:215
void formatValue(std::ostream &out, const char *, const char *fmtEnd, int ntrunc, const T &value)
Format a value into a stream, delegating to operator<< by default.
Definition: tinyformat.h:349
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
Definition: translation.h:47