1
2
3
4
5
6
7 package filelock
8
9 import (
10 "internal/syscall/windows"
11 "io/fs"
12 "syscall"
13 )
14
15 type lockType uint32
16
17 const (
18 readLock lockType = 0
19 writeLock lockType = windows.LOCKFILE_EXCLUSIVE_LOCK
20 )
21
22 const (
23 reserved = 0
24 allBytes = ^uint32(0)
25 )
26
27 func lock(f File, lt lockType) error {
28
29
30
31
32
33 ol := new(syscall.Overlapped)
34
35 err := windows.LockFileEx(syscall.Handle(f.Fd()), uint32(lt), reserved, allBytes, allBytes, ol)
36 if err != nil {
37 return &fs.PathError{
38 Op: lt.String(),
39 Path: f.Name(),
40 Err: err,
41 }
42 }
43 return nil
44 }
45
46 func unlock(f File) error {
47 ol := new(syscall.Overlapped)
48 err := windows.UnlockFileEx(syscall.Handle(f.Fd()), reserved, allBytes, allBytes, ol)
49 if err != nil {
50 return &fs.PathError{
51 Op: "Unlock",
52 Path: f.Name(),
53 Err: err,
54 }
55 }
56 return nil
57 }
58
View as plain text