Project

General

Profile

Actions

Feature #20497

closed

Tempfile.create_io

Added by akr (Akira Tanaka) 7 months ago. Updated 7 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117928]

Description

I propose Tempfile.create_io.
It is similar to Tempfile.create but the actual file is unlinked before returning the method.

https://github.com/ruby/ruby/pull/10803

Purpose:

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.

Example:

tmpio = Tempfile.create_io  # => #<IO:fd 5>
tmpio.class                 # => IO
tmpio.path                  # => nil
tmpio.stat.mode.to_s(8)     # => "100600"
tmpio.puts "foo"
tmpio.rewind
tmpio.read                  # => "foo\n"
tmpio.close

Portability:
This feature (unlink just after file creation) is supported on Unix for a long time.

Linux 3.11 has O_TMPFILE to create an unnamed file.
The current implementation uses it.

I heard that it is possible on Windows. (O_SHARE_DELETE?)
I'm not sure how to use it.

Updated by matz (Yukihiro Matsumoto) 7 months ago

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.

Matz.

Updated by shyouhei (Shyouhei Urabe) 7 months ago

Previous discussions:

Because the proposed Tempfile.create_io returns a non-File IO instance, there is no way to assign a path in this ticket.

Updated by akr (Akira Tanaka) 7 months ago

matz (Yukihiro Matsumoto) wrote in #note-1:

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"
}

Updated by akr (Akira Tanaka) 7 months ago

Several candidates for the keyword argument:

  • unlink_first: true

  • remove_immediately: true

  • delete_on_creation: true

  • unnamed: true

  • nameless: true

  • anonymous: true

  • named: false

Updated by byroot (Jean Boussier) 7 months ago

My personal preference would go to anonymous: true, probably followed by linked: false.

Updated by austin (Austin Ziegler) 7 months ago

I have a minor preference for unnamed: true, but anonymous: true is probably more approachable overall.

Updated by akr (Akira Tanaka) 7 months ago

I talked with Matz today.
He said anonymous: true is acceptable.

Actions #8

Updated by akr (Akira Tanaka) 7 months ago

  • Status changed from Open to Closed

Applied in changeset git|3ee83c73c38070d695537d4322ce4decb970a54a.


Tempfile.create(anonymous: true) implemented. (#10803)

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.

[Feature #20497]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0