Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/lockutil/lockutil_windows.go
2610 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
// From https://github.com/containerd/nerdctl/blob/v0.13.0/pkg/lockutil/lockutil_windows.go
5
/*
6
Copyright The containerd Authors.
7
8
Licensed under the Apache License, Version 2.0 (the "License");
9
you may not use this file except in compliance with the License.
10
You may obtain a copy of the License at
11
12
http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS,
16
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
See the License for the specific language governing permissions and
18
limitations under the License.
19
*/
20
21
package lockutil
22
23
import (
24
"fmt"
25
"os"
26
"syscall"
27
"unsafe"
28
29
"github.com/sirupsen/logrus"
30
)
31
32
// LockFile modified from https://github.com/boltdb/bolt/blob/v1.3.1/bolt_windows.go using MIT.
33
var (
34
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
35
procLockFileEx = modkernel32.NewProc("LockFileEx")
36
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
37
)
38
39
// LOCKFILE_EXCLUSIVE_LOCK from https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
40
const flagLockfileExclusiveLock = 0x00000002
41
42
func WithDirLock(dir string, fn func() error) error {
43
dirFile, err := os.OpenFile(dir+".lock", os.O_CREATE, 0o644)
44
if err != nil {
45
return err
46
}
47
defer dirFile.Close()
48
if err := lockFileEx(
49
syscall.Handle(dirFile.Fd()), // hFile
50
flagLockfileExclusiveLock, // dwFlags
51
0, // dwReserved
52
1, // nNumberOfBytesToLockLow
53
0, // nNumberOfBytesToLockHigh
54
&syscall.Overlapped{}, // lpOverlapped
55
); err != nil {
56
return fmt.Errorf("failed to lock %q: %w", dir, err)
57
}
58
59
defer func() {
60
if err := unlockFileEx(
61
syscall.Handle(dirFile.Fd()), // hFile
62
0, // dwReserved
63
1, // nNumberOfBytesToLockLow
64
0, // nNumberOfBytesToLockHigh
65
&syscall.Overlapped{}, // lpOverlapped
66
); err != nil {
67
logrus.WithError(err).Errorf("failed to unlock %q", dir)
68
}
69
}()
70
return fn()
71
}
72
73
func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
74
r, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
75
if r == 0 {
76
return err
77
}
78
return nil
79
}
80
81
func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
82
r, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
83
if r == 0 {
84
return err
85
}
86
return nil
87
}
88
89