Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

fix: correctly handle symlink file sizes #78

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down