-
Notifications
You must be signed in to change notification settings - Fork 0
/
billyfs.go
283 lines (214 loc) · 6.1 KB
/
billyfs.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
package billyfs
import (
"encoding/binary"
"fmt"
"github.com/apple/foundationdb/bindings/go/src/fdb/subspace"
"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
pkg_errors "github.com/pkg/errors"
"os"
"path"
"strings"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
"github.com/go-git/go-billy/v5"
)
// FoundationDbFs representds a billy filesystem over FoundationDb KV store
type FoundationDbFs struct {
db fdb.Database
}
// ensure that FoundationDbFs fulfills interfaces
var _ billy.Basic = FoundationDbFs{}
var _ billy.Dir = FoundationDbFs{}
var _ billy.Capable = FoundationDbFs{}
func init() {
fdb.APIVersion(620)
}
// NewFoundationDbFs Creates new FoundationDBFs
func NewFoundationDbFs(clusterFile string) (FoundationDbFs, error) {
//fdb.setAPIVersion
db, error := fdb.OpenDatabase(clusterFile)
if error != nil {
return FoundationDbFs{}, error
}
return FoundationDbFs{db}, nil
}
//billy.Dir methods
// MkdirAll creates full path
func (fs FoundationDbFs) MkdirAll(path string, perm os.FileMode) error {
//TODO : add meta key to preserve file info
_, err := fs.createOrGet(path, &fileModeApplicator{perm, nil})
if err != nil {
return pkg_errors.Wrap(err, "failed_on_mkdirall")
}
return err
}
type fileModeApplicator struct {
perm os.FileMode
permAsByte []byte
}
func (p *fileModeApplicator) visit(w fdb.Transaction, step *opResult) {
if step.wasCreated {
if p.permAsByte == nil {
p.permAsByte = make([]byte, 4)
binary.LittleEndian.PutUint32(p.permAsByte, uint32(p.perm))
}
w.Set(step.Pack(tuple.Tuple{0xFC, 0x00}), p.permAsByte)
}
}
type opResult struct {
subspace.Subspace
wasCreated bool
}
type SpaceVisitor interface {
visit(w fdb.Transaction, result *opResult)
}
func (fs *FoundationDbFs) createOrGet(path string, txSpaceVisitor SpaceVisitor) (*opResult, error) {
fsPath := fs.split(path)
var out interface{}
var err error
for i := range fsPath {
out, err = fs.db.Transact(func(w fdb.Transaction) (interface{}, error) {
path := fsPath[0 : i+1]
once, err := directory.Exists(w, path)
if err != nil {
return nil, err
}
created, err := directory.CreateOrOpen(w, path, nil)
if err != nil {
return nil, err
}
dang := &opResult{
Subspace: created,
wasCreated: !once,
}
//in reality this visitor have to be called for every node in path, since we need to
// apply txSpaceVisitor per subspace
if txSpaceVisitor != nil {
txSpaceVisitor.visit(w, dang)
}
return dang, nil
})
if err != nil {
return nil, pkg_errors.WithMessagef(err, "Unable to obtain subspace %s", path)
}
}
return out.(*opResult), nil
}
// ReadDir returns all file entries in a pth
func (fs FoundationDbFs) ReadDir(path string) ([]os.FileInfo, error) {
fsPath := fs.split(path)
list, err := fs.db.ReadTransact(func(r fdb.ReadTransaction) (interface{}, error) {
entries, err := directory.List(r, fsPath)
if err != nil {
return nil, err
}
result := make([]os.FileInfo, len(entries))
//below is bad, since we have to read all entries for path!
// it's not that bad, since entries are last element, so we need to just construct subspace and unpack
node, err := nodeOrRoot(r, fsPath)
for i := range entries {
result[i], err = stat(r, node, entries[i])
}
return result, nil
})
if err != nil {
return nil, err
}
slice, ok := list.([]os.FileInfo)
if !ok {
return nil, fmt.Errorf("Failed converting to slice %v", list)
}
return slice, nil
}
func nodeOrRoot(r fdb.ReadTransaction, p []string) (directory.Directory, error) {
var node directory.Directory
var err error
if len(p) == 0 {
node = directory.Root()
} else {
node, err = directory.Open(r, p, nil)
if err != nil {
return nil, err
}
}
return node, nil
}
func stat(r fdb.ReadTransaction, p directory.Directory, n string) (os.FileInfo, error) {
entry, err := p.Open(r, []string{n}, nil)
if err != nil {
return nil, err
}
bytes := r.Get(entry.Pack(tuple.Tuple{0xFC, 0x00})).MustGet()
return dirFileInfo{n, os.FileMode(binary.LittleEndian.Uint32(bytes))}, nil
}
//billy.Basic methods
// Open a file
func (fs FoundationDbFs) Open(path string) (billy.File, error) {
return fs.OpenFile(path, os.O_RDONLY, 0666)
}
// Create creates a file
func (fs FoundationDbFs) Create(path string) (billy.File, error) {
return fs.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
}
func (fs *FoundationDbFs) split(in string) []string {
//need to add normalisation
clean := path.Clean(in)
return fs.norm(strings.Split(clean, "/"))
}
func (FoundationDbFs) norm(in []string) []string {
//we don't want to have empty strings in path array
if in == nil {
return nil
}
var result []string
for i := range in {
if in[i] != "" {
result = append(result, in[i])
}
}
return result
}
// OpenFile full fledged call
func (fs FoundationDbFs) OpenFile(path string, flag int, perm os.FileMode) (billy.File, error) {
return NewFile(&fs, path, flag, perm)
}
// Remove deletes path
func (fs FoundationDbFs) Remove(path string) error {
fsPath := fs.split(path)
_, err := fs.db.Transact(func(tx fdb.Transaction) (interface{}, error) {
return directory.Root().Remove(tx, fsPath)
})
return err
}
// Rename renames path
func (FoundationDbFs) Rename(from string, to string) error {
return nil
}
// Stat obtains file meta
func (fs FoundationDbFs) Stat(path string) (os.FileInfo, error) {
fsPath := fs.split(path)
if len(fsPath) == 0 {
return dirFileInfo{name: "/", mode: os.ModeDir | os.ModePerm}, nil
}
stat, err := fs.db.ReadTransact(func(r fdb.ReadTransaction) (interface{}, error) {
ind := len(fsPath) - 1
node, err := nodeOrRoot(r, fsPath[0:ind])
if err != nil {
return nil, err
}
return stat(r, node, fsPath[ind])
})
if err != nil {
return nil, err
}
return stat.(os.FileInfo), nil
}
// Join joins path
func (FoundationDbFs) Join(arr ...string) string {
return path.Join(arr...)
}
//billy.Capable methods
// Capabilities what fs can do
func (FoundationDbFs) Capabilities() billy.Capability {
return billy.ReadAndWriteCapability | billy.SeekCapability | billy.TruncateCapability
}