2021-02-26 22:37:07 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-03-25 18:43:19 +00:00
|
|
|
"sync"
|
2021-02-26 22:37:07 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
2023-03-25 18:43:19 +00:00
|
|
|
// at windows only files could be synced
|
|
|
|
// Sync for directories is not supported.
|
|
|
|
func mustSyncPath(path string) {
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:47:02 +00:00
|
|
|
func mustRemoveDirAtomic(dir string) {
|
2024-02-24 00:07:51 +00:00
|
|
|
n := atomicDirRemoveCounter.Add(1)
|
2023-05-03 08:47:02 +00:00
|
|
|
tmpDir := fmt.Sprintf("%s.must-remove.%d", dir, n)
|
|
|
|
if err := os.Rename(dir, tmpDir); err != nil {
|
|
|
|
logger.Panicf("FATAL: cannot move %s to %s: %s", dir, tmpDir, err)
|
|
|
|
}
|
2023-05-09 06:10:18 +00:00
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
logger.Warnf("cannot remove dir: %q: %s; restart VictoriaMetrics to complete dir removal; "+
|
|
|
|
"see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/70#issuecomment-1491529183", tmpDir, err)
|
2023-05-03 08:47:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:37:07 +00:00
|
|
|
const (
|
|
|
|
lockfileExclusiveLock = 2
|
|
|
|
fileFlagNormal = 0x00000080
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://github.com/juju/fslock/blob/master/fslock_windows.go
|
2021-02-26 23:01:47 +00:00
|
|
|
func createFlockFile(flockFile string) (*os.File, error) {
|
2021-02-26 22:37:07 +00:00
|
|
|
name, err := windows.UTF16PtrFromString(flockFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
handle, err := windows.CreateFile(
|
|
|
|
name,
|
|
|
|
windows.GENERIC_READ|windows.DELETE,
|
|
|
|
windows.FILE_SHARE_READ|windows.FILE_SHARE_DELETE,
|
|
|
|
nil,
|
|
|
|
windows.OPEN_ALWAYS,
|
|
|
|
windows.FILE_FLAG_OVERLAPPED|fileFlagNormal,
|
|
|
|
0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create lock file %q: %w", flockFile, err)
|
|
|
|
}
|
|
|
|
ol, err := newOverlapped()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create Overlapped handler: %w", err)
|
|
|
|
}
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex
|
2024-09-13 10:22:25 +00:00
|
|
|
err = windows.LockFileEx(handle, lockfileExclusiveLock, 0, 0, 0, ol)
|
|
|
|
if err != nil {
|
2021-02-26 22:37:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(handle), flockFile), nil
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:43:19 +00:00
|
|
|
var (
|
2023-03-25 18:57:37 +00:00
|
|
|
mmapByAddrLock sync.Mutex
|
|
|
|
mmapByAddr = map[uintptr]windows.Handle{}
|
2023-03-25 18:43:19 +00:00
|
|
|
)
|
|
|
|
|
2021-02-26 23:01:47 +00:00
|
|
|
func mmap(fd int, length int) ([]byte, error) {
|
2023-03-25 18:43:19 +00:00
|
|
|
flProtect := uint32(windows.PAGE_READONLY)
|
|
|
|
dwDesiredAccess := uint32(windows.FILE_MAP_READ)
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-file-mapping-object#file-mapping-size
|
|
|
|
// do not specify any length params, windows will set it according to the file size.
|
|
|
|
// If length > file size, truncate is required according to api definition, we don't want it.
|
|
|
|
h, errno := windows.CreateFileMapping(windows.Handle(fd), nil, flProtect, 0, 0, nil)
|
|
|
|
if h == 0 {
|
|
|
|
return nil, os.NewSyscallError("CreateFileMapping", errno)
|
|
|
|
}
|
|
|
|
addr, errno := windows.MapViewOfFile(h, dwDesiredAccess, 0, 0, 0)
|
|
|
|
if addr == 0 {
|
|
|
|
windows.CloseHandle(h)
|
|
|
|
return nil, os.NewSyscallError("MapViewOfFile", errno)
|
|
|
|
}
|
2024-02-29 23:43:06 +00:00
|
|
|
data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length)
|
2023-03-25 18:43:19 +00:00
|
|
|
|
2023-03-25 18:57:37 +00:00
|
|
|
mmapByAddrLock.Lock()
|
|
|
|
mmapByAddr[addr] = h
|
|
|
|
mmapByAddrLock.Unlock()
|
|
|
|
|
2023-03-25 18:43:19 +00:00
|
|
|
return data, nil
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 18:43:19 +00:00
|
|
|
func mUnmap(data []byte) error {
|
|
|
|
// flush is not needed, since we perform only reading operation.
|
|
|
|
// In case of write, additional call FlushViewOfFile must be performed.
|
2024-02-29 15:26:07 +00:00
|
|
|
addr := uintptr(unsafe.Pointer(unsafe.SliceData(data)))
|
2023-03-25 18:43:19 +00:00
|
|
|
|
2023-03-25 18:57:37 +00:00
|
|
|
mmapByAddrLock.Lock()
|
|
|
|
h, ok := mmapByAddr[addr]
|
2023-03-25 18:43:19 +00:00
|
|
|
if !ok {
|
|
|
|
logger.Fatalf("BUG: unmapping for non exist addr: %d", addr)
|
|
|
|
}
|
2023-03-25 18:57:37 +00:00
|
|
|
delete(mmapByAddr, addr)
|
|
|
|
mmapByAddrLock.Unlock()
|
2023-03-25 18:43:19 +00:00
|
|
|
|
2023-03-25 18:57:37 +00:00
|
|
|
if err := windows.UnmapViewOfFile(addr); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmap memory mapped file: %w", err)
|
|
|
|
}
|
|
|
|
errno := windows.CloseHandle(h)
|
|
|
|
return os.NewSyscallError("CloseHandle", errno)
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetFreeSpace(path string) uint64 {
|
2024-09-13 10:22:25 +00:00
|
|
|
var freeBytes uint64
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw
|
|
|
|
err := windows.GetDiskFreeSpaceEx(windows.StringToUTF16Ptr(path), &freeBytes, nil, nil)
|
|
|
|
if err != nil {
|
2023-03-25 18:57:37 +00:00
|
|
|
logger.Panicf("FATAL: cannot get free space for %q : %s", path, err)
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
2024-09-13 10:22:25 +00:00
|
|
|
return freeBytes
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stub
|
|
|
|
func fadviseSequentialRead(f *os.File, prefetch bool) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-overlapped
|
|
|
|
func newOverlapped() (*windows.Overlapped, error) {
|
2024-09-13 10:22:25 +00:00
|
|
|
event, err := windows.CreateEvent(nil, 1, 1, nil)
|
2021-02-26 22:37:07 +00:00
|
|
|
if err != nil {
|
2024-09-13 10:22:25 +00:00
|
|
|
return nil, fmt.Errorf("cannot create event: %w", err)
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
return &windows.Overlapped{HEvent: event}, nil
|
|
|
|
}
|