Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for os.O_EXCL in afero.MemMapFs #245

Merged
merged 1 commit into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ func (m *MemMapFs) lockfreeOpen(name string) (*mem.FileData, error) {
func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
chmod := false
file, err := m.openWrite(name)
if err == nil && (flag&os.O_EXCL > 0) {
return nil, &os.PathError{Op: "open", Path: name, Err: ErrFileExists}
}
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
file, err = m.Create(name)
chmod = true
Expand Down
23 changes: 23 additions & 0 deletions memmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ func checkPathError(t *testing.T, err error, op string) {
}
}

// Ensure os.O_EXCL is correctly handled.
func TestOpenFileExcl(t *testing.T) {
const fileName = "/myFileTest"
const fileMode = os.FileMode(0765)

fs := NewMemMapFs()

// First creation should succeed.
f, err := fs.OpenFile(fileName, os.O_CREATE|os.O_EXCL, fileMode)
if err != nil {
t.Errorf("OpenFile Create Excl failed: %s", err)
return
}
f.Close()

// Second creation should fail.
_, err = fs.OpenFile(fileName, os.O_CREATE|os.O_EXCL, fileMode)
if err == nil {
t.Errorf("OpenFile Create Excl should have failed, but it didn't")
}
checkPathError(t, err, "Open")
}

// Ensure Permissions are set on OpenFile/Mkdir/MkdirAll
func TestPermSet(t *testing.T) {
const fileName = "/myFileTest"
Expand Down