/
appendfs.go
212 lines (196 loc) · 4.9 KB
/
appendfs.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
package appendfs
import (
"os"
"sync"
"errors"
"encoding/binary"
"io"
"fmt"
"bufio"
"github.com/hanwen/go-fuse/fuse"
"github.com/golang/protobuf/proto"
"github.com/e-tothe-ipi/appendfs/messages"
)
type AppendFS struct {
root *AppendFSNode
blockSize uint32
dataMutex sync.RWMutex
dataFile io.ReadWriter
dataFileOffset int
dataFilePath string
nodeIdMutex sync.RWMutex
lastNodeId uint64
metadataMutex sync.RWMutex
metadataFile io.ReadWriteSeeker
metadataFilePath string
}
func NewAppendFS(dataFilePath string, metadataFilePath string) (*AppendFS, error) {
fs := &AppendFS{}
fs.blockSize = 4096
fs.dataFilePath = dataFilePath
dataFile, err := os.OpenFile(dataFilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
return nil, err
}
dataFileStat, err := dataFile.Stat()
if err != nil {
return nil, err
}
fs.dataFile = dataFile
fs.dataFileOffset = int(dataFileStat.Size())
fs.metadataFilePath = metadataFilePath
metadataFile, err := os.OpenFile(metadataFilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
return nil, err
}
fs.metadataFile = metadataFile
fs.root = CreateNode(nil)
fs.root.attr.Mode = fuse.S_IFDIR | 0755
fs.root.attr.Nlink = 2
fs.root.fs = fs
fs.root.nodeId = fs.NextNodeId()
return fs, nil
}
func (fs *AppendFS) Root() *AppendFSNode {
return fs.root
}
func (fs *AppendFS) NextNodeId() uint64 {
fs.nodeIdMutex.Lock()
fs.lastNodeId += 1
out := fs.lastNodeId
fs.nodeIdMutex.Unlock()
return out
}
func (fs *AppendFS) seenNodeId(lastNodeId uint64) {
if lastNodeId > fs.lastNodeId {
fs.lastNodeId = lastNodeId
}
}
func (fs *AppendFS) AppendData(data []byte) (int, error) {
fs.dataMutex.Lock()
pos := fs.dataFileOffset
n, err := fs.dataFile.Write(data)
fs.dataFileOffset += n
fs.dataMutex.Unlock()
return pos, err
}
func (fs *AppendFS) AppendMetadata(metadata *messages.NodeMetadata) error {
data, err := proto.Marshal(metadata)
if err != nil {
return err
}
n := uint64(len(data))
nBuf := make([]byte, 12)
nBufLen := binary.PutUvarint(nBuf, n)
nBuf = nBuf[:nBufLen]
fs.metadataMutex.Lock()
_, err = fs.metadataFile.Write(nBuf)
if err == nil {
_, err = fs.metadataFile.Write(data)
}
fs.metadataMutex.Unlock()
return err
}
func (fs *AppendFS) LoadMetadata() error {
ret := (error)(nil)
fs.metadataMutex.Lock()
nodes := make(map[uint64]*messages.NodeMetadata)
children := make(map[uint64][]uint64)
_, err := fs.metadataFile.Seek(0, 0)
bufferedReader := bufio.NewReader(fs.metadataFile)
if err != nil {
ret = err
goto Finally
}
for {
msgLen, err := binary.ReadUvarint(bufferedReader)
if err != nil {
// EOF is ok here
if err == io.EOF {
fmt.Println("Reached expected EOF")
break
}
ret = err
goto Finally
}
msgBuf := make([]byte, msgLen)
//fmt.Printf("MsgLen: %d\n", msgLen)
_, err = io.ReadAtLeast(bufferedReader, msgBuf, int(msgLen))
if err != nil {
ret = err
goto Finally
}
metadata := &messages.NodeMetadata{}
err = proto.Unmarshal(msgBuf, metadata)
if err != nil {
ret = err
goto Finally
}
if currentNode, ok := nodes[metadata.GetNodeId()]; ok {
if metadata.Contents != nil {
currentNode.Contents = nil
}
proto.Merge(currentNode, metadata)
//fmt.Printf("Merged %d\n", currentNode.GetNodeId())
} else {
nodes[metadata.GetNodeId()] = metadata
//fmt.Printf("Added %d\n", metadata.GetNodeId())
}
}
for id, node := range nodes {
fs.seenNodeId(id)
if !node.GetValid() {
continue
}
if node.ParentNodeId != nil {
if currentChildren, ok := children[node.GetParentNodeId()]; ok {
children[node.GetParentNodeId()] = append(currentChildren, id)
} else {
children[node.GetParentNodeId()] = append(make([]uint64, 0), id)
}
} else {
ret = errors.New("Corrupt metadata: missing ParentFileId for file " + string(id))
goto Finally
}
}
fs.addChildrenHelper(nodes, children, fs.root)
Finally:
fs.metadataMutex.Unlock()
return ret
}
func (fs *AppendFS) addChildrenHelper(nodes map[uint64]*messages.NodeMetadata, children map[uint64][]uint64, currentNode *AppendFSNode) {
nodeId := currentNode.nodeId
fmt.Printf("Adding Children for node %d\n", nodeId)
if nodeChildren, ok := children[nodeId]; ok {
newChildren := make([]*AppendFSNode, 0)
for _, childId := range nodeChildren {
childMetadata := nodes[childId]
child := FromNodeMetadata(fs, childMetadata)
currentNode.Inode().NewChild(child.name, child.attr.IsDir(), child)
newChildren = append(newChildren, child)
}
for _, child := range newChildren {
fs.addChildrenHelper(nodes, children, child)
}
}
}
func (fs *AppendFS) Close() error {
var err error
fs.dataMutex.Lock()
if closer, ok := fs.dataFile.(io.Closer); ok {
err = closer.Close()
}
fs.dataMutex.Unlock()
if err != nil {
return err
}
fs.metadataMutex.Lock()
if closer, ok := fs.metadataFile.(io.Closer); ok {
err = closer.Close()
}
fs.metadataMutex.Unlock()
if err != nil {
return err
}
return nil
}