2016-11-22 19:32:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-07-20 20:31:51 +00:00
|
|
|
"fmt"
|
2016-11-22 19:32:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containers/storage/pkg/stringid"
|
2017-07-20 20:31:51 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sys/unix"
|
2016-11-22 19:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
// IsReadWrite() checks if the lock file is read-write
|
|
|
|
IsReadWrite() bool
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lockfile struct {
|
2017-07-20 20:31:51 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
file string
|
|
|
|
fd uintptr
|
|
|
|
lw string
|
|
|
|
locktype int16
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lockfiles map[string]*lockfile
|
|
|
|
lockfilesLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// GetLockfile opens a read-write lock file, creating it if necessary. The
|
|
|
|
// Locker object it returns will be returned unlocked.
|
2016-11-22 19:32:10 +00:00
|
|
|
func GetLockfile(path string) (Locker, error) {
|
|
|
|
lockfilesLock.Lock()
|
|
|
|
defer lockfilesLock.Unlock()
|
|
|
|
if lockfiles == nil {
|
|
|
|
lockfiles = make(map[string]*lockfile)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
cleanPath := filepath.Clean(path)
|
|
|
|
if locker, ok := lockfiles[cleanPath]; ok {
|
|
|
|
if !locker.IsReadWrite() {
|
|
|
|
return nil, errors.Wrapf(ErrLockReadOnly, "lock %q is a read-only lock", cleanPath)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
return locker, nil
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
fd, err := unix.Open(cleanPath, os.O_RDWR|os.O_CREATE, unix.S_IRUSR|unix.S_IWUSR)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
return nil, errors.Wrapf(err, "error opening %q", cleanPath)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
unix.CloseOnExec(fd)
|
|
|
|
locker := &lockfile{file: path, fd: uintptr(fd), lw: stringid.GenerateRandomID(), locktype: unix.F_WRLCK}
|
2016-11-22 19:32:10 +00:00
|
|
|
lockfiles[filepath.Clean(path)] = locker
|
|
|
|
return locker, nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// GetROLockfile opens a read-only lock file. The Locker object it returns
|
|
|
|
// will be returned unlocked.
|
|
|
|
func GetROLockfile(path string) (Locker, error) {
|
|
|
|
lockfilesLock.Lock()
|
|
|
|
defer lockfilesLock.Unlock()
|
|
|
|
if lockfiles == nil {
|
|
|
|
lockfiles = make(map[string]*lockfile)
|
|
|
|
}
|
|
|
|
cleanPath := filepath.Clean(path)
|
|
|
|
if locker, ok := lockfiles[cleanPath]; ok {
|
|
|
|
if locker.IsReadWrite() {
|
|
|
|
return nil, fmt.Errorf("lock %q is a read-write lock", cleanPath)
|
|
|
|
}
|
|
|
|
return locker, nil
|
|
|
|
}
|
|
|
|
fd, err := unix.Open(cleanPath, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error opening %q", cleanPath)
|
|
|
|
}
|
|
|
|
unix.CloseOnExec(fd)
|
|
|
|
locker := &lockfile{file: path, fd: uintptr(fd), lw: stringid.GenerateRandomID(), locktype: unix.F_RDLCK}
|
|
|
|
lockfiles[filepath.Clean(path)] = locker
|
|
|
|
return locker, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks the lock file
|
2016-11-22 19:32:10 +00:00
|
|
|
func (l *lockfile) Lock() {
|
2017-04-27 17:59:23 +00:00
|
|
|
lk := unix.Flock_t{
|
2017-07-20 20:31:51 +00:00
|
|
|
Type: l.locktype,
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Unlock unlocks the lock file
|
2016-11-22 19:32:10 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Touch updates the lock file with the UID of the user
|
2016-11-22 19:32:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Modified indicates if the lock file has been updated since the last time it was loaded
|
2016-11-22 19:32:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// TouchedSince indicates if the lock file has been touched since the specified time
|
2016-11-22 19:32:10 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
// IsRWLock indicates if the lock file is a read-write lock
|
|
|
|
func (l *lockfile) IsReadWrite() bool {
|
|
|
|
return (l.locktype == unix.F_WRLCK)
|
|
|
|
}
|