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.create_io implemented. #10803

Merged
merged 16 commits into from
Jun 1, 2024
Merged

Tempfile.create_io implemented. #10803

merged 16 commits into from
Jun 1, 2024

Conversation

akr
Copy link
Contributor

@akr akr commented May 19, 2024

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
Copy link
Member

nobu commented May 19, 2024

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

@akr
Copy link
Contributor Author

akr commented May 19, 2024

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
Copy link
Member

nobu commented May 19, 2024

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

This comment has been minimized.

@akr
Copy link
Contributor Author

akr commented May 20, 2024

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.


def test_create_io_removes_file
Dir.mktmpdir {|d|
tmpio = Tempfile.create_io(nil, d)
Copy link
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)

lib/tempfile.rb Outdated
end
end
if tmpio.nil?
mode |= File::SHARE_DELETE
Copy link
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
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
100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants