Snow
Snow

Reputation: 447

Are File::Temp filenames guaranteed to be unique across processes?

I am using File::Temp to create unique temporary files in Perl. If I am running a script that creates temp files in a couple of different processes, is it guaranteed that the filenames will be unique between the processes?

Upvotes: 3

Views: 330

Answers (3)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

File::Temp tries to ensure that it doesn't open an existing file by setting the O_EXCL flag to sysopen, which causes sysopen to fail if the file already exists. However:

O_EXCL may not work on network filesystems...

So on some network filesystems, it's possible that one process could open an existing temporary file created by another process.

There are other problems with File::Temp on NFS, so you should only use it on a local filesystem anyway.

Upvotes: 1

dolmen
dolmen

Reputation: 8706

No, there is no guarantee that the temp files filenames will be unique between the processes.

When you ask for the creation of a temporary file with File::Temp::tempfile, there is at most a garantee that the file did not exist at the creation time. But there is no garantee that a file with that name never existed before.

For example if process A creates a temp file, then closes it and removes it, the filename created later by File::Temp::tempfile in process B may be the same. It is very unlikely but possible. In anycase, you should not consider the tempfile path as a unique id beyond the lifetime of the tempfile.

Upvotes: 3

Toto
Toto

Reputation: 91428

According to the doc, it is safe:

The security aspect of temporary file creation is emphasized such that a filehandle and filename are returned together. This helps guarantee that a race condition can not occur where the temporary file is created by another process between checking for the existence of the file and its opening. Additional security levels are provided to check, for example, that the sticky bit is set on world writable directories. See safe_level for more information.

Upvotes: 2

Related Questions