Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 06009a2

Browse files
overboolschomatis
authored andcommitted
fix(type): issue #23
1 parent 929f9a5 commit 06009a2

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

archive/tar/writer.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
mdag "github.com/ipfs/go-merkledag"
1414
ft "github.com/ipfs/go-unixfs"
1515
uio "github.com/ipfs/go-unixfs/io"
16-
upb "github.com/ipfs/go-unixfs/pb"
1716

1817
ipld "github.com/ipfs/go-ipld-format"
1918
)
@@ -79,15 +78,15 @@ func (w *Writer) WriteNode(nd ipld.Node, fpath string) error {
7978
}
8079

8180
switch fsNode.Type() {
82-
case upb.Data_Metadata:
81+
case ft.TMetadata:
8382
fallthrough
84-
case upb.Data_Directory, upb.Data_HAMTShard:
83+
case ft.TDirectory, ft.THAMTShard:
8584
return w.writeDir(nd, fpath)
86-
case upb.Data_Raw:
85+
case ft.TRaw:
8786
fallthrough
88-
case upb.Data_File:
87+
case ft.TFile:
8988
return w.writeFile(nd, fsNode, fpath)
90-
case upb.Data_Symlink:
89+
case ft.TSymlink:
9190
return writeSymlinkHeader(w.TarW, string(fsNode.Data()), fpath)
9291
default:
9392
return ft.ErrUnrecognizedType

hamt/hamt.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ import (
2727

2828
dag "github.com/ipfs/go-merkledag"
2929
format "github.com/ipfs/go-unixfs"
30-
upb "github.com/ipfs/go-unixfs/pb"
31-
3230
bitfield "github.com/Stebalien/go-bitfield"
33-
proto "github.com/gogo/protobuf/proto"
3431
cid "github.com/ipfs/go-cid"
3532
ipld "github.com/ipfs/go-ipld-format"
3633
"github.com/spaolacci/murmur3"
@@ -108,7 +105,7 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
108105
}
109106

110107

111-
if fsn.Type() != upb.Data_HAMTShard {
108+
if fsn.Type() != format.THAMTShard {
112109
return nil, fmt.Errorf("node was not a dir shard")
113110
}
114111

@@ -176,13 +173,7 @@ func (ds *Shard) Node() (ipld.Node, error) {
176173
cindex++
177174
}
178175

179-
typ := upb.Data_HAMTShard
180-
data, err := proto.Marshal(&upb.Data{
181-
Type: &typ,
182-
Fanout: proto.Uint64(uint64(ds.tableSize)),
183-
HashType: proto.Uint64(HashMurmur3),
184-
Data: ds.bitfield.Bytes(),
185-
})
176+
data, err := format.HAMTShardData(ds.bitfield.Bytes(), uint64(ds.tableSize), HashMurmur3)
186177
if err != nil {
187178
return nil, err
188179
}

io/dagreader.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77

88
mdag "github.com/ipfs/go-merkledag"
99
ft "github.com/ipfs/go-unixfs"
10-
ftpb "github.com/ipfs/go-unixfs/pb"
11-
1210
ipld "github.com/ipfs/go-ipld-format"
1311
)
1412

@@ -49,12 +47,12 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
4947
}
5048

5149
switch fsNode.Type() {
52-
case ftpb.Data_Directory, ftpb.Data_HAMTShard:
50+
case ft.TDirectory, ft.THAMTShard:
5351
// Dont allow reading directories
5452
return nil, ErrIsDir
55-
case ftpb.Data_File, ftpb.Data_Raw:
53+
case ft.TFile, ft.TRaw:
5654
return NewPBFileReader(ctx, n, fsNode, serv), nil
57-
case ftpb.Data_Metadata:
55+
case ft.TMetadata:
5856
if len(n.Links()) == 0 {
5957
return nil, errors.New("incorrectly formatted metadata object")
6058
}
@@ -68,7 +66,7 @@ func NewDagReader(ctx context.Context, n ipld.Node, serv ipld.NodeGetter) (DagRe
6866
return nil, mdag.ErrNotProtobuf
6967
}
7068
return NewDagReader(ctx, childpb, serv)
71-
case ftpb.Data_Symlink:
69+
case ft.TSymlink:
7270
return nil, ErrCantReadSymlinks
7371
default:
7472
return nil, ft.ErrUnrecognizedType

io/pbdagreader.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88

99
mdag "github.com/ipfs/go-merkledag"
1010
ft "github.com/ipfs/go-unixfs"
11-
ftpb "github.com/ipfs/go-unixfs/pb"
12-
1311
cid "github.com/ipfs/go-cid"
1412
ipld "github.com/ipfs/go-ipld-format"
1513
)
@@ -134,10 +132,10 @@ func (dr *PBDagReader) loadBufNode(node ipld.Node) error {
134132
}
135133

136134
switch fsNode.Type() {
137-
case ftpb.Data_File:
135+
case ft.TFile:
138136
dr.buf = NewPBFileReader(dr.ctx, node, fsNode, dr.serv)
139137
return nil
140-
case ftpb.Data_Raw:
138+
case ft.TRaw:
141139
dr.buf = NewBufDagReader(fsNode.Data())
142140
return nil
143141
default:
@@ -318,7 +316,7 @@ func (dr *PBDagReader) Seek(offset int64, whence int) (int64, error) {
318316
// for this seems to be good(-enough) solution as it's only returned by
319317
// precalcNextBuf when we step out of file range.
320318
// This is needed for gateway to function properly
321-
if err == io.EOF && dr.file.Type() == ftpb.Data_File {
319+
if err == io.EOF && dr.file.Type() == ft.TFile {
322320
return -1, nil
323321
}
324322
return n, err

unixfs.go

+17
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ func SymlinkData(path string) ([]byte, error) {
106106
return out, nil
107107
}
108108

109+
// HAMTShardData return a `Data_HAMTShard` protobuf message
110+
func HAMTShardData(data []byte, fanout uint64, hashType uint64) ([]byte, error) {
111+
pbdata := new(pb.Data)
112+
typ := pb.Data_HAMTShard
113+
pbdata.Type = &typ
114+
pbdata.HashType = proto.Uint64(hashType)
115+
pbdata.Data = data
116+
pbdata.Fanout = proto.Uint64(fanout)
117+
118+
out, err := proto.Marshal(pbdata)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
return out, nil
124+
}
125+
109126
// UnwrapData unmarshals a protobuf messages and returns the contents.
110127
func UnwrapData(data []byte) ([]byte, error) {
111128
pbdata := new(pb.Data)

0 commit comments

Comments
 (0)