-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
258 lines (207 loc) · 5.93 KB
/
store.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
)
const storeFileName = ".sto"
type store struct {
rootPath string
Entries map[string]link
}
func initStore(rootPath string) (store, error) {
stat, err := os.Stat(rootPath)
if errors.Is(err, os.ErrNotExist) {
return store{}, pathNotExistError(rootPath)
}
if err != nil {
return store{}, fmt.Errorf("reading stat %q: %v", rootPath, err)
}
if !stat.IsDir() {
return store{}, notDirectoryError(rootPath)
}
storeFilePath := fmt.Sprintf("%s/%s", rootPath, storeFileName)
_, err = os.Stat(storeFilePath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return store{}, fmt.Errorf("reading stat %q: %v", storeFilePath, err)
}
if err == nil {
return store{}, storeAlreadyExistsError(rootPath)
}
f, err := os.Create(storeFilePath)
if err != nil {
return store{}, fmt.Errorf("creating store file %q: %v", storeFilePath, err)
}
if err := f.Close(); err != nil {
return store{}, fmt.Errorf("closing store file %q: %v", storeFilePath, err)
}
rootPath = formatDirPath(rootPath)
return store{rootPath: rootPath, Entries: make(map[string]link)}, nil
}
func openStore(rootPath string) (s store, err error) {
s.rootPath = formatDirPath(rootPath)
storeFilePath := fmt.Sprintf("%s/%s", rootPath, storeFileName)
stat, err := os.Stat(storeFilePath)
if errors.Is(err, os.ErrNotExist) {
return store{}, storeNotExistError(rootPath)
}
if err != nil {
return store{}, fmt.Errorf("reading stat %q: %v", storeFilePath, err)
}
if stat.Size() == 0 {
s.Entries = make(map[string]link)
return s, err
}
f, err := os.Open(storeFilePath)
if err != nil {
return store{}, fmt.Errorf("opening store file %q: %v", storeFilePath, err)
}
defer func() {
if closeErr := f.Close(); closeErr != nil {
err = fmt.Errorf("closing store file %q: %v", storeFilePath, closeErr)
}
}()
dec := json.NewDecoder(f)
if err := dec.Decode(&s); err != nil {
return store{}, fmt.Errorf("reading store file %q: %v", storeFilePath, err)
}
return s, err
}
func (s *store) entries() ([]link, error) {
var entries []link
for _, entry := range s.Entries {
externalEntry, err := fromInternalEntry(entry, s.rootPath)
if err != nil {
return nil, fmt.Errorf("converting internal entry %+v: %v", entry, err)
}
entries = append(entries, externalEntry)
}
return entries, nil
}
func (s *store) add(l link) error {
if err := s.validateEntry(l); err != nil {
return err
}
internalEntry, err := fromExternalEntry(l, s.rootPath)
if err != nil {
return fmt.Errorf("converting external entry %+v: %v", l, err)
}
if internalEntry.Name == "" {
internalEntry.Name = internalEntry.SourcePath
}
if exisitingEntry, ok := s.Entries[internalEntry.Name]; ok {
if exisitingEntry != internalEntry {
return entryExistError(internalEntry.Name)
}
return nil
}
s.Entries[internalEntry.Name] = internalEntry
if err := s.persist(); err != nil {
return fmt.Errorf("persisting store: %v", err)
}
return nil
}
func (s *store) get(name string) (link, bool, error) {
entry, ok := s.Entries[name]
if !ok {
return link{}, false, nil
}
externalEntry, err := fromInternalEntry(entry, s.rootPath)
if err != nil {
return link{}, false, fmt.Errorf("converting internal entry %+v: %v", entry, err)
}
return externalEntry, true, nil
}
func (s *store) update(name string, l link) error {
if err := s.validateEntry(l); err != nil {
return err
}
if _, ok := s.Entries[name]; !ok {
return entryNotExistError(name)
}
internalEntry, err := fromExternalEntry(l, s.rootPath)
if err != nil {
return fmt.Errorf("converting external entry %+v: %v", l, err)
}
if internalEntry.Name != name {
delete(s.Entries, name)
name = internalEntry.Name
}
s.Entries[name] = internalEntry
if err := s.persist(); err != nil {
return fmt.Errorf("persisting store: %v", err)
}
return nil
}
func (s *store) persist() (err error) {
storeFilePath := fmt.Sprintf("%s/%s", s.rootPath, storeFileName)
f, err := os.OpenFile(storeFilePath, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("opening store file %q: %v", storeFilePath, err)
}
defer func() {
if closeErr := f.Close(); closeErr != nil {
err = fmt.Errorf("closing store file %q: %v", storeFilePath, closeErr)
}
}()
enc := json.NewEncoder(f)
if err := enc.Encode(s); err != nil {
return fmt.Errorf("writing to store file %q: %v", storeFilePath, err)
}
return err
}
func (s *store) validateEntry(l link) error {
if l.DestinationPath == "" {
return errors.New("DestinationPath empty")
}
if l.SourcePath == "" {
return errors.New("SourcePath empty")
}
if !strings.HasPrefix(l.SourcePath, s.rootPath) {
return sourceOutsideRootError{
rootPath: s.rootPath,
sourcePath: l.SourcePath,
}
}
return nil
}
func formatDirPath(path string) string {
if len(path) == 0 || path[len(path)-1] != '/' {
return fmt.Sprintf("%s/", path)
}
return path
}
type pathNotExistError string
func (e pathNotExistError) Error() string {
return fmt.Sprintf("path %q does not exist", string(e))
}
type notDirectoryError string
func (e notDirectoryError) Error() string {
return fmt.Sprintf("%q is not a directory", string(e))
}
type storeAlreadyExistsError string
func (e storeAlreadyExistsError) Error() string {
return fmt.Sprintf("store at %q already exists", string(e))
}
type sourceOutsideRootError struct {
rootPath string
sourcePath string
}
func (e sourceOutsideRootError) Error() string {
e.rootPath = formatDirPath(e.rootPath)
return fmt.Sprintf("source %q is outside of root %q", e.sourcePath, e.rootPath)
}
type storeNotExistError string
func (e storeNotExistError) Error() string {
return fmt.Sprintf("store at %q does not exist", string(e))
}
type entryExistError string
func (e entryExistError) Error() string {
return fmt.Sprintf("entry with name %q already exists", string(e))
}
type entryNotExistError string
func (e entryNotExistError) Error() string {
return fmt.Sprintf("entry with name %q not found", string(e))
}