mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-23 02:26:49 -05:00
c78ee5aac7
* ⬆ Update to Go 1.17 * 🏗 Update workflows and add windows-arm64 * 💾 Update generated files * 📛 Update not-so-friendly filenames
38 lines
813 B
Go
38 lines
813 B
Go
//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()
|
|
}
|
|
}
|