2019-12-31 18:35:41 +01:00
|
|
|
// Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-04-06 20:19:21 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_FS_H
|
|
|
|
#define BITCOIN_FS_H
|
|
|
|
|
2024-08-06 19:39:26 +02:00
|
|
|
#include <tinyformat.h>
|
2017-04-06 20:19:21 +02:00
|
|
|
|
2024-08-06 19:40:56 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <ios>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2017-04-06 20:19:21 +02:00
|
|
|
/** Filesystem operations and types */
|
2024-08-06 19:39:26 +02:00
|
|
|
namespace fs {
|
|
|
|
|
2024-08-06 19:40:56 +02:00
|
|
|
using namespace std::filesystem;
|
2024-08-06 19:39:26 +02:00
|
|
|
|
|
|
|
/**
|
2024-08-06 19:40:56 +02:00
|
|
|
* Path class wrapper to block calls to the fs::path(std::string) implicit
|
|
|
|
* constructor and the fs::path::string() method, which have unsafe and
|
|
|
|
* unpredictable behavior on Windows (see implementation note in
|
|
|
|
* \ref PathToString for details)
|
2024-08-06 19:39:26 +02:00
|
|
|
*/
|
2024-08-06 19:40:56 +02:00
|
|
|
class path : public std::filesystem::path
|
2024-08-06 19:39:26 +02:00
|
|
|
{
|
|
|
|
public:
|
2024-08-06 19:40:56 +02:00
|
|
|
using std::filesystem::path::path;
|
2024-08-06 19:39:26 +02:00
|
|
|
|
|
|
|
// Allow path objects arguments for compatibility.
|
2024-08-06 19:40:56 +02:00
|
|
|
path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
|
|
|
|
path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
|
|
|
|
path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(std::move(path)); return *this; }
|
2024-08-06 19:39:26 +02:00
|
|
|
|
|
|
|
// Allow literal string arguments, which are safe as long as the literals are ASCII.
|
2024-08-06 19:40:56 +02:00
|
|
|
path(const char* c) : std::filesystem::path(c) {}
|
|
|
|
path& operator=(const char* c) { std::filesystem::path::operator=(c); return *this; }
|
|
|
|
path& operator/=(const char* c) { std::filesystem::path::operator/=(c); return *this; }
|
|
|
|
path& append(const char* c) { std::filesystem::path::append(c); return *this; }
|
2024-08-06 19:39:26 +02:00
|
|
|
|
|
|
|
// Disallow std::string arguments to avoid locale-dependent decoding on windows.
|
|
|
|
path(std::string) = delete;
|
|
|
|
path& operator=(std::string) = delete;
|
|
|
|
path& operator/=(std::string) = delete;
|
|
|
|
path& append(std::string) = delete;
|
|
|
|
|
|
|
|
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
|
|
|
|
std::string string() const = delete;
|
|
|
|
|
2022-03-17 21:24:26 +01:00
|
|
|
std::string u8string() const
|
|
|
|
{
|
|
|
|
const auto& utf8_str{std::filesystem::path::u8string()};
|
|
|
|
// utf8_str might either be std::string (C++17) or std::u8string
|
|
|
|
// (C++20). Convert both to std::string. This method can be removed
|
|
|
|
// after switching to C++20.
|
|
|
|
return std::string{utf8_str.begin(), utf8_str.end()};
|
|
|
|
}
|
|
|
|
|
2024-08-06 19:40:56 +02:00
|
|
|
// Required for path overloads in <fstream>.
|
|
|
|
// See https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96e0367ead5d8dcac3bec2865582e76e2fbab190
|
|
|
|
path& make_preferred() { std::filesystem::path::make_preferred(); return *this; }
|
|
|
|
path filename() const { return std::filesystem::path::filename(); }
|
2024-08-06 19:39:26 +02:00
|
|
|
};
|
|
|
|
|
2022-03-17 21:24:26 +01:00
|
|
|
static inline path u8path(const std::string& utf8_str)
|
|
|
|
{
|
|
|
|
return std::filesystem::u8path(utf8_str);
|
|
|
|
}
|
|
|
|
|
2024-08-06 19:40:56 +02:00
|
|
|
// Disallow implicit std::string conversion for absolute to avoid
|
2024-08-06 19:39:26 +02:00
|
|
|
// locale-dependent encoding on windows.
|
2024-08-06 19:40:56 +02:00
|
|
|
static inline path absolute(const path& p)
|
2024-08-06 19:39:26 +02:00
|
|
|
{
|
2024-08-06 19:40:56 +02:00
|
|
|
return std::filesystem::absolute(p);
|
2024-08-06 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow implicit std::string conversion for exists to avoid
|
|
|
|
// locale-dependent encoding on windows.
|
|
|
|
static inline bool exists(const path& p)
|
|
|
|
{
|
2024-08-06 19:40:56 +02:00
|
|
|
return std::filesystem::exists(p);
|
2024-08-06 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow explicit quoted stream I/O.
|
|
|
|
static inline auto quoted(const std::string& s)
|
|
|
|
{
|
2024-08-06 19:40:56 +02:00
|
|
|
return std::quoted(s, '"', '&');
|
2024-08-06 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow safe path append operations.
|
|
|
|
static inline path operator+(path p1, path p2)
|
|
|
|
{
|
2024-08-06 19:40:56 +02:00
|
|
|
p1 += std::move(p2);
|
2024-08-06 19:39:26 +02:00
|
|
|
return p1;
|
|
|
|
}
|
|
|
|
|
2024-08-06 19:40:25 +02:00
|
|
|
// Disallow implicit std::string conversion for copy_file
|
|
|
|
// to avoid locale-dependent encoding on Windows.
|
2024-08-06 19:40:56 +02:00
|
|
|
static inline bool copy_file(const path& from, const path& to, copy_options options)
|
2024-08-06 19:40:25 +02:00
|
|
|
{
|
2024-08-06 19:40:56 +02:00
|
|
|
return std::filesystem::copy_file(from, to, options);
|
2024-08-06 19:40:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 19:39:26 +02:00
|
|
|
/**
|
2024-08-06 19:40:56 +02:00
|
|
|
* Convert path object to a byte string. On POSIX, paths natively are byte
|
2024-07-20 12:43:13 +02:00
|
|
|
* strings, so this is trivial. On Windows, paths natively are Unicode, so an
|
|
|
|
* encoding step is necessary. The inverse of \ref PathToString is \ref
|
|
|
|
* PathFromString. The strings returned and parsed by these functions can be
|
|
|
|
* used to call POSIX APIs, and for roundtrip conversion, logging, and
|
|
|
|
* debugging.
|
2024-08-06 19:39:26 +02:00
|
|
|
*
|
2024-07-20 12:43:13 +02:00
|
|
|
* Because \ref PathToString and \ref PathFromString functions don't specify an
|
|
|
|
* encoding, they are meant to be used internally, not externally. They are not
|
|
|
|
* appropriate to use in applications requiring UTF-8, where
|
|
|
|
* fs::path::u8string() and fs::u8path() methods should be used instead. Other
|
|
|
|
* applications could require still different encodings. For example, JSON, XML,
|
2024-08-06 19:40:56 +02:00
|
|
|
* or URI applications might prefer to use higher-level escapes (\uXXXX or
|
2024-07-20 12:43:13 +02:00
|
|
|
* &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
|
|
|
|
* may require encoding paths with their respective UTF-8 derivatives WTF-8,
|
|
|
|
* PEP-383, and CESU-8 (see https://en.wikipedia.org/wiki/UTF-8#Derivatives).
|
2024-08-06 19:39:26 +02:00
|
|
|
*/
|
|
|
|
static inline std::string PathToString(const path& path)
|
|
|
|
{
|
2024-07-20 12:43:13 +02:00
|
|
|
// Implementation note: On Windows, the std::filesystem::path(string)
|
|
|
|
// constructor and std::filesystem::path::string() method are not safe to
|
|
|
|
// use here, because these methods encode the path using C++'s narrow
|
|
|
|
// multibyte encoding, which on Windows corresponds to the current "code
|
|
|
|
// page", which is unpredictable and typically not able to represent all
|
2022-03-17 21:24:26 +01:00
|
|
|
// valid paths. So fs::path::u8string() and
|
|
|
|
// fs::u8path() functions are used instead on Windows. On
|
2024-07-20 12:43:13 +02:00
|
|
|
// POSIX, u8string/u8path functions are not safe to use because paths are
|
|
|
|
// not always valid UTF-8, so plain string methods which do not transform
|
|
|
|
// the path there are used.
|
2024-08-06 19:39:26 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
return path.u8string();
|
|
|
|
#else
|
|
|
|
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
|
2024-08-06 19:40:56 +02:00
|
|
|
return path.std::filesystem::path::string();
|
2024-08-06 19:39:26 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert byte string to path object. Inverse of \ref PathToString.
|
|
|
|
*/
|
|
|
|
static inline path PathFromString(const std::string& string)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return u8path(string);
|
|
|
|
#else
|
2024-08-06 19:40:56 +02:00
|
|
|
return std::filesystem::path(string);
|
2024-08-06 19:39:26 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} // namespace fs
|
2017-04-06 20:19:21 +02:00
|
|
|
|
|
|
|
/** Bridge operations to C stdio */
|
|
|
|
namespace fsbridge {
|
|
|
|
FILE *fopen(const fs::path& p, const char *mode);
|
2018-07-25 11:33:22 +02:00
|
|
|
|
2021-01-21 18:47:57 +01:00
|
|
|
/**
|
|
|
|
* Helper function for joining two paths
|
|
|
|
*
|
|
|
|
* @param[in] base Base path
|
|
|
|
* @param[in] path Path to combine with base
|
|
|
|
* @returns path unchanged if it is an absolute path, otherwise returns base joined with path. Returns base unchanged if path is empty.
|
|
|
|
* @pre Base path must be absolute
|
|
|
|
* @post Returned path will always be absolute
|
|
|
|
*/
|
|
|
|
fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
|
|
|
|
|
2018-07-25 11:33:22 +02:00
|
|
|
class FileLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileLock() = delete;
|
|
|
|
FileLock(const FileLock&) = delete;
|
|
|
|
FileLock(FileLock&&) = delete;
|
|
|
|
explicit FileLock(const fs::path& file);
|
|
|
|
~FileLock();
|
|
|
|
bool TryLock();
|
|
|
|
std::string GetReason() { return reason; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string reason;
|
|
|
|
#ifndef WIN32
|
|
|
|
int fd = -1;
|
|
|
|
#else
|
|
|
|
void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
|
|
|
|
#endif
|
|
|
|
};
|
2018-09-10 20:08:56 +02:00
|
|
|
|
|
|
|
std::string get_filesystem_error_message(const fs::filesystem_error& e);
|
2017-04-06 20:19:21 +02:00
|
|
|
};
|
|
|
|
|
2024-08-06 19:39:26 +02:00
|
|
|
// Disallow path operator<< formatting in tinyformat to avoid locale-dependent
|
|
|
|
// encoding on windows.
|
|
|
|
namespace tinyformat {
|
2024-08-06 19:40:56 +02:00
|
|
|
template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
|
2024-08-06 19:39:26 +02:00
|
|
|
template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
|
|
|
|
} // namespace tinyformat
|
|
|
|
|
2017-05-19 15:49:19 +02:00
|
|
|
#endif // BITCOIN_FS_H
|