Golint: unixfs main module
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 6, 2018 at 12:05 UTC
16a54a85615267dae18e49f83ebf79dbf6e80556
2 files changed
+33
-8
unixfs/unixfs.go
renamed
+33
-8
@@ -1,4 +1,4 @@
1
-// Package format implements a data format for files in the IPFS filesystem It
1
+// Package unixfs implements a data format for files in the IPFS filesystem It
2
// is not the only format in ipfs, but it is the one that the filesystem
3
// assumes
4
package unixfs
@@ -6,11 +6,13 @@ package unixfs
6
import (
7
"errors"
8
9
+ proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
10
+
11
dag "github.com/ipfs/go-ipfs/merkledag"
12
pb "github.com/ipfs/go-ipfs/unixfs/pb"
11
- proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
13
)
14
15
+// Shorthands for protobuffer types
16
const (
17
TRaw = pb.Data_Raw
18
TFile = pb.Data_File
@@ -20,10 +22,14 @@ const (
22
THAMTShard = pb.Data_HAMTShard
23
)
24
23
-var ErrMalformedFileFormat = errors.New("malformed data in file format")
24
-var ErrInvalidDirLocation = errors.New("found directory node in unexpected place")
25
-var ErrUnrecognizedType = errors.New("unrecognized node type")
25
+// Common errors
26
+var (
27
+ ErrMalformedFileFormat = errors.New("malformed data in file format")
28
+ ErrInvalidDirLocation = errors.New("found directory node in unexpected place")
29
+ ErrUnrecognizedType = errors.New("unrecognized node type")
30
+)
31
32
+// FromBytes unmarshals a byte slice as protobuf Data.
33
func FromBytes(data []byte) (*pb.Data, error) {
34
pbdata := new(pb.Data)
35
err := proto.Unmarshal(data, pbdata)
@@ -33,6 +39,8 @@ func FromBytes(data []byte) (*pb.Data, error) {
39
return pbdata, nil
40
}
41
42
+// FilePBData creates a protobuf File with the given
43
+// byte slice and returns the marshaled protobuf bytes representing it.
44
func FilePBData(data []byte, totalsize uint64) []byte {
45
pbfile := new(pb.Data)
46
typ := pb.Data_File
@@ -98,6 +106,7 @@ func SymlinkData(path string) ([]byte, error) {
106
return out, nil
107
}
108
109
+// UnwrapData unmarshals a protobuf messages and returns the contents.
110
func UnwrapData(data []byte) ([]byte, error) {
111
pbdata := new(pb.Data)
112
err := proto.Unmarshal(data, pbdata)
@@ -107,6 +116,10 @@ func UnwrapData(data []byte) ([]byte, error) {
116
return pbdata.GetData(), nil
117
}
118
119
+// DataSize returns the size of the contents in protobuf wrapped slice.
120
+// For raw data it simply provides the length of it. For Data_Files, it
121
+// will return the associated filesize. Note that Data_Directories will
122
+// return an error.
123
func DataSize(data []byte) (uint64, error) {
124
pbdata := new(pb.Data)
125
err := proto.Unmarshal(data, pbdata)
@@ -116,16 +129,17 @@ func DataSize(data []byte) (uint64, error) {
129
130
switch pbdata.GetType() {
131
case pb.Data_Directory:
119
- return 0, errors.New("Cant get data size of directory!")
132
+ return 0, errors.New("Cant get data size of directory")
133
case pb.Data_File:
134
return pbdata.GetFilesize(), nil
135
case pb.Data_Raw:
136
return uint64(len(pbdata.GetData())), nil
137
default:
125
- return 0, errors.New("Unrecognized node data type!")
138
+ return 0, errors.New("Unrecognized node data type")
139
}
140
}
141
142
+// An FSNode represents a filesystem object.
143
type FSNode struct {
144
Data []byte
145
@@ -139,6 +153,7 @@ type FSNode struct {
153
Type pb.Data_DataType
154
}
155
156
+// FSNodeFromBytes unmarshal a protobuf message onto an FSNode.
157
func FSNodeFromBytes(b []byte) (*FSNode, error) {
158
pbn := new(pb.Data)
159
err := proto.Unmarshal(b, pbn)
@@ -160,11 +175,13 @@ func (n *FSNode) AddBlockSize(s uint64) {
175
n.blocksizes = append(n.blocksizes, s)
176
}
177
178
+// RemoveBlockSize removes the given child block's size.
179
func (n *FSNode) RemoveBlockSize(i int) {
180
n.subtotal -= n.blocksizes[i]
181
n.blocksizes = append(n.blocksizes[:i], n.blocksizes[i+1:]...)
182
}
183
184
+// GetBytes marshals this node as a protobuf message.
185
func (n *FSNode) GetBytes() ([]byte, error) {
186
pbn := new(pb.Data)
187
pbn.Type = &n.Type
@@ -180,16 +197,19 @@ func (n *FSNode) FileSize() uint64 {
197
return uint64(len(n.Data)) + n.subtotal
198
}
199
200
+// NumChildren returns the number of child blocks of this node
201
func (n *FSNode) NumChildren() int {
202
return len(n.blocksizes)
203
}
204
205
+// Metadata is used to store additional FSNode information.
206
type Metadata struct {
207
MimeType string
208
Size uint64
209
}
210
192
-//MetadataFromBytes Unmarshals a protobuf message into Metadata.
211
+// MetadataFromBytes Unmarshals a protobuf Data message into Metadata.
212
+// The provided slice should have been encoded with BytesForMetadata().
213
func MetadataFromBytes(b []byte) (*Metadata, error) {
214
pbd := new(pb.Data)
215
err := proto.Unmarshal(b, pbd)
@@ -210,12 +230,16 @@ func MetadataFromBytes(b []byte) (*Metadata, error) {
230
return md, nil
231
}
232
233
+// Bytes marshals Metadata as a protobuf message of Metadata type.
234
func (m *Metadata) Bytes() ([]byte, error) {
235
pbm := new(pb.Metadata)
236
pbm.MimeType = &m.MimeType
237
return proto.Marshal(pbm)
238
}
239
240
+// BytesForMetadata wraps the given Metadata as a profobuf message of Data type,
241
+// setting the DataType to Metadata. The wrapped bytes are itself the
242
+// result of calling m.Bytes().
243
func BytesForMetadata(m *Metadata) ([]byte, error) {
244
pbd := new(pb.Data)
245
pbd.Filesize = proto.Uint64(m.Size)
@@ -230,6 +254,7 @@ func BytesForMetadata(m *Metadata) ([]byte, error) {
254
return proto.Marshal(pbd)
255
}
256
257
+// EmptyDirNode creates an empty folder Protonode.
258
func EmptyDirNode() *dag.ProtoNode {
259
return dag.NodeWithData(FolderPBData())
260
}