Skip to content

Commit

Permalink
Merge pull request #5253 from schomatis/fix/dag-truncate/same-size
Browse files Browse the repository at this point in the history
fix truncating when already at the correct size
  • Loading branch information
whyrusleeping authored Jul 18, 2018
2 parents 9bad5fe + e6545a2 commit d74ec0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions mfs/mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,3 +1111,28 @@ func TestFileDescriptors(t *testing.T) {
t.Fatal(err)
}
}

func TestTruncateAtSize(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ds, rt := setupRoot(ctx, t)

dir := rt.GetDirectory()

nd := dag.NodeWithData(ft.FilePBData(nil, 0))
fi, err := NewFile("test", nd, dir, ds)
if err != nil {
t.Fatal(err)
}

fd, err := fi.Open(OpenReadWrite, true)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
_, err = fd.Write([]byte("test"))
if err != nil {
t.Fatal(err)
}
fd.Truncate(4)
}
3 changes: 3 additions & 0 deletions unixfs/mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ func (dm *DagModifier) Truncate(size int64) error {
if err != nil {
return err
}
if size == int64(realSize) {
return nil
}

// Truncate can also be used to expand the file
if size > int64(realSize) {
Expand Down

0 comments on commit d74ec0d

Please sign in to comment.