Support O_APPEND flag in *MemMapFs.Openfile

To allow opening an existing file in append mode.
This commit is contained in:
Alex Tomlins 2015-08-08 18:55:20 +01:00 committed by spf13
parent 47e9336de6
commit 7441922445
2 changed files with 30 additions and 2 deletions

View File

@ -75,12 +75,30 @@ func TestOpenFile(t *testing.T) {
path := testDir + "/" + testName
fs.MkdirAll(testDir, 0777) // Just in case.
fs.Remove(path) // Just in case.
defer fs.Remove(path)
f, err := fs.OpenFile(path, os.O_CREATE, 0600)
f, err := fs.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_CREATE) failed:", err)
continue
}
io.WriteString(f, "initial")
f.Close()
f, err = fs.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_APPEND) failed:", err)
continue
}
io.WriteString(f, "|append")
f.Close()
f, err = fs.OpenFile(path, os.O_RDONLY, 0600)
contents, _ := ioutil.ReadAll(f)
expectedContents := "initial|append"
if string(contents) != expectedContents {
t.Errorf("%v: appending, expected '%v', got: '%v'", fs.Name(), expectedContents, string(contents))
}
f.Close()
}
}

View File

@ -209,7 +209,17 @@ func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, erro
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
file, err = m.Create(name)
}
return file, err
if err != nil {
return nil, err
}
if flag&os.O_APPEND > 0 {
_, err = file.Seek(0, os.SEEK_END)
if err != nil {
file.Close()
return nil, err
}
}
return file, nil
}
func (m *MemMapFs) Remove(name string) error {