1616 ErrIsDir = errors .New ("this dag node is a directory" )
1717 ErrCantReadSymlinks = errors .New ("cannot currently read symlinks" )
1818 ErrUnkownNodeType = errors .New ("unknown node type" )
19+ ErrSeekNotSupported = errors .New ("file does not support seeking" )
1920)
2021
2122// 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) {
345346 switch whence {
346347 case io .SeekStart :
347348 if offset < 0 {
348- return - 1 , errors .New ("invalid offset" )
349+ return dr . offset , errors .New ("invalid offset" )
349350 }
350351
351352 if offset == dr .offset {
@@ -359,6 +360,11 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
359360 // Seek from the beginning of the DAG.
360361 dr .resetPosition ()
361362
363+ // Shortcut seeking to the beginning, we're already there.
364+ if offset == 0 {
365+ return 0 , nil
366+ }
367+
362368 // Use the internal reader's context to fetch the child node promises
363369 // (see `ipld.NavigableIPLDNode.FetchChild` for details).
364370 dr .dagWalker .SetContext (dr .ctx )
@@ -388,7 +394,7 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
388394 // If there aren't enough size hints don't seek
389395 // (see the `io.EOF` handling error comment below).
390396 if fsNode .NumChildren () != len (node .Links ()) {
391- return io . EOF
397+ return ErrSeekNotSupported
392398 }
393399
394400 // Internal nodes have no data, so just iterate through the
@@ -445,16 +451,6 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
445451 }
446452 })
447453
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-
458454 if err != nil {
459455 return 0 , err
460456 }
@@ -484,6 +480,7 @@ func (dr *dagReader) Seek(offset int64, whence int) (int64, error) {
484480// in the `SeekStart` case.
485481func (dr * dagReader ) resetPosition () {
486482 dr .currentNodeData = nil
483+ dr .offset = 0
487484
488485 dr .dagWalker = ipld .NewWalker (dr .ctx , ipld .NewNavigableIPLDNode (dr .rootNode , dr .serv ))
489486 // TODO: This could be avoided (along with storing the `dr.rootNode` and
0 commit comments