Commit 06009a2f authored by Overbool's avatar Overbool Committed by Lucas Molas

fix(type): issue #23

parent 929f9a57
......@@ -13,7 +13,6 @@ import (
mdag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
uio "github.com/ipfs/go-unixfs/io"
upb "github.com/ipfs/go-unixfs/pb"
ipld "github.com/ipfs/go-ipld-format"
)
......@@ -79,15 +78,15 @@ func (w *Writer) WriteNode(nd ipld.Node, fpath string) error {
}
switch fsNode.Type() {
case upb.Data_Metadata:
case ft.TMetadata:
fallthrough
case upb.Data_Directory, upb.Data_HAMTShard:
case ft.TDirectory, ft.THAMTShard:
return w.writeDir(nd, fpath)
case upb.Data_Raw:
case ft.TRaw:
fallthrough
case upb.Data_File:
case ft.TFile:
return w.writeFile(nd, fsNode, fpath)
case upb.Data_Symlink:
case ft.TSymlink:
return writeSymlinkHeader(w.TarW, string(fsNode.Data()), fpath)
default:
return ft.ErrUnrecognizedType
......
......@@ -27,10 +27,7 @@ import (
dag "github.com/ipfs/go-merkledag"
format "github.com/ipfs/go-unixfs"
upb "github.com/ipfs/go-unixfs/pb"
bitfield "github.com/Stebalien/go-bitfield"
proto "github.com/gogo/protobuf/proto"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/spaolacci/murmur3"
......@@ -108,7 +105,7 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
}
if fsn.Type() != upb.Data_HAMTShard {
if fsn.Type() != format.THAMTShard {
return nil, fmt.Errorf("node was not a dir shard")
}
......@@ -176,13 +173,7 @@ func (ds *Shard) Node() (ipld.Node, error) {
cindex++
}
typ := upb.Data_HAMTShard
data, err := proto.Marshal(&upb.Data{
Type: &typ,
Fanout: proto.Uint64(uint64(ds.tableSize)),
HashType: proto.Uint64(HashMurmur3),
Data: ds.bitfield.Bytes(),
})
data, err := format.HAMTShardData(ds.bitfield.Bytes(), uint64(ds.tableSize), HashMurmur3)
if err != nil {
return nil, err
}
......
......@@ -7,8 +7,6 @@ import (
mdag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
ftpb "github.com/ipfs/go-unixfs/pb"
ipld "github.com/ipfs/go-ipld-format"
)
......@@ -49,12 +47,12 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
}
switch fsNode.Type() {
case ftpb.Data_Directory, ftpb.Data_HAMTShard:
case ft.TDirectory, ft.THAMTShard:
// Dont allow reading directories
return nil, ErrIsDir
case ftpb.Data_File, ftpb.Data_Raw:
case ft.TFile, ft.TRaw:
return NewPBFileReader(ctx, n, fsNode, serv), nil
case ftpb.Data_Metadata:
case ft.TMetadata:
if len(n.Links()) == 0 {
return nil, errors.New("incorrectly formatted metadata object")
}
......@@ -68,7 +66,7 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
return nil, mdag.ErrNotProtobuf
}
return NewDagReader(ctx, childpb, serv)
case ftpb.Data_Symlink:
case ft.TSymlink:
return nil, ErrCantReadSymlinks
default:
return nil, ft.ErrUnrecognizedType
......
......@@ -8,8 +8,6 @@ import (
mdag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
ftpb "github.com/ipfs/go-unixfs/pb"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
......@@ -134,10 +132,10 @@ func (dr *PBDagReader) loadBufNode(node ipld.Node) error {
}
switch fsNode.Type() {
case ftpb.Data_File:
case ft.TFile:
dr.buf = NewPBFileReader(dr.ctx, node, fsNode, dr.serv)
return nil
case ftpb.Data_Raw:
case ft.TRaw:
dr.buf = NewBufDagReader(fsNode.Data())
return nil
default:
......@@ -318,7 +316,7 @@ func (dr *PBDagReader) Seek(offset int64, whence int) (int64, error) {
// for this seems to be good(-enough) solution as it's only returned by
// precalcNextBuf when we step out of file range.
// This is needed for gateway to function properly
if err == io.EOF && dr.file.Type() == ftpb.Data_File {
if err == io.EOF && dr.file.Type() == ft.TFile {
return -1, nil
}
return n, err
......
......@@ -106,6 +106,23 @@ func SymlinkData(path string) ([]byte, error) {
return out, nil
}
// HAMTShardData return a `Data_HAMTShard` protobuf message
func HAMTShardData(data []byte, fanout uint64, hashType uint64) ([]byte, error) {
pbdata := new(pb.Data)
typ := pb.Data_HAMTShard
pbdata.Type = &typ
pbdata.HashType = proto.Uint64(hashType)
pbdata.Data = data
pbdata.Fanout = proto.Uint64(fanout)
out, err := proto.Marshal(pbdata)
if err != nil {
return nil, err
}
return out, nil
}
// UnwrapData unmarshals a protobuf messages and returns the contents.
func UnwrapData(data []byte) ([]byte, error) {
pbdata := new(pb.Data)
......
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