filelocker_other.go 813 B

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