Commit cc425aef authored by Jeromy's avatar Jeromy

fixed data size reporting

parent e92bbfbf
...@@ -13,7 +13,7 @@ It has these top-level messages: ...@@ -13,7 +13,7 @@ It has these top-level messages:
*/ */
package merkledag package merkledag
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" import proto "code.google.com/p/goprotobuf/proto"
import math "math" import math "math"
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
...@@ -59,6 +59,7 @@ func (x *PBData_DataType) UnmarshalJSON(data []byte) error { ...@@ -59,6 +59,7 @@ func (x *PBData_DataType) UnmarshalJSON(data []byte) error {
type PBData struct { type PBData struct {
Type *PBData_DataType `protobuf:"varint,1,req,enum=merkledag.PBData_DataType" json:"Type,omitempty"` Type *PBData_DataType `protobuf:"varint,1,req,enum=merkledag.PBData_DataType" json:"Type,omitempty"`
Data []byte `protobuf:"bytes,2,opt" json:"Data,omitempty"` Data []byte `protobuf:"bytes,2,opt" json:"Data,omitempty"`
Filesize *uint64 `protobuf:"varint,3,opt,name=filesize" json:"filesize,omitempty"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
} }
...@@ -80,6 +81,13 @@ func (m *PBData) GetData() []byte { ...@@ -80,6 +81,13 @@ func (m *PBData) GetData() []byte {
return nil return nil
} }
func (m *PBData) GetFilesize() uint64 {
if m != nil && m.Filesize != nil {
return *m.Filesize
}
return 0
}
func init() { func init() {
proto.RegisterEnum("merkledag.PBData_DataType", PBData_DataType_name, PBData_DataType_value) proto.RegisterEnum("merkledag.PBData_DataType", PBData_DataType_name, PBData_DataType_value)
} }
...@@ -9,4 +9,5 @@ message PBData { ...@@ -9,4 +9,5 @@ message PBData {
required DataType Type = 1; required DataType Type = 1;
optional bytes Data = 2; optional bytes Data = 2;
optional uint64 filesize = 3;
} }
package merkledag package merkledag
import ( import (
"errors"
"fmt" "fmt"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
...@@ -107,6 +108,25 @@ func (n *Node) Size() (uint64, error) { ...@@ -107,6 +108,25 @@ func (n *Node) Size() (uint64, error) {
return s, nil return s, nil
} }
func (n *Node) DataSize() (uint64, error) {
pbdata := new(PBData)
err := proto.Unmarshal(n.Data, pbdata)
if err != nil {
return 0, err
}
switch pbdata.GetType() {
case PBData_Directory:
return 0, errors.New("Cant get data size of directory!")
case PBData_File:
return pbdata.GetFilesize(), nil
case PBData_Raw:
return uint64(len(pbdata.GetData())), nil
default:
return 0, errors.New("Unrecognized node data type!")
}
}
// Multihash hashes the encoded data of this node. // Multihash hashes the encoded data of this node.
func (n *Node) Multihash() (mh.Multihash, error) { func (n *Node) Multihash() (mh.Multihash, error) {
b, err := n.Encoded(false) b, err := n.Encoded(false)
...@@ -211,11 +231,12 @@ func (n *DAGService) Get(k u.Key) (*Node, error) { ...@@ -211,11 +231,12 @@ func (n *DAGService) Get(k u.Key) (*Node, error) {
return Decoded(b.Data) return Decoded(b.Data)
} }
func FilePBData(data []byte) []byte { func FilePBData(data []byte, totalsize uint64) []byte {
pbfile := new(PBData) pbfile := new(PBData)
typ := PBData_File typ := PBData_File
pbfile.Type = &typ pbfile.Type = &typ
pbfile.Data = data pbfile.Data = data
pbfile.Filesize = proto.Uint64(totalsize)
data, err := proto.Marshal(pbfile) data, err := proto.Marshal(pbfile)
if err != nil { if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment