File: rawfilelock_windows.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (48 lines) | stat: -rw-r--r-- 1,008 bytes parent folder | download | duplicates (2)
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
//go:build windows

package rawfilelock

import (
	"golang.org/x/sys/windows"
)

const (
	reserved = 0
	allBytes = ^uint32(0)
)

type fileHandle windows.Handle

func openHandle(path string, mode int) (fileHandle, error) {
	mode |= windows.O_CLOEXEC
	fd, err := windows.Open(path, mode, windows.S_IWRITE)
	return fileHandle(fd), err
}

func lockHandle(fd fileHandle, lType LockType, nonblocking bool) error {
	flags := 0
	if lType != ReadLock {
		flags = windows.LOCKFILE_EXCLUSIVE_LOCK
	}
	if nonblocking {
		flags |= windows.LOCKFILE_FAIL_IMMEDIATELY
	}
	ol := new(windows.Overlapped)
	if err := windows.LockFileEx(windows.Handle(fd), uint32(flags), reserved, allBytes, allBytes, ol); err != nil {
		if nonblocking {
			return err
		}
		panic(err)
	}
	return nil
}

func unlockAndCloseHandle(fd fileHandle) {
	ol := new(windows.Overlapped)
	windows.UnlockFileEx(windows.Handle(fd), reserved, allBytes, allBytes, ol)
	closeHandle(fd)
}

func closeHandle(fd fileHandle) {
	windows.Close(windows.Handle(fd))
}