Branch data Line data Source code
# 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 : : # 17 : : /** Filesystem operations and types */ # 18 : : namespace fs = boost::filesystem; # 19 : : # 20 : : /** Bridge operations to C stdio */ # 21 : : namespace fsbridge { # 22 : : FILE *fopen(const fs::path& p, const char *mode); # 23 : : # 24 : : /** # 25 : : * Helper function for joining two paths # 26 : : * # 27 : : * @param[in] base Base path # 28 : : * @param[in] path Path to combine with base # 29 : : * @returns path unchanged if it is an absolute path, otherwise returns base joined with path. Returns base unchanged if path is empty. # 30 : : * @pre Base path must be absolute # 31 : : * @post Returned path will always be absolute # 32 : : */ # 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 : 10 : 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__) # 101 : : typedef fs::ifstream ifstream; # 102 : : typedef fs::ofstream ofstream; # 103 : : #endif // WIN32 && __GLIBCXX__ # 104 : : }; # 105 : : # 106 : : #endif // BITCOIN_FS_H