Skip to content

Commit

Permalink
Finish basic support for raw nodes in dag modifier.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Jun 20, 2017
1 parent 9619c84 commit b92e53b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
37 changes: 37 additions & 0 deletions test/sharness/t0250-files-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_expect_success "can create some files for testing" '
FILE1=$(echo foo | ipfs add -q) &&
FILE2=$(echo bar | ipfs add -q) &&
FILE3=$(echo baz | ipfs add -q) &&
FILE9=$(echo zip | ipfs add -q --raw-leaves) &&
mkdir stuff_test &&
echo cats > stuff_test/a &&
echo dogs > stuff_test/b &&
Expand Down Expand Up @@ -252,6 +253,42 @@ test_files_api() {
test_cmp roothash roothashafter
'

# test raw node

test_expect_success "can put a raw-node into root" '
ipfs files cp /ipfs/$FILE9 /file9
'

test_expect_success "file shows up in root" '
verify_dir_contents / file9 cats
'

test_expect_success "can read file" '
ipfs files read /file9 > file9out
'

test_expect_success "output looks good" '
echo zip > expected &&
test_cmp expected file9out
'

test_expect_success "can remove file from root" '
ipfs files rm /file9
'

test_expect_success "file no longer appears" '
verify_dir_contents / cats
'

test_expect_success "check root hash" '
ipfs files stat --hash / > roothash
'

test_expect_success "check root hash was not changed" '
ipfs files stat --hash / > roothashafter &&
test_cmp roothash roothashafter
'

# test read options

test_expect_success "read from offset works" '
Expand Down
14 changes: 8 additions & 6 deletions unixfs/mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ var ErrNoRawYet = fmt.Errorf("currently only fully support protonodes in the dag
func (dm *DagModifier) Size() (int64, error) {
pbnd, ok := dm.curNode.(*mdag.ProtoNode)
if !ok {
if dm.wrBuf == nil {
sz, err := dm.curNode.Size()
return int64(sz), err
}
return 0, ErrNoRawYet
}

Expand All @@ -162,10 +166,8 @@ func (dm *DagModifier) Size() (int64, error) {
return 0, err
}

if dm.wrBuf != nil {
if uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
return int64(dm.wrBuf.Len()) + int64(dm.writeStart), nil
}
if dm.wrBuf != nil && uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
return int64(dm.wrBuf.Len()) + int64(dm.writeStart), nil
}

return int64(pbn.GetFilesize()), nil
Expand Down Expand Up @@ -397,12 +399,12 @@ func (dm *DagModifier) CtxReadFull(ctx context.Context, b []byte) (int, error) {
}

// GetNode gets the modified DAG Node
func (dm *DagModifier) GetNode() (*mdag.ProtoNode, error) {
func (dm *DagModifier) GetNode() (node.Node, error) {
err := dm.Sync()
if err != nil {
return nil, err
}
return dm.curNode.Copy().(*mdag.ProtoNode), nil
return dm.curNode.Copy(), nil
}

// HasChanges returned whether or not there are unflushed changes to this dag
Expand Down
3 changes: 2 additions & 1 deletion unixfs/mod/dagmodifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

h "github.com/ipfs/go-ipfs/importer/helpers"
trickle "github.com/ipfs/go-ipfs/importer/trickle"
mdag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
testu "github.com/ipfs/go-ipfs/unixfs/test"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestDagModifierBasic(t *testing.T) {
t.Fatal(err)
}

size, err := ft.DataSize(node.Data())
size, err := ft.DataSize(node.(*mdag.ProtoNode).Data())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit b92e53b

Please sign in to comment.