Skip to content

Tempfile.create_io implemented. - #10803

Merged
akr merged 16 commits into
ruby:masterfrom
akr:tempfile-create-io
Jun 1, 2024
Merged

akr merged 16 commits into
ruby:masterfrom
akr:tempfile-create-io

Conversation

@akr

@akr akr commented May 19, 2024

Copy link
Copy Markdown
Contributor

Tempfile.create_io creates an unnamed file.

It is implemented using Tempfile.create and unlink. It try to use O_TMPFILE available since Linux 3.11.

Tempfile.create_io creates an unnamed file.

It is implemented using Tempfile.create and unlink.
It try to use O_TMPFILE available since Linux 3.11.
@nobu

nobu commented May 19, 2024

Copy link
Copy Markdown
Member

Unfortunately, create-and-unlink can't work on Windows.

@akr

akr commented May 19, 2024

Copy link
Copy Markdown
Contributor Author

Unfortunately, create-and-unlink can't work on Windows.

I heard that it is possible on Windows.
(I talked about this feature with kosaki-san and naruse-san at RubyKaigi 2024 after party.)
I found FILE_SHARE_DELETE.
Is it really impossible?

@nobu

nobu commented May 19, 2024

Copy link
Copy Markdown
Member

Hmm, I've forgot it.
File::SHARE_DELETE would be necessary to do it.

@cloudbees-smart-tests

This comment has been minimized.

@akr

akr commented May 20, 2024

Copy link
Copy Markdown
Contributor Author

I added File::SHARE_DELETE. (not tested)

I searched FILE_SHARE_DELETE.
It seems it makes subsequent delete operation succeeds but the actual file is not deleted until the closing.
I think it is acceptable for this feature because a user of Tempfile.create_io doesn't need to remove the file anyway.

I also found FILE_FLAG_DELETE_ON_CLOSE and FILE_ATTRIBUTE_TEMPORARY.
I'm not sure if they are useful here or not.

Comment thread test/test_tempfile.rb Outdated

def test_create_io_removes_file
Dir.mktmpdir {|d|
tmpio = Tempfile.create_io(nil, d)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tmpio = Tempfile.create_io(nil, d)
tmpio = Tempfile.create_io("", d)

Comment thread lib/tempfile.rb Outdated
end
end
if tmpio.nil?
mode |= File::SHARE_DELETE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In w32_wopen, _wopen is used unless binary mode, and it does not have share mode argument.

Suggested change
mode |= File::SHARE_DELETE
mode |= File::SHARE_DELETE | File::BINARY

akr added 3 commits May 20, 2024 17:47
The default value of the 1st argument is "".
nobu's comment:
In w32_wopen, _wopen is used unless binary mode,
and it does not have share mode argument.
@ioquatix

Copy link
Copy Markdown
Member

I believe we used the same feature for unnamed unix sockets on Windows.

akr added 7 commits May 25, 2024 02:14
API changed:
`Tempfile.create_io` method is removed.
`Tempfile.create` method may take `unlink_first: true`.
`Tempfile.create(unlink_first: true)` deletes the created temporary file
just after creation.
`Tempfile.create(unlink_first: true)` returns
a File object which path method returns
the temporary directory with a trailing slash.
private method names renamed.
@akr
akr merged commit 3ee83c7 into ruby:master Jun 1, 2024
koxya added a commit to koxya/doctree that referenced this pull request Dec 21, 2025
Ruby 3.4 added the `anonymous:` keyword to Tempfile.create, so update the reference
manual to reflect the new method signature and document the new parameter.

When `anonymous: true` is given, the temporary file is unlinked immediately after
creation to reduce the chance of being accessed via its pathname (while the
returned File can still be used). The default remains false, so existing behavior
is unchanged.

[#10803(ruby/ruby#10803)
koxya added a commit to koxya/doctree that referenced this pull request Dec 21, 2025
Ruby 3.4 added the `anonymous:` keyword to Tempfile.create, so update the reference
manual to reflect the new method signature and document the new parameter.

When `anonymous: true` is given, the temporary file is unlinked immediately after
creation to reduce the chance of being accessed via its pathname (while the
returned File can still be used). The default remains false, so existing behavior
is unchanged.

[#10803](ruby/ruby#10803)
koxya added a commit to koxya/doctree that referenced this pull request Dec 21, 2025
Ruby 3.4 added the `anonymous:` keyword to Tempfile.create, so update the reference
manual to reflect the new method signature and document the new parameter.

When `anonymous: true` is given, the temporary file is unlinked immediately after
creation to reduce the chance of being accessed via its pathname (while the
returned File can still be used). The default remains false, so existing behavior
is unchanged.

[#10803](ruby/ruby#10803)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants