mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-11 17:52:48 +01:00
refactor: replace boost::filesystem with std::filesystem
Warning: Replacing fs::system_complete calls with fs::absolute calls in this commit may cause minor changes in behaviour because fs::absolute no longer strips trailing slashes; however these changes are believed to be safe. Co-authored-by: Russell Yanofsky <russ@yanofsky.org> Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
This commit is contained in:
116
src/fs.cpp
116
src/fs.cpp
@@ -37,7 +37,7 @@ FILE *fopen(const fs::path& p, const char *mode)
|
||||
fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
|
||||
{
|
||||
assert(base.is_absolute());
|
||||
return fs::absolute(path, base);
|
||||
return path.empty() ? base : fs::path(base / path);
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
@@ -153,118 +153,4 @@ std::string get_filesystem_error_message(const fs::filesystem_error& e)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef __GLIBCXX__
|
||||
|
||||
// reference: https://github.com/gcc-mirror/gcc/blob/gcc-7_3_0-release/libstdc%2B%2B-v3/include/std/fstream#L270
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wswitch"
|
||||
#endif
|
||||
static std::string openmodeToStr(std::ios_base::openmode mode)
|
||||
{
|
||||
switch (mode & ~std::ios_base::ate) {
|
||||
case std::ios_base::out:
|
||||
case std::ios_base::out | std::ios_base::trunc:
|
||||
return "w";
|
||||
case std::ios_base::out | std::ios_base::app:
|
||||
case std::ios_base::app:
|
||||
return "a";
|
||||
case std::ios_base::in:
|
||||
return "r";
|
||||
case std::ios_base::in | std::ios_base::out:
|
||||
return "r+";
|
||||
case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
|
||||
return "w+";
|
||||
case std::ios_base::in | std::ios_base::out | std::ios_base::app:
|
||||
case std::ios_base::in | std::ios_base::app:
|
||||
return "a+";
|
||||
case std::ios_base::out | std::ios_base::binary:
|
||||
case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
|
||||
return "wb";
|
||||
case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
|
||||
case std::ios_base::app | std::ios_base::binary:
|
||||
return "ab";
|
||||
case std::ios_base::in | std::ios_base::binary:
|
||||
return "rb";
|
||||
case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
|
||||
return "r+b";
|
||||
case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
|
||||
return "w+b";
|
||||
case std::ios_base::in | std::ios_base::out | std::ios_base::app | std::ios_base::binary:
|
||||
case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
|
||||
return "a+b";
|
||||
default:
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
void ifstream::open(const fs::path& p, std::ios_base::openmode mode)
|
||||
{
|
||||
close();
|
||||
mode |= std::ios_base::in;
|
||||
m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
|
||||
if (m_file == nullptr) {
|
||||
return;
|
||||
}
|
||||
m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
|
||||
rdbuf(&m_filebuf);
|
||||
if (mode & std::ios_base::ate) {
|
||||
seekg(0, std::ios_base::end);
|
||||
}
|
||||
}
|
||||
|
||||
void ifstream::close()
|
||||
{
|
||||
if (m_file != nullptr) {
|
||||
m_filebuf.close();
|
||||
fclose(m_file);
|
||||
}
|
||||
m_file = nullptr;
|
||||
}
|
||||
|
||||
void ofstream::open(const fs::path& p, std::ios_base::openmode mode)
|
||||
{
|
||||
close();
|
||||
mode |= std::ios_base::out;
|
||||
m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
|
||||
if (m_file == nullptr) {
|
||||
return;
|
||||
}
|
||||
m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
|
||||
rdbuf(&m_filebuf);
|
||||
if (mode & std::ios_base::ate) {
|
||||
seekp(0, std::ios_base::end);
|
||||
}
|
||||
}
|
||||
|
||||
void ofstream::close()
|
||||
{
|
||||
if (m_file != nullptr) {
|
||||
m_filebuf.close();
|
||||
fclose(m_file);
|
||||
}
|
||||
m_file = nullptr;
|
||||
}
|
||||
#else // __GLIBCXX__
|
||||
|
||||
#if BOOST_VERSION >= 107700
|
||||
static_assert(sizeof(*BOOST_FILESYSTEM_C_STR(boost::filesystem::path())) == sizeof(wchar_t),
|
||||
#else
|
||||
static_assert(sizeof(*boost::filesystem::path().BOOST_FILESYSTEM_C_STR) == sizeof(wchar_t),
|
||||
#endif // BOOST_VERSION >= 107700
|
||||
"Warning: This build is using boost::filesystem ofstream and ifstream "
|
||||
"implementations which will fail to open paths containing multibyte "
|
||||
"characters. You should delete this static_assert to ignore this warning, "
|
||||
"or switch to a different C++ standard library like the Microsoft C++ "
|
||||
"Standard Library (where boost uses non-standard extensions to construct "
|
||||
"stream objects with wide filenames), or the GNU libstdc++ library (where "
|
||||
"a more complicated workaround has been implemented above).");
|
||||
|
||||
#endif // __GLIBCXX__
|
||||
#endif // WIN32
|
||||
|
||||
} // fsbridge
|
||||
|
||||
Reference in New Issue
Block a user