aboutsummaryrefslogtreecommitdiffstats
path: root/drive
diff options
context:
space:
mode:
authorPetter Rasmussen2016-02-13 18:28:33 +0100
committerPetter Rasmussen2016-02-13 18:28:33 +0100
commit03384c697930351faae77ab6e750a67ce501641d (patch)
tree6ec42087f35565bdaadbc1674c3fc777094d53fe /drive
parentdd623e82015df217a84cffd6592c379aad3b4c29 (diff)
downloadgdrive-03384c697930351faae77ab6e750a67ce501641d.tar.bz2
Download to tmp file and rename on success
Diffstat (limited to 'drive')
-rw-r--r--drive/download.go17
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 {