Skip to content

Commit

Permalink
memfs: fix trunc-append case
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jan 8, 2023
1 parent 1b88f62 commit 568b9c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ func (f *file) Duplicate(filename string, mode os.FileMode, flag int) billy.File
flag: flag,
}

if isAppend(flag) {
new.position = int64(new.content.Len())
}

if isTruncate(flag) {
new.content.Truncate()
}

if isAppend(flag) {
new.position = int64(new.content.Len())
}

return new
}

Expand Down
20 changes: 20 additions & 0 deletions memfs/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/test"
"github.com/go-git/go-billy/v5/util"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -83,3 +84,22 @@ func (s *MemorySuite) TestOrder(c *C) {
}
}
}

func (s *MemorySuite) TestTruncateAppend(c *C) {
err := util.WriteFile(s.FS, "truncate_append", []byte("file-content"), 0666)
c.Assert(err, IsNil)

f, err := s.FS.OpenFile("truncate_append", os.O_WRONLY|os.O_TRUNC|os.O_APPEND, 0666)
c.Assert(err, IsNil)

n, err := f.Write([]byte("replace"))
c.Assert(err, IsNil)
c.Assert(n, Equals, len("replace"))

err = f.Close()
c.Assert(err, IsNil)

data, err := util.ReadFile(s.FS, "truncate_append")
c.Assert(err, IsNil)
c.Assert(string(data), Equals, "replace")
}

0 comments on commit 568b9c3

Please sign in to comment.