blob: b1817230a3cfb60b60b1a286317a93feaee96111 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build windows
16
17package fileutil
18
19import (
20 "errors"
21 "fmt"
22 "os"
23 "syscall"
24 "unsafe"
25)
26
27var (
28 modkernel32 = syscall.NewLazyDLL("kernel32.dll")
29 procLockFileEx = modkernel32.NewProc("LockFileEx")
30
31 errLocked = errors.New("The process cannot access the file because another process has locked a portion of the file.")
32)
33
34const (
35 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
36 LOCKFILE_EXCLUSIVE_LOCK = 2
37 LOCKFILE_FAIL_IMMEDIATELY = 1
38
39 // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
40 errLockViolation syscall.Errno = 0x21
41)
42
43func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
44 f, err := open(path, flag, perm)
45 if err != nil {
46 return nil, err
47 }
48 if err := lockFile(syscall.Handle(f.Fd()), LOCKFILE_FAIL_IMMEDIATELY); err != nil {
49 f.Close()
50 return nil, err
51 }
52 return &LockedFile{f}, nil
53}
54
55func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
56 f, err := open(path, flag, perm)
57 if err != nil {
58 return nil, err
59 }
60 if err := lockFile(syscall.Handle(f.Fd()), 0); err != nil {
61 f.Close()
62 return nil, err
63 }
64 return &LockedFile{f}, nil
65}
66
67func open(path string, flag int, perm os.FileMode) (*os.File, error) {
68 if path == "" {
69 return nil, fmt.Errorf("cannot open empty filename")
70 }
71 var access uint32
72 switch flag {
73 case syscall.O_RDONLY:
74 access = syscall.GENERIC_READ
75 case syscall.O_WRONLY:
76 access = syscall.GENERIC_WRITE
77 case syscall.O_RDWR:
78 access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
79 case syscall.O_WRONLY | syscall.O_CREAT:
80 access = syscall.GENERIC_ALL
81 default:
82 panic(fmt.Errorf("flag %v is not supported", flag))
83 }
84 fd, err := syscall.CreateFile(&(syscall.StringToUTF16(path)[0]),
85 access,
86 syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
87 nil,
88 syscall.OPEN_ALWAYS,
89 syscall.FILE_ATTRIBUTE_NORMAL,
90 0)
91 if err != nil {
92 return nil, err
93 }
94 return os.NewFile(uintptr(fd), path), nil
95}
96
97func lockFile(fd syscall.Handle, flags uint32) error {
98 var flag uint32 = LOCKFILE_EXCLUSIVE_LOCK
99 flag |= flags
100 if fd == syscall.InvalidHandle {
101 return nil
102 }
103 err := lockFileEx(fd, flag, 1, 0, &syscall.Overlapped{})
104 if err == nil {
105 return nil
106 } else if err.Error() == errLocked.Error() {
107 return ErrLocked
108 } else if err != errLockViolation {
109 return err
110 }
111 return nil
112}
113
114func lockFileEx(h syscall.Handle, flags, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
115 var reserved uint32 = 0
116 r1, _, e1 := syscall.Syscall6(procLockFileEx.Addr(), 6, uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
117 if r1 == 0 {
118 if e1 != 0 {
119 err = error(e1)
120 } else {
121 err = syscall.EINVAL
122 }
123 }
124 return err
125}