From 6a67f8a50b6976a9bc36ac6619ab574abf2c5742 Mon Sep 17 00:00:00 2001 From: Martin Bertschler Date: Thu, 7 Jan 2016 11:47:44 +0100 Subject: [PATCH] Remove FileExists error in MemMapFs.Rename when destination file exists, because os.Rename also doesn't return the error --- afero_test.go | 29 ++++++++++++++++++++++++----- memmap.go | 24 ++++++++++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/afero_test.go b/afero_test.go index 6c9c64b..26e23cd 100644 --- a/afero_test.go +++ b/afero_test.go @@ -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) } } } diff --git a/memmap.go b/memmap.go index 2f2c612..21fbe67 100644 --- a/memmap.go +++ b/memmap.go @@ -278,20 +278,16 @@ 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) - fileData := m.getData()[oldname] - delete(m.getData(), oldname) - mem.ChangeFileName(fileData, newname) - m.getData()[newname] = fileData - m.registerWithParent(fileData) - m.mu.Unlock() - m.mu.RLock() - } else { - return &os.PathError{"rename", newname, ErrDestinationExists} - } + m.mu.RUnlock() + m.mu.Lock() + m.unRegisterWithParent(oldname) + fileData := m.getData()[oldname] + delete(m.getData(), oldname) + mem.ChangeFileName(fileData, newname) + m.getData()[newname] = fileData + m.registerWithParent(fileData) + m.mu.Unlock() + m.mu.RLock() } else { return &os.PathError{"rename", oldname, ErrFileNotFound} }