mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-29 02:26:31 +00:00
e43a46e982
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
35 lines
617 B
Go
35 lines
617 B
Go
package storage
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
|
|
"git.iim.gay/grufwub/go-store/util"
|
|
)
|
|
|
|
type lockableFile struct {
|
|
*os.File
|
|
}
|
|
|
|
func openLock(path string) (*lockableFile, error) {
|
|
file, err := open(path, defaultFileLockFlags)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &lockableFile{file}, nil
|
|
}
|
|
|
|
func (f *lockableFile) lock() error {
|
|
return f.flock(syscall.LOCK_EX | syscall.LOCK_NB)
|
|
}
|
|
|
|
func (f *lockableFile) unlock() error {
|
|
return f.flock(syscall.LOCK_UN | syscall.LOCK_NB)
|
|
}
|
|
|
|
func (f *lockableFile) flock(how int) error {
|
|
return util.RetryOnEINTR(func() error {
|
|
return syscall.Flock(int(f.Fd()), how)
|
|
})
|
|
}
|