You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Xray-core/transport/internet/filelocker_other.go

40 lines
914 B

//go:build !windows
4 years ago
// +build !windows
package internet
import (
"context"
4 years ago
"os"
"github.com/xtls/xray-core/common/errors"
4 years ago
"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 errors.New("failed to lock file: ", fl.path).Base(err)
4 years ago
}
fl.file = f
return nil
}
// Release lock
func (fl *FileLocker) Release() {
if err := unix.Flock(int(fl.file.Fd()), unix.LOCK_UN); err != nil {
errors.LogInfoInner(context.Background(), err, "failed to unlock file: ", fl.path)
4 years ago
}
if err := fl.file.Close(); err != nil {
errors.LogInfoInner(context.Background(), err, "failed to close file: ", fl.path)
4 years ago
}
if err := os.Remove(fl.path); err != nil {
errors.LogInfoInner(context.Background(), err, "failed to remove file: ", fl.path)
4 years ago
}
}