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.
187 lines
5.0 KiB
187 lines
5.0 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.
|
||
|
*/
|
||
9 years ago
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
9 years ago
|
import (
|
||
9 years ago
|
"bytes"
|
||
9 years ago
|
"errors"
|
||
9 years ago
|
"hash"
|
||
|
"io"
|
||
|
|
||
|
"github.com/klauspost/reedsolomon"
|
||
9 years ago
|
"github.com/minio/blake2b-simd"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// newHashWriters - inititialize a slice of hashes for the disk count.
|
||
8 years ago
|
func newHashWriters(diskCount int, algo string) []hash.Hash {
|
||
9 years ago
|
hashWriters := make([]hash.Hash, diskCount)
|
||
|
for index := range hashWriters {
|
||
8 years ago
|
hashWriters[index] = newHash(algo)
|
||
9 years ago
|
}
|
||
|
return hashWriters
|
||
|
}
|
||
|
|
||
9 years ago
|
// newHash - gives you a newly allocated hash depending on the input algorithm.
|
||
|
func newHash(algo string) hash.Hash {
|
||
|
switch algo {
|
||
9 years ago
|
case "blake2b":
|
||
|
return blake2b.New512()
|
||
9 years ago
|
// Add new hashes here.
|
||
|
default:
|
||
9 years ago
|
// Default to blake2b.
|
||
|
return blake2b.New512()
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// hashSum calculates the hash of the entire path and returns.
|
||
9 years ago
|
func hashSum(disk StorageAPI, volume, path string, writer hash.Hash) ([]byte, error) {
|
||
9 years ago
|
// Allocate staging buffer of 128KiB for copyBuffer.
|
||
9 years ago
|
buf := make([]byte, readSizeV1)
|
||
9 years ago
|
|
||
|
// Copy entire buffer to writer.
|
||
|
if err := copyBuffer(writer, disk, volume, path, buf); err != nil {
|
||
|
return nil, err
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Return the final hash sum.
|
||
9 years ago
|
return writer.Sum(nil), nil
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// getDataBlockLen - get length of data blocks from encoded blocks.
|
||
|
func getDataBlockLen(enBlocks [][]byte, dataBlocks int) int {
|
||
9 years ago
|
size := 0
|
||
9 years ago
|
// Figure out the data block length.
|
||
|
for _, block := range enBlocks[:dataBlocks] {
|
||
9 years ago
|
size += len(block)
|
||
|
}
|
||
9 years ago
|
return size
|
||
|
}
|
||
|
|
||
|
// Writes all the data blocks from encoded blocks until requested
|
||
|
// outSize length. Provides a way to skip bytes until the offset.
|
||
8 years ago
|
func writeDataBlocks(dst io.Writer, enBlocks [][]byte, dataBlocks int, offset int64, length int64) (int64, error) {
|
||
9 years ago
|
// Offset and out size cannot be negative.
|
||
8 years ago
|
if offset < 0 || length < 0 {
|
||
9 years ago
|
return 0, errUnexpected
|
||
|
}
|
||
|
|
||
9 years ago
|
// Do we have enough blocks?
|
||
|
if len(enBlocks) < dataBlocks {
|
||
|
return 0, reedsolomon.ErrTooFewShards
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Do we have enough data?
|
||
8 years ago
|
if int64(getDataBlockLen(enBlocks, dataBlocks)) < length {
|
||
9 years ago
|
return 0, reedsolomon.ErrShortData
|
||
|
}
|
||
|
|
||
|
// Counter to decrement total left to write.
|
||
8 years ago
|
write := length
|
||
9 years ago
|
|
||
|
// Counter to increment total written.
|
||
|
totalWritten := int64(0)
|
||
|
|
||
|
// Write all data blocks to dst.
|
||
|
for _, block := range enBlocks[:dataBlocks] {
|
||
|
// Skip blocks until we have reached our offset.
|
||
8 years ago
|
if offset >= int64(len(block)) {
|
||
9 years ago
|
// Decrement offset.
|
||
8 years ago
|
offset -= int64(len(block))
|
||
9 years ago
|
continue
|
||
|
} else {
|
||
|
// Skip until offset.
|
||
8 years ago
|
block = block[offset:]
|
||
9 years ago
|
|
||
|
// Reset the offset for next iteration to read everything
|
||
|
// from subsequent blocks.
|
||
8 years ago
|
offset = 0
|
||
9 years ago
|
}
|
||
|
// We have written all the blocks, write the last remaining block.
|
||
|
if write < int64(len(block)) {
|
||
|
n, err := io.Copy(dst, bytes.NewReader(block[:write]))
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
totalWritten += n
|
||
|
break
|
||
|
}
|
||
|
// Copy the block.
|
||
|
n, err := io.Copy(dst, bytes.NewReader(block))
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Decrement output size.
|
||
|
write -= n
|
||
|
|
||
|
// Increment written.
|
||
|
totalWritten += n
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Success.
|
||
|
return totalWritten, nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// 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.
|
||
|
func getChunkSize(blockSize int64, dataBlocks int) int64 {
|
||
|
return (blockSize + int64(dataBlocks) - 1) / int64(dataBlocks)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// copyBuffer - copies from disk, volume, path to input writer until either EOF
|
||
|
// is reached at volume, path or an error occurs. A success copyBuffer returns
|
||
|
// err == nil, not err == EOF. Because copyBuffer is defined to read from path
|
||
|
// until EOF. It does not treat an EOF from ReadFile an error to be reported.
|
||
|
// Additionally copyBuffer stages through the provided buffer; otherwise if it
|
||
|
// has zero length, returns error.
|
||
|
func copyBuffer(writer io.Writer, disk StorageAPI, volume string, path string, buf []byte) error {
|
||
|
// Error condition of zero length buffer.
|
||
|
if buf != nil && len(buf) == 0 {
|
||
|
return errors.New("empty buffer in readBuffer")
|
||
|
}
|
||
|
|
||
|
// Starting offset for Reading the file.
|
||
|
startOffset := int64(0)
|
||
|
|
||
|
// Read until io.EOF.
|
||
|
for {
|
||
|
n, err := disk.ReadFile(volume, path, startOffset, buf)
|
||
|
if n > 0 {
|
||
8 years ago
|
m, wErr := writer.Write(buf[:n])
|
||
|
if wErr != nil {
|
||
|
return wErr
|
||
9 years ago
|
}
|
||
|
if int64(m) != n {
|
||
|
return io.ErrShortWrite
|
||
|
}
|
||
|
}
|
||
|
if err != nil {
|
||
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||
|
break
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
// Progress the offset.
|
||
|
startOffset += n
|
||
|
}
|
||
|
|
||
|
// Success.
|
||
|
return nil
|
||
|
}
|