AK: Make SeekableStream::truncate() take a size_t

Similar to the return values earlier, a signed value doesn't really make
sense here. Relying on the much more standard `size_t` makes it easier
to use Stream in all contexts.
This commit is contained in:
Tim Schumacher 2023-01-29 13:49:42 +01:00 committed by Andrew Kaster
parent a85c18d3c4
commit 371c51f934
Notes: sideshowbarker 2024-07-17 05:02:35 +09:00
6 changed files with 9 additions and 6 deletions

View File

@ -295,7 +295,7 @@ public:
return result;
}
virtual ErrorOr<void> truncate(off_t length) override
virtual ErrorOr<void> truncate(size_t length) override
{
return m_helper.stream().truncate(length);
}

View File

@ -48,7 +48,7 @@ void FixedMemoryStream::close()
// FIXME: It doesn't make sense to close a memory stream. Therefore, we don't do anything here. Is that fine?
}
ErrorOr<void> FixedMemoryStream::truncate(off_t)
ErrorOr<void> FixedMemoryStream::truncate(size_t)
{
return Error::from_errno(EBADF);
}

View File

@ -22,7 +22,7 @@ public:
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
virtual ErrorOr<void> truncate(off_t) override;
virtual ErrorOr<void> truncate(size_t) override;
virtual ErrorOr<Bytes> read(Bytes bytes) override;
virtual ErrorOr<size_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;

View File

@ -120,7 +120,7 @@ public:
virtual ErrorOr<size_t> size();
/// Shrinks or extends the stream to the given size. Returns an errno in
/// the case of an error.
virtual ErrorOr<void> truncate(off_t length) = 0;
virtual ErrorOr<void> truncate(size_t length) = 0;
/// Seeks until after the given amount of bytes to be discarded instead of
/// reading and discarding everything manually;
virtual ErrorOr<void> discard(size_t discarded_bytes) override;

View File

@ -184,8 +184,11 @@ ErrorOr<size_t> File::seek(i64 offset, SeekMode mode)
return seek_result;
}
ErrorOr<void> File::truncate(off_t length)
ErrorOr<void> File::truncate(size_t length)
{
if (length > static_cast<size_t>(NumericLimits<off_t>::max()))
return Error::from_string_literal("Length is larger than the maximum supported length");
return System::ftruncate(m_fd, length);
}

View File

@ -169,7 +169,7 @@ public:
virtual bool is_open() const override;
virtual void close() override;
virtual ErrorOr<size_t> seek(i64 offset, SeekMode) override;
virtual ErrorOr<void> truncate(off_t length) override;
virtual ErrorOr<void> truncate(size_t length) override;
int leak_fd(Badge<::IPC::File>)
{