aboutsummaryrefslogtreecommitdiff
path: root/dep/fmt/src/os.cc
diff options
context:
space:
mode:
Diffstat (limited to 'dep/fmt/src/os.cc')
-rw-r--r--dep/fmt/src/os.cc153
1 files changed, 96 insertions, 57 deletions
diff --git a/dep/fmt/src/os.cc b/dep/fmt/src/os.cc
index 6850024588f..f388ead0191 100644
--- a/dep/fmt/src/os.cc
+++ b/dep/fmt/src/os.cc
@@ -25,21 +25,24 @@
# define WIN32_LEAN_AND_MEAN
# endif
# include <io.h>
-# include <windows.h>
-
-# define O_CREAT _O_CREAT
-# define O_TRUNC _O_TRUNC
# ifndef S_IRUSR
# define S_IRUSR _S_IREAD
# endif
-
# ifndef S_IWUSR
# define S_IWUSR _S_IWRITE
# endif
-
-# ifdef __MINGW32__
-# define _SH_DENYNO 0x40
+# ifndef S_IRGRP
+# define S_IRGRP 0
+# endif
+# ifndef S_IWGRP
+# define S_IWGRP 0
+# endif
+# ifndef S_IROTH
+# define S_IROTH 0
+# endif
+# ifndef S_IWOTH
+# define S_IWOTH 0
# endif
# endif // _WIN32
#endif // FMT_USE_FCNTL
@@ -48,14 +51,10 @@
# include <windows.h>
#endif
-#ifdef fileno
-# undef fileno
-#endif
-
namespace {
#ifdef _WIN32
// Return type of read and write functions.
-using RWResult = int;
+using rwresult = int;
// On Windows the count argument to read and write is unsigned, so convert
// it from size_t preventing integer overflow.
@@ -64,7 +63,7 @@ inline unsigned convert_rwcount(std::size_t count) {
}
#elif FMT_USE_FCNTL
// Return type of read and write functions.
-using RWResult = ssize_t;
+using rwresult = ssize_t;
inline std::size_t convert_rwcount(std::size_t count) { return count; }
#endif
@@ -73,14 +72,14 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; }
FMT_BEGIN_NAMESPACE
#ifdef _WIN32
-detail::utf16_to_utf8::utf16_to_utf8(wstring_view s) {
+detail::utf16_to_utf8::utf16_to_utf8(basic_string_view<wchar_t> s) {
if (int error_code = convert(s)) {
FMT_THROW(windows_error(error_code,
"cannot convert string from UTF-16 to UTF-8"));
}
}
-int detail::utf16_to_utf8::convert(wstring_view s) {
+int detail::utf16_to_utf8::convert(basic_string_view<wchar_t> s) {
if (s.size() > INT_MAX) return ERROR_INVALID_PARAMETER;
int s_size = static_cast<int>(s.size());
if (s_size == 0) {
@@ -101,51 +100,90 @@ int detail::utf16_to_utf8::convert(wstring_view s) {
return 0;
}
-void windows_error::init(int err_code, string_view format_str,
- format_args args) {
- error_code_ = err_code;
- memory_buffer buffer;
- detail::format_windows_error(buffer, err_code, vformat(format_str, args));
- std::runtime_error& base = *this;
- base = std::runtime_error(to_string(buffer));
+namespace detail {
+
+class system_message {
+ system_message(const system_message&) = delete;
+ void operator=(const system_message&) = delete;
+
+ unsigned long result_;
+ wchar_t* message_;
+
+ static bool is_whitespace(wchar_t c) noexcept {
+ return c == L' ' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\0';
+ }
+
+ public:
+ explicit system_message(unsigned long error_code)
+ : result_(0), message_(nullptr) {
+ result_ = FormatMessageW(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ reinterpret_cast<wchar_t*>(&message_), 0, nullptr);
+ if (result_ != 0) {
+ while (result_ != 0 && is_whitespace(message_[result_ - 1])) {
+ --result_;
+ }
+ }
+ }
+ ~system_message() { LocalFree(message_); }
+ explicit operator bool() const noexcept { return result_ != 0; }
+ operator basic_string_view<wchar_t>() const noexcept {
+ return basic_string_view<wchar_t>(message_, result_);
+ }
+};
+
+class utf8_system_category final : public std::error_category {
+ public:
+ const char* name() const noexcept override { return "system"; }
+ std::string message(int error_code) const override {
+ system_message msg(error_code);
+ if (msg) {
+ utf16_to_utf8 utf8_message;
+ if (utf8_message.convert(msg) == ERROR_SUCCESS) {
+ return utf8_message.str();
+ }
+ }
+ return "unknown error";
+ }
+};
+
+} // namespace detail
+
+FMT_API const std::error_category& system_category() noexcept {
+ static const detail::utf8_system_category category;
+ return category;
+}
+
+std::system_error vwindows_error(int err_code, string_view format_str,
+ format_args args) {
+ auto ec = std::error_code(err_code, system_category());
+ return std::system_error(ec, vformat(format_str, args));
}
void detail::format_windows_error(detail::buffer<char>& out, int error_code,
- string_view message) FMT_NOEXCEPT {
+ const char* message) noexcept {
FMT_TRY {
- wmemory_buffer buf;
- buf.resize(inline_buffer_size);
- for (;;) {
- wchar_t* system_message = &buf[0];
- int result = FormatMessageW(
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
- error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message,
- static_cast<uint32_t>(buf.size()), nullptr);
- if (result != 0) {
- utf16_to_utf8 utf8_message;
- if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
- format_to(buffer_appender<char>(out), "{}: {}", message,
- utf8_message);
- return;
- }
- break;
+ system_message msg(error_code);
+ if (msg) {
+ utf16_to_utf8 utf8_message;
+ if (utf8_message.convert(msg) == ERROR_SUCCESS) {
+ fmt::format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message);
+ return;
}
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
- break; // Can't get error message, report error code instead.
- buf.resize(buf.size() * 2);
}
}
FMT_CATCH(...) {}
format_error_code(out, error_code, message);
}
-void report_windows_error(int error_code,
- fmt::string_view message) FMT_NOEXCEPT {
+void report_windows_error(int error_code, const char* message) noexcept {
report_error(detail::format_windows_error, error_code, message);
}
#endif // _WIN32
-buffered_file::~buffered_file() FMT_NOEXCEPT {
+buffered_file::~buffered_file() noexcept {
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
report_system_error(errno, "cannot close file");
}
@@ -164,18 +202,19 @@ void buffered_file::close() {
if (result != 0) FMT_THROW(system_error(errno, "cannot close file"));
}
-// A macro used to prevent expansion of fileno on broken versions of MinGW.
-#define FMT_ARGS
-
-int buffered_file::fileno() const {
- int fd = FMT_POSIX_CALL(fileno FMT_ARGS(file_));
+int buffered_file::descriptor() const {
+ int fd = FMT_POSIX_CALL(fileno(file_));
if (fd == -1) FMT_THROW(system_error(errno, "cannot get file descriptor"));
return fd;
}
#if FMT_USE_FCNTL
file::file(cstring_view path, int oflag) {
- int mode = S_IRUSR | S_IWUSR;
+# ifdef _WIN32
+ using mode_t = int;
+# endif
+ constexpr mode_t mode =
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
# if defined(_WIN32) && !defined(__MINGW32__)
fd_ = -1;
FMT_POSIX_CALL(sopen_s(&fd_, path.c_str(), oflag, _SH_DENYNO, mode));
@@ -186,7 +225,7 @@ file::file(cstring_view path, int oflag) {
FMT_THROW(system_error(errno, "cannot open file {}", path.c_str()));
}
-file::~file() FMT_NOEXCEPT {
+file::~file() noexcept {
// Don't retry close in case of EINTR!
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
@@ -229,14 +268,14 @@ long long file::size() const {
}
std::size_t file::read(void* buffer, std::size_t count) {
- RWResult result = 0;
+ rwresult result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
if (result < 0) FMT_THROW(system_error(errno, "cannot read from file"));
return detail::to_unsigned(result);
}
std::size_t file::write(const void* buffer, std::size_t count) {
- RWResult result = 0;
+ rwresult result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
if (result < 0) FMT_THROW(system_error(errno, "cannot write to file"));
return detail::to_unsigned(result);
@@ -260,10 +299,10 @@ void file::dup2(int fd) {
}
}
-void file::dup2(int fd, error_code& ec) FMT_NOEXCEPT {
+void file::dup2(int fd, std::error_code& ec) noexcept {
int result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd)));
- if (result == -1) ec = error_code(errno);
+ if (result == -1) ec = std::error_code(errno, std::generic_category());
}
void file::pipe(file& read_end, file& write_end) {