Sometimes, applications need a temporary file but it is not required to access via a pathname.
In this case, the created file can be unlinked just after temporary file creation.
This removes the obligation of removing the file from applications.
So, Tempfile.create_io is easier to use than Tempfile.create.
From what I understand, the real intent of this proposal is to "let the OS clean up after the generated tempfile". If so, I don't think the name create_io represents the intent.
I vote for adding a keyword argument to Tempfile.create.
From what I understand, the real intent of this proposal is to "let the OS clean up after the generated tempfile". If so, I don't think the name create_io represents the intent.
I vote for adding a keyword argument to Tempfile.create.
I updated the PR to change the interface:
I added unlink_first keyword argument for Tempfile.create.
f = Tempfile.create(unlink_first: true)
# The file is already removed because unlink_first
f.path # => "/tmp/" (no filename since no file)
f.puts "foo"
f.rewind
f.read # => "foo\n"
f.close
Tempfile.create(unlink_first: true) {|f|
# The file is already removed because unlink_first
f.path # => "/tmp/" (no filename since no file)
f.puts "foo"
f.rewind
f.read # => "foo\n"
}
The keyword argument anonymous is implemented for Tempfile.create
The default is anonymous: false.
The behavior is not changed as before.
The created temporary file is immediately removed if anonymous: true is specified.
So applications don't need to remove the file.
The actual storage of the file is reclaimed by the OS when the file is closed.
It uses O_TMPFILE for Linux 3.11 or later.
It creates an anonymous file from the beginning.
It uses FILE_SHARE_DELETE for Windows.
It makes it possible to remove the opened file.