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.
162 lines
4.2 KiB
162 lines
4.2 KiB
10 years ago
|
/*
|
||
|
* Mini Object Storage, (C) 2015 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package fragment
|
||
10 years ago
|
|
||
|
import (
|
||
10 years ago
|
"bytes"
|
||
10 years ago
|
"encoding/binary"
|
||
10 years ago
|
"errors"
|
||
10 years ago
|
"io"
|
||
10 years ago
|
|
||
10 years ago
|
"github.com/minio-io/minio/pkg/utils/checksum/crc32c"
|
||
10 years ago
|
"github.com/minio-io/minio/pkg/utils/crypto/sha512"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
/*
|
||
|
|
||
10 years ago
|
DONUT v1 Spec
|
||
|
**********************
|
||
10 years ago
|
BlockStart uint32 // Magic="MINI"=1229867341
|
||
|
VersionMajor uint32
|
||
10 years ago
|
Reserved uint64
|
||
10 years ago
|
DataLen uint64
|
||
|
HeaderCrc32c uint32
|
||
10 years ago
|
BlockData uint32 // Magic="DATA"=1096040772
|
||
10 years ago
|
Data io.Reader // matches length
|
||
10 years ago
|
HeaderCrc32c uint32
|
||
|
DataSha512 [64]byte
|
||
|
BlockLen uint64 // length of entire frame, inclusive of MINI and INIM
|
||
10 years ago
|
BlockEnd uint32 // Magic="INIM"=1296649801
|
||
10 years ago
|
|
||
10 years ago
|
*/
|
||
|
|
||
10 years ago
|
// Magic list
|
||
10 years ago
|
var (
|
||
|
MagicMINI = binary.LittleEndian.Uint32([]byte{'M', 'I', 'N', 'I'})
|
||
|
MagicDATA = binary.LittleEndian.Uint32([]byte{'D', 'A', 'T', 'A'})
|
||
|
MagicINIM = binary.LittleEndian.Uint32([]byte{'I', 'N', 'I', 'M'})
|
||
|
)
|
||
|
|
||
10 years ago
|
// DonutFrameHeader -
|
||
|
// --------------
|
||
|
// BlockStart uint32
|
||
|
// VersionMajor uint32
|
||
|
// Reserved uint64
|
||
|
// DataLen uint64
|
||
|
// --------------
|
||
10 years ago
|
type DonutFrameHeader struct {
|
||
10 years ago
|
MagicMINI uint32
|
||
|
Version uint32
|
||
|
Reserved uint64
|
||
|
DataLength uint64
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
// Crc32c checksum
|
||
10 years ago
|
type Crc32c uint32
|
||
10 years ago
|
|
||
|
// Sha512 checksum
|
||
10 years ago
|
type Sha512 [sha512.Size]byte
|
||
10 years ago
|
|
||
10 years ago
|
// DonutFrameFooter -
|
||
|
// --------------
|
||
|
// DataSha512 [64]byte
|
||
|
// BlockLen uint64
|
||
|
// BlockEnd uint32
|
||
|
// --------------
|
||
10 years ago
|
type DonutFrameFooter struct {
|
||
|
DataSha512 Sha512
|
||
|
OffsetToMINI uint64
|
||
|
MagicINIM uint32
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Data buffer
|
||
10 years ago
|
type Data bytes.Buffer
|
||
10 years ago
|
|
||
10 years ago
|
// Write - write donut format to input io.Writer, returns error upon any failure
|
||
|
func Write(target io.Writer, reader io.Reader, length uint64) error {
|
||
10 years ago
|
// write header
|
||
|
header := DonutFrameHeader{
|
||
10 years ago
|
MagicMINI: MagicMINI,
|
||
|
Version: 1,
|
||
|
Reserved: 0,
|
||
|
DataLength: length,
|
||
10 years ago
|
}
|
||
10 years ago
|
var headerBytes bytes.Buffer
|
||
|
binary.Write(&headerBytes, binary.LittleEndian, header)
|
||
|
headerCrc := crc32c.Sum32(headerBytes.Bytes())
|
||
10 years ago
|
|
||
10 years ago
|
binary.Write(&headerBytes, binary.LittleEndian, headerCrc)
|
||
|
binary.Write(&headerBytes, binary.LittleEndian, MagicDATA)
|
||
|
// write header
|
||
|
headerLen, err := io.Copy(target, &headerBytes)
|
||
10 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
10 years ago
|
// write DATA
|
||
|
// create sha512 tee
|
||
|
sumReader, sumWriter := io.Pipe()
|
||
|
defer sumWriter.Close()
|
||
|
checksumChannel := make(chan checksumValue)
|
||
|
go generateChecksum(sumReader, checksumChannel)
|
||
|
teeReader := io.TeeReader(reader, sumWriter)
|
||
10 years ago
|
dataLength, err := io.Copy(target, teeReader)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return err
|
||
|
}
|
||
10 years ago
|
if uint64(dataLength) != length {
|
||
|
return errors.New("Specified data length and amount written mismatched")
|
||
|
}
|
||
10 years ago
|
sumWriter.Close()
|
||
|
dataChecksum := <-checksumChannel
|
||
|
if dataChecksum.err != nil {
|
||
|
return dataChecksum.err
|
||
10 years ago
|
}
|
||
10 years ago
|
// generate footer
|
||
|
frameFooter := DonutFrameFooter{
|
||
|
DataSha512: dataChecksum.checksum,
|
||
|
OffsetToMINI: length + uint64(headerLen) + uint64(80), /*footer size*/
|
||
|
MagicINIM: MagicINIM,
|
||
|
}
|
||
|
var frameFooterBytes bytes.Buffer
|
||
|
binary.Write(&frameFooterBytes, binary.LittleEndian, frameFooter)
|
||
10 years ago
|
// write footer crc
|
||
10 years ago
|
footerChecksum := crc32c.Sum32(frameFooterBytes.Bytes())
|
||
|
if err := binary.Write(target, binary.LittleEndian, footerChecksum); err != nil {
|
||
10 years ago
|
return err
|
||
|
}
|
||
10 years ago
|
// write write footer
|
||
|
_, err = io.Copy(target, &frameFooterBytes)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
type checksumValue struct {
|
||
|
checksum Sha512
|
||
|
err error
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func generateChecksum(reader io.Reader, c chan<- checksumValue) {
|
||
|
checksum, err := sha512.SumStream(reader)
|
||
|
result := checksumValue{
|
||
|
checksum: checksum,
|
||
|
err: err,
|
||
|
}
|
||
|
c <- result
|
||
10 years ago
|
}
|