Skip to main content

coven/
handle_device_only.rs

1//! Making cloud files device-only on this device at the app's request
2//! (§17.3, E8): downloading them, then the write that switches them.
3
4use crate::*;
5
6impl CovenHandle {
7    /// Downloads cloud files to the paths named under `folder`, for a write
8    /// to switch them all to device-only on this device with
9    /// `CovenWrite::make_device_only`, each copy becoming the original this
10    /// device records (§17.3). Each name is written as the one rule for
11    /// every platform makes it. The member must be in each file's audience
12    /// and may change every row referencing it. A path through a symbolic
13    /// link below the folder, where something is already, or naming what
14    /// another path names once written, case and Unicode form aside, is
15    /// refused. `on_progress` is called on this task with each file's
16    /// download and how many of its bytes, and of all the call's, are down,
17    /// at most a few times a second. Until it returns, `cancel` stops the
18    /// call; a failure, a panic in a download among them, or a cancel removes
19    /// every copy and folder it made.
20    pub async fn prepare_device_only(
21        &self,
22        folder: &std::path::Path,
23        files: Vec<(FileId, RelativePath)>,
24        cancel: &tokio::sync::watch::Receiver<bool>,
25        on_progress: impl Fn(Progress<DeviceOnlyStep>),
26    ) -> Result<PreparedDeviceOnly, DeviceOnlyError> {
27        let (progress, watched) = tokio::sync::watch::channel(None);
28        let preparing = self.files.prepare_device_only(folder, files, cancel, progress);
29        coven_foundation::progress::forward(watched, preparing, on_progress).await
30    }
31
32    /// `prepare_device_only`, then `CovenWrite::make_device_only` in a write
33    /// of its own. A write that fails removes the copies.
34    pub async fn make_device_only(
35        &self,
36        folder: &std::path::Path,
37        files: Vec<(FileId, RelativePath)>,
38        cancel: &tokio::sync::watch::Receiver<bool>,
39        on_progress: impl Fn(Progress<DeviceOnlyStep>),
40    ) -> Result<(), DeviceOnlyError> {
41        let prepared = self.prepare_device_only(folder, files, cancel, on_progress).await?;
42        self.write(move |write| write.make_device_only(prepared))
43            .await
44            .map_err(DeviceOnlyError::from)
45    }
46}
47
48#[cfg(test)]
49#[path = "handle_device_only_tests.rs"]
50mod tests;