first commit
This commit is contained in:
27
httpdl/alloc_linux.go
Normal file
27
httpdl/alloc_linux.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user