From 26008c3c919206a0bd0b6ce6dd0c2695336cbade Mon Sep 17 00:00:00 2001 From: Eric Promislow Date: Fri, 18 Jun 2021 12:27:18 -0700 Subject: [PATCH] Copy the temp file to dest only if rename fails --- internal/downloader/download.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/downloader/download.go b/internal/downloader/download.go index 4665687..db47470 100644 --- a/internal/downloader/download.go +++ b/internal/downloader/download.go @@ -185,10 +185,20 @@ func (d *Downloder) download(desc, urlToGet, destination string, mode os.FileMod return &common.ShaMismatchError{Url: urlToGet, ShaExpected: shaExpected, ShaActual: shaActual} } - tempInput, err := ioutil.ReadFile(temporaryDestinationFile.Name()) + err = os.Rename(temporaryDestinationFile.Name(), destination) if err != nil { - return fmt.Errorf("Error reading temporary file %s: %v", - temporaryDestinationFile.Name(), err) + linkErr, ok := err.(*os.LinkError) + if ok { + fmt.Fprintf(os.Stderr, "Cross-device error trying to rename a file: %s -- will do a full copy\n", linkErr) + tempInput, err := ioutil.ReadFile(temporaryDestinationFile.Name()) + if err != nil { + return fmt.Errorf("Error reading temporary file %s: %v", + temporaryDestinationFile.Name(), err) + } + err = ioutil.WriteFile(destination, tempInput, mode) + } + } else { + err = os.Chmod(destination, mode) } - return ioutil.WriteFile(destination, tempInput, mode) + return err }