From 5f645e84d89a727eb9205a4e1c325e7cdc910421 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Wed, 29 Dec 2021 23:13:11 +0000 Subject: [PATCH] LibCore: Use Error::from_errno in Stream APIs This makes Stream APIs work with Lagom and is overall cleaner. --- Userland/Libraries/LibCore/Stream.cpp | 10 +++++----- Userland/Libraries/LibCore/Stream.h | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 0f1f3b5371..303c0e5a8f 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -164,7 +164,7 @@ ErrorOr File::read(Bytes buffer) // NOTE: POSIX says that if the fd is not open for reading, the call // will return EBADF. Since we already know whether we can or // can't read the file, let's avoid a syscall. - return EBADF; + return Error::from_errno(EBADF); } ssize_t rc = ::read(m_fd, buffer.data(), buffer.size()); @@ -180,7 +180,7 @@ ErrorOr File::write(ReadonlyBytes buffer) { if (!has_flag(m_mode, OpenMode::Write)) { // NOTE: Same deal as Read. - return EBADF; + return Error::from_errno(EBADF); } ssize_t rc = ::write(m_fd, buffer.data(), buffer.size()); @@ -343,7 +343,7 @@ ErrorOr Socket::connect_inet(int fd, SocketAddress const& address) ErrorOr PosixSocketHelper::read(Bytes buffer) { if (!is_open()) { - return ENOTCONN; + return Error::from_errno(ENOTCONN); } ssize_t rc = ::recv(m_fd, buffer.data(), buffer.size(), 0); @@ -364,7 +364,7 @@ ErrorOr PosixSocketHelper::read(Bytes buffer) ErrorOr PosixSocketHelper::write(ReadonlyBytes buffer) { if (!is_open()) { - return ENOTCONN; + return Error::from_errno(ENOTCONN); } ssize_t rc = ::send(m_fd, buffer.data(), buffer.size(), 0); @@ -483,7 +483,7 @@ ErrorOr> TCPSocket::adopt_fd(int fd) ErrorOr PosixSocketHelper::pending_bytes() const { if (!is_open()) { - return ENOTCONN; + return Error::from_errno(ENOTCONN); } int value; diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index af409537b1..d940760da6 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -340,7 +340,7 @@ public: // datagram to be discarded. That's not very nice, so let's bail // early, telling the caller that he should allocate a bigger // buffer. - return EMSGSIZE; + return Error::from_errno(EMSGSIZE); } return m_helper.read(buffer); @@ -469,13 +469,13 @@ public: static ErrorOr>> create_buffered(NonnullOwnPtr stream, size_t buffer_size) { if (!buffer_size) - return EINVAL; + return Error::from_errno(EINVAL); if (!stream->is_open()) - return ENOTCONN; + return Error::from_errno(ENOTCONN); auto maybe_buffer = ByteBuffer::create_uninitialized(buffer_size); if (!maybe_buffer.has_value()) - return ENOMEM; + return Error::from_errno(ENOMEM); return adopt_nonnull_own_or_enomem(new BufferedType(move(stream), maybe_buffer.release_value())); } @@ -486,9 +486,9 @@ public: ErrorOr read(Bytes buffer) { if (!stream().is_open()) - return ENOTCONN; + return Error::from_errno(ENOTCONN); if (!buffer.size()) - return ENOBUFS; + return Error::from_errno(ENOBUFS); // Let's try to take all we can from the buffer first. size_t buffer_nread = 0; @@ -536,10 +536,10 @@ public: ErrorOr read_until_any_of(Bytes buffer, Array candidates) { if (!stream().is_open()) - return ENOTCONN; + return Error::from_errno(ENOTCONN); if (buffer.is_empty()) - return ENOBUFS; + return Error::from_errno(ENOBUFS); // We fill the buffer through can_read_line. if (!TRY(can_read_line())) @@ -557,7 +557,7 @@ public: // violating the invariant twice the next time the user attempts // to read, which is No Good. So let's give a descriptive error // to the caller about why it can't read. - return EMSGSIZE; + return Error::from_errno(EMSGSIZE); } m_buffer.span().slice(0, m_buffered_size).copy_to(buffer);