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