Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 8bea61e

Browse files
committed
fix(fsnode): issue #17
1 parent 90ca3d9 commit 8bea61e

File tree

7 files changed

+48
-32
lines changed

7 files changed

+48
-32
lines changed

hamt/hamt.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,29 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
102102
return nil, dag.ErrNotProtobuf
103103
}
104104

105-
pbd, err := format.FromBytes(pbnd.Data())
105+
fsn, err := format.FSNodeFromBytes(pbnd.Data())
106106
if err != nil {
107107
return nil, err
108108
}
109109

110-
if pbd.GetType() != upb.Data_HAMTShard {
110+
111+
if fsn.Type() != upb.Data_HAMTShard {
111112
return nil, fmt.Errorf("node was not a dir shard")
112113
}
113114

114-
if pbd.GetHashType() != HashMurmur3 {
115+
if fsn.HashType() != HashMurmur3 {
115116
return nil, fmt.Errorf("only murmur3 supported as hash function")
116117
}
117118

118-
ds, err := makeShard(dserv, int(pbd.GetFanout()))
119+
ds, err := makeShard(dserv, int(fsn.Fanout()))
119120
if err != nil {
120121
return nil, err
121122
}
122123

123124
ds.nd = pbnd.Copy().(*dag.ProtoNode)
124125
ds.children = make([]child, len(pbnd.Links()))
125-
ds.bitfield.SetBytes(pbd.GetData())
126-
ds.hashFunc = pbd.GetHashType()
126+
ds.bitfield.SetBytes(fsn.Data())
127+
ds.hashFunc = fsn.HashType()
127128
ds.builder = ds.nd.CidBuilder()
128129

129130
return ds, nil

importer/trickle/trickledag.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ func verifyTDagRec(n ipld.Node, depth int, p VerifyParams) error {
277277
// zero depth dag is raw data block
278278
switch nd := n.(type) {
279279
case *dag.ProtoNode:
280-
pbn, err := ft.FromBytes(nd.Data())
280+
fsn, err := ft.FSNodeFromBytes(nd.Data())
281281
if err != nil {
282282
return err
283283
}
284284

285-
if pbn.GetType() != ft.TRaw {
285+
if fsn.Type() != ft.TRaw {
286286
return errors.New("expected raw block")
287287
}
288288

@@ -325,16 +325,16 @@ func verifyTDagRec(n ipld.Node, depth int, p VerifyParams) error {
325325
}
326326

327327
// Verify this is a branch node
328-
pbn, err := ft.FromBytes(nd.Data())
328+
fsn, err := ft.FSNodeFromBytes(nd.Data())
329329
if err != nil {
330330
return err
331331
}
332332

333-
if pbn.GetType() != ft.TFile {
334-
return fmt.Errorf("expected file as branch node, got: %s", pbn.GetType())
333+
if fsn.Type() != ft.TFile {
334+
return fmt.Errorf("expected file as branch node, got: %s", fsn.Type())
335335
}
336336

337-
if len(pbn.Data) > 0 {
337+
if len(fsn.Data()) > 0 {
338338
return errors.New("branch node should not have data")
339339
}
340340

io/resolve.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func ResolveUnixfsOnce(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
1616
switch nd := nd.(type) {
1717
case *dag.ProtoNode:
18-
upb, err := ft.FromBytes(nd.Data())
18+
fsn, err := ft.FSNodeFromBytes(nd.Data())
1919
if err != nil {
2020
// Not a unixfs node, use standard object traversal code
2121
lnk, err := nd.GetNodeLink(names[0])
@@ -26,7 +26,7 @@ func ResolveUnixfsOnce(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, na
2626
return lnk, names[1:], nil
2727
}
2828

29-
switch upb.GetType() {
29+
switch fsn.Type() {
3030
case ft.THAMTShard:
3131
rods := dag.NewReadOnlyDagService(ds)
3232
s, err := hamt.NewHamtFromDag(rods, nd)

mod/dagmodifier.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
trickle "github.com/ipfs/go-unixfs/importer/trickle"
1414
uio "github.com/ipfs/go-unixfs/io"
1515

16-
proto "github.com/gogo/protobuf/proto"
1716
cid "github.com/ipfs/go-cid"
1817
chunker "github.com/ipfs/go-ipfs-chunker"
1918
ipld "github.com/ipfs/go-ipld-format"
@@ -173,11 +172,11 @@ func (dm *DagModifier) Size() (int64, error) {
173172
func fileSize(n ipld.Node) (uint64, error) {
174173
switch nd := n.(type) {
175174
case *mdag.ProtoNode:
176-
f, err := ft.FromBytes(nd.Data())
175+
fsn, err := ft.FSNodeFromBytes(nd.Data())
177176
if err != nil {
178177
return 0, err
179178
}
180-
return f.GetFilesize(), nil
179+
return fsn.FileSize(), nil
181180
case *mdag.RawNode:
182181
return uint64(len(nd.RawData())), nil
183182
default:
@@ -238,18 +237,18 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (cid.Cid, error) {
238237
if len(n.Links()) == 0 {
239238
switch nd0 := n.(type) {
240239
case *mdag.ProtoNode:
241-
f, err := ft.FromBytes(nd0.Data())
240+
fsn, err := ft.FSNodeFromBytes(nd0.Data())
242241
if err != nil {
243242
return cid.Cid{}, err
244243
}
245244

246-
_, err = dm.wrBuf.Read(f.Data[offset:])
245+
_, err = dm.wrBuf.Read(fsn.Data()[offset:])
247246
if err != nil && err != io.EOF {
248247
return cid.Cid{}, err
249248
}
250249

251250
// Update newly written node..
252-
b, err := proto.Marshal(f)
251+
b, err := fsn.GetBytes()
253252
if err != nil {
254253
return cid.Cid{}, err
255254
}
@@ -300,13 +299,13 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (cid.Cid, error) {
300299
return cid.Cid{}, ErrNotUnixfs
301300
}
302301

303-
f, err := ft.FromBytes(node.Data())
302+
fsn, err := ft.FSNodeFromBytes(node.Data())
304303
if err != nil {
305304
return cid.Cid{}, err
306305
}
307306

308307
var cur uint64
309-
for i, bs := range f.GetBlocksizes() {
308+
for i, bs := range fsn.BlockSizes() {
310309
// We found the correct child to write into
311310
if cur+bs > offset {
312311
child, err := node.Links()[i].GetNode(dm.ctx, dm.dagserv)
@@ -510,11 +509,11 @@ func (dm *DagModifier) dagTruncate(ctx context.Context, n ipld.Node, size uint64
510509
switch nd := n.(type) {
511510
case *mdag.ProtoNode:
512511
// TODO: this can likely be done without marshaling and remarshaling
513-
pbn, err := ft.FromBytes(nd.Data())
512+
fsn, err := ft.FSNodeFromBytes(nd.Data())
514513
if err != nil {
515514
return nil, err
516515
}
517-
nd.SetData(ft.WrapData(pbn.Data[:size]))
516+
nd.SetData(ft.WrapData(fsn.Data()[:size]))
518517
return nd, nil
519518
case *mdag.RawNode:
520519
return mdag.NewRawNodeWPrefix(nd.RawData()[:size], nd.Cid().Prefix())

test/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ func ArrComp(a, b []byte) error {
107107

108108
// PrintDag pretty-prints the given dag to stdout.
109109
func PrintDag(nd *mdag.ProtoNode, ds ipld.DAGService, indent int) {
110-
pbd, err := ft.FromBytes(nd.Data())
110+
fsn, err := ft.FSNodeFromBytes(nd.Data())
111111
if err != nil {
112112
panic(err)
113113
}
114114

115115
for i := 0; i < indent; i++ {
116116
fmt.Print(" ")
117117
}
118-
fmt.Printf("{size = %d, type = %s, children = %d", pbd.GetFilesize(), pbd.GetType().String(), len(pbd.GetBlocksizes()))
118+
fmt.Printf("{size = %d, type = %s, children = %d", fsn.FileSize(), fsn.Type().String(), fsn.NumChildren())
119119
if len(nd.Links()) > 0 {
120120
fmt.Println()
121121
}

unixfs.go

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
)
3030

3131
// FromBytes unmarshals a byte slice as protobuf Data.
32+
// Deprecated: Use `FSNodeFromBytes` instead to avoid direct manipulation of `pb.Data`.
3233
func FromBytes(data []byte) (*pb.Data, error) {
3334
pbdata := new(pb.Data)
3435
err := proto.Unmarshal(data, pbdata)
@@ -182,6 +183,16 @@ func NewFSNode(dataType pb.Data_DataType) *FSNode {
182183
return n
183184
}
184185

186+
// HashType gets hash type of format
187+
func (n *FSNode) HashType() uint64 {
188+
return n.format.GetHashType()
189+
}
190+
191+
// Fanout gets fanout of format
192+
func (n *FSNode) Fanout() uint64 {
193+
return n.format.GetFanout()
194+
}
195+
185196
// AddBlockSize adds the size of the next child block of this node
186197
func (n *FSNode) AddBlockSize(s uint64) {
187198
n.UpdateFilesize(int64(s))
@@ -200,6 +211,11 @@ func (n *FSNode) BlockSize(i int) uint64 {
200211
return n.format.Blocksizes[i]
201212
}
202213

214+
// BlockSizes gets blocksizes of format
215+
func (n *FSNode) BlockSizes() []uint64 {
216+
return n.format.GetBlocksizes()
217+
}
218+
203219
// RemoveAllBlockSizes removes all the child block sizes of this node.
204220
func (n *FSNode) RemoveAllBlockSizes() {
205221
n.format.Blocksizes = []uint64{}

unixfs_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func TestPBdataTools(t *testing.T) {
7676
t.Fatal("Unwrap failed to produce the correct wrapped data.")
7777
}
7878

79-
rawPBdata, err := FromBytes(rawPB)
79+
rawPBdata, err := FSNodeFromBytes(rawPB)
8080
if err != nil {
8181
t.Fatal(err)
8282
}
8383

84-
isRaw := rawPBdata.GetType() == TRaw
84+
isRaw := rawPBdata.Type() == TRaw
8585
if !isRaw {
8686
t.Fatal("WrapData does not create pb.Data_Raw!")
8787
}
@@ -97,8 +97,8 @@ func TestPBdataTools(t *testing.T) {
9797
}
9898

9999
dirPB := FolderPBData()
100-
dir, err := FromBytes(dirPB)
101-
isDir := dir.GetType() == TDirectory
100+
dir, err := FSNodeFromBytes(dirPB)
101+
isDir := dir.Type() == TDirectory
102102
if !isDir {
103103
t.Fatal("FolderPBData does not create a directory!")
104104
}
@@ -115,8 +115,8 @@ func TestPBdataTools(t *testing.T) {
115115
t.Fatal(err)
116116
}
117117

118-
catSymPB, err := FromBytes(catSym)
119-
isSym := catSymPB.GetType() == TSymlink
118+
catSymPB, err := FSNodeFromBytes(catSym)
119+
isSym := catSymPB.Type() == TSymlink
120120
if !isSym {
121121
t.Fatal("Failed to make a Symlink.")
122122
}

0 commit comments

Comments
 (0)