hyprstream_vfs/fuse_adapter/zerocopy.rs
1//! In-memory `ZeroCopyReader` / `ZeroCopyWriter` adapters.
2//!
3//! `fuse_backend_rs`'s `read`/`write` ops do not hand back a buffer; they copy
4//! through the `ZeroCopyReader` / `ZeroCopyWriter` traits, which in turn move
5//! bytes to/from the backend's real file via [`FileReadWriteVolatile`]. The VFS
6//! `Mount::read`/`write` API, however, is buffer-based (`Vec<u8>` / `&[u8]`).
7//! These two tiny adapters bridge the gap with a heap buffer — no host fd, no
8//! mmap, so they work uniformly over passthrough, overlay and RAFS backends.
9
10use std::cmp::min;
11use std::io::{self, Read, Write};
12
13use fuse_backend_rs::api::filesystem::{ZeroCopyReader, ZeroCopyWriter};
14use fuse_backend_rs::file_buf::FileVolatileSlice;
15use fuse_backend_rs::file_traits::FileReadWriteVolatile;
16
17/// Chunk size for staging copies through a heap buffer.
18const STAGE_CHUNK: usize = 128 * 1024;
19
20// ─────────────────────────────────────────────────────────────────────────────
21// MemWriter — sink for FileSystem::read
22// ─────────────────────────────────────────────────────────────────────────────
23
24/// A [`ZeroCopyWriter`] that accumulates everything written into an owned
25/// `Vec<u8>`. Used as the `w` argument of `FileSystem::read`.
26pub struct MemWriter {
27 buf: Vec<u8>,
28}
29
30impl MemWriter {
31 pub fn with_capacity(cap: usize) -> Self {
32 Self {
33 buf: Vec::with_capacity(cap),
34 }
35 }
36
37 pub fn into_inner(self) -> Vec<u8> {
38 self.buf
39 }
40}
41
42impl Write for MemWriter {
43 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
44 self.buf.extend_from_slice(data);
45 Ok(data.len())
46 }
47
48 fn flush(&mut self) -> io::Result<()> {
49 Ok(())
50 }
51}
52
53impl ZeroCopyWriter for MemWriter {
54 /// Copy up to `count` bytes out of the backend file `f` (starting at `off`)
55 /// into our buffer, staging through a heap chunk.
56 fn write_from(
57 &mut self,
58 f: &mut dyn FileReadWriteVolatile,
59 count: usize,
60 off: u64,
61 ) -> io::Result<usize> {
62 let mut stage = vec![0u8; min(count, STAGE_CHUNK)];
63 // SAFETY: `stage` is a live, exclusively-borrowed heap buffer for the
64 // duration of the volatile read; no other accessor exists.
65 let slice = unsafe { FileVolatileSlice::from_mut_slice(&mut stage) };
66 let n = f.read_at_volatile(slice, off)?;
67 self.buf.extend_from_slice(&stage[..n]);
68 Ok(n)
69 }
70
71 fn available_bytes(&self) -> usize {
72 // The destination is an unbounded heap Vec.
73 usize::MAX
74 }
75}
76
77// ─────────────────────────────────────────────────────────────────────────────
78// MemReader — source for FileSystem::write
79// ─────────────────────────────────────────────────────────────────────────────
80
81/// A [`ZeroCopyReader`] backed by a caller-owned byte slice. Used as the `r`
82/// argument of `FileSystem::write`.
83pub struct MemReader<'a> {
84 data: &'a [u8],
85 pos: usize,
86}
87
88impl<'a> MemReader<'a> {
89 pub fn new(data: &'a [u8]) -> Self {
90 Self { data, pos: 0 }
91 }
92}
93
94impl Read for MemReader<'_> {
95 fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
96 let n = min(out.len(), self.data.len() - self.pos);
97 out[..n].copy_from_slice(&self.data[self.pos..self.pos + n]);
98 self.pos += n;
99 Ok(n)
100 }
101}
102
103impl ZeroCopyReader for MemReader<'_> {
104 /// Copy up to `count` bytes from our slice into the backend file `f` at
105 /// `off`, staging through a heap chunk.
106 fn read_to(
107 &mut self,
108 f: &mut dyn FileReadWriteVolatile,
109 count: usize,
110 off: u64,
111 ) -> io::Result<usize> {
112 let remaining = self.data.len() - self.pos;
113 let take = min(min(count, remaining), STAGE_CHUNK);
114 if take == 0 {
115 return Ok(0);
116 }
117 let mut stage = self.data[self.pos..self.pos + take].to_vec();
118 // SAFETY: `stage` is a live, exclusively-borrowed heap buffer for the
119 // duration of the volatile write; no other accessor exists.
120 let slice = unsafe { FileVolatileSlice::from_mut_slice(&mut stage) };
121 let n = f.write_at_volatile(slice, off)?;
122 self.pos += n;
123 Ok(n)
124 }
125}