Skip to content

Commit

Permalink
Download to tmp file and rename on success
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 13, 2016
1 parent dd623e8 commit 03384c6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,23 @@ func (self *Drive) downloadBinary(f *drive.File, args DownloadArgs) error {
return err
}

// Download to tmp file
tmpPath := filename + ".incomplete"

// Create new file
outFile, err := os.Create(filename)
outFile, err := os.Create(tmpPath)
if err != nil {
return fmt.Errorf("Unable to create new file: %s", err)
}

// Close file on function exit
defer outFile.Close()

fmt.Fprintf(args.Out, "\nDownloading %s...\n", f.Name)
started := time.Now()

// Save file to disk
bytes, err := io.Copy(outFile, srcReader)
if err != nil {
outFile.Close()
os.Remove(tmpPath)
return fmt.Errorf("Failed saving file: %s", err)
}

Expand All @@ -99,7 +101,12 @@ func (self *Drive) downloadBinary(f *drive.File, args DownloadArgs) error {
//if deleteSourceFile {
// self.Delete(args.Id)
//}
return nil

// Close File
outFile.Close()

// Rename tmp file to proper filename
return os.Rename(tmpPath, filename)
}

func (self *Drive) downloadDirectory(parent *drive.File, args DownloadArgs) error {
Expand Down

0 comments on commit 03384c6

Please sign in to comment.