diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-11-11 17:05:35 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-11-11 17:05:35 +0100 |
commit | 42738fc53867acf87f0a76e67121affd627272ad (patch) | |
tree | 92b0c5188e48a52010afad3ed4a9b69b74606677 /src/source | |
parent | 62b1b76a87bd3298030a04576fbcd68051a7c61f (diff) | |
parent | 3933a3bc13183e053c0e6cfed78d00390878da0b (diff) |
Merge branch 'source-cache'
Diffstat (limited to 'src/source')
-rw-r--r-- | src/source/mod.rs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/source/mod.rs b/src/source/mod.rs new file mode 100644 index 0000000..16e5e4c --- /dev/null +++ b/src/source/mod.rs @@ -0,0 +1,102 @@ +use std::path::PathBuf; + +use anyhow::Result; +use anyhow::Error; +use url::Url; + +use crate::package::Package; +use crate::package::PackageName; +use crate::package::PackageVersion; +use crate::package::Source; +use crate::util::progress::ProgressBars; + +pub struct SourceCache { + root: PathBuf, +} + +impl SourceCache { + pub fn new(root: PathBuf) -> Self { + SourceCache { root } + } + + pub fn source_for(&self, p: &Package) -> SourceEntry { + SourceEntry::for_package(self.root.clone(), p) + } +} + +pub struct SourceEntry { + cache_root: PathBuf, + package_name: PackageName, + package_version: PackageVersion, + package_source: Source, + package_source_path: PathBuf, +} + +impl SourceEntry { + + fn for_package(cache_root: PathBuf, package: &Package) -> Self { + let package_source_path = cache_root.join(format!("{}-{}.source", package.name(), package.version())); + + SourceEntry { + cache_root, + package_name: package.name().clone(), + package_version: package.version().clone(), + package_source: package.source().clone(), + package_source_path + } + } + + pub fn exists(&self) -> bool { + self.package_source_path.exists() + } + + pub fn path(&self) -> &PathBuf { + &self.package_source_path + } + + pub fn url(&self) -> &Url { + self.package_source.url() + } + + pub async fn remove_file(&self) -> Result<()> { + tokio::fs::remove_file(&self.package_source_path).await.map_err(Error::from) + } + + pub async fn verify_hash(&self) -> Result<bool> { + use tokio::io::AsyncReadExt; + + let mut buf = vec![]; + tokio::fs::OpenOptions::new() + .create(false) + .create_new(false) + .read(true) + .open(&self.package_source_path) + .await? + .read_to_end(&mut buf) + .await?; + + self.package_source.hash().matches_hash_of(&buf) + } + + pub async fn open(&self) -> Result<tokio::fs::File> { + tokio::fs::OpenOptions::new() + .create(false) + .create_new(false) + .read(true) + .open(&self.package_source_path) + .await + .map_err(Error::from) + } + + pub async fn create(&self) -> Result<tokio::fs::File> { + tokio::fs::OpenOptions::new() + .create(true) + .create_new(true) + .write(true) + .open(&self.package_source_path) + .await + .map_err(Error::from) + } + +} + |