Repository: spf13/afero (go)

Issue:
MemMapFs does not respect os.O_EXCL
If `OpenFile` is called with `os.O_CREATE` and a file with the given path already exists, an `ErrFileExists` error should be returned.

While `OsFs` does support this, `MemMapFs` does not fail.

Pull request:
Add support for os.O_EXCL in afero.MemMapFs
Fixes #244 

Patch:
diff --git a/memmap.go b/memmap.go
index 943bad0..b7b1474 100644
--- a/memmap.go
+++ b/memmap.go
@@ -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


Test patch:
diff --git a/memmap_test.go b/memmap_test.go
index 47414ab..a88ea30 100644
--- a/memmap_test.go
+++ b/memmap_test.go
@@ -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"