Skip to content

Commit

Permalink
Remove FileExists error in MemMapFs.Rename when destination file exis…
Browse files Browse the repository at this point in the history
…ts, because os.Rename also doesn't return the error
  • Loading branch information
mbertschler committed Jan 7, 2016
1 parent d8bbf35 commit 6a67f8a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
29 changes: 24 additions & 5 deletions afero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down

0 comments on commit 6a67f8a

Please sign in to comment.