From 9ef50487fb98c2cd07bd3dcf9b738a9e8a9bd277 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 26 Apr 2014 20:33:12 -0700 Subject: [PATCH] core: Create cache directory only when needed [GH-367] --- CHANGELOG.md | 1 + packer.go | 5 ----- packer/cache.go | 9 +++++++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9131b4d2..d1fb43056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ BUG FIXES: * core: Errors are properly shown when adding bad floppy files. [GH-1043] * core: Fix some URL parsing issues on Windows. + * core: Create Cache directory only when it is needed. [GH-367] * builder/amazon-instance: Use S3Endpoint for ec2-upload-bundle arg, which works for every region. [GH-904] * builder/digitalocean: updated default image_id [GH-1032] diff --git a/packer.go b/packer.go index d645df45e..0674e503b 100644 --- a/packer.go +++ b/packer.go @@ -106,11 +106,6 @@ func wrappedMain() int { return 1 } - if err := os.MkdirAll(cacheDir, 0755); err != nil { - fmt.Fprintf(os.Stderr, "Error preparing cache directory: \n\n%s\n", err) - return 1 - } - log.Printf("Setting cache directory: %s", cacheDir) cache := &packer.FileCache{CacheDir: cacheDir} diff --git a/packer/cache.go b/packer/cache.go index a9bd4a62b..d65b5b806 100644 --- a/packer/cache.go +++ b/packer/cache.go @@ -3,6 +3,8 @@ package packer import ( "crypto/sha256" "encoding/hex" + "log" + "os" "path/filepath" "strings" "sync" @@ -84,6 +86,13 @@ func (f *FileCache) cachePath(key string, hashKey string) string { } } + // Make the cache directory. We ignore errors here, but + // log them in case something happens. + if err := os.MkdirAll(f.CacheDir, 0755); err != nil { + log.Printf( + "[ERR] Error making cacheDir: %s %s", f.CacheDir, err) + } + return filepath.Join(f.CacheDir, hashKey+suffix) }