Skip to content

Commit 41b8001

Browse files
committed
Adds write_all
1 parent 4da165d commit 41b8001

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/errno/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use core::ffi::c_int;
22
use core::num::NonZero;
33

4+
pub const EINTR: Errno = unsafe { Errno(NonZero::new_unchecked(4)) };
5+
pub const EIO: Errno = unsafe { Errno(NonZero::new_unchecked(5)) };
6+
47
/// Encapsulates an errno value.
5-
#[derive(Debug, Clone, Copy)]
8+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69
pub struct Errno(NonZero<c_int>);
710

811
impl Errno {

src/fd/mod.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::errno::Errno;
1+
use crate::errno::{Errno, EINTR, EIO};
22
use crate::pcpu::Pcpu;
33
use crate::thread::Thread;
44
use crate::uio::{IoVec, Uio, UioRw, UioSeg};
@@ -11,6 +11,7 @@ pub const AT_FDCWD: c_int = -100;
1111

1212
/// # Safety
1313
/// `path` cannot be null and must point to a null-terminated string if `seg` is [`UioSeg::Kernel`].
14+
#[inline(never)]
1415
pub unsafe fn openat<K: Kernel>(
1516
kern: K,
1617
fd: c_int,
@@ -33,20 +34,46 @@ pub unsafe fn openat<K: Kernel>(
3334
}
3435

3536
/// # Safety
36-
/// - `buf` cannot be null and must be valid up to `len` if `seg` is [`UioSeg::Kernel`].
37-
/// - `td` cannot be null.
37+
/// `td` cannot be null.
38+
#[inline(never)]
39+
pub unsafe fn write_all<K: Kernel>(
40+
kern: K,
41+
fd: c_int,
42+
mut data: &[u8],
43+
seg: UioSeg,
44+
td: *mut K::Thread,
45+
) -> Result<(), Errno> {
46+
while !data.is_empty() {
47+
let written = match write(kern, fd, data, seg, td) {
48+
Ok(v) => v,
49+
Err(EINTR) => continue,
50+
Err(e) => return Err(e),
51+
};
52+
53+
if written == 0 {
54+
return Err(EIO);
55+
}
56+
57+
data = &data[written..];
58+
}
59+
60+
Ok(())
61+
}
62+
63+
/// # Safety
64+
/// `td` cannot be null.
65+
#[inline(never)]
3866
pub unsafe fn write<K: Kernel>(
3967
kern: K,
4068
fd: c_int,
41-
buf: *const u8,
69+
data: &[u8],
4270
seg: UioSeg,
43-
len: usize,
4471
td: *mut K::Thread,
4572
) -> Result<usize, Errno> {
4673
// Setup iovec.
4774
let mut vec = IoVec {
48-
ptr: buf.cast_mut(),
49-
len,
75+
ptr: data.as_ptr().cast_mut(),
76+
len: data.len(),
5077
};
5178

5279
// Write.
@@ -92,6 +119,7 @@ impl<K: Kernel> OwnedFd<K> {
92119
}
93120

94121
impl<K: Kernel> Drop for OwnedFd<K> {
122+
#[inline(never)]
95123
fn drop(&mut self) {
96124
// This drop must be called from the same process as the one that created the FD.
97125
assert_eq!(

0 commit comments

Comments
 (0)