From 77d3a07ff22fc8bb67a5bf2ccd8f022a94bc00f0 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 28 Jul 2024 17:25:20 -0700 Subject: [PATCH] Fix std::filesystem This change makes a second pass, at fixing the errno issue with libcxx's filesystem code. Previously, 89.01% of LLVM's test suite was passing and now 98.59% of their tests pass. Best of all, it's now possible for Clang to be built as a working APE binary that can to compile the Cosmopolitan repository. Please note it has only been vetted so far for some objects, and more work would obviously need to be done in cosmo, to fix warnings. --- test/libcxx/errc_test.cc | 22 ++ test/libcxx/filesystem_test.cc | 341 ++++++++++++++++++ third_party/libcxx/BUILD.mk | 2 +- third_party/libcxx/__system_error/errc.h | 4 + .../libcxx/__system_error/error_code.h | 13 +- .../libcxx/__system_error/error_condition.h | 10 +- .../libcxx/__system_error/system_error.h | 1 + third_party/libcxx/{fs/cosmo.cpp => errc.cpp} | 29 +- third_party/libcxx/fs/directory_iterator.cpp | 8 - third_party/libcxx/fs/error.h | 9 +- third_party/libcxx/fs/file_descriptor.h | 4 - third_party/libcxx/ios | 1 + third_party/libcxx/system_error | 2 +- third_party/libcxx/system_error.cpp | 31 +- 14 files changed, 408 insertions(+), 69 deletions(-) create mode 100644 test/libcxx/errc_test.cc create mode 100644 test/libcxx/filesystem_test.cc rename third_party/libcxx/{fs/cosmo.cpp => errc.cpp} (95%) diff --git a/test/libcxx/errc_test.cc b/test/libcxx/errc_test.cc new file mode 100644 index 00000000000..373cdf78ce4 --- /dev/null +++ b/test/libcxx/errc_test.cc @@ -0,0 +1,22 @@ +#include +#include + +bool test_errc_mapping(int posix_error, std::errc expected_errc) { + std::error_code ec(posix_error, std::generic_category()); + return ec.value() == posix_error && // + std::error_condition(expected_errc) == ec; +} + +int main() { + if (!test_errc_mapping(EACCES, std::errc::permission_denied)) + return 1; + if (!test_errc_mapping(ENOENT, std::errc::no_such_file_or_directory)) + return 2; + if (!test_errc_mapping(EEXIST, std::errc::file_exists)) + return 3; + if (!test_errc_mapping(EINVAL, std::errc::invalid_argument)) + return 4; + if (!test_errc_mapping(ENOSPC, std::errc::no_space_on_device)) + return 5; + return 0; +} diff --git a/test/libcxx/filesystem_test.cc b/test/libcxx/filesystem_test.cc new file mode 100644 index 00000000000..762e650b297 --- /dev/null +++ b/test/libcxx/filesystem_test.cc @@ -0,0 +1,341 @@ +#include +#include +#include +#include +#include +#include +#include + +#define ASSERT(condition) \ + if (!(condition)) { \ + fprintf(stderr, "%s:%d: test failed: %s\n", __FILE__, __LINE__, \ + #condition); \ + return 1; \ + } + +namespace fs = std::filesystem; + +fs::path g_temp_path; +fs::path g_orig_path; +std::string g_tmpdir; + +void setup() { + g_orig_path = fs::current_path(); + fs::path temp_path = fs::temp_directory_path(); + auto now = std::chrono::system_clock::now(); + auto now_ms = std::chrono::time_point_cast(now); + auto value = now_ms.time_since_epoch(); + long duration = value.count(); + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(0, 999999); + int random_number = dis(gen); + std::string dir_name = + "temp_" + std::to_string(duration) + "_" + std::to_string(random_number); + g_temp_path = temp_path / dir_name; + fs::create_directory(g_temp_path); + fs::current_path(g_temp_path); + fs::create_directory("tmp"); + g_tmpdir = fs::absolute("tmp"); + setenv("TMPDIR", g_tmpdir.c_str(), true); +} + +void teardown() { + fs::current_path(g_orig_path); + fs::remove_all(g_temp_path); +} + +int test_create_directory() { + fs::path dir = "test_dir"; + ASSERT(fs::create_directory(dir)); + ASSERT(fs::is_directory(dir)); + ASSERT(!fs::create_directory(dir)); + fs::remove(dir); + return 0; +} + +int test_create_directories() { + fs::path dirs = "test_dir/nested/deep"; + ASSERT(fs::create_directories(dirs)); + ASSERT(fs::is_directory(dirs)); + ASSERT(!fs::create_directories(dirs)); + fs::remove_all("test_dir"); + return 0; +} + +int test_remove() { + fs::path file = "test_file.txt"; + std::ofstream(file).put('a'); + ASSERT(fs::remove(file)); + ASSERT(!fs::remove(file)); + return 0; +} + +int test_remove_all() { + fs::path dir = "test_dir/nested/deep"; + fs::create_directories(dir); + ASSERT(fs::remove_all("test_dir") > 0); + ASSERT(fs::remove_all("test_dir") == 0); + return 0; +} + +int test_rename() { + fs::path old_name = "old.txt"; + fs::path new_name = "new.txt"; + std::ofstream(old_name).put('a'); + fs::rename(old_name, new_name); + ASSERT(!fs::exists(old_name)); + ASSERT(fs::exists(new_name)); + fs::remove(new_name); + return 0; +} + +int test_copy() { + fs::path src = "src.txt"; + fs::path dst = "dst.txt"; + std::ofstream(src) << "test"; + fs::copy(src, dst); + ASSERT(fs::exists(dst)); + ASSERT(fs::file_size(src) == fs::file_size(dst)); + fs::remove(src); + fs::remove(dst); + return 0; +} + +int test_copy_file() { + fs::path src = "src.txt"; + fs::path dst = "dst.txt"; + std::ofstream(src) << "test"; + ASSERT(fs::copy_file(src, dst)); + ASSERT(!fs::copy_file(src, dst, fs::copy_options::skip_existing)); + fs::remove(src); + fs::remove(dst); + return 0; +} + +int test_exists() { + fs::path file = "test.txt"; + ASSERT(!fs::exists(file)); + std::ofstream(file).put('a'); + ASSERT(fs::exists(file)); + fs::remove(file); + return 0; +} + +int test_is_regular_file() { + fs::path file = "test.txt"; + fs::path dir = "test_dir"; + std::ofstream(file).put('a'); + fs::create_directory(dir); + ASSERT(fs::is_regular_file(file)); + ASSERT(!fs::is_regular_file(dir)); + fs::remove(file); + fs::remove(dir); + return 0; +} + +int test_is_directory() { + fs::path file = "test.txt"; + fs::path dir = "test_dir"; + std::ofstream(file).put('a'); + fs::create_directory(dir); + ASSERT(!fs::is_directory(file)); + ASSERT(fs::is_directory(dir)); + fs::remove(file); + fs::remove(dir); + return 0; +} + +int test_is_symlink() { + fs::path file = "test.txt"; + fs::path link = "test_link"; + std::ofstream(file).put('a'); + fs::create_symlink(file, link); + ASSERT(!fs::is_symlink(file)); + ASSERT(fs::is_symlink(link)); + fs::remove(file); + fs::remove(link); + return 0; +} + +int test_file_size() { + fs::path file = "test.txt"; + std::ofstream(file) << "test"; + ASSERT(fs::file_size(file) == 4); + fs::remove(file); + return 0; +} + +int test_last_write_time() { + fs::path file = "test.txt"; + auto now = fs::file_time_type::clock::now(); + std::ofstream(file).put('a'); + fs::last_write_time(file, now); + ASSERT(fs::last_write_time(file) == now); + fs::remove(file); + return 0; +} + +int test_permissions() { + fs::path file = "test.txt"; + std::ofstream(file).put('a'); + fs::permissions(file, fs::perms::owner_read | fs::perms::owner_write); + auto perms = fs::status(file).permissions(); + ASSERT((perms & fs::perms::owner_read) != fs::perms::none); + ASSERT((perms & fs::perms::owner_write) != fs::perms::none); + ASSERT((perms & fs::perms::owner_exec) == fs::perms::none); + fs::remove(file); + return 0; +} + +int test_current_path() { + auto original_path = fs::current_path(); + fs::path new_path = fs::temp_directory_path(); + fs::current_path(new_path); + ASSERT(fs::current_path() == new_path); + fs::current_path(original_path); + return 0; +} + +int test_absolute() { + fs::path relative = "test.txt"; + auto abs_path = fs::absolute(relative); + ASSERT(abs_path.is_absolute()); + return 0; +} + +int test_canonical() { + fs::path dir = "test_dir"; + fs::path file = dir / "test.txt"; + fs::create_directories(dir); + std::ofstream(file).put('a'); + auto can_path = fs::canonical(file); + ASSERT(can_path.is_absolute()); + ASSERT(!can_path.lexically_normal().string().empty()); + fs::remove_all(dir); + return 0; +} + +int test_read_symlink() { + fs::path file = "test.txt"; + fs::path link = "test_link"; + std::ofstream(file).put('a'); + fs::create_symlink(file, link); + ASSERT(fs::read_symlink(link) == file); + fs::remove(file); + fs::remove(link); + return 0; +} + +int test_create_symlink_and_hard_link() { + fs::path file = "test.txt"; + fs::path symlink = "test_symlink"; + fs::path hardlink = "test_hardlink"; + std::ofstream(file).put('a'); + fs::create_symlink(file, symlink); + fs::create_hard_link(file, hardlink); + ASSERT(fs::exists(symlink)); + ASSERT(fs::exists(hardlink)); + ASSERT(fs::is_symlink(symlink)); + ASSERT(!fs::is_symlink(hardlink)); + fs::remove(file); + fs::remove(symlink); + fs::remove(hardlink); + return 0; +} + +int test_space() { + auto space_info = fs::space("."); + ASSERT(space_info.capacity > 0); + ASSERT(space_info.free > 0); + ASSERT(space_info.available > 0); + return 0; +} + +int test_equivalent() { + fs::path file1 = "test1.txt"; + fs::path file2 = "test2.txt"; + std::ofstream(file1).put('a'); + fs::create_hard_link(file1, file2); + ASSERT(fs::equivalent(file1, file2)); + fs::remove(file1); + fs::remove(file2); + return 0; +} + +int test_resize_file() { + fs::path file = "test.txt"; + std::ofstream(file) << "test"; + fs::resize_file(file, 10); + ASSERT(fs::file_size(file) == 10); + fs::remove(file); + return 0; +} + +int test_status() { + fs::path file = "test.txt"; + std::ofstream(file).put('a'); + auto status = fs::status(file); + ASSERT(status.type() == fs::file_type::regular); + fs::remove(file); + return 0; +} + +int test_copy_enoent() { + fs::path src = "non_existent_file.txt"; + fs::path dst = "destination.txt"; + try { + fs::copy(src, dst); + ASSERT(false); + } catch (const fs::filesystem_error& e) { + if (e.code() == std::errc::no_such_file_or_directory) { + return 0; + } else { + ASSERT(false); + } + } catch (const std::exception& e) { + ASSERT(false); + } +} + +#define RUN(func) \ + result = func(); \ + if (result) \ + return result + +int test() { + int result = 0; + RUN(test_copy_enoent); + RUN(test_create_directory); + RUN(test_create_directories); + RUN(test_remove); + RUN(test_remove_all); + RUN(test_rename); + RUN(test_copy); + RUN(test_copy_file); + RUN(test_exists); + RUN(test_is_regular_file); + RUN(test_is_directory); + RUN(test_is_symlink); + RUN(test_file_size); + RUN(test_last_write_time); + RUN(test_permissions); + RUN(test_current_path); + RUN(test_absolute); + RUN(test_canonical); + RUN(test_read_symlink); + RUN(test_create_symlink_and_hard_link); + RUN(test_space); + RUN(test_equivalent); + RUN(test_resize_file); + RUN(test_status); + return 0; +} + +int main() { + int rc; + setup(); + rc = test(); + teardown(); + return rc; +} diff --git a/third_party/libcxx/BUILD.mk b/third_party/libcxx/BUILD.mk index 22c4a0d840f..fca4bebf107 100644 --- a/third_party/libcxx/BUILD.mk +++ b/third_party/libcxx/BUILD.mk @@ -1094,11 +1094,11 @@ third_party/libcxx/fs/filesystem_clock.cpp \ third_party/libcxx/fs/filesystem_error.cpp \ third_party/libcxx/fs/int128_builtins.cpp \ third_party/libcxx/fs/operations.cpp \ -third_party/libcxx/fs/cosmo.cpp \ third_party/libcxx/fs/path.cpp \ third_party/libcxx/ryu/d2fixed.cpp \ third_party/libcxx/ryu/d2s.cpp \ third_party/libcxx/ryu/f2s.cpp \ +third_party/libcxx/errc.cpp \ THIRD_PARTY_LIBCXX_A_HDRS_CHECKEM = \ third_party/libcxx/__assertion_handler \ diff --git a/third_party/libcxx/__system_error/errc.h b/third_party/libcxx/__system_error/errc.h index 33a2645d920..8f6532ec960 100644 --- a/third_party/libcxx/__system_error/errc.h +++ b/third_party/libcxx/__system_error/errc.h @@ -141,6 +141,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD // This leads to the odd pushing and popping of the deprecated // diagnostic. _LIBCPP_DECLARE_STRONG_ENUM(errc){ + success = 0, address_family_not_supported = 65536, // = EAFNOSUPPORT, address_in_use, // = EADDRINUSE, address_not_available, // = EADDRNOTAVAIL, @@ -221,6 +222,9 @@ _LIBCPP_DECLARE_STRONG_ENUM(errc){ wrong_protocol_type}; _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc) +errc __err_to_errc(int) noexcept; +int __errc_to_err(errc) noexcept; + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP___ERRC diff --git a/third_party/libcxx/__system_error/error_code.h b/third_party/libcxx/__system_error/error_code.h index 475f2bb96a5..4f6e1494555 100644 --- a/third_party/libcxx/__system_error/error_code.h +++ b/third_party/libcxx/__system_error/error_code.h @@ -46,8 +46,9 @@ class _LIBCPP_EXPORTED_FROM_ABI error_code { public: _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {} - - _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {} + _LIBCPP_HIDE_FROM_ABI error_code(errc __val) _NOEXCEPT : __val_(__errc_to_err(__val)), __cat_(&system_category()) {} + _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__errc_to_err((errc)__val)), __cat_(&__cat) {} + _LIBCPP_HIDE_FROM_ABI error_code(errc __val, const error_category& __cat) _NOEXCEPT : __val_(__errc_to_err(__val)), __cat_(&__cat) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT { @@ -72,7 +73,7 @@ class _LIBCPP_EXPORTED_FROM_ABI error_code { __cat_ = &system_category(); } - _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; } + _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __errc_to_err((errc)__val_); } _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; } @@ -86,13 +87,17 @@ class _LIBCPP_EXPORTED_FROM_ABI error_code { }; inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT { - return error_code(static_cast(__e), generic_category()); + return error_code(__e, generic_category()); } inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_code& __y) _NOEXCEPT { return __x.category() == __y.category() && __x.value() == __y.value(); } +inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, errc __y) _NOEXCEPT { + return __x == error_code(__y, __x.category()); +} + inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT { return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value()); } diff --git a/third_party/libcxx/__system_error/error_condition.h b/third_party/libcxx/__system_error/error_condition.h index 42898c1f0e9..9003bc9195c 100644 --- a/third_party/libcxx/__system_error/error_condition.h +++ b/third_party/libcxx/__system_error/error_condition.h @@ -54,8 +54,12 @@ class _LIBCPP_EXPORTED_FROM_ABI error_condition { public: _LIBCPP_HIDE_FROM_ABI error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {} + _LIBCPP_HIDE_FROM_ABI error_condition(errc __val, const error_category& __cat) _NOEXCEPT + : __val_(__errc_to_err(__val)), + __cat_(&__cat) {} + _LIBCPP_HIDE_FROM_ABI error_condition(int __val, const error_category& __cat) _NOEXCEPT - : __val_(__val), + : __val_(__errc_to_err((errc)__val)), __cat_(&__cat) {} template ::value, int> = 0> @@ -81,7 +85,7 @@ class _LIBCPP_EXPORTED_FROM_ABI error_condition { __cat_ = &generic_category(); } - _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; } + _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __errc_to_err((errc)__val_); } _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; } string message() const; @@ -90,7 +94,7 @@ class _LIBCPP_EXPORTED_FROM_ABI error_condition { }; inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(errc __e) _NOEXCEPT { - return error_condition(static_cast(__e), generic_category()); + return error_condition(__e, generic_category()); } inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT { diff --git a/third_party/libcxx/__system_error/system_error.h b/third_party/libcxx/__system_error/system_error.h index 362e6750565..ff41ea9a7db 100644 --- a/third_party/libcxx/__system_error/system_error.h +++ b/third_party/libcxx/__system_error/system_error.h @@ -11,6 +11,7 @@ #define _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H #include <__config> +#include <__system_error/errc.h> #include <__system_error/error_category.h> #include <__system_error/error_code.h> #include <__verbose_abort> diff --git a/third_party/libcxx/fs/cosmo.cpp b/third_party/libcxx/errc.cpp similarity index 95% rename from third_party/libcxx/fs/cosmo.cpp rename to third_party/libcxx/errc.cpp index fbb2ab1d41f..101fe97d691 100644 --- a/third_party/libcxx/fs/cosmo.cpp +++ b/third_party/libcxx/errc.cpp @@ -1,11 +1,8 @@ -#ifdef __COSMOPOLITAN__ -#include +#include <__system_error/errc.h> -_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM +_LIBCPP_BEGIN_NAMESPACE_STD -namespace detail { - -std::errc __cosmo_err_to_errc_impl(int err) { +static std::errc __err_to_errc_impl(int err) noexcept { if (err == EAFNOSUPPORT) return errc::address_family_not_supported; if (err == EADDRINUSE) return errc::address_in_use; if (err == EADDRNOTAVAIL) return errc::address_not_available; @@ -87,7 +84,7 @@ std::errc __cosmo_err_to_errc_impl(int err) { return errc::not_supported; } -int __cosmo_errc_to_err_impl(std::errc err) { +static int __errc_to_err_impl(std::errc err) noexcept { if (err == errc::address_family_not_supported) return EAFNOSUPPORT; if (err == errc::address_in_use) return EADDRINUSE; if (err == errc::address_not_available) return EADDRNOTAVAIL; @@ -169,20 +166,20 @@ int __cosmo_errc_to_err_impl(std::errc err) { return ENOTSUP; } -std::errc __cosmo_err_to_errc(int err) { +std::errc __err_to_errc(int err) noexcept { + if (!err) + return (std::errc)0; if (err >= 65536) return (std::errc)err; - return __cosmo_err_to_errc_impl(err); + return __err_to_errc_impl(err); } -int __cosmo_errc_to_err(std::errc err) { +int __errc_to_err(std::errc err) noexcept { + if (!(int)err) + return 0; if ((int)err < 65536) return (int)err; - return __cosmo_errc_to_err_impl(err); + return __errc_to_err_impl(err); } -} // end namespace detail - -_LIBCPP_END_NAMESPACE_FILESYSTEM - -#endif // __COSMOPOLITAN__ +_LIBCPP_END_NAMESPACE_STD diff --git a/third_party/libcxx/fs/directory_iterator.cpp b/third_party/libcxx/fs/directory_iterator.cpp index a7ffa91889f..6602d42ac84 100644 --- a/third_party/libcxx/fs/directory_iterator.cpp +++ b/third_party/libcxx/fs/directory_iterator.cpp @@ -118,11 +118,7 @@ class __dir_stream { if ((__stream_ = ::opendir(root.c_str())) == nullptr) { ec = detail::capture_errno(); const bool allow_eacces = bool(opts & directory_options::skip_permission_denied); -#ifdef __COSMOPOLITAN__ if (allow_eacces && ec.value() == (int)errc::permission_denied) -#else - if (allow_eacces && ec.value() == EACCES) -#endif ec.clear(); return; } @@ -311,11 +307,7 @@ bool recursive_directory_iterator::__try_recursion(error_code* ec) { } if (m_ec) { const bool allow_eacess = bool(__imp_->__options_ & directory_options::skip_permission_denied); -#ifdef __COSMOPOLITAN__ if (m_ec.value() == (int)errc::permission_denied && allow_eacess) { -#else - if (m_ec.value() == EACCES && allow_eacess) { -#endif if (ec) ec->clear(); } else { diff --git a/third_party/libcxx/fs/error.h b/third_party/libcxx/fs/error.h index e74d0691766..21742d77e32 100644 --- a/third_party/libcxx/fs/error.h +++ b/third_party/libcxx/fs/error.h @@ -98,16 +98,9 @@ inline errc __win_err_to_errc(int err) { #endif // _LIBCPP_WIN32API -errc __cosmo_err_to_errc(int); -int __cosmo_errc_to_err(errc); - inline error_code capture_errno() { _LIBCPP_ASSERT_INTERNAL(errno != 0, "Expected errno to be non-zero"); -#ifdef __COSMOPOLITAN__ - return error_code((int)__cosmo_err_to_errc(errno), generic_category()); -#else - return error_code(errno, generic_category()); -#endif + return error_code((int)__err_to_errc(errno), generic_category()); } #if defined(_LIBCPP_WIN32API) diff --git a/third_party/libcxx/fs/file_descriptor.h b/third_party/libcxx/fs/file_descriptor.h index d41fe77bb38..eab0aa2dea3 100644 --- a/third_party/libcxx/fs/file_descriptor.h +++ b/third_party/libcxx/fs/file_descriptor.h @@ -194,12 +194,8 @@ inline perms posix_get_perms(const StatT& st) noexcept { return static_cast err("posix_stat", ec, &p); diff --git a/third_party/libcxx/ios b/third_party/libcxx/ios index d8a3643c7ad..92c32f2037a 100644 --- a/third_party/libcxx/ios +++ b/third_party/libcxx/ios @@ -13,6 +13,7 @@ /* ios synopsis +#include "third_party/libcxx/__system_error/error_code.h" #include namespace std diff --git a/third_party/libcxx/system_error b/third_party/libcxx/system_error index eeab347788a..2d3af910dcf 100644 --- a/third_party/libcxx/system_error +++ b/third_party/libcxx/system_error @@ -146,8 +146,8 @@ template <> struct hash; #include <__config> #include <__system_error/errc.h> -#include <__system_error/error_category.h> #include <__system_error/error_code.h> +#include <__system_error/error_category.h> #include <__system_error/error_condition.h> #include <__system_error/system_error.h> #include diff --git a/third_party/libcxx/system_error.cpp b/third_party/libcxx/system_error.cpp index 6f6b417a9f1..5c5df1fdab6 100644 --- a/third_party/libcxx/system_error.cpp +++ b/third_party/libcxx/system_error.cpp @@ -16,6 +16,7 @@ #include #include #include +#include <__system_error/errc.h> #include "config_elast.h" @@ -23,10 +24,6 @@ # include #endif -#ifdef __COSMOPOLITAN__ -#include -#endif - _LIBCPP_BEGIN_NAMESPACE_STD namespace { @@ -39,9 +36,7 @@ string do_strerror_r(int ev); # if defined(_LIBCPP_MSVCRT_LIKE) string do_strerror_r(int ev) { -#ifdef __COSMOPOLITAN__ - ev = (int)filesystem::detail::__cosmo_errc_to_err(ev); -#endif + ev = __errc_to_err(ev); char buffer[strerror_buff_size]; if (::strerror_s(buffer, strerror_buff_size, ev) == 0) return string(buffer); @@ -88,9 +83,7 @@ string do_strerror_r(int ev) { // Preserve errno around the call. (The C++ standard requires that // system_error functions not modify errno). const int old_errno = errno; -#ifdef __COSMOPOLITAN__ - ev = filesystem::detail::__cosmo_errc_to_err((errc)ev); -#endif + ev = __errc_to_err((errc)ev); const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer); // If we didn't get any message, print one now. if (!error_message[0]) { @@ -139,9 +132,7 @@ class _LIBCPP_HIDDEN __generic_error_category : public __do_message { const char* __generic_error_category::name() const noexcept { return "generic"; } string __generic_error_category::message(int ev) const { -#ifdef __COSMOPOLITAN__ - ev = filesystem::detail::__cosmo_errc_to_err((errc)ev); -#endif + ev = __errc_to_err((errc)ev); #ifdef _LIBCPP_ELAST if (ev > _LIBCPP_ELAST) return string("unspecified generic_category error"); @@ -169,9 +160,7 @@ class _LIBCPP_HIDDEN __system_error_category : public __do_message { const char* __system_error_category::name() const noexcept { return "system"; } string __system_error_category::message(int ev) const { -#ifdef __COSMOPOLITAN__ - ev = filesystem::detail::__cosmo_errc_to_err((errc)ev); -#endif + ev = __errc_to_err((errc)ev); #ifdef _LIBCPP_ELAST if (ev > _LIBCPP_ELAST) return string("unspecified system_category error"); @@ -180,9 +169,7 @@ string __system_error_category::message(int ev) const { } error_condition __system_error_category::default_error_condition(int ev) const noexcept { -#ifdef __COSMOPOLITAN__ - ev = filesystem::detail::__cosmo_errc_to_err((errc)ev); -#endif + ev = __errc_to_err((errc)ev); #ifdef _LIBCPP_ELAST if (ev > _LIBCPP_ELAST) return error_condition(ev, system_category()); @@ -231,11 +218,7 @@ system_error::~system_error() noexcept {} void __throw_system_error(int ev, const char* what_arg) { #ifndef _LIBCPP_HAS_NO_EXCEPTIONS -#ifdef __COSMOPOLITAN__ - std::__throw_system_error(error_code((int)filesystem::detail::__cosmo_err_to_errc(ev), system_category()), what_arg); -#else - std::__throw_system_error(error_code(ev, system_category()), what_arg); -#endif + std::__throw_system_error(error_code((int)__err_to_errc(ev), system_category()), what_arg); #else // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily. _LIBCPP_VERBOSE_ABORT(