Skip to content

Commit

Permalink
Merge pull request #10 from jawr/o_excl
Browse files Browse the repository at this point in the history
memfs: Add support for O_EXC to memfs
  • Loading branch information
mcuadros authored Mar 10, 2021
2 parents b791567 + c862faa commit d62fe84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions memfs/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package memfs

import (
"fmt"
"io"
"os"
"testing"

"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d62fe84

Please sign in to comment.