Skip to content

Commit 6a647f5

Browse files
committed
pipes.rs: don't mix raw write and buffered write
1 parent 055a66b commit 6a647f5

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/uucore/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ parser-num = ["extendedbigdecimal", "num-traits"]
170170
parser-size = ["parser-num", "procfs"]
171171
parser-glob = ["glob"]
172172
parser = ["parser-num", "parser-size", "parser-glob"]
173-
pipes = []
173+
pipes = ["fs"]
174174
process = ["libc"]
175175
proc-info = ["tty", "walkdir"]
176176
quoting-style = ["i18n-common"]

src/uucore/src/lib/features/pipes.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ where
139139
// If the first splice manages to copy to the intermediate
140140
// pipe, but the second splice to stdout fails for some reason
141141
// we can recover by copying the data that we have from the
142-
// intermediate pipe to stdout using normal read/write. Then
142+
// intermediate pipe to stdout using raw read/write syscalls. Then
143143
// we tell the caller to fall back.
144144
debug_assert!(n <= MAX_ROOTLESS_PIPE_SIZE, "unexpected RAM usage");
145-
let mut drain = Vec::with_capacity(n);
146-
pipe_rd.take(n as u64).read_to_end(&mut drain)?;
147-
dest.write_all(&drain)?;
145+
let mut buf = Vec::with_capacity(n);
146+
let buf = buf.spare_capacity_mut();
147+
let (buf, _) = rustix::io::read(pipe_rd, &mut *buf)?;
148+
crate::io::write_all_raw(dest, buf)?;
148149
return Ok(true);
149150
}
150151
}
@@ -210,9 +211,11 @@ pub fn send_n_bytes(
210211
}
211212
} else {
212213
debug_assert!(s <= MAX_ROOTLESS_PIPE_SIZE, "unexpected RAM usage");
213-
let mut drain = Vec::with_capacity(s);
214-
broker_r.take(s as u64).read_to_end(&mut drain)?;
215-
target.write_all(&drain)?;
214+
// drain pipe before fallback to raw write
215+
let mut buf = Vec::with_capacity(s);
216+
let buf = buf.spare_capacity_mut();
217+
let (buf, _) = rustix::io::read(broker_r, &mut *buf)?;
218+
crate::io::write_all_raw(&target, buf)?;
216219
break true;
217220
}
218221
}

0 commit comments

Comments
 (0)