-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema_cache.go
More file actions
141 lines (111 loc) · 2.68 KB
/
schema_cache.go
File metadata and controls
141 lines (111 loc) · 2.68 KB
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
136
137
138
139
140
141
package eleconf
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
)
func NewSchemaCache() (*SchemaCache, error) {
userCache, err := os.UserCacheDir()
if err != nil {
return nil, fmt.Errorf("get user cache dir: %w", err)
}
cacheDir := filepath.Join(userCache, "eleconf", "schema_cache")
err = os.MkdirAll(cacheDir, 0o700)
if err != nil {
return nil, fmt.Errorf("create schema cache dir: %w", err)
}
return &SchemaCache{
cacheDir: cacheDir,
}, nil
}
type SchemaCache struct {
cacheDir string
}
type CacheEntry struct {
URL string
Hash string
}
func (sc *SchemaCache) Read(
assetURL string, logicalURL string, hash string,
) ([]byte, bool, error) {
path, err := urlToPath(sc.cacheDir, logicalURL)
if err != nil {
return nil, false, err
}
infoPath := path + ".info"
infoData, err := os.ReadFile(infoPath)
if errors.Is(err, os.ErrNotExist) {
return nil, false, nil
} else if err != nil {
return nil, false, fmt.Errorf("read info file: %w", err)
}
var ce CacheEntry
err = json.Unmarshal(infoData, &ce)
if err != nil {
return nil, false, fmt.Errorf("invalid info file: %w", err)
}
if ce.Hash != hash {
return nil, false, fmt.Errorf(
"hash mismatch: got %s expected %s",
ce.Hash, hash)
}
schemaData, err := os.ReadFile(path)
if err != nil {
return nil, false, fmt.Errorf("read schema file: %w", err)
}
sum := fmt.Sprintf("%x", sha256.Sum256(schemaData))
if sum != ce.Hash {
return nil, false, fmt.Errorf("schema file didn't match the stored or requested hash")
}
return schemaData, true, nil
}
func urlToPath(base string, logicalURL string) (string, error) {
pu, err := url.Parse(logicalURL)
if err != nil {
return "", fmt.Errorf("invalid asset URL: %w", err)
}
subpath := []string{
base,
pu.Scheme,
pu.Hostname(),
}
uPath := strings.Split(pu.Path, "/")
subpath = append(subpath, uPath...)
return filepath.Join(subpath...), nil
}
func (sc *SchemaCache) Store(
assetURL string, logicalURL string, hash string, data []byte,
) error {
path, err := urlToPath(sc.cacheDir, logicalURL)
if err != nil {
return err
}
dir := filepath.Dir(path)
err = os.MkdirAll(dir, 0o700)
if err != nil {
return fmt.Errorf("create entry directory: %w", err)
}
info := CacheEntry{
URL: assetURL,
Hash: hash,
}
infoPath := path + ".info"
infoData, err := json.MarshalIndent(info, "", " ")
if err != nil {
return fmt.Errorf("marshal info JSON: %w", err)
}
err = os.WriteFile(infoPath, infoData, 0o600)
if err != nil {
return fmt.Errorf("write info file: %w", err)
}
err = os.WriteFile(path, data, 0o600)
if err != nil {
return fmt.Errorf("write schema file: %w", err)
}
return nil
}