2021-02-02 14:43:40 +00:00
|
|
|
package fs
|
2020-08-15 15:45:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-09-07 14:37:26 +00:00
|
|
|
"syscall"
|
2020-08-15 15:45:05 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2021-02-02 14:43:40 +00:00
|
|
|
func PreallocateFile(wr *os.File, size int64) error {
|
2020-08-15 15:45:05 +00:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// int fallocate(int fd, int mode, off_t offset, off_t len)
|
|
|
|
// use mode = 0 to also change the file size
|
2024-09-07 14:37:26 +00:00
|
|
|
return ignoringEINTR(func() error { return unix.Fallocate(int(wr.Fd()), 0, 0, size) })
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignoringEINTR makes a function call and repeats it if it returns
|
|
|
|
// an EINTR error.
|
|
|
|
// copied from /usr/lib/go/src/internal/poll/fd_posix.go of go 1.23.1
|
|
|
|
func ignoringEINTR(fn func() error) error {
|
|
|
|
for {
|
|
|
|
err := fn()
|
|
|
|
if err != syscall.EINTR {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 15:45:05 +00:00
|
|
|
}
|