Fix descriptor leaks in writeFile and appendFile

Always release the fd, no matter if we error or succeed.