Remove FileExists error in MemMapFs.Rename when destination file exists, because os.Rename also doesn't return the error

This commit is contained in:
Martin Bertschler 2016-01-07 11:47:44 +01:00
parent d8bbf358ca
commit 6a67f8a50b
2 changed files with 34 additions and 19 deletions

View File

@ -194,20 +194,39 @@ func TestRename(t *testing.T) {
tDir := testDir(fs)
from := filepath.Join(tDir, "/renamefrom")
to := filepath.Join(tDir, "/renameto")
exists := filepath.Join(tDir, "/renameexists")
file, err := fs.Create(from)
if err != nil {
t.Fatalf("open %q failed: %v", to, err)
t.Fatalf("%s: open %q failed: %v", fs.Name(), to, err)
}
if err = file.Close(); err != nil {
t.Errorf("close %q failed: %v", to, err)
t.Errorf("%s: close %q failed: %v", fs.Name(), to, err)
}
file, err = fs.Create(exists)
if err != nil {
t.Fatalf("%s: open %q failed: %v", fs.Name(), to, err)
}
if err = file.Close(); err != nil {
t.Errorf("%s: close %q failed: %v", fs.Name(), to, err)
}
err = fs.Rename(from, to)
if err != nil {
t.Fatalf("rename %q, %q failed: %v", to, from, err)
t.Fatalf("%s: rename %q, %q failed: %v", fs.Name(), to, from, err)
}
file, err = fs.Create(from)
if err != nil {
t.Fatalf("%s: open %q failed: %v", fs.Name(), to, err)
}
if err = file.Close(); err != nil {
t.Errorf("%s: close %q failed: %v", fs.Name(), to, err)
}
err = fs.Rename(from, exists)
if err != nil {
t.Errorf("%s: rename %q, %q failed: %v", fs.Name(), exists, from, err)
}
names, err := readDirNames(fs, tDir)
if err != nil {
t.Fatalf("readDirNames error: %v", err)
t.Errorf("%s: readDirNames error: %v", fs.Name(), err)
}
found := false
for _, e := range names {
@ -224,7 +243,7 @@ func TestRename(t *testing.T) {
_, err = fs.Stat(to)
if err != nil {
t.Errorf("stat %q failed: %v", to, err)
t.Errorf("%s: stat %q failed: %v", fs.Name(), to, err)
}
}
}

View File

@ -278,7 +278,6 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
m.mu.RLock()
defer m.mu.RUnlock()
if _, ok := m.getData()[oldname]; ok {
if _, ok := m.getData()[newname]; !ok {
m.mu.RUnlock()
m.mu.Lock()
m.unRegisterWithParent(oldname)
@ -289,9 +288,6 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
m.registerWithParent(fileData)
m.mu.Unlock()
m.mu.RLock()
} else {
return &os.PathError{"rename", newname, ErrDestinationExists}
}
} else {
return &os.PathError{"rename", oldname, ErrFileNotFound}
}