Add Release function for flock

pull/2349/head
Erik Wilson 2020-10-05 18:22:44 -07:00
parent 360d82d20e
commit 66d29148f7
No known key found for this signature in database
GPG Key ID: 28E43BB8BE202CF8
2 changed files with 17 additions and 11 deletions

View File

@ -19,6 +19,11 @@ limitations under the License.
package flock package flock
// Acquire is not implemented on non-unix systems. // Acquire is not implemented on non-unix systems.
func Acquire(path string) error { func Acquire(path string) (int, error) {
return -1, nil
}
// Release is not implemented on non-unix systems.
func Release(lock int) error {
return nil return nil
} }

View File

@ -20,16 +20,17 @@ package flock
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"
// Acquire acquires a lock on a file for the duration of the process. This method // Acquire creates an exclusive lock on a file for the duration of the process, or until Release(d).
// is reentrant. // This method is reentrant.
func Acquire(path string) error { func Acquire(path string) (int, error) {
fd, err := unix.Open(path, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600) lock, err := unix.Open(path, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600)
if err != nil { if err != nil {
return err return -1, err
} }
return lock, unix.Flock(lock, unix.LOCK_EX)
// We don't need to close the fd since we should hold }
// it until the process exits.
// Release removes an existing lock held by this process.
return unix.Flock(fd, unix.LOCK_EX) func Release(lock int) error {
return unix.Flock(lock, unix.LOCK_UN)
} }