forked from mirror/afero
Return os.ErrExist from CopyOnWriteFs.Mkdir/MkdirAll
Before this commit, `CopyOnWriteFs` would return `syscall.EEXIST` in `Mkdir` and `MkdirAll` when a directory already exists. The main problem with this is that `os.IsExist` returns `false` for that error on Windows. These methods now return `os.ErrExist`, which is in line with how the other file systems behave. Fixes #189
This commit is contained in:
parent
d40851caa0
commit
5e9f8ecaa4
|
@ -267,7 +267,7 @@ func (u *CopyOnWriteFs) Mkdir(name string, perm os.FileMode) error {
|
||||||
return u.layer.MkdirAll(name, perm)
|
return u.layer.MkdirAll(name, perm)
|
||||||
}
|
}
|
||||||
if dir {
|
if dir {
|
||||||
return syscall.EEXIST
|
return ErrFileExists
|
||||||
}
|
}
|
||||||
return u.layer.MkdirAll(name, perm)
|
return u.layer.MkdirAll(name, perm)
|
||||||
}
|
}
|
||||||
|
@ -282,7 +282,7 @@ func (u *CopyOnWriteFs) MkdirAll(name string, perm os.FileMode) error {
|
||||||
return u.layer.MkdirAll(name, perm)
|
return u.layer.MkdirAll(name, perm)
|
||||||
}
|
}
|
||||||
if dir {
|
if dir {
|
||||||
return syscall.EEXIST
|
return ErrFileExists
|
||||||
}
|
}
|
||||||
return u.layer.MkdirAll(name, perm)
|
return u.layer.MkdirAll(name, perm)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,49 @@
|
||||||
package afero
|
package afero
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestCopyOnWrite(t *testing.T) {
|
func TestCopyOnWrite(t *testing.T) {
|
||||||
var fs Fs
|
osFs := NewOsFs()
|
||||||
var err error
|
writeDir, err := TempDir(osFs, "", "copy-on-write-test")
|
||||||
base := NewOsFs()
|
|
||||||
roBase := NewReadOnlyFs(base)
|
|
||||||
ufs := NewCopyOnWriteFs(roBase, NewMemMapFs())
|
|
||||||
|
|
||||||
fs = ufs
|
|
||||||
err = fs.MkdirAll("nonexistent/directory/", 0744)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatal("error creating tempDir", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
_, err = fs.Create("nonexistent/directory/newfile")
|
defer osFs.RemoveAll(writeDir)
|
||||||
|
|
||||||
|
compositeFs := NewCopyOnWriteFs(NewReadOnlyFs(NewOsFs()), osFs)
|
||||||
|
|
||||||
|
var dir = filepath.Join(writeDir, "some/path")
|
||||||
|
|
||||||
|
err = compositeFs.MkdirAll(dir, 0744)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatal(err)
|
||||||
return
|
}
|
||||||
|
_, err = compositeFs.Create(filepath.Join(dir, "newfile"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/spf13/afero/issues/189
|
||||||
|
// We want the composite file system to behave like the OS file system
|
||||||
|
// on Mkdir and MkdirAll
|
||||||
|
for _, fs := range []Fs{osFs, compositeFs} {
|
||||||
|
err = fs.Mkdir(dir, 0744)
|
||||||
|
if err == nil || !os.IsExist(err) {
|
||||||
|
t.Errorf("Mkdir: Got %q for %T", err, fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/spf13/afero/issues/191
|
||||||
|
if _, ok := fs.(*OsFs); !ok {
|
||||||
|
err = fs.MkdirAll(dir, 0744)
|
||||||
|
if err == nil || !os.IsExist(err) {
|
||||||
|
t.Errorf("MkdirAll: Got %q for %T", err, fs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyOnWriteFileInMemMapBase(t *testing.T) {
|
func TestCopyOnWriteFileInMemMapBase(t *testing.T) {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1 +1,3 @@
|
||||||
module github.com/spf13/afero
|
module github.com/spf13/afero
|
||||||
|
|
||||||
|
require golang.org/x/text v0.3.0
|
||||||
|
|
Loading…
Reference in New Issue