2012-12-21 05:57:03 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-02-27 06:54:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/util"
|
2012-12-21 06:32:21 +00:00
|
|
|
"errors"
|
2013-01-17 08:22:18 +00:00
|
|
|
"fmt"
|
2012-12-21 05:57:03 +00:00
|
|
|
"io"
|
2013-07-14 02:44:24 +00:00
|
|
|
"log"
|
2012-12-21 05:57:03 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
LastModifiedBytesLength = 5
|
2013-01-20 11:40:04 +00:00
|
|
|
)
|
|
|
|
|
2013-01-21 03:44:23 +00:00
|
|
|
func (n *Needle) DiskSize() uint32 {
|
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
return NeedleHeaderSize + n.Size + padding + NeedleChecksumSize
|
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
func (n *Needle) Append(w io.Writer, version Version) (size uint32, err error) {
|
2013-02-06 22:30:44 +00: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 {
|
2013-07-14 02:44:24 +00:00
|
|
|
log.Printf("Failed to seek %s back to %d with error: %s\n", w, off, e)
|
2013-02-06 22:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s, end)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Cnnot Read Current Volume Position: %s", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-21 08:36:55 +00:00
|
|
|
header := make([]byte, NeedleHeaderSize)
|
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
|
|
|
n.Size = uint32(len(n.Data))
|
2013-01-20 11:40:04 +00:00
|
|
|
size = n.Size
|
2012-12-21 08:36:55 +00:00
|
|
|
util.Uint32toBytes(header[12:16], 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
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2013-01-20 11:40:04 +00:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
|
|
|
return
|
2013-01-17 08:22:18 +00:00
|
|
|
case Version2:
|
2012-12-21 10:13:02 +00: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-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
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
size = n.DataSize
|
2012-12-21 10:13:02 +00:00
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = w.Write(header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
if n.DataSize > 0 {
|
|
|
|
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
|
|
|
|
}
|
2012-12-21 10:13:02 +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() {
|
|
|
|
util.Uint8toBytes(header[0:1], n.NameSize)
|
|
|
|
if _, err = w.Write(header[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write(n.Name); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
}
|
2013-07-12 07:30:45 +00:00
|
|
|
if n.HasMime() {
|
|
|
|
util.Uint8toBytes(header[0:1], n.MimeSize)
|
|
|
|
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() {
|
|
|
|
util.Uint64toBytes(header[0:8], n.LastModified)
|
|
|
|
if _, err = w.Write(header[8-LastModifiedBytesLength : 8]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-09 06:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
|
2013-01-20 11:40:04 +00:00
|
|
|
_, err = w.Write(header[0 : NeedleChecksumSize+padding])
|
|
|
|
return n.DataSize, err
|
2012-12-21 08:36:55 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
return 0, fmt.Errorf("Unsupported Version! (%d)", version)
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2013-01-20 11:40:04 +00:00
|
|
|
|
|
|
|
func (n *Needle) Read(r io.Reader, size uint32, version Version) (ret int, err error) {
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-22 20:38:09 +00:00
|
|
|
bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
|
2013-01-20 11:40:04 +00:00
|
|
|
if ret, err = r.Read(bytes); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
n.readNeedleHeader(bytes)
|
|
|
|
n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
|
2012-12-22 20:38:09 +00:00
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
|
2013-07-12 05:44:59 +00:00
|
|
|
newChecksum := NewCRC(n.Data)
|
|
|
|
if checksum != newChecksum.Value() {
|
2012-12-21 05:57:03 +00:00
|
|
|
return 0, errors.New("CRC error! Data On Disk Corrupted!")
|
|
|
|
}
|
2013-07-12 05:44:59 +00:00
|
|
|
n.Checksum = newChecksum
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2013-01-17 08:22:18 +00:00
|
|
|
case Version2:
|
2013-01-02 23:39:55 +00:00
|
|
|
if size == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2012-12-22 20:38:09 +00:00
|
|
|
bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
|
2013-01-20 11:40:04 +00:00
|
|
|
if ret, err = r.Read(bytes); err != nil {
|
|
|
|
return
|
2013-01-02 23:39:55 +00:00
|
|
|
}
|
|
|
|
if ret != int(NeedleHeaderSize+size+NeedleChecksumSize) {
|
|
|
|
return 0, errors.New("File Entry Not Found!")
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
n.readNeedleHeader(bytes)
|
2013-01-02 23:39:55 +00:00
|
|
|
if n.Size != size {
|
|
|
|
return 0, fmt.Errorf("File Entry Not Found! Needle %d Memory %d", n.Size, size)
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
|
2012-12-22 20:38:09 +00:00
|
|
|
checksum := util.BytesToUint32(bytes[NeedleHeaderSize+n.Size : NeedleHeaderSize+n.Size+NeedleChecksumSize])
|
2013-07-12 05:44:59 +00:00
|
|
|
newChecksum := NewCRC(n.Data)
|
|
|
|
if checksum != newChecksum.Value() {
|
2012-12-21 10:13:02 +00:00
|
|
|
return 0, errors.New("CRC error! Data On Disk Corrupted!")
|
|
|
|
}
|
2013-07-12 05:44:59 +00:00
|
|
|
n.Checksum = newChecksum
|
2013-01-20 11:40:04 +00:00
|
|
|
return
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
return 0, fmt.Errorf("Unsupported Version! (%d)", version)
|
2012-12-21 05:57:03 +00:00
|
|
|
}
|
2012-12-21 10:13:02 +00: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 10:20:38 +00: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-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
|
|
|
|
n.Name = bytes[index : index+int(n.NameSize)]
|
|
|
|
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
|
|
|
|
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() {
|
|
|
|
n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
|
|
|
|
index = index + LastModifiedBytesLength
|
2012-12-21 10:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
|
2013-01-20 11:40:04 +00:00
|
|
|
func ReadNeedleHeader(r *os.File, version Version) (n *Needle, bodyLength uint32, err error) {
|
2012-12-21 06:32:21 +00:00
|
|
|
n = new(Needle)
|
2012-12-21 10:13:02 +00:00
|
|
|
if version == Version1 || version == Version2 {
|
2012-12-21 08:36:55 +00:00
|
|
|
bytes := make([]byte, NeedleHeaderSize)
|
2013-01-20 11:40:04 +00:00
|
|
|
var count int
|
|
|
|
count, err = r.Read(bytes)
|
|
|
|
if count <= 0 || err != nil {
|
|
|
|
return nil, 0, err
|
2012-12-21 06:32:21 +00:00
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
n.readNeedleHeader(bytes)
|
2012-12-22 20:38:09 +00:00
|
|
|
padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
bodyLength = n.Size + NeedleChecksumSize + padding
|
2012-12-21 08:36:55 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//n should be a needle already read the header
|
|
|
|
//the input stream will read until next file entry
|
2013-01-20 11:40:04 +00:00
|
|
|
func (n *Needle) ReadNeedleBody(r *os.File, version Version, bodyLength uint32) (err error) {
|
2013-02-06 22:30:44 +00:00
|
|
|
if bodyLength <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-17 08:22:18 +00:00
|
|
|
switch version {
|
|
|
|
case Version1:
|
2012-12-21 08:36:55 +00:00
|
|
|
bytes := make([]byte, bodyLength)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = r.Read(bytes); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 08:36:55 +00:00
|
|
|
n.Data = bytes[:n.Size]
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
2013-01-17 08:22:18 +00:00
|
|
|
case Version2:
|
2012-12-21 10:13:02 +00:00
|
|
|
bytes := make([]byte, bodyLength)
|
2013-01-20 11:40:04 +00:00
|
|
|
if _, err = r.Read(bytes); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-21 10:13:02 +00:00
|
|
|
n.readNeedleDataVersion2(bytes[0:n.Size])
|
|
|
|
n.Checksum = NewCRC(n.Data)
|
2013-01-20 11:40:04 +00:00
|
|
|
default:
|
|
|
|
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
|
|
|
|
}
|