[erlang-questions] file:open with O_EXCL mode

Michael Santos michael.santos@REDACTED
Wed Jan 20 14:36:02 CET 2010

On Wed, Jan 20, 2010 at 02:50:08PM +0300, Dmitry Belayev wrote:
> I have a problem creating unique files on linux in Erlang.
> In current version I cannot be sure that file I am creating is really  
> unique because even if I check it's existense with filelib:is_regular  
> and call file:open(..., [write,...]) some other process (not only from  
> withing Erlang) can create file with the same name. What I need is  
> atomically create file if it does not exist or get error if it exists.
> I found old mail with workaround:
> http://www.erlang.org/pipermail/erlang-questions/2009-February/041654.html
> This solution works fine but I'd like not to spawn external commands.
> So, is there any way to open file with O_EXCL mode from Erlang? Or will  
> it be possible in next releases?

While this doesn't answer your question directly, under Unix you can
guarantee atomicity by creating a directory and then the temp file inside
of the directory. For example:


The example spawns chmod, since Erlang doesn't seem to provide a way of
setting permissions. Instead, you could write to a private directory or
set your umask before starting beam.

More information about the erlang-questions mailing list