diff --git a/memmap.go b/memmap.go index d6c744e..0797f9b 100644 --- a/memmap.go +++ b/memmap.go @@ -245,7 +245,7 @@ func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, erro perm &= chmodBits chmod := false file, err := m.openWrite(name) - if err == nil && (flag&os.O_EXCL > 0) { + if err == nil && (flag&(os.O_CREATE|os.O_EXCL)) == (os.O_CREATE|os.O_EXCL) { return nil, &os.PathError{Op: "open", Path: name, Err: ErrFileExists} } if os.IsNotExist(err) && (flag&os.O_CREATE > 0) { diff --git a/memmap_test.go b/memmap_test.go index c47fadc..1e93ddf 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -918,3 +918,25 @@ func TestMemMapFsRename(t *testing.T) { } } } + +func TestMemMapCreateThenOpen(t *testing.T) { + fs := NewMemMapFs() + filePath := "/test/data.txt" + err := fs.MkdirAll("/test", 0744) + if err != nil { + t.Fatal(err) + } + f1, err := fs.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0644) + if err != nil { + t.Fatal(err) + } + err = f1.Close() + if err != nil { + t.Fatal(err) + } + f2, err := fs.OpenFile(filePath, os.O_RDWR|os.O_EXCL, 0644) + if err != nil { + t.Fatal(err) + } + f2.Close() +}