From 079e27335523ae25403a301127a0b4e59e58d219 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 5 Jul 2022 12:09:52 -0700 Subject: [PATCH] ircd: Upgrade to std::filesystem from boost::filesystem. --- configure.ac | 11 ++- include/ircd/fs/error.h | 47 +++++++------ include/ircd/fs/fs.h | 8 --- include/ircd/fs/path.h | 16 ++--- ircd/exec.cc | 2 +- ircd/fs.cc | 149 ++++++++++++++-------------------------- ircd/fs_path.cc | 91 ++++-------------------- ircd/mods.cc | 6 +- ircd/mods.h | 3 +- 9 files changed, 105 insertions(+), 228 deletions(-) diff --git a/configure.ac b/configure.ac index 51f098cc2..b503c1857 100644 --- a/configure.ac +++ b/configure.ac @@ -2559,7 +2559,7 @@ dnl BOOST_VERSION_MIN="1.66" BOOST_VERSION_MIN_PATCH="0" -BOOST_BUILT_LIBS="system,filesystem,context,coroutine,chrono" +BOOST_BUILT_LIBS="system,context,coroutine,chrono" AC_SUBST(BOOST_CPPFLAGS, []) AC_SUBST(BOOST_LDFLAGS, []) @@ -2640,7 +2640,6 @@ RB_HELP_STRING([--with-included-boost[[[=shared]]]], [Use the boost sources from AX_BOOST_ASIO AX_BOOST_SYSTEM - AX_BOOST_FILESYSTEM AX_BOOST_COROUTINE AX_BOOST_CONTEXT AX_BOOST_CHRONO @@ -2650,16 +2649,16 @@ RB_HELP_STRING([--with-included-boost[[[=shared]]]], [Use the boost sources from if [[ "$boost_linkage" = "shared" ]]; then AM_COND_IF([MINGW], [ - AC_SUBST(BOOST_LIBS, ["-lboost_coroutine-mgw53-1_61.dll -lboost_context-mgw53-1_61.dll -lboost_thread-mgw53-1_61.dll -lboost_filesystem-mgw53-1_61.dll -lboost_system-mgw53-1_61.dll"]) + AC_SUBST(BOOST_LIBS, ["-lboost_coroutine-mgw53-1_61.dll -lboost_context-mgw53-1_61.dll -lboost_thread-mgw53-1_61.dll -lboost_system-mgw53-1_61.dll"]) ], [ - AC_SUBST(BOOST_LIBS, ["-lboost_coroutine -lboost_context -lboost_thread -lboost_filesystem -lboost_chrono -lboost_system"]) + AC_SUBST(BOOST_LIBS, ["-lboost_coroutine -lboost_context -lboost_thread -lboost_chrono -lboost_system"]) ]) else AM_COND_IF([MINGW], [ - AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_coroutine-mgw53-1_61.dll.a $boost_libdir/libboost_context-mgw53-1_61.dll.a $boost_libdir/libboost_thread-mgw53-1_61.dll.a $boost_libdir/libboost_filesystem-mgw53-1_61.dll.a $boost_libdir/libboost_system-mgw53-1_61.dll.a"]) + AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_coroutine-mgw53-1_61.dll.a $boost_libdir/libboost_context-mgw53-1_61.dll.a $boost_libdir/libboost_thread-mgw53-1_61.dll.a $boost_libdir/libboost_system-mgw53-1_61.dll.a"]) ], [ - AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_coroutine.a $boost_libdir/libboost_context.a $boost_libdir/libboost_thread.a $boost_libdir/libboost_filesystem.a $boost_libdir/libboost_system.a"]) + AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_coroutine.a $boost_libdir/libboost_context.a $boost_libdir/libboost_thread.a $boost_libdir/libboost_system.a"]) ]) fi diff --git a/include/ircd/fs/error.h b/include/ircd/fs/error.h index 2165e8f93..c6487235a 100644 --- a/include/ircd/fs/error.h +++ b/include/ircd/fs/error.h @@ -11,12 +11,6 @@ #pragma once #define HAVE_IRCD_FS_ERROR_H -// Forward declarations for boost -namespace boost::filesystem -{ - struct filesystem_error; -} - namespace ircd::fs { struct error; // does not participate in ircd::exception hierarchy @@ -24,50 +18,55 @@ namespace ircd::fs namespace ircd { - std::error_code make_error_code(const boost::filesystem::filesystem_error &); - std::system_error make_system_error(const boost::filesystem::filesystem_error &); - - string_view string(const mutable_buffer &, const boost::filesystem::filesystem_error &); - std::string string(const boost::filesystem::filesystem_error &); + string_view string(const mutable_buffer &, const std::filesystem::filesystem_error &); + std::string string(const std::filesystem::filesystem_error &); } struct ircd::fs::error -:std::system_error +:std::filesystem::filesystem_error { - static thread_local char buf[1024]; + static constexpr size_t max_len {4096}; - public: template - error(const boost::filesystem::filesystem_error &e, + [[clang::internal_linkage]] + error(const std::filesystem::filesystem_error &e, const string_view &fmt, args&&...); template + [[clang::internal_linkage]] error(const std::error_code &e, const string_view &fmt, args&&...); - error(const boost::filesystem::filesystem_error &e); + error(const std::error_code &e, const string_view &fmt); + error(const std::filesystem::filesystem_error &e, const string_view &fmt); + error(const std::filesystem::filesystem_error &e); + ~error() noexcept; }; template -ircd::fs::error::error(const boost::filesystem::filesystem_error &e, +[[gnu::noinline, gnu::visibility("internal")]] +ircd::fs::error::error(const std::filesystem::filesystem_error &e, const string_view &fmt, args&&... a) -:error +:std::filesystem::filesystem_error { - make_error_code(e), fmt, std::forward(a)... + fmt::snstringf{max_len, fmt, std::forward(a)...}, + e.path1(), + e.path2(), + e.code(), } {} template +[[gnu::noinline, gnu::visibility("internal")]] ircd::fs::error::error(const std::error_code &e, const string_view &fmt, args&&... a) -:std::system_error{[&] -() -> std::system_error +:std::filesystem::filesystem_error { - fmt::sprintf{buf, fmt, std::forward(a)...}; - return {e, buf}; -}()} + fmt::snstringf{max_len, fmt, std::forward(a)...}, + e, +} {} diff --git a/include/ircd/fs/fs.h b/include/ircd/fs/fs.h index e6512b93c..c7ead3215 100644 --- a/include/ircd/fs/fs.h +++ b/include/ircd/fs/fs.h @@ -11,9 +11,6 @@ #pragma once #define HAVE_IRCD_FS_H -// Forward declarations for boost because it is not included here. -namespace boost::filesystem {} - /// Local filesystem interface. /// /// IRCd has wrapped operations for the local filesystem to maintain a @@ -31,11 +28,6 @@ namespace ircd::fs { struct init; - // Forward interface to boost::filesystem. We do not include boost - // from here; it is used internally only. Some exposed interfaces - // may make forward-declared references to boost symbols. - namespace filesystem = boost::filesystem; - // Log facility for ircd::fs extern log::log log; } diff --git a/include/ircd/fs/path.h b/include/ircd/fs/path.h index a5a4805c1..9e3e7c149 100644 --- a/include/ircd/fs/path.h +++ b/include/ircd/fs/path.h @@ -11,12 +11,6 @@ #pragma once #define HAVE_IRCD_FS_PATH_H -// Forward declarations for boost because it is not included here. -namespace boost::filesystem -{ - struct path; -} - // Filesystem path utility interface namespace ircd::fs { @@ -31,15 +25,15 @@ namespace ircd::fs extern const mutable_buffer name_scratch; // must include boost in unit to call these; use path() instead - filesystem::path _path(std::string); - filesystem::path _path(const string_view &); - filesystem::path _path(const path_views &); - filesystem::path _path(const path_strings &); + std::filesystem::path _path(std::string); + std::filesystem::path _path(const string_view &); + std::filesystem::path _path(const path_views &); + std::filesystem::path _path(const path_strings &); // append path strings together to create a viable result amalgam. string_view path(const mutable_buffer &, const path_views &); string_view path(const mutable_buffer &, const path_strings &); - string_view path(const mutable_buffer &, const filesystem::path &); + string_view path(const mutable_buffer &, const std::filesystem::path &); // guarantees result is contained within the base, mitigating `../` etc. string_view path(const mutable_buffer &, const string_view &base, const path_views &); diff --git a/ircd/exec.cc b/ircd/exec.cc index e567eefcb..ae64b1f6b 100644 --- a/ircd/exec.cc +++ b/ircd/exec.cc @@ -108,7 +108,7 @@ try eptr = {}; child = std::make_unique ( - fs::_path(path), + fs::_path(path).string(), argv, (boost::process::std_in) = pipe->first, (boost::process::std_out & boost::process::std_err) = pipe->second, diff --git a/ircd/fs.cc b/ircd/fs.cc index 137408938..7ece4ff91 100644 --- a/ircd/fs.cc +++ b/ircd/fs.cc @@ -13,7 +13,6 @@ #include #if IRCD_USE_AIO > 0 #include "fs_aio.h" @@ -387,29 +386,19 @@ ircd::fs::support::rlimit_nofile() bool ircd::fs::mkdir(const string_view &path) -try { - return filesystem::create_directories(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::create_directories(_path(path)); } bool ircd::fs::remove(const string_view &path) -try { const prof::syscall_usage_warning message { "fs::remove(%s)", path }; - return filesystem::remove(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::remove(_path(path)); } bool @@ -421,27 +410,22 @@ ircd::fs::remove(std::nothrow_t, "fs::remove(%s)", path }; - boost::system::error_code ec; - return filesystem::remove(_path(path), ec); + std::error_code ec; + return std::filesystem::remove(_path(path), ec); } bool ircd::fs::rename(const string_view &old, const string_view &new_) -try { const prof::syscall_usage_warning message { "fs::rename(%s, %s)", old, new_ }; - filesystem::rename(_path(old), _path(new_)); + std::filesystem::rename(_path(old), _path(new_)); return true; } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} bool ircd::fs::rename(std::nothrow_t, @@ -453,17 +437,16 @@ ircd::fs::rename(std::nothrow_t, "fs::rename(%s, %s)", old, new_ }; - boost::system::error_code ec; - filesystem::rename(_path(old), _path(new_), ec); + std::error_code ec; + std::filesystem::rename(_path(old), _path(new_), ec); return !ec; } std::vector ircd::fs::ls_r(const string_view &path) -try { - const filesystem::recursive_directory_iterator end; - filesystem::recursive_directory_iterator it + const std::filesystem::recursive_directory_iterator end; + std::filesystem::recursive_directory_iterator it { _path(path) }; @@ -477,17 +460,12 @@ try return ret; } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} std::vector ircd::fs::ls(const string_view &path) -try { - static const filesystem::directory_iterator end; - filesystem::directory_iterator it + static const std::filesystem::directory_iterator end; + std::filesystem::directory_iterator it { _path(path) }; @@ -501,64 +479,40 @@ try return ret; } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} size_t ircd::fs::size(const string_view &path) -try { - return filesystem::file_size(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::file_size(_path(path)); } bool ircd::fs::is_exec(const string_view &path) -try { - return filesystem::status(_path(path)).permissions() & filesystem::owner_exe; -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + static const auto mask + { + std::filesystem::perms::owner_exec + }; + + return ulong(std::filesystem::status(_path(path)).permissions() & mask); } bool ircd::fs::is_reg(const string_view &path) -try { - return filesystem::is_regular_file(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::is_regular_file(_path(path)); } bool ircd::fs::is_dir(const string_view &path) -try { - return filesystem::is_directory(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::is_directory(_path(path)); } bool ircd::fs::exists(const string_view &path) -try { - return filesystem::exists(_path(path)); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::exists(_path(path)); } /////////////////////////////////////////////////////////////////////////////// @@ -2838,7 +2792,7 @@ ircd::fs::bytes(const const_iovec_view &iov) // std::string -ircd::string(const boost::filesystem::filesystem_error &e) +ircd::string(const std::filesystem::filesystem_error &e) { return ircd::string(512, [&e] (const mutable_buffer &buf) @@ -2849,7 +2803,7 @@ ircd::string(const boost::filesystem::filesystem_error &e) ircd::string_view ircd::string(const mutable_buffer &buf, - const boost::filesystem::filesystem_error &e) + const std::filesystem::filesystem_error &e) { return fmt::sprintf { @@ -2857,42 +2811,45 @@ ircd::string(const mutable_buffer &buf, }; } -std::system_error -ircd::make_system_error(const boost::filesystem::filesystem_error &e) -{ - return std::system_error - { - make_error_code(e), e.what() - }; -} - -std::error_code -ircd::make_error_code(const boost::filesystem::filesystem_error &e) -{ - const boost::system::error_code &ec - { - e.code() - }; - - return make_error_code(ec); -} - // // error::error // -decltype(ircd::fs::error::buf) -thread_local -ircd::fs::error::buf; - -ircd::fs::error::error(const boost::filesystem::filesystem_error &e) -:std::system_error +ircd::fs::error::error(const std::error_code &e, + const string_view &fmt) +:std::filesystem::filesystem_error { - make_system_error(e) + fmt, + e, } { } +ircd::fs::error::error(const std::filesystem::filesystem_error &e, + const string_view &fmt) +:std::filesystem::filesystem_error +{ + fmt, + e.path1(), + e.path2(), + e.code(), +} +{ +} + +ircd::fs::error::error(const std::filesystem::filesystem_error &e) +:std::filesystem::filesystem_error +{ + e +} +{ +} + +ircd::fs::error::~error() +noexcept +{ +} + /////////////////////////////////////////////////////////////////////////////// // // Internal utils diff --git a/ircd/fs_path.cc b/ircd/fs_path.cc index 4305bd49c..1278218f1 100644 --- a/ircd/fs_path.cc +++ b/ircd/fs_path.cc @@ -11,7 +11,6 @@ #include /// Default maximum path string length (for all filesystems & platforms). decltype(ircd::fs::NAME_MAX_LEN) @@ -241,7 +240,7 @@ try { return path(buf, canonical(_path(p))); } -catch(const filesystem::filesystem_error &e) +catch(const std::filesystem::filesystem_error &e) { throw error { @@ -256,9 +255,9 @@ ircd::fs::canonical(const mutable_buffer &buf, const string_view &p) try { - return path(buf, canonical(_path(p), _path(root))); + return path(buf, std::filesystem::canonical(_path(root) / _path(p))); } -catch(const filesystem::filesystem_error &e) +catch(const std::filesystem::filesystem_error &e) { throw error { @@ -271,76 +270,46 @@ ircd::string_view ircd::fs::relative(const mutable_buffer &buf, const string_view &root, const string_view &p) -try { - return path(buf, relative(_path(p), _path(root))); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return path(buf, std::filesystem::relative(_path(root) / _path(p))); } ircd::string_view ircd::fs::absolute(const mutable_buffer &buf, const string_view &root, const string_view &p) -try { - return path(buf, absolute(_path(p), _path(root))); -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return path(buf, std::filesystem::absolute(_path(root) / _path(p))); } ircd::string_view ircd::fs::parent(const mutable_buffer &buf, const string_view &p) -try { return path(buf, _path(p).parent_path()); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} ircd::string_view ircd::fs::filename(const mutable_buffer &buf, const string_view &p) -try { return path(buf, _path(p).filename()); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} ircd::string_view ircd::fs::extension(const mutable_buffer &buf, const string_view &p) -try { return path(buf, _path(p).extension()); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} ircd::string_view ircd::fs::extension(const mutable_buffer &buf, const string_view &p, const string_view &replace) -try { return path(buf, _path(p).replace_extension(_path(replace))); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} bool ircd::fs::is_relative(const string_view &p) @@ -360,35 +329,25 @@ ircd::fs::is_absolute(const string_view &p) std::string ircd::fs::cwd() -try { const auto &cur { - filesystem::current_path() + std::filesystem::current_path() }; return cur.string(); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} ircd::string_view ircd::fs::cwd(const mutable_buffer &buf) -try { const auto &cur { - filesystem::current_path() + std::filesystem::current_path() }; return strlcpy(buf, cur.native()); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} #ifdef _PC_PATH_MAX size_t @@ -510,7 +469,7 @@ ircd::fs::path(const mutable_buffer &buf, ircd::string_view ircd::fs::path(const mutable_buffer &buf, - const filesystem::path &path) + const std::filesystem::path &path) { return strlcpy(buf, path.c_str()); } @@ -533,54 +492,34 @@ ircd::fs::path(const mutable_buffer &buf, // fs::_path() // -boost::filesystem::path +std::filesystem::path ircd::fs::_path(const path_strings &list) -try { - filesystem::path ret; + std::filesystem::path ret; for(const auto &s : list) ret /= s; return ret.string(); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} -boost::filesystem::path +std::filesystem::path ircd::fs::_path(const path_views &list) -try { - filesystem::path ret; + std::filesystem::path ret; for(const auto &s : list) ret /= _path(s); return ret.string(); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} -boost::filesystem::path +std::filesystem::path ircd::fs::_path(const string_view &s) -try { return _path(std::string{s}); } -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; -} -boost::filesystem::path +std::filesystem::path ircd::fs::_path(std::string s) -try { - return filesystem::path{std::move(s)}; -} -catch(const filesystem::filesystem_error &e) -{ - throw error{e}; + return std::filesystem::path{std::move(s)}; } diff --git a/ircd/mods.cc b/ircd/mods.cc index f7ebd389f..c1c08f8e9 100644 --- a/ircd/mods.cc +++ b/ircd/mods.cc @@ -473,7 +473,7 @@ ircd::mods::available() ret.emplace_front(unpostfixed(std::move(relpath))); } } - catch(const filesystem::filesystem_error &e) + catch(const std::filesystem::filesystem_error &e) { log::warning { @@ -631,8 +631,6 @@ catch(const std::exception &e) bool ircd::mods::available(const string_view &name) { - using filesystem::path; - std::vector why; return !search(name, why).empty(); } @@ -1136,7 +1134,7 @@ try return closure(info); } -catch(const filesystem::filesystem_error &e) +catch(const std::filesystem::filesystem_error &e) { throw fs::error { diff --git a/ircd/mods.h b/ircd/mods.h index 5ddf175a8..e22a0f8d9 100644 --- a/ircd/mods.h +++ b/ircd/mods.h @@ -8,11 +8,10 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. -#include +#define BOOST_DLL_USE_STD_FS #include #include // Module's internal API -namespace filesystem = boost::filesystem; namespace load_mode = boost::dll::load_mode; namespace ircd::mods {