-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelock_windows.go
95 lines (82 loc) · 2.47 KB
/
filelock_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build windows
package filelock
import (
"io/fs"
"syscall"
"unsafe"
)
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procLockFileEx = modkernel32.NewProc("LockFileEx")
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
)
const (
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex#parameters
sysLOCKFILE_EXCLUSIVE_LOCK = 0x00000002
sysLOCKFILE_FAIL_IMMEDIATELY = 0x00000001
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
errnoERROR_LOCK_VIOLATION syscall.Errno = 33
errnoERROR_NOT_SUPPORTED syscall.Errno = 50
errnoERROR_CALL_NOT_IMPLEMENTED syscall.Errno = 120
reserved = 0
allBytes = ^uint32(0)
)
type lockType uint32
const (
readLock lockType = 0x00000000
readLockNB lockType = sysLOCKFILE_FAIL_IMMEDIATELY
writeLock lockType = sysLOCKFILE_EXCLUSIVE_LOCK
writeLockNB lockType = sysLOCKFILE_EXCLUSIVE_LOCK | sysLOCKFILE_FAIL_IMMEDIATELY
)
func lock(f File, lt lockType) error {
// Per https://golang.org/issue/19098, “Programs currently expect the Fd
// method to return a handle that uses ordinary synchronous I/O.”
// However, LockFileEx still requires an OVERLAPPED structure,
// which contains the file offset of the beginning of the lock range.
// We want to lock the entire file, so we leave the offset as zero.
ol := new(syscall.Overlapped)
r1, _, err := procLockFileEx.Call(
uintptr(syscall.Handle(f.Fd())), uintptr(uint32(lt)),
uintptr(reserved), uintptr(allBytes),
uintptr(allBytes), uintptr(unsafe.Pointer(ol)),
)
if r1 == 0 {
if (lt & sysLOCKFILE_FAIL_IMMEDIATELY) > 0 {
if err == errnoERROR_LOCK_VIOLATION || err == syscall.ERROR_IO_PENDING {
return ErrWouldBlock
}
}
return &fs.PathError{
Op: lt.String(),
Path: f.Name(),
Err: err,
}
}
return nil
}
func unlock(f File) error {
ol := new(syscall.Overlapped)
r1, _, err := procUnlockFileEx.Call(
uintptr(syscall.Handle(f.Fd())), uintptr(reserved),
uintptr(allBytes), uintptr(allBytes), uintptr(unsafe.Pointer(ol)),
)
if r1 == 0 {
return &fs.PathError{
Op: "Unlock",
Path: f.Name(),
Err: err,
}
}
return nil
}
func isNotSupported(err error) bool {
switch err {
case errnoERROR_NOT_SUPPORTED, errnoERROR_CALL_NOT_IMPLEMENTED, ErrNotSupported:
return true
default:
return false
}
}