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

implement better availability probing for copy_file_range #79274

Merged
merged 2 commits into from
Dec 10, 2020

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Nov 21, 2020

Followup to #75428 (comment)

Previously syscall detection was overly pessimistic. Any attempt to copy to an immutable file (EPERM) would disable copy_file_range support for the whole process.

The change tries to copy_file_range on invalid file descriptors which will never run into the immutable file case and thus we can clearly distinguish syscall availability.

@rust-highfive
Copy link
Collaborator

r? @withoutboats

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 21, 2020
@Dylan-DPC-zz
Copy link

r? @nagisa

@rust-highfive rust-highfive assigned nagisa and unassigned withoutboats Dec 8, 2020
Copy link
Member

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

Broadly LGTM. See inline comments for small nits. The constant magic number thing is the only blocker.

// To distinguish these cases we probe with invalid file descriptors which should result in EBADF if the syscall is supported
// and some other error (ENOSYS or EPERM) if it's not available
let available = match unsafe {
cvt(copy_file_range(-1, ptr::null_mut(), -1, ptr::null_mut(), 1, 0))
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a short blurb explaining why -1 is the descriptor we want to use here (and why it is guaranteed to be invalid)? Maybe even extract the magic number into constant? Maybe like this:

/// Invalid file descriptor.
///
/// Valid file descriptors on UNIX are specified to be positive as per e.g. documentation of `open`.
const INVALID_FD: _ = -1;

// and some other error (ENOSYS or EPERM) if it's not available
let available = match unsafe {
cvt(copy_file_range(-1, ptr::null_mut(), -1, ptr::null_mut(), 1, 0))
} {
Copy link
Member

Choose a reason for hiding this comment

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

Style nit: This match can probably be more concisely written as such:

let available = matches!(cvt(...).map_err(|e| e.raw_os_error()), Err(Some(libc::EBADF)))

If you decide you want to continue using match, I would suggest extracting the expression passed to match into a separate variable, because match COMPLEX_EXPRESSION { ... } tends to look pretty bad when auto-formatted.

let result = unsafe { cvt(...) };
let available = match result { ... }

@the8472
Copy link
Member Author

the8472 commented Dec 9, 2020

The review items should be addressed now.

@the8472
Copy link
Member Author

the8472 commented Dec 9, 2020

Yeah, didn't run tests before pushing. One moment.

previously any attempt to copy to an immutable file (EPERM) would disable
copy_file_range support for the whole process.
@nagisa
Copy link
Member

nagisa commented Dec 9, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Dec 9, 2020

📌 Commit 7647d03 has been approved by nagisa

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2020
@bors
Copy link
Contributor

bors commented Dec 10, 2020

⌛ Testing commit 7647d03 with merge e413d89...

@bors
Copy link
Contributor

bors commented Dec 10, 2020

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing e413d89 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 10, 2020
@bors bors merged commit e413d89 into rust-lang:master Dec 10, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants