Skip to content

Commit ecd9879

Browse files
authored
Add a changelog entry for the fcntl_setpipe_size change. (#1165)
Also add another test, and make the names of the private implementation functions match the names of the public function.
1 parent c8fe26b commit ecd9879

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changes from 0.38.x to 1.0
22

3+
`rustix::pipe::fcntl_getpipe_size` now returns the new size, which may be
4+
greater than the requested size.
5+
36
`rustix::thread::FutexOperation` and `rustix::thread::futex` are removed. Use
47
the functions in the `rustix::thread::futex` module instead.
58

src/backend/libc/pipe/syscalls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ pub(crate) fn tee(
112112

113113
#[cfg(linux_kernel)]
114114
#[inline]
115-
pub(crate) fn fcntl_getpipe_sz(fd: BorrowedFd<'_>) -> io::Result<usize> {
115+
pub(crate) fn fcntl_getpipe_size(fd: BorrowedFd<'_>) -> io::Result<usize> {
116116
unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_GETPIPE_SZ)).map(|size| size as usize) }
117117
}
118118

119119
#[cfg(linux_kernel)]
120120
#[inline]
121-
pub(crate) fn fcntl_setpipe_sz(fd: BorrowedFd<'_>, size: usize) -> io::Result<usize> {
121+
pub(crate) fn fcntl_setpipe_size(fd: BorrowedFd<'_>, size: usize) -> io::Result<usize> {
122122
let size: c::c_int = size.try_into().map_err(|_| io::Errno::PERM)?;
123123

124124
unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_SETPIPE_SZ, size)).map(|size| size as usize) }

src/backend/linux_raw/pipe/syscalls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub(crate) fn tee(
9999
}
100100

101101
#[inline]
102-
pub(crate) fn fcntl_getpipe_sz(fd: BorrowedFd<'_>) -> io::Result<usize> {
102+
pub(crate) fn fcntl_getpipe_size(fd: BorrowedFd<'_>) -> io::Result<usize> {
103103
#[cfg(target_pointer_width = "32")]
104104
unsafe {
105105
ret_usize(syscall_readonly!(__NR_fcntl64, fd, c_uint(F_GETPIPE_SZ)))
@@ -111,7 +111,7 @@ pub(crate) fn fcntl_getpipe_sz(fd: BorrowedFd<'_>) -> io::Result<usize> {
111111
}
112112

113113
#[inline]
114-
pub(crate) fn fcntl_setpipe_sz(fd: BorrowedFd<'_>, size: usize) -> io::Result<usize> {
114+
pub(crate) fn fcntl_setpipe_size(fd: BorrowedFd<'_>, size: usize) -> io::Result<usize> {
115115
let size: c::c_int = size.try_into().map_err(|_| io::Errno::PERM)?;
116116

117117
#[cfg(target_pointer_width = "32")]

src/pipe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub fn tee<FdIn: AsFd, FdOut: AsFd>(
204204
#[cfg(linux_kernel)]
205205
#[inline]
206206
pub fn fcntl_getpipe_size<Fd: AsFd>(fd: Fd) -> io::Result<usize> {
207-
backend::pipe::syscalls::fcntl_getpipe_sz(fd.as_fd())
207+
backend::pipe::syscalls::fcntl_getpipe_size(fd.as_fd())
208208
}
209209

210210
/// `fnctl(fd, F_SETPIPE_SZ)`—Set the buffer capacity of a pipe.
@@ -216,5 +216,5 @@ pub fn fcntl_getpipe_size<Fd: AsFd>(fd: Fd) -> io::Result<usize> {
216216
#[cfg(linux_kernel)]
217217
#[inline]
218218
pub fn fcntl_setpipe_size<Fd: AsFd>(fd: Fd, size: usize) -> io::Result<usize> {
219-
backend::pipe::syscalls::fcntl_setpipe_sz(fd.as_fd(), size)
219+
backend::pipe::syscalls::fcntl_setpipe_size(fd.as_fd(), size)
220220
}

tests/pipe/fcntl.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,33 @@ fn test_fcntl_setpipe_size() {
2929
assert_eq!(reader_size, new_size);
3030
assert_eq!(reader_size, writer_size);
3131
}
32+
33+
/// Test that we can write up to the pipe buffer size without blocking.
34+
#[cfg(linux_kernel)]
35+
#[test]
36+
fn test_fcntl_pipe_sized_writes() {
37+
use rustix::io::{read, write};
38+
use rustix::pipe::{fcntl_getpipe_size, fcntl_setpipe_size};
39+
40+
let (reader, writer) = rustix::pipe::pipe().unwrap();
41+
42+
let size = fcntl_getpipe_size(&reader).unwrap();
43+
44+
let ones = vec![1; size];
45+
assert_eq!(write(&writer, &ones), Ok(size));
46+
let mut buf = vec![2; size];
47+
assert_eq!(read(&reader, &mut buf), Ok(size));
48+
assert_eq!(buf, ones);
49+
50+
let size = size * 2;
51+
let set_size = fcntl_setpipe_size(&reader, size).unwrap();
52+
let get_size = fcntl_getpipe_size(&reader).unwrap();
53+
assert_eq!(size, set_size);
54+
assert_eq!(size, get_size);
55+
56+
let ones = vec![1; size];
57+
assert_eq!(write(&writer, &ones), Ok(size));
58+
let mut buf = vec![2; size];
59+
assert_eq!(read(&reader, &mut buf), Ok(size));
60+
assert_eq!(buf, ones);
61+
}

0 commit comments

Comments
 (0)