Skip to content

Commit

Permalink
refactor: replace boost::filesystem with std::filesystem
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Hennadii Stepanov <[email protected]>
  • Loading branch information
3 people authored and fanquake committed Oct 21, 2021
1 parent 4e49275 commit b70c843
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 269 deletions.
118 changes: 3 additions & 115 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <cassert>

#include <fs.h>

#ifndef WIN32
Expand Down Expand Up @@ -34,7 +36,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
Expand Down Expand Up @@ -150,118 +152,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
121 changes: 31 additions & 90 deletions src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,41 @@
#ifndef BITCOIN_FS_H
#define BITCOIN_FS_H

#include <stdio.h>
#include <string>
#if defined WIN32 && defined __GLIBCXX__
#include <ext/stdio_filebuf.h>
#endif

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <tinyformat.h>

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <string>
#include <utility>

/** Filesystem operations and types */
namespace fs {

using namespace boost::filesystem;
using namespace std::filesystem;

/**
* Path class wrapper to prepare application code for transition from
* boost::filesystem library to std::filesystem implementation. The main
* purpose of the class is to define fs::path::u8string() and fs::u8path()
* functions not present in boost. It also blocks calls to the
* fs::path(std::string) implicit constructor and the fs::path::string()
* method, which worked well in the boost::filesystem implementation, but have
* unsafe and unpredictable behavior on Windows in the std::filesystem
* implementation (see implementation note in \ref PathToString for details).
* Path class wrapper to blocks 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)
*/
class path : public boost::filesystem::path
class path : public std::filesystem::path
{
public:
using boost::filesystem::path::path;
using std::filesystem::path::path;

// Allow path objects arguments for compatibility.
path(boost::filesystem::path path) : boost::filesystem::path::path(std::move(path)) {}
path& operator=(boost::filesystem::path path) { boost::filesystem::path::operator=(std::move(path)); return *this; }
path& operator/=(boost::filesystem::path path) { boost::filesystem::path::operator/=(std::move(path)); return *this; }
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; }

// Allow literal string arguments, which are safe as long as the literals are ASCII.
path(const char* c) : boost::filesystem::path(c) {}
path& operator=(const char* c) { boost::filesystem::path::operator=(c); return *this; }
path& operator/=(const char* c) { boost::filesystem::path::operator/=(c); return *this; }
path& append(const char* c) { boost::filesystem::path::append(c); return *this; }
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; }

// Disallow std::string arguments to avoid locale-dependent decoding on windows.
path(std::string) = delete;
Expand All @@ -54,35 +49,26 @@ class path : public boost::filesystem::path

// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
std::string string() const = delete;

// Define UTF-8 string conversion method not present in boost::filesystem but present in std::filesystem.
std::string u8string() const { return boost::filesystem::path::string(); }
};

// Define UTF-8 string conversion function not present in boost::filesystem but present in std::filesystem.
static inline path u8path(const std::string& string)
{
return boost::filesystem::path(string);
}

// Disallow implicit std::string conversion for system_complete to avoid
// Disallow implicit std::string conversion for absolute to avoid
// locale-dependent encoding on windows.
static inline path system_complete(const path& p)
static inline path absolute(const path& p)
{
return boost::filesystem::system_complete(p);
return std::filesystem::absolute(p);
}

// Disallow implicit std::string conversion for exists to avoid
// locale-dependent encoding on windows.
static inline bool exists(const path& p)
{
return boost::filesystem::exists(p);
return std::filesystem::exists(p);
}

// Allow explicit quoted stream I/O.
static inline auto quoted(const std::string& s)
{
return boost::io::quoted(s, '&');
return std::quoted(s, '"', '&');
}

// Allow safe path append operations.
Expand Down Expand Up @@ -123,7 +109,7 @@ static inline std::string PathToString(const path& path)
return path.u8string();
#else
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
return path.boost::filesystem::path::string();
return path.std::filesystem::path::string();
#endif
}

