diff --git a/packer/cache.go b/packer/cache.go index 6c2ed0207..9d69f0c56 100644 --- a/packer/cache.go +++ b/packer/cache.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/hex" "path/filepath" + "regexp" "sync" ) @@ -14,6 +15,9 @@ type Cache interface { // Packer guarantees that no other process will write to this file while // the lock is held. // + // If the key has an extension (e.g., file.ext), the resulting path + // will have that extension as well. + // // The cache will block and wait for the lock. Lock(string) string @@ -68,7 +72,13 @@ func (f *FileCache) RUnlock(key string) { } func (f *FileCache) cachePath(key string, hashKey string) string { - return filepath.Join(f.CacheDir, hashKey) + var suffixPattern = regexp.MustCompile(`(\.\w+)$`) + matches := suffixPattern.FindStringSubmatch(key) + suffix := "" + if matches != nil { + suffix = matches[0] + } + return filepath.Join(f.CacheDir, hashKey+suffix) } func (f *FileCache) hashKey(key string) string { diff --git a/packer/cache_test.go b/packer/cache_test.go index 1be344ecc..5fe6f50d0 100644 --- a/packer/cache_test.go +++ b/packer/cache_test.go @@ -3,6 +3,7 @@ package packer import ( "io/ioutil" "os" + "strings" "testing" ) @@ -36,19 +37,23 @@ func TestFileCache(t *testing.T) { defer os.RemoveAll(cacheDir) cache := &FileCache{CacheDir: cacheDir} - path := cache.Lock("foo") + path := cache.Lock("foo.iso") + + if !strings.HasSuffix(path, ".iso") { + t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path) + } err = ioutil.WriteFile(path, []byte("data"), 0666) if err != nil { t.Fatalf("error writing: %s", err) } - cache.Unlock("foo") + cache.Unlock("foo.iso") - path, ok := cache.RLock("foo") + path, ok := cache.RLock("foo.iso") if !ok { t.Fatal("cache says key doesn't exist") } - defer cache.RUnlock("foo") + defer cache.RUnlock("foo.iso") data, err := ioutil.ReadFile(path) if err != nil {