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

unix/kernel_copy.rs: copy_file_range_candidate allows empty output files #114373

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions library/std/src/sys/unix/kernel_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ enum FdMeta {
NoneObtained,
}

#[derive(PartialEq)]
enum FdHandle {
Input,
Output,
}

impl FdMeta {
fn maybe_fifo(&self) -> bool {
match self {
Expand All @@ -114,12 +120,14 @@ impl FdMeta {
}
}

fn copy_file_range_candidate(&self) -> bool {
fn copy_file_range_candidate(&self, f: FdHandle) -> bool {
match self {
// copy_file_range will fail on empty procfs files. `read` can determine whether EOF has been reached
// without extra cost and skip the write, thus there is no benefit in attempting copy_file_range
FdMeta::Metadata(meta) if meta.is_file() && meta.len() > 0 => true,
FdMeta::NoneObtained => true,
FdMeta::Metadata(meta) if f == FdHandle::Input && meta.is_file() && meta.len() > 0 => {
true
}
FdMeta::Metadata(meta) if f == FdHandle::Output && meta.is_file() => true,
_ => false,
}
}
Expand Down Expand Up @@ -197,7 +205,9 @@ impl<R: CopyRead, W: CopyWrite> SpecCopy for Copier<'_, '_, R, W> {
written += flush()?;
let max_write = reader.min_limit();

if input_meta.copy_file_range_candidate() && output_meta.copy_file_range_candidate() {
if input_meta.copy_file_range_candidate(FdHandle::Input)
&& output_meta.copy_file_range_candidate(FdHandle::Output)
{
let result = copy_regular_files(readfd, writefd, max_write);
result.update_take(reader);

Expand Down
Loading