From 4182cedde6c7165b2028de6d09831394d9a481c6 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 26 Sep 2019 12:00:06 -0700 Subject: [PATCH] fix: correctly handle symlink file sizes The file size of a symlink is the symlink data. This commit was moved from ipfs/go-unixfs@94a9c5d7befb25d85b162cd9b731030f31428865 --- unixfs/unixfs.go | 15 ++++++++++----- unixfs/unixfs_test.go | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/unixfs/unixfs.go b/unixfs/unixfs.go index 84caf6f44..05abf6576 100644 --- a/unixfs/unixfs.go +++ b/unixfs/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/unixfs_test.go b/unixfs/unixfs_test.go index 79267133d..cf9e8548b 100644 --- a/unixfs/unixfs_test.go +++ b/unixfs/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) {