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.
This commit is contained in:
Justine Tunney 2024-07-28 17:25:20 -07:00
parent 0d7c272d3f
commit 77d3a07ff2
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
14 changed files with 408 additions and 69 deletions

22
test/libcxx/errc_test.cc Normal file
View file

@ -0,0 +1,22 @@
#include <cerrno>
#include <system_error>
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;
}

View file

@ -0,0 +1,341 @@
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <random>
#include <string>
#include <thread>
#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<std::chrono::milliseconds>(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;
}

View file

@ -1094,11 +1094,11 @@ third_party/libcxx/fs/filesystem_clock.cpp \
third_party/libcxx/fs/filesystem_error.cpp \ third_party/libcxx/fs/filesystem_error.cpp \
third_party/libcxx/fs/int128_builtins.cpp \ third_party/libcxx/fs/int128_builtins.cpp \
third_party/libcxx/fs/operations.cpp \ third_party/libcxx/fs/operations.cpp \
third_party/libcxx/fs/cosmo.cpp \
third_party/libcxx/fs/path.cpp \ third_party/libcxx/fs/path.cpp \
third_party/libcxx/ryu/d2fixed.cpp \ third_party/libcxx/ryu/d2fixed.cpp \
third_party/libcxx/ryu/d2s.cpp \ third_party/libcxx/ryu/d2s.cpp \
third_party/libcxx/ryu/f2s.cpp \ third_party/libcxx/ryu/f2s.cpp \
third_party/libcxx/errc.cpp \
THIRD_PARTY_LIBCXX_A_HDRS_CHECKEM = \ THIRD_PARTY_LIBCXX_A_HDRS_CHECKEM = \
third_party/libcxx/__assertion_handler \ third_party/libcxx/__assertion_handler \

View file

@ -141,6 +141,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
// This leads to the odd pushing and popping of the deprecated // This leads to the odd pushing and popping of the deprecated
// diagnostic. // diagnostic.
_LIBCPP_DECLARE_STRONG_ENUM(errc){ _LIBCPP_DECLARE_STRONG_ENUM(errc){
success = 0,
address_family_not_supported = 65536, // = EAFNOSUPPORT, address_family_not_supported = 65536, // = EAFNOSUPPORT,
address_in_use, // = EADDRINUSE, address_in_use, // = EADDRINUSE,
address_not_available, // = EADDRNOTAVAIL, address_not_available, // = EADDRNOTAVAIL,
@ -221,6 +222,9 @@ _LIBCPP_DECLARE_STRONG_ENUM(errc){
wrong_protocol_type}; wrong_protocol_type};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc) _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc)
errc __err_to_errc(int) noexcept;
int __errc_to_err(errc) noexcept;
_LIBCPP_END_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ERRC #endif // _LIBCPP___ERRC

View file

