spf13 / afero

A FileSystem Abstraction System for Go
Apache License 2.0
5.8k stars 498 forks source link

MemMapFs.Mkdir should error if parent path does not exist #149

Open ts2909 opened 6 years ago

ts2909 commented 6 years ago

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.

purpleidea commented 4 years ago

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.

jxsl13 commented 2 years ago

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