Fix MemMapFs.Remove() to really delete the file.

It was attempting to delete a file with a hardcoded path of "name" as
opposed to the path in the `name` variable.

Fixing this exposed a deadlock because the function was attempting to
acquire an exclusive lock when it already had a read lock.
This commit is contained in:
Alex Tomlins 2015-08-08 22:47:18 +01:00 committed by spf13
parent 5849bf9936
commit a269144fd6
2 changed files with 21 additions and 5 deletions

View File

@ -114,6 +114,24 @@ func TestRename(t *testing.T) {
} }
} }
func TestRemove(t *testing.T) {
for _, fs := range Fss {
path := testDir + "/" + testName
fs.MkdirAll(testDir, 0777) // Just in case.
fs.Create(path)
err := fs.Remove(path)
if err != nil {
t.Fatalf("%v: Remove() failed: %v", fs.Name(), err)
}
_, err = fs.Stat(path)
if !os.IsNotExist(err) {
t.Errorf("%v: Remove() didn't remove file", fs.Name())
}
}
}
func TestTruncate(t *testing.T) { func TestTruncate(t *testing.T) {
for _, fs := range Fss { for _, fs := range Fss {
f := newFile("TestTruncate", fs, t) f := newFile("TestTruncate", fs, t)

View File

@ -173,13 +173,11 @@ func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, erro
} }
func (m *MemMapFs) Remove(name string) error { func (m *MemMapFs) Remove(name string) error {
m.rlock()
defer m.runlock()
if _, ok := m.getData()["name"]; ok {
m.lock() m.lock()
defer m.unlock()
if _, ok := m.getData()[name]; ok {
delete(m.getData(), name) delete(m.getData(), name)
m.unlock()
} }
return nil return nil
} }