p9: Remove dependency on sys_util

The read_dir utility was only used by p9. Other code uses fs::read_dir,
so we can move it from base::unix into p9.

BUG=b:229114164
TEST=presubmit

Change-Id: I9bd12b11233582245db8251aeb3d4480170a2d48
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3584628
Reviewed-by: Allen Webb <allenwebb@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Dennis Kempin 2022-04-13 10:30:37 -07:00 committed by Chromeos LUCI
parent d1440454f5
commit a7d91e5f66
5 changed files with 36 additions and 9 deletions

View file

@ -38,7 +38,6 @@ mod poll;
mod priority;
pub mod rand;
mod raw_fd;
pub mod read_dir;
mod sched;
pub mod scoped_signal_handler;
mod shm;

View file

@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
libc = "*"
sys_util = { path = "../sys_util" } # provided by ebuild
wire_format_derive = { path = "wire_format_derive", version = "*" }
[features]

View file

@ -14,3 +14,15 @@ mod server;
pub mod fuzzing;
pub use server::*;
#[macro_export]
macro_rules! syscall {
($e:expr) => {{
let res = $e;
if res < 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(res)
}
}};
}

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
mod read_dir;
use std::cmp::min;
use std::collections::{btree_map, BTreeMap};
use std::ffi::{CStr, CString};
@ -14,9 +16,9 @@ use std::os::unix::fs::FileExt;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::path::Path;
use sys_util::{read_dir::read_dir, syscall};
use crate::protocol::*;
use crate::syscall;
use read_dir::read_dir;
// Tlopen and Tlcreate flags. Taken from "include/net/9p/9p.h" in the linux tree.
const P9_RDONLY: u32 = 0o00000000;

View file

@ -4,8 +4,6 @@
use std::{ffi::CStr, io::Result, mem::size_of, os::unix::io::AsRawFd};
use data_model::DataInit;
use crate::syscall;
#[repr(C, packed)]
@ -16,7 +14,25 @@ struct LinuxDirent64 {
d_reclen: libc::c_ushort,
d_ty: libc::c_uchar,
}
unsafe impl DataInit for LinuxDirent64 {}
impl LinuxDirent64 {
// Note: Taken from data_model::DataInit
fn from_slice(data: &[u8]) -> Option<&Self> {
// Early out to avoid an unneeded `align_to` call.
if data.len() != size_of::<Self>() {
return None;
}
// The `align_to` method ensures that we don't have any unaligned references.
// This aliases a pointer, but because the pointer is from a const slice reference,
// there are no mutable aliases.
// Finally, the reference returned can not outlive data because they have equal implicit
// lifetime constraints.
match unsafe { data.align_to::<Self>() } {
([], [mid], []) => Some(mid),
_ => None,
}
}
}
pub struct DirEntry<'r> {
pub ino: libc::ino64_t,
@ -45,8 +61,7 @@ impl<'d, D: AsRawFd> ReadDir<'d, D> {
self.buf.as_mut_ptr() as *mut LinuxDirent64,
self.buf.len() as libc::c_int,
)
})
.map_err(|e| e.into());
});
match res {
Ok(end) => {
self.current = 0;