Avoid calling lstat() and then creating the file in mkstemp()