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

Commit 0faf573

Browse files
authored
Merge pull request #78 from ipfs/fix/file-size
fix: correctly handle symlink file sizes
2 parents 5567923 + 94a9c5d commit 0faf573

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ require (
1919
github.com/spaolacci/murmur3 v1.1.0
2020
github.com/warpfork/go-wish v0.0.0-20190328234359-8b3e70f8e830 // indirect
2121
)
22+
23+
go 1.12

unixfs.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,16 @@ func DataSize(data []byte) (uint64, error) {
152152
if err != nil {
153153
return 0, err
154154
}
155+
return size(pbdata)
156+
}
155157

158+
func size(pbdata *pb.Data) (uint64, error) {
156159
switch pbdata.GetType() {
157-
case pb.Data_Directory:
160+
case pb.Data_Directory, pb.Data_HAMTShard:
158161
return 0, errors.New("can't get data size of directory")
159162
case pb.Data_File:
160163
return pbdata.GetFilesize(), nil
161-
case pb.Data_Raw:
164+
case pb.Data_Symlink, pb.Data_Raw:
162165
return uint64(len(pbdata.GetData())), nil
163166
default:
164167
return 0, errors.New("unrecognized node data type")
@@ -253,10 +256,12 @@ func (n *FSNode) GetBytes() ([]byte, error) {
253256
return proto.Marshal(&n.format)
254257
}
255258

256-
// FileSize returns the total size of this tree. That is, the size of
257-
// the data in this node plus the size of all its children.
259+
// FileSize returns the size of the file.
258260
func (n *FSNode) FileSize() uint64 {
259-
return n.format.GetFilesize()
261+
// XXX: This needs to be able to return an error when we don't know the
262+
// size.
263+
size, _ := size(&n.format)
264+
return size
260265
}
261266

262267
// NumChildren returns the number of child blocks of this node

unixfs_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,21 @@ func TestPBdataTools(t *testing.T) {
123123
if err != nil {
124124
t.Fatal(err)
125125
}
126+
}
126127

127-
_, sizeErr := DataSize(catSym)
128-
if sizeErr == nil {
129-
t.Fatal("DataSize didn't throw an error when taking the size of a Symlink.")
128+
func TestSymlinkFilesize(t *testing.T) {
129+
path := "/ipfs/adad123123/meowgie.gif"
130+
sym, err := SymlinkData(path)
131+
if err != nil {
132+
t.Fatal(err)
133+
}
134+
size, err := DataSize(sym)
135+
if err != nil {
136+
t.Fatal(err)
137+
}
138+
if int(size) != len(path) {
139+
t.Fatalf("size mismatch: %d != %d", size, len(path))
130140
}
131-
132141
}
133142

134143
func TestMetadata(t *testing.T) {

0 commit comments

Comments
 (0)