From 89e07b875d2b62e4eb1b96c0f97b18848e6b41bc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 7 Jul 2013 17:44:13 -0700 Subject: [PATCH] post-processor/vagrant: properly close file handles [GH-100] --- CHANGELOG.md | 2 ++ post-processor/vagrant/util.go | 21 +++++++++++++++++++++ post-processor/vagrant/virtualbox.go | 15 ++------------- post-processor/vagrant/vmware.go | 15 ++------------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46c1c8a49..7218653b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ BUG FIXES: the output path. * vagrant: Properly configure the provider-specific post-processors so things like `vagrantfile_template` work. [GH-129] +* vagrant: Close filehandles when copying files so Windows can + rename files. [GH-100] ## 0.1.4 (July 2, 2013) diff --git a/post-processor/vagrant/util.go b/post-processor/vagrant/util.go index 5966008c5..b194c129b 100644 --- a/post-processor/vagrant/util.go +++ b/post-processor/vagrant/util.go @@ -21,6 +21,27 @@ type OutputPathTemplate struct { Provider string } +// Copies a file by copying the contents of the file to another place. +func CopyContents(dst, src string) error { + srcF, err := os.Open(src) + if err != nil { + return err + } + defer srcF.Close() + + dstF, err := os.Create(dst) + if err != nil { + return err + } + defer dstF.Close() + + if _, err := io.Copy(dstF, srcF); err != nil { + return err + } + + return nil +} + // DirToBox takes the directory and compresses it into a Vagrant-compatible // box. This function does not perform checks to verify that dir is // actually a proper box. This is an expected precondition. diff --git a/post-processor/vagrant/virtualbox.go b/post-processor/vagrant/virtualbox.go index fd8b7dfce..0539d0167 100644 --- a/post-processor/vagrant/virtualbox.go +++ b/post-processor/vagrant/virtualbox.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" - "io" "io/ioutil" "log" "os" @@ -66,19 +65,9 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac // Copy all of the original contents into the temporary directory for _, path := range artifact.Files() { ui.Message(fmt.Sprintf("Copying: %s", path)) - src, err := os.Open(path) - if err != nil { - return nil, false, err - } - defer src.Close() - dst, err := os.Create(filepath.Join(dir, filepath.Base(path))) - if err != nil { - return nil, false, err - } - defer dst.Close() - - if _, err := io.Copy(dst, src); err != nil { + dstPath := filepath.Join(dir, filepath.Base(path)) + if err := CopyContents(dstPath, path); err != nil { return nil, false, err } } diff --git a/post-processor/vagrant/vmware.go b/post-processor/vagrant/vmware.go index 211e6322c..471582026 100644 --- a/post-processor/vagrant/vmware.go +++ b/post-processor/vagrant/vmware.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" - "io" "io/ioutil" "os" "path/filepath" @@ -51,19 +50,9 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif // Copy all of the original contents into the temporary directory for _, path := range artifact.Files() { ui.Message(fmt.Sprintf("Copying: %s", path)) - src, err := os.Open(path) - if err != nil { - return nil, false, err - } - defer src.Close() - dst, err := os.Create(filepath.Join(dir, filepath.Base(path))) - if err != nil { - return nil, false, err - } - defer dst.Close() - - if _, err := io.Copy(dst, src); err != nil { + dstPath := filepath.Join(dir, filepath.Base(path)) + if err := CopyContents(dstPath, path); err != nil { return nil, false, err } }