Skip to content

Commit

Permalink
Merge pull request #5237 from schomatis/fix/unixfs/dag-reader/next-buf
Browse files Browse the repository at this point in the history
 unixfs: refactor `precalcNextBuf`
  • Loading branch information
whyrusleeping committed Jul 18, 2018
2 parents e8cc529 + 13d9110 commit a44bffb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions unixfs/io/dagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io
import (
"context"
"errors"
"fmt"
"io"

mdag "github.com/ipfs/go-ipfs/merkledag"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
26 changes: 12 additions & 14 deletions unixfs/io/pbdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
1 change: 0 additions & 1 deletion unixfs/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down

0 comments on commit a44bffb

Please sign in to comment.