Trim output shards to be equal size to input shards on last read.

master
Klaus Post 2015-10-24 12:42:04 +02:00
parent 3fada41987
commit 32af654e33
1 changed files with 14 additions and 1 deletions

View File

@ -133,6 +133,7 @@ func (r rsStream) Encode(data []io.Reader, parity []io.Writer) error {
default:
return err
}
out = trimShards(out, shardSize(in))
err = r.r.Encode(all)
if err != nil {
return err
@ -144,6 +145,16 @@ func (r rsStream) Encode(data []io.Reader, parity []io.Writer) error {
}
}
// Trim the shards so they are all the same size
func trimShards(in [][]byte, size int) [][]byte {
for i := range in {
if in[i] != nil {
in[i] = in[i][0:size]
}
}
return in
}
func readShards(dst [][]byte, in []io.Reader) error {
if len(in) != len(dst) {
panic("internal error: in and dst size does not match")
@ -164,7 +175,7 @@ func readShards(dst [][]byte, in []io.Reader) error {
size = n
} else if n != size {
// Shard sizes must match.
return ErrShortData
return ErrShardSize
}
dst[i] = dst[i][0:n]
case nil:
@ -265,6 +276,8 @@ func (r rsStream) Reconstruct(valid []io.Reader, fill []io.Writer) error {
if err != nil {
return err
}
all = trimShards(all, shardSize(all))
err = r.r.Reconstruct(all)
if err != nil {
return err