Allow Passing Flags to openFile function
Motivation
Currently, you can't pass flags (e.g. O_CREAT, O_EXCL, O_WRONLY) to openFile and withFile functions. In some applications, it is necessary to control underlying flags to Operating System's open function. Consider this Python code;
import os
import errno
flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
try:
file_handle = os.open(target, flags)
except OSError as e:
if e.errno == errno.EEXIST: # Failed as the file already exists.
pass
else: # Something unexpected went wrong so reraise the exception.
raise
else: # No exception, so the file must have been created successfully.
with os.fdopen(file_handle, 'w') as file_obj:
# here we are sure of 3 things
# 1) File didn't exist before we try to open it
# 2) We have exclusive access to it
# 3) Checking for existence of file and creating it was an atomic operation, so no race-conditions there.
Proposal
Implement openFile'
and withFile'
functions, and OpenFileFlags
data type, so that you can open files like in the following examples;
f <- openFile' "test.txt" (O_CREAT | O_EXCL | O_WRONLY)
withFile' "test.txt" (O_CREAT | O_EXCL | O_WRONLY) $ \h -> getlines h >>= mapM_ putStrLn