@ -46,8 +46,9 @@ class _LIBCPP_EXPORTED_FROM_ABI error_code {
public: public:
_LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {} _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
_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_(__val), __cat_(&__cat) {} _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 <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0> template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT { _LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT {
@ -72,7 +73,7 @@ public:
__cat_ = &system_category(); __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_; } _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
@ -86,13 +87,17 @@ public:
}; };
inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT { inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT {
return error_code(static_cast<int>(__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 { 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(); 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 { 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()); return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value());
} }

View file

@ -54,8 +54,12 @@ class _LIBCPP_EXPORTED_FROM_ABI error_condition {
public: public:
_LIBCPP_HIDE_FROM_ABI error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {} _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 _LIBCPP_HIDE_FROM_ABI error_condition(int __val, const error_category& __cat) _NOEXCEPT
: __val_(__val), : __val_(__errc_to_err((errc)__val)),
__cat_(&__cat) {} __cat_(&__cat) {}
template <class _Ep, __enable_if_t<is_error_condition_enum<_Ep>::value, int> = 0> template <class _Ep, __enable_if_t<is_error_condition_enum<_Ep>::value, int> = 0>
@ -81,7 +85,7 @@ public:
__cat_ = &generic_category(); __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_; } _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
string message() const; string message() const;
@ -90,7 +94,7 @@ public:
}; };
inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(errc __e) _NOEXCEPT { inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(errc __e) _NOEXCEPT {
return error_condition(static_cast<int>(__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 { inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT {

View file

@ -11,6 +11,7 @@
#define _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H #define _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H
#include <__config> #include <__config>
#include <__system_error/errc.h>
#include <__system_error/error_category.h> #include <__system_error/error_category.h>
#include <__system_error/error_code.h> #include <__system_error/error_code.h>
#include <__verbose_abort> #include <__verbose_abort>

View file

@ -1,11 +1,8 @@
#ifdef __COSMOPOLITAN__ #include <__system_error/errc.h>
#include <filesystem>
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM _LIBCPP_BEGIN_NAMESPACE_STD
namespace detail { static std::errc __err_to_errc_impl(int err) noexcept {
std::errc __cosmo_err_to_errc_impl(int err) {
if (err == EAFNOSUPPORT) return errc::address_family_not_supported; if (err == EAFNOSUPPORT) return errc::address_family_not_supported;
if (err == EADDRINUSE) return errc::address_in_use; if (err == EADDRINUSE) return errc::address_in_use;
if (err == EADDRNOTAVAIL) return errc::address_not_available; 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; 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_family_not_supported) return EAFNOSUPPORT;
if (err == errc::address_in_use) return EADDRINUSE; if (err == errc::address_in_use) return EADDRINUSE;
if (err == errc::address_not_available) return EADDRNOTAVAIL; if (err == errc::address_not_available) return EADDRNOTAVAIL;
@ -169,20 +166,20 @@ int __cosmo_errc_to_err_impl(std::errc err) {
return ENOTSUP; 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) if (err >= 65536)
return (std::errc)err; 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) if ((int)err < 65536)
return (int)err; return (int)err;
return __cosmo_errc_to_err_impl(err); return __errc_to_err_impl(err);
} }
} // end namespace detail _LIBCPP_END_NAMESPACE_STD
_LIBCPP_END_NAMESPACE_FILESYSTEM
#endif // __COSMOPOLITAN__

View file

@ -118,11 +118,7 @@ public:
if ((__stream_ = ::opendir(root.c_str())) == nullptr) { if ((__stream_ = ::opendir(root.c_str())) == nullptr) {
ec = detail::capture_errno(); ec = detail::capture_errno();
const bool allow_eacces = bool(opts & directory_options::skip_permission_denied); const bool allow_eacces = bool(opts & directory_options::skip_permission_denied);
#ifdef __COSMOPOLITAN__
if (allow_eacces && ec.value() == (int)errc::permission_denied) if (allow_eacces && ec.value() == (int)errc::permission_denied)
#else
if (allow_eacces && ec.value() == EACCES)
#endif
ec.clear(); ec.clear();
return; return;
} }
@ -311,11 +307,7 @@ bool recursive_directory_iterator::__try_recursion(error_code* ec) {
} }
if (m_ec) { if (m_ec) {
const bool allow_eacess = bool(__imp_->__options_ & directory_options::skip_permission_denied); const bool allow_eacess = bool(__imp_->__options_ & directory_options::skip_permission_denied);
#ifdef __COSMOPOLITAN__
if (m_ec.value() == (int)errc::permission_denied && allow_eacess) { if (m_ec.value() == (int)errc::permission_denied && allow_eacess) {
#else
if (m_ec.value() == EACCES && allow_eacess) {
#endif
if (ec) if (ec)
ec->clear(); ec->clear();
} else { } else {

View file

@ -98,16 +98,9 @@ inline errc __win_err_to_errc(int err) {
#endif // _LIBCPP_WIN32API #endif // _LIBCPP_WIN32API
errc __cosmo_err_to_errc(int);
int __cosmo_errc_to_err(errc);
inline error_code capture_errno() { inline error_code capture_errno() {
_LIBCPP_ASSERT_INTERNAL(errno != 0, "Expected errno to be non-zero"); _LIBCPP_ASSERT_INTERNAL(errno != 0, "Expected errno to be non-zero");
#ifdef __COSMOPOLITAN__ return error_code((int)__err_to_errc(errno), generic_category());
return error_code((int)__cosmo_err_to_errc(errno), generic_category());
#else
return error_code(errno, generic_category());
#endif
} }
#if defined(_LIBCPP_WIN32API) #if defined(_LIBCPP_WIN32API)

View file

@ -194,12 +194,8 @@ inline perms posix_get_perms(const StatT& st) noexcept { return static_cast<perm
inline file_status create_file_status(error_code& m_ec, path const& p, const StatT& path_stat, error_code* ec) { inline file_status create_file_status(error_code& m_ec, path const& p, const StatT& path_stat, error_code* ec) {
if (ec) if (ec)
*ec = m_ec; *ec = m_ec;
#ifdef __COSMOPOLITAN__
if (m_ec && (m_ec.value() == (int)errc::no_such_file_or_directory || if (m_ec && (m_ec.value() == (int)errc::no_such_file_or_directory ||
m_ec.value() == (int)errc::not_a_directory)) { m_ec.value() == (int)errc::not_a_directory)) {
#else
if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
#endif
return file_status(file_type::not_found); return file_status(file_type::not_found);
} else if (m_ec) { } else if (m_ec) {
ErrorHandler<void> err("posix_stat", ec, &p); ErrorHandler<void> err("posix_stat", ec, &p);

View file

@ -13,6 +13,7 @@
/* /*
ios synopsis ios synopsis
#include "third_party/libcxx/__system_error/error_code.h"
#include <iosfwd> #include <iosfwd>
namespace std namespace std

View file

@ -146,8 +146,8 @@ template <> struct hash<std::error_condition>;
#include <__config> #include <__config>
#include <__system_error/errc.h> #include <__system_error/errc.h>
#include <__system_error/error_category.h>
#include <__system_error/error_code.h> #include <__system_error/error_code.h>
#include <__system_error/error_category.h>
#include <__system_error/error_condition.h> #include <__system_error/error_condition.h>
#include <__system_error/system_error.h> #include <__system_error/system_error.h>
#include <version> #include <version>

View file

@ -16,6 +16,7 @@
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <system_error> #include <system_error>
#include <__system_error/errc.h>
#include "config_elast.h" #include "config_elast.h"
@ -23,10 +24,6 @@
# include <android/api-level.h> # include <android/api-level.h>
#endif #endif
#ifdef __COSMOPOLITAN__
#include <fs/error.h>
#endif
_LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_NAMESPACE_STD
namespace { namespace {
@ -39,9 +36,7 @@ string do_strerror_r(int ev);
# if defined(_LIBCPP_MSVCRT_LIKE) # if defined(_LIBCPP_MSVCRT_LIKE)
string do_strerror_r(int ev) { string do_strerror_r(int ev) {
#ifdef __COSMOPOLITAN__ ev = __errc_to_err(ev);
ev = (int)filesystem::detail::__cosmo_errc_to_err(ev);
#endif
char buffer[strerror_buff_size]; char buffer[strerror_buff_size];
if (::strerror_s(buffer, strerror_buff_size, ev) == 0) if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
return string(buffer); return string(buffer);
@ -88,9 +83,7 @@ string do_strerror_r(int ev) {
// Preserve errno around the call. (The C++ standard requires that // Preserve errno around the call. (The C++ standard requires that
// system_error functions not modify errno). // system_error functions not modify errno).
const int old_errno = errno; const int old_errno = errno;
#ifdef __COSMOPOLITAN__ ev = __errc_to_err((errc)ev);
ev = filesystem::detail::__cosmo_errc_to_err((errc)ev);
#endif
const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer); 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 we didn't get any message, print one now.
if (!error_message[0]) { if (!error_message[0]) {
@ -139,9 +132,7 @@ public:
const char* __generic_error_category::name() const noexcept { return "generic"; } const char* __generic_error_category::name() const noexcept { return "generic"; }
string __generic_error_category::message(int ev) const { string __generic_error_category::message(int ev) const {
#ifdef __COSMOPOLITAN__ ev = __errc_to_err((errc)ev);
ev = filesystem::detail::__cosmo_errc_to_err((errc)ev);
#endif
#ifdef _LIBCPP_ELAST #ifdef _LIBCPP_ELAST
if (ev > _LIBCPP_ELAST) if (ev > _LIBCPP_ELAST)
return string("unspecified generic_category error"); return string("unspecified generic_category error");
@ -169,9 +160,7 @@ public:
const char* __system_error_category::name() const noexcept { return "system"; } const char* __system_error_category::name() const noexcept { return "system"; }
string __system_error_category::message(int ev) const { string __system_error_category::message(int ev) const {
#ifdef __COSMOPOLITAN__ ev = __errc_to_err((errc)ev);
ev = filesystem::detail::__cosmo_errc_to_err((errc)ev);
#endif
#ifdef _LIBCPP_ELAST #ifdef _LIBCPP_ELAST
if (ev > _LIBCPP_ELAST) if (ev > _LIBCPP_ELAST)
return string("unspecified system_category error"); 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 { error_condition __system_error_category::default_error_condition(int ev) const noexcept {
#ifdef __COSMOPOLITAN__ ev = __errc_to_err((errc)ev);
ev = filesystem::detail::__cosmo_errc_to_err((errc)ev);
#endif
#ifdef _LIBCPP_ELAST #ifdef _LIBCPP_ELAST
if (ev > _LIBCPP_ELAST) if (ev > _LIBCPP_ELAST)
return error_condition(ev, system_category()); 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) { void __throw_system_error(int ev, const char* what_arg) {
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
#ifdef __COSMOPOLITAN__ std::__throw_system_error(error_code((int)__err_to_errc(ev), system_category()), what_arg);
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
#else #else
// The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily. // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.
_LIBCPP_VERBOSE_ABORT( _LIBCPP_VERBOSE_ABORT(