Skip to content

Commit

Permalink
ci: Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwhite committed Sep 12, 2023
1 parent 9b54803 commit f8622a8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
45 changes: 32 additions & 13 deletions reverie-process/src/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::pin::Pin;
use core::task::Context;
use core::task::Poll;
use std::ffi::CStr;
use std::ffi::CString;
use std::io;
use std::io::Read;
use std::io::Write;
Expand Down Expand Up @@ -378,20 +377,37 @@ pub fn is_dir(path: *const libc::c_char) -> bool {
}
}

fn cstring_as_slice(s: &mut CString) -> &mut [libc::c_char] {
let bytes = s.as_bytes_with_nul();
unsafe {
// This is safe because we are already provided a mutable `CString` and
// we don't alias the two mutable references.
core::slice::from_raw_parts_mut(bytes.as_ptr() as *mut libc::c_char, bytes.len())
/// Copies the bytes of a `CStr` to a buffer. Helpful to avoid allocations when
/// performing path operations in a child process that hasn't called `execve`
/// yet.
fn copy_cstr_to_slice<'a>(
s: &CStr,
buf: &'a mut [libc::c_char],
) -> Result<&'a mut [libc::c_char], Errno> {
let bytes = s.to_bytes_with_nul();

if bytes.len() > buf.len() {
return Err(Errno::ENAMETOOLONG);
}

unsafe {
core::ptr::copy_nonoverlapping(
bytes.as_ptr() as *const libc::c_char,
buf.as_mut_ptr(),
bytes.len(),
)
};

Ok(&mut buf[0..bytes.len()])
}

/// Creates every path component in `path` without allocating. This is done by
/// replacing each `/` with a NUL terminator as needed (and then changing the
/// `\0` back to `/` afterwards).
pub fn create_dir_all(path: &mut CString, mode: libc::mode_t) -> Result<(), Errno> {
create_dir_all_(cstring_as_slice(path), mode)
/// copying the path to a static buffer and replacing each `/` with a NUL
/// terminator as needed (and then changing the `\0` back to `/` afterwards).
pub fn create_dir_all(path: &CStr, mode: libc::mode_t) -> Result<(), Errno> {
let mut buf = ['\0' as libc::c_char; libc::PATH_MAX as usize];
let path = copy_cstr_to_slice(path, &mut buf)?;
create_dir_all_(path, mode)
}

/// Helper function. The last character in the path is always `\0`.
Expand Down Expand Up @@ -431,11 +447,13 @@ fn create_dir_all_(path: &mut [libc::c_char], mode: libc::mode_t) -> Result<(),

/// Creates an empty file at `path` without allocating.
pub fn touch_path(
path: &mut CString,
path: &CStr,
file_mode: libc::mode_t,
dir_mode: libc::mode_t,
) -> Result<(), Errno> {
touch_path_(cstring_as_slice(path), file_mode, dir_mode)
let mut buf = ['\0' as libc::c_char; libc::PATH_MAX as usize];
let path = copy_cstr_to_slice(path, &mut buf)?;
touch_path_(path, file_mode, dir_mode)
}

/// Helper function. The last character in the path is always `\0`.
Expand Down Expand Up @@ -493,6 +511,7 @@ where

#[cfg(test)]
mod tests {
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;

use const_cstr::const_cstr;
Expand Down
5 changes: 2 additions & 3 deletions reverie-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ mod tests {
.trim_end()
.split('\n')
.map(|line| {
let mut items = line.splitn(2, ':');
let first = items.next().unwrap();
let second = items.next().unwrap();
let (first, second) = line.split_once(':').unwrap();
(first, second.trim())
})
.collect()
Expand Down Expand Up @@ -655,6 +653,7 @@ mod tests {
.arg("/proc/self/status")
.seccomp(filter)
.seccomp_notify()
.stdout(Stdio::null())
.spawn()
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions reverie-process/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ impl Mount {
// a different tmpfs.
if let Some(src) = &self.source {
if FileType::new(src.as_ptr())?.is_dir() {
create_dir_all(&mut self.target, 0o777)?;
create_dir_all(&self.target, 0o777)?;
} else {
touch_path(&mut self.target, 0o666, 0o777)?;
touch_path(&self.target, 0o666, 0o777)?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion reverie-process/src/seccomp/notif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl futures::stream::Stream for SeccompNotif {
/// currently pending, the operation blocks until an event occurs.
///
/// NOTE: This is only available since Linux 5.0.
fn seccomp_notif_recv(fd: &mut Fd) -> io::Result<seccomp_notif> {
fn seccomp_notif_recv(fd: &Fd) -> io::Result<seccomp_notif> {
// According to the docs, this struct must be zeroed out first.
let mut response = core::mem::MaybeUninit::<seccomp_notif>::zeroed();

Expand Down

0 comments on commit f8622a8

Please sign in to comment.