Re: [PATCH v2 4/8] rust: debugfs: support for binary large objects

Alexandre Courbot
2025-10-22T05:58:40Z

On Tue Oct 21, 2025 at 7:26 AM JST, Danilo Krummrich wrote:
<snip>
> @@ -65,8 +66,8 @@ fn deref(&self) -> &Self::Target {
>
> struct WriterAdapter<T>(T);
>
> -impl<'a, T: Writer> Display for WriterAdapter<&'a T> {
> - fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +impl<'a, T: Writer> fmt::Display for WriterAdapter<&'a T> {
> + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> self.0.write(f)
> }
> }
> @@ -245,3 +246,139 @@ impl<T: Reader + Sync> WriteFile<T> for T {
> unsafe { FileOps::new(operations, 0o200) }
> };
> }
> +
> +extern "C" fn blob_read<T: BinaryWriter>(
> + file: *mut bindings::file,
> + buf: *mut c_char,
> + count: usize,
> + ppos: *mut file::Offset,
> +) -> isize {
> + // SAFETY:
> + // - `file` is a valid pointer to a `struct file`.
> + // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
> + let this = unsafe { &*((*file).private_data.cast::<T>()) };
> +
> + // SAFETY: `ppos` is a valid `file::Offset` pointer.
> + let pos = unsafe { &mut *ppos };
> +
> + let mut writer = UserSlice::new(UserPtr::from_ptr(buf.cast()), count).writer();
> +
> + let ret = || -> Result<isize> {
> + let offset = *pos;
> +
> + let written = this.write_to_slice(&mut writer, offset)?;
> + *pos = offset.saturating_add(file::Offset::try_from(written)?);
> +
> + Ok(written.try_into()?)
> + }();
> +
> + match ret {
> + Ok(n) => n,
> + Err(e) => e.to_errno() as isize,
> + }
> +}
> +
> +pub(crate) trait BinaryReadFile<T> {

I know this trait is crate-local, but having a short documentation for
it would be helpful for first-time readers like myself. :)