Files
got/httpdl/alloc_linux.go
2026-06-19 12:32:14 +09:00

28 lines
641 B
Go

//go:build linux
package httpdl
import (
"os"
"golang.org/x/sys/unix"
)
// allocate reserves disk space for the output file according to the
// --file-allocation mode: none does nothing (sparse), trunc sets the size,
// prealloc/falloc reserve real blocks with fallocate (falling back to a plain
// truncate if the filesystem does not support it).
func allocate(f *os.File, mode string, size int64) error {
switch mode {
case "none":
return nil
case "trunc":
return f.Truncate(size)
default: // prealloc, falloc
if err := unix.Fallocate(int(f.Fd()), 0, 0, size); err != nil {
return f.Truncate(size)
}
return nil
}
}