diff --git a/core/io/memory.rs b/core/io/memory.rs index aed9531c1..93c248571 100644 --- a/core/io/memory.rs +++ b/core/io/memory.rs @@ -174,6 +174,20 @@ impl File for MemoryFile { Ok(c) } + fn truncate(&self, len: u64, c: Completion) -> Result> { + let new_size = len as usize; + if new_size < self.size.get() { + // Truncate pages + unsafe { + let pages = &mut *self.pages.get(); + pages.retain(|&k, _| k * PAGE_SIZE < new_size); + } + } + self.size.set(new_size); + c.complete(0); + Ok(Arc::new(c)) + } + fn size(&self) -> Result { Ok(self.size.get() as u64) } diff --git a/core/io/unix.rs b/core/io/unix.rs index cfe6d37fe..0eee99321 100644 --- a/core/io/unix.rs +++ b/core/io/unix.rs @@ -450,6 +450,21 @@ impl File for UnixFile<'_> { let file = self.file.lock().unwrap(); Ok(file.metadata()?.len()) } + + #[instrument(err, skip_all, level = Level::INFO)] + fn truncate(&self, len: u64, c: Completion) -> Result> { + let file = self.file.borrow(); + let result = file.set_len(len); + let c = Arc::new(c); + match result { + Ok(()) => { + trace!("file truncated to len=({})", len); + c.complete(0); + Ok(c) + } + Err(e) => Err(e.into()), + } + } } impl Drop for UnixFile<'_> { diff --git a/core/io/windows.rs b/core/io/windows.rs index 701e61269..ed21257fd 100644 --- a/core/io/windows.rs +++ b/core/io/windows.rs @@ -123,6 +123,13 @@ impl File for WindowsFile { } #[instrument(err, skip_all, level = Level::TRACE)] + fn truncate(&self, len: u64, c: Completion) -> Result<()> { + let mut file = self.file.borrow_mut(); + file.set_len(len).map_err(LimboError::IOError)?; + c.complete(0); + Ok(()) + } + fn size(&self) -> Result { let file = self.file.read(); Ok(file.metadata().unwrap().len())