mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:07:35 +00:00
LibCore+Tests: Add SeekableStream::truncate()
This commit is contained in:
parent
d9fb1b8c2e
commit
4d5080388a
4 changed files with 27 additions and 0 deletions
|
@ -135,6 +135,18 @@ TEST_CASE(file_adopt_invalid_fd)
|
||||||
EXPECT_EQ(maybe_file.error().code(), EBADF);
|
EXPECT_EQ(maybe_file.error().code(), EBADF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(file_truncate)
|
||||||
|
{
|
||||||
|
auto maybe_file = Core::Stream::File::open("/tmp/file-truncate-test.txt", Core::Stream::OpenMode::Write);
|
||||||
|
auto file = maybe_file.release_value();
|
||||||
|
|
||||||
|
EXPECT(!file->truncate(999).is_error());
|
||||||
|
EXPECT_EQ(file->size().release_value(), 999);
|
||||||
|
|
||||||
|
EXPECT(!file->truncate(42).is_error());
|
||||||
|
EXPECT_EQ(file->size().release_value(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
// TCPSocket tests
|
// TCPSocket tests
|
||||||
|
|
||||||
TEST_CASE(should_error_when_connection_fails)
|
TEST_CASE(should_error_when_connection_fails)
|
||||||
|
|
|
@ -26,6 +26,8 @@ public:
|
||||||
virtual bool is_open() const override { return true; }
|
virtual bool is_open() const override { return true; }
|
||||||
// FIXME: It doesn't make sense to close an memory stream. Therefore, we don't do anything here. Is that fine?
|
// FIXME: It doesn't make sense to close an memory stream. Therefore, we don't do anything here. Is that fine?
|
||||||
virtual void close() override { }
|
virtual void close() override { }
|
||||||
|
// FIXME: It doesn't make sense to truncate a memory stream. Therefore, we don't do anything here. Is that fine?
|
||||||
|
virtual ErrorOr<void> truncate(off_t) override { return Error::from_errno(ENOTSUP); }
|
||||||
|
|
||||||
virtual ErrorOr<size_t> read(Bytes bytes) override
|
virtual ErrorOr<size_t> read(Bytes bytes) override
|
||||||
{
|
{
|
||||||
|
|
|
@ -215,6 +215,11 @@ ErrorOr<off_t> File::seek(i64 offset, SeekMode mode)
|
||||||
return seek_result;
|
return seek_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> File::truncate(off_t length)
|
||||||
|
{
|
||||||
|
return System::ftruncate(m_fd, length);
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
|
ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
|
||||||
{
|
{
|
||||||
int socket_domain;
|
int socket_domain;
|
||||||
|
|
|
@ -81,6 +81,9 @@ public:
|
||||||
/// Returns the total size of the stream, or an errno in the case of an
|
/// Returns the total size of the stream, or an errno in the case of an
|
||||||
/// error. May not preserve the original position on the stream on failure.
|
/// error. May not preserve the original position on the stream on failure.
|
||||||
virtual ErrorOr<off_t> size();
|
virtual ErrorOr<off_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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The Socket class is the base class for all concrete BSD-style socket
|
/// The Socket class is the base class for all concrete BSD-style socket
|
||||||
|
@ -197,6 +200,7 @@ public:
|
||||||
virtual bool is_open() const override;
|
virtual bool is_open() const override;
|
||||||
virtual void close() override;
|
virtual void close() override;
|
||||||
virtual ErrorOr<off_t> seek(i64 offset, SeekMode) override;
|
virtual ErrorOr<off_t> seek(i64 offset, SeekMode) override;
|
||||||
|
virtual ErrorOr<void> truncate(off_t length) override;
|
||||||
|
|
||||||
virtual ~File() override { close(); }
|
virtual ~File() override { close(); }
|
||||||
|
|
||||||
|
@ -757,6 +761,10 @@ public:
|
||||||
m_helper.clear_buffer();
|
m_helper.clear_buffer();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
virtual ErrorOr<void> truncate(off_t length) override
|
||||||
|
{
|
||||||
|
return m_helper.stream().truncate(length);
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<size_t> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
|
ErrorOr<size_t> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
|
||||||
ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
|
ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue