Handle O_CREATE flag to *MemMapFs.OpenFile.

To allow MemMapFs to be used in place of OsFs in tests where this flag
is used to create the file if it doesn't already exist..
This commit is contained in:
Alex Tomlins 2015-08-08 18:50:19 +01:00 committed by spf13
parent 42d35dace7
commit 47e9336de6
2 changed files with 20 additions and 1 deletions

View File

@ -70,6 +70,21 @@ func TestRead0(t *testing.T) {
}
}
func TestOpenFile(t *testing.T) {
for _, fs := range Fss {
path := testDir + "/" + testName
fs.MkdirAll(testDir, 0777) // Just in case.
fs.Remove(path) // Just in case.
f, err := fs.OpenFile(path, os.O_CREATE, 0600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_CREATE) failed:", err)
continue
}
f.Close()
}
}
func TestMemFileRead(t *testing.T) {
f := MemFileCreate("testfile")
f.WriteString("abcd")

View File

@ -205,7 +205,11 @@ func (m *MemMapFs) lockfreeOpen(name string) (File, error) {
}
func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
return m.Open(name)
file, err := m.Open(name)
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
file, err = m.Create(name)
}
return file, err
}
func (m *MemMapFs) Remove(name string) error {