Skip to content

Commit

Permalink
Merge pull request #29 from nikandfor/fix/trunc-append
Browse files Browse the repository at this point in the history
memfs: fix trunc-append
  • Loading branch information
mcuadros authored Feb 5, 2023
2 parents 1b88f62 + 568b9c3 commit f5b6b18
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 f5b6b18

Please sign in to comment.