Take Greg's comment on mkdir into account

This adds a check to see if we need to create a parent directory and returns
the first error we find that is either mkdir generated, or found by stat but not
a "file or directory does not exist" error (which we would've solved by calling
mkdir afterwards).