Golint: unixfs/io
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 6, 2018 at 13:48 UTC
ca4271b7827fe3234d192011dac52e0b0b600e9d
6 files changed
+51
-31
unixfs/io/bufdagreader.go
+3
-1
@@ -10,7 +10,9 @@ type bufDagReader struct {
10
*bytes.Reader
11
}
12
13
-func NewBufDagReader(b []byte) *bufDagReader {
13
+// newBufDagReader returns a DAG reader for the given byte slice.
14
+// BufDagReader is used to read RawNodes.
15
+func newBufDagReader(b []byte) *bufDagReader {
16
return &bufDagReader{bytes.NewReader(b)}
17
}
18
unixfs/io/dagreader.go
+10
-4
@@ -14,10 +14,15 @@ import (
14
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
15
)
16
17
-var ErrIsDir = errors.New("this dag node is a directory")
18
-
19
-var ErrCantReadSymlinks = errors.New("cannot currently read symlinks")
17
+// Common errors
18
+var (
19
+ ErrIsDir = errors.New("this dag node is a directory")
20
+ ErrCantReadSymlinks = errors.New("cannot currently read symlinks")
21
+)
22
23
+// A DagReader represents a ReadSeekCloser which offers additional methods
24
+// like Size. Different implementations of readers are used for the different
25
+// types of unixfs/protobuf-encoded nodes.
26
type DagReader interface {
27
ReadSeekCloser
28
Size() uint64
@@ -25,6 +30,7 @@ type DagReader interface {
30
Offset() int64
31
}
32
33
+// A ReadSeekCloser implements interfaces to read, write, seek and close.
34
type ReadSeekCloser interface {
35
io.Reader
36
io.Seeker
@@ -37,7 +43,7 @@ type ReadSeekCloser interface {
43
func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagReader, error) {
44
switch n := n.(type) {
45
case *mdag.RawNode:
40
- return NewBufDagReader(n.RawData()), nil
46
+ return newBufDagReader(n.RawData()), nil
47
case *mdag.ProtoNode:
48
pb := new(ftpb.Data)
49
if err := proto.Unmarshal(n.Data(), pb); err != nil {
unixfs/io/dagreader_test.go
+1
-1
@@ -102,7 +102,7 @@ func TestSeekAndReadLarge(t *testing.T) {
102
t.Fatal("seeked read failed")
103
}
104
105
- pbdr := reader.(*pbDagReader)
105
+ pbdr := reader.(*PBDagReader)
106
var count int
107
for i, p := range pbdr.promises {
108
if i > 20 && i < 30 {
unixfs/io/dirbuilder.go
+13
-4
@@ -8,8 +8,8 @@ import (
8
mdag "github.com/ipfs/go-ipfs/merkledag"
9
format "github.com/ipfs/go-ipfs/unixfs"
10
hamt "github.com/ipfs/go-ipfs/unixfs/hamt"
11
- cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
11
12
+ cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
13
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
14
)
15
@@ -25,11 +25,14 @@ var UseHAMTSharding = false
25
// DefaultShardWidth is the default value used for hamt sharding width.
26
var DefaultShardWidth = 256
27
28
+// Directory allows to work with UnixFS directory nodes, adding and removing
29
+// children. It allows to work with different directory schemes,
30
+// like the classic or the HAMT one.
31
type Directory struct {
32
dserv ipld.DAGService
33
dirnode *mdag.ProtoNode
34
32
- shard *hamt.HamtShard
35
+ shard *hamt.Shard
36
}
37
38
// NewDirectory returns a Directory. It needs a DAGService to add the Children
@@ -37,7 +40,7 @@ func NewDirectory(dserv ipld.DAGService) *Directory {
40
db := new(Directory)
41
db.dserv = dserv
42
if UseHAMTSharding {
40
- s, err := hamt.NewHamtShard(dserv, DefaultShardWidth)
43
+ s, err := hamt.NewShard(dserv, DefaultShardWidth)
44
if err != nil {
45
panic(err) // will only panic if DefaultShardWidth is a bad value
46
}
@@ -113,7 +116,7 @@ func (d *Directory) AddChild(ctx context.Context, name string, nd ipld.Node) err
116
}
117
118
func (d *Directory) switchToSharding(ctx context.Context) error {
116
- s, err := hamt.NewHamtShard(d.dserv, DefaultShardWidth)
119
+ s, err := hamt.NewShard(d.dserv, DefaultShardWidth)
120
if err != nil {
121
return err
122
}
@@ -136,6 +139,7 @@ func (d *Directory) switchToSharding(ctx context.Context) error {
139
return nil
140
}
141
142
+// ForEachLink applies the given function to Links in the directory.
143
func (d *Directory) ForEachLink(ctx context.Context, f func(*ipld.Link) error) error {
144
if d.shard == nil {
145
for _, l := range d.dirnode.Links() {
@@ -149,6 +153,7 @@ func (d *Directory) ForEachLink(ctx context.Context, f func(*ipld.Link) error) e
153
return d.shard.ForEachLink(ctx, f)
154
}
155
156
+// Links returns the all the links in the directory node.
157
func (d *Directory) Links(ctx context.Context) ([]*ipld.Link, error) {
158
if d.shard == nil {
159
return d.dirnode.Links(), nil
@@ -157,6 +162,9 @@ func (d *Directory) Links(ctx context.Context) ([]*ipld.Link, error) {
162
return d.shard.EnumLinks(ctx)
163
}
164
165
+// Find returns the ipld.Node with the given name, if it is contained in this
166
+// directory. Find only searches in the most inmediate links, and not
167
+// recursively in the tree.
168
func (d *Directory) Find(ctx context.Context, name string) (ipld.Node, error) {
169
if d.shard == nil {
170
lnk, err := d.dirnode.GetNodeLink(name)
@@ -179,6 +187,7 @@ func (d *Directory) Find(ctx context.Context, name string) (ipld.Node, error) {
187
return lnk.GetNode(ctx, d.dserv)
188
}
189
190
+// RemoveChild removes the child with the given name.
191
func (d *Directory) RemoveChild(ctx context.Context, name string) error {
192
if d.shard == nil {
193
return d.dirnode.RemoveNodeLink(name)
unixfs/io/doc.go
+1
-1
@@ -1,3 +1,3 @@
1
-// package unixfs/io implements convenience objects for working with the ipfs
1
+// Package io implements convenience objects for working with the ipfs
2
// unixfs data format.
3
package io
unixfs/io/pbdagreader.go
+23
-20
@@ -15,8 +15,8 @@ import (
15
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
16
)
17
18
-// DagReader provides a way to easily read the data contained in a dag.
19
-type pbDagReader struct {
18
+// PBDagReader provides a way to easily read the data contained in a dag.
19
+type PBDagReader struct {
20
serv ipld.NodeGetter
21
22
// the node being read
@@ -48,16 +48,16 @@ type pbDagReader struct {
48
cancel func()
49
}
50
51
-var _ DagReader = (*pbDagReader)(nil)
51
+var _ DagReader = (*PBDagReader)(nil)
52
53
// NewPBFileReader constructs a new PBFileReader.
54
-func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv ipld.NodeGetter) *pbDagReader {
54
+func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv ipld.NodeGetter) *PBDagReader {
55
fctx, cancel := context.WithCancel(ctx)
56
curLinks := getLinkCids(n)
57
- return &pbDagReader{
57
+ return &PBDagReader{
58
node: n,
59
serv: serv,
60
- buf: NewBufDagReader(pb.GetData()),
60
+ buf: newBufDagReader(pb.GetData()),
61
promises: make([]*ipld.NodePromise, len(curLinks)),
62
links: curLinks,
63
ctx: fctx,
@@ -68,7 +68,7 @@ func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv
68
69
const preloadSize = 10
70
71
-func (dr *pbDagReader) preloadNextNodes(ctx context.Context) {
71
+func (dr *PBDagReader) preloadNextNodes(ctx context.Context) {
72
beg := dr.linkPosition
73
end := beg + preloadSize
74
if end >= len(dr.links) {
@@ -82,7 +82,7 @@ func (dr *pbDagReader) preloadNextNodes(ctx context.Context) {
82
83
// precalcNextBuf follows the next link in line and loads it from the
84
// DAGService, setting the next buffer to read from
85
-func (dr *pbDagReader) precalcNextBuf(ctx context.Context) error {
85
+func (dr *PBDagReader) precalcNextBuf(ctx context.Context) error {
86
if dr.buf != nil {
87
dr.buf.Close() // Just to make sure
88
dr.buf = nil
@@ -119,7 +119,7 @@ func (dr *pbDagReader) precalcNextBuf(ctx context.Context) error {
119
dr.buf = NewPBFileReader(dr.ctx, nxt, pb, dr.serv)
120
return nil
121
case ftpb.Data_Raw:
122
- dr.buf = NewBufDagReader(pb.GetData())
122
+ dr.buf = newBufDagReader(pb.GetData())
123
return nil
124
case ftpb.Data_Metadata:
125
return errors.New("shouldnt have had metadata object inside file")
@@ -145,17 +145,17 @@ func getLinkCids(n ipld.Node) []*cid.Cid {
145
}
146
147
// Size return the total length of the data from the DAG structured file.
148
-func (dr *pbDagReader) Size() uint64 {
148
+func (dr *PBDagReader) Size() uint64 {
149
return dr.pbdata.GetFilesize()
150
}
151
152
// Read reads data from the DAG structured file
153
-func (dr *pbDagReader) Read(b []byte) (int, error) {
153
+func (dr *PBDagReader) Read(b []byte) (int, error) {
154
return dr.CtxReadFull(dr.ctx, b)
155
}
156
157
// CtxReadFull reads data from the DAG structured file
158
-func (dr *pbDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
158
+func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
159
if dr.buf == nil {
160
if err := dr.precalcNextBuf(ctx); err != nil {
161
return 0, err
@@ -189,7 +189,8 @@ func (dr *pbDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
189
}
190
}
191
192
-func (dr *pbDagReader) WriteTo(w io.Writer) (int64, error) {
192
+// WriteTo writes to the given writer.
193
+func (dr *PBDagReader) WriteTo(w io.Writer) (int64, error) {
194
if dr.buf == nil {
195
if err := dr.precalcNextBuf(dr.ctx); err != nil {
196
return 0, err
@@ -220,12 +221,14 @@ func (dr *pbDagReader) WriteTo(w io.Writer) (int64, error) {
221
}
222
}
223
223
-func (dr *pbDagReader) Close() error {
224
+// Close closes the reader.
225
+func (dr *PBDagReader) Close() error {
226
dr.cancel()
227
return nil
228
}
229
228
-func (dr *pbDagReader) Offset() int64 {
230
+// Offset returns the current reader offset
231
+func (dr *PBDagReader) Offset() int64 {
232
return dr.offset
233
}
234
@@ -233,7 +236,7 @@ func (dr *pbDagReader) Offset() int64 {
236
// interface matches standard unix seek
237
// TODO: check if we can do relative seeks, to reduce the amount of dagreader
238
// recreations that need to happen.
236
-func (dr *pbDagReader) Seek(offset int64, whence int) (int64, error) {
239
+func (dr *PBDagReader) Seek(offset int64, whence int) (int64, error) {
240
switch whence {
241
case io.SeekStart:
242
if offset < 0 {
@@ -253,17 +256,17 @@ func (dr *pbDagReader) Seek(offset int64, whence int) (int64, error) {
256
if dr.buf != nil {
257
dr.buf.Close()
258
}
256
- dr.buf = NewBufDagReader(pb.GetData()[offset:])
259
+ dr.buf = newBufDagReader(pb.GetData()[offset:])
260
261
// start reading links from the beginning
262
dr.linkPosition = 0
263
dr.offset = offset
264
return offset, nil
262
- } else {
263
- // skip past root block data
264
- left -= int64(len(pb.Data))
265
}
266
267
+ // skip past root block data
268
+ left -= int64(len(pb.Data))
269
+
270
// iterate through links and find where we need to be
271
for i := 0; i < len(pb.Blocksizes); i++ {
272
if pb.Blocksizes[i] > uint64(left) {