This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fsrefstore.go
350 lines (298 loc) · 9.84 KB
/
fsrefstore.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
package filestore
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
pb "github.com/ipfs/go-filestore/pb"
proto "github.com/gogo/protobuf/proto"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dsns "github.com/ipfs/go-datastore/namespace"
dsq "github.com/ipfs/go-datastore/query"
dshelp "github.com/ipfs/go-ipfs-ds-help"
posinfo "github.com/ipfs/go-ipfs-posinfo"
ipld "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
)
// FilestorePrefix identifies the key prefix for FileManager blocks.
//
// Deprecated: use github.com/ipfs/boxo/filestore.FilestorePrefix
var FilestorePrefix = ds.NewKey("filestore")
// FileManager is a blockstore implementation which stores special
// blocks FilestoreNode type. These nodes only contain a reference
// to the actual location of the block data in the filesystem
// (a path and an offset).
//
// Deprecated: use github.com/ipfs/boxo/filestore.FileManager
type FileManager struct {
AllowFiles bool
AllowUrls bool
ds ds.Batching
root string
}
// CorruptReferenceError implements the error interface.
// It is used to indicate that the block contents pointed
// by the referencing blocks cannot be retrieved (i.e. the
// file is not found, or the data changed as it was being read).
//
// Deprecated: use github.com/ipfs/boxo/filestore.CorruptReferenceError
type CorruptReferenceError struct {
Code Status
Err error
}
// Error() returns the error message in the CorruptReferenceError
// as a string.
func (c CorruptReferenceError) Error() string {
return c.Err.Error()
}
// NewFileManager initializes a new file manager with the given
// datastore and root. All FilestoreNodes paths are relative to the
// root path given here, which is prepended for any operations.
//
// Deprecated: use github.com/ipfs/boxo/filestore.NewFileManager
func NewFileManager(ds ds.Batching, root string) *FileManager {
return &FileManager{ds: dsns.Wrap(ds, FilestorePrefix), root: root}
}
// AllKeysChan returns a channel from which to read the keys stored in
// the FileManager. If the given context is cancelled the channel will be
// closed.
//
// All CIDs returned are of type Raw.
func (f *FileManager) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
q := dsq.Query{KeysOnly: true}
res, err := f.ds.Query(ctx, q)
if err != nil {
return nil, err
}
out := make(chan cid.Cid, dsq.KeysOnlyBufSize)
go func() {
defer close(out)
for {
v, ok := res.NextSync()
if !ok {
return
}
k := ds.RawKey(v.Key)
mhash, err := dshelp.DsKeyToMultihash(k)
if err != nil {
logger.Errorf("decoding cid from filestore: %s", err)
continue
}
select {
case out <- cid.NewCidV1(cid.Raw, mhash):
case <-ctx.Done():
return
}
}
}()
return out, nil
}
// DeleteBlock deletes the reference-block from the underlying
// datastore. It does not touch the referenced data.
func (f *FileManager) DeleteBlock(ctx context.Context, c cid.Cid) error {
err := f.ds.Delete(ctx, dshelp.MultihashToDsKey(c.Hash()))
if err == ds.ErrNotFound {
return ipld.ErrNotFound{Cid: c}
}
return err
}
// Get reads a block from the datastore. Reading a block
// is done in two steps: the first step retrieves the reference
// block from the datastore. The second step uses the stored
// path and offsets to read the raw block data directly from disk.
func (f *FileManager) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
dobj, err := f.getDataObj(ctx, c.Hash())
if err != nil {
return nil, err
}
out, err := f.readDataObj(ctx, c.Hash(), dobj)
if err != nil {
return nil, err
}
return blocks.NewBlockWithCid(out, c)
}
// GetSize gets the size of the block from the datastore.
//
// This method may successfully return the size even if returning the block
// would fail because the associated file is no longer available.
func (f *FileManager) GetSize(ctx context.Context, c cid.Cid) (int, error) {
dobj, err := f.getDataObj(ctx, c.Hash())
if err != nil {
return -1, err
}
return int(dobj.GetSize_()), nil
}
func (f *FileManager) readDataObj(ctx context.Context, m mh.Multihash, d *pb.DataObj) ([]byte, error) {
if IsURL(d.GetFilePath()) {
return f.readURLDataObj(ctx, m, d)
}
return f.readFileDataObj(m, d)
}
func (f *FileManager) getDataObj(ctx context.Context, m mh.Multihash) (*pb.DataObj, error) {
o, err := f.ds.Get(ctx, dshelp.MultihashToDsKey(m))
switch err {
case ds.ErrNotFound:
return nil, ipld.ErrNotFound{Cid: cid.NewCidV1(cid.Raw, m)}
case nil:
//
default:
return nil, err
}
return unmarshalDataObj(o)
}
func unmarshalDataObj(data []byte) (*pb.DataObj, error) {
var dobj pb.DataObj
if err := proto.Unmarshal(data, &dobj); err != nil {
return nil, err
}
return &dobj, nil
}
func (f *FileManager) readFileDataObj(m mh.Multihash, d *pb.DataObj) ([]byte, error) {
if !f.AllowFiles {
return nil, ErrFilestoreNotEnabled
}
p := filepath.FromSlash(d.GetFilePath())
abspath := filepath.Join(f.root, p)
fi, err := os.Open(abspath)
if os.IsNotExist(err) {
return nil, &CorruptReferenceError{StatusFileNotFound, err}
} else if err != nil {
return nil, &CorruptReferenceError{StatusFileError, err}
}
defer fi.Close()
_, err = fi.Seek(int64(d.GetOffset()), io.SeekStart)
if err != nil {
return nil, &CorruptReferenceError{StatusFileError, err}
}
outbuf := make([]byte, d.GetSize_())
_, err = io.ReadFull(fi, outbuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return nil, &CorruptReferenceError{StatusFileChanged, err}
} else if err != nil {
return nil, &CorruptReferenceError{StatusFileError, err}
}
// Work with CIDs for this, as they are a nice wrapper and things
// will not break if multihashes underlying types change.
origCid := cid.NewCidV1(cid.Raw, m)
outcid, err := origCid.Prefix().Sum(outbuf)
if err != nil {
return nil, err
}
if !origCid.Equals(outcid) {
return nil, &CorruptReferenceError{StatusFileChanged,
fmt.Errorf("data in file did not match. %s offset %d", d.GetFilePath(), d.GetOffset())}
}
return outbuf, nil
}
// reads and verifies the block from URL
func (f *FileManager) readURLDataObj(ctx context.Context, m mh.Multihash, d *pb.DataObj) ([]byte, error) {
if !f.AllowUrls {
return nil, ErrUrlstoreNotEnabled
}
req, err := http.NewRequestWithContext(ctx, "GET", d.GetFilePath(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", d.GetOffset(), d.GetOffset()+d.GetSize_()-1))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, &CorruptReferenceError{StatusFileError, err}
}
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusPartialContent {
return nil, &CorruptReferenceError{StatusFileError,
fmt.Errorf("expected HTTP 200 or 206 got %d", res.StatusCode)}
}
outbuf := make([]byte, d.GetSize_())
_, err = io.ReadFull(res.Body, outbuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return nil, &CorruptReferenceError{StatusFileChanged, err}
} else if err != nil {
return nil, &CorruptReferenceError{StatusFileError, err}
}
res.Body.Close()
// Work with CIDs for this, as they are a nice wrapper and things
// will not break if multihashes underlying types change.
origCid := cid.NewCidV1(cid.Raw, m)
outcid, err := origCid.Prefix().Sum(outbuf)
if err != nil {
return nil, err
}
if !origCid.Equals(outcid) {
return nil, &CorruptReferenceError{StatusFileChanged,
fmt.Errorf("data in file did not match. %s offset %d", d.GetFilePath(), d.GetOffset())}
}
return outbuf, nil
}
// Has returns if the FileManager is storing a block reference. It does not
// validate the data, nor checks if the reference is valid.
func (f *FileManager) Has(ctx context.Context, c cid.Cid) (bool, error) {
// NOTE: interesting thing to consider. Has doesnt validate the data.
// So the data on disk could be invalid, and we could think we have it.
dsk := dshelp.MultihashToDsKey(c.Hash())
return f.ds.Has(ctx, dsk)
}
type putter interface {
Put(context.Context, ds.Key, []byte) error
}
// Put adds a new reference block to the FileManager. It does not check
// that the reference is valid.
func (f *FileManager) Put(ctx context.Context, b *posinfo.FilestoreNode) error {
return f.putTo(ctx, b, f.ds)
}
func (f *FileManager) putTo(ctx context.Context, b *posinfo.FilestoreNode, to putter) error {
var dobj pb.DataObj
if IsURL(b.PosInfo.FullPath) {
if !f.AllowUrls {
return ErrUrlstoreNotEnabled
}
dobj.FilePath = b.PosInfo.FullPath
} else {
if !f.AllowFiles {
return ErrFilestoreNotEnabled
}
//lint:ignore SA1019 // ignore staticcheck
if !filepath.HasPrefix(b.PosInfo.FullPath, f.root) {
return fmt.Errorf("cannot add filestore references outside ipfs root (%s)", f.root)
}
p, err := filepath.Rel(f.root, b.PosInfo.FullPath)
if err != nil {
return err
}
dobj.FilePath = filepath.ToSlash(p)
}
dobj.Offset = b.PosInfo.Offset
dobj.Size_ = uint64(len(b.RawData()))
data, err := proto.Marshal(&dobj)
if err != nil {
return err
}
return to.Put(ctx, dshelp.MultihashToDsKey(b.Cid().Hash()), data)
}
// PutMany is like Put() but takes a slice of blocks instead,
// allowing it to create a batch transaction.
func (f *FileManager) PutMany(ctx context.Context, bs []*posinfo.FilestoreNode) error {
batch, err := f.ds.Batch(ctx)
if err != nil {
return err
}
for _, b := range bs {
if err := f.putTo(ctx, b, batch); err != nil {
return err
}
}
return batch.Commit(ctx)
}
// IsURL returns true if the string represents a valid URL that the
// urlstore can handle. More specifically it returns true if a string
// begins with 'http://' or 'https://'.
//
// Deprecated: use github.com/ipfs/boxo/filestore.IsURL
func IsURL(str string) bool {
return (len(str) > 7 && str[0] == 'h' && str[1] == 't' && str[2] == 't' && str[3] == 'p') &&
((len(str) > 8 && str[4] == 's' && str[5] == ':' && str[6] == '/' && str[7] == '/') ||
(str[4] == ':' && str[5] == '/' && str[6] == '/'))
}