diff --git a/AK/Error.h b/AK/Error.h index ad21cf60759..3fcbab99b9a 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -31,7 +31,12 @@ public: // For calling this method from userspace programs, we will simply return from // the Error::from_string_view method! [[nodiscard]] static Error from_string_view_or_print_error_and_return_errno(StringView string_literal, int code); - [[nodiscard]] static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); } + +#ifndef KERNEL + [[nodiscard]] static Error from_syscall(StringView syscall_name, int rc) + { + return Error(syscall_name, rc); + } [[nodiscard]] static Error from_string_view(StringView string_literal) { return Error(string_literal); } [[nodiscard]] static Error copy(Error const& error) @@ -57,17 +62,29 @@ public: { return from_string_view(string); } +#endif bool operator==(Error const& other) const { +#ifdef KERNEL + return m_code == other.m_code; +#else return m_code == other.m_code && m_string_literal == other.m_string_literal && m_syscall == other.m_syscall; +#endif } - bool is_errno() const { return m_code != 0; } - bool is_syscall() const { return m_syscall; } - int code() const { return m_code; } - StringView string_literal() const { return m_string_literal; } +#ifndef KERNEL + bool is_errno() const + { + return m_code != 0; + } + bool is_syscall() const { return m_syscall; } + StringView string_literal() const + { + return m_string_literal; + } +#endif protected: Error(int code) @@ -76,6 +93,7 @@ protected: } private: +#ifndef KERNEL Error(StringView string_literal) : m_string_literal(string_literal) { @@ -92,8 +110,13 @@ private: Error& operator=(Error const&) = default; StringView m_string_literal; +#endif + int m_code { 0 }; + +#ifndef KERNEL bool m_syscall { false }; +#endif }; template diff --git a/AK/Format.h b/AK/Format.h index c86412c556a..3ccec0eee73 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -684,9 +684,7 @@ struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Error const& error) { #if defined(AK_OS_SERENITY) && defined(KERNEL) - if (error.is_errno()) - return Formatter::format(builder, "Error(errno={})"sv, error.code()); - return Formatter::format(builder, "Error({})"sv, error.string_literal()); + return Formatter::format(builder, "Error(errno={})"sv, error.code()); #else if (error.is_syscall()) return Formatter::format(builder, "{}: {} (errno={})"sv, error.string_literal(), strerror(error.code()), error.code()); diff --git a/AK/Hex.cpp b/AK/Hex.cpp index d046aaceaae..8e09729fbbb 100644 --- a/AK/Hex.cpp +++ b/AK/Hex.cpp @@ -15,18 +15,18 @@ namespace AK { ErrorOr decode_hex(StringView input) { if ((input.length() % 2) != 0) - return Error::from_string_literal("Hex string was not an even length"); + return Error::from_string_view_or_print_error_and_return_errno("Hex string was not an even length"sv, EINVAL); auto output = TRY(ByteBuffer::create_zeroed(input.length() / 2)); for (size_t i = 0; i < input.length() / 2; ++i) { auto const c1 = decode_hex_digit(input[i * 2]); if (c1 >= 16) - return Error::from_string_literal("Hex string contains invalid digit"); + return Error::from_string_view_or_print_error_and_return_errno("Hex string contains invalid digit"sv, EINVAL); auto const c2 = decode_hex_digit(input[i * 2 + 1]); if (c2 >= 16) - return Error::from_string_literal("Hex string contains invalid digit"); + return Error::from_string_view_or_print_error_and_return_errno("Hex string contains invalid digit"sv, EINVAL); output[i] = (c1 << 4) + c2; } diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index 10c35f0bf01..237fd345bfe 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -59,19 +59,19 @@ ErrorOr FixedMemoryStream::seek(i64 offset, SeekMode seek_mode) switch (seek_mode) { case SeekMode::SetPosition: if (offset > static_cast(m_bytes.size())) - return Error::from_string_literal("Offset past the end of the stream memory"); + return Error::from_string_view_or_print_error_and_return_errno("Offset past the end of the stream memory"sv, EINVAL); m_offset = offset; break; case SeekMode::FromCurrentPosition: if (offset + static_cast(m_offset) > static_cast(m_bytes.size())) - return Error::from_string_literal("Offset past the end of the stream memory"); + return Error::from_string_view_or_print_error_and_return_errno("Offset past the end of the stream memory"sv, EINVAL); m_offset += offset; break; case SeekMode::FromEndPosition: if (offset > static_cast(m_bytes.size())) - return Error::from_string_literal("Offset past the start of the stream memory"); + return Error::from_string_view_or_print_error_and_return_errno("Offset past the start of the stream memory"sv, EINVAL); m_offset = m_bytes.size() - offset; break; @@ -92,7 +92,7 @@ ErrorOr FixedMemoryStream::write(ReadonlyBytes bytes) ErrorOr FixedMemoryStream::write_entire_buffer(ReadonlyBytes bytes) { if (remaining() < bytes.size()) - return Error::from_string_literal("Write of entire buffer ends past the memory area"); + return Error::from_string_view_or_print_error_and_return_errno("Write of entire buffer ends past the memory area"sv, EINVAL); TRY(write(bytes)); return {}; @@ -163,7 +163,7 @@ ErrorOr AllocatingMemoryStream::discard(size_t count) VERIFY(m_write_offset >= m_read_offset); if (count > used_buffer_size()) - return Error::from_string_literal("Number of discarded bytes is higher than the number of allocated bytes"); + return Error::from_string_view_or_print_error_and_return_errno("Number of discarded bytes is higher than the number of allocated bytes"sv, EINVAL); m_read_offset += count; diff --git a/AK/Stream.cpp b/AK/Stream.cpp index 19b40e0c2c2..2b0ae90da2c 100644 --- a/AK/Stream.cpp +++ b/AK/Stream.cpp @@ -16,14 +16,19 @@ ErrorOr Stream::read_entire_buffer(Bytes buffer) size_t nread = 0; while (nread < buffer.size()) { if (is_eof()) - return Error::from_string_literal("Reached end-of-file before filling the entire buffer"); + return Error::from_string_view_or_print_error_and_return_errno("Reached end-of-file before filling the entire buffer"sv, EIO); auto result = read(buffer.slice(nread)); if (result.is_error()) { +#ifdef KERNEL + if (result.error().code() == EINTR) { + continue; + } +#else if (result.error().is_errno() && result.error().code() == EINTR) { continue; } - +#endif return result.release_error(); } @@ -69,7 +74,7 @@ ErrorOr Stream::discard(size_t discarded_bytes) while (discarded_bytes > 0) { if (is_eof()) - return Error::from_string_literal("Reached end-of-file before reading all discarded bytes"); + return Error::from_string_view_or_print_error_and_return_errno("Reached end-of-file before reading all discarded bytes"sv, EIO); auto slice = TRY(read(buffer.span().slice(0, min(discarded_bytes, continuous_read_size)))); discarded_bytes -= slice.size(); @@ -84,10 +89,15 @@ ErrorOr Stream::write_entire_buffer(ReadonlyBytes buffer) while (nwritten < buffer.size()) { auto result = write(buffer.slice(nwritten)); if (result.is_error()) { +#ifdef KERNEL + if (result.error().code() == EINTR) { + continue; + } +#else if (result.error().is_errno() && result.error().code() == EINTR) { continue; } - +#endif return result.release_error(); }