use std::ops::Range;
use windows::{
core,
Win32::Storage::CloudFilters::{CfReportProviderProgress, CF_CONNECTION_KEY},
};
use crate::{
command::{self, Command},
filter::{RawConnectionKey, RawTransferKey},
placeholder_file::PlaceholderFile,
sealed, utility,
};
#[derive(Debug)]
pub struct FetchData {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl FetchData {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn report_progress(&self, total: u64, completed: u64) -> core::Result<()> {
unsafe {
CfReportProviderProgress(
CF_CONNECTION_KEY(self.connection_key),
self.transfer_key,
total as i64,
completed as i64,
)
}?;
Ok(())
}
}
impl utility::ReadAt for FetchData {
fn read_at(&self, buf: &mut [u8], offset: u64) -> core::Result<u64> {
command::Read {
buffer: buf,
position: offset,
}
.execute(self.connection_key, self.transfer_key)
}
}
impl utility::WriteAt for FetchData {
fn write_at(&self, buf: &[u8], offset: u64) -> core::Result<()> {
command::Write {
buffer: buf,
position: offset,
}
.execute(self.connection_key, self.transfer_key)
}
}
impl sealed::Sealed for FetchData {}
#[derive(Debug)]
pub struct ValidateData {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl ValidateData {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn pass(&self, range: Range<u64>) -> core::Result<()> {
command::Validate { range }.execute(self.connection_key, self.transfer_key)
}
}
impl utility::ReadAt for ValidateData {
fn read_at(&self, buf: &mut [u8], offset: u64) -> core::Result<u64> {
command::Read {
buffer: buf,
position: offset,
}
.execute(self.connection_key, self.transfer_key)
}
}
impl sealed::Sealed for ValidateData {}
#[derive(Debug)]
pub struct FetchPlaceholders {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl FetchPlaceholders {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn pass_with_placeholder(&self, placeholders: &mut [PlaceholderFile]) -> core::Result<()> {
command::CreatePlaceholders {
total: placeholders.len() as _,
placeholders,
}
.execute(self.connection_key, self.transfer_key)
}
}
#[derive(Debug)]
pub struct Dehydrate {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl Dehydrate {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn pass(&self) -> core::Result<()> {
command::Dehydrate { blob: &[] }.execute(self.connection_key, self.transfer_key)
}
pub fn pass_with_blob(&self, blob: &[u8]) -> core::Result<()> {
command::Dehydrate { blob }.execute(self.connection_key, self.transfer_key)
}
}
#[derive(Debug)]
pub struct Delete {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl Delete {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn pass(&self) -> core::Result<()> {
command::Delete.execute(self.connection_key, self.transfer_key)
}
}
#[derive(Debug)]
pub struct Rename {
connection_key: RawConnectionKey,
transfer_key: RawTransferKey,
}
impl Rename {
pub(crate) fn new(connection_key: RawConnectionKey, transfer_key: RawTransferKey) -> Self {
Self {
connection_key,
transfer_key,
}
}
pub fn pass(&self) -> core::Result<()> {
command::Rename.execute(self.connection_key, self.transfer_key)
}
}