ipfs object put: return error if object is empty (fixes #883)
Henry committed
Mar 10, 2015 at 22:58 UTC
b688e72de05cef362e310c90f9968a0614830adf
3 files changed
+25
core/commands/object.go
+9
@@ -379,6 +379,9 @@ func objectGet(n *core.IpfsNode, fpath path.Path) (*dag.Node, error) {
379
return dagnode, nil
380
}
381
382
+// ErrEmptyNode is returned when the input to 'ipfs object put' contains no data
383
+var ErrEmptyNode = errors.New("no data or links in this node")
384
+
385
// objectPut takes a format option, serializes bytes from stdin and updates the dag with that data
386
func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, error) {
387
var (
@@ -404,6 +407,12 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
407
return nil, err
408
}
409
410
+ // check that we have data in the Node to add
411
+ // otherwise we will add the empty object without raising an error
412
+ if node.Data == "" && len(node.Links) == 0 {
413
+ return nil, ErrEmptyNode
414
+ }
415
+
416
dagnode, err = deserializeNode(node)
417
if err != nil {
418
return nil, err
test/sharness/t0051-object-data/brokenPut.json
new
+5
@@ -0,0 +1,5 @@
1
+{
2
+ "this": "should",
3
+ "return": "an",
4
+ "error":"not valid dag object"
5
+}
\ No newline at end of file
test/sharness/t0051-object.sh
+11
@@ -82,5 +82,16 @@ test_expect_success "'ipfs object put' from stdin (pb) output looks good" '
82
test_cmp expected_putStdinOut actual_putPbStdinOut
83
'
84
85
+test_expect_success "'ipfs object put broken.json' should fail" '
86
+ test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.json 2>actual_putBrokenErr >actual_putBroken
87
+'
88
+
89
+test_expect_success "'ipfs object put broken.hjson' output looks good" '
90
+ touch expected_putBroken &&
91
+ printf "Error: no data or links in this node\n" > expected_putBrokenErr &&
92
+ test_cmp expected_putBroken actual_putBroken &&
93
+ test_cmp expected_putBrokenErr actual_putBrokenErr
94
+'
95
+
96
97
test_done