Skip to content

Commit c8f80b9

Browse files
committed
Create a raw node instead of a file node when there is no content.
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 <[email protected]>
1 parent 190524b commit c8f80b9

File tree

5 files changed

+73
-27
lines changed

5 files changed

+73
-27
lines changed

core/coreapi/unixfs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var helloStr = "hello, world!"
3535
var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", nil, nil)
3636

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

4040
func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
4141
var ident config.Identity

importer/balanced/builder.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
5151

5252
}
5353
if root == nil {
54-
root = db.NewUnixfsNode()
54+
// this should only happen with an empty node, so return a leaf
55+
var err error
56+
root, err = db.NewLeaf(nil)
57+
if err != nil {
58+
return nil, err
59+
}
5560
}
5661

5762
out, err := db.Add(root)

importer/helpers/dagbuilder.go

+29-24
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,34 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
122122
return n
123123
}
124124

125+
// NewLeaf creates a leaf node filled with data
126+
func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {
127+
if len(data) > BlockSizeLimit {
128+
return nil, ErrSizeLimitExceeded
129+
}
130+
131+
if db.rawLeaves {
132+
if db.prefix == nil {
133+
return &UnixfsNode{
134+
rawnode: dag.NewRawNode(data),
135+
raw: true,
136+
}, nil
137+
}
138+
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
139+
if err != nil {
140+
return nil, err
141+
}
142+
return &UnixfsNode{
143+
rawnode: rawnode,
144+
raw: true,
145+
}, nil
146+
}
147+
148+
blk := db.newUnixfsBlock()
149+
blk.SetData(data)
150+
return blk, nil
151+
}
152+
125153
// newUnixfsBlock creates a new Unixfs node to represent a raw data block
126154
func (db *DagBuilderHelper) newUnixfsBlock() *UnixfsNode {
127155
n := &UnixfsNode{
@@ -164,30 +192,7 @@ func (db *DagBuilderHelper) GetNextDataNode() (*UnixfsNode, error) {
164192
return nil, nil
165193
}
166194

167-
if len(data) > BlockSizeLimit {
168-
return nil, ErrSizeLimitExceeded
169-
}
170-
171-
if db.rawLeaves {
172-
if db.prefix == nil {
173-
return &UnixfsNode{
174-
rawnode: dag.NewRawNode(data),
175-
raw: true,
176-
}, nil
177-
}
178-
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
179-
if err != nil {
180-
return nil, err
181-
}
182-
return &UnixfsNode{
183-
rawnode: rawnode,
184-
raw: true,
185-
}, nil
186-
}
187-
188-
blk := db.newUnixfsBlock()
189-
blk.SetData(data)
190-
return blk, nil
195+
return db.NewLeaf(data)
191196
}
192197

193198
// SetPosInfo sets the offset information of a node using the fullpath and stat

test/sharness/t0040-add-and-cat.sh

+36
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ test_add_cat_file() {
166166
echo "added $HASH .hello.txt" >expected &&
167167
test_cmp expected actual
168168
'
169+
170+
test_expect_success "add zero length file" '
171+
touch zero-length-file &&
172+
ZEROHASH=$(ipfs add -q zero-length-file) &&
173+
echo $ZEROHASH
174+
'
175+
176+
test_expect_success "zero length file has correct hash" '
177+
test "$ZEROHASH" = Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW
178+
'
179+
180+
test_expect_success "cat zero length file" '
181+
ipfs cat $ZEROHASH > zero-length-file_out
182+
'
183+
184+
test_expect_success "make sure it looks good" '
185+
test_cmp zero-length-file zero-length-file_out
186+
'
169187
}
170188

171189
test_add_cat_5MB() {
@@ -221,6 +239,24 @@ test_add_cat_raw() {
221239
test_expect_success "make sure it looks good" '
222240
test_cmp afile afile_out
223241
'
242+
243+
test_expect_success "add zero length file with raw-leaves" '
244+
touch zero-length-file &&
245+
ZEROHASH=$(ipfs add -q --raw-leaves zero-length-file) &&
246+
echo $ZEROHASH
247+
'
248+
249+
test_expect_success "zero length file has correct hash" '
250+
test "$ZEROHASH" = zb2rhmy65F3REf8SZp7De11gxtECBGgUKaLdiDj7MCGCHxbDW
251+
'
252+
253+
test_expect_success "cat zero length file" '
254+
ipfs cat $ZEROHASH > zero-length-file_out
255+
'
256+
257+
test_expect_success "make sure it looks good" '
258+
test_cmp zero-length-file zero-length-file_out
259+
'
224260
}
225261

226262
test_add_cat_expensive() {

test/sharness/t0111-gateway-writeable.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test_launch_ipfs_daemon --writable
1414
test_expect_success "ipfs daemon --writable overrides config" '
1515
curl -v -X POST http://$GWAY_ADDR/ipfs/ 2> outfile &&
1616
grep "HTTP/1.1 201 Created" outfile &&
17-
grep "Location: /ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" outfile
17+
grep "Location: /ipfs/Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW" outfile
1818
'
1919
test_kill_ipfs_daemon
2020

0 commit comments

Comments
 (0)