-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashing.go
146 lines (121 loc) · 2.93 KB
/
hashing.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
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v3"
)
type HashStore interface {
// Add a hash to the HashStore.
Add(name, hash string) error
// Get a hash from the HashStore.
Get(name string) (string, error)
// Persist the hashes.
Save() error
}
const (
HashStrategyReadWrite = "readwrite"
HashStrategyRead = "read"
)
// An implementation of the HashStore that stores all hashes inside a single
// JSON file.
type JSONHashStore struct {
path string
hashes map[string]string
strategy string
}
func NewJSONHashStore(path, strategy string) (*JSONHashStore, error) {
hashes := make(map[string]string)
content, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else {
if err := json.Unmarshal(content, &hashes); err != nil {
// If the file is invalid JSON, instead of erroring out,
// just start from scratch so a new valid file is
// generated.
log.Printf("Unable to parse hashes from %s: %v\n", path, err)
}
}
hashes["//"] = "AUTO GENERATED. DO NOT EDIT."
return &JSONHashStore{
path: path,
hashes: hashes,
strategy: strategy,
}, nil
}
func (s *JSONHashStore) Add(name, hash string) error {
s.hashes[name] = hash
return nil
}
func (s *JSONHashStore) Get(name string) (string, error) {
return s.hashes[name], nil
}
func (s *JSONHashStore) Save() error {
if s.strategy == HashStrategyRead {
// Read-only mode, so don't write.
return nil
}
b, err := json.MarshalIndent(s.hashes, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.path, b, 0644)
}
type ChartHash struct {
Hash string `yaml:"hash"`
}
// An implementation of HashStore that stores hashes in a "hash.sum" file.
type SumFileStore struct {
path string
strategy string
}
func NewSumFileStore(path, strategy string) *SumFileStore {
return &SumFileStore{
path: path,
strategy: strategy,
}
}
func (s *SumFileStore) Add(name, hash string) error {
ch := ChartHash{
Hash: hash,
}
data, err := yaml.Marshal(&ch)
if err != nil {
return err
}
if s.strategy == HashStrategyRead {
// Read-only mode, don't write
return nil
}
return os.WriteFile(s.filepath(name), data, 0664)
}
func (s *SumFileStore) Get(name string) (string, error) {
filepath := s.filepath(name)
yfile, err := os.ReadFile(filepath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// This is fine to do since there are cases where there won't be a hash. e.g. root
return "", nil
}
return "", fmt.Errorf("error reading file hash from %s error: %w", filepath, err)
}
ch := ChartHash{}
err2 := yaml.Unmarshal(yfile, &ch)
if err2 != nil {
return "", fmt.Errorf("error unmarshaling hash %s error: %w", filepath, err2)
}
return ch.Hash, nil
}
func (s *SumFileStore) Save() error {
// Already written in Add
return nil
}
func (s *SumFileStore) filepath(name string) string {
return filepath.Join(s.path, name, "hash.sum")
}