2016-03-28 00:52:38 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
import (
|
2016-06-24 05:06:23 -04:00
|
|
|
"bytes"
|
2016-05-31 23:23:31 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2016-06-19 16:35:26 -04:00
|
|
|
"io"
|
2016-06-21 17:34:11 -04:00
|
|
|
"sync"
|
2016-06-01 19:43:31 -04:00
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
2016-05-31 23:23:31 -04:00
|
|
|
)
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// isSuccessDecodeBlocks - do we have all the blocks to be successfully decoded?.
|
|
|
|
// input disks here are expected to be ordered i.e parityBlocks
|
|
|
|
// are preceded by dataBlocks. For for information look at getOrderedDisks().
|
|
|
|
func isSuccessDecodeBlocks(disks []StorageAPI, dataBlocks int) bool {
|
|
|
|
// Count number of data and parity blocks that were read.
|
|
|
|
var successDataBlocksCount = 0
|
|
|
|
var successParityBlocksCount = 0
|
|
|
|
for index, disk := range disks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index < dataBlocks {
|
|
|
|
successDataBlocksCount++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
successParityBlocksCount++
|
|
|
|
}
|
|
|
|
// Returns true if we have atleast dataBlocks + 1 parity.
|
|
|
|
return successDataBlocksCount+successParityBlocksCount >= dataBlocks+1
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSuccessDataBlocks - do we have all the data blocks?
|
|
|
|
// input disks here are expected to be ordered i.e parityBlocks
|
|
|
|
// are preceded by dataBlocks. For for information look at getOrderedDisks().
|
|
|
|
func isSuccessDataBlocks(disks []StorageAPI, dataBlocks int) bool {
|
|
|
|
// Count number of data blocks that were read.
|
|
|
|
var successDataBlocksCount = 0
|
|
|
|
for index, disk := range disks[:dataBlocks] {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index < dataBlocks {
|
|
|
|
successDataBlocksCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns true if we have all the dataBlocks.
|
|
|
|
return successDataBlocksCount >= dataBlocks
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrderedDisks - get ordered disks from erasure distribution.
|
|
|
|
// returns ordered slice of disks from their actual distribution.
|
|
|
|
func getOrderedDisks(distribution []int, disks []StorageAPI, blockCheckSums []checkSumInfo) (orderedDisks []StorageAPI, orderedBlockCheckSums []checkSumInfo) {
|
|
|
|
orderedDisks = make([]StorageAPI, len(disks))
|
|
|
|
orderedBlockCheckSums = make([]checkSumInfo, len(disks))
|
|
|
|
// From disks gets ordered disks.
|
|
|
|
for index := range disks {
|
|
|
|
blockIndex := distribution[index]
|
|
|
|
orderedDisks[blockIndex-1] = disks[index]
|
|
|
|
orderedBlockCheckSums[blockIndex-1] = blockCheckSums[index]
|
|
|
|
}
|
|
|
|
return orderedDisks, orderedBlockCheckSums
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// erasureReadFile - read bytes from erasure coded files and writes to given writer.
|
|
|
|
// Erasure coded files are read block by block as per given erasureInfo and data chunks
|
2016-06-22 12:05:03 -04:00
|
|
|
// are decoded into a data block. Data block is trimmed for given offset and length,
|
|
|
|
// then written to given writer. This function also supports bit-rot detection by
|
2016-06-19 16:35:26 -04:00
|
|
|
// verifying checksum of individual block's checksum.
|
2016-06-21 17:34:11 -04:00
|
|
|
func erasureReadFile(writer io.Writer, disks []StorageAPI, volume string, path string, partName string, eInfos []erasureInfo, offset int64, length int64, totalLength int64) (int64, error) {
|
2016-06-01 19:43:31 -04:00
|
|
|
// Pick one erasure info.
|
2016-06-02 19:34:15 -04:00
|
|
|
eInfo := pickValidErasureInfo(eInfos)
|
2016-06-01 19:43:31 -04:00
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// Gather previously calculated block checksums.
|
|
|
|
blockCheckSums := metaPartBlockChecksums(disks, eInfos, partName)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// []orderedDisks will have first eInfo.DataBlocks disks as data
|
|
|
|
// disks and rest will be parity.
|
|
|
|
orderedDisks, orderedBlockCheckSums := getOrderedDisks(eInfo.Distribution, disks, blockCheckSums)
|
2016-06-21 17:34:11 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// bitrotVerify verifies if the file on a particular disk doesn't have bitrot
|
|
|
|
// by verifying the hash of the contents of the file.
|
2016-06-22 12:05:03 -04:00
|
|
|
bitrotVerify := func() func(diskIndex int) bool {
|
|
|
|
verified := make([]bool, len(orderedDisks))
|
2016-06-24 05:06:23 -04:00
|
|
|
// Return closure so that we have reference to []verified and
|
|
|
|
// not recalculate the hash on it everytime the function is
|
|
|
|
// called for the same disk.
|
2016-06-22 12:05:03 -04:00
|
|
|
return func(diskIndex int) bool {
|
|
|
|
if verified[diskIndex] {
|
2016-06-24 05:06:23 -04:00
|
|
|
// Already validated.
|
2016-06-22 12:05:03 -04:00
|
|
|
return true
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
// Is this a valid block?
|
2016-06-22 12:05:03 -04:00
|
|
|
isValid := isValidBlock(orderedDisks[diskIndex], volume, path, orderedBlockCheckSums[diskIndex])
|
|
|
|
verified[diskIndex] = isValid
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Total bytes written to writer
|
|
|
|
bytesWritten := int64(0)
|
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Each element of enBlocks holds curChunkSize'd amount of data read from its corresponding disk.
|
|
|
|
enBlocks := make([][]byte, len(orderedDisks))
|
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// chunkSize is roughly BlockSize/DataBlocks.
|
|
|
|
// chunkSize is calculated such that chunkSize*DataBlocks accommodates BlockSize bytes.
|
|
|
|
// So chunkSize*DataBlocks can be slightly larger than BlockSize if BlockSize is not divisible by
|
|
|
|
// DataBlocks. The extra space will have 0-padding.
|
|
|
|
chunkSize := getEncodedBlockLen(eInfo.BlockSize, eInfo.DataBlocks)
|
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Get start and end block, also bytes to be skipped based on the input offset.
|
2016-06-22 12:05:03 -04:00
|
|
|
startBlock, endBlock, bytesToSkip := getBlockInfo(offset, totalLength, eInfo.BlockSize)
|
|
|
|
|
|
|
|
// For each block, read chunk from each disk. If we are able to read all the data disks then we don't
|
|
|
|
// need to read parity disks. If one of the data disk is missing we need to read DataBlocks+1 number
|
|
|
|
// of disks. Once read, we Reconstruct() missing data if needed and write it to the given writer.
|
2016-06-21 17:34:11 -04:00
|
|
|
for block := startBlock; bytesWritten < length; block++ {
|
2016-06-24 05:06:23 -04:00
|
|
|
// curChunkSize is chunkSize until end block.
|
2016-06-21 17:34:11 -04:00
|
|
|
curChunkSize := chunkSize
|
2016-06-22 12:05:03 -04:00
|
|
|
if block == endBlock && (totalLength%eInfo.BlockSize != 0) {
|
|
|
|
// If this is the last block and size of the block is < BlockSize.
|
|
|
|
curChunkSize = getEncodedBlockLen(totalLength%eInfo.BlockSize, eInfo.DataBlocks)
|
2016-06-21 17:34:11 -04:00
|
|
|
}
|
2016-06-21 00:40:10 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Block offset.
|
|
|
|
// NOTE: That for the offset calculation we have to use chunkSize and
|
|
|
|
// not curChunkSize. If we use curChunkSize for offset calculation
|
|
|
|
// then it can result in wrong offset for the last block.
|
|
|
|
blockOffset := block * chunkSize
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-21 17:34:11 -04:00
|
|
|
// Figure out the number of disks that are needed for the read.
|
2016-06-22 12:05:03 -04:00
|
|
|
// We will need DataBlocks number of disks if all the data disks are up.
|
|
|
|
// We will need DataBlocks+1 number of disks even if one of the data disks is down.
|
2016-06-24 05:06:23 -04:00
|
|
|
readableDiskCount := 0
|
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// Count the number of data disks that are up.
|
2016-06-21 17:34:11 -04:00
|
|
|
for _, disk := range orderedDisks[:eInfo.DataBlocks] {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
readableDiskCount++
|
2016-06-21 17:34:11 -04:00
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Readable disks..
|
|
|
|
if readableDiskCount < eInfo.DataBlocks {
|
|
|
|
// Not enough data disks up, so we need DataBlocks+1 number
|
|
|
|
// of disks for reed-solomon Reconstruct()
|
|
|
|
readableDiskCount = eInfo.DataBlocks + 1
|
2016-06-21 17:34:11 -04:00
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Initialize wait group.
|
|
|
|
var wg = &sync.WaitGroup{}
|
2016-06-22 12:05:03 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Current disk index from which to read, this will be used later
|
|
|
|
// in case one of the parallel reads fails.
|
2016-06-21 17:34:11 -04:00
|
|
|
index := 0
|
2016-06-24 05:06:23 -04:00
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// Read from the disks in parallel.
|
2016-06-21 17:34:11 -04:00
|
|
|
for _, disk := range orderedDisks {
|
|
|
|
if disk == nil {
|
|
|
|
index++
|
|
|
|
continue
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Increment wait group.
|
2016-06-21 17:34:11 -04:00
|
|
|
wg.Add(1)
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Start reading from disk in a go-routine.
|
2016-06-21 17:34:11 -04:00
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Verify bit rot for this disk slice.
|
|
|
|
if !bitrotVerify(index) {
|
2016-06-22 12:05:03 -04:00
|
|
|
// So that we don't read from this disk for the next block.
|
|
|
|
orderedDisks[index] = nil
|
|
|
|
return
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Chunk writer.
|
|
|
|
chunkWriter := bytes.NewBuffer(make([]byte, 0, curChunkSize))
|
|
|
|
|
|
|
|
// CopyN copies until current chunk size.
|
|
|
|
err := copyN(chunkWriter, disk, volume, path, blockOffset, curChunkSize)
|
2016-06-21 17:34:11 -04:00
|
|
|
if err != nil {
|
2016-06-22 12:05:03 -04:00
|
|
|
// So that we don't read from this disk for the next block.
|
2016-06-21 17:34:11 -04:00
|
|
|
orderedDisks[index] = nil
|
|
|
|
return
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Copy the read blocks.
|
|
|
|
enBlocks[index] = chunkWriter.Bytes()
|
|
|
|
|
|
|
|
// Reset the buffer.
|
|
|
|
chunkWriter.Reset()
|
|
|
|
|
|
|
|
// Successfully read.
|
2016-06-21 17:34:11 -04:00
|
|
|
}(index, disk)
|
2016-06-24 05:06:23 -04:00
|
|
|
|
2016-06-21 17:34:11 -04:00
|
|
|
index++
|
2016-06-24 05:06:23 -04:00
|
|
|
readableDiskCount--
|
|
|
|
// We have read all the readable disks.
|
|
|
|
if readableDiskCount == 0 {
|
2016-06-21 17:34:11 -04:00
|
|
|
break
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-06-21 17:34:11 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Wait for all the reads to finish.
|
2016-06-21 17:34:11 -04:00
|
|
|
wg.Wait()
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// FIXME: make this parallel.
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// If we have all the data blocks no need to decode.
|
|
|
|
if !isSuccessDataBlocks(orderedDisks, eInfo.DataBlocks) {
|
|
|
|
// If we don't have DataBlocks number of data blocks we
|
|
|
|
// will have to read enough parity blocks such that we
|
|
|
|
// have DataBlocks+1 number for blocks for rs.Reconstruct().
|
|
|
|
// index is either dataBlocks or dataBlocks + 1.
|
2016-06-21 17:34:11 -04:00
|
|
|
for ; index < len(orderedDisks); index++ {
|
2016-06-24 05:06:23 -04:00
|
|
|
// We have enough blocks to decode, break out.
|
|
|
|
if isSuccessDecodeBlocks(orderedDisks, eInfo.DataBlocks) {
|
|
|
|
// We have DataBlocks+1 blocks, enough for rs.Reconstruct()
|
2016-06-21 17:34:11 -04:00
|
|
|
break
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// This disk was previously set to nil and ignored, do not read again.
|
|
|
|
if orderedDisks[index] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify bit-rot for this index.
|
|
|
|
if !bitrotVerify(index) {
|
2016-06-22 12:05:03 -04:00
|
|
|
// Mark nil so that we don't read from this disk for the next block.
|
|
|
|
orderedDisks[index] = nil
|
|
|
|
continue
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Chunk writer.
|
|
|
|
chunkWriter := bytes.NewBuffer(make([]byte, 0, curChunkSize))
|
|
|
|
|
|
|
|
// CopyN copies until current chunk size.
|
|
|
|
err := copyN(chunkWriter, orderedDisks[index], volume, path, blockOffset, curChunkSize)
|
2016-06-21 17:34:11 -04:00
|
|
|
if err != nil {
|
2016-06-24 05:06:23 -04:00
|
|
|
// ERROR: Mark nil so that we don't read from
|
|
|
|
// this disk for the next block.
|
2016-06-21 17:34:11 -04:00
|
|
|
orderedDisks[index] = nil
|
|
|
|
continue
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Copy the read blocks.
|
|
|
|
chunkWriter.Read(enBlocks[index])
|
|
|
|
|
|
|
|
// Reset the buffer.
|
|
|
|
chunkWriter.Reset()
|
2016-06-21 17:34:11 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// Reconstruct the missing data blocks.
|
2016-06-01 19:43:31 -04:00
|
|
|
err := decodeData(enBlocks, eInfo.DataBlocks, eInfo.ParityBlocks)
|
2016-05-31 23:23:31 -04:00
|
|
|
if err != nil {
|
2016-06-19 16:35:26 -04:00
|
|
|
return bytesWritten, err
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
// Success.
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
var outSize, outOffset int64
|
2016-06-22 12:05:03 -04:00
|
|
|
// enBlocks data can have 0-padding hence we need to figure the exact number
|
|
|
|
// of bytes we want to read from enBlocks.
|
|
|
|
blockSize := eInfo.BlockSize
|
|
|
|
if block == endBlock && totalLength%eInfo.BlockSize != 0 {
|
|
|
|
// For the last block, the block size can be less than BlockSize.
|
|
|
|
blockSize = totalLength % eInfo.BlockSize
|
|
|
|
}
|
2016-05-24 10:44:32 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// If this is start block, skip unwanted bytes.
|
|
|
|
if block == startBlock {
|
2016-06-22 15:55:23 -04:00
|
|
|
outOffset = bytesToSkip
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-05-29 18:38:14 -04:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Total data to be read.
|
|
|
|
outSize = blockSize
|
|
|
|
if length-bytesWritten < blockSize {
|
2016-06-22 12:05:03 -04:00
|
|
|
// We should not send more data than what was requested.
|
2016-06-22 15:55:23 -04:00
|
|
|
outSize = length - bytesWritten
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Write data blocks.
|
|
|
|
n, err := writeDataBlocks(writer, enBlocks, eInfo.DataBlocks, outOffset, outSize)
|
2016-06-19 16:35:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return bytesWritten, err
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Update total bytes written.
|
2016-06-22 15:55:23 -04:00
|
|
|
bytesWritten += n
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-06-24 05:06:23 -04:00
|
|
|
// Success.
|
2016-06-19 16:35:26 -04:00
|
|
|
return bytesWritten, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-06-02 04:49:46 -04:00
|
|
|
|
|
|
|
// PartObjectChecksum - returns the checksum for the part name from the checksum slice.
|
|
|
|
func (e erasureInfo) PartObjectChecksum(partName string) checkSumInfo {
|
|
|
|
for _, checksum := range e.Checksum {
|
|
|
|
if checksum.Name == partName {
|
|
|
|
return checksum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return checkSumInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// xlMetaPartBlockChecksums - get block checksums for a given part.
|
|
|
|
func metaPartBlockChecksums(disks []StorageAPI, eInfos []erasureInfo, partName string) (blockCheckSums []checkSumInfo) {
|
|
|
|
for index := range disks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if eInfos[index].IsValid() {
|
|
|
|
// Save the read checksums for a given part.
|
|
|
|
blockCheckSums = append(blockCheckSums, eInfos[index].PartObjectChecksum(partName))
|
|
|
|
} else {
|
|
|
|
blockCheckSums = append(blockCheckSums, checkSumInfo{})
|
|
|
|
}
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
return blockCheckSums
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes block index and block distribution to get the disk index.
|
2016-06-05 07:25:15 -04:00
|
|
|
func toDiskIndex(blockIdx int, distribution []int) int {
|
2016-06-02 04:49:46 -04:00
|
|
|
// Find out the right disk index for the input block index.
|
|
|
|
for index, blockIndex := range distribution {
|
2016-06-05 07:25:15 -04:00
|
|
|
if blockIndex-1 == blockIdx {
|
|
|
|
return index
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 07:25:15 -04:00
|
|
|
return -1
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// isValidBlock - calculates the checksum hash for the block and
|
|
|
|
// validates if its correct returns true for valid cases, false otherwise.
|
2016-06-22 12:05:03 -04:00
|
|
|
func isValidBlock(disk StorageAPI, volume, path string, blockCheckSum checkSumInfo) (ok bool) {
|
2016-06-05 07:25:15 -04:00
|
|
|
ok = false
|
2016-06-22 12:05:03 -04:00
|
|
|
if disk == nil {
|
|
|
|
return false
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
// Read everything for a given block and calculate hash.
|
2016-06-22 12:05:03 -04:00
|
|
|
hashWriter := newHash(blockCheckSum.Algorithm)
|
|
|
|
hashBytes, err := hashSum(disk, volume, path, hashWriter)
|
2016-06-02 04:49:46 -04:00
|
|
|
if err != nil {
|
2016-06-05 07:25:15 -04:00
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
2016-06-22 12:05:03 -04:00
|
|
|
ok = hex.EncodeToString(hashBytes) == blockCheckSum.Hash
|
2016-06-05 07:25:15 -04:00
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeData - decode encoded blocks.
|
|
|
|
func decodeData(enBlocks [][]byte, dataBlocks, parityBlocks int) error {
|
2016-06-24 05:06:23 -04:00
|
|
|
// Initialized reedsolomon.
|
2016-06-02 04:49:46 -04:00
|
|
|
rs, err := reedsolomon.New(dataBlocks, parityBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Reconstruct encoded blocks.
|
2016-06-02 04:49:46 -04:00
|
|
|
err = rs.Reconstruct(enBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
2016-06-02 04:49:46 -04:00
|
|
|
// Verify reconstructed blocks (parity).
|
|
|
|
ok, err := rs.Verify(enBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// Blocks cannot be reconstructed, corrupted data.
|
|
|
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
|
|
|
|
// Success.
|
2016-06-02 04:49:46 -04:00
|
|
|
return nil
|
|
|
|
}
|