You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.3 KiB
163 lines
4.3 KiB
![]()
9 years ago
|
/*
|
||
|
* 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
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"io"
|
||
![]()
8 years ago
|
"sync"
|
||
![]()
9 years ago
|
)
|
||
|
|
||
![]()
8 years ago
|
// ReadFile - decoded erasure coded file.
|
||
|
func (e erasure) ReadFile(volume, path string, startOffset int64) (io.ReadCloser, error) {
|
||
![]()
9 years ago
|
// Input validation.
|
||
|
if !isValidVolname(volume) {
|
||
|
return nil, errInvalidArgument
|
||
|
}
|
||
|
if !isValidPath(path) {
|
||
|
return nil, errInvalidArgument
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
var wg = &sync.WaitGroup{}
|
||
|
|
||
|
readers := make([]io.ReadCloser, len(e.storageDisks))
|
||
|
for index, disk := range e.storageDisks {
|
||
|
wg.Add(1)
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
// If disk.ReadFile returns error and we don't have read
|
||
|
// quorum it will be taken care as ReedSolomon.Reconstruct()
|
||
|
// will fail later.
|
||
|
offset := int64(0)
|
||
|
if reader, err := disk.ReadFile(volume, path, offset); err == nil {
|
||
|
readers[index] = reader
|
||
|
}
|
||
|
}(index, disk)
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
wg.Wait()
|
||
![]()
9 years ago
|
|
||
|
// Initialize pipe.
|
||
|
pipeReader, pipeWriter := io.Pipe()
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
go func() {
|
||
![]()
8 years ago
|
// Read until EOF.
|
||
|
for {
|
||
![]()
9 years ago
|
// Calculate the current encoded block size.
|
||
![]()
8 years ago
|
curEncBlockSize := getEncodedBlockLen(erasureBlockSize, e.DataBlocks)
|
||
|
enBlocks := make([][]byte, len(e.storageDisks))
|
||
![]()
9 years ago
|
// Loop through all readers and read.
|
||
|
for index, reader := range readers {
|
||
![]()
8 years ago
|
// Initialize shard slice and fill the data from each parts.
|
||
|
enBlocks[index] = make([]byte, curEncBlockSize)
|
||
![]()
9 years ago
|
if reader == nil {
|
||
|
continue
|
||
|
}
|
||
![]()
8 years ago
|
// Read the necessary blocks.
|
||
|
n, rErr := io.ReadFull(reader, enBlocks[index])
|
||
|
if rErr == io.EOF {
|
||
|
// Close the pipe.
|
||
|
pipeWriter.Close()
|
||
|
|
||
|
// Cleanly close all the underlying data readers.
|
||
|
for _, reader := range readers {
|
||
|
if reader == nil {
|
||
|
continue
|
||
|
}
|
||
|
reader.Close()
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
return
|
||
|
}
|
||
|
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
||
|
readers[index].Close()
|
||
|
readers[index] = nil
|
||
|
continue
|
||
|
}
|
||
|
enBlocks[index] = enBlocks[index][:n]
|
||
![]()
9 years ago
|
}
|
||
|
|
||
|
// Check blocks if they are all zero in length.
|
||
|
if checkBlockSize(enBlocks) == 0 {
|
||
![]()
8 years ago
|
pipeWriter.CloseWithError(errDataCorrupt)
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Verify the blocks.
|
||
![]()
8 years ago
|
ok, err := e.ReedSolomon.Verify(enBlocks)
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Verification failed, blocks require reconstruction.
|
||
|
if !ok {
|
||
![]()
8 years ago
|
for index, reader := range readers {
|
||
|
if reader == nil {
|
||
|
// Reconstruct expects missing blocks to be nil.
|
||
|
enBlocks[index] = nil
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
err = e.ReedSolomon.Reconstruct(enBlocks)
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
// Verify reconstructed blocks again.
|
||
![]()
8 years ago
|
ok, err = e.ReedSolomon.Verify(enBlocks)
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
if !ok {
|
||
|
// Blocks cannot be reconstructed, corrupted data.
|
||
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
||
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
// Get all the data blocks.
|
||
![]()
8 years ago
|
dataBlocks := getDataBlocks(enBlocks, e.DataBlocks)
|
||
![]()
8 years ago
|
|
||
|
// Verify if the offset is right for the block, if not move to
|
||
|
// the next block.
|
||
|
if startOffset > 0 {
|
||
|
startOffset = startOffset - int64(len(dataBlocks))
|
||
![]()
8 years ago
|
// Start offset is greater than or equal to zero, skip the dataBlocks.
|
||
|
if startOffset >= 0 {
|
||
![]()
8 years ago
|
continue
|
||
|
}
|
||
![]()
8 years ago
|
// Now get back the remaining offset if startOffset is negative.
|
||
![]()
8 years ago
|
startOffset = startOffset + int64(len(dataBlocks))
|
||
|
}
|
||
|
|
||
|
// Write safely the necessary blocks.
|
||
|
_, err = pipeWriter.Write(dataBlocks[int(startOffset):])
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
// Reset offset to '0' to read rest of the blocks.
|
||
|
startOffset = int64(0)
|
||
![]()
9 years ago
|
}
|
||
|
}()
|
||
|
|
||
|
// Return the pipe for the top level caller to start reading.
|
||
|
return pipeReader, nil
|
||
|
}
|