forked from OpenPrinting/ipp-usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flock_unix.go
43 lines (34 loc) · 834 Bytes
/
flock_unix.go
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
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* File locking -- UNIX version
*/
package main
import (
"os"
"syscall"
)
// FileLock acquires file lock
func FileLock(file *os.File, exclusive, wait bool) error {
var how int
if exclusive {
how = syscall.LOCK_EX
} else {
how = syscall.LOCK_SH
}
if !wait {
how |= syscall.LOCK_NB
}
err := syscall.Flock(int(file.Fd()), how)
if err == syscall.Errno(syscall.EWOULDBLOCK) {
err = ErrLockIsBusy
}
return err
}
// FileUnlock releases file lock
func FileUnlock(file *os.File) error {
return syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
}