2018-08-19 10:38:26 +00:00
|
|
|
//+build windows
|
|
|
|
|
2020-03-24 16:26:45 +00:00
|
|
|
package file
|
2018-08-19 10:38:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ntdll = windows.NewLazySystemDLL("ntdll.dll")
|
|
|
|
ntQueryVolumeInformationFile = ntdll.NewProc("NtQueryVolumeInformationFile")
|
|
|
|
ntSetInformationFile = ntdll.NewProc("NtSetInformationFile")
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileAllocationInformation struct {
|
|
|
|
AllocationSize uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileFsSizeInformation struct {
|
|
|
|
TotalAllocationUnits uint64
|
|
|
|
AvailableAllocationUnits uint64
|
|
|
|
SectorsPerAllocationUnit uint32
|
|
|
|
BytesPerSector uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type ioStatusBlock struct {
|
|
|
|
Status, Information uintptr
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:15:20 +00:00
|
|
|
// PreallocateImplemented is a constant indicating whether the
|
|
|
|
// implementation of Preallocate actually does anything.
|
|
|
|
const PreallocateImplemented = true
|
|
|
|
|
2020-03-24 16:26:45 +00:00
|
|
|
// PreAllocate the file for performance reasons
|
|
|
|
func PreAllocate(size int64, out *os.File) error {
|
2018-08-19 10:38:26 +00:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
iosb ioStatusBlock
|
|
|
|
fsSizeInfo fileFsSizeInformation
|
|
|
|
allocInfo fileAllocationInformation
|
|
|
|
)
|
|
|
|
|
|
|
|
// Query info about the block sizes on the file system
|
|
|
|
_, _, e1 := ntQueryVolumeInformationFile.Call(
|
|
|
|
uintptr(out.Fd()),
|
|
|
|
uintptr(unsafe.Pointer(&iosb)),
|
|
|
|
uintptr(unsafe.Pointer(&fsSizeInfo)),
|
|
|
|
uintptr(unsafe.Sizeof(fsSizeInfo)),
|
|
|
|
uintptr(3), // FileFsSizeInformation
|
|
|
|
)
|
|
|
|
if e1 != nil && e1 != syscall.Errno(0) {
|
|
|
|
return errors.Wrap(e1, "preAllocate NtQueryVolumeInformationFile failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the allocation size
|
|
|
|
clusterSize := uint64(fsSizeInfo.BytesPerSector) * uint64(fsSizeInfo.SectorsPerAllocationUnit)
|
|
|
|
if clusterSize <= 0 {
|
|
|
|
return errors.Errorf("preAllocate clusterSize %d <= 0", clusterSize)
|
|
|
|
}
|
|
|
|
allocInfo.AllocationSize = (1 + uint64(size-1)/clusterSize) * clusterSize
|
|
|
|
|
|
|
|
// Ask for the allocation
|
|
|
|
_, _, e1 = ntSetInformationFile.Call(
|
|
|
|
uintptr(out.Fd()),
|
|
|
|
uintptr(unsafe.Pointer(&iosb)),
|
|
|
|
uintptr(unsafe.Pointer(&allocInfo)),
|
|
|
|
uintptr(unsafe.Sizeof(allocInfo)),
|
|
|
|
uintptr(19), // FileAllocationInformation
|
|
|
|
)
|
|
|
|
if e1 != nil && e1 != syscall.Errno(0) {
|
|
|
|
return errors.Wrap(e1, "preAllocate NtSetInformationFile failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-06 12:41:48 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
FSCTL_SET_SPARSE = 0x000900c4
|
|
|
|
)
|
|
|
|
|
2020-05-19 09:15:20 +00:00
|
|
|
// SetSparseImplemented is a constant indicating whether the
|
|
|
|
// implementation of SetSparse actually does anything.
|
|
|
|
const SetSparseImplemented = true
|
|
|
|
|
2020-03-24 16:26:45 +00:00
|
|
|
// SetSparse makes the file be a sparse file
|
|
|
|
func SetSparse(out *os.File) error {
|
2020-06-05 09:30:30 +00:00
|
|
|
var bytesReturned uint32
|
|
|
|
err := syscall.DeviceIoControl(syscall.Handle(out.Fd()), FSCTL_SET_SPARSE, nil, 0, nil, 0, &bytesReturned, nil)
|
2020-03-06 12:41:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "DeviceIoControl FSCTL_SET_SPARSE")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|