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 <k@kevina.org>
Kevin Atkinson committed
Feb 13, 2018 at 17:15 UTC
485a5e59e9ad8b48d362fa36f52d99739065ed04
5 files changed
+73
-27
core/coreapi/unixfs_test.go
+1
-1
@@ -36,7 +36,7 @@ var helloStr = "hello, world!"
36
var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", nil, nil)
37
38
// `echo -n | ipfs add`
39
-var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
39
+var emptyFile = coreapi.ResolvedPath("/ipfs/Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW", nil, nil)
40
41
func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
42
var ident config.Identity
importer/balanced/builder.go
+6
-1
@@ -51,7 +51,12 @@ func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
51
52
}
53
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
+ }
60
}
61
62
out, err := db.Add(root)
importer/helpers/dagbuilder.go
+29
-24
@@ -122,6 +122,34 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
122
return n
123
}
124
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
+
153
// newUnixfsBlock creates a new Unixfs node to represent a raw data block
154
func (db *DagBuilderHelper) newUnixfsBlock() *UnixfsNode {
155
n := &UnixfsNode{
@@ -164,30 +192,7 @@ func (db *DagBuilderHelper) GetNextDataNode() (*UnixfsNode, error) {
192
return nil, nil
193
}
194
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)
196
}
197
198
// SetPosInfo sets the offset information of a node using the fullpath and stat
test/sharness/t0040-add-and-cat.sh
+36
@@ -166,6 +166,24 @@ test_add_cat_file() {
166
echo "added $HASH .hello.txt" >expected &&
167
test_cmp expected actual
168
'
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
+ '
187
}
188
189
test_add_cat_5MB() {
@@ -221,6 +239,24 @@ test_add_cat_raw() {
239
test_expect_success "make sure it looks good" '
240
test_cmp afile afile_out
241
'
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
+ '
260
}
261
262
test_add_cat_expensive() {
test/sharness/t0111-gateway-writeable.sh
+1
-1
@@ -14,7 +14,7 @@ test_launch_ipfs_daemon --writable
14
test_expect_success "ipfs daemon --writable overrides config" '
15
curl -v -X POST http://$GWAY_ADDR/ipfs/ 2> outfile &&
16
grep "HTTP/1.1 201 Created" outfile &&
17
- grep "Location: /ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" outfile
17
+ grep "Location: /ipfs/Qmdsf68UUYTSSx3i4GtDJfxzpAEZt7Mp23m3qa36LYMSiW" outfile
18
'
19
test_kill_ipfs_daemon
20