From cfcd0a75dd3b194be30b5fc9206db9a3a5374662 Mon Sep 17 00:00:00 2001 From: Nathan LeClaire Date: Tue, 5 May 2015 14:11:03 -0700 Subject: [PATCH] Fix Windows ISO rename issue Signed-off-by: Nathan LeClaire --- utils/b2d.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/utils/b2d.go b/utils/b2d.go index 853880a8..461cdc02 100644 --- a/utils/b2d.go +++ b/utils/b2d.go @@ -94,6 +94,15 @@ func (b *B2dUtils) GetLatestBoot2DockerReleaseURL() (string, error) { return isoUrl, nil } +func removeFileIfExists(name string) error { + if _, err := os.Stat(name); err == nil { + if err := os.Remove(name); err != nil { + log.Fatalf("Error removing temporary download file: %s", err) + } + } + return nil +} + // Download boot2docker ISO image for the given tag and save it at dest. func (b *B2dUtils) DownloadISO(dir, file, isoUrl string) error { u, err := url.Parse(isoUrl) @@ -121,7 +130,11 @@ func (b *B2dUtils) DownloadISO(dir, file, isoUrl string) error { return err } - defer os.Remove(f.Name()) + defer func() { + if err := removeFileIfExists(f.Name()); err != nil { + log.Fatalf("Error removing file: %s", err) + } + }() if _, err := io.Copy(f, src); err != nil { // TODO: display download progress? @@ -132,7 +145,16 @@ func (b *B2dUtils) DownloadISO(dir, file, isoUrl string) error { return err } - if err := os.Rename(f.Name(), filepath.Join(dir, file)); err != nil { + // Dest is the final path of the boot2docker.iso file. + dest := filepath.Join(dir, file) + + // Windows can't rename in place, so remove the old file before + // renaming the temporary downloaded file. + if err := removeFileIfExists(dest); err != nil { + return err + } + + if err := os.Rename(f.Name(), dest); err != nil { return err }