diff --git a/readonlyfs.go b/readonlyfs.go index bd8f926..6be06fa 100644 --- a/readonlyfs.go +++ b/readonlyfs.go @@ -88,6 +88,10 @@ func (r *ReadOnlyFs) Mkdir(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 } diff --git a/readonlyfs_test.go b/readonlyfs_test.go new file mode 100644 index 0000000..0977056 --- /dev/null +++ b/readonlyfs_test.go @@ -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") + } +}