2021-09-09 14:25:25 +02:00
|
|
|
//go:build windows
|
2018-08-19 12:38:26 +02:00
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
package file
|
2018-08-19 12:38:26 +02:00
|
|
|
|
|
|
|
import (
|
2021-11-04 11:12:57 +01:00
|
|
|
"fmt"
|
2018-08-19 12:38:26 +02:00
|
|
|
"os"
|
2021-02-16 17:36:27 +01:00
|
|
|
"sync"
|
2018-08-19 12:38:26 +02:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ntdll = windows.NewLazySystemDLL("ntdll.dll")
|
|
|
|
ntQueryVolumeInformationFile = ntdll.NewProc("NtQueryVolumeInformationFile")
|
|
|
|
ntSetInformationFile = ntdll.NewProc("NtSetInformationFile")
|
2021-02-16 17:36:27 +01:00
|
|
|
preAllocateMu sync.Mutex
|
2018-08-19 12:38:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 11:15:20 +02:00
|
|
|
// PreallocateImplemented is a constant indicating whether the
|
|
|
|
// implementation of Preallocate actually does anything.
|
|
|
|
const PreallocateImplemented = true
|
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
// PreAllocate the file for performance reasons
|
|
|
|
func PreAllocate(size int64, out *os.File) error {
|
2018-08-19 12:38:26 +02:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:36:27 +01:00
|
|
|
preAllocateMu.Lock()
|
|
|
|
defer preAllocateMu.Unlock()
|
|
|
|
|
2018-08-19 12:38:26 +02:00
|
|
|
var (
|
|
|
|
iosb ioStatusBlock
|
|
|
|
fsSizeInfo fileFsSizeInformation
|
|
|
|
allocInfo fileAllocationInformation
|
|
|
|
)
|
|
|
|
|
|
|
|
// Query info about the block sizes on the file system
|
|
|
|
_, _, e1 := ntQueryVolumeInformationFile.Call(
|
2024-04-17 16:40:07 +02:00
|
|
|
out.Fd(),
|
2018-08-19 12:38:26 +02:00
|
|
|
uintptr(unsafe.Pointer(&iosb)),
|
|
|
|
uintptr(unsafe.Pointer(&fsSizeInfo)),
|
2024-04-17 16:40:07 +02:00
|
|
|
unsafe.Sizeof(fsSizeInfo),
|
2018-08-19 12:38:26 +02:00
|
|
|
uintptr(3), // FileFsSizeInformation
|
|
|
|
)
|
|
|
|
if e1 != nil && e1 != syscall.Errno(0) {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("preAllocate NtQueryVolumeInformationFile failed: %w", e1)
|
2018-08-19 12:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the allocation size
|
|
|
|
clusterSize := uint64(fsSizeInfo.BytesPerSector) * uint64(fsSizeInfo.SectorsPerAllocationUnit)
|
|
|
|
if clusterSize <= 0 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("preAllocate clusterSize %d <= 0", clusterSize)
|
2018-08-19 12:38:26 +02:00
|
|
|
}
|
|
|
|
allocInfo.AllocationSize = (1 + uint64(size-1)/clusterSize) * clusterSize
|
|
|
|
|
|
|
|
// Ask for the allocation
|
|
|
|
_, _, e1 = ntSetInformationFile.Call(
|
2024-04-17 16:40:07 +02:00
|
|
|
out.Fd(),
|
2018-08-19 12:38:26 +02:00
|
|
|
uintptr(unsafe.Pointer(&iosb)),
|
|
|
|
uintptr(unsafe.Pointer(&allocInfo)),
|
2024-04-17 16:40:07 +02:00
|
|
|
unsafe.Sizeof(allocInfo),
|
2018-08-19 12:38:26 +02:00
|
|
|
uintptr(19), // FileAllocationInformation
|
|
|
|
)
|
|
|
|
if e1 != nil && e1 != syscall.Errno(0) {
|
2024-04-17 16:40:07 +02:00
|
|
|
if e1 == windows.ERROR_DISK_FULL || e1 == windows.ERROR_HANDLE_DISK_FULL {
|
2021-02-17 14:05:15 +01:00
|
|
|
return ErrDiskFull
|
|
|
|
}
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("preAllocate NtSetInformationFile failed: %w", e1)
|
2018-08-19 12:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-06 13:41:48 +01:00
|
|
|
|
2020-05-19 11:15:20 +02:00
|
|
|
// SetSparseImplemented is a constant indicating whether the
|
|
|
|
// implementation of SetSparse actually does anything.
|
|
|
|
const SetSparseImplemented = true
|
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
// SetSparse makes the file be a sparse file
|
|
|
|
func SetSparse(out *os.File) error {
|
2020-06-05 11:30:30 +02:00
|
|
|
var bytesReturned uint32
|
2024-04-17 16:40:07 +02:00
|
|
|
err := syscall.DeviceIoControl(syscall.Handle(out.Fd()), windows.FSCTL_SET_SPARSE, nil, 0, nil, 0, &bytesReturned, nil)
|
2020-03-06 13:41:48 +01:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("DeviceIoControl FSCTL_SET_SPARSE: %w", err)
|
2020-03-06 13:41:48 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|