forked from TRON-US/go-unixfs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
unixfs.go
522 lines (451 loc) · 14 KB
/
unixfs.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Package unixfs implements a data format for files in the IPFS filesystem It
// is not the only format in ipfs, but it is the one that the filesystem
// assumes
package unixfs
import (
"context"
"errors"
"fmt"
proto "github.com/gogo/protobuf/proto"
dag "github.com/ipfs/go-merkledag"
pb "github.com/bittorrent/go-unixfs/pb"
ipld "github.com/ipfs/go-ipld-format"
)
// A LinkResult for any parallel enumeration of links
// TODO: Should this live in go-ipld-format?
type LinkResult struct {
Link *ipld.Link
Err error
}
// Shorthands for protobuffer types
const (
TRaw = pb.Data_Raw
TFile = pb.Data_File
TDirectory = pb.Data_Directory
TMetadata = pb.Data_Metadata
TSymlink = pb.Data_Symlink
THAMTShard = pb.Data_HAMTShard
TTokenMeta = pb.Data_TokenMeta
)
// Common errors
var (
ErrMalformedFileFormat = errors.New("malformed data in file format")
ErrUnrecognizedType = errors.New("unrecognized node type")
ErrNotMetadataRoot = errors.New("expected token metadata protobuf dag node")
ErrUnexpectedLinks = errors.New("expected more than two links under the given dag node")
ErrMetadataAccessDenied = errors.New("Token metadata can not be accessed by default. Use --meta option.")
)
// FromBytes unmarshals a byte slice as protobuf Data.
// Deprecated: Use `FSNodeFromBytes` instead to avoid direct manipulation of `pb.Data`.
func FromBytes(data []byte) (*pb.Data, error) {
pbdata := new(pb.Data)
err := proto.Unmarshal(data, pbdata)
if err != nil {
return nil, err
}
return pbdata, nil
}
// FilePBData creates a protobuf File with the given
// byte slice and returns the marshaled protobuf bytes representing it.
func FilePBData(data []byte, totalsize uint64) []byte {
pbfile := new(pb.Data)
typ := pb.Data_File
pbfile.Type = &typ
pbfile.Data = data
pbfile.Filesize = proto.Uint64(totalsize)
data, err := proto.Marshal(pbfile)
if err != nil {
// This really shouldnt happen, i promise
// The only failure case for marshal is if required fields
// are not filled out, and they all are. If the proto object
// gets changed and nobody updates this function, the code
// should panic due to programmer error
panic(err)
}
return data
}
// FolderPBData returns Bytes that represent a Directory.
func FolderPBData() []byte {
pbfile := new(pb.Data)
typ := pb.Data_Directory
pbfile.Type = &typ
data, err := proto.Marshal(pbfile)
if err != nil {
//this really shouldnt happen, i promise
panic(err)
}
return data
}
// WrapData marshals raw bytes into a `Data_Raw` type protobuf message.
func WrapData(b []byte) []byte {
pbdata := new(pb.Data)
typ := pb.Data_Raw
pbdata.Data = b
pbdata.Type = &typ
pbdata.Filesize = proto.Uint64(uint64(len(b)))
out, err := proto.Marshal(pbdata)
if err != nil {
// This shouldnt happen. seriously.
panic(err)
}
return out
}
// SymlinkData returns a `Data_Symlink` protobuf message for the path you specify.
func SymlinkData(path string) ([]byte, error) {
pbdata := new(pb.Data)
typ := pb.Data_Symlink
pbdata.Data = []byte(path)
pbdata.Type = &typ
out, err := proto.Marshal(pbdata)
if err != nil {
return nil, err
}
return out, nil
}
// HAMTShardData return a `Data_HAMTShard` protobuf message
func HAMTShardData(data []byte, fanout uint64, hashType uint64) ([]byte, error) {
pbdata := new(pb.Data)
typ := pb.Data_HAMTShard
pbdata.Type = &typ
pbdata.HashType = proto.Uint64(hashType)
pbdata.Data = data
pbdata.Fanout = proto.Uint64(fanout)
out, err := proto.Marshal(pbdata)
if err != nil {
return nil, err
}
return out, nil
}
// UnwrapData unmarshals a protobuf messages and returns the contents.
func UnwrapData(data []byte) ([]byte, error) {
pbdata := new(pb.Data)
err := proto.Unmarshal(data, pbdata)
if err != nil {
return nil, err
}
return pbdata.GetData(), nil
}
// DataSize returns the size of the contents in protobuf wrapped slice.
// For raw data it simply provides the length of it. For Data_Files, it
// will return the associated filesize. Note that Data_Directories will
// return an error.
func DataSize(data []byte) (uint64, error) {
pbdata := new(pb.Data)
err := proto.Unmarshal(data, pbdata)
if err != nil {
return 0, err
}
return size(pbdata)
}
func size(pbdata *pb.Data) (uint64, error) {
switch pbdata.GetType() {
case pb.Data_Directory, pb.Data_HAMTShard:
return 0, errors.New("can't get data size of directory")
case pb.Data_File, pb.Data_Raw, pb.Data_TokenMeta:
return pbdata.GetFilesize(), nil
case pb.Data_Symlink:
return uint64(len(pbdata.GetData())), nil
default:
return 0, errors.New("unrecognized node data type")
}
}
// An FSNode represents a filesystem object using the UnixFS specification.
//
// The `NewFSNode` constructor should be used instead of just calling `new(FSNode)`
// to guarantee that the required (`Type` and `Filesize`) fields in the `format`
// structure are initialized before marshaling (in `GetBytes()`).
type FSNode struct {
// UnixFS format defined as a protocol buffers message.
format pb.Data
}
// FSNodeFromBytes unmarshal a protobuf message onto an FSNode.
func FSNodeFromBytes(b []byte) (*FSNode, error) {
n := new(FSNode)
err := proto.Unmarshal(b, &n.format)
if err != nil {
return nil, err
}
return n, nil
}
// NewFSNode creates a new FSNode structure with the given `dataType`.
//
// It initializes the (required) `Type` field (that doesn't have a `Set()`
// accessor so it must be specified at creation), otherwise the `Marshal()`
// method in `GetBytes()` would fail (`required field "Type" not set`).
//
// It also initializes the `Filesize` pointer field to ensure its value
// is never nil before marshaling, this is not a required field but it is
// done to be backwards compatible with previous `go-ipfs` versions hash.
// (If it wasn't initialized there could be cases where `Filesize` could
// have been left at nil, when the `FSNode` was created but no data or
// child nodes were set to adjust it, as is the case in `NewLeaf()`.)
func NewFSNode(dataType pb.Data_DataType) *FSNode {
n := new(FSNode)
n.format.Type = &dataType
// Initialize by `Filesize` by updating it with a dummy (zero) value.
n.UpdateFilesize(0)
return n
}
// HashType gets hash type of format
func (n *FSNode) HashType() uint64 {
return n.format.GetHashType()
}
// Fanout gets fanout of format
func (n *FSNode) Fanout() uint64 {
return n.format.GetFanout()
}
// AddBlockSize adds the size of the next child block of this node
func (n *FSNode) AddBlockSize(s uint64) {
n.UpdateFilesize(int64(s))
n.format.Blocksizes = append(n.format.Blocksizes, s)
}
// RemoveBlockSize removes the given child block's size.
func (n *FSNode) RemoveBlockSize(i int) {
n.UpdateFilesize(-int64(n.format.Blocksizes[i]))
n.format.Blocksizes = append(n.format.Blocksizes[:i], n.format.Blocksizes[i+1:]...)
}
// BlockSize returns the block size indexed by `i`.
// TODO: Evaluate if this function should be bounds checking.
func (n *FSNode) BlockSize(i int) uint64 {
return n.format.Blocksizes[i]
}
// BlockSizes gets blocksizes of format
func (n *FSNode) BlockSizes() []uint64 {
return n.format.GetBlocksizes()
}
// RemoveAllBlockSizes removes all the child block sizes of this node.
func (n *FSNode) RemoveAllBlockSizes() {
n.format.Blocksizes = []uint64{}
n.format.Filesize = proto.Uint64(uint64(len(n.Data())))
}
// GetBytes marshals this node as a protobuf message.
func (n *FSNode) GetBytes() ([]byte, error) {
return proto.Marshal(&n.format)
}
// FileSize returns the size of the file.
func (n *FSNode) FileSize() uint64 {
// XXX: This needs to be able to return an error when we don't know the
// size.
size, _ := size(&n.format)
return size
}
// NumChildren returns the number of child blocks of this node
func (n *FSNode) NumChildren() int {
return len(n.format.Blocksizes)
}
// Data retrieves the `Data` field from the internal `format`.
func (n *FSNode) Data() []byte {
return n.format.GetData()
}
// Data retrieves the `Data` field from the internal `format`.
func (n *FSNode) Metadata() []byte {
return nil
}
// SetData sets the `Data` field from the internal `format`
// updating its `Filesize`.
func (n *FSNode) SetData(newData []byte) {
n.UpdateFilesize(int64(len(newData) - len(n.Data())))
n.format.Data = newData
}
// UpdateFilesize updates the `Filesize` field from the internal `format`
// by a signed difference (`filesizeDiff`).
// TODO: Add assert to check for `Filesize` > 0?
func (n *FSNode) UpdateFilesize(filesizeDiff int64) {
n.format.Filesize = proto.Uint64(uint64(
int64(n.format.GetFilesize()) + filesizeDiff))
}
// Type retrieves the `Type` field from the internal `format`.
func (n *FSNode) Type() pb.Data_DataType {
return n.format.GetType()
}
// IsDir checks whether the node represents a directory
func (n *FSNode) IsDir() bool {
switch n.Type() {
case pb.Data_Directory, pb.Data_HAMTShard:
return true
default:
return false
}
}
// Metadata is used to store additional FSNode information.
type Metadata struct {
MimeType string
Size uint64
}
// MetadataFromBytes Unmarshals a protobuf Data message into Metadata.
// The provided slice should have been encoded with BytesForMetadata().
func MetadataFromBytes(b []byte) (*Metadata, error) {
pbd := new(pb.Data)
err := proto.Unmarshal(b, pbd)
if err != nil {
return nil, err
}
if pbd.GetType() != pb.Data_Metadata {
return nil, errors.New("incorrect node type")
}
pbm := new(pb.Metadata)
err = proto.Unmarshal(pbd.Data, pbm)
if err != nil {
return nil, err
}
md := new(Metadata)
md.MimeType = pbm.GetMimeType()
return md, nil
}
// Bytes marshals Metadata as a protobuf message of Metadata type.
func (m *Metadata) Bytes() ([]byte, error) {
pbm := new(pb.Metadata)
pbm.MimeType = &m.MimeType
return proto.Marshal(pbm)
}
// BytesForMetadata wraps the given Metadata as a profobuf message of Data type,
// setting the DataType to Metadata. The wrapped bytes are itself the
// result of calling m.Bytes().
func BytesForMetadata(m *Metadata) ([]byte, error) {
pbd := new(pb.Data)
pbd.Filesize = proto.Uint64(m.Size)
typ := pb.Data_Metadata
pbd.Type = &typ
mdd, err := m.Bytes()
if err != nil {
return nil, err
}
pbd.Data = mdd
return proto.Marshal(pbd)
}
// EmptyDirNode creates an empty folder Protonode.
func EmptyDirNode() *dag.ProtoNode {
return dag.NodeWithData(FolderPBData())
}
// EmptyFileNode creates an empty file Protonode.
func EmptyFileNode() *dag.ProtoNode {
return dag.NodeWithData(FilePBData(nil, 0))
}
// ReadUnixFSNodeData extracts the UnixFS data from an IPLD node.
// Raw nodes are (also) processed because they are used as leaf
// nodes containing (only) UnixFS data.
func ReadUnixFSNodeData(node ipld.Node) (data []byte, err error) {
switch node := node.(type) {
case *dag.ProtoNode:
fsNode, err := FSNodeFromBytes(node.Data())
if err != nil {
return nil, fmt.Errorf("incorrectly formatted protobuf: %s", err)
}
switch fsNode.Type() {
case pb.Data_File, pb.Data_Raw, pb.Data_TokenMeta:
return fsNode.Data(), nil
// Only leaf nodes (of type `Data_Raw`) contain data but due to a
// bug the `Data_File` type (normally used for internal nodes) is
// also used for leaf nodes, so both types are accepted here
// (see the `balanced` package for more details).
default:
return nil, fmt.Errorf("found %s node in unexpected place",
fsNode.Type().String())
}
case *dag.RawNode:
return node.RawData(), nil
default:
return nil, ErrUnrecognizedType
// TODO: To avoid rewriting the error message, but a different error from
// `unixfs.ErrUnrecognizedType` should be used (defining it in the
// `merkledag` or `go-ipld-format` packages).
}
}
// ExtractFsNode extracts the `unixfs.FSNode` from the `ipld.Node` (assuming this
// was implemented by a `mdag.ProtoNode`).
func ExtractFSNode(node ipld.Node) (*FSNode, error) {
protoNode, ok := node.(*dag.ProtoNode)
if !ok {
return nil, errors.New("expected a ProtoNode as internal node")
}
fsNode, err := FSNodeFromBytes(protoNode.Data())
if err != nil {
return nil, err
}
return fsNode, nil
}
// GetMetaSubdagRoot returns metadata subDag root if the given 'nd' is the dummy
// root of a DAG with metadata subDag.
func GetMetaSubdagRoot(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (ipld.Node, error) {
nd, ok := n.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}
if nd.Links() != nil && len(nd.Links()) >= 2 {
link := nd.Links()[0]
c := link.Cid
child, err := serv.Get(ctx, c)
if err != nil {
return nil, err
}
childNode, ok := child.(*dag.ProtoNode)
if !ok {
return nil, err
}
// Make sure first child is of TTokenMeta.
// If not, return nil.
fsType, err := GetFSType(childNode)
if err != nil {
return nil, err
}
if TTokenMeta != fsType {
return nil, ErrNotMetadataRoot
}
return childNode, nil
}
return nil, ErrUnexpectedLinks
}
type DagMetaNodes struct {
DataNode ipld.Node
MetaNode ipld.Node
}
// GetChildrenForDagWithMeta returns a struct containing both the meta root node and
// the data root node, if the given 'nd' is top of the DAG with token metadata.
// Return `nil, nil` if 'nd' is no such node and there is no error.
func GetChildrenForDagWithMeta(ctx context.Context, nd ipld.Node, ds ipld.DAGService) (*DagMetaNodes, error) {
if nd == nil {
return nil, errors.New("invalid argument value returned: nil")
}
_, ok := nd.(*dag.ProtoNode)
if !ok {
return nil, errors.New("expected Dag profobuf node")
}
if len(nd.Links()) < 2 {
return nil, nil
}
nodes := &DagMetaNodes{}
for i := 0; i < 2; i++ {
lnk := nd.Links()[i]
c := lnk.Cid
child, err := ds.Get(ctx, c)
if err != nil {
return nil, err
}
childNode, ok := child.(*dag.ProtoNode)
if !ok {
return nil, err
}
if i == 0 {
// Make sure first child is of TTokenMeta.
// If not, return nil.
fsType, err := GetFSType(childNode)
if err != nil {
return nil, err
}
if TTokenMeta != fsType {
return nil, nil
}
nodes.MetaNode = child
} else {
nodes.DataNode = child
}
}
return nodes, nil
}
func GetFSType(n *dag.ProtoNode) (pb.Data_DataType, error) {
d, err := FSNodeFromBytes(n.Data())
if err != nil {
return 0, err
}
return d.Type(), nil
}