Expand All @@ -135,7 +121,7 @@ static inline path PathFromString(const std::string& string)
#ifdef WIN32
return u8path(string);
#else
return boost::filesystem::path(string);
return std::filesystem::path(string);
#endif
}
} // namespace fs
Expand Down Expand Up @@ -177,59 +163,14 @@ namespace fsbridge {

std::string get_filesystem_error_message(const fs::filesystem_error& e);

// GNU libstdc++ specific workaround for opening UTF-8 paths on Windows.
//
// On Windows, it is only possible to reliably access multibyte file paths through
// `wchar_t` APIs, not `char` APIs. But because the C++ standard doesn't
// require ifstream/ofstream `wchar_t` constructors, and the GNU library doesn't
// provide them (in contrast to the Microsoft C++ library, see
// https://stackoverflow.com/questions/821873/how-to-open-an-stdfstream-ofstream-or-ifstream-with-a-unicode-filename/822032#822032),
// Boost is forced to fall back to `char` constructors which may not work properly.
//
// Work around this issue by creating stream objects with `_wfopen` in
// combination with `__gnu_cxx::stdio_filebuf`. This workaround can be removed
// with an upgrade to C++17, where streams can be constructed directly from
// `std::filesystem::path` objects.

#if defined WIN32 && defined __GLIBCXX__
class ifstream : public std::istream
{
public:
ifstream() = default;
explicit ifstream(const fs::path& p, std::ios_base::openmode mode = std::ios_base::in) { open(p, mode); }
~ifstream() { close(); }
void open(const fs::path& p, std::ios_base::openmode mode = std::ios_base::in);
bool is_open() { return m_filebuf.is_open(); }
void close();

private:
__gnu_cxx::stdio_filebuf<char> m_filebuf;
FILE* m_file = nullptr;
};
class ofstream : public std::ostream
{
public:
ofstream() = default;
explicit ofstream(const fs::path& p, std::ios_base::openmode mode = std::ios_base::out) { open(p, mode); }
~ofstream() { close(); }
void open(const fs::path& p, std::ios_base::openmode mode = std::ios_base::out);
bool is_open() { return m_filebuf.is_open(); }
void close();

private:
__gnu_cxx::stdio_filebuf<char> m_filebuf;
FILE* m_file = nullptr;
};
#else // !(WIN32 && __GLIBCXX__)
typedef fs::ifstream ifstream;
typedef fs::ofstream ofstream;
#endif // WIN32 && __GLIBCXX__
typedef std::ifstream ifstream;
typedef std::ofstream ofstream;
};

// Disallow path operator<< formatting in tinyformat to avoid locale-dependent
// encoding on windows.
namespace tinyformat {
template<> inline void formatValue(std::ostream&, const char*, const char*, int, const boost::filesystem::path&) = delete;
template<> inline void formatValue(std::ostream&, const char*, const char*, int, const std::filesystem::path&) = delete;
template<> inline void formatValue(std::ostream&, const char*, const char*, int, const fs::path&) = delete;
} // namespace tinyformat

Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static fs::path GetPidFile(const ArgsManager& args)

[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)
{
fsbridge::ofstream file{GetPidFile(args)};
fsbridge::ofstream file{static_cast<std::filesystem::path>(GetPidFile(args))};
if (file) {
#ifdef WIN32
tfm::format(file, "%d\n", GetCurrentProcessId());
Expand Down
1 change: 1 addition & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <atomic>
#include <cstdint>
#include <functional>
#include <list>
#include <mutex>
#include <string>
Expand Down
4 changes: 2 additions & 2 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ bool openBitcoinConf()
fs::path pathConfig = GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));

/* Create the file */
fsbridge::ofstream configFile(pathConfig, std::ios_base::app);
fsbridge::ofstream configFile(static_cast<std::filesystem::path>(pathConfig), std::ios_base::app);

if (!configFile.good())
return false;
Expand Down Expand Up @@ -585,7 +585,7 @@ fs::path static GetAutostartFilePath()

bool GetStartOnSystemStartup()
{
fsbridge::ifstream optionFile(GetAutostartFilePath());
fsbridge::ifstream optionFile(static_cast<std::filesystem::path>(GetAutostartFilePath()));
if (!optionFile.good())
return false;
// Scan through file for "Hidden=true":
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
*/
fsbridge::ofstream file;
fs::path filepath_tmp = GetAuthCookieFile(true);
file.open(filepath_tmp);
file.open(static_cast<std::filesystem::path>(filepath_tmp));
if (!file.is_open()) {
LogPrintf("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp));
return false;
Expand All @@ -110,7 +110,7 @@ bool GetAuthCookie(std::string *cookie_out)
fsbridge::ifstream file;
std::string cookie;
fs::path filepath = GetAuthCookieFile();
file.open(filepath);
file.open(static_cast<std::filesystem::path>(filepath));
if (!file.is_open())
return false;
std::getline(file, cookie);
Expand Down
Loading

0 comments on commit b70c843

Please sign in to comment.