LCOV - code coverage report
Current view: top level - src/util - fs.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 28 44 63.6 %
Date: 2023-09-26 12:08:55 Functions: 21 27 77.8 %

          Line data    Source code
       1             : // Copyright (c) 2017-2022 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             : #include <filesystem> // IWYU pragma: export
      12             : #include <functional>
      13             : #include <iomanip>
      14             : #include <ios>
      15             : #include <ostream>
      16             : #include <string>
      17             : #include <utility>
      18             : 
      19             : /** Filesystem operations and types */
      20             : namespace fs {
      21             : 
      22             : using namespace std::filesystem;
      23             : 
      24             : /**
      25             :  * Path class wrapper to block calls to the fs::path(std::string) implicit
      26             :  * constructor and the fs::path::string() method, which have unsafe and
      27             :  * unpredictable behavior on Windows (see implementation note in
      28             :  * \ref PathToString for details)
      29             :  */
      30             : class path : public std::filesystem::path
      31             : {
      32             : public:
      33             :     using std::filesystem::path::path;
      34             : 
      35             :     // Allow path objects arguments for compatibility.
      36       53286 :     path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
      37        4859 :     path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
      38        3423 :     path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(path); return *this; }
      39             : 
      40             :     // Allow literal string arguments, which are safe as long as the literals are ASCII.
      41          12 :     path(const char* c) : std::filesystem::path(c) {}
      42           0 :     path& operator=(const char* c) { std::filesystem::path::operator=(c); return *this; }
      43           8 :     path& operator/=(const char* c) { std::filesystem::path::operator/=(c); return *this; }
      44             :     path& append(const char* c) { std::filesystem::path::append(c); return *this; }
      45             : 
      46             :     // Disallow std::string arguments to avoid locale-dependent decoding on windows.
      47             :     path(std::string) = delete;
      48             :     path& operator=(std::string) = delete;
      49             :     path& operator/=(std::string) = delete;
      50             :     path& append(std::string) = delete;
      51             : 
      52             :     // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
      53             :     std::string string() const = delete;
      54             : 
      55           0 :     std::string u8string() const
      56             :     {
      57           0 :         const auto& utf8_str{std::filesystem::path::u8string()};
      58             :         // utf8_str might either be std::string (C++17) or std::u8string
      59             :         // (C++20). Convert both to std::string. This method can be removed
      60             :         // after switching to C++20.
      61           0 :         return std::string{utf8_str.begin(), utf8_str.end()};
      62           0 :     }
      63             : 
      64             :     // Required for path overloads in <fstream>.
      65             :     // See https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96e0367ead5d8dcac3bec2865582e76e2fbab190
      66             :     path& make_preferred() { std::filesystem::path::make_preferred(); return *this; }
      67           0 :     path filename() const { return std::filesystem::path::filename(); }
      68             : };
      69             : 
      70         405 : static inline path u8path(const std::string& utf8_str)
      71             : {
      72             : #if __cplusplus < 202002L
      73         405 :     return std::filesystem::u8path(utf8_str);
      74             : #else
      75             :     return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
      76             : #endif
      77           0 : }
      78             : 
      79             : // Disallow implicit std::string conversion for absolute to avoid
      80             : // locale-dependent encoding on windows.
      81         170 : static inline path absolute(const path& p)
      82             : {
      83         170 :     return std::filesystem::absolute(p);
      84           0 : }
      85             : 
      86             : // Disallow implicit std::string conversion for exists to avoid
      87             : // locale-dependent encoding on windows.
      88       10042 : static inline bool exists(const path& p)
      89             : {
      90       10042 :     return std::filesystem::exists(p);
      91             : }
      92             : 
      93             : // Allow explicit quoted stream I/O.
      94           0 : static inline auto quoted(const std::string& s)
      95             : {
      96           0 :     return std::quoted(s, '"', '&');
      97             : }
      98             : 
      99             : // Allow safe path append operations.
     100         406 : static inline path operator/(path p1, path p2)
     101             : {
     102         406 :     p1 /= std::move(p2);
     103         406 :     return p1;
     104           0 : }
     105           6 : static inline path operator/(path p1, const char* p2)
     106             : {
     107           6 :     p1 /= p2;
     108           6 :     return p1;
     109             : }
     110           2 : static inline path operator+(path p1, const char* p2)
     111             : {
     112           2 :     p1 += p2;
     113           2 :     return p1;
     114             : }
     115             : static inline path operator+(path p1, path::value_type p2)
     116             : {
     117             :     p1 += p2;
     118             :     return p1;
     119             : }
     120             : 
     121             : // Disallow unsafe path append operations.
     122             : template<typename T> static inline path operator/(path p1, T p2) = delete;
     123             : template<typename T> static inline path operator+(path p1, T p2) = delete;
     124             : 
     125             : // Disallow implicit std::string conversion for copy_file
     126             : // to avoid locale-dependent encoding on Windows.
     127           0 : static inline bool copy_file(const path& from, const path& to, copy_options options)
     128             : {
     129           0 :     return std::filesystem::copy_file(from, to, options);
     130             : }
     131             : 
     132             : /**
     133             :  * Convert path object to a byte string. On POSIX, paths natively are byte
     134             :  * strings, so this is trivial. On Windows, paths natively are Unicode, so an
     135             :  * encoding step is necessary. The inverse of \ref PathToString is \ref
     136             :  * PathFromString. The strings returned and parsed by these functions can be
     137             :  * used to call POSIX APIs, and for roundtrip conversion, logging, and
     138             :  * debugging.
     139             :  *
     140             :  * Because \ref PathToString and \ref PathFromString functions don't specify an
     141             :  * encoding, they are meant to be used internally, not externally. They are not
     142             :  * appropriate to use in applications requiring UTF-8, where
     143             :  * fs::path::u8string() and fs::u8path() methods should be used instead. Other
     144             :  * applications could require still different encodings. For example, JSON, XML,
     145             :  * or URI applications might prefer to use higher-level escapes (\uXXXX or
     146             :  * &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
     147             :  * may require encoding paths with their respective UTF-8 derivatives WTF-8,
     148             :  * PEP-383, and CESU-8 (see https://en.wikipedia.org/wiki/UTF-8#Derivatives).
     149             :  */
     150        4647 : static inline std::string PathToString(const path& path)
     151             : {
     152             :     // Implementation note: On Windows, the std::filesystem::path(string)
     153             :     // constructor and std::filesystem::path::string() method are not safe to
     154             :     // use here, because these methods encode the path using C++'s narrow
     155             :     // multibyte encoding, which on Windows corresponds to the current "code
     156             :     // page", which is unpredictable and typically not able to represent all
     157             :     // valid paths. So fs::path::u8string() and
     158             :     // fs::u8path() functions are used instead on Windows. On
     159             :     // POSIX, u8string/u8path functions are not safe to use because paths are
     160             :     // not always valid UTF-8, so plain string methods which do not transform
     161             :     // the path there are used.
     162             : #ifdef WIN32
     163             :     return path.u8string();
     164             : #else
     165             :     static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
     166        4647 :     return path.std::filesystem::path::string();
     167             : #endif
     168             : }
     169             : 
     170             : /**
     171             :  * Convert byte string to path object. Inverse of \ref PathToString.
     172             :  */
     173         338 : static inline path PathFromString(const std::string& string)
     174             : {
     175             : #ifdef WIN32
     176             :     return u8path(string);
     177             : #else
     178         338 :     return std::filesystem::path(string);
     179             : #endif
     180           0 : }
     181             : 
     182             : /**
     183             :  * Create directory (and if necessary its parents), unless the leaf directory
     184             :  * already exists or is a symlink to an existing directory.
     185             :  * This is a temporary workaround for an issue in libstdc++ that has been fixed
     186             :  * upstream [PR101510].
     187             :  */
     188         490 : static inline bool create_directories(const std::filesystem::path& p)
     189             : {
     190         490 :     if (std::filesystem::is_symlink(p) && std::filesystem::is_directory(p)) {
     191           0 :         return false;
     192             :     }
     193         490 :     return std::filesystem::create_directories(p);
     194         490 : }
     195             : 
     196             : /**
     197             :  * This variant is not used. Delete it to prevent it from accidentally working
     198             :  * around the workaround. If it is needed, add a workaround in the same pattern
     199             :  * as above.
     200             :  */
     201             : bool create_directories(const std::filesystem::path& p, std::error_code& ec) = delete;
     202             : 
     203             : } // namespace fs
     204             : 
     205             : /** Bridge operations to C stdio */
     206             : namespace fsbridge {
     207             :     using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
     208             :     FILE *fopen(const fs::path& p, const char *mode);
     209             : 
     210             :     /**
     211             :      * Helper function for joining two paths
     212             :      *
     213             :      * @param[in] base  Base path
     214             :      * @param[in] path  Path to combine with base
     215             :      * @returns path unchanged if it is an absolute path, otherwise returns base joined with path. Returns base unchanged if path is empty.
     216             :      * @pre  Base path must be absolute
     217             :      * @post Returned path will always be absolute
     218             :      */
     219             :     fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
     220             : 
     221             :     class FileLock
     222             :     {
     223             :     public:
     224             :         FileLock() = delete;
     225             :         FileLock(const FileLock&) = delete;
     226             :         FileLock(FileLock&&) = delete;
     227             :         explicit FileLock(const fs::path& file);
     228             :         ~FileLock();
     229             :         bool TryLock();
     230           0 :         std::string GetReason() { return reason; }
     231             : 
     232             :     private:
     233             :         std::string reason;
     234             : #ifndef WIN32
     235             :         int fd = -1;
     236             : #else
     237             :         void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
     238             : #endif
     239             :     };
     240             : 
     241             :     std::string get_filesystem_error_message(const fs::filesystem_error& e);
     242             : };
     243             : 
     244             : // Disallow path operator<< formatting in tinyformat to avoid locale-dependent
     245             : // encoding on windows.
     246             : namespace tinyformat {
     247             : template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
     248             : template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
     249             : } // namespace tinyformat
     250             : 
     251             : #endif // BITCOIN_UTIL_FS_H

Generated by: LCOV version 1.14