From cf893f2c67461c8931b97ae70a2228948bd0272f Mon Sep 17 00:00:00 2001 From: Jim Schmidt Date: Wed, 7 Feb 2024 10:57:31 -0500 Subject: [PATCH] Fix https://github.com/spf13/afero/issues/415 --- memmap.go | 2 +- memmap_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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() +}