This commit is contained in:
Jim Schmidt 2024-02-07 10:57:31 -05:00
parent 5c4385aa20
commit cf893f2c67
2 changed files with 23 additions and 1 deletions

View File

@ -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) {

View File

@ -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()
}