Redo error checking logic for stat()

Reimplements 7cca3bc9326ac3a56d1c70af29fc81939ca37b70