Skip to content

Commit

Permalink
Merge pull request ipfs/go-unixfs#78 from ipfs/fix/file-size
Browse files Browse the repository at this point in the history
fix: correctly handle symlink file sizes

This commit was moved from ipfs/go-unixfs@0faf573
  • Loading branch information
Stebalien authored Sep 26, 2019
2 parents 4dbca3f + 4182ced commit 8b66a95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 10 additions & 5 deletions unixfs/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/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

0 comments on commit 8b66a95

Please sign in to comment.