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

Commit

Permalink
fix(fsnode): issue #17
Browse files Browse the repository at this point in the history
  • Loading branch information
overbool committed Sep 20, 2018
1 parent 90ca3d9 commit ffb1fae
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 3 additions & 1 deletion hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
return nil, dag.ErrNotProtobuf
}

pbd, err := format.FromBytes(pbnd.Data())
fsn, err := format.FSNodeFromBytes(pbnd.Data())
if err != nil {
return nil, err
}

pbd := fsn.Format()

if pbd.GetType() != upb.Data_HAMTShard {
return nil, fmt.Errorf("node was not a dir shard")
}
Expand Down
7 changes: 4 additions & 3 deletions importer/trickle/trickledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ func verifyTDagRec(n ipld.Node, depth int, p VerifyParams) error {
// zero depth dag is raw data block
switch nd := n.(type) {
case *dag.ProtoNode:
pbn, err := ft.FromBytes(nd.Data())
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return err
}

if pbn.GetType() != ft.TRaw {
if fsn.Format().GetType() != ft.TRaw {
return errors.New("expected raw block")
}

Expand Down Expand Up @@ -325,10 +325,11 @@ func verifyTDagRec(n ipld.Node, depth int, p VerifyParams) error {
}

// Verify this is a branch node
pbn, err := ft.FromBytes(nd.Data())
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return err
}
pbn := fsn.Format()

if pbn.GetType() != ft.TFile {
return fmt.Errorf("expected file as branch node, got: %s", pbn.GetType())
Expand Down
4 changes: 3 additions & 1 deletion io/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func ResolveUnixfsOnce(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
switch nd := nd.(type) {
case *dag.ProtoNode:
upb, err := ft.FromBytes(nd.Data())
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
// Not a unixfs node, use standard object traversal code
lnk, err := nd.GetNodeLink(names[0])
Expand All @@ -26,6 +26,8 @@ func ResolveUnixfsOnce(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, na
return lnk, names[1:], nil
}

upb := fsn.Format()

switch upb.GetType() {
case ft.THAMTShard:
rods := dag.NewReadOnlyDagService(ds)
Expand Down
16 changes: 9 additions & 7 deletions mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ func (dm *DagModifier) Size() (int64, error) {
func fileSize(n ipld.Node) (uint64, error) {
switch nd := n.(type) {
case *mdag.ProtoNode:
f, err := ft.FromBytes(nd.Data())
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return 0, err
}
return f.GetFilesize(), nil
return fsn.Format().GetFilesize(), nil
case *mdag.RawNode:
return uint64(len(nd.RawData())), nil
default:
Expand Down Expand Up @@ -238,11 +238,13 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (cid.Cid, error) {
if len(n.Links()) == 0 {
switch nd0 := n.(type) {
case *mdag.ProtoNode:
f, err := ft.FromBytes(nd0.Data())
fsn, err := ft.FSNodeFromBytes(nd0.Data())
if err != nil {
return cid.Cid{}, err
}

f := fsn.Format()

_, err = dm.wrBuf.Read(f.Data[offset:])
if err != nil && err != io.EOF {
return cid.Cid{}, err
Expand Down Expand Up @@ -300,13 +302,13 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (cid.Cid, error) {
return cid.Cid{}, ErrNotUnixfs
}

f, err := ft.FromBytes(node.Data())
fsn, err := ft.FSNodeFromBytes(node.Data())
if err != nil {
return cid.Cid{}, err
}

var cur uint64
for i, bs := range f.GetBlocksizes() {
for i, bs := range fsn.Format().GetBlocksizes() {
// We found the correct child to write into
if cur+bs > offset {
child, err := node.Links()[i].GetNode(dm.ctx, dm.dagserv)
Expand Down Expand Up @@ -510,11 +512,11 @@ func (dm *DagModifier) dagTruncate(ctx context.Context, n ipld.Node, size uint64
switch nd := n.(type) {
case *mdag.ProtoNode:
// TODO: this can likely be done without marshaling and remarshaling
pbn, err := ft.FromBytes(nd.Data())
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return nil, err
}
nd.SetData(ft.WrapData(pbn.Data[:size]))
nd.SetData(ft.WrapData(fsn.Format().Data[:size]))
return nd, nil
case *mdag.RawNode:
return mdag.NewRawNodeWPrefix(nd.RawData()[:size], nd.Cid().Prefix())
Expand Down
3 changes: 2 additions & 1 deletion test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ func ArrComp(a, b []byte) error {

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

for i := 0; i < indent; i++ {
fmt.Print(" ")
Expand Down
5 changes: 5 additions & 0 deletions unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func NewFSNode(dataType pb.Data_DataType) *FSNode {
return n
}

// Format returns the format inside this node
func (n *FSNode) Format() *pb.Data {
return &n.format
}

// AddBlockSize adds the size of the next child block of this node
func (n *FSNode) AddBlockSize(s uint64) {
n.UpdateFilesize(int64(s))
Expand Down

0 comments on commit ffb1fae

Please sign in to comment.