vfs/directory/watchers/event_producers.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! When generating a watcher event, one needs "a list of names" that are then converted into
//! buffers sent to the watchers. In a sense, an iterator over a list of strings would work, but
//! in order to avoid copying the data around, this namespace provides a more specialized version
//! of this abstraction.
use fidl_fuchsia_io as fio;
use static_assertions::assert_eq_size;
/// Watcher event producer, that generates buffers filled with watcher events. Watchers use this
/// API to obtain buffers that are then sent to the actual watchers. Every producer may generate
/// multiple events, but they all need to be of the same type, as returned by [`Self::event()`] and
/// [`Self::mask()`] methods.
pub trait EventProducer {
/// Returns a mask that represents the type of events this producer can generate, as one of the
/// `fidl_fuchsia_io::WatchMask::*` constants. There might be only one bit set and it should
/// correspond to the event returned by the [`Self::event()`] method. It is a duplication, but it
/// helps the callers that need both masks and event IDs.
fn mask(&self) -> fio::WatchMask;
/// Returns an event ID this event producer will use to populate the buffers, as one of the
/// `fidl_fuchsia_io::WatchEvent::*` constants. Must match what [`Self::mask()`], returns, see
/// there for details.
fn event(&self) -> fio::WatchEvent;
/// Checks if this producer can create another buffer, returning `true` if it can. This method
/// does not actually need to construct the buffer just yet, as an optimization if it will not
/// be needed.
fn prepare_for_next_buffer(&mut self) -> bool;
/// Returns a copy of the current buffer prepared by this producer. This method will be the
/// one constructing a buffer, if necessary, after a preceding call to
/// [`Self::prepare_for_next_buffer()`].
///
/// Note that this method will keep returning copies of the same buffer, until
/// [`Self::prepare_for_next_buffer()`] is not called explicitly.
fn buffer(&mut self) -> Vec<u8>;
}
/// Common mechanism used by both [`StaticVecEventProducer`] and, later, [`SinkEventProducer`].
struct CachingEventProducer {
mask: fio::WatchMask,
event: fio::WatchEvent,
current_buffer: Option<Vec<u8>>,
}
impl CachingEventProducer {
fn new(mask: fio::WatchMask, event: fio::WatchEvent) -> Self {
CachingEventProducer { mask, event, current_buffer: None }
}
fn mask(&self) -> fio::WatchMask {
self.mask
}
fn event(&self) -> fio::WatchEvent {
self.event
}
fn prepare_for_next_buffer(&mut self) {
self.current_buffer = None;
}
/// Users of [`CachingEventProducer`] should use this method to implement
/// [`EventProducer::buffer`]. `fill_buffer` is a callback used to populate the buffer when
/// necessary. It's 'u8' argument is the event ID used by this producer.
fn buffer<FillBuffer>(&mut self, fill_buffer: FillBuffer) -> Vec<u8>
where
FillBuffer: FnOnce(fio::WatchEvent) -> Vec<u8>,
{
match &self.current_buffer {
Some(buf) => buf.clone(),
None => {
let buf = fill_buffer(self.event);
self.current_buffer = Some(buf.clone());
buf
}
}
}
}
/// An [`EventProducer`] that uses a `Vec<String>` with names of the entires to be put into the
/// watcher event.
pub struct StaticVecEventProducer {
cache: CachingEventProducer,
names: Vec<String>,
next: usize,
}
impl StaticVecEventProducer {
/// Constructs a new [`EventProducer`] that is producing names form the specified list,
/// building events of type `WatchEvent::Added`. `names` is not allowed to be empty.
pub fn added(names: Vec<String>) -> Self {
Self::new(fio::WatchMask::ADDED, fio::WatchEvent::Added, names)
}
/// Constructs a new [`EventProducer`] that is producing names form the specified list,
/// building events of type `WatchEvent::Removed`. `names` is not allowed to be empty.
pub fn removed(names: Vec<String>) -> Self {
Self::new(fio::WatchMask::REMOVED, fio::WatchEvent::Removed, names)
}
/// Constructs a new [`EventProducer`] that is producing names form the specified list,
/// building events of type `WatchEvent::Existing`. `names` is not allowed to be empty.
pub fn existing(names: Vec<String>) -> Self {
Self::new(fio::WatchMask::EXISTING, fio::WatchEvent::Existing, names)
}
fn new(mask: fio::WatchMask, event: fio::WatchEvent, names: Vec<String>) -> Self {
debug_assert!(!names.is_empty());
Self { cache: CachingEventProducer::new(mask, event), names, next: 0 }
}
// Can not use `&mut self` here as it would "lock" the whole object disallowing the
// `self.cache.buffer()` call where we want to pass this method in a closure.
fn fill_buffer(event: fio::WatchEvent, next: &mut usize, names: &mut Vec<String>) -> Vec<u8> {
let mut buffer = vec![];
while *next < names.len() {
if !encode_name(&mut buffer, event, &names[*next]) {
break;
}
*next += 1;
}
buffer
}
}
impl EventProducer for StaticVecEventProducer {
fn mask(&self) -> fio::WatchMask {
self.cache.mask()
}
fn event(&self) -> fio::WatchEvent {
self.cache.event()
}
fn prepare_for_next_buffer(&mut self) -> bool {
self.cache.prepare_for_next_buffer();
self.next < self.names.len()
}
fn buffer(&mut self) -> Vec<u8> {
let cache = &mut self.cache;
let next = &mut self.next;
let names = &mut self.names;
cache.buffer(|event| Self::fill_buffer(event, next, names))
}
}
/// An event producer for an event containing only one name. It is slightly optimized, but
/// otherwise functionally equivalent to the [`StaticVecEventProducer`] with an array of one
/// element.
pub struct SingleNameEventProducer {
producer: SingleBufferEventProducer,
}
impl SingleNameEventProducer {
/// Constructs a new [`SingleNameEventProducer`] that will produce an event for one name of
/// type `WatchEvent::Deleted`. Deleted refers to the directory the watcher itself is on, and
/// therefore statically refers to itself as ".".
pub fn deleted() -> Self {
Self::new(fio::WatchMask::DELETED, fio::WatchEvent::Deleted, ".")
}
/// Constructs a new [`SingleNameEventProducer`] that will produce an event for one name of
/// type `WatchEvent::Added`.
pub fn added(name: &str) -> Self {
Self::new(fio::WatchMask::ADDED, fio::WatchEvent::Added, name)
}
/// Constructs a new [`SingleNameEventProducer`] that will produce an event for one name of
/// type `WatchEvent::Removed`.
pub fn removed(name: &str) -> Self {
Self::new(fio::WatchMask::REMOVED, fio::WatchEvent::Removed, name)
}
/// Constructs a new [`SingleNameEventProducer`] that will produce an event for one name of
/// type `WatchEvent::Existing`.
pub fn existing(name: &str) -> Self {
Self::new(fio::WatchMask::EXISTING, fio::WatchEvent::Existing, name)
}
/// Constructs a new [`SingleNameEventProducer`] that will produce an `WatchEvent::Idle` event.
pub fn idle() -> Self {
Self::new(fio::WatchMask::IDLE, fio::WatchEvent::Idle, "")
}
fn new(mask: fio::WatchMask, event: fio::WatchEvent, name: &str) -> Self {
let mut buffer = vec![];
encode_name(&mut buffer, event, name);
Self { producer: SingleBufferEventProducer::new(mask, event, buffer) }
}
}
impl EventProducer for SingleNameEventProducer {
fn mask(&self) -> fio::WatchMask {
self.producer.mask()
}
fn event(&self) -> fio::WatchEvent {
self.producer.event()
}
fn prepare_for_next_buffer(&mut self) -> bool {
self.producer.prepare_for_next_buffer()
}
fn buffer(&mut self) -> Vec<u8> {
self.producer.buffer()
}
}
pub(crate) fn encode_name(buffer: &mut Vec<u8>, event: fio::WatchEvent, name: &str) -> bool {
if buffer.len() + (2 + name.len()) > fio::MAX_BUF as usize {
return false;
}
// We are going to encode the file name length as u8.
debug_assert!(u8::max_value() as u64 >= fio::MAX_FILENAME);
buffer.push(event.into_primitive());
buffer.push(name.len() as u8);
buffer.extend_from_slice(name.as_bytes());
true
}
enum SingleBufferEventProducerState {
Start,
FirstEvent,
Done,
}
/// An event producer for an event that has one buffer of data.
pub struct SingleBufferEventProducer {
mask: fio::WatchMask,
event: fio::WatchEvent,
buffer: Vec<u8>,
state: SingleBufferEventProducerState,
}
impl SingleBufferEventProducer {
/// Constructs a new [`SingleBufferEventProducer`] that will produce an event for one name of
/// type `WatchEvent::Existing`.
pub fn existing(buffer: Vec<u8>) -> Self {
assert_eq_size!(usize, u64);
debug_assert!(buffer.len() as u64 <= fio::MAX_BUF);
Self::new(fio::WatchMask::EXISTING, fio::WatchEvent::Existing, buffer)
}
fn new(mask: fio::WatchMask, event: fio::WatchEvent, buffer: Vec<u8>) -> Self {
assert_eq_size!(usize, u64);
debug_assert!(buffer.len() as u64 <= fio::MAX_BUF);
Self { mask, event, buffer, state: SingleBufferEventProducerState::Start }
}
}
impl EventProducer for SingleBufferEventProducer {
fn mask(&self) -> fio::WatchMask {
self.mask
}
fn event(&self) -> fio::WatchEvent {
self.event
}
fn prepare_for_next_buffer(&mut self) -> bool {
match self.state {
SingleBufferEventProducerState::Start => {
self.state = SingleBufferEventProducerState::FirstEvent;
true
}
SingleBufferEventProducerState::FirstEvent => {
self.state = SingleBufferEventProducerState::Done;
false
}
SingleBufferEventProducerState::Done => false,
}
}
fn buffer(&mut self) -> Vec<u8> {
self.buffer.clone()
}
}