Skip to content

Commit 77ec5cd

Browse files
authored
Merge pull request ipfs/go-ipfs-chunker#22 from ipfs/fix/buzhash-empty-block
fix: don't return an empty block at the end This commit was moved from ipfs/go-ipfs-chunker@0311814
2 parents aff0ae7 + 358dbca commit 77ec5cd

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

Diff for: chunker/buzhash.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
4040
n, err := io.ReadFull(b.r, b.buf[b.n:])
4141
if err != nil {
4242
if err == io.ErrUnexpectedEOF || err == io.EOF {
43-
if b.n+n < buzMin {
43+
buffered := b.n + n
44+
if buffered < buzMin {
4445
b.err = io.EOF
45-
res := make([]byte, b.n+n)
46+
// Read nothing? Don't return an empty block.
47+
if buffered == 0 {
48+
pool.Put(b.buf)
49+
b.buf = nil
50+
return nil, b.err
51+
}
52+
res := make([]byte, buffered)
4653
copy(res, b.buf)
4754

4855
pool.Put(b.buf)

Diff for: chunker/buzhash_test.go

+33-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chunk
22

33
import (
44
"bytes"
5-
"fmt"
65
"io"
76
"testing"
87

@@ -11,33 +10,48 @@ import (
1110

1211
func TestBuzhashChunking(t *testing.T) {
1312
data := make([]byte, 1024*1024*16)
14-
util.NewTimeSeededRand().Read(data)
1513

16-
r := NewBuzhash(bytes.NewReader(data))
14+
chunkCount := 0
15+
rounds := 100
1716

18-
var chunks [][]byte
17+
for i := 0; i < rounds; i++ {
18+
util.NewTimeSeededRand().Read(data)
1919

20-
for {
21-
chunk, err := r.NextBytes()
22-
if err != nil {
23-
if err == io.EOF {
24-
break
20+
r := NewBuzhash(bytes.NewReader(data))
21+
22+
var chunks [][]byte
23+
24+
for {
25+
chunk, err := r.NextBytes()
26+
if err != nil {
27+
if err == io.EOF {
28+
break
29+
}
30+
t.Fatal(err)
2531
}
26-
t.Fatal(err)
32+
33+
chunks = append(chunks, chunk)
2734
}
35+
chunkCount += len(chunks)
2836

29-
chunks = append(chunks, chunk)
30-
}
37+
for i, chunk := range chunks {
38+
if len(chunk) == 0 {
39+
t.Fatalf("chunk %d/%d is empty", i+1, len(chunks))
40+
}
41+
}
3142

32-
t.Logf("average block size: %d\n", len(data)/len(chunks))
43+
for i, chunk := range chunks[:len(chunks)-1] {
44+
if len(chunk) < buzMin {
45+
t.Fatalf("chunk %d/%d is less than the minimum size", i+1, len(chunks))
46+
}
47+
}
3348

34-
unchunked := bytes.Join(chunks, nil)
35-
if !bytes.Equal(unchunked, data) {
36-
fmt.Printf("%d %d\n", len(unchunked), len(data))
37-
//ioutil.WriteFile("./incorrect", unchunked, 0777)
38-
//ioutil.WriteFile("./correct", data, 0777)
39-
t.Fatal("data was chunked incorrectly")
49+
unchunked := bytes.Join(chunks, nil)
50+
if !bytes.Equal(unchunked, data) {
51+
t.Fatal("data was chunked incorrectly")
52+
}
4053
}
54+
t.Logf("average block size: %d\n", len(data)*rounds/chunkCount)
4155
}
4256

4357
func TestBuzhashChunkReuse(t *testing.T) {

0 commit comments

Comments
 (0)