address comments from @cryptix in PR
Jeromy committed
Mar 9, 2015 at 16:51 UTC
3119088ae7db92e723ed84153f0dc4b107720114
1 file changed
+10
-25
unixfs/mod/dagmodifier.go
+10
-25
@@ -20,6 +20,10 @@ import (
20
u "github.com/jbenet/go-ipfs/util"
21
)
22
23
+var ErrSeekFail = errors.New("failed to seek properly")
24
+var ErrSeekEndNotImpl = errors.New("SEEK_END currently not implemented")
25
+var ErrUnrecognizedWhence = errors.New("unrecognized whence")
26
+
27
// 2MB
28
var writebufferSize = 1 << 21
29
@@ -199,6 +203,8 @@ func (dm *DagModifier) Flush() error {
203
}
204
205
// modifyDag writes the data in 'data' over the data in 'node' starting at 'offset'
206
+// returns the new key of the passed in node and whether or not all the data in the reader
207
+// has been consumed.
208
func (dm *DagModifier) modifyDag(node *mdag.Node, offset uint64, data io.Reader) (u.Key, bool, error) {
209
f, err := ft.FromBytes(node.Data)
210
if err != nil {
@@ -291,7 +297,7 @@ func (dm *DagModifier) Read(b []byte) (int, error) {
297
}
298
299
if i != int64(dm.curWrOff) {
294
- return 0, errors.New("failed to seek properly")
300
+ return 0, ErrSeekFail
301
}
302
303
dm.read = dr
@@ -302,28 +308,6 @@ func (dm *DagModifier) Read(b []byte) (int, error) {
308
return n, err
309
}
310
305
-// splitBytes uses a splitterFunc to turn a large array of bytes
306
-// into many smaller arrays of bytes
307
-func (dm *DagModifier) splitBytes(in io.Reader) ([]u.Key, error) {
308
- var out []u.Key
309
- blks := dm.splitter.Split(in)
310
- for blk := range blks {
311
- nd := help.NewUnixfsNode()
312
- nd.SetData(blk)
313
- dagnd, err := nd.GetDagNode()
314
- if err != nil {
315
- return nil, err
316
- }
317
-
318
- k, err := dm.dagserv.Add(dagnd)
319
- if err != nil {
320
- return nil, err
321
- }
322
- out = append(out, k)
323
- }
324
- return out, nil
325
-}
326
-
311
// GetNode gets the modified DAG Node
312
func (dm *DagModifier) GetNode() (*mdag.Node, error) {
313
err := dm.Flush()
@@ -352,9 +336,9 @@ func (dm *DagModifier) Seek(offset int64, whence int) (int64, error) {
336
dm.curWrOff = uint64(offset)
337
dm.writeStart = uint64(offset)
338
case os.SEEK_END:
355
- return 0, errors.New("SEEK_END currently not implemented")
339
+ return 0, ErrSeekEndNotImpl
340
default:
357
- return 0, errors.New("unrecognized whence")
341
+ return 0, ErrUnrecognizedWhence
342
}
343
344
if dm.read != nil {
@@ -425,6 +409,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er
409
return nil, err
410
}
411
412
+ // found the child we want to cut
413
if size < cur+childsize {
414
nchild, err := dagTruncate(child, size-cur, ds)
415
if err != nil {