Issue #1494 has been updated by Hongli Lai.


Hi guys. Let me re-explain the issue:

There are times when you just need a large anonymous disk-backed byte buffer. For example, when you're writing a web server and you want to buffer the client's upload data. In cases like this, the filename of the buffer temp file you're using doesn't matter, it just matters that you have a file handle open; you're not going to pass the filename to another process or anything.

POSIX systems allow you to do just this. You create a file, unlink it from the filesystem, but keep the file handle open. The result is a file handle that only you can access - no other processes can access it because there's no filesystem entry anymore. This doesn't work on Microsoft Windows however, because on Windows one may only unlink a file if nobody has it open.

Portable applications that need a disk-backed byte buffer will want to do something like this on POSIX:

1. Create the file.
2. Unlink it. This will always succeed.
3. Use the file handle to do things.
4. When done, close the file handle.

On Windows on the other hand they'd want to do this instead:

1. Create the file.
2. Use the file handle to do things.
3. When done, close the file handle.
4. Unlink the file.

Tempfile can support this in a portable manner by recommending applications to use it as follows:

1. Create the file.
2. Unlink it if possible (i.e. only on POSIX systems).
3. Use the file handle to do things. It doesn't matter whether the previous unlink action succeeded, but the file handle must remain open because the app is going to do some work with it.
4. When done, close the file handle.
5. If the unlink action in #2 failed, then unlink it now.

Making the #unlink call close the file handle is not acceptable; it will totally destroy step #3. When calling #unlink, the programmer does not want to close it; it just wants the filesystem entry to disappear, if platform allows it, but the file handle must remain open.

Part of the problem is that not everybody knows this trick or that POSIX and Windows behave differently. The current Tempfile documentation documents this very sparsely.

My version of Tempfile:
- Supports the aforementioned use case: http://github.com/FooBarWidget/better/blob/master/lib/better/tempfile.rb#L307-327
- Has extensive documentation which, among other things, explains the unlink-after-closing trick and all the cross-platform caveats that come with it.
  http://github.com/FooBarWidget/better/blob/master/lib/better/tempfile.rb#L307-327 and
  http://github.com/FooBarWidget/better/blob/master/lib/better/tempfile.rb#L76-91
- Has extensive documentation for everything else as well.
- Fixes a few bugs. These are explained in the 'Comparison' section in the overview documentation of the class.
- Has many unit tests. Ruby's Tempfile currently has almost no unit tests.

Please consider accepting it upstream.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1494

----------------------------------------
http://redmine.ruby-lang.org