2012-12-21 13:57:03 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-12-21 14:32:21 +08:00
|
|
|
"errors"
|
2013-01-17 16:22:18 +08:00
|
|
|
"fmt"
|
2012-12-21 13:57:03 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2012-12-21 13:57:03 +08:00
|
|
|
)
|
|
|
|
|
2013-01-20 19:40:04 +08:00
|
|
|
const (
|
2013-07-09 14:38:38 +08:00
|
|
|
FlagGzip = 0x01
|
|
|
|
FlagHasName = 0x02
|
|
|
|
FlagHasMime = 0x04
|
|
|
|
FlagHasLastModifiedDate = 0x08
|
2014-09-21 03:38:59 +08:00
|
|
|
FlagHasTtl = 0x10
|
2017-01-08 09:16:29 +08:00
|
|
|
FlagHasPairs = 0x20
|
2015-12-15 14:38:58 +08:00
|
|
|
FlagIsChunkManifest = 0x80
|
2013-07-09 14:38:38 +08:00
|
|
|
LastModifiedBytesLength = 5
|
2014-09-21 03:38:59 +08:00
|
|
|
TtlBytesLength = 2
|
2013-01-20 19:40:04 +08:00
|
|
|
)
|
|
|
|
|
2013-09-20 02:06:14 +08:00
|
|
|
func (n *Needle) DiskSize() int64 {
|
2017-01-07 02:22:20 +08:00
|
|
|
return getActualSize(n.Size)
|
2013-01-21 11:44:23 +08:00
|
|
|
}
|
2017-01-07 02:22:20 +08:00
|
|
|
|
|
|
|
func (n *Needle) Append(w io.Writer, version Version) (size uint32, actualSize int64, err error) {
|
2013-02-07 06:30:44 +08:00
|
|
|
if s, ok := w.(io.Seeker); ok {
|
|
|
|
if end, e := s.Seek(0, 1); e == nil {
|
|
|
|
defer func(s io.Seeker, off int64) {
|
|
|
|
if err != nil {
|
|
|
|
if _, e = s.Seek(off, 0); e != nil {
|
2015-01-14 09:04:41 +08:00
|
|
|
glog.V(0).Infof("Failed to seek %s back to %d with error: %v", w, off, e)
|
2013-02-07 06:30:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s, end)
|
|
|
|
} else {
|
2015-01-15 01:56:13 +08:00
|
|
|
err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
|
2013-02-07 06:30:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-01-17 16:22:18 +08:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-21 16:36:55 +08:00
|
|
|
header := make([]byte, NeedleHeaderSize)
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
2012-12-21 16:36:55 +08:00
|
|
|
n.Size = uint32(len(n.Data))
|
2013-01-20 19:40:04 +08:00
|
|
|
size = n.Size
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
2013-01-20 19:40:04 +08:00
|
|
|
if _, err = w.Write(header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-07 02:22:20 +08:00
|
|
|
actualSize = NeedleHeaderSize + int64(n.Size)
|
2012-12-23 04:38:09 +08:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2013-01-20 19:40:04 +08:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
|
|
|
return
|
2013-01-17 16:22:18 +08:00
|
|
|
case Version2:
|
2012-12-21 18:13:02 +08:00
|
|
|
header := make([]byte, NeedleHeaderSize)
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
2012-12-21 18:13:02 +08:00
|
|
|
n.DataSize, n.NameSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Name)), uint8(len(n.Mime))
|
|
|
|
if n.DataSize > 0 {
|
|
|
|
n.Size = 4 + n.DataSize + 1
|
2012-12-23 04:38:09 +08:00
|
|
|
if n.HasName() {
|
2012-12-21 18:13:02 +08:00
|
|
|
n.Size = n.Size + 1 + uint32(n.NameSize)
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
if n.HasMime() {
|
2012-12-21 18:13:02 +08:00
|
|
|
n.Size = n.Size + 1 + uint32(n.MimeSize)
|
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
if n.HasLastModifiedDate() {
|
|
|
|
n.Size = n.Size + LastModifiedBytesLength
|
|
|
|
}
|
2014-09-21 03:38:59 +08:00
|
|
|
if n.HasTtl() {
|
|
|
|
n.Size = n.Size + TtlBytesLength
|
|
|
|
}
|
2017-01-08 09:16:29 +08:00
|
|
|
if n.HasPairs() {
|
|
|
|
n.Size += 2 + uint32(n.PairsSize)
|
|
|
|
}
|
2015-03-09 15:34:26 +08:00
|
|
|
} else {
|
|
|
|
n.Size = 0
|
2012-12-21 18:13:02 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
size = n.DataSize
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
2013-01-20 19:40:04 +08:00
|
|
|
if _, err = w.Write(header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 18:13:02 +08:00
|
|
|
if n.DataSize > 0 {
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[0:4], n.DataSize)
|
2013-01-20 19:40:04 +08:00
|
|
|
if _, err = w.Write(header[0:4]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint8toBytes(header[0:1], n.Flags)
|
2013-01-20 19:40:04 +08:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-12 15:30:45 +08:00
|
|
|
if n.HasName() {
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint8toBytes(header[0:1], n.NameSize)
|
2013-07-12 15:30:45 +08:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Name); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
}
|
2013-07-12 15:30:45 +08:00
|
|
|
if n.HasMime() {
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint8toBytes(header[0:1], n.MimeSize)
|
2013-07-12 15:30:45 +08:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Mime); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
}
|
2013-07-12 15:30:45 +08:00
|
|
|
if n.HasLastModifiedDate() {
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint64toBytes(header[0:8], n.LastModified)
|
2013-07-12 15:30:45 +08:00
|
|
|
if _, err = w.Write(header[8-LastModifiedBytesLength : 8]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
}
|
2015-07-09 14:21:53 +08:00
|
|
|
if n.HasTtl() && n.Ttl != nil {
|
2014-09-21 03:38:59 +08:00
|
|
|
n.Ttl.ToBytes(header[0:TtlBytesLength])
|
|
|
|
if _, err = w.Write(header[0:TtlBytesLength]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 09:16:29 +08:00
|
|
|
if n.HasPairs() {
|
|
|
|
util.Uint16toBytes(header[0:2], n.PairsSize)
|
|
|
|
if _, err = w.Write(header[0:2]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Pairs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
2016-04-10 15:24:22 +08:00
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2013-01-20 19:40:04 +08:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
2017-01-07 02:22:20 +08:00
|
|
|
|
|
|
|
actualSize = NeedleHeaderSize + int64(n.Size) + NeedleChecksumSize + int64(padding)
|
|
|
|
|
|
|
|
return n.DataSize, actualSize, err
|
2012-12-21 16:36:55 +08:00
|
|
|
}
|
2017-01-07 02:22:20 +08:00
|
|
|
return 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
2013-01-20 19:40:04 +08:00
|
|
|
|
2016-04-16 02:56:53 +08:00
|
|
|
func ReadNeedleBlob(r *os.File, offset int64, size uint32) (dataSlice []byte, block *Block, err error) {
|
2017-01-08 09:16:29 +08:00
|
|
|
NeedleWithoutPaddingSize := NeedleHeaderSize + size + NeedleChecksumSize
|
|
|
|
padding := NeedlePaddingSize - (NeedleWithoutPaddingSize % NeedlePaddingSize)
|
|
|
|
readSize := NeedleWithoutPaddingSize + padding
|
2016-04-18 03:03:45 +08:00
|
|
|
return getBytesForFileBlock(r, offset, int(readSize))
|
2016-04-14 16:30:26 +08:00
|
|
|
}
|
|
|
|
|
2015-05-26 15:58:41 +08:00
|
|
|
func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {
|
2016-04-16 02:56:53 +08:00
|
|
|
bytes, block, err := ReadNeedleBlob(r, offset, size)
|
2015-05-26 15:58:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-13 15:19:57 +08:00
|
|
|
n.rawBlock = block
|
2015-05-26 15:58:41 +08:00
|
|
|
n.ParseNeedleHeader(bytes)
|
|
|
|
if n.Size != size {
|
|
|
|
return fmt.Errorf("File Entry Not Found. Needle %d Memory %d", n.Size, size)
|
|
|
|
}
|
2013-01-17 16:22:18 +08:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-21 18:13:02 +08:00
|
|
|
n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
|
2013-01-17 16:22:18 +08:00
|
|
|
case Version2:
|
2012-12-21 18:13:02 +08:00
|
|
|
n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
2016-07-25 11:40:35 +08:00
|
|
|
if size == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-10 15:24:22 +08:00
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
|
2015-05-26 15:58:41 +08:00
|
|
|
newChecksum := NewCRC(n.Data)
|
|
|
|
if checksum != newChecksum.Value() {
|
|
|
|
return errors.New("CRC error! Data On Disk Corrupted")
|
|
|
|
}
|
|
|
|
n.Checksum = newChecksum
|
|
|
|
return nil
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
func (n *Needle) ParseNeedleHeader(bytes []byte) {
|
2016-04-10 15:24:22 +08:00
|
|
|
n.Cookie = util.BytesToUint32(bytes[0:4])
|
|
|
|
n.Id = util.BytesToUint64(bytes[4:12])
|
|
|
|
n.Size = util.BytesToUint32(bytes[12:NeedleHeaderSize])
|
2012-12-21 18:13:02 +08:00
|
|
|
}
|
|
|
|
func (n *Needle) readNeedleDataVersion2(bytes []byte) {
|
2012-12-21 18:20:38 +08:00
|
|
|
index, lenBytes := 0, len(bytes)
|
|
|
|
if index < lenBytes {
|
2016-04-10 15:24:22 +08:00
|
|
|
n.DataSize = util.BytesToUint32(bytes[index : index+4])
|
2012-12-21 18:20:38 +08:00
|
|
|
index = index + 4
|
2015-03-09 15:34:26 +08:00
|
|
|
if int(n.DataSize)+index > lenBytes {
|
|
|
|
// this if clause is due to bug #87 and #93, fixed in v0.69
|
|
|
|
// remove this clause later
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 18:20:38 +08:00
|
|
|
n.Data = bytes[index : index+int(n.DataSize)]
|
|
|
|
index = index + int(n.DataSize)
|
|
|
|
n.Flags = bytes[index]
|
|
|
|
index = index + 1
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
if index < lenBytes && n.HasName() {
|
2012-12-21 18:13:02 +08:00
|
|
|
n.NameSize = uint8(bytes[index])
|
|
|
|
index = index + 1
|
|
|
|
n.Name = bytes[index : index+int(n.NameSize)]
|
|
|
|
index = index + int(n.NameSize)
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
if index < lenBytes && n.HasMime() {
|
2012-12-21 18:13:02 +08:00
|
|
|
n.MimeSize = uint8(bytes[index])
|
|
|
|
index = index + 1
|
|
|
|
n.Mime = bytes[index : index+int(n.MimeSize)]
|
2013-07-09 14:38:38 +08:00
|
|
|
index = index + int(n.MimeSize)
|
|
|
|
}
|
|
|
|
if index < lenBytes && n.HasLastModifiedDate() {
|
2016-04-10 15:24:22 +08:00
|
|
|
n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
|
2013-07-09 14:38:38 +08:00
|
|
|
index = index + LastModifiedBytesLength
|
2012-12-21 18:13:02 +08:00
|
|
|
}
|
2014-09-21 03:38:59 +08:00
|
|
|
if index < lenBytes && n.HasTtl() {
|
|
|
|
n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
|
|
|
|
index = index + TtlBytesLength
|
|
|
|
}
|
2017-01-08 09:16:29 +08:00
|
|
|
if index < lenBytes && n.HasPairs() {
|
|
|
|
n.PairsSize = util.BytesToUint16(bytes[index : index+2])
|
|
|
|
index += 2
|
|
|
|
end := index + int(n.PairsSize)
|
|
|
|
n.Pairs = bytes[index:end]
|
|
|
|
index = end
|
|
|
|
}
|
2012-12-21 18:13:02 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
|
2014-03-19 14:48:01 +08:00
|
|
|
func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bodyLength uint32, err error) {
|
2012-12-21 14:32:21 +08:00
|
|
|
n = new(Needle)
|
2012-12-21 18:13:02 +08:00
|
|
|
if version == Version1 || version == Version2 {
|
2012-12-21 16:36:55 +08:00
|
|
|
bytes := make([]byte, NeedleHeaderSize)
|
2013-01-20 19:40:04 +08:00
|
|
|
var count int
|
2014-03-19 14:48:01 +08:00
|
|
|
count, err = r.ReadAt(bytes, offset)
|
2013-01-20 19:40:04 +08:00
|
|
|
if count <= 0 || err != nil {
|
|
|
|
return nil, 0, err
|
2012-12-21 14:32:21 +08:00
|
|
|
}
|
2015-05-26 15:58:41 +08:00
|
|
|
n.ParseNeedleHeader(bytes)
|
2012-12-23 04:38:09 +08:00
|
|
|
padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
bodyLength = n.Size + NeedleChecksumSize + padding
|
2012-12-21 16:36:55 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//n should be a needle already read the header
|
|
|
|
//the input stream will read until next file entry
|
2014-03-19 14:48:01 +08:00
|
|
|
func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength uint32) (err error) {
|
2013-02-07 06:30:44 +08:00
|
|
|
if bodyLength <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-17 16:22:18 +08:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-21 16:36:55 +08:00
|
|
|
bytes := make([]byte, bodyLength)
|
2014-03-19 14:48:01 +08:00
|
|
|
if _, err = r.ReadAt(bytes, offset); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2012-12-21 16:36:55 +08:00
|
|
|
n.Data = bytes[:n.Size]
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
2013-01-17 16:22:18 +08:00
|
|
|
case Version2:
|
2012-12-21 18:13:02 +08:00
|
|
|
bytes := make([]byte, bodyLength)
|
2014-03-19 14:48:01 +08:00
|
|
|
if _, err = r.ReadAt(bytes, offset); err != nil {
|
2013-01-20 19:40:04 +08:00
|
|
|
return
|
|
|
|
}
|
2012-12-21 18:13:02 +08:00
|
|
|
n.readNeedleDataVersion2(bytes[0:n.Size])
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
2013-01-20 19:40:04 +08:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unsupported Version! (%d)", version)
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
2012-12-21 14:32:21 +08:00
|
|
|
return
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
2012-12-22 18:10:45 +08:00
|
|
|
|
2012-12-23 04:38:09 +08:00
|
|
|
func (n *Needle) IsGzipped() bool {
|
2013-01-20 19:40:04 +08:00
|
|
|
return n.Flags&FlagGzip > 0
|
2012-12-23 04:38:09 +08:00
|
|
|
}
|
|
|
|
func (n *Needle) SetGzipped() {
|
2013-01-20 19:40:04 +08:00
|
|
|
n.Flags = n.Flags | FlagGzip
|
2012-12-23 04:38:09 +08:00
|
|
|
}
|
|
|
|
func (n *Needle) HasName() bool {
|
2013-01-20 19:40:04 +08:00
|
|
|
return n.Flags&FlagHasName > 0
|
2012-12-23 04:38:09 +08:00
|
|
|
}
|
|
|
|
func (n *Needle) SetHasName() {
|
2013-01-20 19:40:04 +08:00
|
|
|
n.Flags = n.Flags | FlagHasName
|
2012-12-23 04:38:09 +08:00
|
|
|
}
|
|
|
|
func (n *Needle) HasMime() bool {
|
2013-01-20 19:40:04 +08:00
|
|
|
return n.Flags&FlagHasMime > 0
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
func (n *Needle) SetHasMime() {
|
2013-01-20 19:40:04 +08:00
|
|
|
n.Flags = n.Flags | FlagHasMime
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
func (n *Needle) HasLastModifiedDate() bool {
|
|
|
|
return n.Flags&FlagHasLastModifiedDate > 0
|
|
|
|
}
|
|
|
|
func (n *Needle) SetHasLastModifiedDate() {
|
|
|
|
n.Flags = n.Flags | FlagHasLastModifiedDate
|
|
|
|
}
|
2014-09-21 03:38:59 +08:00
|
|
|
func (n *Needle) HasTtl() bool {
|
|
|
|
return n.Flags&FlagHasTtl > 0
|
|
|
|
}
|
|
|
|
func (n *Needle) SetHasTtl() {
|
|
|
|
n.Flags = n.Flags | FlagHasTtl
|
|
|
|
}
|
2015-11-29 23:49:41 +08:00
|
|
|
|
2015-12-01 20:53:44 +08:00
|
|
|
func (n *Needle) IsChunkedManifest() bool {
|
2015-12-15 14:38:58 +08:00
|
|
|
return n.Flags&FlagIsChunkManifest > 0
|
2015-11-29 23:49:41 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 14:38:58 +08:00
|
|
|
func (n *Needle) SetIsChunkManifest() {
|
|
|
|
n.Flags = n.Flags | FlagIsChunkManifest
|
2015-11-29 23:49:41 +08:00
|
|
|
}
|
2017-01-08 09:16:29 +08:00
|
|
|
|
|
|
|
func (n *Needle) HasPairs() bool {
|
|
|
|
return n.Flags&FlagHasPairs != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Needle) SetHasPairs() {
|
|
|
|
n.Flags = n.Flags | FlagHasPairs
|
|
|
|
}
|