From 78517ac908a2400abb3c94493f1f7465098500c3 Mon Sep 17 00:00:00 2001 From: jawr Date: Mon, 14 Sep 2020 20:45:54 +0900 Subject: [PATCH] Check to see if OpenFile has the O_EXCL flag and return the appropriate error if it does. --- memfs/memory.go | 8 ++++++++ memfs/memory_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/memfs/memory.go b/memfs/memory.go index d4ac056..e471993 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -51,6 +51,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) } @@ -368,6 +372,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 1ec74b8..320b7c4 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" @@ -44,3 +46,16 @@ func (s *MemorySuite) TestNegativeOffsets(c *C) { _, err = f.Write(buf) 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()) +}