-
Notifications
You must be signed in to change notification settings - Fork 2
/
crabfs.go
335 lines (261 loc) · 7.86 KB
/
crabfs.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
package crabfs
import (
"context"
"crypto/aes"
"crypto/rand"
"io"
"path"
"time"
crabfsCrypto "github.com/runletapp/crabfs/crypto"
"github.com/runletapp/crabfs/identity"
"github.com/runletapp/crabfs/interfaces"
"github.com/runletapp/crabfs/options"
pb "github.com/runletapp/crabfs/protos"
ipfsDatastore "github.com/ipfs/go-datastore"
ipfsDatastoreSync "github.com/ipfs/go-datastore/sync"
ipfsDsLeveldb "github.com/ipfs/go-ds-leveldb"
ipfsBlockstore "github.com/ipfs/go-ipfs-blockstore"
)
var _ interfaces.Core = &crabFS{}
type crabFS struct {
settings *options.Settings
host interfaces.Host
datastore *ipfsDatastoreSync.MutexDatastore
blockstore ipfsBlockstore.Blockstore
fetcherFactory interfaces.FetcherFactory
gc interfaces.GarbageCollector
}
// New create a new CrabFS
func New(opts ...options.Option) (interfaces.Core, error) {
settings := options.Settings{}
if err := settings.SetDefaults(); err != nil {
return nil, err
}
for _, opt := range opts {
if err := opt(&settings); err != nil {
return nil, err
}
}
if settings.BlockSize == 0 {
return nil, ErrInvalidBlockSize
}
if settings.Identity == nil {
var err error
settings.Identity, err = identity.CreateIdentity()
if err != nil {
return nil, err
}
}
var rawDatastore ipfsDatastore.Datastore
if settings.RelayOnly {
rawDatastore = ipfsDatastore.NewMapDatastore()
} else if settings.Root == "" {
// Use an in-memory datastore
rawDatastore = ipfsDatastore.NewMapDatastore()
} else {
var err error
rawDatastore, err = ipfsDsLeveldb.NewDatastore(path.Join(settings.Root, "db"), nil)
if err != nil {
return nil, err
}
}
datastore := ipfsDatastoreSync.MutexWrap(rawDatastore)
blockstore := ipfsBlockstore.NewBlockstore(datastore)
gc, err := GarbageCollectorNew(settings.Context, settings.GCInterval, datastore, blockstore)
if err != nil {
return nil, err
}
hostFactory := HostNew
host, err := hostFactory(&settings, datastore, blockstore)
if err != nil {
return nil, err
}
if !settings.RelayOnly {
if err := host.Announce(); err != nil {
return nil, err
}
}
fs := &crabFS{
settings: &settings,
host: host,
datastore: datastore,
blockstore: blockstore,
fetcherFactory: BasicFetcherNew,
gc: gc,
}
if !settings.RelayOnly {
go fs.background()
if err := gc.Start(); err != nil {
return nil, err
}
}
return fs, nil
}
func (fs *crabFS) background() {
locker := fs.gc.Locker()
locker.Lock()
fs.host.Reprovide(fs.settings.Context, true)
locker.Unlock()
ticker := time.NewTicker(fs.settings.ReprovideInterval)
for {
select {
case <-ticker.C:
locker := fs.gc.Locker()
locker.Lock()
// fs.host.Reprovide(fs.settings.Context, false)
locker.Unlock()
case <-fs.settings.Context.Done():
return
}
}
}
func (fs *crabFS) Close() error {
if err := fs.host.Close(); err != nil {
return err
}
return fs.datastore.Close()
}
func (fs *crabFS) Get(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string) (interfaces.Fetcher, error) {
locker := fs.gc.Locker()
locker.Lock()
object, err := fs.host.GetContent(ctx, privateKey.GetPublic(), bucket, filename)
if err != nil {
locker.Unlock()
return nil, err
}
fetcher, err := fs.fetcherFactory(ctx, fs, object, privateKey)
if err != nil {
locker.Unlock()
return nil, err
}
go func() {
// We hold a gc locker until the fetcher is done, this way we avoid
// the gc messing around the fetch operation
<-fetcher.Context().Done()
locker.Unlock()
}()
return fetcher, nil
}
func (fs *crabFS) Lock(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string) (*pb.LockToken, error) {
return fs.host.Lock(ctx, privateKey, bucket, filename)
}
func (fs *crabFS) Unlock(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string, token *pb.LockToken) error {
return fs.host.Unlock(ctx, privateKey, bucket, filename, token)
}
func (fs *crabFS) IsLocked(ctx context.Context, publicKey crabfsCrypto.PubKey, bucket string, filename string) (bool, error) {
return fs.host.IsLocked(ctx, publicKey, bucket, filename)
}
func (fs *crabFS) generateKey(size int) ([]byte, error) {
b := make([]byte, size)
_, err := rand.Read(b)
return b, err
}
func (fs *crabFS) prepareFile(privateKey crabfsCrypto.PrivKey, file io.Reader) (interfaces.BlockMap, []byte, int64, error) {
key, err := fs.generateKey(32) // aes-256
if err != nil {
return nil, nil, 0, err
}
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, nil, 0, err
}
slicer, err := BlockSlicerNew(file, fs.settings.BlockSize, cipher)
if err != nil {
return nil, nil, 0, err
}
blockMap := interfaces.BlockMap{}
totalSize := int64(0)
blockMeta, block, n, err := slicer.Next()
for {
if err != nil && err != io.EOF {
return nil, nil, 0, err
}
if block == nil {
break
}
if err := fs.blockstore.Put(block); err != nil {
return nil, nil, 0, err
}
blockMap[blockMeta.Start] = blockMeta
totalSize += n
// Process block
blockMeta, block, n, err = slicer.Next()
}
cipherKey, err := privateKey.GetPublic().Encrypt(key, []byte("crabfs"))
if err != nil {
return nil, nil, 0, err
}
return blockMap, cipherKey, totalSize, nil
}
func (fs *crabFS) Put(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string, file io.Reader, mtime time.Time) error {
locker := fs.gc.Locker()
locker.Lock()
defer locker.Unlock()
blockMap, cipherKey, totalSize, err := fs.prepareFile(privateKey, file)
if err != nil {
return err
}
return fs.host.Publish(ctx, privateKey, cipherKey, bucket, filename, blockMap, mtime, totalSize)
}
func (fs *crabFS) PutWithCacheTTL(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string, file io.Reader, mtime time.Time, ttl uint64) error {
locker := fs.gc.Locker()
locker.Lock()
defer locker.Unlock()
blockMap, cipherKey, totalSize, err := fs.prepareFile(privateKey, file)
if err != nil {
return err
}
return fs.host.PublishWithCacheTTL(ctx, privateKey, cipherKey, bucket, filename, blockMap, mtime, totalSize, ttl)
}
func (fs *crabFS) PutAndLock(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string, file io.Reader, mtime time.Time) (*pb.LockToken, error) {
locker := fs.gc.Locker()
locker.Lock()
defer locker.Unlock()
blockMap, cipherKey, totalSize, err := fs.prepareFile(privateKey, file)
if err != nil {
return nil, err
}
return fs.host.PublishAndLock(ctx, privateKey, cipherKey, bucket, filename, blockMap, mtime, totalSize)
}
func (fs *crabFS) Remove(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string) error {
if err := fs.gc.Schedule(); err != nil {
return err
}
return fs.host.Remove(ctx, privateKey, bucket, filename)
}
func (fs *crabFS) GetID() string {
return fs.host.GetID()
}
func (fs *crabFS) GetAddrs() []string {
return fs.host.GetAddrs()
}
func (fs *crabFS) Blockstore() ipfsBlockstore.Blockstore {
return fs.blockstore
}
func (fs *crabFS) Host() interfaces.Host {
return fs.host
}
func (fs *crabFS) GarbageCollector() interfaces.GarbageCollector {
return fs.gc
}
func (fs *crabFS) WithBucket(privateKey crabfsCrypto.PrivKey, bucket string) (interfaces.Bucket, error) {
if err := fs.PublishPublicKey(privateKey.GetPublic()); err != nil {
return nil, err
}
return BucketCoreNew(fs, privateKey, bucket, ""), nil
}
func (fs *crabFS) WithBucketRoot(privateKey crabfsCrypto.PrivKey, bucket string, baseDir string) (interfaces.Bucket, error) {
if err := fs.PublishPublicKey(privateKey.GetPublic()); err != nil {
return nil, err
}
return BucketCoreNew(fs, privateKey, bucket, baseDir), nil
}
func (fs *crabFS) PublishPublicKey(publicKey crabfsCrypto.PubKey) error {
locker := fs.gc.Locker()
locker.Lock()
defer locker.Unlock()
return fs.host.PutPublicKey(publicKey)
}
func (fs *crabFS) GetIdentity() identity.Identity {
return fs.settings.Identity
}