diff options
| author | Petter Rasmussen | 2016-02-13 18:28:33 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-13 18:28:33 +0100 |
| commit | 03384c697930351faae77ab6e750a67ce501641d (patch) | |
| tree | 6ec42087f35565bdaadbc1674c3fc777094d53fe /drive/download.go | |
| parent | dd623e82015df217a84cffd6592c379aad3b4c29 (diff) | |
| download | gdrive-03384c697930351faae77ab6e750a67ce501641d.tar.bz2 | |
Download to tmp file and rename on success
Diffstat (limited to 'drive/download.go')
| -rw-r--r-- | drive/download.go | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/drive/download.go b/drive/download.go index 8477fd7..29af98e 100644 --- a/drive/download.go +++ b/drive/download.go @@ -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) } @@ -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 { |
