| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | // +build windows |
||
| 3 | package providers |
||
| 4 | |||
| 5 | import ( |
||
| 6 | "syscall" |
||
| 7 | "unsafe" |
||
| 8 | ) |
||
| 9 | |||
| 10 | var ( |
||
| 11 | modkernel32 = syscall.NewLazyDLL("kernel32.dll") |
||
| 12 | procLockFileEx = modkernel32.NewProc("LockFileEx") |
||
| 13 | procUnlockFileEx = modkernel32.NewProc("UnlockFileEx") |
||
| 14 | ) |
||
| 15 | |||
| 16 | const ( |
||
| 17 | // LOCKFILE_EXCLUSIVE_LOCK - request exclusive lock |
||
| 18 | lockfileExclusiveLock = 0x00000002 |
||
| 19 | ) |
||
| 20 | |||
| 21 | // lockFile acquires an exclusive lock on the file using Windows LockFileEx |
||
| 22 | func lockFile(fd int) error { |
||
| 23 | // LockFileEx parameters: |
||
| 24 | // - hFile: file handle |
||
| 25 | // - dwFlags: LOCKFILE_EXCLUSIVE_LOCK for exclusive lock |
||
| 26 | // - dwReserved: must be 0 |
||
| 27 | // - nNumberOfBytesToLockLow: low-order 32 bits of lock range (1 byte is enough) |
||
| 28 | // - nNumberOfBytesToLockHigh: high-order 32 bits of lock range |
||
| 29 | // - lpOverlapped: pointer to OVERLAPPED structure |
||
| 30 | var overlapped syscall.Overlapped |
||
| 31 | r1, _, err := procLockFileEx.Call( |
||
| 32 | uintptr(fd), |
||
| 33 | uintptr(lockfileExclusiveLock), |
||
| 34 | 0, |
||
| 35 | 1, |
||
| 36 | 0, |
||
| 37 | uintptr(unsafe.Pointer(&overlapped)), |
||
| 38 | ) |
||
| 39 | if r1 == 0 { |
||
| 40 | return err |
||
| 41 | } |
||
| 42 | return nil |
||
| 43 | } |
||
| 44 | |||
| 45 | // unlockFile releases the lock on the file using Windows UnlockFileEx |
||
| 46 | func unlockFile(fd int) error { |
||
| 47 | var overlapped syscall.Overlapped |
||
| 48 | r1, _, err := procUnlockFileEx.Call( |
||
| 49 | uintptr(fd), |
||
| 50 | 0, |
||
| 51 | 1, |
||
| 52 | 0, |
||
| 53 | uintptr(unsafe.Pointer(&overlapped)), |
||
| 54 | ) |
||
| 55 | if r1 == 0 { |
||
| 56 | return err |
||
| 57 | } |
||
| 58 | return nil |
||
| 59 | } |
||
| 60 |