From 35b844ba4c8aa940444ed7b013462520e57b0f05 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Sep 2020 16:09:26 +0200 Subject: [PATCH] LibCore: Add Core::IODevice::truncate() This is just a wrapper around ftruncate(). Today I also learned that ftruncate() does not reset the file descriptor offset. :^) --- Libraries/LibCore/IODevice.cpp | 10 ++++++++++ Libraries/LibCore/IODevice.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/Libraries/LibCore/IODevice.cpp b/Libraries/LibCore/IODevice.cpp index df69d3b182f..22d4eb994bf 100644 --- a/Libraries/LibCore/IODevice.cpp +++ b/Libraries/LibCore/IODevice.cpp @@ -266,6 +266,16 @@ bool IODevice::seek(i64 offset, SeekMode mode, off_t* pos) return true; } +bool IODevice::truncate(off_t size) +{ + int rc = ftruncate(m_fd, size); + if (rc < 0) { + set_error(errno); + return false; + } + return true; +} + bool IODevice::write(const u8* data, int size) { int rc = ::write(m_fd, data, size); diff --git a/Libraries/LibCore/IODevice.h b/Libraries/LibCore/IODevice.h index 8c8ee6beb6d..2fe1e93e65f 100644 --- a/Libraries/LibCore/IODevice.h +++ b/Libraries/LibCore/IODevice.h @@ -65,6 +65,8 @@ public: bool write(const u8*, int size); bool write(const StringView&); + bool truncate(off_t); + bool can_read_line() const; bool can_read() const;