Skip to content

Commit 78517ac

Browse files
committed
Check to see if OpenFile has the O_EXCL flag and return the appropriate error if it does.
1 parent d7a8afc commit 78517ac

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

memfs/memory.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func (fs *Memory) OpenFile(filename string, flag int, perm os.FileMode) (billy.F
5151
return nil, err
5252
}
5353
} else {
54+
if isExclusive(flag) {
55+
return nil, os.ErrExist
56+
}
57+
5458
if target, isLink := fs.resolveLink(filename, f); isLink {
5559
return fs.OpenFile(target, flag, perm)
5660
}
@@ -368,6 +372,10 @@ func isCreate(flag int) bool {
368372
return flag&os.O_CREATE != 0
369373
}
370374

375+
func isExclusive(flag int) bool {
376+
return flag&os.O_EXCL != 0
377+
}
378+
371379
func isAppend(flag int) bool {
372380
return flag&os.O_APPEND != 0
373381
}

memfs/memory_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package memfs
22

33
import (
4+
"fmt"
45
"io"
6+
"os"
57
"testing"
68

79
"github.com/go-git/go-billy/v5"
@@ -44,3 +46,16 @@ func (s *MemorySuite) TestNegativeOffsets(c *C) {
4446
_, err = f.Write(buf)
4547
c.Assert(err, ErrorMatches, "writeat negative: negative offset")
4648
}
49+
50+
func (s *MemorySuite) TestExclusive(c *C) {
51+
f, err := s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
52+
c.Assert(err, IsNil)
53+
54+
fmt.Fprint(f, "mememememe")
55+
56+
err = f.Close()
57+
c.Assert(err, IsNil)
58+
59+
_, err = s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
60+
c.Assert(err, ErrorMatches, os.ErrExist.Error())
61+
}

0 commit comments

Comments
 (0)