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