16
16
ErrIsDir = errors .New ("this dag node is a directory" )
17
17
ErrCantReadSymlinks = errors .New ("cannot currently read symlinks" )
18
18
ErrUnkownNodeType = errors .New ("unknown node type" )
19
+ ErrSeekNotSupported = errors .New ("file does not support seeking" )
19
20
)
20
21
21
22
// TODO: Rename the `DagReader` interface, this doesn't read *any* DAG, just
@@ -345,7 +346,7 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
345
346
switch whence {
346
347
case io .SeekStart :
347
348
if offset < 0 {
348
- return - 1 , errors .New ("invalid offset" )
349
+ return dr . offset , errors .New ("invalid offset" )
349
350
}
350
351
351
352
if offset == dr .offset {
@@ -359,6 +360,11 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
359
360
// Seek from the beginning of the DAG.
360
361
dr .resetPosition ()
361
362
363
+ // Shortcut seeking to the beginning, we're already there.
364
+ if offset == 0 {
365
+ return 0 , nil
366
+ }
367
+
362
368
// Use the internal reader's context to fetch the child node promises
363
369
// (see `ipld.NavigableIPLDNode.FetchChild` for details).
364
370
dr .dagWalker .SetContext (dr .ctx )
@@ -388,7 +394,7 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
388
394
// If there aren't enough size hints don't seek
389
395
// (see the `io.EOF` handling error comment below).
390
396
if fsNode .NumChildren () != len (node .Links ()) {
391
- return io . EOF
397
+ return ErrSeekNotSupported
392
398
}
393
399
394
400
// Internal nodes have no data, so just iterate through the
@@ -445,16 +451,6 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
445
451
}
446
452
})
447
453
448
- if err == io .EOF {
449
- // TODO: Taken from https://github.com/ipfs/go-ipfs/pull/4320,
450
- // check if still valid.
451
- // Return negative number if we can't figure out the file size. Using io.EOF
452
- // for this seems to be good(-enough) solution as it's only returned by
453
- // precalcNextBuf when we step out of file range.
454
- // This is needed for gateway to function properly
455
- return - 1 , nil
456
- }
457
-
458
454
if err != nil {
459
455
return 0 , err
460
456
}
@@ -484,6 +480,7 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
484
480
// in the `SeekStart` case.
485
481
func (dr * dagReader ) resetPosition () {
486
482
dr .currentNodeData = nil
483
+ dr .offset = 0
487
484
488
485
dr .dagWalker = ipld .NewWalker (dr .ctx , ipld .NewNavigableIPLDNode (dr .rootNode , dr .serv ))
489
486
// TODO: This could be avoided (along with storing the `dr.rootNode` and
0 commit comments