Debian Codegraph @

cache/internal/flock/flock_winapi.go

raw
1 // Copyright 2015 Tim Heckman. All rights reserved.
2 // Use of this source code is governed by the BSD 3-Clause
3 // license that can be found in the LICENSE file.
4
5 //go:build windows
6 // +build windows
7
8 package flock
9
10 import (
11 "syscall"
12 "unsafe"
13 )
14
15 var (
16 kernel32, _ = syscall.LoadLibrary("kernel32.dll")
17 procLockFileEx, _ = syscall.GetProcAddress(kernel32, "LockFileEx")
18 procUnlockFileEx, _ = syscall.GetProcAddress(kernel32, "UnlockFileEx")
19 )
20
21 const (
22 winLockfileFailImmediately = 0x00000001
23 winLockfileExclusiveLock = 0x00000002
24 winLockfileSharedLock = 0x00000000
25 )
26
27 // Use of 0x00000000 for the shared lock is a guess based on some the MS Windows
28 // `LockFileEX` docs, which document the `LOCKFILE_EXCLUSIVE_LOCK` flag as:
29 //
30 // > The function requests an exclusive lock. Otherwise, it requests a shared
31 // > lock.
32 //
33 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
34
35 func lockFileEx(handle syscall.Handle, flags uint32, reserved uint32, numberOfBytesToLockLow uint32, numberOfBytesToLockHigh uint32, offset *syscall.Overlapped) (bool, syscall.Errno) {
36 r1, _, errNo := syscall.SyscallN(
37 uintptr(procLockFileEx),
38 uintptr(handle),
39 uintptr(flags),
40 uintptr(reserved),
41 uintptr(numberOfBytesToLockLow),
42 uintptr(numberOfBytesToLockHigh),
43 uintptr(unsafe.Pointer(offset)))
44
45 if r1 != 1 {
46 if errNo == 0 {
47 return false, syscall.EINVAL
48 }
49
50 return false, errNo
51 }
52
53 return true, 0
54 }
55
56 func unlockFileEx(handle syscall.Handle, reserved uint32, numberOfBytesToLockLow uint32, numberOfBytesToLockHigh uint32, offset *syscall.Overlapped) (bool, syscall.Errno) {
57 r1, _, errNo := syscall.SyscallN(
58 uintptr(procUnlockFileEx),
59 uintptr(handle),
60 uintptr(reserved),
61 uintptr(numberOfBytesToLockLow),
62 uintptr(numberOfBytesToLockHigh),
63 uintptr(unsafe.Pointer(offset)),
64 0)
65
66 if r1 != 1 {
67 if errNo == 0 {
68 return false, syscall.EINVAL
69 }
70
71 return false, errNo
72 }
73
74 return true, 0
75 }