diff options
author | Chirantan Ekbote <chirantan@chromium.org> | 2021-08-24 21:52:54 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-08-24 21:52:54 +0000 |
commit | 4a18ac12b163d3a9e538d4a19e2e36a77c1c038c (patch) | |
tree | 5f68bfda686f36ce3a472121465e2abdb972c17f | |
parent | acffe1523233e2f68b0eb51a9c4aeb79609c6088 (diff) | |
parent | c7cc0267b6fee03b3c8abda52674633e71705685 (diff) | |
download | crosvm-4a18ac12b163d3a9e538d4a19e2e36a77c1c038c.tar.gz |
UPSTREAM: fuse: Implement Reader and Writer for mutable references am: 23cecdfc31 am: c7c288d2fe am: f7adc1e392 am: c7cc0267b6
Original change: https://android-review.googlesource.com/c/platform/external/crosvm/+/1806640
Change-Id: Ie3098b013fd4ee78ab0aef702573e26386d955b4
-rw-r--r-- | devices/src/virtio/fs/worker.rs | 2 | ||||
-rw-r--r-- | fuse/src/server.rs | 23 | ||||
-rw-r--r-- | fuse/src/worker.rs | 2 |
3 files changed, 26 insertions, 1 deletions
diff --git a/devices/src/virtio/fs/worker.rs b/devices/src/virtio/fs/worker.rs index b4943bd6a..e3bab811c 100644 --- a/devices/src/virtio/fs/worker.rs +++ b/devices/src/virtio/fs/worker.rs @@ -20,6 +20,8 @@ use crate::virtio::{Interrupt, Queue, Reader, SignalableInterrupt, Writer}; impl fuse::Reader for Reader {} impl fuse::Writer for Writer { + type ClosureWriter = Self; + fn write_at<F>(&mut self, offset: usize, f: F) -> io::Result<usize> where F: Fn(&mut Self) -> io::Result<usize>, diff --git a/fuse/src/server.rs b/fuse/src/server.rs index f28e9488a..7538c4d39 100644 --- a/fuse/src/server.rs +++ b/fuse/src/server.rs @@ -24,11 +24,17 @@ const DIRENT_PADDING: [u8; 8] = [0; 8]; /// A trait for reading from the underlying FUSE endpoint. pub trait Reader: io::Read {} +impl<R: Reader> Reader for &'_ mut R {} + /// A trait for writing to the underlying FUSE endpoint. The FUSE device expects the write /// operation to happen in one write transaction. Since there are cases when data needs to be /// generated earlier than the header, it implies the writer implementation to keep an internal /// buffer. The buffer then can be flushed once header and data are both prepared. pub trait Writer: io::Write { + /// The type passed in to the closure in `write_at`. For most implementations, this should be + /// `Self`. + type ClosureWriter: Writer + ZeroCopyWriter; + /// Allows a closure to generate and write data at the current writer's offset. The current /// writer is passed as a mutable reference to the closure. As an example, this provides an /// adapter for the read implementation of a filesystem to write directly to the final buffer @@ -41,12 +47,27 @@ pub trait Writer: io::Write { /// complexity. fn write_at<F>(&mut self, offset: usize, f: F) -> io::Result<usize> where - F: Fn(&mut Self) -> io::Result<usize>; + F: Fn(&mut Self::ClosureWriter) -> io::Result<usize>; /// Checks if the writer can still accept certain amount of data. fn has_sufficient_buffer(&self, size: u32) -> bool; } +impl<W: Writer> Writer for &'_ mut W { + type ClosureWriter = W::ClosureWriter; + + fn write_at<F>(&mut self, offset: usize, f: F) -> io::Result<usize> + where + F: Fn(&mut Self::ClosureWriter) -> io::Result<usize>, + { + (**self).write_at(offset, f) + } + + fn has_sufficient_buffer(&self, size: u32) -> bool { + (**self).has_sufficient_buffer(size) + } +} + /// A trait for memory mapping for DAX. /// /// For some transports (like virtio) it may be possible to share a region of memory with the diff --git a/fuse/src/worker.rs b/fuse/src/worker.rs index 378e5eee3..18b344492 100644 --- a/fuse/src/worker.rs +++ b/fuse/src/worker.rs @@ -77,6 +77,8 @@ impl Write for DevFuseWriter<'_> { } impl Writer for DevFuseWriter<'_> { + type ClosureWriter = Self; + fn write_at<F>(&mut self, offset: usize, f: F) -> io::Result<usize> where F: Fn(&mut Self) -> io::Result<usize>, |