2019-04-19 04:43:36 +00:00
|
|
|
package needle
|
2012-12-21 05:57:03 +00:00
|
|
|
|
|
|
|
import (
|
2012-12-21 06:32:21 +00:00
|
|
|
"errors"
|
2013-01-17 08:22:18 +00:00
|
|
|
"fmt"
|
2019-01-16 08:48:03 +00:00
|
|
|
"io"
|
2012-12-21 05:57:03 +00:00
|
|
|
"os"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
"math"
|
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-22 00:39:10 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2012-12-21 05:57:03 +00:00
|
|
|
)
|
|
|
|
|
2013-01-20 11:40:04 +00:00
|
|
|
const (
|
2013-07-09 06:38:38 +00:00
|
|
|
FlagGzip = 0x01
|
|
|
|
FlagHasName = 0x02
|
|
|
|
FlagHasMime = 0x04
|
|
|
|
FlagHasLastModifiedDate = 0x08
|
2014-09-20 19:38:59 +00:00
|
|
|
FlagHasTtl = 0x10
|
2017-01-08 01:16:29 +00:00
|
|
|
FlagHasPairs = 0x20
|
2015-12-15 06:38:58 +00:00
|
|
|
FlagIsChunkManifest = 0x80
|
2013-07-09 06:38:38 +00:00
|
|
|
LastModifiedBytesLength = 5
|
2014-09-20 19:38:59 +00:00
|
|
|
TtlBytesLength = 2
|
2013-01-20 11:40:04 +00:00
|
|
|
)
|
|
|
|
|
2018-07-24 08:36:04 +00:00
|
|
|
func (n *Needle) DiskSize(version Version) int64 {
|
2019-05-28 07:51:01 +00:00
|
|
|
return GetActualSize(n.Size, version)
|
2013-01-21 03:44:23 +00:00
|
|
|
}
|
2017-01-06 18:22:20 +00:00
|
|
|
|
2019-01-16 08:48:03 +00:00
|
|
|
func (n *Needle) Append(w *os.File, version Version) (offset uint64, size uint32, actualSize int64, err error) {
|
|
|
|
if end, e := w.Seek(0, io.SeekEnd); e == nil {
|
2018-12-31 23:08:32 +00:00
|
|
|
defer func(w *os.File, off int64) {
|
|
|
|
if err != nil {
|
|
|
|
if te := w.Truncate(end); te != nil {
|
|
|
|
glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
|
2013-02-06 22:30:44 +00:00
|
|
|
}
|
2018-12-31 23:08:32 +00:00
|
|
|
}
|
|
|
|
}(w, end)
|
2019-01-16 08:48:03 +00:00
|
|
|
offset = uint64(end)
|
2018-12-31 23:08:32 +00:00
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
|
|
|
|
return
|
2013-02-06 22:30:44 +00:00
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2019-04-19 07:39:34 +00:00
|
|
|
header := make([]byte, NeedleHeaderSize)
|
2018-07-08 09:28:04 +00:00
|
|
|
CookieToBytes(header[0:CookieSize], n.Cookie)
|
|
|
|
NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
|
2012-12-21 08:36:55 +00:00
|
|
|
n.Size = uint32(len(n.Data))
|
2013-01-20 11:40:04 +00:00
|
|
|
size = n.Size
|
2018-07-08 09:28:04 +00:00
|
|
|
util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = w.Write(header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
actualSize = NeedleHeaderSize + int64(n.Size)
|
2018-07-24 08:36:04 +00:00
|
|
|
padding := PaddingLength(n.Size, version)
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2018-07-24 08:38:08 +00:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2018-07-24 08:36:04 +00:00
|
|
|
case Version2, Version3:
|
2019-04-19 07:39:34 +00:00
|
|
|
header := make([]byte, NeedleHeaderSize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
|
2018-07-08 09:28:04 +00:00
|
|
|
CookieToBytes(header[0:CookieSize], n.Cookie)
|
|
|
|
NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
|
2018-12-11 04:57:01 +00:00
|
|
|
if len(n.Name) >= math.MaxUint8 {
|
2018-12-11 04:57:51 +00:00
|
|
|
n.NameSize = math.MaxUint8
|
2018-12-11 04:57:01 +00:00
|
|
|
} else {
|
|
|
|
n.NameSize = uint8(len(n.Name))
|
|
|
|
}
|
|
|
|
n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
|
2012-12-21 10:13:02 +00:00
|
|
|
if n.DataSize > 0 {
|
|
|
|
n.Size = 4 + n.DataSize + 1
|
2012-12-22 20:38:09 +00:00
|
|
|
if n.HasName() {
|
2012-12-21 10:13:02 +00:00
|
|
|
n.Size = n.Size + 1 + uint32(n.NameSize)
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
if n.HasMime() {
|
2012-12-21 10:13:02 +00:00
|
|
|
n.Size = n.Size + 1 + uint32(n.MimeSize)
|
|
|
|
}
|
2013-07-09 06:38:38 +00:00
|
|
|
if n.HasLastModifiedDate() {
|
|
|
|
n.Size = n.Size + LastModifiedBytesLength
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
if n.HasTtl() {
|
|
|
|
n.Size = n.Size + TtlBytesLength
|
|
|
|
}
|
2017-01-08 01:16:29 +00:00
|
|
|
if n.HasPairs() {
|
|
|
|
n.Size += 2 + uint32(n.PairsSize)
|
|
|
|
}
|
2015-03-09 07:34:26 +00:00
|
|
|
} else {
|
|
|
|
n.Size = 0
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
size = n.DataSize
|
2018-07-08 09:28:04 +00:00
|
|
|
util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
|
2019-04-19 07:39:34 +00:00
|
|
|
if _, err = w.Write(header[0:NeedleHeaderSize]); err != nil {
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
if n.DataSize > 0 {
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint32toBytes(header[0:4], n.DataSize)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = w.Write(header[0:4]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint8toBytes(header[0:1], n.Flags)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-12 07:30:45 +00:00
|
|
|
if n.HasName() {
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint8toBytes(header[0:1], n.NameSize)
|
2013-07-12 07:30:45 +00:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-12-11 04:57:01 +00:00
|
|
|
if _, err = w.Write(n.Name[:n.NameSize]); err != nil {
|
2013-07-12 07:30:45 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
}
|
2013-07-12 07:30:45 +00:00
|
|
|
if n.HasMime() {
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint8toBytes(header[0:1], n.MimeSize)
|
2013-07-12 07:30:45 +00:00
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Mime); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
}
|
2013-07-12 07:30:45 +00:00
|
|
|
if n.HasLastModifiedDate() {
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint64toBytes(header[0:8], n.LastModified)
|
2018-07-24 08:38:08 +00:00
|
|
|
if _, err = w.Write(header[8-LastModifiedBytesLength : 8]); err != nil {
|
2013-07-12 07:30:45 +00:00
|
|
|
return
|
|
|
|
}
|
2013-07-09 06:38:38 +00:00
|
|
|
}
|
2015-07-09 06:21:53 +00:00
|
|
|
if n.HasTtl() && n.Ttl != nil {
|
2014-09-20 19:38:59 +00:00
|
|
|
n.Ttl.ToBytes(header[0:TtlBytesLength])
|
|
|
|
if _, err = w.Write(header[0:TtlBytesLength]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 01:16:29 +00: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 06:38:38 +00:00
|
|
|
}
|
2018-07-24 08:36:04 +00:00
|
|
|
padding := PaddingLength(n.Size, version)
|
2016-04-10 07:24:22 +00:00
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2018-07-24 08:36:04 +00:00
|
|
|
if version == Version2 {
|
2018-07-24 08:38:08 +00:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
2018-07-24 08:36:04 +00:00
|
|
|
} else {
|
|
|
|
// version3
|
|
|
|
util.Uint64toBytes(header[NeedleChecksumSize:NeedleChecksumSize+TimestampSize], n.AppendAtNs)
|
2018-07-24 08:38:08 +00:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+TimestampSize+padding])
|
2018-07-24 08:36:04 +00:00
|
|
|
}
|
2017-01-06 18:22:20 +00:00
|
|
|
|
2019-05-28 07:51:01 +00:00
|
|
|
return offset, n.DataSize, GetActualSize(n.Size, version), err
|
2012-12-21 08:36:55 +00:00
|
|
|
}
|
2018-12-31 23:08:32 +00:00
|
|
|
return 0, 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
|
2018-07-24 08:36:04 +00:00
|
|
|
func ReadNeedleBlob(r *os.File, offset int64, size uint32, version Version) (dataSlice []byte, err error) {
|
2019-05-28 07:51:01 +00:00
|
|
|
dataSlice = make([]byte, int(GetActualSize(size, version)))
|
2018-07-08 09:28:04 +00:00
|
|
|
_, err = r.ReadAt(dataSlice, offset)
|
|
|
|
return dataSlice, err
|
2016-04-14 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 18:59:03 +00:00
|
|
|
// ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
|
|
|
|
func (n *Needle) ReadBytes(bytes []byte, offset int64, size uint32, version Version) (err error) {
|
2015-05-26 07:58:41 +00:00
|
|
|
n.ParseNeedleHeader(bytes)
|
|
|
|
if n.Size != size {
|
2019-05-28 21:27:21 +00:00
|
|
|
return fmt.Errorf("entry not found: offset %d found id %d size %d, expected size %d", offset, n.Id, n.Size, size)
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2019-04-19 07:39:34 +00:00
|
|
|
n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
|
2018-07-24 08:36:04 +00:00
|
|
|
case Version2, Version3:
|
2019-04-19 07:39:34 +00:00
|
|
|
err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2019-04-30 03:22:19 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2019-03-19 12:34:43 +00:00
|
|
|
return err
|
2016-07-25 03:40:35 +00:00
|
|
|
}
|
2019-04-19 08:56:38 +00:00
|
|
|
if size > 0 {
|
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
|
|
|
|
newChecksum := NewCRC(n.Data)
|
|
|
|
if checksum != newChecksum.Value() {
|
|
|
|
return errors.New("CRC error! Data On Disk Corrupted")
|
|
|
|
}
|
|
|
|
n.Checksum = newChecksum
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|
2018-07-24 08:36:04 +00:00
|
|
|
if version == Version3 {
|
2019-04-19 07:39:34 +00:00
|
|
|
tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
|
2018-07-24 08:38:08 +00:00
|
|
|
n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
|
2018-07-24 08:36:04 +00:00
|
|
|
}
|
2015-05-26 07:58:41 +00:00
|
|
|
return nil
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
|
2019-05-27 18:59:03 +00:00
|
|
|
// ReadData hydrates the needle from the file, with only n.Id is set.
|
|
|
|
func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {
|
|
|
|
bytes, err := ReadNeedleBlob(r, offset, size, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.ReadBytes(bytes, offset, size, version)
|
|
|
|
}
|
|
|
|
|
2015-05-26 07:58:41 +00:00
|
|
|
func (n *Needle) ParseNeedleHeader(bytes []byte) {
|
2018-07-08 09:28:04 +00:00
|
|
|
n.Cookie = BytesToCookie(bytes[0:CookieSize])
|
2018-07-24 08:38:08 +00:00
|
|
|
n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
|
2019-04-19 07:39:34 +00:00
|
|
|
n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
|
2019-03-19 12:34:43 +00:00
|
|
|
func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
|
2012-12-21 10:20:38 +00:00
|
|
|
index, lenBytes := 0, len(bytes)
|
|
|
|
if index < lenBytes {
|
2018-07-24 08:38:08 +00:00
|
|
|
n.DataSize = util.BytesToUint32(bytes[index : index+4])
|
2012-12-21 10:20:38 +00:00
|
|
|
index = index + 4
|
2019-03-20 03:56:27 +00:00
|
|
|
if int(n.DataSize)+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 1)
|
2015-03-09 07:34:26 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.Data = bytes[index : index+int(n.DataSize)]
|
2012-12-21 10:20:38 +00:00
|
|
|
index = index + int(n.DataSize)
|
|
|
|
n.Flags = bytes[index]
|
|
|
|
index = index + 1
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
if index < lenBytes && n.HasName() {
|
2012-12-21 10:13:02 +00:00
|
|
|
n.NameSize = uint8(bytes[index])
|
|
|
|
index = index + 1
|
2019-03-20 03:56:27 +00:00
|
|
|
if int(n.NameSize)+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 2)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.Name = bytes[index : index+int(n.NameSize)]
|
2012-12-21 10:13:02 +00:00
|
|
|
index = index + int(n.NameSize)
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
if index < lenBytes && n.HasMime() {
|
2012-12-21 10:13:02 +00:00
|
|
|
n.MimeSize = uint8(bytes[index])
|
|
|
|
index = index + 1
|
2019-03-20 03:56:27 +00:00
|
|
|
if int(n.MimeSize)+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 3)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.Mime = bytes[index : index+int(n.MimeSize)]
|
2013-07-09 06:38:38 +00:00
|
|
|
index = index + int(n.MimeSize)
|
|
|
|
}
|
|
|
|
if index < lenBytes && n.HasLastModifiedDate() {
|
2019-03-20 03:56:27 +00:00
|
|
|
if LastModifiedBytesLength+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 4)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
|
2013-07-09 06:38:38 +00:00
|
|
|
index = index + LastModifiedBytesLength
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
if index < lenBytes && n.HasTtl() {
|
2019-03-20 03:56:27 +00:00
|
|
|
if TtlBytesLength+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 5)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
|
2014-09-20 19:38:59 +00:00
|
|
|
index = index + TtlBytesLength
|
|
|
|
}
|
2017-01-08 01:16:29 +00:00
|
|
|
if index < lenBytes && n.HasPairs() {
|
2019-03-20 03:56:27 +00:00
|
|
|
if 2+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 6)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2018-07-24 08:38:08 +00:00
|
|
|
n.PairsSize = util.BytesToUint16(bytes[index : index+2])
|
2017-01-08 01:16:29 +00:00
|
|
|
index += 2
|
2019-03-20 03:56:27 +00:00
|
|
|
if int(n.PairsSize)+index > lenBytes {
|
|
|
|
return fmt.Errorf("index out of range %d", 7)
|
2019-03-19 12:34:43 +00:00
|
|
|
}
|
2017-01-08 01:16:29 +00:00
|
|
|
end := index + int(n.PairsSize)
|
|
|
|
n.Pairs = bytes[index:end]
|
|
|
|
index = end
|
|
|
|
}
|
2019-03-19 12:34:43 +00:00
|
|
|
return nil
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
|
2019-04-18 07:18:29 +00:00
|
|
|
func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
|
2012-12-21 06:32:21 +00:00
|
|
|
n = new(Needle)
|
2018-07-24 08:36:04 +00:00
|
|
|
if version == Version1 || version == Version2 || version == Version3 {
|
2019-04-19 07:39:34 +00:00
|
|
|
bytes = make([]byte, NeedleHeaderSize)
|
2013-01-20 11:40:04 +00:00
|
|
|
var count int
|
2014-03-19 06:48:01 +00:00
|
|
|
count, err = r.ReadAt(bytes, offset)
|
2013-01-20 11:40:04 +00:00
|
|
|
if count <= 0 || err != nil {
|
2019-04-18 07:18:29 +00:00
|
|
|
return nil, bytes, 0, err
|
2012-12-21 06:32:21 +00:00
|
|
|
}
|
2015-05-26 07:58:41 +00:00
|
|
|
n.ParseNeedleHeader(bytes)
|
2018-07-24 08:36:04 +00:00
|
|
|
bodyLength = NeedleBodyLength(n.Size, version)
|
2012-12-21 08:36:55 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:36:04 +00:00
|
|
|
func PaddingLength(needleSize uint32, version Version) uint32 {
|
|
|
|
if version == Version3 {
|
|
|
|
// this is same value as version2, but just listed here for clarity
|
2019-04-19 07:39:34 +00:00
|
|
|
return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
|
2018-07-24 08:36:04 +00:00
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
|
2018-07-24 08:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NeedleBodyLength(needleSize uint32, version Version) int64 {
|
|
|
|
if version == Version3 {
|
|
|
|
return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
|
|
|
|
}
|
|
|
|
return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
|
|
|
|
}
|
|
|
|
|
2012-12-21 08:36:55 +00:00
|
|
|
//n should be a needle already read the header
|
|
|
|
//the input stream will read until next file entry
|
2019-04-18 07:18:29 +00:00
|
|
|
func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
|
2019-03-18 06:28:43 +00:00
|
|
|
|
2013-02-06 22:30:44 +00:00
|
|
|
if bodyLength <= 0 {
|
2019-04-18 07:18:29 +00:00
|
|
|
return nil, nil
|
2013-02-06 22:30:44 +00:00
|
|
|
}
|
2019-04-19 02:22:13 +00:00
|
|
|
bytes = make([]byte, bodyLength)
|
|
|
|
if _, err = r.ReadAt(bytes, offset); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.ReadNeedleBodyBytes(bytes, version)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
|
|
|
|
|
|
|
|
if len(needleBody) <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2019-04-19 02:22:13 +00:00
|
|
|
n.Data = needleBody[:n.Size]
|
2012-12-21 08:36:55 +00:00
|
|
|
n.Checksum = NewCRC(n.Data)
|
2018-07-24 08:36:04 +00:00
|
|
|
case Version2, Version3:
|
2019-04-19 02:22:13 +00:00
|
|
|
err = n.readNeedleDataVersion2(needleBody[0:n.Size])
|
2012-12-21 10:13:02 +00:00
|
|
|
n.Checksum = NewCRC(n.Data)
|
2019-03-18 06:28:43 +00:00
|
|
|
|
|
|
|
if version == Version3 {
|
2019-03-19 12:34:43 +00:00
|
|
|
tsOffset := n.Size + NeedleChecksumSize
|
2019-04-19 02:22:13 +00:00
|
|
|
n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
|
2019-03-18 06:28:43 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
default:
|
2019-04-19 02:22:13 +00:00
|
|
|
err = fmt.Errorf("unsupported version %d!", version)
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2012-12-21 06:32:21 +00:00
|
|
|
return
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2012-12-22 10:10:45 +00:00
|
|
|
|
2012-12-22 20:38:09 +00:00
|
|
|
func (n *Needle) IsGzipped() bool {
|
2013-01-20 11:40:04 +00:00
|
|
|
return n.Flags&FlagGzip > 0
|
2012-12-22 20:38:09 +00:00
|
|
|
}
|
|
|
|
func (n *Needle) SetGzipped() {
|
2013-01-20 11:40:04 +00:00
|
|
|
n.Flags = n.Flags | FlagGzip
|
2012-12-22 20:38:09 +00:00
|
|
|
}
|
|
|
|
func (n *Needle) HasName() bool {
|
2013-01-20 11:40:04 +00:00
|
|
|
return n.Flags&FlagHasName > 0
|
2012-12-22 20:38:09 +00:00
|
|
|
}
|
|
|
|
func (n *Needle) SetHasName() {
|
2013-01-20 11:40:04 +00:00
|
|
|
n.Flags = n.Flags | FlagHasName
|
2012-12-22 20:38:09 +00:00
|
|
|
}
|
|
|
|
func (n *Needle) HasMime() bool {
|
2013-01-20 11:40:04 +00:00
|
|
|
return n.Flags&FlagHasMime > 0
|
2012-12-22 10:10:45 +00:00
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
func (n *Needle) SetHasMime() {
|
2013-01-20 11:40:04 +00:00
|
|
|
n.Flags = n.Flags | FlagHasMime
|
2012-12-22 10:10:45 +00:00
|
|
|
}
|
2013-07-09 06:38:38 +00:00
|
|
|
func (n *Needle) HasLastModifiedDate() bool {
|
|
|
|
return n.Flags&FlagHasLastModifiedDate > 0
|
|
|
|
}
|
|
|
|
func (n *Needle) SetHasLastModifiedDate() {
|
|
|
|
n.Flags = n.Flags | FlagHasLastModifiedDate
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
func (n *Needle) HasTtl() bool {
|
|
|
|
return n.Flags&FlagHasTtl > 0
|
|
|
|
}
|
|
|
|
func (n *Needle) SetHasTtl() {
|
|
|
|
n.Flags = n.Flags | FlagHasTtl
|
|
|
|
}
|
2015-11-29 15:49:41 +00:00
|
|
|
|
2015-12-01 12:53:44 +00:00
|
|
|
func (n *Needle) IsChunkedManifest() bool {
|
2015-12-15 06:38:58 +00:00
|
|
|
return n.Flags&FlagIsChunkManifest > 0
|
2015-11-29 15:49:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 06:38:58 +00:00
|
|
|
func (n *Needle) SetIsChunkManifest() {
|
|
|
|
n.Flags = n.Flags | FlagIsChunkManifest
|
2015-11-29 15:49:41 +00:00
|
|
|
}
|
2017-01-08 01:16:29 +00:00
|
|
|
|
|
|
|
func (n *Needle) HasPairs() bool {
|
|
|
|
return n.Flags&FlagHasPairs != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Needle) SetHasPairs() {
|
|
|
|
n.Flags = n.Flags | FlagHasPairs
|
|
|
|
}
|
2019-04-19 04:43:36 +00:00
|
|
|
|
2019-05-28 07:51:01 +00:00
|
|
|
func GetActualSize(size uint32, version Version) int64 {
|
2019-04-19 07:39:34 +00:00
|
|
|
return NeedleHeaderSize + NeedleBodyLength(size, version)
|
2019-04-19 04:43:36 +00:00
|
|
|
}
|