diff --git a/builder/common/config.go b/builder/common/config.go index 16df8c257..1eab19375 100644 --- a/builder/common/config.go +++ b/builder/common/config.go @@ -2,6 +2,7 @@ package common import ( "fmt" + "path/filepath" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" "net/url" @@ -75,6 +76,18 @@ func DownloadableURL(original string) (string, error) { if _, err := os.Stat(url.Path); err != nil { return "", err } + + url.Path, err = filepath.Abs(url.Path) + if err != nil { + return "", err + } + + url.Path, err = filepath.EvalSymlinks(url.Path) + if err != nil { + return "", err + } + + url.Path = filepath.Clean(url.Path) } // Make sure it is lowercased diff --git a/builder/common/config_test.go b/builder/common/config_test.go index 8a6517250..2a337f5c6 100644 --- a/builder/common/config_test.go +++ b/builder/common/config_test.go @@ -98,6 +98,13 @@ func TestDownloadableURL_FilePaths(t *testing.T) { defer os.Remove(tf.Name()) tf.Close() + tfPath, err := filepath.EvalSymlinks(tf.Name()) + if err != nil { + t.Fatalf("tempfile err: %s", err) + } + + tfPath = filepath.Clean(tfPath) + // Relative filepath. We run this test in a func so that // the defers run right away. func() { @@ -106,19 +113,19 @@ func TestDownloadableURL_FilePaths(t *testing.T) { t.Fatalf("getwd err: %s", err) } - err = os.Chdir(filepath.Dir(tf.Name())) + err = os.Chdir(filepath.Dir(tfPath)) if err != nil { t.Fatalf("chdir err: %s", err) } defer os.Chdir(wd) - filename := filepath.Base(tf.Name()) + filename := filepath.Base(tfPath) u, err := DownloadableURL(filename) if err != nil { t.Fatalf("err: %s", err) } - if u != fmt.Sprintf("file:///%s", filename) { + if u != fmt.Sprintf("file://%s", tfPath) { t.Fatalf("unexpected: %s", u) } }() @@ -132,12 +139,12 @@ func TestDownloadableURL_FilePaths(t *testing.T) { } // Good file - u, err := DownloadableURL(prefix + tf.Name()) + u, err := DownloadableURL(prefix + tfPath) if err != nil { t.Fatalf("err: %s", err) } - if u != fmt.Sprintf("file://%s", tf.Name()) { + if u != fmt.Sprintf("file://%s", tfPath) { t.Fatalf("unexpected: %s", u) } }