-
Notifications
You must be signed in to change notification settings - Fork 2
/
buddyfs.go
108 lines (90 loc) · 2.15 KB
/
buddyfs.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
package gobuddyfs
import (
"encoding/binary"
"encoding/json"
"sync"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/golang/glog"
)
const BLOCK_SIZE = 4096
const ROOT_BLOCK_KEY = "ROOT"
func min(a int, b int) int {
if a < b {
return a
}
return b
}
// BuddyFS implements the Buddy file system.
type BuddyFS struct {
Lock sync.Mutex
Store KVStore
blkGen BlockGenerator
FSM *FSMeta
fs.FS
}
type FSMeta struct {
Dir
}
func (fsm FSMeta) Marshal() ([]byte, error) {
return json.Marshal(fsm)
}
func (fsm *FSMeta) Unmarshal(data []byte) error {
return json.Unmarshal(data, fsm)
}
func NewBuddyFS(store KVStore) *BuddyFS {
bfs := &BuddyFS{Store: store, Lock: sync.Mutex{},
blkGen: new(RandomizedBlockGenerator)}
return bfs
}
func (bfs BuddyFS) CreateNewFSMetadata() *FSMeta {
return &FSMeta{Dir: Dir{Block: bfs.blkGen.NewNamedBlock("/"),
blkGen: bfs.blkGen, Dirs: []Block{}, Files: []Block{}, Lock: sync.RWMutex{}}}
}
func (bfs *BuddyFS) Root() (fs.Node, error) {
bfs.Lock.Lock()
defer bfs.Lock.Unlock()
if bfs.FSM == nil {
rootKey, err := bfs.Store.Get(ROOT_BLOCK_KEY, true)
if rootKey == nil {
glog.Infoln("Creating new root block")
// Root key not found
root := bfs.CreateNewFSMetadata()
root.MarkDirty()
err = root.WriteBlock(root, bfs.Store)
if err == nil {
buffer := make([]byte, 80)
binary.PutVarint(buffer, root.Block.Id)
err = bfs.Store.Set(ROOT_BLOCK_KEY, buffer)
if err == nil {
bfs.FSM = root
bfs.FSM.KVS = bfs.Store
return bfs.FSM, nil
} else {
glog.Errorf("Error while creating ROOT key: %q", err)
return nil, fuse.EIO
}
} else {
glog.Errorf("Error while creating root node: %q", err)
return nil, fuse.EIO
}
}
var root FSMeta
var n int
root.Block.Id, n = binary.Varint(rootKey)
if n <= 0 {
glog.Errorf("Error while decoding root key")
return nil, fuse.EIO
}
err = root.ReadBlock(&root, bfs.Store)
if err != nil {
glog.Errorf("Error while read root block: %q", err)
return nil, fuse.EIO
}
bfs.FSM = &root
bfs.FSM.KVS = bfs.Store
bfs.FSM.blkGen = new(RandomizedBlockGenerator)
return bfs.FSM, nil
}
return bfs.FSM, nil
}