2021-09-20 13:00:55 +00:00
|
|
|
//go:build !windows
|
2020-11-25 11:01:53 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2024-06-29 18:32:57 +00:00
|
|
|
"context"
|
2020-11-25 11:01:53 +00:00
|
|
|
"os"
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-11-25 11:01:53 +00:00
|
|
|
"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()
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to lock file: ", fl.path).Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
fl.file = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release lock
|
|
|
|
func (fl *FileLocker) Release() {
|
|
|
|
if err := unix.Flock(int(fl.file.Fd()), unix.LOCK_UN); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfoInner(context.Background(), err, "failed to unlock file: ", fl.path)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if err := fl.file.Close(); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfoInner(context.Background(), err, "failed to close file: ", fl.path)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if err := os.Remove(fl.path); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfoInner(context.Background(), err, "failed to remove file: ", fl.path)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
}
|