Enable read and write operations of the attribute to handle offset

This commit is contained in:
Chen Chengjun
2025-06-06 07:16:01 +00:00
committed by Tate, Hongliang Tian
parent 3b1248ba7c
commit 1dd1c8c775
4 changed files with 49 additions and 9 deletions

View File

@ -76,6 +76,8 @@ pub enum Error {
PermissionDenied,
/// Other internal error
InternalError(&'static str),
/// Arithmetic overflow occurred
Overflow,
}
impl core::fmt::Display for Error {
@ -88,6 +90,7 @@ impl core::fmt::Display for Error {
Error::AttributeError => write!(f, "Attribute error"),
Error::PermissionDenied => write!(f, "Permission denied for operation"),
Error::InternalError(msg) => write!(f, "Internal error: {}", msg),
Error::Overflow => write!(f, "Numerical overflow occurred"),
}
}
}

View File

@ -118,6 +118,33 @@ pub trait SysNode: SysObj {
/// Writes the value of an attribute.
fn write_attr(&self, name: &str, reader: &mut VmReader) -> Result<usize>;
/// Reads the value of an attribute from the specified `offset`.
fn read_attr_at(&self, name: &str, offset: usize, writer: &mut VmWriter) -> Result<usize> {
let (attr_buffer, attr_len) = {
let attr_buffer_len = writer.avail().checked_add(offset).ok_or(Error::Overflow)?;
let mut buffer = vec![0; attr_buffer_len];
let len = self.read_attr(
name,
&mut VmWriter::from(buffer.as_mut_slice()).to_fallible(),
)?;
(buffer, len)
};
if attr_len <= offset {
return Ok(0);
}
writer
.write_fallible(VmReader::from(attr_buffer.as_slice()).skip(offset))
.map_err(|_| Error::AttributeError)
}
/// Writes the value of an attribute at the specified `offset`.
fn write_attr_at(&self, name: &str, _offset: usize, reader: &mut VmReader) -> Result<usize> {
// In general, the `offset` for attribute write operations is ignored directly.
self.write_attr(name, reader)
}
/// Shows the string value of an attribute.
///
/// Most attributes are textual, rather binary (see `SysAttrFlags::IS_BINARY`).