Commit 9f1f2cdb authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

Merge pull request #5237 from schomatis/fix/unixfs/dag-reader/next-buf

 unixfs: refactor `precalcNextBuf`
parents 999c6686 0271059b
......@@ -3,7 +3,6 @@ package io
import (
"context"
"errors"
"fmt"
"io"
mdag "github.com/ipfs/go-ipfs/merkledag"
......@@ -17,6 +16,7 @@ import (
var (
ErrIsDir = errors.New("this dag node is a directory")
ErrCantReadSymlinks = errors.New("cannot currently read symlinks")
ErrUnkownNodeType = errors.New("unknown node type")
)
// A DagReader provides read-only read and seek acess to a unixfs file.
......@@ -74,6 +74,6 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
return nil, ft.ErrUnrecognizedType
}
default:
return nil, fmt.Errorf("unrecognized node type")
return nil, ErrUnkownNodeType
}
}
......@@ -122,34 +122,32 @@ func (dr *PBDagReader) precalcNextBuf(ctx context.Context) error {
dr.linkPosition++
switch nxt := nxt.(type) {
return dr.loadBufNode(nxt)
}
func (dr *PBDagReader) loadBufNode(node ipld.Node) error {
switch node := node.(type) {
case *mdag.ProtoNode:
fsNode, err := ft.FSNodeFromBytes(nxt.Data())
fsNode, err := ft.FSNodeFromBytes(node.Data())
if err != nil {
return fmt.Errorf("incorrectly formatted protobuf: %s", err)
}
switch fsNode.Type() {
case ftpb.Data_Directory, ftpb.Data_HAMTShard:
// A directory should not exist within a file
return ft.ErrInvalidDirLocation
case ftpb.Data_File:
dr.buf = NewPBFileReader(dr.ctx, nxt, fsNode, dr.serv)
dr.buf = NewPBFileReader(dr.ctx, node, fsNode, dr.serv)
return nil
case ftpb.Data_Raw:
dr.buf = NewBufDagReader(fsNode.Data())
return nil
case ftpb.Data_Metadata:
return errors.New("shouldnt have had metadata object inside file")
case ftpb.Data_Symlink:
return errors.New("shouldnt have had symlink inside file")
default:
return ft.ErrUnrecognizedType
return fmt.Errorf("found %s node in unexpected place", fsNode.Type().String())
}
case *mdag.RawNode:
dr.buf = NewBufDagReader(node.RawData())
return nil
default:
var err error
dr.buf, err = NewDagReader(ctx, nxt, dr.serv)
return err
return ErrUnkownNodeType
}
}
......
......@@ -25,7 +25,6 @@ const (
// Common errors
var (
ErrMalformedFileFormat = errors.New("malformed data in file format")
ErrInvalidDirLocation = errors.New("found directory node in unexpected place")
ErrUnrecognizedType = errors.New("unrecognized node type")
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment