fix: only enforce O_EXCL on MemMapFs OpenFile when O_CREATE is set

Per POSIX, O_EXCL is meaningful only together with O_CREATE. MemMapFs
OpenFile returned ErrFileExists whenever O_EXCL was set on an existing
file, so reopening a file with O_RDWR|O_EXCL failed even though no
creation was requested. Guard the existence check with O_CREATE so that
O_EXCL without O_CREATE opens the existing file, matching os.OpenFile.

Fixes #415
This commit is contained in:
dualfroz committed 2026-09-02 11:37:36 +02:00
1 parent 2ae4e0266a
commit afcb7e66f3
2 files changed
+32 -1

No files matched your search

+3 -1
View File
@@ -263,7 +263,9 @@ 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) {
// O_EXCL is only meaningful together with O_CREATE (POSIX); without
// O_CREATE it must not prevent opening an existing file.
if err == nil && flag&os.O_EXCL > 0 && flag&os.O_CREATE > 0 {
return nil, &os.PathError{Op: "open", Path: name, Err: ErrFileExists}
}
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {