-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
498 lines (417 loc) · 11.5 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime/debug"
"strings"
)
// An interface to a content addressable file store
// StoreID is a handle for an object within the store
type StoreID []byte
func (s StoreID) String() string {
return hex.EncodeToString(s)
}
// MarshalJSON marshals a StoreID to a json string
func (s StoreID) MarshalJSON() ([]byte, error) {
return []byte("\"" + s.String() + "\""), nil
}
// UnMarshalJSON attempts to unmarshal a json string as a storeid
func (s *StoreID) UnMarshalJSON(j []byte) error {
b, err := hex.DecodeString(string(j))
sid := StoreID(b)
s = &sid
return err
}
// ByID implements sorting for arrays of StoreID
type ByID []StoreID
func (a ByID) Len() int { return len(a) }
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByID) Less(i, j int) bool {
c := bytes.Compare(a[i], a[j])
return c < 0
}
// StoreIDFromString parses a string and returns the StoreID
// that it would represent
func StoreIDFromString(str string) (StoreID, error) {
b, err := hex.DecodeString(str)
return StoreID(b), err
}
// StoreWalker is used to enumerate a store. Given a StoreID as an
// argument, it returns all the StoreIDs that the contents of the
// input store item may refer to
type StoreWalker func(StoreID) []StoreID
// Storer is an interface to a content addressable blob store. Files
// can be written to it, and then accesed and referred to based on an
// ID representing the content of the written item.
type Storer interface {
Store() (StoreWriteCloser, error) // Write something to the store
CopyToStore(io.ReadCloser) (StoreID, error) // Write something to the store
Open(StoreID) (io.ReadCloser, error) // Open a file by id
Size(StoreID) (int64, error) // Open a file by id
Link(StoreID, ...string) error // Link a file id to a given location
UnLink(StoreID) error // UnLink a blob
EmptyFileID() StoreID // Return the StoreID for an 0 byte object
SetRef(name string, id StoreID) error // Set a reference
GetRef(name string) (StoreID, error) // Get a reference
DeleteRef(name string) error // Delete a reference
ListRefs() map[string]StoreID // Get a reference
ForEach(f func(StoreID)) // Call function for each ID
}
type sha1Store struct {
tempDir string
baseDir string
prefixDepth int
}
func (t *sha1Store) storeIDToPathName(id StoreID) (string, string, error) {
if len(id) != sha1.Size {
log.Printf("Invalid store ID(%v)", id)
debug.PrintStack()
return "", "", errors.New("invalid StoreID " + string(id))
}
idStr := id.String()
prefix := idStr[0:t.prefixDepth]
filePath := t.baseDir + "/"
for c := range prefix {
filePath = filePath + string(prefix[c]) + "/"
}
fileName := filePath + idStr
return fileName, filePath, nil
}
func (t *sha1Store) Store() (StoreWriteCloser, error) {
file, err := ioutil.TempFile(t.tempDir, "blob")
if err != nil {
return nil, err
}
h := sha1.New()
mwriter := io.MultiWriter(file, h)
doneChan := make(chan string)
completeChan := make(chan error)
writer := &hashedStoreWriter{
hasher: h,
writer: mwriter,
done: doneChan,
complete: completeChan,
}
go func() {
// We can't use defer to clean up the TempFile, as
// we must ensure it is deleted before we return success
// to the calling channel. Should probably rewrite this
extraLink := <-doneChan
err := file.Sync()
if err != nil {
err = errors.New("failed to sync blob, " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
err = file.Close()
if err != nil {
err = errors.New("failed to close blob, " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
id, err := writer.Identity()
if err != nil {
err = errors.New("failed to rewtrieve hash of blob, " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
name, path, err := t.storeIDToPathName(id)
if err != nil {
err = errors.New("Failed to translate id to path " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
err = os.MkdirAll(path, 0755)
if err != nil {
err = errors.New("Failed to create blob directory " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
_, err = os.Stat(name)
if err == nil {
os.Remove(file.Name())
} else {
err = os.Link(file.Name(), name)
if err != nil {
err = errors.New("Failed to link blob " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
}
if extraLink != "" {
srcInfo, _ := os.Stat(name)
targetInfo, _ := os.Stat(extraLink)
if !os.SameFile(srcInfo, targetInfo) {
err = os.Link(name, extraLink)
if err != nil {
err = errors.New("Failed to link blob " + err.Error())
os.Remove(file.Name())
writer.complete <- err
return
}
}
}
os.Remove(file.Name())
writer.complete <- err
}()
return writer, nil
}
func (t *sha1Store) CopyToStore(r io.ReadCloser) (id StoreID, err error) {
w, err := t.Store()
if err != nil {
return nil, errors.New("during copy to store, " + err.Error())
}
_, err = io.Copy(w, r)
if err != nil {
return nil, errors.New("during copy to store, " + err.Error())
}
err = r.Close()
if err != nil {
return nil, errors.New("during copy to store, " + err.Error())
}
err = w.Close()
if err != nil {
return nil, errors.New("during copy to store, " + err.Error())
}
return w.Identity()
}
func (t *sha1Store) Open(id StoreID) (reader io.ReadCloser, err error) {
name, _, err := t.storeIDToPathName(id)
if err != nil {
err = errors.New("Failed to translate id to path " + err.Error())
return nil, err
}
reader, err = os.Open(name)
return reader, err
}
func (t *sha1Store) Size(id StoreID) (size int64, err error) {
name, _, err := t.storeIDToPathName(id)
if err != nil {
err = errors.New("Failed to translate id to path " + err.Error())
return 0, err
}
info, err := os.Stat(name)
size = info.Size()
return size, err
}
func (t *sha1Store) Link(id StoreID, targets ...string) (err error) {
name, _, err := t.storeIDToPathName(id)
if err != nil {
err = errors.New("Failed to translate id to path " + err.Error())
return err
}
for t := range targets {
target := targets[t]
prefix := strings.LastIndex(target, "/")
if prefix > -1 {
linkDir := target[0 : prefix+1]
linkName := target[prefix+1:]
if linkName == "" {
return errors.New("reference name cannot end in /")
}
err = os.MkdirAll(linkDir, 0777)
if err != nil {
if os.IsExist(err) {
break
} else {
return err
}
}
}
linkerr := os.Link(name, targets[t])
if linkerr != nil {
if os.IsExist(linkerr) {
var f1, f2 os.FileInfo
f1, err = os.Stat(name)
f2, err = os.Stat(targets[t])
if os.SameFile(f1, f2) {
break
}
return errors.New("File associated with differet hashes")
}
return err
}
}
return err
}
func (t *sha1Store) UnLink(id StoreID) (err error) {
name, _, err := t.storeIDToPathName(id)
if err != nil {
err = errors.New("Failed to translate id to path " + err.Error())
return err
}
err = os.Remove(name)
return err
}
// Sha1Store creates a blob store that uses hex encoded sha1 strings of
// ingested blobs for IDs
func Sha1Store(
baseDir string, // Base directory of the persistant store
tempDir string, // Temporary directory for ingesting files
prefixDepth int, // How many chars to use for directory prefixes
) Storer {
store := &sha1Store{
tempDir: tempDir,
baseDir: baseDir,
prefixDepth: prefixDepth,
}
return store
}
func (t *sha1Store) EmptyFileID() StoreID {
hasher := sha1.New()
id := hasher.Sum(nil)
return id
}
func (t *sha1Store) SetRef(name string, id StoreID) error {
refsPath := t.baseDir + "/refs/"
refDir := refsPath
refLog, err := os.OpenFile(t.baseDir+"/reflog", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return errors.New("Could not open reflog, " + err.Error())
}
defer refLog.Close()
prefix := strings.LastIndex(name, "/")
if prefix > -1 {
refDir = refDir + name[0:prefix+1]
name = name[prefix+1:]
if name == "" {
return errors.New("reference name cannot end in /")
}
}
refFile := refDir + name + ".ref"
newRef := false
oldRef, err := ioutil.ReadFile(refFile)
if err != nil {
if !os.IsNotExist(err) {
return errors.New("Could not read old reference, " + err.Error())
}
newRef = true
} else {
newRef = false
}
err = os.MkdirAll(refDir, 0777)
if err != nil {
return err
}
if newRef {
_, err = refLog.WriteString(fmt.Sprintf("Create:%s:%s\n", refFile, id.String()))
} else {
_, err = refLog.WriteString(fmt.Sprintf("Update:%s:%s(%s)\n", refFile, id.String(), oldRef))
}
if err != nil {
return errors.New("Failed writing reflog, " + err.Error())
}
err = refLog.Sync()
if err != nil {
return errors.New("Failed syncing reflog, " + err.Error())
}
err = ioutil.WriteFile(refFile, []byte(id.String()), 0777)
return err
}
func (t *sha1Store) GetRef(name string) (StoreID, error) {
refsPath := t.baseDir + "/refs"
refFile := refsPath + "/" + name + ".ref"
f, err := os.Open(refFile)
if err != nil {
return nil, err
}
defer f.Close()
refStr, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
refid, err := StoreIDFromString(string(refStr))
return refid, err
}
func (t *sha1Store) DeleteRef(name string) error {
refsPath := t.baseDir + "/refs"
refFile := refsPath + "/" + name + ".ref"
return os.Remove(refFile)
}
func (t *sha1Store) ListRefs() map[string]StoreID {
refsPath := t.baseDir + "/refs"
refs := make(map[string]StoreID)
walker := func(path string, info os.FileInfo, err error) error {
var reterr error
if err != nil {
return err
}
if info.IsDir() {
return reterr
}
refname := strings.TrimSuffix(path[len(refsPath)+1:], ".ref")
id, _ := t.GetRef(refname)
refs[refname] = id
return reterr
}
filepath.Walk(refsPath, walker)
return refs
}
// StoreWriteCloser is used to arite a file to the file store.
// Write data, and call Close() when done. After calling Close,
// Identitiy will return the ID of the item in the store.
type StoreWriteCloser interface {
io.WriteCloser
CloseAndLink(string) error // Return the
Identity() (StoreID, error) // Return the
}
type hashedStoreWriter struct {
writer io.Writer
hasher hash.Hash
closed bool
done chan string // Indicate completion, pass a filename to create a link atomically
complete chan error
}
func (w *hashedStoreWriter) Write(p []byte) (n int, err error) {
if w.closed {
return 0, errors.New("attempt to write to closed file")
}
return w.writer.Write(p)
}
func (w *hashedStoreWriter) Close() (err error) {
return w.CloseAndLink("")
}
func (w *hashedStoreWriter) CloseAndLink(target string) error {
if w.closed {
return errors.New("attempt to close closed file")
}
w.closed = true
w.done <- target
return <-w.complete
}
func (w *hashedStoreWriter) Identity() (id StoreID, err error) {
if !w.closed {
return nil, errors.New("Identitty called before file storage was compete")
}
return StoreID(w.hasher.Sum(nil)), nil
}
func (t *sha1Store) ForEach(fn func(StoreID)) {
walker := func(path string, info os.FileInfo, err error) error {
var reterr error
if info.IsDir() {
return reterr
}
id, _ := StoreIDFromString(info.Name())
if len(id) != 0 {
fn(id)
}
return reterr
}
filepath.Walk(t.baseDir, walker)
return
}