@cryptotaxi247 / kubo / commits / 47001548e

Finish basic support for raw nodes in dag modifier.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Jun 20, 2017 at 17:05 UTC 47001548e1d074ba2d03578ef9647ee0ad3d0fbb
3 files changed +57 -17
test/sharness/t0250-files-api.sh
+37
@@ -15,6 +15,7 @@ test_expect_success "can create some files for testing" '
15 FILE1=$(echo foo | ipfs add -q) &&
16 FILE2=$(echo bar | ipfs add -q) &&
17 FILE3=$(echo baz | ipfs add -q) &&
18 + FILE9=$(echo zip | ipfs add -q --raw-leaves) &&
19 mkdir stuff_test &&
20 echo cats > stuff_test/a &&
21 echo dogs > stuff_test/b &&
@@ -252,6 +253,42 @@ test_files_api() {
253 test_cmp roothash roothashafter
254 '
255
256 + # test raw node
257 +
258 + test_expect_success "can put a raw-node into root" '
259 + ipfs files cp /ipfs/$FILE9 /file9
260 + '
261 +
262 + test_expect_success "file shows up in root" '
263 + verify_dir_contents / file9 cats
264 + '
265 +
266 + test_expect_success "can read file" '
267 + ipfs files read /file9 > file9out
268 + '
269 +
270 + test_expect_success "output looks good" '
271 + echo zip > expected &&
272 + test_cmp expected file9out
273 + '
274 +
275 + test_expect_success "can remove file from root" '
276 + ipfs files rm /file9
277 + '
278 +
279 + test_expect_success "file no longer appears" '
280 + verify_dir_contents / cats
281 + '
282 +
283 + test_expect_success "check root hash" '
284 + ipfs files stat --hash / > roothash
285 + '
286 +
287 + test_expect_success "check root hash was not changed" '
288 + ipfs files stat --hash / > roothashafter &&
289 + test_cmp roothash roothashafter
290 + '
291 +
292 # test read options
293
294 test_expect_success "read from offset works" '
unixfs/mod/dagmodifier.go
+18 -16
@@ -152,23 +152,25 @@ func (dm *DagModifier) Write(b []byte) (int, error) {
152 var ErrNoRawYet = fmt.Errorf("currently only fully support protonodes in the dagmodifier")
153
154 func (dm *DagModifier) Size() (int64, error) {
155 - pbnd, ok := dm.curNode.(*mdag.ProtoNode)
156 - if !ok {
157 - return 0, ErrNoRawYet
158 - }
159 -
160 - pbn, err := ft.FromBytes(pbnd.Data())
161 - if err != nil {
162 - return 0, err
163 - }
164 -
165 - if dm.wrBuf != nil {
166 - if uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
155 + switch nd := dm.curNode.(type) {
156 + case *mdag.ProtoNode:
157 + pbn, err := ft.FromBytes(nd.Data())
158 + if err != nil {
159 + return 0, err
160 + }
161 + if dm.wrBuf != nil && uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
162 return int64(dm.wrBuf.Len()) + int64(dm.writeStart), nil
163 }
164 + return int64(pbn.GetFilesize()), nil
165 + case *mdag.RawNode:
166 + if dm.wrBuf != nil {
167 + return 0, ErrNoRawYet
168 + }
169 + sz, err := nd.Size()
170 + return int64(sz), err
171 + default:
172 + return 0, ErrNotUnixfs
173 }
170 -
171 - return int64(pbn.GetFilesize()), nil
174 }
175
176 // Sync writes changes to this dag to disk
@@ -397,12 +399,12 @@ func (dm *DagModifier) CtxReadFull(ctx context.Context, b []byte) (int, error) {
399 }
400
401 // GetNode gets the modified DAG Node
400 -func (dm *DagModifier) GetNode() (*mdag.ProtoNode, error) {
402 +func (dm *DagModifier) GetNode() (node.Node, error) {
403 err := dm.Sync()
404 if err != nil {
405 return nil, err
406 }
405 - return dm.curNode.Copy().(*mdag.ProtoNode), nil
407 + return dm.curNode.Copy(), nil
408 }
409
410 // HasChanges returned whether or not there are unflushed changes to this dag
unixfs/mod/dagmodifier_test.go
+2 -1
@@ -9,6 +9,7 @@ import (
9
10 h "github.com/ipfs/go-ipfs/importer/helpers"
11 trickle "github.com/ipfs/go-ipfs/importer/trickle"
12 + mdag "github.com/ipfs/go-ipfs/merkledag"
13 ft "github.com/ipfs/go-ipfs/unixfs"
14 uio "github.com/ipfs/go-ipfs/unixfs/io"
15 testu "github.com/ipfs/go-ipfs/unixfs/test"
@@ -105,7 +106,7 @@ func TestDagModifierBasic(t *testing.T) {
106 t.Fatal(err)
107 }
108
108 - size, err := ft.DataSize(node.Data())
109 + size, err := ft.DataSize(node.(*mdag.ProtoNode).Data())
110 if err != nil {
111 t.Fatal(err)
112 }