From 38ae1a0ba9eeca963166bcfa83a69ab0cdcb2104 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 20 Jul 2013 19:20:29 -0700 Subject: [PATCH] packer: Properly handle ? in URLs in cache keys /cc @sit - Found another edge case --- packer/cache.go | 14 +++++++------- packer/cache_test.go | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packer/cache.go b/packer/cache.go index ae0c2ef6b..85a23e084 100644 --- a/packer/cache.go +++ b/packer/cache.go @@ -73,14 +73,14 @@ func (f *FileCache) RUnlock(key string) { func (f *FileCache) cachePath(key string, hashKey string) string { suffix := "" - dotIndex := strings.LastIndex(key, ".") - if dotIndex > -1 { - suffix = key[dotIndex:len(key)] + endIndex := strings.Index(key, "?") + if endIndex == -1 { + endIndex = len(key) + } - idx := strings.Index(suffix, "?") - if idx != -1 { - suffix = suffix[0:idx] - } + dotIndex := strings.LastIndex(key[0:endIndex], ".") + if dotIndex > -1 { + suffix = key[dotIndex:endIndex] } return filepath.Join(f.CacheDir, hashKey+suffix) diff --git a/packer/cache_test.go b/packer/cache_test.go index 2d76edee5..306226cf9 100644 --- a/packer/cache_test.go +++ b/packer/cache_test.go @@ -37,8 +37,8 @@ func TestFileCache(t *testing.T) { defer os.RemoveAll(cacheDir) cache := &FileCache{CacheDir: cacheDir} - path := cache.Lock("foo.ext?foo=bar") - defer cache.Unlock("foo.ext?foo=bar") + path := cache.Lock("foo.ext?foo=bar.foo") + defer cache.Unlock("foo.ext?foo=bar.foo") if !strings.HasSuffix(path, ".ext") { t.Fatalf("bad extension with question mark: %s", path) }