summaryrefslogtreecommitdiffstats
path: root/src/source/mod.rs
blob: 81bf34daa7cd41b07477f8d37fc1a6b460807d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::path::PathBuf;

use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use log::trace;
use url::Url;

use crate::package::Package;
use crate::package::PackageName;
use crate::package::PackageVersion;
use crate::package::Source;

#[derive(Clone, Debug)]
pub struct SourceCache {
    root: PathBuf,
}

impl SourceCache {
    pub fn new(root: PathBuf) -> Self {
        SourceCache { root }
    }

    pub fn sources_for(&self, p: &Package) -> Vec<SourceEntry> {
        SourceEntry::for_package(self.root.clone(), p)
    }
}

#[derive(Debug)]
pub struct SourceEntry {
    cache_root: PathBuf,
    package_name: PackageName,
    package_version: PackageVersion,
    package_source_name: String,
    package_source: Source,
}

impl SourceEntry {

    fn source_file_path(&self) -> PathBuf {
        self.source_file_directory().join(format!("{}-{}.source", self.package_source_name, self.package_source.hash().value()))
    }

    fn source_file_directory(&self) -> PathBuf {
        self.cache_root.join(format!("{}-{}", self.package_name, self.package_version))
    }

    fn for_package(cache_root: PathBuf, package: &Package) -> Vec<Self> {
        package.sources()
            .clone()
            .into_iter()
            .map(|(source_name, source)| {
                SourceEntry {
                    cache_root: cache_root.clone(),
                    package_name: package.name().clone(),
                    package_version: package.version().clone(),
                    package_source_name: source_name,
                    package_source: source,
                }
            })
            .collect()
    }

    pub fn exists(&self) -> bool {
        self.source_file_path().exists()
    }

    pub fn path(&self) -> PathBuf {
        self.source_file_path()
    }

    pub fn url(&self) -> &Url {
        self.package_source.url()
    }

    pub async fn remove_file(&self) -> Result<()> {
        let p = self.source_file_path();
        tokio::fs::remove_file(&p).await?;
        Ok(())
    }

    pub async fn verify_hash(&self) -> Result<()> {
        use tokio::io::AsyncReadExt;

        let p = self.source_file_path();

        trace!("Reading to buffer: {}", p.display());
        let mut buf = vec![];
        tokio::fs::OpenOptions::new()
            .create(false)
            .create_new(false)
            .read(true)
            .open(&p)
            .await?
            .read_to_end(&mut buf)
            .await?;

        trace!("Reading to buffer finished: {}", p.display());
        self.package_source
            .hash()
            .matches_hash_of(&buf)
    }

    pub async fn create(&self) -> Result<tokio::fs::File> {
        let p = self.source_file_path();
        trace!("Creating source file: {}", p.display());

        if !self.cache_root.is_dir() {
            trace!("Cache root does not exist: {}", self.cache_root.display());
            return Err(anyhow!("Cache root {} does not exist!", self.cache_root.display()))
        }

        {
            let dir = self.source_file_directory();
            if !dir.is_dir() {
                trace!("Creating directory: {}", dir.display());
                tokio::fs::create_dir(&dir)
                    .await
                    .with_context(|| {
                        anyhow!("Creating source cache directory for package {} {}: {}",
                            self.package_source_name,
                            self.package_source.hash().value(),
                            dir.display())
                    })?;
            } else {
                trace!("Directory exists: {}", dir.display());
            }
        }

        trace!("Creating file now: {}", p.display());
        tokio::fs::OpenOptions::new()
            .create(true)
            .create_new(true)
            .write(true)
            .open(&p)
            .await
            .with_context(|| anyhow!("Creating file: {}", p.display()))
            .map_err(Error::from)
    }

}