Jump to page: 1 2
Thread overview
[Issue 13996] Function for returning a temporary file with a randomly generated name where the name can be accessed
Feb 04, 2015
Martin Nowak
Feb 04, 2015
Jonathan M Davis
Feb 04, 2015
Jonathan M Davis
Feb 04, 2015
Martin Nowak
Feb 04, 2015
Martin Nowak
Feb 04, 2015
Martin Nowak
Feb 04, 2015
Jonathan M Davis
Apr 05, 2015
Martin Nowak
Jul 26, 2015
Timothee Cour
Jul 26, 2015
Jonathan M Davis
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

Martin Nowak <code@dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@dawg.eu

--- Comment #1 from Martin Nowak <code@dawg.eu> ---
You forgot suffix argument :).

It's missing for ages, we just needed it for dub https://github.com/D-Programming-Language/dub/pull/497#issuecomment-72763326.

The python implementation takes a reasonable approach, generate random names and try to exclusively open the file.

https://github.com/python/cpython/blob/1196330dedbe741f8a0c418abcd2956fad29837f/Lib/tempfile.py#L191

The new function could be called namedTempFile. It's still useful to have a tempFile function that directly unlink the file.

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #2 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
(In reply to Martin Nowak from comment #1)
> You forgot suffix argument :).

What suffix? As suggested, it takes an optional prefix and randomly generates the rest. You mean that it should take an extension? Or do you think that it should take a suffix rather than a prefix? I suppose that if it takes a suffix rather than a prefix, the extension could just be part of the suffix.

> It's missing for ages, we just needed it for dub https://github.com/D-Programming-Language/dub/pull/497#issuecomment-72763326.
> 
> The python implementation takes a reasonable approach, generate random names and try to exclusively open the file.
> 
> https://github.com/python/cpython/blob/ 1196330dedbe741f8a0c418abcd2956fad29837f/Lib/tempfile.py#L191
> 
> The new function could be called namedTempFile. It's still useful to have a tempFile function that directly unlink the file.

I have a working implementation for Linux. The problem is that I can't figure out how to get _wsopen or any of its relatives to work on Windows. The linker fails to find them, even when I verify that they're in snn.lib (or whatever the name of the library is with the stdio stuff in it that comes with dmc). So, I'm not quite sure what to do. I could easily create a PR, but it's kind of pointless if it doesn't work on Windows.

As for keeping tmpfile, I suppose that we could - to avoid forcing anyone to change their code if nothing else - but in my experience, the function is utterly useless. And replacing it with tempFile would be trivial. instead of

auto file

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #3 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
Drat. I managed to post by accident. In any case, tmpfile could easily be replaced such that

auto file = File.tmpfile();

became

auto file = File.tempFile();
scope(exit) std.file.remove(file.name);

so IMHO tmpfile becomes redundant on top of being pretty useless. But it _does_ mean deprecating it if we're getting rid of it, so it might be better to just keep it around rather than dealing with that.

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #4 from Martin Nowak <code@dawg.eu> ---
(In reply to Jonathan M Davis from comment #3)
> auto file = File.tempFile();
> scope(exit) std.file.remove(file.name);

There is a security aspect, because the current tempfile is unlinked at the
moment it's created. So no other process can access it.
But as that's the advanced use-case it deserves the longer name, anonTempFile
or so.

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #5 from Martin Nowak <code@dawg.eu> ---
> What suffix? As suggested, it takes an optional prefix and randomly generates the rest. You mean that it should take an extension?

Yes, prefix and suffix, random in between.

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #6 from Martin Nowak <code@dawg.eu> ---
A mkdtemp for directories is missing as well.

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #7 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
Okay. I now have a pull request for this, and I added a suffix parameter to what I had so that now both a prefix and suffix can be set:

https://github.com/D-Programming-Language/phobos/pull/2956

--
February 04, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #8 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/0f389cb0aa5a8f5fd1a36dbdd338f1560d08ee73 Add declaration for _wsopen. It's needed for issue# 13996.

https://github.com/D-Programming-Language/druntime/commit/af9cabcd48a4fef66213ad84e4eaf8959ce36306 Merge pull request #1154 from jmdavis/_wsopen

Add declaration for _wsopen. It's needed for issue# 13996.

--
February 19, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #9 from github-bugzilla@puremagic.com ---
Commits pushed to 2.067 at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/0f389cb0aa5a8f5fd1a36dbdd338f1560d08ee73 Add declaration for _wsopen. It's needed for issue# 13996.

https://github.com/D-Programming-Language/druntime/commit/af9cabcd48a4fef66213ad84e4eaf8959ce36306 Merge pull request #1154 from jmdavis/_wsopen

--
March 31, 2015
https://issues.dlang.org/show_bug.cgi?id=13996

--- Comment #10 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/e3e9f2965f438170157338767700bc1d7292dd67 Implement issue# 13996. Add File.scratchFile.

This adds a File.scratchFile, which generates a random file name and returns an open std.stdio.File for it. Unlike with File.tmpfile, it's a normal file which is _not_ deleted when the file is closed, and you actually have access to the file's name, which is necessary in many situations - particularly when writing unit tests that need to write to a file and then read from it.

https://github.com/D-Programming-Language/phobos/commit/8a65ff8db444f35f6322debbb970166b8c587beb Merge pull request #2956 from jmdavis/tempFile

Implement issue# 13996. Add File.tempFile.

--
« First   ‹ Prev
1 2