aboutsummaryrefslogtreecommitdiffstats
path: root/util.go
diff options
context:
space:
mode:
authorPetter Rasmussen2016-02-06 19:33:42 +0100
committerPetter Rasmussen2016-02-06 21:34:35 +0100
commit69fb273d2f99604aa3fb80e88fb58f48fc1998c0 (patch)
tree1579e3ca79517c5c92e619e22f21a0eedc23eb4b /util.go
parentb7e45b080ffe13d286be65ce396da47b36a16944 (diff)
downloadgdrive-69fb273d2f99604aa3fb80e88fb58f48fc1998c0.tar.bz2
CachedMd5Comparer
Diffstat (limited to 'util.go')
-rw-r--r--util.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/util.go b/util.go
index d40d33e..041daed 100644
--- a/util.go
+++ b/util.go
@@ -4,7 +4,10 @@ import (
"runtime"
"path/filepath"
"fmt"
+ "encoding/json"
"os"
+ "io"
+ "crypto/md5"
)
func GetDefaultConfigDir() string {
@@ -56,3 +59,32 @@ func checkErr(err error) {
os.Exit(1)
}
}
+
+func writeJson(path string, data interface{}) error {
+ tmpFile := path + ".tmp"
+ f, err := os.Create(tmpFile)
+ if err != nil {
+ return err
+ }
+
+ err = json.NewEncoder(f).Encode(data)
+ f.Close()
+ if err != nil {
+ os.Remove(tmpFile)
+ return err
+ }
+
+ return os.Rename(tmpFile, path)
+}
+
+func md5sum(path string) string {
+ h := md5.New()
+ f, err := os.Open(path)
+ if err != nil {
+ return ""
+ }
+ defer f.Close()
+
+ io.Copy(h, f)
+ return fmt.Sprintf("%x", h.Sum(nil))
+}