scheme-utils: Have FpathWriter handle writing the scheme name

This commit is contained in:
bjorn3
2026-04-19 14:49:11 +02:00
parent 64d23e9e05
commit daf726c30d
24 changed files with 58 additions and 73 deletions
+2 -6
View File
@@ -323,12 +323,8 @@ impl SchemeSync for Ac97 {
}
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
let _handle = self.handles.lock().get_mut(id)?;
w.push_str("/scheme/audiohw");
Ok(())
})
fn fpath(&mut self, _id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, "audiohw", |_| Ok(()))
}
fn on_close(&mut self, id: usize) {
+2 -6
View File
@@ -1076,12 +1076,8 @@ impl SchemeSync for IntelHDA {
}
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
let _handle = self.handles.lock().get_mut(id)?;
w.push_str("/scheme/audiohw");
Ok(())
})
fn fpath(&mut self, _id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, "audiohw", |_| Ok(()))
}
fn on_close(&mut self, id: usize) {
+2 -6
View File
@@ -222,12 +222,8 @@ impl SchemeSync for Sb16 {
Err(Error::new(EBADF))
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
let _handle = self.handles.lock().get_mut(id)?;
w.push_str("/scheme/audiohw");
Ok(())
})
fn fpath(&mut self, _id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, "audiohw", |_| Ok(()))
}
fn on_close(&mut self, id: usize) {
+2 -2
View File
@@ -424,13 +424,13 @@ impl<T: GraphicsAdapter> SchemeSync for GraphicsSchemeInner<T> {
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> syscall::Result<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, &self.scheme_name, |w| {
match self.handles.get(id)? {
Handle::V2 {
vt,
next_id: _,
buffers: _,
} => write!(w, "/scheme/{}/v2/{vt}", self.scheme_name).unwrap(),
} => write!(w, "v2/{vt}").unwrap(),
Handle::SchemeRoot => return Err(Error::new(EOPNOTSUPP)),
};
Ok(())
+1 -4
View File
@@ -197,10 +197,7 @@ impl SchemeSync for FbbootlogScheme {
}
fn fpath(&mut self, _id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("fbbootlog:");
Ok(())
})
FpathWriter::with_legacy(buf, "fbbootlog", |_| Ok(()))
}
fn fsync(&mut self, _id: usize, _ctx: &CallerCtx) -> Result<()> {
+2 -2
View File
@@ -125,9 +125,9 @@ impl SchemeSync for FbconScheme {
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with_legacy(buf, "fbcon", |w| {
let handle = self.get_vt_handle_mut(id)?;
write!(w, "fbcon:{}", handle.vt_i.0).unwrap();
write!(w, "{}", handle.vt_i.0).unwrap();
Ok(())
})
}
+3 -3
View File
@@ -269,12 +269,12 @@ impl SchemeSync for InputScheme {
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> syscall::Result<usize> {
FpathWriter::with(buf, |w| {
let display = self.display.as_ref().ok_or(SysError::new(EINVAL))?;
FpathWriter::with(buf, display, |w| {
let handle = self.handles.get(id)?;
if let Handle::Consumer { vt, .. } = handle {
let display = self.display.as_ref().ok_or(SysError::new(EINVAL))?;
write!(w, "/scheme/{}/{vt}", display).unwrap();
write!(w, "{vt}").unwrap();
Ok(())
} else {
Err(SysError::new(EINVAL))
+1 -2
View File
@@ -315,8 +315,7 @@ impl<T: NetworkAdapter> SchemeSync for NetworkSchemeInner<T> {
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
write!(w, "/scheme/{}/", self.scheme_name).unwrap();
FpathWriter::with(buf, &self.scheme_name, |w| {
let path = match self.handles.get(id)? {
Handle::Data { .. } => "",
Handle::Mac { .. } => "mac",
+1 -2
View File
@@ -559,8 +559,7 @@ impl<T: Disk> SchemeAsync for DiskSchemeInner<T> {
}
async fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
write!(w, "{}:", self.scheme_name).unwrap();
FpathWriter::with_legacy(buf, &self.scheme_name, |w| {
match *self.handles.get(id)? {
Handle::List(_) => (),
Handle::Disk(number) => {
+3 -3
View File
@@ -198,7 +198,7 @@ impl Handle {
///
/// # Returns
/// - A [String] containing the scheme path that the handle is associated with.
pub(crate) fn to_scheme(&self) -> String {
pub(crate) fn to_path(&self) -> String {
match self {
Handle::TopLevel(_) => String::from(""),
Handle::Port(port_num, _) => {
@@ -2214,9 +2214,9 @@ impl<const N: usize> SchemeSync for &Xhci<N> {
}
fn fpath(&mut self, fd: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, "xhci", |w| {
let handle = self.handles.get(&fd).ok_or(Error::new(EBADF))?;
write!(w, "{}", handle.to_scheme()).unwrap();
write!(w, "{}", handle.to_path()).unwrap();
Ok(())
})
}
+1 -3
View File
@@ -331,9 +331,7 @@ impl<'sock> SchemeSync for ChanScheme<'sock> {
}
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("chan:");
FpathWriter::with_legacy(buf, "chan", |w| {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
if let Extra::SchemeRoot = handle.extra {
return Ok(());
+1 -2
View File
@@ -90,8 +90,7 @@ impl SchemeSync for ShmScheme {
})
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("/scheme/shm/");
FpathWriter::with(buf, "shm", |w| {
w.push_str(self.handles.get(id).and_then(Handle::as_shm)?);
Ok(())
})
+1 -2
View File
@@ -567,8 +567,7 @@ impl<'sock> UdsDgramScheme<'sock> {
}
fn fpath_inner(path: &String, buf: &mut [u8]) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("/scheme/uds_dgram/");
FpathWriter::with(buf, "uds_dgram", |w| {
w.push_str(path);
Ok(())
})
+1 -2
View File
@@ -1229,8 +1229,7 @@ impl<'sock> UdsStreamScheme<'sock> {
}
fn fpath_inner(path: &String, buf: &mut [u8]) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("/scheme/uds_stream/");
FpathWriter::with(buf, "uds_stream", |w| {
w.push_str(path);
Ok(())
})
+1 -2
View File
@@ -229,8 +229,7 @@ impl<'sock> SchemeSync for LogScheme<'sock> {
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("/scheme/log/");
FpathWriter::with(buf, "log", |w| {
w.push_str(match self.handles.get(id)? {
LogHandle::Log { context, .. } => context,
LogHandle::AddSink => "add_sink",
+3 -3
View File
@@ -250,14 +250,14 @@ impl<'a> SchemeSocket for IcmpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, "icmp", |w| {
if let SchemeFile::Socket(ref socket_file) = *file {
match socket_file.data.socket_type {
IcmpSocketType::Echo => {
write!(w, "/scheme/icmp/echo/{}", socket_file.data.ip).unwrap();
write!(w, "echo/{}", socket_file.data.ip).unwrap();
}
IcmpSocketType::Udp => {
write!(w, "/scheme/icmp/udp/{}", socket_file.data.ip).unwrap();
write!(w, "udp/{}", socket_file.data.ip).unwrap();
}
}
Ok(())
+2 -2
View File
@@ -138,8 +138,8 @@ impl<'a> SchemeSocket for RawSocket<'a> {
}
fn fpath(&self, _file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
FpathWriter::with(buf, |w| {
write!(w, "/scheme/ip/{}", self.ip_protocol()).unwrap();
FpathWriter::with(buf, "ip", |w| {
write!(w, "{}", self.ip_protocol()).unwrap();
Ok(())
})
}
+1 -2
View File
@@ -273,9 +273,8 @@ impl<'a> SchemeSocket for TcpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, "tcp", |w| {
let unspecified = "0.0.0.0:0";
write!(w, "/scheme/tcp/").unwrap();
match self.remote_endpoint() {
Some(endpoint) => write!(w, "{}", endpoint).unwrap(),
None => w.push_str(unspecified),
+1 -2
View File
@@ -269,9 +269,8 @@ impl<'a> SchemeSocket for UdpSocket<'a> {
}
fn fpath(&self, file: &SchemeFile<Self>, buf: &mut [u8]) -> SyscallResult<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, "udp", |w| {
let unspecified = "0.0.0.0:0";
w.push_str("/scheme/udp/");
// remote
match file {
+2 -2
View File
@@ -32,8 +32,8 @@ impl Pty {
}
pub fn path(&self, buf: &mut [u8]) -> Result<usize> {
FpathWriter::with(buf, |w| {
write!(w, "/scheme/pty/{}", self.id).unwrap();
FpathWriter::with(buf, "pty", |w| {
write!(w, "{}", self.id).unwrap();
Ok(())
})
}
+6 -6
View File
@@ -1,5 +1,4 @@
use std::convert::{TryFrom, TryInto};
use std::iter;
use std::os::unix::io::AsRawFd;
use std::{mem, str};
@@ -460,7 +459,7 @@ impl SchemeSync for Scheme {
Err(Error::new(ENOSYS))
}
fn fpath(&mut self, fd: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
FpathWriter::with(buf, &self.scheme_name, |w| {
let mut current_inode = match *self.handles.get(fd)? {
Handle::Inode(inode) => inode,
Handle::SchemeRoot => return Err(Error::new(EISDIR)),
@@ -495,10 +494,11 @@ impl SchemeSync for Scheme {
current_info = parent_info;
}
for component in iter::once(self.scheme_name.trim_start_matches('/'))
.chain(chain.iter().copied().rev())
{
write!(w, "/{component}").unwrap();
for (i, component) in chain.iter().copied().rev().enumerate() {
if i != 0 {
w.push_str("/");
}
w.push_str(component);
}
Ok(())
})
+1 -4
View File
@@ -431,10 +431,7 @@ impl SchemeSync for RandScheme {
Ok(EventFlags::EVENT_READ)
}
fn fpath(&mut self, _file: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("/scheme/rand");
Ok(())
})
FpathWriter::with(buf, "rand", |_| Ok(()))
}
fn fstat(&mut self, file: usize, stat: &mut Stat, _ctx: &CallerCtx) -> Result<()> {
+17 -1
View File
@@ -81,8 +81,24 @@ pub struct FpathWriter<'a> {
}
impl<'a> FpathWriter<'a> {
pub fn with(buf: &'a mut [u8], f: impl FnOnce(&mut Self) -> Result<()>) -> Result<usize> {
pub fn with(
buf: &'a mut [u8],
scheme_name: &str,
f: impl FnOnce(&mut Self) -> Result<()>,
) -> Result<usize> {
let mut w = FpathWriter { buf, written: 0 };
write!(w, "/scheme/{scheme_name}/").unwrap();
f(&mut w)?;
Ok(w.written)
}
pub fn with_legacy(
buf: &'a mut [u8],
scheme_name: &str,
f: impl FnOnce(&mut Self) -> Result<()>,
) -> Result<usize> {
let mut w = FpathWriter { buf, written: 0 };
write!(w, "{scheme_name}:").unwrap();
f(&mut w)?;
Ok(w.written)
}
+1 -4
View File
@@ -68,10 +68,7 @@ impl SchemeSync for ZeroScheme {
}
fn fpath(&mut self, _id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, |w| {
w.push_str("zero:");
Ok(())
})
FpathWriter::with(buf, "zero", |_| Ok(()))
}
fn fsync(&mut self, _id: usize, _ctx: &CallerCtx) -> Result<()> {