Skip to content

Commit

Permalink
Create a raw node instead of a file node when there is no content.
Browse files Browse the repository at this point in the history
This fixes things so when raw-leaves are enabled a zero size file creates
a zero size raw leaf.  When raw-leaves are not enabled the hash created
changes from QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH to
Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW, since the type field
changed from TFile to TRaw.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Feb 13, 2018
1 parent 190524b commit c8f80b9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
2 changes: 1 addition & 1 deletion core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var helloStr = "hello, world!"
var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", nil, nil)

// `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
var emptyFile = coreapi.ResolvedPath("/ipfs/Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW", nil, nil)

func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
var ident config.Identity
Expand Down
7 changes: 6 additions & 1 deletion importer/balanced/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {

}
if root == nil {
root = db.NewUnixfsNode()
// this should only happen with an empty node, so return a leaf
var err error
root, err = db.NewLeaf(nil)
if err != nil {
return nil, err
}
}

out, err := db.Add(root)
Expand Down
53 changes: 29 additions & 24 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
return n
}

// NewLeaf creates a leaf node filled with data
func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {
if len(data) > BlockSizeLimit {
return nil, ErrSizeLimitExceeded
}

if db.rawLeaves {
if db.prefix == nil {
return &UnixfsNode{
rawnode: dag.NewRawNode(data),
raw: true,
}, nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
if err != nil {
return nil, err
}
return &UnixfsNode{
rawnode: rawnode,
raw: true,
}, nil
}

blk := db.newUnixfsBlock()
blk.SetData(data)
return blk, nil
}

// newUnixfsBlock creates a new Unixfs node to represent a raw data block
func (db *DagBuilderHelper) newUnixfsBlock() *UnixfsNode {
n := &UnixfsNode{
Expand Down Expand Up @@ -164,30 +192,7 @@ func (db *DagBuilderHelper) GetNextDataNode() (*UnixfsNode, error) {
return nil, nil
}

if len(data) > BlockSizeLimit {
return nil, ErrSizeLimitExceeded
}

if db.rawLeaves {
if db.prefix == nil {
return &UnixfsNode{
rawnode: dag.NewRawNode(data),
raw: true,
}, nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
if err != nil {
return nil, err
}
return &UnixfsNode{
rawnode: rawnode,
raw: true,
}, nil
}

blk := db.newUnixfsBlock()
blk.SetData(data)
return blk, nil
return db.NewLeaf(data)
}

// SetPosInfo sets the offset information of a node using the fullpath and stat
Expand Down
36 changes: 36 additions & 0 deletions test/sharness/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ test_add_cat_file() {
echo "added $HASH .hello.txt" >expected &&
test_cmp expected actual
'

test_expect_success "add zero length file" '
touch zero-length-file &&
ZEROHASH=$(ipfs add -q zero-length-file) &&
echo $ZEROHASH
'

test_expect_success "zero length file has correct hash" '
test "$ZEROHASH" = Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW
'

test_expect_success "cat zero length file" '
ipfs cat $ZEROHASH > zero-length-file_out
'

test_expect_success "make sure it looks good" '
test_cmp zero-length-file zero-length-file_out
'
}

test_add_cat_5MB() {
Expand Down Expand Up @@ -221,6 +239,24 @@ test_add_cat_raw() {
test_expect_success "make sure it looks good" '
test_cmp afile afile_out
'

test_expect_success "add zero length file with raw-leaves" '
touch zero-length-file &&
ZEROHASH=$(ipfs add -q --raw-leaves zero-length-file) &&
echo $ZEROHASH
'

test_expect_success "zero length file has correct hash" '
test "$ZEROHASH" = zb2rhmy65F3REf8SZp7De11gxtECBGgUKaLdiDj7MCGCHxbDW
'

test_expect_success "cat zero length file" '
ipfs cat $ZEROHASH > zero-length-file_out
'

test_expect_success "make sure it looks good" '
test_cmp zero-length-file zero-length-file_out
'
}

test_add_cat_expensive() {
Expand Down
2 changes: 1 addition & 1 deletion test/sharness/t0111-gateway-writeable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_launch_ipfs_daemon --writable
test_expect_success "ipfs daemon --writable overrides config" '
curl -v -X POST http://$GWAY_ADDR/ipfs/ 2> outfile &&
grep "HTTP/1.1 201 Created" outfile &&
grep "Location: /ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" outfile
grep "Location: /ipfs/Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW" outfile
'
test_kill_ipfs_daemon

Expand Down

0 comments on commit c8f80b9

Please sign in to comment.