2013-06-09 22:01:28 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2013-06-10 02:06:47 -04:00
|
|
|
"encoding/hex"
|
2013-06-09 22:01:28 -04:00
|
|
|
"path/filepath"
|
2013-07-20 19:50:18 -04:00
|
|
|
"strings"
|
2013-06-09 22:01:28 -04:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache implements a caching interface where files can be stored for
|
|
|
|
// re-use between multiple runs.
|
|
|
|
type Cache interface {
|
|
|
|
// Lock takes a key and returns the path where the file can be written to.
|
|
|
|
// Packer guarantees that no other process will write to this file while
|
|
|
|
// the lock is held.
|
|
|
|
//
|
2013-07-20 00:19:05 -04:00
|
|
|
// If the key has an extension (e.g., file.ext), the resulting path
|
|
|
|
// will have that extension as well.
|
|
|
|
//
|
2013-06-09 22:01:28 -04:00
|
|
|
// The cache will block and wait for the lock.
|
2013-06-09 22:25:48 -04:00
|
|
|
Lock(string) string
|
2013-06-09 22:01:28 -04:00
|
|
|
|
|
|
|
// Unlock will unlock a certain cache key. Be very careful that this
|
|
|
|
// is only called once per lock obtained.
|
|
|
|
Unlock(string)
|
|
|
|
|
|
|
|
// RLock returns the path to a key in the cache and locks it for reading.
|
|
|
|
// The second return parameter is whether the key existed or not.
|
|
|
|
// This will block if any locks are held for writing. No lock will be
|
|
|
|
// held if the key doesn't exist.
|
|
|
|
RLock(string) (string, bool)
|
|
|
|
|
|
|
|
// RUnlock will unlock a key for reading.
|
|
|
|
RUnlock(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileCache implements a Cache by caching the data directly to a cache
|
|
|
|
// directory.
|
|
|
|
type FileCache struct {
|
|
|
|
CacheDir string
|
|
|
|
l sync.Mutex
|
|
|
|
rw map[string]*sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2013-06-09 22:25:48 -04:00
|
|
|
func (f *FileCache) Lock(key string) string {
|
2013-06-09 22:01:28 -04:00
|
|
|
hashKey := f.hashKey(key)
|
|
|
|
rw := f.rwLock(hashKey)
|
|
|
|
rw.Lock()
|
|
|
|
|
2013-07-20 00:15:14 -04:00
|
|
|
return f.cachePath(key, hashKey)
|
2013-06-09 22:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileCache) Unlock(key string) {
|
|
|
|
hashKey := f.hashKey(key)
|
|
|
|
rw := f.rwLock(hashKey)
|
|
|
|
rw.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileCache) RLock(key string) (string, bool) {
|
|
|
|
hashKey := f.hashKey(key)
|
|
|
|
rw := f.rwLock(hashKey)
|
|
|
|
rw.RLock()
|
|
|
|
|
2013-07-20 00:15:14 -04:00
|
|
|
return f.cachePath(key, hashKey), true
|
2013-06-09 22:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileCache) RUnlock(key string) {
|
|
|
|
hashKey := f.hashKey(key)
|
|
|
|
rw := f.rwLock(hashKey)
|
|
|
|
rw.RUnlock()
|
|
|
|
}
|
|
|
|
|
2013-07-20 00:15:14 -04:00
|
|
|
func (f *FileCache) cachePath(key string, hashKey string) string {
|
2013-07-20 00:19:05 -04:00
|
|
|
suffix := ""
|
2013-07-20 19:50:18 -04:00
|
|
|
dotIndex := strings.LastIndex(key, ".")
|
|
|
|
if dotIndex > -1 {
|
|
|
|
suffix = key[dotIndex:len(key)]
|
2013-07-20 19:53:55 -04:00
|
|
|
|
|
|
|
idx := strings.Index(suffix, "?")
|
|
|
|
if idx != -1 {
|
|
|
|
suffix = suffix[0:idx]
|
|
|
|
}
|
2013-07-20 00:19:05 -04:00
|
|
|
}
|
2013-07-20 19:50:18 -04:00
|
|
|
|
2013-07-20 00:19:05 -04:00
|
|
|
return filepath.Join(f.CacheDir, hashKey+suffix)
|
2013-07-20 00:15:14 -04:00
|
|
|
}
|
|
|
|
|
2013-06-09 22:01:28 -04:00
|
|
|
func (f *FileCache) hashKey(key string) string {
|
|
|
|
sha := sha256.New()
|
|
|
|
sha.Write([]byte(key))
|
2013-06-10 02:06:47 -04:00
|
|
|
return hex.EncodeToString(sha.Sum(nil))
|
2013-06-09 22:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileCache) rwLock(hashKey string) *sync.RWMutex {
|
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
|
|
|
|
if f.rw == nil {
|
|
|
|
f.rw = make(map[string]*sync.RWMutex)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, ok := f.rw[hashKey]; ok {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
var result sync.RWMutex
|
|
|
|
f.rw[hashKey] = &result
|
|
|
|
return &result
|
|
|
|
}
|