Skip to content

Commit

Permalink
Handle reading null-padded files
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k authored and hannahhoward committed Nov 19, 2020
1 parent b3311f2 commit 11b6074
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,46 @@ func TestRoundtrip(t *testing.T) {
}
}

func TestRoundtripNullPadded(t *testing.T) {
dserv := dstest.Mock()
a := dag.NewRawNode([]byte("aaaa"))

assertAddNodes(t, dserv, a)

buf := new(bytes.Buffer)
if err := WriteCar(context.Background(), dserv, []cid.Cid{a.Cid()}, buf); err != nil {
t.Fatal(err)
}

buf.Write([]byte{0, 0, 0, 0, 0})

bserv := dstest.Bserv()
ch, err := LoadCar(bserv.Blockstore(), buf)
if err != nil {
t.Fatal(err)
}

if len(ch.Roots) != 1 {
t.Fatal("should have one root")
}

if !ch.Roots[0].Equals(a.Cid()) {
t.Fatal("got wrong cid")
}

bs := bserv.Blockstore()
for _, nd := range []format.Node{a} {
has, err := bs.Has(nd.Cid())
if err != nil {
t.Fatal(err)
}

if !has {
t.Fatal("should have cid in blockstore")
}
}
}

func TestRoundtripFilestore(t *testing.T) {
dserv := dstest.Mock()
a := dag.NewRawNode([]byte("aaaa"))
Expand Down
4 changes: 4 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func ReadNode(br *bufio.Reader) (cid.Cid, uint64, []byte, error) {
return cid.Cid{}, 0, nil, err
}

if len(data) == 0 {
return cid.Undef, l, nil, io.EOF // null-padded file
}

c, n, err := ReadCid(data)
if err != nil {
return cid.Cid{}, 0, nil, err
Expand Down

0 comments on commit 11b6074

Please sign in to comment.