2012-12-21 13:57:03 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-12-21 14:32:21 +08:00
|
|
|
"errors"
|
2012-12-21 13:57:03 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"pkg/util"
|
|
|
|
)
|
|
|
|
|
2012-12-21 16:36:55 +08:00
|
|
|
func (n *Needle) Append(w io.Writer, version Version) uint32 {
|
|
|
|
if version == Version1 {
|
|
|
|
header := make([]byte, NeedleHeaderSize)
|
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
|
|
|
n.Size = uint32(len(n.Data))
|
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
|
|
|
w.Write(header)
|
|
|
|
w.Write(n.Data)
|
2012-12-23 04:38:09 +08:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
|
|
|
w.Write(header[0 : NeedleChecksumSize+padding])
|
2012-12-21 18:20:38 +08:00
|
|
|
return n.Size
|
2012-12-21 16:36:55 +08:00
|
|
|
} else if version == Version2 {
|
2012-12-21 18:13:02 +08:00
|
|
|
header := make([]byte, NeedleHeaderSize)
|
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
|
|
|
w.Write(header)
|
|
|
|
if n.DataSize > 0 {
|
|
|
|
util.Uint32toBytes(header[0:4], n.DataSize)
|
|
|
|
w.Write(header[0:4])
|
|
|
|
w.Write(n.Data)
|
|
|
|
util.Uint8toBytes(header[0:1], n.Flags)
|
|
|
|
w.Write(header[0:1])
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
if n.HasName() {
|
2012-12-21 18:13:02 +08:00
|
|
|
util.Uint8toBytes(header[0:1], n.NameSize)
|
|
|
|
w.Write(header[0:1])
|
|
|
|
w.Write(n.Name)
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
if n.HasMime() {
|
2012-12-21 18:13:02 +08:00
|
|
|
util.Uint8toBytes(header[0:1], n.MimeSize)
|
|
|
|
w.Write(header[0:1])
|
|
|
|
w.Write(n.Mime)
|
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
|
|
|
w.Write(header[0 : NeedleChecksumSize+padding])
|
2012-12-21 18:20:38 +08:00
|
|
|
return n.DataSize
|
2012-12-21 16:36:55 +08:00
|
|
|
}
|
2012-12-21 13:57:03 +08:00
|
|
|
return n.Size
|
|
|
|
}
|
|
|
|
func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
|
|
|
|
if version == Version1 {
|
2012-12-23 04:38:09 +08:00
|
|
|
bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
|
2012-12-21 13:57:03 +08:00
|
|
|
ret, e := r.Read(bytes)
|
2012-12-21 18:13:02 +08:00
|
|
|
n.readNeedleHeader(bytes)
|
|
|
|
n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
|
2012-12-23 04:38:09 +08:00
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
|
2012-12-21 13:57:03 +08:00
|
|
|
if checksum != NewCRC(n.Data).Value() {
|
|
|
|
return 0, errors.New("CRC error! Data On Disk Corrupted!")
|
|
|
|
}
|
|
|
|
return ret, e
|
2012-12-21 14:32:21 +08:00
|
|
|
} else if version == Version2 {
|
2012-12-23 04:38:09 +08:00
|
|
|
bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
|
2012-12-21 18:13:02 +08:00
|
|
|
ret, e := r.Read(bytes)
|
|
|
|
n.readNeedleHeader(bytes)
|
|
|
|
n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
|
2012-12-23 04:38:09 +08:00
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+n.Size : NeedleHeaderSize+n.Size+NeedleChecksumSize])
|
2012-12-21 18:13:02 +08:00
|
|
|
if checksum != NewCRC(n.Data).Value() {
|
|
|
|
return 0, errors.New("CRC error! Data On Disk Corrupted!")
|
|
|
|
}
|
|
|
|
return ret, e
|
2012-12-21 13:57:03 +08:00
|
|
|
}
|
|
|
|
return 0, errors.New("Unsupported Version!")
|
|
|
|
}
|
2012-12-21 18:13:02 +08:00
|
|
|
func (n *Needle) readNeedleHeader(bytes []byte) {
|
|
|
|
n.Cookie = util.BytesToUint32(bytes[0:4])
|
|
|
|
n.Id = util.BytesToUint64(bytes[4:12])
|
|
|
|
n.Size = util.BytesToUint32(bytes[12:NeedleHeaderSize])
|
|
|
|
}
|
|
|
|
func (n *Needle) readNeedleDataVersion2(bytes []byte) {
|
2012-12-21 18:20:38 +08:00
|
|
|
index, lenBytes := 0, len(bytes)
|
|
|
|
if index < lenBytes {
|
|
|
|
n.DataSize = util.BytesToUint32(bytes[index : index+4])
|
|
|
|
index = index + 4
|
|
|
|
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)]
|
|
|
|
}
|
|
|
|
}
|
2012-12-21 16:36:55 +08:00
|
|
|
func ReadNeedleHeader(r *os.File, version Version) (n *Needle, bodyLength uint32) {
|
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)
|
2012-12-21 14:32:21 +08:00
|
|
|
count, e := r.Read(bytes)
|
|
|
|
if count <= 0 || e != nil {
|
|
|
|
return nil, 0
|
|
|
|
}
|
2012-12-21 18:13:02 +08:00
|
|
|
n.readNeedleHeader(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
|
|
|
|
func (n *Needle) ReadNeedleBody(r *os.File, version Version, bodyLength uint32) {
|
|
|
|
if version == Version1 {
|
|
|
|
bytes := make([]byte, bodyLength)
|
|
|
|
r.Read(bytes)
|
|
|
|
n.Data = bytes[:n.Size]
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
2012-12-21 14:32:21 +08:00
|
|
|
} else if version == Version2 {
|
2012-12-21 18:13:02 +08:00
|
|
|
bytes := make([]byte, bodyLength)
|
|
|
|
r.Read(bytes)
|
|
|
|
n.readNeedleDataVersion2(bytes[0:n.Size])
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
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 {
|
|
|
|
return n.Flags&0x01 == 0x01
|
|
|
|
}
|
|
|
|
func (n *Needle) SetGzipped() {
|
|
|
|
n.Flags = n.Flags | 0x01
|
|
|
|
}
|
|
|
|
func (n *Needle) HasName() bool {
|
|
|
|
return n.Flags&0x02 == 0x02
|
|
|
|
}
|
|
|
|
func (n *Needle) SetHasName() {
|
|
|
|
n.Flags = n.Flags | 0x02
|
|
|
|
}
|
|
|
|
func (n *Needle) HasMime() bool {
|
|
|
|
return n.Flags&0x04 == 0x04
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
func (n *Needle) SetHasMime() {
|
|
|
|
n.Flags = n.Flags | 0x04
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|