diff --git a/memfs/memory.go b/memfs/memory.go index 6054d22..f217693 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -52,6 +52,10 @@ func (fs *Memory) OpenFile(filename string, flag int, perm os.FileMode) (billy.F return nil, err } } else { + if isExclusive(flag) { + return nil, os.ErrExist + } + if target, isLink := fs.resolveLink(filename, f); isLink { return fs.OpenFile(target, flag, perm) } @@ -377,6 +381,10 @@ func isCreate(flag int) bool { return flag&os.O_CREATE != 0 } +func isExclusive(flag int) bool { + return flag&os.O_EXCL != 0 +} + func isAppend(flag int) bool { return flag&os.O_APPEND != 0 } diff --git a/memfs/memory_test.go b/memfs/memory_test.go index 95edcc9..324daeb 100644 --- a/memfs/memory_test.go +++ b/memfs/memory_test.go @@ -1,7 +1,9 @@ package memfs import ( + "fmt" "io" + "os" "testing" "github.com/go-git/go-billy/v5" @@ -45,6 +47,19 @@ func (s *MemorySuite) TestNegativeOffsets(c *C) { c.Assert(err, ErrorMatches, "writeat negative: negative offset") } + +func (s *MemorySuite) TestExclusive(c *C) { + f, err := s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + c.Assert(err, IsNil) + + fmt.Fprint(f, "mememememe") + + err = f.Close() + c.Assert(err, IsNil) + + _, err = s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + c.Assert(err, ErrorMatches, os.ErrExist.Error()) + func (s *MemorySuite) TestOrder(c *C) { var err error