Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempfile: Add Tempfile.tempname #5360

Merged
merged 1 commit into from Jan 2, 2018

Conversation

woodruffw
Copy link
Contributor

This new method behaves similarly to the Dir::Tempname.make_tmpname
method in Ruby in that it creates a filename that is not likely
to currently exist on disk.

Some key differences:

  • tempname is fully qualified with the value of Tempfile.dirname,
    while make_tmpname was not fully qualified by default.

  • tempname does not allow the user to specify a custom prefix.

  • tempname does not allow the user to specify a custom trailing
    identifier.

Like make_tmpname in Ruby, this implementation is susceptible to TOCTOU, and does not actually test for the file's existence beforehand -- it counts on the space of Random.rand(0x100000000).to_s(36) being large enough for most purposes.

I'm just learning Crystal, so please let me know if there's anything I can do to improve this PR!

This new method behaves similarly to the `Dir::Tempname.make_tmpname`
method in Ruby in that it creates a filename that is not likely
to currently exist on disk.

Some key differences:

* `tempname` is fully qualified with the value of `Tempfile.dirname`,
while `make_tmpname` was not fully qualified by default.

* `tempname` does not allow the user to specify a custom prefix.

* `tempname` does not allow the user to specify a custom trailing
identifier.
@woodruffw
Copy link
Contributor Author

For consideration: it looks like Ruby is merging make_tmpname into Dir::Tmpname.create in the next release and including a retry-or-fail facility: https://github.com/ruby/ruby/blob/25d56ea7b7b52dc81af30c92a9a0e2d2dab6ff27/lib/tmpdir.rb#L109

@RX14
Copy link
Contributor

RX14 commented Dec 7, 2017

What exactly is the usecase?

@woodruffw
Copy link
Contributor Author

What exactly is the usecase?

Programs that take a temporary filename to create instead of creating one for themselves.

This happens sometimes with programs that open a socket for IPC: they need the user (or invoker) to generate the filename for them, since they don't have a facility for sharing the filename themselves.

An example with MPV:

tmpfile=$(mktemp -u)
mpv --input-ipc-server="${tmpfile}"
whatever-client-program "${tmpfile}"

@RX14 RX14 added this to the Next milestone Jan 2, 2018
@RX14 RX14 merged commit 7cad39c into crystal-lang:master Jan 2, 2018
@woodruffw woodruffw deleted the tempfile-tempname branch January 2, 2018 16:43
lukeasrodgers pushed a commit to lukeasrodgers/crystal that referenced this pull request Jan 7, 2018
This new method behaves similarly to the `Dir::Tempname.make_tmpname`
method in Ruby in that it creates a filename that is not likely
to currently exist on disk.

Some key differences:

* `tempname` is fully qualified with the value of `Tempfile.dirname`,
while `make_tmpname` was not fully qualified by default.

* `tempname` does not allow the user to specify a custom prefix.

* `tempname` does not allow the user to specify a custom trailing
identifier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants