ensure correct permissions are set for directories

This PR includes the commit from #6995 and adds testing for this regression. Directory permissions were being corrupted on aufs because of the absence of parent directories in the layer, thus forcing their creation with root ownership and default permissions.

PR #6995 couldn't be merged as it was because it lacked tests and this had to be regression tested.

Thanks to @ljk for looking into this and for sending PR #6995.

Fixes #5892 
Fixes #6722
Closes #6995
I believe this is unreachable code.