Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
//go:build !windows
// +build !windows
 
package internet
 
import (
    "os"
 
    "golang.org/x/sys/unix"
)
 
// Acquire lock
func (fl *FileLocker) Acquire() error {
    f, err := os.Create(fl.path)
    if err != nil {
        return err
    }
    if err := unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil {
        f.Close()
        return newError("failed to lock file: ", fl.path).Base(err)
    }
    fl.file = f
    return nil
}
 
// Release lock
func (fl *FileLocker) Release() {
    if err := unix.Flock(int(fl.file.Fd()), unix.LOCK_UN); err != nil {
        newError("failed to unlock file: ", fl.path).Base(err).WriteToLog()
    }
    if err := fl.file.Close(); err != nil {
        newError("failed to close file: ", fl.path).Base(err).WriteToLog()
    }
    if err := os.Remove(fl.path); err != nil {
        newError("failed to remove file: ", fl.path).Base(err).WriteToLog()
    }
}