Bitcoin Core 32.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// The `util/fs.h` header is designed to be a drop-in replacement for `filesystem`.
12#include <filesystem> // IWYU pragma: export
13#include <functional>
14#include <iomanip>
15#include <ios>
16#include <string>
17#include <string_view>
18#include <type_traits>
19#include <utility>
20
22namespace fs {
23
24using namespace std::filesystem;
25
32class path : public std::filesystem::path
33{
34public:
35 using std::filesystem::path::path;
36
37 // Convenience method for accessing standard path type without needing a cast.
38 std::filesystem::path& std_path() { return *this; }
39 const std::filesystem::path& std_path() const { return *this; }
40
41 // Allow path objects arguments for compatibility.
42 path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
43 path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
44 path& operator/=(const std::filesystem::path& path) { std::filesystem::path::operator/=(path); return *this; }
45
46 // Allow literal string arguments, which are safe as long as the literals are ASCII.
47 path(const char* c) : std::filesystem::path(c) {}
48 path& operator=(const char* c) { std::filesystem::path::operator=(c); return *this; }
49 path& operator/=(const char* c) { std::filesystem::path::operator/=(c); return *this; }
50 path& append(const char* c) { std::filesystem::path::append(c); return *this; }
51
52 // Disallow std::string arguments to avoid locale-dependent decoding on windows.
53 path(std::string) = delete;
54 path& operator=(std::string) = delete;
55 path& operator/=(std::string) = delete;
56 path& append(std::string) = delete;
57
58 // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
59 std::string string() const = delete;
60
61 // Disallow implicit string conversion to ensure code is portable.
62 // `string_type` may be `string` or `wstring` depending on the platform, so
63 // using this conversion could result in code that compiles on unix but
64 // fails to compile on windows, or vice versa.
65 operator string_type() const = delete;
66
73 std::string utf8string() const
74 {
75 const std::u8string& utf8_str{std::filesystem::path::u8string()};
76 return std::string{utf8_str.begin(), utf8_str.end()};
77 }
78};
79
80static inline path u8path(std::string_view utf8_str)
81{
82 return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
83}
84
85// Disallow implicit std::string conversion for absolute to avoid
86// locale-dependent encoding on windows.
87static inline path absolute(const path& p)
88{
90}
91
92// Disallow implicit std::string conversion for exists to avoid
93// locale-dependent encoding on windows.
94static inline bool exists(const path& p)
95{
97}
98static inline bool exists(const std::filesystem::file_status& s)
99{
101}
102
103// Allow explicit quoted stream I/O.
104static inline auto quoted(const std::string& s)
105{
106 return std::quoted(s, '"', '&');
107}
108
109// Allow safe path append operations.
110static inline path operator/(path p1, const path& p2)
111{
112 p1 /= p2;
113 return p1;
114}
115static inline path operator/(path p1, const char* p2)
116{
117 p1 /= p2;
118 return p1;
119}
120static inline path operator+(path p1, const char* p2)
121{
122 p1 += p2;
123 return p1;
124}
125static inline path operator+(path p1, path::value_type p2)
126{
127 p1 += p2;
128 return p1;
129}
130
131// Disallow unsafe path append operations.
132template<typename T> static inline path operator/(path p1, T p2) = delete;
133template<typename T> static inline path operator+(path p1, T p2) = delete;
134
135// Disallow implicit std::string conversion for copy_file
136// to avoid locale-dependent encoding on Windows.
137static inline bool copy_file(const path& from, const path& to, copy_options options)
138{
139 return std::filesystem::copy_file(from, to, options);
140}
141
160static inline std::string PathToString(const path& path)
161{
162 // Implementation note: On Windows, the std::filesystem::path(string)
163 // constructor and std::filesystem::path::string() method are not safe to
164 // use here, because these methods encode the path using C++'s narrow
165 // multibyte encoding, which on Windows corresponds to the current "code
166 // page", which is unpredictable and typically not able to represent all
167 // valid paths. So fs::path::utf8string() and
168 // fs::u8path() functions are used instead on Windows. On
169 // POSIX, u8string/utf8string/u8path functions are not safe to use because paths are
170 // not always valid UTF-8, so plain string methods which do not transform
171 // the path there are used.
172#ifdef WIN32
173 return path.utf8string();
174#else
175 static_assert(std::is_same_v<path::string_type, std::string>, "PathToString not implemented on this platform");
176 return path.std::filesystem::path::string();
177#endif
178}
179
183static inline path PathFromString(const std::string& string)
184{
185#ifdef WIN32
186 return u8path(string);
187#else
188 return std::filesystem::path(string);
189#endif
190}
191} // namespace fs
192
194namespace fsbridge {
195 using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
196 FILE *fopen(const fs::path& p, const char *mode);
197
207 fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
208
210 {
211 public:
212 FileLock() = delete;
213 FileLock(const FileLock&) = delete;
214 FileLock(FileLock&&) = delete;
215 explicit FileLock(const fs::path& file);
216 ~FileLock();
217 bool TryLock();
218 std::string GetReason() { return reason; }
219
220 private:
221 std::string reason;
222#ifndef WIN32
223 int fd = -1;
224#else
225 void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
226#endif
227 };
228};
229
230// Disallow path operator<< formatting in tinyformat to avoid locale-dependent
231// encoding on windows.
232namespace tinyformat {
233template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
234template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
235} // namespace tinyformat
236
237#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:33
const std::filesystem::path & std_path() const
Definition: fs.h:39
path(std::filesystem::path path)
Definition: fs.h:42
path & operator=(std::string)=delete
std::filesystem::path & std_path()
Definition: fs.h:38
std::string string() const =delete
path & operator/=(const std::filesystem::path &path)
Definition: fs.h:44
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:73
path & append(std::string)=delete
path & operator=(std::filesystem::path path)
Definition: fs.h:43
path & append(const char *c)
Definition: fs.h:50
path & operator=(const char *c)
Definition: fs.h:48
path(std::string)=delete
path & operator/=(const char *c)
Definition: fs.h:49
path(const char *c)
Definition: fs.h:47
FileLock(FileLock &&)=delete
FileLock(const FileLock &)=delete
std::string reason
Definition: fs.h:221
std::string GetReason()
Definition: fs.h:218
bool TryLock()
Definition: fs.cpp:60
static path absolute(const path &p)
Definition: fs.h:87
static path u8path(std::string_view utf8_str)
Definition: fs.h:80
static auto quoted(const std::string &s)
Definition: fs.h:104
static bool exists(const path &p)
Definition: fs.h:94
static bool copy_file(const path &from, const path &to, copy_options options)
Definition: fs.h:137
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:160
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:183
static path operator/(path p1, const path &p2)
Definition: fs.h:110
Bridge operations to C stdio.
Definition: fs.cpp:21
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:23
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:32
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:195
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