[Lazarus] Safe temp file name

Henry Vermaak henry.vermaak at gmail.com
Wed Nov 25 16:43:57 CET 2015


On Wed, Nov 25, 2015 at 05:51:41AM -0800, Aradeonas wrote:
> Hi,
> 
> I usedĀ GetTempFileName in a multi thread system and it causes problems
> as you guess already. So the best way in my mind is using GUID and maybe
> use it in a loop for checking not existing for extra safety. But I want
> to know is there any clean implementation already that I dont know of?

It's pointless to check whether the file exists, since there's a race
between checking whether the file exists and creating/using it.

You need something that creates the file atomically only if it doesn't
exist.  For posix use open(2) with O_CREAT | O_EXCL.  For Windows use
CreateFile() with CREATE_NEW.  When this fails, try again with another
file name.

Henry




More information about the Lazarus mailing list