diff --git a/fs_test.go b/fs_test.go index ab667e9..bdbdc7e 100644 --- a/fs_test.go +++ b/fs_test.go @@ -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() } } diff --git a/memmap.go b/memmap.go index 7053fa3..686d0e6 100644 --- a/memmap.go +++ b/memmap.go @@ -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 {