Fix error handling
Merge branch 'bugfix_chmod' into upstream-4.0-stable