2016-11-22 19:32:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
"github.com/containers/storage/pkg/stringid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Locker represents a file lock where the file is used to cache an
|
|
|
|
// identifier of the last party that made changes to whatever's being protected
|
|
|
|
// by the lock.
|
|
|
|
type Locker interface {
|
|
|
|
sync.Locker
|
|
|
|
|
|
|
|
// Touch records, for others sharing the lock, that the caller was the
|
|
|
|
// last writer. It should only be called with the lock held.
|
|
|
|
Touch() error
|
|
|
|
|
|
|
|
// Modified() checks if the most recent writer was a party other than the
|
|
|
|
// last recorded writer. It should only be called with the lock held.
|
|
|
|
Modified() (bool, error)
|
|
|
|
|
|
|
|
// TouchedSince() checks if the most recent writer modified the file (likely using Touch()) after the specified time.
|
|
|
|
TouchedSince(when time.Time) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type lockfile struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
file string
|
|
|
|
fd uintptr
|
|
|
|
lw string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lockfiles map[string]*lockfile
|
|
|
|
lockfilesLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetLockfile opens a lock file, creating it if necessary. The Locker object
|
|
|
|
// return will be returned unlocked.
|
|
|
|
func GetLockfile(path string) (Locker, error) {
|
|
|
|
lockfilesLock.Lock()
|
|
|
|
defer lockfilesLock.Unlock()
|
|
|
|
if lockfiles == nil {
|
|
|
|
lockfiles = make(map[string]*lockfile)
|
|
|
|
}
|
|
|
|
if locker, ok := lockfiles[filepath.Clean(path)]; ok {
|
|
|
|
return locker, nil
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
fd, err := unix.Open(filepath.Clean(path), os.O_RDWR|os.O_CREATE, unix.S_IRUSR|unix.S_IWUSR)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
locker := &lockfile{file: path, fd: uintptr(fd), lw: stringid.GenerateRandomID()}
|
|
|
|
lockfiles[filepath.Clean(path)] = locker
|
|
|
|
return locker, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockfile) Lock() {
|
2017-04-27 17:59:23 +00:00
|
|
|
lk := unix.Flock_t{
|
|
|
|
Type: unix.F_WRLCK,
|
2016-11-22 19:32:10 +00:00
|
|
|
Whence: int16(os.SEEK_SET),
|
|
|
|
Start: 0,
|
|
|
|
Len: 0,
|
|
|
|
Pid: int32(os.Getpid()),
|
|
|
|
}
|
|
|
|
l.mu.Lock()
|
2017-04-27 17:59:23 +00:00
|
|
|
for unix.FcntlFlock(l.fd, unix.F_SETLKW, &lk) != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockfile) Unlock() {
|
2017-04-27 17:59:23 +00:00
|
|
|
lk := unix.Flock_t{
|
|
|
|
Type: unix.F_UNLCK,
|
2016-11-22 19:32:10 +00:00
|
|
|
Whence: int16(os.SEEK_SET),
|
|
|
|
Start: 0,
|
|
|
|
Len: 0,
|
|
|
|
Pid: int32(os.Getpid()),
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
for unix.FcntlFlock(l.fd, unix.F_SETLKW, &lk) != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
l.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockfile) Touch() error {
|
|
|
|
l.lw = stringid.GenerateRandomID()
|
|
|
|
id := []byte(l.lw)
|
2017-04-27 17:59:23 +00:00
|
|
|
_, err := unix.Seek(int(l.fd), 0, os.SEEK_SET)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
n, err := unix.Write(int(l.fd), id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(id) {
|
2017-04-27 17:59:23 +00:00
|
|
|
return unix.ENOSPC
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
err = unix.Fsync(int(l.fd))
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockfile) Modified() (bool, error) {
|
|
|
|
id := []byte(l.lw)
|
2017-04-27 17:59:23 +00:00
|
|
|
_, err := unix.Seek(int(l.fd), 0, os.SEEK_SET)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
n, err := unix.Read(int(l.fd), id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if n != len(id) {
|
2017-04-27 17:59:23 +00:00
|
|
|
return true, unix.ENOSPC
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
lw := l.lw
|
|
|
|
l.lw = string(id)
|
|
|
|
return l.lw != lw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockfile) TouchedSince(when time.Time) bool {
|
2017-04-27 17:59:23 +00:00
|
|
|
st := unix.Stat_t{}
|
|
|
|
err := unix.Fstat(int(l.fd), &st)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2017-04-27 17:59:23 +00:00
|
|
|
touched := time.Unix(statTMtimeUnix(st))
|
2016-11-22 19:32:10 +00:00
|
|
|
return when.Before(touched)
|
|
|
|
}
|