No tempFile() in std.file

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 15 15:38:15 PDT 2017


On Monday, May 15, 2017 21:52:27 Nordlöw via Digitalmars-d-learn wrote:
> Why isn't there a function, say `tempFile()`, in
>
> https://dlang.org/phobos/std_file.html
>
> that creates a temporary _file_ when we already have
>
> https://dlang.org/phobos/std_file.html#tempDir

We have std.stdio.File.tmpfile, which is kind of terrible, because you can't
get at its name, and it gets deleted as soon as the File is destroyed. And
we briefly had std.stdio.File.scratchFile (IIRC someone didn't like tempFile
for one reason or another, which is what I had named it originally), but it
pulled in enough dependencies (IIRC, beacuse it used tempDir, which is in
std.file, std.datetime got pulled in, because other stuff in std.file uses
SysTime, and std.datetime pulled in yet more...) that hello world grew
considerably in size (since it uses std.stdio and File for writeln), and
there was enough screaming about that that the function was removed.

I don't remember what the proposal was for what we needed to do to fix
having a bunch of Phobos pulled in to make scratchFile work, but until
that's been sorted out, we're kind of stuck, as stupid as that is:

https://issues.dlang.org/show_bug.cgi?id=14599

Personally, I think that it would be very much worth making hello world
larger, since hello world really doesn't matter, but because there are
plenty of folks checking out D who write hello world and then look at the
executable size, it was considered unacceptable for it to get much larger.

Now, we _could_ add a tempFile to std.file which returned a supposedly
unique filename in tempDir, but simply using it would result in a race
condition, because (as unlikely as it may be) someone else could create the
file between the time that you get the file name from tempFile and the time
when you try to open it to write to it - that's why using a solution that
involves opening the file is needed. I suppose that we could add a tempFile
that did what std.stdio.File.scratchFile did but create an empty file and
return its path rather than returning a File, though that would be a bit
annoying, since you'd then have to open it to operate on it instead of just
writing to it. Maybe it would be worth doing though given the stupidity
blocking std.stdio.File.scratchFile.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list