1// +build !windows
2
3package internet
4
5import (
6	"os"
7
8	"golang.org/x/sys/unix"
9)
10
11// Acquire lock
12func (fl *FileLocker) Acquire() error {
13	f, err := os.Create(fl.path)
14	if err != nil {
15		return err
16	}
17	if err := unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil {
18		f.Close()
19		return newError("failed to lock file: ", fl.path).Base(err)
20	}
21	fl.file = f
22	return nil
23}
24
25// Release lock
26func (fl *FileLocker) Release() {
27	if err := unix.Flock(int(fl.file.Fd()), unix.LOCK_UN); err != nil {
28		newError("failed to unlock file: ", fl.path).Base(err).WriteToLog()
29	}
30	if err := fl.file.Close(); err != nil {
31		newError("failed to close file: ", fl.path).Base(err).WriteToLog()
32	}
33	if err := os.Remove(fl.path); err != nil {
34		newError("failed to remove file: ", fl.path).Base(err).WriteToLog()
35	}
36}
37