minio/pkg/storage/donut/v1/donut.go

151 lines
4.0 KiB
Go
Raw Normal View History

2015-02-17 20:47:10 -05:00
/*
* 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.
*/
package v1
2015-02-17 20:47:10 -05:00
import (
"bytes"
2015-02-18 22:46:01 -05:00
"encoding/binary"
"errors"
2015-02-17 20:47:10 -05:00
"io"
2015-02-27 02:04:07 -05:00
"github.com/minio-io/minio/pkg/utils/checksum/crc32c"
2015-02-28 18:53:55 -05:00
"github.com/minio-io/minio/pkg/utils/crypto/sha512"
2015-02-17 20:47:10 -05:00
)
2015-02-18 18:52:12 -05:00
/*
2015-02-18 22:46:01 -05:00
DONUT v1 Spec
**********************
BlockStart [4]byte // Magic="MINI"=1229867341
2015-02-18 18:52:12 -05:00
VersionMajor uint16
VersionMinor uint16
VersionPatch uint16
VersionReserved uint16
Reserved uint64
2015-02-28 18:59:52 -05:00
DataLen uint64
HeaderCrc32c uint32
BlockData [4]byte // Magic="DATA"=1096040772
2015-02-18 20:18:33 -05:00
Data io.Reader // matches length
2015-02-28 18:59:52 -05:00
HeaderCrc32c uint32
DataSha512 [64]byte
BlockLen uint64 // length of entire frame, inclusive of MINI and INIM
2015-02-21 01:42:58 -05:00
BlockEnd [4]byte // Magic="INIM"=1296649801
2015-02-18 22:46:01 -05:00
2015-02-18 18:52:12 -05:00
*/
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'})
)
2015-02-28 18:53:55 -05:00
type DonutFrameHeader struct {
MagicMINI uint32
2015-02-18 18:52:12 -05:00
VersionMajor uint16
VersionMinor uint16
VersionPatch uint16
VersionReserved uint16
Reserved uint64
2015-02-28 18:53:55 -05:00
DataLength uint64
2015-02-18 18:52:12 -05:00
}
2015-02-28 18:53:55 -05:00
type Crc32c uint32
type Sha512 [sha512.Size]byte
2015-02-18 18:52:12 -05:00
2015-02-28 18:53:55 -05:00
type DonutFrameFooter struct {
DataSha512 Sha512
OffsetToMINI uint64
MagicINIM uint32
2015-02-18 18:52:12 -05:00
}
2015-02-28 18:53:55 -05:00
type Data bytes.Buffer
2015-02-28 18:53:55 -05:00
func Write(target io.Writer, reader io.Reader, length uint64) error {
// write header
header := DonutFrameHeader{
MagicMINI: MagicMINI,
VersionMajor: 1,
VersionMinor: 0,
VersionPatch: 0,
VersionReserved: 0,
Reserved: 0,
2015-02-28 18:53:55 -05:00
DataLength: length,
}
2015-02-28 18:53:55 -05:00
var headerBytes bytes.Buffer
binary.Write(&headerBytes, binary.LittleEndian, header)
headerCrc := crc32c.Sum32(headerBytes.Bytes())
2015-02-28 18:53:55 -05:00
binary.Write(&headerBytes, binary.LittleEndian, headerCrc)
binary.Write(&headerBytes, binary.LittleEndian, MagicDATA)
// write header
headerLen, err := io.Copy(target, &headerBytes)
2015-02-27 15:18:08 -05:00
if err != nil {
return err
}
2015-02-28 18:53:55 -05:00
// 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)
dataLength, err := io.Copy(target, teeReader)
2015-02-28 18:53:55 -05:00
if err != nil {
2015-02-21 02:24:01 -05:00
return err
}
if uint64(dataLength) != length {
return errors.New("Specified data length and amount written mismatched")
}
2015-02-28 18:53:55 -05:00
sumWriter.Close()
dataChecksum := <-checksumChannel
if dataChecksum.err != nil {
return dataChecksum.err
}
2015-02-28 18:53:55 -05:00
// 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)
2015-02-27 02:04:07 -05:00
// write footer crc
2015-02-28 18:53:55 -05:00
footerChecksum := crc32c.Sum32(frameFooterBytes.Bytes())
if err := binary.Write(target, binary.LittleEndian, footerChecksum); err != nil {
2015-02-21 02:24:01 -05:00
return err
}
2015-02-28 18:53:55 -05:00
// write write footer
_, err = io.Copy(target, &frameFooterBytes)
if err != nil {
return err
}
return nil
}
2015-02-18 22:46:01 -05:00
2015-02-28 18:53:55 -05:00
type checksumValue struct {
checksum Sha512
err error
}
2015-02-27 02:04:07 -05:00
2015-02-28 18:53:55 -05:00
func generateChecksum(reader io.Reader, c chan<- checksumValue) {
checksum, err := sha512.SumStream(reader)
result := checksumValue{
checksum: checksum,
err: err,
}
c <- result
2015-02-18 22:46:01 -05:00
}