From d8a9719d81f50ac941a67285e29c64202056cf7e Mon Sep 17 00:00:00 2001 From: Petter Rasmussen Date: Sat, 6 Feb 2016 21:53:34 +0100 Subject: Write to temp file first --- auth/token.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'auth') 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) } -- cgit v1.2.3