-
Notifications
You must be signed in to change notification settings - Fork 14
/
cache.go
135 lines (107 loc) · 2.86 KB
/
cache.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package nix
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/benchkram/errz"
"github.com/benchkram/bob/bob/global"
"github.com/benchkram/bob/pkg/file"
"github.com/benchkram/bob/pkg/filehash"
)
type Cache struct {
db map[string]string
f *os.File
path string
}
type CacheOption func(f *Cache)
// WithPath adds a custom file path which is used
// to store cache content on the filesystem.
func WithPath(path string) CacheOption {
return func(n *Cache) {
n.path = path
}
}
// NewCacheStore initialize a Nix cache store inside dir
func NewCacheStore(opts ...CacheOption) (_ *Cache, err error) {
defer errz.Recover(&err)
c := Cache{
db: make(map[string]string),
path: global.BobNixCacheFile,
}
for _, opt := range opts {
if opt == nil {
continue
}
opt(&c)
}
f, err := os.OpenFile(c.path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
errz.Fatal(err)
c.f = f
scanner := bufio.NewScanner(f)
for scanner.Scan() {
pair := strings.SplitN(scanner.Text(), ":", 2)
c.db[pair[0]] = pair[1]
}
if err := scanner.Err(); err != nil {
errz.Fatal(err)
}
return &c, nil
}
// Get value from cache by its key
// Additionally also checks if path exists on the system
func (c *Cache) Get(key string) (string, bool) {
path, ok := c.db[key]
// Assure path exists on the filesystem.
if ok && !file.Exists(path) {
return path, false
}
return path, ok
}
// Save dependency inside the cache with its corresponding store path
func (c *Cache) Save(key string, storePath string) (err error) {
defer errz.Recover(&err)
if _, err := c.f.Write([]byte(fmt.Sprintf("%s:%s\n", key, storePath))); err != nil {
_ = c.f.Close() // ignore error; Write error takes precedence
errz.Fatal(err)
}
c.db[key] = storePath
return nil
}
// Close closes the file used in cache
func (c *Cache) Close() error {
return c.f.Close()
}
func (c *Cache) Clean() (err error) {
defer errz.Recover(&err)
err = c.f.Truncate(0)
errz.Fatal(err)
c.db = make(map[string]string)
return nil
}
// GenerateKey generates key for the cache for a Dependency
//
// if it's a .nix file it will hash the nixpkgs + file contents
// if it's a package name will hash the packageName:nixpkgs content
func GenerateKey(dependency Dependency) (_ string, err error) {
defer errz.Recover(&err)
var h []byte
if strings.HasSuffix(dependency.Name, ".nix") {
aggregatedHashes := bytes.NewBuffer([]byte{})
h, err = filehash.HashBytes(bytes.NewBufferString(dependency.Nixpkgs))
errz.Fatal(err)
aggregatedHashes.Write(h)
fileHash, err := filehash.Hash(dependency.Name)
errz.Fatal(err)
aggregatedHashes.Write(fileHash)
h, err = filehash.HashBytes(aggregatedHashes)
errz.Fatal(err)
} else {
toHash := fmt.Sprintf("%s:%s", dependency.Name, dependency.Nixpkgs)
h, err = filehash.HashBytes(bytes.NewBufferString(toHash))
errz.Fatal(err)
}
return hex.EncodeToString(h), nil
}