Unverified Commit 0faf5738 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #78 from ipfs/fix/file-size

fix: correctly handle symlink file sizes
parents 5567923f 94a9c5d7
......@@ -19,3 +19,5 @@ require (
github.com/spaolacci/murmur3 v1.1.0
github.com/warpfork/go-wish v0.0.0-20190328234359-8b3e70f8e830 // indirect
)
go 1.12
......@@ -152,13 +152,16 @@ func DataSize(data []byte) (uint64, error) {
if err != nil {
return 0, err
}
return size(pbdata)
}
func size(pbdata *pb.Data) (uint64, error) {
switch pbdata.GetType() {
case pb.Data_Directory:
case pb.Data_Directory, pb.Data_HAMTShard:
return 0, errors.New("can't get data size of directory")
case pb.Data_File:
return pbdata.GetFilesize(), nil
case pb.Data_Raw:
case pb.Data_Symlink, pb.Data_Raw:
return uint64(len(pbdata.GetData())), nil
default:
return 0, errors.New("unrecognized node data type")
......@@ -253,10 +256,12 @@ func (n *FSNode) GetBytes() ([]byte, error) {
return proto.Marshal(&n.format)
}
// FileSize returns the total size of this tree. That is, the size of
// the data in this node plus the size of all its children.
// FileSize returns the size of the file.
func (n *FSNode) FileSize() uint64 {
return n.format.GetFilesize()
// XXX: This needs to be able to return an error when we don't know the
// size.
size, _ := size(&n.format)
return size
}
// NumChildren returns the number of child blocks of this node
......
......@@ -123,12 +123,21 @@ func TestPBdataTools(t *testing.T) {
if err != nil {
t.Fatal(err)
}
}
_, sizeErr := DataSize(catSym)
if sizeErr == nil {
t.Fatal("DataSize didn't throw an error when taking the size of a Symlink.")
func TestSymlinkFilesize(t *testing.T) {
path := "/ipfs/adad123123/meowgie.gif"
sym, err := SymlinkData(path)
if err != nil {
t.Fatal(err)
}
size, err := DataSize(sym)
if err != nil {
t.Fatal(err)
}
if int(size) != len(path) {
t.Fatalf("size mismatch: %d != %d", size, len(path))
}
}
func TestMetadata(t *testing.T) {
......
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