os: add file.Truncate

This commit is contained in:
leongross
2024-08-02 08:22:46 -07:00
committed by GitHub
parent 417a26d20c
commit a8a532f9d6
6 changed files with 116 additions and 12 deletions
+19
View File
@@ -125,6 +125,25 @@ func Readlink(name string) (string, error) {
}
}
// Truncate changes the size of the file.
// It does not change the I/O offset.
// If there is an error, it will be of type *PathError.
// Alternatively just use 'raw' syscall by file name
func (f *File) Truncate(size int64) (err error) {
if f.handle == nil {
return ErrClosed
}
e := ignoringEINTR(func() error {
return syscall.Truncate(f.name, size)
})
if e != nil {
return &PathError{Op: "truncate", Path: f.name, Err: e}
}
return
}
// ReadAt reads up to len(b) bytes from the File starting at the given absolute offset.
// It returns the number of bytes read and any error encountered, possibly io.EOF.
// At end of file, Pread returns 0, io.EOF.