aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetter Rasmussen2016-02-06 21:53:34 +0100
committerPetter Rasmussen2016-02-06 21:53:34 +0100
commitd8a9719d81f50ac941a67285e29c64202056cf7e (patch)
tree4a70a71b19e733c6077395f909aa19535963e860
parent69fb273d2f99604aa3fb80e88fb58f48fc1998c0 (diff)
downloadgdrive-d8a9719d81f50ac941a67285e29c64202056cf7e.tar.bz2
Write to temp file first
-rw-r--r--auth/token.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/auth/token.go b/auth/token.go
index 926d9f6..1c1150b 100644
--- a/auth/token.go
+++ b/auth/token.go
@@ -3,6 +3,7 @@ package auth
import (
"golang.org/x/oauth2"
"encoding/json"
+ "os"
"io/ioutil"
)
@@ -53,5 +54,15 @@ func SaveToken(path string, token *oauth2.Token) error {
if err = mkdir(path); err != nil {
return err
}
- return ioutil.WriteFile(path, data, 0600)
+
+ // Write to temp file first
+ tmpFile := path + ".tmp"
+ err = ioutil.WriteFile(tmpFile, data, 0600)
+ if err != nil {
+ os.Remove(tmpFile)
+ return err
+ }
+
+ // Move file to correct path
+ return os.Rename(tmpFile, path)
}