swarm/storage: Fix LazyChunkReader.ReadAt() (#429)

This commit is contained in:
Balint Gabor 2018-04-25 13:03:21 +02:00 committed by GitHub
parent 41d5566716
commit 987d9bda1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -496,9 +496,14 @@ func (self *LazyChunkReader) join(b []byte, off int64, eoff int64, depth int, tr
start := off / treeSize start := off / treeSize
end := (eoff + treeSize - 1) / treeSize end := (eoff + treeSize - 1) / treeSize
// last non-leaf chunk can be shorter than default chunk size, let's not read it further then its end
currentBranches := int64(len(chunkData)-8) / self.hashSize
if end > currentBranches {
end = currentBranches
}
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
defer wg.Wait() defer wg.Wait()
for i := start; i < end; i++ { for i := start; i < end; i++ {
soff := i * treeSize soff := i * treeSize
roff := soff roff := soff

View file

@ -129,6 +129,21 @@ func testRandomData(usePyramid bool, hash string, n int, tester *chunkerTester)
} }
} }
// testing partial read
for i := 1; i < n; i += 10000 {
readableLength := n - i
output := make([]byte, readableLength)
r, err := reader.ReadAt(output, int64(i))
if r != readableLength || err != io.EOF {
tester.t.Fatalf("readAt error with offset %v read: %v n = %v err = %v\n", i, r, readableLength, err)
}
if input != nil {
if !bytes.Equal(output, input[i:]) {
tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", input[i:], output)
}
}
}
return key return key
} }