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