forked from mirror/afero
Remove FileExists error in MemMapFs.Rename when destination file exists, because os.Rename also doesn't return the error
This commit is contained in:
parent
d8bbf358ca
commit
6a67f8a50b
|
@ -194,20 +194,39 @@ func TestRename(t *testing.T) {
|
||||||
tDir := testDir(fs)
|
tDir := testDir(fs)
|
||||||
from := filepath.Join(tDir, "/renamefrom")
|
from := filepath.Join(tDir, "/renamefrom")
|
||||||
to := filepath.Join(tDir, "/renameto")
|
to := filepath.Join(tDir, "/renameto")
|
||||||
|
exists := filepath.Join(tDir, "/renameexists")
|
||||||
file, err := fs.Create(from)
|
file, err := fs.Create(from)
|
||||||
if err != nil {
|
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 {
|
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)
|
err = fs.Rename(from, to)
|
||||||
if err != nil {
|
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)
|
names, err := readDirNames(fs, tDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("readDirNames error: %v", err)
|
t.Errorf("%s: readDirNames error: %v", fs.Name(), err)
|
||||||
}
|
}
|
||||||
found := false
|
found := false
|
||||||
for _, e := range names {
|
for _, e := range names {
|
||||||
|
@ -224,7 +243,7 @@ func TestRename(t *testing.T) {
|
||||||
|
|
||||||
_, err = fs.Stat(to)
|
_, err = fs.Stat(to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("stat %q failed: %v", to, err)
|
t.Errorf("%s: stat %q failed: %v", fs.Name(), to, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
24
memmap.go
24
memmap.go
|
@ -278,20 +278,16 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
|
||||||
m.mu.RLock()
|
m.mu.RLock()
|
||||||
defer m.mu.RUnlock()
|
defer m.mu.RUnlock()
|
||||||
if _, ok := m.getData()[oldname]; ok {
|
if _, ok := m.getData()[oldname]; ok {
|
||||||
if _, ok := m.getData()[newname]; !ok {
|
m.mu.RUnlock()
|
||||||
m.mu.RUnlock()
|
m.mu.Lock()
|
||||||
m.mu.Lock()
|
m.unRegisterWithParent(oldname)
|
||||||
m.unRegisterWithParent(oldname)
|
fileData := m.getData()[oldname]
|
||||||
fileData := m.getData()[oldname]
|
delete(m.getData(), oldname)
|
||||||
delete(m.getData(), oldname)
|
mem.ChangeFileName(fileData, newname)
|
||||||
mem.ChangeFileName(fileData, newname)
|
m.getData()[newname] = fileData
|
||||||
m.getData()[newname] = fileData
|
m.registerWithParent(fileData)
|
||||||
m.registerWithParent(fileData)
|
m.mu.Unlock()
|
||||||
m.mu.Unlock()
|
m.mu.RLock()
|
||||||
m.mu.RLock()
|
|
||||||
} else {
|
|
||||||
return &os.PathError{"rename", newname, ErrDestinationExists}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return &os.PathError{"rename", oldname, ErrFileNotFound}
|
return &os.PathError{"rename", oldname, ErrFileNotFound}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue