core: cache makes proper path with slashes after "." [GH-716]

This commit is contained in:
Mitchell Hashimoto 2013-12-19 18:25:01 -08:00
parent 74f018df2b
commit 629f3eee21
3 changed files with 20 additions and 7 deletions

View File

@ -46,6 +46,8 @@ BUG FIXES:
* core: No colored output in machine-readable output. [GH-684]
* core: User variables can now be used for non-string fields. [GH-598]
* core: Fix bad download paths if the download URL contained a "."
before a "/" [GH-716]
* builder/virtualbox: don't download guest additions if disabled. [GH-731]
* post-processor/vsphere: Uploads VM properly. [GH-694]
* post-processor/vsphere: Process user variables.

View File

@ -72,15 +72,16 @@ func (f *FileCache) RUnlock(key string) {
}
func (f *FileCache) cachePath(key string, hashKey string) string {
suffix := ""
endIndex := strings.Index(key, "?")
if endIndex == -1 {
endIndex = len(key)
if endIndex := strings.Index(key, "?"); endIndex > -1 {
key = key[:endIndex]
}
dotIndex := strings.LastIndex(key[0:endIndex], ".")
suffix := ""
dotIndex := strings.LastIndex(key, ".")
if dotIndex > -1 {
suffix = key[dotIndex:endIndex]
if slashIndex := strings.LastIndex(key, "/"); slashIndex <= dotIndex {
suffix = key[dotIndex:]
}
}
return filepath.Join(f.CacheDir, hashKey+suffix)

View File

@ -37,12 +37,22 @@ func TestFileCache(t *testing.T) {
defer os.RemoveAll(cacheDir)
cache := &FileCache{CacheDir: cacheDir}
path := cache.Lock("foo.ext?foo=bar.foo")
// Test path with no extension (GH-716)
path := cache.Lock("/foo.bar/baz")
defer cache.Unlock("/foo.bar/baz")
if strings.Contains(path, ".bar") {
t.Fatalf("bad: %s", path)
}
// Test paths with a ?
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)
}
// Test normal paths
path = cache.Lock("foo.iso")
if !strings.HasSuffix(path, ".iso") {
t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path)