diff options
author | Andreas Kling <kling@serenityos.org> | 2020-08-04 18:02:23 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-04 18:17:16 +0200 |
commit | 7a3ab6c517649e9dfdd50085f10492212b0bcb18 (patch) | |
tree | f251cfc69ebe5f253c1e900c10b9485136b315cd /Kernel/FileSystem/FileDescription.cpp | |
parent | 58feebeed28bab04e7b462f7cd3beb81984f116e (diff) | |
download | serenity-7a3ab6c517649e9dfdd50085f10492212b0bcb18.zip |
Kernel: Make File::write() and File::read() return KResultOr<size_t>
Instead of returning a ssize_t where negative values mean error,
we now return KResultOr<size_t> and use the error state to report
errors exclusively.
Diffstat (limited to 'Kernel/FileSystem/FileDescription.cpp')
-rw-r--r-- | Kernel/FileSystem/FileDescription.cpp | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index c4c3f8f700..43750a074c 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -124,28 +124,32 @@ off_t FileDescription::seek(off_t offset, int whence) return m_current_offset; } -ssize_t FileDescription::read(u8* buffer, ssize_t count) +KResultOr<size_t> FileDescription::read(u8* buffer, size_t count) { LOCKER(m_lock); - if ((m_current_offset + count) < 0) + Checked<size_t> new_offset = m_current_offset; + new_offset += count; + if (new_offset.has_overflow()) return -EOVERFLOW; SmapDisabler disabler; - int nread = m_file->read(*this, offset(), buffer, count); - if (nread > 0 && m_file->is_seekable()) - m_current_offset += nread; - return nread; + auto nread_or_error = m_file->read(*this, offset(), buffer, count); + if (!nread_or_error.is_error() && m_file->is_seekable()) + m_current_offset += nread_or_error.value(); + return nread_or_error; } -ssize_t FileDescription::write(const u8* data, ssize_t size) +KResultOr<size_t> FileDescription::write(const u8* data, size_t size) { LOCKER(m_lock); - if ((m_current_offset + size) < 0) + Checked<size_t> new_offset = m_current_offset; + new_offset += size; + if (new_offset.has_overflow()) return -EOVERFLOW; SmapDisabler disabler; - int nwritten = m_file->write(*this, offset(), data, size); - if (nwritten > 0 && m_file->is_seekable()) - m_current_offset += nwritten; - return nwritten; + auto nwritten_or_error = m_file->write(*this, offset(), data, size); + if (!nwritten_or_error.is_error() && m_file->is_seekable()) + m_current_offset += nwritten_or_error.value(); + return nwritten_or_error; } bool FileDescription::can_write() const |