ReadOnlyFs: MkdirAll on existing path should return nil

This commit is contained in:
Malte Poll 2022-05-17 09:06:12 +02:00
parent 100c9a6d7b
commit 371e51f7c3
2 changed files with 28 additions and 0 deletions

View File

@ -88,6 +88,10 @@ func (r *ReadOnlyFs) Mkdir(n string, p os.FileMode) error {
} }
func (r *ReadOnlyFs) MkdirAll(n string, p os.FileMode) error { func (r *ReadOnlyFs) MkdirAll(n string, p os.FileMode) error {
fi, err := r.source.Stat(n)
if err == nil && fi.IsDir() {
return nil
}
return syscall.EPERM return syscall.EPERM
} }

24
readonlyfs_test.go Normal file
View File

@ -0,0 +1,24 @@
package afero
import (
"testing"
)
func TestMkdirAllReadonly(t *testing.T) {
base := &MemMapFs{}
ro := &ReadOnlyFs{source: base}
base.MkdirAll("/home/test", 0o777)
if err := ro.MkdirAll("/home/test", 0o777); err != nil {
t.Errorf("Failed to MkdirAll on existing path in ReadOnlyFs: %s", err)
}
if err := ro.MkdirAll("/home/test/newdir", 0o777); err == nil {
t.Error("Creating new dir with MkdirAll on ReadOnlyFs should fail but returned nil")
}
base.Create("/home/test/file")
if err := ro.MkdirAll("/home/test/file", 0o777); err == nil {
t.Error("Creating new dir with MkdirAll on ReadOnlyFs where a file already exists should fail but returned nil")
}
}