forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_disk_store.go
547 lines (463 loc) · 13.6 KB
/
block_disk_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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"path/filepath"
"strings"
"github.com/keybase/go-codec/codec"
"github.com/keybase/kbfs/ioutil"
"github.com/keybase/kbfs/kbfsblock"
"github.com/keybase/kbfs/kbfscodec"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/pkg/errors"
)
// blockDiskStore stores block data in flat files on disk.
//
// The directory layout looks like:
//
// dir/0100/0...01/data
// dir/0100/0...01/id
// dir/0100/0...01/ksh
// dir/0100/0...01/refs
// ...
// dir/01cc/5...55/id
// dir/01cc/5...55/refs
// ...
// dir/01dd/6...66/data
// dir/01dd/6...66/id
// dir/01dd/6...66/ksh
// ...
// dir/01ff/f...ff/data
// dir/01ff/f...ff/id
// dir/01ff/f...ff/ksh
// dir/01ff/f...ff/refs
//
// Each block has its own subdirectory with its ID truncated to 17
// bytes (34 characters) as a name. The block subdirectories are
// splayed over (# of possible hash types) * 256 subdirectories -- one
// byte for the hash type (currently only one) plus the first byte of
// the hash data -- using the first four characters of the name to
// keep the number of directories in dir itself to a manageable
// number, similar to git.
//
// Each block directory has the following files:
//
// - id: The full block ID in binary format. Always present.
// - data: The raw block data that should hash to the block ID.
// May be missing.
// - ksh: The raw data for the associated key server half.
// May be missing, but should be present when data is.
// - refs: The list of references to the block, along with other
// block-specific info, encoded as a serialized
// blockJournalInfo. May be missing. TODO: rename this to
// something more generic if we ever upgrade the journal
// version.
//
// Future versions of the disk store might add more files to this
// directory; if any code is written to move blocks around, it should
// be careful to preserve any unknown files in a block directory.
//
// The maximum number of characters added to the root dir by a block
// disk store is 44:
//
// /01ff/f...(30 characters total)...ff/data
//
// blockDiskStore is not goroutine-safe, so any code that uses it must
// guarantee that only one goroutine at a time calls its functions.
type blockDiskStore struct {
codec kbfscodec.Codec
dir string
}
// filesPerBlockMax is an upper bound for the number of files
// (including directories) to store one block: 4 for the regular
// files, 2 for the (splayed) directories, and 1 for the journal
// entry.
const filesPerBlockMax = 7
// makeBlockDiskStore returns a new blockDiskStore for the given
// directory.
func makeBlockDiskStore(codec kbfscodec.Codec, dir string) *blockDiskStore {
return &blockDiskStore{
codec: codec,
dir: dir,
}
}
// The functions below are for building various paths.
func (s *blockDiskStore) blockPath(id kbfsblock.ID) string {
// Truncate to 34 characters, which corresponds to 16 random
// bytes (since the first byte is a hash type) or 128 random
// bits, which means that the expected number of blocks
// generated before getting a path collision is 2^64 (see
// https://en.wikipedia.org/wiki/Birthday_problem#Cast_as_a_collision_problem
// ).
idStr := id.String()
return filepath.Join(s.dir, idStr[:4], idStr[4:34])
}
func (s *blockDiskStore) dataPath(id kbfsblock.ID) string {
return filepath.Join(s.blockPath(id), "data")
}
const idFilename = "id"
func (s *blockDiskStore) idPath(id kbfsblock.ID) string {
return filepath.Join(s.blockPath(id), idFilename)
}
func (s *blockDiskStore) keyServerHalfPath(id kbfsblock.ID) string {
return filepath.Join(s.blockPath(id), "ksh")
}
func (s *blockDiskStore) infoPath(id kbfsblock.ID) string {
// TODO: change the file name to "info" the next we change the
// journal layout.
return filepath.Join(s.blockPath(id), "refs")
}
// makeDir makes the directory for the given block ID and writes the
// ID file, if necessary.
func (s *blockDiskStore) makeDir(id kbfsblock.ID) error {
err := ioutil.MkdirAll(s.blockPath(id), 0700)
if err != nil {
return err
}
// TODO: Only write if the file doesn't exist.
return ioutil.WriteFile(s.idPath(id), []byte(id.String()), 0600)
}
// blockJournalInfo contains info about a particular block in the
// journal, such as the set of references to it.
type blockJournalInfo struct {
Refs blockRefMap
Flushed bool `codec:"f,omitempty"`
codec.UnknownFieldSetHandler
}
// TODO: Add caching for refs
// getRefInfo returns the references for the given ID.
func (s *blockDiskStore) getInfo(id kbfsblock.ID) (blockJournalInfo, error) {
var info blockJournalInfo
err := kbfscodec.DeserializeFromFile(s.codec, s.infoPath(id), &info)
if !ioutil.IsNotExist(err) && err != nil {
return blockJournalInfo{}, err
}
if info.Refs == nil {
info.Refs = make(blockRefMap)
}
return info, nil
}
// putRefInfo stores the given references for the given ID.
func (s *blockDiskStore) putInfo(id kbfsblock.ID, info blockJournalInfo) error {
return kbfscodec.SerializeToFile(s.codec, info, s.infoPath(id))
}
// addRefs adds references for the given contexts to the given ID, all
// with the same status and tag.
func (s *blockDiskStore) addRefs(id kbfsblock.ID, contexts []kbfsblock.Context,
status blockRefStatus, tag string) error {
info, err := s.getInfo(id)
if err != nil {
return err
}
if len(info.Refs) > 0 {
// Check existing contexts, if any.
for _, context := range contexts {
_, err := info.Refs.checkExists(context)
if err != nil {
return err
}
}
}
for _, context := range contexts {
err = info.Refs.put(context, status, tag)
if err != nil {
return err
}
}
return s.putInfo(id, info)
}
// getData returns the data and server half for the given ID, if
// present.
func (s *blockDiskStore) getData(id kbfsblock.ID) (
[]byte, kbfscrypto.BlockCryptKeyServerHalf, error) {
data, err := ioutil.ReadFile(s.dataPath(id))
if ioutil.IsNotExist(err) {
return nil, kbfscrypto.BlockCryptKeyServerHalf{},
blockNonExistentError{id}
} else if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
keyServerHalfPath := s.keyServerHalfPath(id)
buf, err := ioutil.ReadFile(keyServerHalfPath)
if ioutil.IsNotExist(err) {
return nil, kbfscrypto.BlockCryptKeyServerHalf{},
blockNonExistentError{id}
} else if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
// Check integrity.
err = kbfsblock.VerifyID(data, id)
if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
var serverHalf kbfscrypto.BlockCryptKeyServerHalf
err = serverHalf.UnmarshalBinary(buf)
if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
return data, serverHalf, nil
}
// All functions below are public functions.
func (s *blockDiskStore) hasAnyRef(id kbfsblock.ID) (bool, error) {
info, err := s.getInfo(id)
if err != nil {
return false, err
}
return len(info.Refs) > 0, nil
}
func (s *blockDiskStore) hasNonArchivedRef(id kbfsblock.ID) (bool, error) {
info, err := s.getInfo(id)
if err != nil {
return false, err
}
return info.Refs.hasNonArchivedRef(), nil
}
func (s *blockDiskStore) hasContext(id kbfsblock.ID, context kbfsblock.Context) (
bool, error) {
info, err := s.getInfo(id)
if err != nil {
return false, err
}
return info.Refs.checkExists(context)
}
func (s *blockDiskStore) hasData(id kbfsblock.ID) (bool, error) {
_, err := ioutil.Stat(s.dataPath(id))
if ioutil.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
func (s *blockDiskStore) isUnflushed(id kbfsblock.ID) (bool, error) {
ok, err := s.hasData(id)
if err != nil {
return false, err
}
if !ok {
return false, nil
}
// The data is there; has it been flushed?
info, err := s.getInfo(id)
if err != nil {
return false, err
}
return !info.Flushed, nil
}
func (s *blockDiskStore) markFlushed(id kbfsblock.ID) error {
info, err := s.getInfo(id)
if err != nil {
return err
}
info.Flushed = true
return s.putInfo(id, info)
}
func (s *blockDiskStore) getDataSize(id kbfsblock.ID) (int64, error) {
fi, err := ioutil.Stat(s.dataPath(id))
if ioutil.IsNotExist(err) {
return 0, nil
} else if err != nil {
return 0, err
}
return fi.Size(), nil
}
func (s *blockDiskStore) getDataWithContext(id kbfsblock.ID, context kbfsblock.Context) (
[]byte, kbfscrypto.BlockCryptKeyServerHalf, error) {
hasContext, err := s.hasContext(id, context)
if err != nil {
return nil, kbfscrypto.BlockCryptKeyServerHalf{}, err
}
if !hasContext {
return nil, kbfscrypto.BlockCryptKeyServerHalf{},
blockNonExistentError{id}
}
return s.getData(id)
}
func (s *blockDiskStore) getAllRefsForTest() (map[kbfsblock.ID]blockRefMap, error) {
res := make(map[kbfsblock.ID]blockRefMap)
fileInfos, err := ioutil.ReadDir(s.dir)
if ioutil.IsNotExist(err) {
return res, nil
} else if err != nil {
return nil, err
}
for _, fi := range fileInfos {
name := fi.Name()
if !fi.IsDir() {
return nil, errors.Errorf("Unexpected non-dir %q", name)
}
subFileInfos, err := ioutil.ReadDir(filepath.Join(s.dir, name))
if err != nil {
return nil, err
}
for _, sfi := range subFileInfos {
subName := sfi.Name()
if !sfi.IsDir() {
return nil, errors.Errorf("Unexpected non-dir %q",
subName)
}
idPath := filepath.Join(
s.dir, name, subName, idFilename)
idBytes, err := ioutil.ReadFile(idPath)
if err != nil {
return nil, err
}
id, err := kbfsblock.IDFromString(string(idBytes))
if err != nil {
return nil, errors.WithStack(err)
}
if !strings.HasPrefix(id.String(), name+subName) {
return nil, errors.Errorf(
"%q unexpectedly not a prefix of %q",
name+subName, id.String())
}
info, err := s.getInfo(id)
if err != nil {
return nil, err
}
if len(info.Refs) > 0 {
res[id] = info.Refs
}
}
}
return res, nil
}
// put puts the given data for the block, which may already exist, and
// adds a reference for the given context. If isRegularPut is true,
// additional validity checks are performed. If err is nil, putData
// indicates whether the data didn't already exist and was put; if
// false, it means that the data already exists, but this might have
// added a new ref.
func (s *blockDiskStore) put(isRegularPut bool, id kbfsblock.ID, context kbfsblock.Context,
buf []byte, serverHalf kbfscrypto.BlockCryptKeyServerHalf,
tag string) (putData bool, err error) {
err = validateBlockPut(isRegularPut, id, context, buf)
if err != nil {
return false, err
}
// Check the data and retrieve the server half, if they exist.
_, existingServerHalf, err := s.getDataWithContext(id, context)
var exists bool
switch err.(type) {
case blockNonExistentError:
exists = false
case nil:
exists = true
default:
return false, err
}
if exists {
// If the entry already exists, everything should be
// the same, except for possibly additional
// references.
// We checked that both buf and the existing data hash
// to id, so no need to check that they're both equal.
if isRegularPut && existingServerHalf != serverHalf {
return false, errors.Errorf(
"key server half mismatch: expected %s, got %s",
existingServerHalf, serverHalf)
}
} else {
err = s.makeDir(id)
if err != nil {
return false, err
}
err = ioutil.WriteFile(s.dataPath(id), buf, 0600)
if err != nil {
return false, err
}
// TODO: Add integrity-checking for key server half?
data, err := serverHalf.MarshalBinary()
if err != nil {
return false, err
}
err = ioutil.WriteFile(s.keyServerHalfPath(id), data, 0600)
if err != nil {
return false, err
}
}
err = s.addRefs(id, []kbfsblock.Context{context}, liveBlockRef, tag)
if err != nil {
return false, err
}
return !exists, nil
}
func (s *blockDiskStore) addReference(
id kbfsblock.ID, context kbfsblock.Context, tag string) error {
err := s.makeDir(id)
if err != nil {
return err
}
return s.addRefs(id, []kbfsblock.Context{context}, liveBlockRef, tag)
}
func (s *blockDiskStore) archiveReferences(
contexts kbfsblock.ContextMap, tag string) error {
for id, idContexts := range contexts {
err := s.makeDir(id)
if err != nil {
return err
}
err = s.addRefs(id, idContexts, archivedBlockRef, tag)
if err != nil {
return err
}
}
return nil
}
// removeReferences removes references for the given contexts from
// their respective IDs. If tag is non-empty, then a reference will be
// removed only if its most recent tag (passed in to addRefs) matches
// the given one.
func (s *blockDiskStore) removeReferences(
id kbfsblock.ID, contexts []kbfsblock.Context, tag string) (
liveCount int, err error) {
info, err := s.getInfo(id)
if err != nil {
return 0, err
}
if len(info.Refs) == 0 {
return 0, nil
}
for _, context := range contexts {
err := info.Refs.remove(context, tag)
if err != nil {
return 0, err
}
if len(info.Refs) == 0 {
break
}
}
err = s.putInfo(id, info)
if err != nil {
return 0, err
}
return len(info.Refs), nil
}
// remove removes any existing data for the given ID, which must not
// have any references left.
func (s *blockDiskStore) remove(id kbfsblock.ID) error {
hasAnyRef, err := s.hasAnyRef(id)
if err != nil {
return err
}
if hasAnyRef {
return errors.Errorf(
"Trying to remove data for referenced block %s", id)
}
path := s.blockPath(id)
err = ioutil.RemoveAll(path)
if err != nil {
return err
}
// Remove the parent (splayed) directory if it exists and is
// empty.
err = ioutil.Remove(filepath.Dir(path))
if ioutil.IsNotExist(err) || ioutil.IsExist(err) {
err = nil
}
return err
}
func (s blockDiskStore) clear() error {
return ioutil.RemoveAll(s.dir)
}