Update c/storage vendor to v1.10 release

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
Matthew Heon
2019-02-20 17:53:09 -05:00
parent 4de0bf9c74
commit 86dd25ae48
13 changed files with 203 additions and 89 deletions

View File

@@ -1,7 +1,6 @@
package storage
import (
"fmt"
"path/filepath"
"sync"
"time"
@@ -13,7 +12,14 @@ import (
// identifier of the last party that made changes to whatever's being protected
// by the lock.
type Locker interface {
sync.Locker
// Acquire a writer lock.
Lock()
// Unlock the lock.
Unlock()
// Acquire a reader lock.
RLock()
// Touch records, for others sharing the lock, that the caller was the
// last writer. It should only be called with the lock held.
@@ -39,31 +45,22 @@ var (
)
// GetLockfile opens a read-write lock file, creating it if necessary. The
// Locker object it returns will be returned unlocked.
// Locker object may already be locked if the path has already been requested
// by the current process.
func GetLockfile(path string) (Locker, error) {
lockfilesLock.Lock()
defer lockfilesLock.Unlock()
if lockfiles == nil {
lockfiles = make(map[string]Locker)
}
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)
}
return locker, nil
}
locker, err := getLockFile(path, false) // platform dependent locker
if err != nil {
return nil, err
}
lockfiles[filepath.Clean(path)] = locker
return locker, nil
return getLockfile(path, false)
}
// GetROLockfile opens a read-only lock file. The Locker object it returns
// will be returned unlocked.
// GetROLockfile opens a read-only lock file, creating it if necessary. The
// Locker object may already be locked if the path has already been requested
// by the current process.
func GetROLockfile(path string) (Locker, error) {
return getLockfile(path, true)
}
// getLockfile is a helper for GetLockfile and GetROLockfile and returns Locker
// based on the path and read-only property.
func getLockfile(path string, ro bool) (Locker, error) {
lockfilesLock.Lock()
defer lockfilesLock.Unlock()
if lockfiles == nil {
@@ -71,12 +68,15 @@ func GetROLockfile(path string) (Locker, error) {
}
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)
if ro && locker.IsReadWrite() {
return nil, errors.Errorf("lock %q is not a read-only lock", cleanPath)
}
if !ro && !locker.IsReadWrite() {
return nil, errors.Errorf("lock %q is not a read-write lock", cleanPath)
}
return locker, nil
}
locker, err := getLockFile(path, true) // platform dependent locker
locker, err := getLockFile(path, ro) // platform dependent locker
if err != nil {
return nil, err
}