Skip to main content

coven_database/
file_write_moves.rs

1//! The moves of this device's files to the cloud that a write records
2//! (§17.3): which files each moves, checked when the app names them, and
3//! the uploads queued for them once the write applied.
4
5use super::{FileWrite, StagedSource};
6use crate::sqlite::StoreSql;
7use crate::{
8    file_hashes::FileHashes,
9    file_location::{FileLocation, Place},
10    file_moves::{self, MoveState},
11    file_table::Record,
12    DbError, MoveId, Provenance,
13};
14use coven_foundation::{files::StagedName, id_source::FileId};
15use std::collections::BTreeSet;
16
17/// A file of this device, on it only, to move to the cloud (§17.3, E3).
18#[derive(Clone, Copy, Debug)]
19pub enum MoveFile {
20    /// A user's original: its upload checks the bytes it sends against the
21    /// size, modification time and chunk hashes its staging recorded.
22    User(FileId),
23    /// As `User`, and kept whole here too once stored: the write switching
24    /// it to the cloud pins it on this device, which downloads it from
25    /// storage. The original is never read for the cache (§17.8).
26    UserKeptHere(FileId),
27    /// An app file, uploaded from coven's copy.
28    App(FileId),
29    /// As `App`, and kept whole here too once stored: the cache copy is
30    /// made from coven's copy (§17.8).
31    AppKeptHere(FileId),
32}
33
34/// A move this write recorded, with the uploads it queues once applied.
35pub(super) struct Moving {
36    id: MoveId,
37    uploads: Vec<MoveUpload>,
38}
39
40/// One upload of a move: the files over the same bytes it stores, each with
41/// whether this device keeps it whole once stored.
42struct MoveUpload {
43    files: Vec<(FileId, bool)>,
44    size: u64,
45    source: StagedSource,
46    hashes: FileHashes,
47}
48
49impl FileWrite<'_> {
50    /// Record a move of these files to the cloud (§17.3). Each must be on
51    /// this device only (`NotThisDevice`, `NotDeviceOnlyHere`) and in no
52    /// move, whether one this write recorded or a moving or failed one
53    /// (`FileInMove`). A user's original uploads with the facts and hashes
54    /// its staging recorded, so its upload fails the move if the original
55    /// changed since. The files switch to the cloud together, in one write
56    /// once every one is stored.
57    pub(crate) fn move_to_cloud(&self, files: Vec<MoveFile>) -> Result<MoveId, DbError> {
58        let db = self.database;
59        let mut uploads: Vec<MoveUpload> = Vec::new();
60        let mut named = BTreeSet::new();
61        for file in files {
62            let (id, keep) = match file {
63                MoveFile::User(id) | MoveFile::App(id) => (id, false),
64                MoveFile::UserKeptHere(id) | MoveFile::AppKeptHere(id) => (id, true),
65            };
66            if !named.insert(id) {
67                continue;
68            }
69            let record = Record::read(db, id)?.ok_or(DbError::FileAbsent { file: id })?;
70            match record.place {
71                Place::DeviceOnly(device) if device == self.files.device => {}
72                Place::DeviceOnly(_) => return Err(DbError::NotThisDevice { file: id }),
73                Place::Cloud(_) => return Err(DbError::NotDeviceOnlyHere { file: id }),
74            }
75            // A file is in at most one move: one this write recorded, or a
76            // moving or failed one, which the app cancels to free it.
77            let recorded = self
78                .files
79                .moving
80                .borrow()
81                .iter()
82                .find(|moving| {
83                    moving.uploads.iter().any(|upload| upload.files.iter().any(|(f, _)| *f == id))
84                })
85                .map(|moving| moving.id);
86            let holding = match recorded {
87                Some(of) => Some(of),
88                None => file_moves::of_file(db, id)?,
89            };
90            if let Some(of) = holding {
91                return Err(DbError::FileInMove { file: id, id: of });
92            }
93            let upload = match file {
94                MoveFile::User(_) | MoveFile::UserKeptHere(_) => {
95                    let wrong_kind = || DbError::NamespaceKind {
96                        namespace: record.namespace.clone(),
97                        kind: Provenance::AppProvided,
98                    };
99                    let original = crate::user_file::read(db, id)?.ok_or_else(wrong_kind)?;
100                    let hashes = crate::user_file::hashes(db, id)?.ok_or_else(wrong_kind)?;
101                    MoveUpload {
102                        files: vec![(id, keep)],
103                        size: original.size,
104                        source: StagedSource::Original(original),
105                        hashes,
106                    }
107                }
108                MoveFile::App(_) | MoveFile::AppKeptHere(_) => {
109                    let (name, hashes) = copy(db, id)?.ok_or_else(|| DbError::NamespaceKind {
110                        namespace: record.namespace.clone(),
111                        kind: Provenance::UserProvided,
112                    })?;
113                    MoveUpload {
114                        files: vec![(id, keep)],
115                        size: record.size,
116                        source: StagedSource::Copy(name),
117                        hashes,
118                    }
119                }
120            };
121            // Files over the same bytes here share one upload.
122            match uploads.iter_mut().find(|queued| same_bytes(queued, &upload)) {
123                Some(queued) => queued.files.push((id, keep)),
124                None => uploads.push(upload),
125            }
126        }
127        let id = file_moves::create(db)?;
128        self.files.moving.borrow_mut().push(Moving { id, uploads });
129        Ok(id)
130    }
131
132    /// Cancel a move: every file stays on this device only, and its record
133    /// ends (§17.3). Cancelling a cancelled move does nothing; a finished
134    /// move, whose files are in the cloud, can't be cancelled, nor can a
135    /// bound file's, whose held write would go out naming a file no other
136    /// device can read (§17.4).
137    pub(crate) fn cancel_move(&self, id: MoveId) -> Result<(), DbError> {
138        let db = self.database;
139        match file_moves::state(db, id)? {
140            None => Err(DbError::MoveUnknown { id }),
141            Some(MoveState::Finished) => Err(DbError::MoveFinished { id }),
142            Some(MoveState::Cancelled) => Ok(()),
143            Some(MoveState::Moving | MoveState::Failed) if file_moves::bound(db, id)? => {
144                Err(DbError::MoveBound { id })
145            }
146            Some(MoveState::Moving | MoveState::Failed) => {
147                self.files.moving.borrow_mut().retain(|moving| moving.id != id);
148                file_moves::end_unswitched(db, id, MoveState::Cancelled)?;
149                crate::pending::clear_move(db, id)
150            }
151        }
152    }
153
154    /// Queue the uploads of each move this write recorded, for the files
155    /// still on this device only once it applied. A move left with none
156    /// has nothing to switch, and ends.
157    pub(super) fn queue_moves(&self, now: std::time::SystemTime) -> Result<(), DbError> {
158        let db = self.database;
159        for moving in self.files.moving.take() {
160            let mut queued = false;
161            for upload in moving.uploads {
162                let mut files = Vec::new();
163                for (file, keep) in upload.files {
164                    if Record::read(db, file)?
165                        .is_some_and(|record| record.place == Place::DeviceOnly(self.files.device))
166                    {
167                        files.push((file, keep));
168                    }
169                }
170                if files.is_empty() {
171                    continue;
172                }
173                let location = FileLocation::new(self.files.device, self.files.ids)?;
174                crate::file_queue::enqueue(
175                    db,
176                    crate::file_queue::Queued {
177                        files: &files,
178                        size: upload.size,
179                        source: &upload.source,
180                        hashes: &upload.hashes,
181                        of: moving.id,
182                        holds: None,
183                    },
184                    &location,
185                    now,
186                )?;
187                queued = true;
188            }
189            if !queued {
190                file_moves::finish(db, moving.id)?;
191            }
192        }
193        Ok(())
194    }
195}
196
197/// Whether two uploads would send the same bytes here: the same copy, or
198/// the same original as one read recorded it. Two stagings of one path
199/// recorded apart may have read different bytes, and at most one of them
200/// still matches it.
201fn same_bytes(a: &MoveUpload, b: &MoveUpload) -> bool {
202    match (&a.source, &b.source) {
203        (StagedSource::Original(original), StagedSource::Original(other)) => {
204            original == other && a.hashes.content == b.hashes.content
205        }
206        (StagedSource::Copy(a), StagedSource::Copy(b)) => a == b,
207        _ => false,
208    }
209}
210
211/// Coven's copy of an app file, with the hashes its first read recorded.
212fn copy(db: &StoreSql, file: FileId) -> Result<Option<(StagedName, FileHashes)>, DbError> {
213    let Some((path, content)) = db
214        .query("SELECT path,hash FROM _coven_device_files WHERE file=?1", [file], |r| {
215            Ok((r.get::<_, String>(0)?, r.get::<_, Vec<u8>>(1)?))
216        })?
217        .pop()
218    else {
219        return Ok(None);
220    };
221    let chunks = db.query(
222        "SELECT hash FROM _coven_copy_chunks WHERE path=?1 ORDER BY chunk",
223        [&path],
224        |r| r.get::<_, Vec<u8>>(0),
225    )?;
226    Ok(Some((
227        StagedName::parse(&path).ok_or(DbError::DamagedDatabase)?,
228        FileHashes::stored(content, chunks)?,
229    )))
230}