forked from mirror/afero
Support O_APPEND flag in *MemMapFs.Openfile
To allow opening an existing file in append mode.
This commit is contained in:
parent
47e9336de6
commit
7441922445
20
fs_test.go
20
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()
|
||||
}
|
||||
}
|
||||
|
|
12
memmap.go
12
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 {
|
||||
|
|
Loading…
Reference in New Issue