diff options
| author | Petter Rasmussen | 2016-02-13 11:37:19 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-13 11:37:19 +0100 |
| commit | 46e9f195b46fcabbf1fa40e8cf96a66c425c4ddb (patch) | |
| tree | 3b727dd7bab19c73485fcebcadb43e73e9a9394c /drive | |
| parent | 12e431b5e17ff4ceb6680987bd4649a94b83f896 (diff) | |
| download | gdrive-46e9f195b46fcabbf1fa40e8cf96a66c425c4ddb.tar.bz2 | |
Download to tmp file and rename on success
Diffstat (limited to 'drive')
| -rw-r--r-- | drive/sync_download.go | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/drive/sync_download.go b/drive/sync_download.go index 9dcd719..ec496e7 100644 --- a/drive/sync_download.go +++ b/drive/sync_download.go @@ -196,7 +196,7 @@ func (self *Drive) downloadRemoteFile(id, fpath string, args DownloadSyncArgs, t if isBackendError(err) && try < MaxBackendErrorRetries { exponentialBackoffSleep(try) try++ - self.downloadRemoteFile(id, fpath, args, try) + return self.downloadRemoteFile(id, fpath, args, try) } else { return fmt.Errorf("Failed to download file: %s", err) } @@ -213,28 +213,34 @@ func (self *Drive) downloadRemoteFile(id, fpath string, args DownloadSyncArgs, t return err } + // Download to tmp file + tmpPath := fpath + ".incomplete" + // Create new file - outFile, err := os.Create(fpath) + outFile, err := os.Create(tmpPath) if err != nil { return fmt.Errorf("Unable to create local file: %s", err) } - // Close file on function exit - defer outFile.Close() - // Save file to disk _, err = io.Copy(outFile, srcReader) if err != nil { + outFile.Close() if try < MaxBackendErrorRetries { exponentialBackoffSleep(try) try++ - self.downloadRemoteFile(id, fpath, args, try) + return self.downloadRemoteFile(id, fpath, args, try) } else { + os.Remove(tmpPath) return fmt.Errorf("Download was interrupted: %s", err) } } - return nil + // Close file + outFile.Close() + + // Rename tmp file to proper filename + return os.Rename(tmpPath, fpath) } func (self *Drive) deleteExtraneousLocalFiles(files *syncFiles, args DownloadSyncArgs) error { |
