file_system: add setFileLength

This commit is contained in:
2026-06-20 09:45:59 +02:00
parent 2c5afbd283
commit 13f001e317
+15
View File
@@ -488,6 +488,21 @@ pub fn fileSize(self: *FileSystem, open_file: *OpenFile) usize {
return open_file.entity.bytes.items.len;
}
// Resize the file like ftruncate(2): shrinking discards the tail, growing
// zero-fills. The offset is left in place except where it would fall past the
// new end, which we clamp so a later read/write can't slice out of bounds.
pub fn setFileLength(self: *FileSystem, open_file: *OpenFile, gpa: Allocator, length: usize) Allocator.Error!void {
_ = self;
const current = open_file.entity.bytes.items.len;
if (length < current) {
open_file.entity.bytes.shrinkRetainingCapacity(length);
} else if (length > current) {
try open_file.entity.bytes.appendNTimes(gpa, 0, length - current);
}
if (open_file.cursor > length)
open_file.cursor = length;
}
pub const SeekError = error{
NegativeOffset,
Overflow,