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 ...@@ -3,7 +3,6 @@ package io
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
mdag "github.com/ipfs/go-ipfs/merkledag" mdag "github.com/ipfs/go-ipfs/merkledag"
...@@ -17,6 +16,7 @@ import ( ...@@ -17,6 +16,7 @@ import (
var ( var (
ErrIsDir = errors.New("this dag node is a directory") ErrIsDir = errors.New("this dag node is a directory")
ErrCantReadSymlinks = errors.New("cannot currently read symlinks") 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. // 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 ...@@ -74,6 +74,6 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
return nil, ft.ErrUnrecognizedType return nil, ft.ErrUnrecognizedType
} }
default: default:
return nil, fmt.Errorf("unrecognized node type") return nil, ErrUnkownNodeType
} }
} }
...@@ -122,34 +122,32 @@ func (dr *PBDagReader) precalcNextBuf(ctx context.Context) error { ...@@ -122,34 +122,32 @@ func (dr *PBDagReader) precalcNextBuf(ctx context.Context) error {
dr.linkPosition++ 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: case *mdag.ProtoNode:
fsNode, err := ft.FSNodeFromBytes(nxt.Data()) fsNode, err := ft.FSNodeFromBytes(node.Data())
if err != nil { if err != nil {
return fmt.Errorf("incorrectly formatted protobuf: %s", err) return fmt.Errorf("incorrectly formatted protobuf: %s", err)
} }
switch fsNode.Type() { 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: case ftpb.Data_File:
dr.buf = NewPBFileReader(dr.ctx, nxt, fsNode, dr.serv) dr.buf = NewPBFileReader(dr.ctx, node, fsNode, dr.serv)
return nil return nil
case ftpb.Data_Raw: case ftpb.Data_Raw:
dr.buf = NewBufDagReader(fsNode.Data()) dr.buf = NewBufDagReader(fsNode.Data())
return nil 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: 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: default:
var err error return ErrUnkownNodeType
dr.buf, err = NewDagReader(ctx, nxt, dr.serv)
return err
} }
} }
......
...@@ -25,7 +25,6 @@ const ( ...@@ -25,7 +25,6 @@ const (
// Common errors // Common errors
var ( var (
ErrMalformedFileFormat = errors.New("malformed data in file format") ErrMalformedFileFormat = errors.New("malformed data in file format")
ErrInvalidDirLocation = errors.New("found directory node in unexpected place")
ErrUnrecognizedType = errors.New("unrecognized node type") 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