Bitcoin ABC  0.22.12
P2P Digital Currency
fs.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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 <cstdio>
9 #include <string>
10 #if defined WIN32 && defined __GLIBCXX__
11 #include <ext/stdio_filebuf.h>
12 #endif
13 
14 #include <boost/filesystem.hpp>
15 #include <boost/filesystem/fstream.hpp>
16 
18 namespace fs = boost::filesystem;
19 
21 namespace fsbridge {
22 FILE *fopen(const fs::path &p, const char *mode);
23 FILE *freopen(const fs::path &p, const char *mode, FILE *stream);
24 
25 class FileLock {
26 public:
27  FileLock() = delete;
28  FileLock(const FileLock &) = delete;
29  FileLock(FileLock &&) = delete;
30  explicit FileLock(const fs::path &file);
31  ~FileLock();
32  bool TryLock();
33  std::string GetReason() { return reason; }
34 
35 private:
36  std::string reason;
37 #ifndef WIN32
38  int fd = -1;
39 #else
40  // INVALID_HANDLE_VALUE
41  void *hFile = (void *)-1;
42 #endif
43 };
44 
45 std::string get_filesystem_error_message(const fs::filesystem_error &e);
46 
47 // GNU libstdc++ specific workaround for opening UTF-8 paths on Windows.
48 //
49 // On Windows, it is only possible to reliably access multibyte file paths
50 // through `wchar_t` APIs, not `char` APIs. But because the C++ standard doesn't
51 // require ifstream/ofstream `wchar_t` constructors, and the GNU library doesn't
52 // provide them (in contrast to the Microsoft C++ library, see
53 // https://stackoverflow.com/questions/821873/how-to-open-an-stdfstream-ofstream-or-ifstream-with-a-unicode-filename/822032#822032),
54 // Boost is forced to fall back to `char` constructors which may not work
55 // properly.
56 //
57 // Work around this issue by creating stream objects with `_wfopen` in
58 // combination with `__gnu_cxx::stdio_filebuf`. This workaround can be removed
59 // with an upgrade to C++17, where streams can be constructed directly from
60 // `std::filesystem::path` objects.
61 
62 #if defined WIN32 && defined __GLIBCXX__
63 class ifstream : public std::istream {
64 public:
65  ifstream() = default;
66  explicit ifstream(const fs::path &p,
67  std::ios_base::openmode mode = std::ios_base::in) {
68  open(p, mode);
69  }
70  ~ifstream() { close(); }
71  void open(const fs::path &p,
72  std::ios_base::openmode mode = std::ios_base::in);
73  bool is_open() { return m_filebuf.is_open(); }
74  void close();
75 
76 private:
77  __gnu_cxx::stdio_filebuf<char> m_filebuf;
78  FILE *m_file = nullptr;
79 };
80 class ofstream : public std::ostream {
81 public:
82  ofstream() = default;
83  explicit ofstream(const fs::path &p,
84  std::ios_base::openmode mode = std::ios_base::out) {
85  open(p, mode);
86  }
87  ~ofstream() { close(); }
88  void open(const fs::path &p,
89  std::ios_base::openmode mode = std::ios_base::out);
90  bool is_open() { return m_filebuf.is_open(); }
91  void close();
92 
93 private:
94  __gnu_cxx::stdio_filebuf<char> m_filebuf;
95  FILE *m_file = nullptr;
96 };
97 #else // !(WIN32 && __GLIBCXX__)
100 #endif // WIN32 && __GLIBCXX__
101 }; // namespace fsbridge
102 
103 #endif // BITCOIN_FS_H
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:22
fs::ifstream ifstream
Definition: fs.h:98
FILE * freopen(const fs::path &p, const char *mode, FILE *stream)
fs::ofstream ofstream
Definition: fs.h:99
std::string reason
Definition: fs.h:36
Filesystem operations and types.
Definition: fs.cpp:20
std::string GetReason()
Definition: fs.h:33
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:129
bool TryLock()
Definition: fs.cpp:57