summaryrefslogtreecommitdiffstats
path: root/src/source
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-11-11 09:49:08 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-11-11 17:05:26 +0100
commit87548dc2bc0e8ebc4ff3b1145fcada8c16e1ce1d (patch)
tree405e8d1f34d8855525af68b820a9af16775f6117 /src/source
parent0c20a2cdbb14bd175f51deb5ac69ab4190ba0530 (diff)
Add source cache types
This adds a module for source cache handling. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/source')
-rw-r--r--src/source/mod.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/source/mod.rs b/src/source/mod.rs
new file mode 100644
index 0000000..e88fd60
--- /dev/null
+++ b/src/source/mod.rs
@@ -0,0 +1,81 @@
+use std::path::PathBuf;
+
+use anyhow::Result;
+use anyhow::Error;
+
+use crate::package::Package;
+use crate::package::PackageName;
+use crate::package::PackageVersion;
+use crate::package::Source;
+
+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 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)
+ }
+
+}