diff --git a/go.mod b/go.mod index c890902ec..da2e60b18 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/unixfs.go b/unixfs.go index 84caf6f44..05abf6576 100644 --- a/unixfs.go +++ b/unixfs.go @@ -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 diff --git a/unixfs_test.go b/unixfs_test.go index 79267133d..cf9e8548b 100644 --- a/unixfs_test.go +++ b/unixfs_test.go @@ -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) {