2022-09-13 19:32:31 +02:00
|
|
|
use std::fs::File as StdFile;
|
2022-09-13 15:35:57 +02:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-18 15:04:14 +02:00
|
|
|
use std::str::FromStr;
|
2022-09-13 15:35:57 +02:00
|
|
|
|
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
const UPDATE_FILES_PATH: &str = "updates/updates_files";
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
2022-09-13 15:54:35 +02:00
|
|
|
pub enum Error {
|
2023-01-24 16:17:23 +01:00
|
|
|
#[error("Could not parse file name as utf-8")]
|
|
|
|
CouldNotParseFileNameAsUtf8,
|
2022-09-13 15:35:57 +02:00
|
|
|
#[error(transparent)]
|
|
|
|
IoError(#[from] std::io::Error),
|
|
|
|
#[error(transparent)]
|
|
|
|
PersistError(#[from] tempfile::PersistError),
|
2023-01-24 16:17:23 +01:00
|
|
|
#[error(transparent)]
|
|
|
|
UuidError(#[from] uuid::Error),
|
2022-09-13 15:35:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 15:54:35 +02:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-13 19:32:31 +02:00
|
|
|
impl Deref for File {
|
2022-09-13 15:35:57 +02:00
|
|
|
type Target = NamedTempFile;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:32:31 +02:00
|
|
|
impl DerefMut for File {
|
2022-09-13 15:35:57 +02:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:54:35 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2022-09-13 19:32:31 +02:00
|
|
|
pub struct FileStore {
|
2022-09-13 15:54:35 +02:00
|
|
|
path: PathBuf,
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-13 19:32:31 +02:00
|
|
|
impl FileStore {
|
|
|
|
pub fn new(path: impl AsRef<Path>) -> Result<FileStore> {
|
2022-09-21 12:01:46 +02:00
|
|
|
let path = path.as_ref().to_path_buf();
|
2022-09-13 15:54:35 +02:00
|
|
|
std::fs::create_dir_all(&path)?;
|
2022-09-13 19:32:31 +02:00
|
|
|
Ok(FileStore { path })
|
2022-09-13 15:35:57 +02:00
|
|
|
}
|
2022-09-15 13:34:02 +02:00
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-15 13:34:02 +02:00
|
|
|
impl FileStore {
|
2022-09-13 15:54:35 +02:00
|
|
|
/// Creates a new temporary update file.
|
|
|
|
/// A call to `persist` is needed to persist the file in the database.
|
2022-09-13 19:32:31 +02:00
|
|
|
pub fn new_update(&self) -> Result<(Uuid, File)> {
|
2022-09-13 15:54:35 +02:00
|
|
|
let file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
let path = self.path.join(uuid.to_string());
|
2022-09-13 19:32:31 +02:00
|
|
|
let update_file = File { file, path };
|
2022-09-13 15:54:35 +02:00
|
|
|
|
|
|
|
Ok((uuid, update_file))
|
2022-09-13 15:35:57 +02:00
|
|
|
}
|
2022-10-11 09:53:08 +02:00
|
|
|
|
2022-10-10 15:51:28 +02:00
|
|
|
/// Creates a new temporary update file with the given Uuid.
|
|
|
|
/// A call to `persist` is needed to persist the file in the database.
|
2022-10-11 09:55:03 +02:00
|
|
|
pub fn new_update_with_uuid(&self, uuid: u128) -> Result<(Uuid, File)> {
|
2022-10-10 15:51:28 +02:00
|
|
|
let file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
let uuid = Uuid::from_u128(uuid);
|
|
|
|
let path = self.path.join(uuid.to_string());
|
|
|
|
let update_file = File { file, path };
|
|
|
|
|
|
|
|
Ok((uuid, update_file))
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-13 15:54:35 +02:00
|
|
|
/// Returns the file corresponding to the requested uuid.
|
2022-09-13 19:32:31 +02:00
|
|
|
pub fn get_update(&self, uuid: Uuid) -> Result<StdFile> {
|
2022-10-25 14:09:01 +02:00
|
|
|
let path = self.get_update_path(uuid);
|
2022-09-13 19:32:31 +02:00
|
|
|
let file = StdFile::open(path)?;
|
2022-09-13 15:54:35 +02:00
|
|
|
Ok(file)
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-10-25 14:09:01 +02:00
|
|
|
/// Returns the path that correspond to this uuid, the path could not exists.
|
|
|
|
pub fn get_update_path(&self, uuid: Uuid) -> PathBuf {
|
|
|
|
self.path.join(uuid.to_string())
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:54:35 +02:00
|
|
|
/// Copies the content of the update file pointed to by `uuid` to the `dst` directory.
|
|
|
|
pub fn snapshot(&self, uuid: Uuid, dst: impl AsRef<Path>) -> Result<()> {
|
|
|
|
let src = self.path.join(uuid.to_string());
|
|
|
|
let mut dst = dst.as_ref().join(UPDATE_FILES_PATH);
|
|
|
|
std::fs::create_dir_all(&dst)?;
|
|
|
|
dst.push(uuid.to_string());
|
|
|
|
std::fs::copy(src, dst)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2023-01-25 11:20:15 +01:00
|
|
|
/// Compute the size of all the updates contained in the file store.
|
|
|
|
pub fn compute_total_size(&self) -> Result<u64> {
|
2023-01-24 16:17:23 +01:00
|
|
|
let mut total = 0;
|
|
|
|
for uuid in self.all_uuids()? {
|
2023-01-25 11:20:15 +01:00
|
|
|
total += self.compute_size(uuid?).unwrap_or_default();
|
2023-01-24 16:17:23 +01:00
|
|
|
}
|
|
|
|
Ok(total)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:20:15 +01:00
|
|
|
/// Compute the size of one update
|
|
|
|
pub fn compute_size(&self, uuid: Uuid) -> Result<u64> {
|
2022-09-13 15:54:35 +02:00
|
|
|
Ok(self.get_update(uuid)?.metadata()?.len())
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-13 15:54:35 +02:00
|
|
|
pub fn delete(&self, uuid: Uuid) -> Result<()> {
|
|
|
|
let path = self.path.join(uuid.to_string());
|
|
|
|
std::fs::remove_file(path)?;
|
|
|
|
Ok(())
|
2022-09-13 15:35:57 +02:00
|
|
|
}
|
2022-10-18 15:04:14 +02:00
|
|
|
|
|
|
|
/// List the Uuids of the files in the FileStore
|
2023-01-24 16:17:23 +01:00
|
|
|
pub fn all_uuids(&self) -> Result<impl Iterator<Item = Result<Uuid>>> {
|
2023-02-22 13:48:51 +01:00
|
|
|
Ok(self.path.read_dir()?.filter_map(|entry| {
|
|
|
|
let file_name = match entry {
|
|
|
|
Ok(entry) => entry.file_name(),
|
|
|
|
Err(e) => return Some(Err(e.into())),
|
|
|
|
};
|
|
|
|
let file_name = match file_name.to_str() {
|
|
|
|
Some(file_name) => file_name,
|
|
|
|
None => return Some(Err(Error::CouldNotParseFileNameAsUtf8)),
|
|
|
|
};
|
|
|
|
if file_name.starts_with('.') {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Uuid::from_str(file_name).map_err(|e| e.into()))
|
|
|
|
}
|
2023-01-24 16:17:23 +01:00
|
|
|
}))
|
2022-10-18 15:04:14 +02:00
|
|
|
}
|
2022-09-13 15:54:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 19:32:31 +02:00
|
|
|
pub struct File {
|
2022-09-13 15:54:35 +02:00
|
|
|
path: PathBuf,
|
|
|
|
file: NamedTempFile,
|
|
|
|
}
|
2022-09-13 15:35:57 +02:00
|
|
|
|
2022-09-13 19:32:31 +02:00
|
|
|
impl File {
|
2022-09-13 15:54:35 +02:00
|
|
|
pub fn persist(self) -> Result<()> {
|
|
|
|
self.file.persist(&self.path)?;
|
|
|
|
Ok(())
|
2022-09-13 15:35:57 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 13:48:51 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn all_uuids() {
|
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
let fs = FileStore::new(dir.path()).unwrap();
|
|
|
|
let (uuid, mut file) = fs.new_update().unwrap();
|
|
|
|
file.write_all(b"Hello world").unwrap();
|
|
|
|
file.persist().unwrap();
|
|
|
|
let all_uuids = fs.all_uuids().unwrap().collect::<Result<Vec<_>>>().unwrap();
|
|
|
|
assert_eq!(all_uuids, vec![uuid]);
|
|
|
|
|
|
|
|
let (uuid2, file) = fs.new_update().unwrap();
|
|
|
|
let all_uuids = fs.all_uuids().unwrap().collect::<Result<Vec<_>>>().unwrap();
|
|
|
|
assert_eq!(all_uuids, vec![uuid]);
|
|
|
|
|
|
|
|
file.persist().unwrap();
|
|
|
|
let mut all_uuids = fs.all_uuids().unwrap().collect::<Result<Vec<_>>>().unwrap();
|
|
|
|
all_uuids.sort();
|
|
|
|
let mut expected = vec![uuid, uuid2];
|
|
|
|
expected.sort();
|
|
|
|
assert_eq!(all_uuids, expected);
|
|
|
|
}
|
|
|
|
}
|