Skip to main content

coven_database/
file_ref.rs

1//! A file as this device has its record: its id, size, namespace, audience
2//! and place (§17.2, E8). A file never changes its bytes, so a reference
3//! taken earlier still reads what it named.
4
5use crate::sqlite::StoreSql;
6use crate::{
7    file_location::{FileLocation, Place},
8    file_table::Record,
9    DbError,
10};
11use coven_crypto::ContentHash;
12use coven_foundation::id_source::{DeviceId, FileId};
13use coven_merge::Audience;
14
15#[path = "local_file.rs"]
16mod local_file;
17pub(crate) use local_file::{device_only_source, open_copy, open_source, queued_source};
18pub use local_file::{LocalFileError, LocalFileStream, Located};
19
20/// One file as its record stood when it was read.
21#[derive(Clone, Debug, PartialEq)]
22pub struct FileRef {
23    id: FileId,
24    audience: Audience,
25    namespace: String,
26    size: u64,
27    place: Stored,
28}
29
30#[derive(Clone, Debug, PartialEq)]
31enum Stored {
32    Cloud { location: FileLocation, hash: ContentHash },
33    DeviceOnly(DeviceId),
34}
35
36/// Where a file is (§17.2): in storage, or only on one device.
37#[derive(Clone, Copy, Debug, PartialEq)]
38pub enum FilePlace<'a> {
39    /// Stored at its location, with its content hash.
40    Cloud {
41        /// The stored object and its key.
42        location: &'a FileLocation,
43        /// The device that uploaded it.
44        uploader: DeviceId,
45        /// The SHA-256 of its bytes, which every download checks.
46        content_hash: &'a ContentHash,
47    },
48    /// Only on one device, which may be this one: while a file uploads, it
49    /// is on its uploader only.
50    DeviceOnly {
51        /// The device that has it.
52        device: DeviceId,
53    },
54}
55
56impl FileRef {
57    /// This reference with a size its record never held, for tests of what
58    /// sums sizes (a real file this large cannot be staged).
59    #[cfg(test)]
60    pub(crate) fn planted_size(mut self, size: u64) -> Self {
61        self.size = size;
62        self
63    }
64    /// The file's id, which rows name.
65    pub fn id(&self) -> FileId {
66        self.id
67    }
68    /// Its size in bytes.
69    pub fn size(&self) -> u64 {
70        self.size
71    }
72    /// Its namespace, which says its kind and whose cache budget it counts
73    /// against.
74    pub fn namespace(&self) -> &str {
75        &self.namespace
76    }
77    /// Its audience: only that audience's rows reference it, and only its
78    /// members read it.
79    pub fn audience(&self) -> Audience {
80        self.audience.clone()
81    }
82    /// Where it is.
83    pub fn place(&self) -> FilePlace<'_> {
84        match &self.place {
85            Stored::Cloud { location, hash } => {
86                FilePlace::Cloud { location, uploader: location.uploader(), content_hash: hash }
87            }
88            Stored::DeviceOnly(device) => FilePlace::DeviceOnly { device: *device },
89        }
90    }
91
92    pub(crate) fn of(record: Record) -> Result<Self, DbError> {
93        let place = match (record.place, record.hash) {
94            (Place::Cloud(location), Some(hash)) => Stored::Cloud { location, hash },
95            (Place::DeviceOnly(device), None) => Stored::DeviceOnly(device),
96            _ => return Err(DbError::DamagedDatabase),
97        };
98        Ok(Self {
99            id: record.id,
100            audience: record.audience,
101            namespace: record.namespace,
102            size: record.size,
103            place,
104        })
105    }
106}
107
108/// The file this device has a record of under `file`.
109pub(crate) fn read(db: &StoreSql, file: FileId) -> Result<FileRef, DbError> {
110    FileRef::of(Record::read(db, file)?.ok_or(DbError::FileAbsent { file })?)
111}
112
113#[cfg(test)]
114#[path = "file_ref_tests.rs"]
115mod tests;