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
use std::{future::Future, mem::MaybeUninit, path::PathBuf};
use crate::{
error::{CResult, CloudErrorKind},
request::Request,
utility::LocalBoxFuture,
};
use super::{info, ticket, SyncFilter};
/// Async core functions for implementing a Sync Engine.
///
/// [Send] and [Sync] are required as the callback could be invoked from an arbitrary thread, [read
/// here](https://docs.microsoft.com/en-us/windows/win32/api/cfapi/ne-cfapi-cf_callback_type#remarks).
pub trait Filter: Send + Sync {
/// A placeholder hydration has been requested. This means that the placeholder should be
/// populated with its corresponding data on the remote.
fn fetch_data(
&self,
_request: Request,
_ticket: ticket::FetchData,
_info: info::FetchData,
) -> impl Future<Output = CResult<()>>;
/// A placeholder hydration request has been cancelled.
fn cancel_fetch_data(
&self,
_request: Request,
_info: info::CancelFetchData,
) -> impl Future<Output = ()> {
async {}
}
/// Followed by a successful call to [Filter::fetch_data][super::Filter::fetch_data], this callback should verify the integrity of
/// the data persisted in the placeholder.
///
/// **You** are responsible for validating the data in the placeholder. To approve
/// the request, use the ticket provided.
///
/// Note that this callback is only called if [HydrationPolicy::ValidationRequired][crate::root::HydrationPolicy::ValidationRequired]
/// is specified.
fn validate_data(
&self,
_request: Request,
_ticket: ticket::ValidateData,
_info: info::ValidateData,
) -> impl Future<Output = CResult<()>> {
async { Err(CloudErrorKind::NotSupported) }
}
/// A directory population has been requested. The behavior of this callback is dependent on
/// the [PopulationType][crate::root::PopulationType] variant specified during registration.
fn fetch_placeholders(
&self,
_request: Request,
_ticket: ticket::FetchPlaceholders,
_info: info::FetchPlaceholders,
) -> impl Future<Output = CResult<()>> {
async { Err(CloudErrorKind::NotSupported) }
}
/// A directory population request has been cancelled.
fn cancel_fetch_placeholders(
&self,
_request: Request,
_info: info::CancelFetchPlaceholders,
) -> impl Future<Output = ()> {
async {}
}
/// A placeholder file handle has been opened for read, write, and/or delete
/// access.
fn opened(&self, _request: Request, _info: info::Opened) -> impl Future<Output = ()> {
async {}
}
/// A placeholder file handle that has been previously opened with read, write,
/// and/or delete access has been closed.
fn closed(&self, _request: Request, _info: info::Closed) -> impl Future<Output = ()> {
async {}
}
/// A placeholder dehydration has been requested. This means that all of the data persisted in
/// the file will be __completely__ discarded.
///
/// The operating system will handle dehydrating placeholder files automatically. However, it
/// is up to **you** to approve this. Use the ticket to approve the request.
fn dehydrate(
&self,
_request: Request,
_ticket: ticket::Dehydrate,
_info: info::Dehydrate,
) -> impl Future<Output = CResult<()>> {
async { Err(CloudErrorKind::NotSupported) }
}
/// A placeholder dehydration request has been cancelled.
fn dehydrated(&self, _request: Request, _info: info::Dehydrated) -> impl Future<Output = ()> {
async {}
}
/// A placeholder file is about to be deleted.
///
/// The operating system will handle deleting placeholder files automatically. However, it is
/// up to **you** to approve this. Use the ticket to approve the request.
fn delete(
&self,
_request: Request,
_ticket: ticket::Delete,
_info: info::Delete,
) -> impl Future<Output = CResult<()>> {
async { Err(CloudErrorKind::NotSupported) }
}
/// A placeholder file has been deleted.
fn deleted(&self, _request: Request, _info: info::Deleted) -> impl Future<Output = ()> {
async {}
}
/// A placeholder file is about to be renamed or moved.
///
/// The operating system will handle moving and renaming placeholder files automatically.
/// However, it is up to **you** to approve this. Use the ticket to approve the
/// request.
///
/// When the operation is completed, the [Filter::renamed] callback will be called.
fn rename(
&self,
_request: Request,
_ticket: ticket::Rename,
_info: info::Rename,
) -> impl Future<Output = CResult<()>> {
async { Err(CloudErrorKind::NotSupported) }
}
/// A placeholder file has been renamed or moved.
fn renamed(&self, _request: Request, _info: info::Renamed) -> impl Future<Output = ()> {
async {}
}
/// Placeholder for changed attributes under the sync root.
///
/// This callback is implemented using [ReadDirectoryChangesW][https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw]
/// so it is not provided by the `Cloud Filter APIs`.
///
/// This callback is used to detect when a user pins or unpins a placeholder file, etc.
///
/// See also [Cloud Files API Frequently Asked Questions](https://www.userfilesystem.com/programming/faq/).
fn state_changed(&self, _changes: Vec<PathBuf>) -> impl Future<Output = ()> {
async {}
}
}
/// Adapts a [Filter] to the [SyncFilter] trait.
pub struct AsyncBridge<F, B> {
filter: F,
block_on: B,
}
impl<F, B> AsyncBridge<F, B>
where
F: Filter,
B: Fn(LocalBoxFuture<'_, ()>) + Send + Sync,
{
pub fn new(filter: F, block_on: B) -> Self {
Self { filter, block_on }
}
}
impl<F, B> SyncFilter for AsyncBridge<F, B>
where
F: Filter,
B: Fn(LocalBoxFuture<'_, ()>) + Send + Sync,
{
fn fetch_data(
&self,
request: Request,
ticket: ticket::FetchData,
info: info::FetchData,
) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.fetch_data(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn cancel_fetch_data(&self, request: Request, info: info::CancelFetchData) {
(self.block_on)(Box::pin(self.filter.cancel_fetch_data(request, info)))
}
fn validate_data(
&self,
request: Request,
ticket: ticket::ValidateData,
info: info::ValidateData,
) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.validate_data(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn fetch_placeholders(
&self,
request: Request,
ticket: ticket::FetchPlaceholders,
info: info::FetchPlaceholders,
) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.fetch_placeholders(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn cancel_fetch_placeholders(&self, request: Request, info: info::CancelFetchPlaceholders) {
(self.block_on)(Box::pin(
self.filter.cancel_fetch_placeholders(request, info),
))
}
fn opened(&self, request: Request, info: info::Opened) {
(self.block_on)(Box::pin(self.filter.opened(request, info)))
}
fn closed(&self, request: Request, info: info::Closed) {
(self.block_on)(Box::pin(self.filter.closed(request, info)))
}
fn dehydrate(
&self,
request: Request,
ticket: ticket::Dehydrate,
info: info::Dehydrate,
) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.dehydrate(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn dehydrated(&self, request: Request, info: info::Dehydrated) {
(self.block_on)(Box::pin(self.filter.dehydrated(request, info)))
}
fn delete(&self, request: Request, ticket: ticket::Delete, info: info::Delete) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.delete(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn deleted(&self, request: Request, info: info::Deleted) {
(self.block_on)(Box::pin(self.filter.deleted(request, info)))
}
fn rename(&self, request: Request, ticket: ticket::Rename, info: info::Rename) -> CResult<()> {
let mut ret = MaybeUninit::zeroed();
(self.block_on)(Box::pin(async {
ret.write(self.filter.rename(request, ticket, info).await);
}));
unsafe { ret.assume_init() }
}
fn renamed(&self, request: Request, info: info::Renamed) {
(self.block_on)(Box::pin(self.filter.renamed(request, info)))
}
fn state_changed(&self, changes: Vec<PathBuf>) {
(self.block_on)(Box::pin(self.filter.state_changed(changes)))
}
}