Getting a safe path for a temporary file

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 17 08:55:41 PST 2015


On Saturday, 17 January 2015 at 14:37:00 UTC, Laeeth Isharc wrote:
> On Saturday, 17 January 2015 at 13:47:39 UTC, Marc Schütz wrote:
>> Is it currently possible to get the path to a safe temporary 
>> file, i.e. one that is guaranteed to be freshly created and 
>> will not override another existing file?
>>
>> There's `std.file.tempDir`, which doesn't create a unique 
>> file. Then there's `std.stdio.tmpfile()`, which does, but it 
>> returns a `File` object, and its `name` property is `null`.
>>
>> Did I miss something? IMO this is very import functionality. 
>> One use case is passing these names as command line arguments 
>> to an external program that doesn't support stdin/stdout.
>
> I agree that it would be useful.
>
> This is what I used, although there may be a better option:
>
> http://dlang.org/phobos/std_uuid.html

Nice idea, but it still allows for intentional collision attacks 
:-(

The only really safe solution is one that generates (probably) 
unique names, then opens the file with O_EXCL|O_CREAT (or 
whatever other means the OS provides), and if it fails, retries 
with a different name. `std.stdio.tmpfile()` already does that 
(it uses `tmpfile(3)` under the hood), but doesn't allow access 
to the name.


More information about the Digitalmars-d-learn mailing list