No tempFile() in std.file

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 16 08:21:24 PDT 2017


On Tuesday, May 16, 2017 08:06:13 H. S. Teoh via Digitalmars-d-learn wrote:
> On Tue, May 16, 2017 at 06:56:57AM -0700, Jonathan M Davis via 
Digitalmars-d-learn wrote:
> > On Tuesday, May 16, 2017 11:19:14 bachmeier via Digitalmars-d-learn 
wrote:
> > > On Monday, 15 May 2017 at 22:38:15 UTC, Jonathan M Davis wrote:
> > > > 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.
>
> [...]
>
> Don't forget that there are security concerns related to this.  Most
> modern OS APIs tend to prefer a temp file creation call that atomically
> (1) generates a unique filename and (2) creates a file with said
> filename with permissions set such that it can only be exclusively used
> by the calling process.
>
> The reason for this is that there is a race condition between the
> generation of the filename and the creation of the file.
[...]

Yes, which is why you use the system call that fails if the file already
exists when you open it. std.stdio.File.scratchFile dealt with all of that
(and AFAIK, did so correctly, though I could have missed something). And
we'd have it in Phobos still if it weren't for the complaints about hello
world's excecutable size. But all of the subtleties around that mess is why
we don't have a std.file.tempFile which simply returns a suggested file
name. You'd _think_ that it would be a simple issue, but unfortunately, it's
not.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list