afero icon indicating copy to clipboard operation
afero copied to clipboard

MemMapFs.Mkdir should error if parent path does not exist

Open ts2909 opened this issue 7 years ago • 2 comments

MemMapFs.Mkdir happily creates the parent path if it is missing.

&MemFs().Mkdir("/tmp/abc/def",0777) should fail, as the parent directories do not yet exist.

ts2909 avatar Nov 30 '17 00:11 ts2909

After about two hours debugging an issue in https://github.com/purpleidea/mgmt/ today, I realized it was in afero, and then after reading the source, I realized that Mkdir isn't behaving correctly. It's easy to see, since MkdirAll behaves the same as Mkdir.

https://github.com/spf13/afero/blob/588a75ec4f32903aa5e39a2619ba6a4631e28424/memmap.go#L149

I realized someone had to have hit this before, so a quick search in the issues found this.

It's quite important that Mkdir doesn't make the directory if the parent isn't present. It's masking an error if it allows this!!!

Maybe someone could plug in some sort of test suite like https://github.com/pjd/pjdfstest ?

In any case, definite +1 on this bug, sorry.

purpleidea avatar Jul 23 '19 07:07 purpleidea

I have got the same behavior. When registering with the parent directory in case that the parent directory is not found (nil) one is simply being created. But what should actually happen is that the MkDir should return a PathError.

Testing with os filesystem: image

jxsl13 avatar Feb 01 '22 10:02 jxsl13