From 371e51f7c3bbbfad6a058530273909645dc627f2 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Tue, 17 May 2022 09:06:12 +0200 Subject: [PATCH] ReadOnlyFs: MkdirAll on existing path should return nil --- readonlyfs.go | 4 ++++ readonlyfs_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 readonlyfs_test.go 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") + } +}