compatible with Go1
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@46 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"http"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
. "util"
|
||||
"pkg/util"
|
||||
)
|
||||
|
||||
type Needle struct {
|
||||
@@ -21,6 +21,7 @@ type Needle struct {
|
||||
}
|
||||
|
||||
func NewNeedle(r *http.Request) (n *Needle) {
|
||||
|
||||
n = new(Needle)
|
||||
form, fe := r.MultipartReader()
|
||||
if fe != nil {
|
||||
@@ -30,11 +31,11 @@ func NewNeedle(r *http.Request) (n *Needle) {
|
||||
data, _ := ioutil.ReadAll(part)
|
||||
n.Data = data
|
||||
|
||||
commaSep := strings.LastIndex(r.URL.Path, ",")
|
||||
commaSep := strings.LastIndex(r.URL.Path, ",")
|
||||
dotSep := strings.LastIndex(r.URL.Path, ".")
|
||||
fid := r.URL.Path[commaSep+1:]
|
||||
if dotSep > 0 {
|
||||
fid = r.URL.Path[commaSep+1:dotSep]
|
||||
fid = r.URL.Path[commaSep+1 : dotSep]
|
||||
}
|
||||
|
||||
n.ParsePath(fid)
|
||||
@@ -49,51 +50,51 @@ func (n *Needle) ParsePath(fid string) {
|
||||
}
|
||||
n.Key, n.Cookie = ParseKeyHash(fid)
|
||||
}
|
||||
func (n *Needle) Append(w io.Writer) (uint32){
|
||||
func (n *Needle) Append(w io.Writer) uint32 {
|
||||
header := make([]byte, 16)
|
||||
Uint32toBytes(header[0:4], n.Cookie)
|
||||
Uint64toBytes(header[4:12], n.Key)
|
||||
util.Uint32toBytes(header[0:4], n.Cookie)
|
||||
util.Uint64toBytes(header[4:12], n.Key)
|
||||
n.Size = uint32(len(n.Data))
|
||||
Uint32toBytes(header[12:16], n.Size)
|
||||
util.Uint32toBytes(header[12:16], n.Size)
|
||||
w.Write(header)
|
||||
w.Write(n.Data)
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
Uint32toBytes(header[0:4], uint32(n.Checksum))
|
||||
util.Uint32toBytes(header[0:4], uint32(n.Checksum))
|
||||
w.Write(header[0 : rest+4])
|
||||
return n.Size
|
||||
}
|
||||
func (n *Needle) Read(r io.Reader, size uint32)(int, os.Error) {
|
||||
func (n *Needle) Read(r io.Reader, size uint32) (int, error) {
|
||||
bytes := make([]byte, size+16+4)
|
||||
ret, e := r.Read(bytes)
|
||||
n.Cookie = BytesToUint32(bytes[0:4])
|
||||
n.Key = BytesToUint64(bytes[4:12])
|
||||
n.Size = BytesToUint32(bytes[12:16])
|
||||
n.Cookie = util.BytesToUint32(bytes[0:4])
|
||||
n.Key = util.BytesToUint64(bytes[4:12])
|
||||
n.Size = util.BytesToUint32(bytes[12:16])
|
||||
n.Data = bytes[16 : 16+size]
|
||||
n.Checksum = int32(BytesToUint32(bytes[16+size : 16+size+4]))
|
||||
n.Checksum = int32(util.BytesToUint32(bytes[16+size : 16+size+4]))
|
||||
return ret, e
|
||||
}
|
||||
func ReadNeedle(r *os.File) (*Needle, uint32) {
|
||||
n := new(Needle)
|
||||
bytes := make([]byte, 16)
|
||||
count, e := r.Read(bytes)
|
||||
if count<=0 || e!=nil {
|
||||
return nil, 0
|
||||
}
|
||||
n.Cookie = BytesToUint32(bytes[0:4])
|
||||
n.Key = BytesToUint64(bytes[4:12])
|
||||
n.Size = BytesToUint32(bytes[12:16])
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
r.Seek(int64(n.Size+4+rest), 1)
|
||||
return n, 16+n.Size+4+rest
|
||||
n := new(Needle)
|
||||
bytes := make([]byte, 16)
|
||||
count, e := r.Read(bytes)
|
||||
if count <= 0 || e != nil {
|
||||
return nil, 0
|
||||
}
|
||||
n.Cookie = util.BytesToUint32(bytes[0:4])
|
||||
n.Key = util.BytesToUint64(bytes[4:12])
|
||||
n.Size = util.BytesToUint32(bytes[12:16])
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
r.Seek(int64(n.Size+4+rest), 1)
|
||||
return n, 16 + n.Size + 4 + rest
|
||||
}
|
||||
func ParseKeyHash(key_hash_string string)(uint64,uint32) {
|
||||
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
||||
key_hash_len := len(key_hash_bytes)
|
||||
if khe != nil || key_hash_len <= 4 {
|
||||
log.Println("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
||||
return 0, 0
|
||||
}
|
||||
key := BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
||||
hash := BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
||||
return key, hash
|
||||
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
|
||||
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
||||
key_hash_len := len(key_hash_bytes)
|
||||
if khe != nil || key_hash_len <= 4 {
|
||||
log.Println("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
||||
return 0, 0
|
||||
}
|
||||
key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
||||
hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
||||
return key, hash
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user