| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package filelock |
| 8 |
|
| 9 |
import ( |
| 10 |
"io/fs" |
| 11 |
"syscall" |
| 12 |
|
| 13 |
"github.com/rogpeppe/go-internal/internal/syscall/windows" |
| 14 |
) |
| 15 |
|
| 16 |
type lockType uint32 |
| 17 |
|
| 18 |
const ( |
| 19 |
readLock lockType = 0 |
| 20 |
writeLock lockType = windows.LOCKFILE_EXCLUSIVE_LOCK |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
reserved = 0 |
| 25 |
allBytes = ^uint32(0) |
| 26 |
) |
| 27 |
|
| 28 |
func lock(f File, lt lockType) error { |
| 29 |
Fd |
| 30 |
” |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
ol := new(syscall.Overlapped) |
| 35 |
|
| 36 |
err := windows.LockFileEx(syscall.Handle(f.Fd()), uint32(lt), reserved, allBytes, allBytes, ol) |
| 37 |
if err != nil { |
| 38 |
return &fs.PathError{ |
| 39 |
Op: lt.String(), |
| 40 |
Path: f.Name(), |
| 41 |
Err: err, |
| 42 |
} |
| 43 |
} |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func unlock(f File) error { |
| 48 |
ol := new(syscall.Overlapped) |
| 49 |
err := windows.UnlockFileEx(syscall.Handle(f.Fd()), reserved, allBytes, allBytes, ol) |
| 50 |
if err != nil { |
| 51 |
return &fs.PathError{ |
| 52 |
Op: "Unlock", |
| 53 |
Path: f.Name(), |
| 54 |
Err: err, |
| 55 |
} |
| 56 |
} |
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
func isNotSupported(err error) bool { |
| 61 |
switch err { |
| 62 |
case windows.ERROR_NOT_SUPPORTED, windows.ERROR_CALL_NOT_IMPLEMENTED, ErrNotSupported: |
| 63 |
return true |
| 64 |
default: |
| 65 |
return false |
| 66 |
} |
| 67 |
} |