forked from iopipe/turtle-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_path.go
66 lines (54 loc) · 1.17 KB
/
cache_path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"github.com/Sirupsen/logrus"
"fmt"
"os"
"path"
"crypto/sha256"
"io/ioutil"
"os/user"
)
func getCachePath(name string) (string, error) {
myuser, err := user.Current()
if err != nil {
return "", err
}
pathParts := []string{myuser.HomeDir, ".iopipe", "filter_cache", name}
return path.Join(pathParts...), nil
}
func ensureCachePath() error {
path, err := getCachePath("")
if err != nil {
return err
}
return os.MkdirAll(path, 0700)
}
func readFilterCache(name string) ([]byte, error) {
var err error
diskPath, err := getCachePath(name)
if err != nil {
return nil, err
}
/* Do we have this cached? */
if _, err = os.Stat(diskPath); err != nil {
return nil, err
}
script, err := ioutil.ReadFile(diskPath)
logrus.Debug("Read filter from cache:\n" + string(script[:]))
return script[:], nil
}
func writeFilterCache(body []byte) (string, error) {
var err error
/* Verify digest */
chksum := sha256.Sum256(body[:])
id := fmt.Sprintf("%x", chksum)
diskPath, err := getCachePath(id)
if err != nil {
return id, err
}
/* Write cache */
if err = ioutil.WriteFile(diskPath, body, 0600); err != nil {
return id, err
}
return id, nil
}