summaryrefslogtreecommitdiffstats
path: root/src/util.rs
blob: 0685848fdd41ab9055c15874732f53b79036e133 (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
use std::path::{Component, Path, PathBuf};
use log::{debug, info, warn};
use sha1_smol::{Digest, Sha1};
use tokio::fs::File;
use tokio::{fs, io};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use crate::util;

#[derive(Debug)]
pub enum IntegrityError {
    SizeMismatch{ expect: usize, actual: usize },
    Sha1Mismatch{ expect: Digest, actual: Digest }
}

impl Display for IntegrityError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            IntegrityError::SizeMismatch{ expect, actual } =>
                write!(f, "size mismatch (expect {expect} bytes, got {actual} bytes)"),
            IntegrityError::Sha1Mismatch {expect, actual} =>
                write!(f, "sha1 mismatch (expect {expect}, got {actual})")
        }
    }
}

impl Error for IntegrityError {}

pub fn verify_sha1(expect: Digest, s: &str) -> Result<(), Digest> {
    let dig = Sha1::from(s).digest();

    if dig == expect {
        return Ok(());
    }

    Err(dig)
}

#[derive(Debug)]
pub enum FileVerifyError {
    Integrity(PathBuf, IntegrityError),
    Open(PathBuf, tokio::io::Error),
    Read(PathBuf, tokio::io::Error),
}

impl Display for FileVerifyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            FileVerifyError::Integrity(path, e) => write!(f, "file integrity error {}: {}", path.display(), e),
            FileVerifyError::Open(path, e) => write!(f, "error opening file {}: {}", path.display(), e),
            FileVerifyError::Read(path, e) => write!(f, "error reading file {}: {}", path.display(), e)
        }
    }
}

impl Error for FileVerifyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            FileVerifyError::Integrity(_, e) => Some(e),
            FileVerifyError::Open(_, e) => Some(e),
            FileVerifyError::Read(_, e) => Some(e)
        }
    }
}

pub async fn verify_file(path: impl AsRef<Path>, expect_size: Option<usize>, expect_sha1: Option<Digest>) -> Result<(), FileVerifyError> {
    let path = path.as_ref();
    
    if expect_size.is_none() && expect_sha1.is_none() {
        return match fs::metadata(path).await {
            Ok(_) => {
                debug!("No size or sha1 for {}, have to assume it's good.", path.display());
                Ok(())
            },
            Err(e) => {
                Err(FileVerifyError::Open(path.to_path_buf(), e))
            }
        }
    }
    
    let mut file = File::open(path).await.map_err(|e| FileVerifyError::Open(path.to_owned(), e))?;

    let mut tally = 0usize;
    let mut st = Sha1::new();
    let mut buf = [0u8; 4096];

    loop {
        let n = match file.read(&mut buf).await {
            Ok(n) => n,
            Err(e) => match e.kind() {
                ErrorKind::Interrupted => continue,
                _ => return Err(FileVerifyError::Read(path.to_owned(), e))
            }
        };
        
        if n == 0 {
            break;
        }
        
        st.update(&buf[..n]);
        tally += n;
    }
    
    let dig = st.digest();
    
    if expect_size.is_some_and(|sz| sz != tally) {
        return Err(FileVerifyError::Integrity(path.to_owned(), IntegrityError::SizeMismatch {
            expect: expect_size.unwrap(),
            actual: tally
        }));
    } else if expect_sha1.is_some_and(|exp_dig| exp_dig != dig) {
        return Err(FileVerifyError::Integrity(path.to_owned(), IntegrityError::Sha1Mismatch {
            expect: expect_sha1.unwrap(),
            actual: dig
        }));
    }
    
    Ok(())
}

#[derive(Debug)]
pub enum EnsureFileError {
    IO { what: &'static str, error: io::Error },
    Download { url: String, error: reqwest::Error },
    Integrity(IntegrityError),
    Offline,
    MissingURL
}

impl Display for EnsureFileError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            EnsureFileError::IO { what, error } => write!(f, "i/o error ensuring file ({what}): {error}"),
            EnsureFileError::Download { url, error } => write!(f, "error downloading file ({url}): {error}"),
            EnsureFileError::Integrity(e) => write!(f, "integrity error for downloaded file: {e}"),
            EnsureFileError::Offline => f.write_str("unable to download file while offline"),
            EnsureFileError::MissingURL => f.write_str("missing url"),
        }
    }
}

impl Error for EnsureFileError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            EnsureFileError::IO { error, .. } => Some(error),
            EnsureFileError::Download { error, .. } => Some(error),
            EnsureFileError::Integrity(error) => Some(error),
            _ => None
        }
    }
}

pub async fn should_download(path: impl AsRef<Path>, expect_size: Option<usize>, expect_sha1: Option<Digest>) -> Result<bool, io::Error> {
    let path = path.as_ref();

    match verify_file(path, expect_size, expect_sha1).await {
        Ok(()) => {
            debug!("Skipping download for file {}, integrity matches.", path.display());
            Ok(false)
        },
        Err(FileVerifyError::Open(_, e)) if e.kind() == ErrorKind::NotFound => {
            debug!("File {} is missing, downloading it.", path.display());
            Ok(true)
        },
        Err(FileVerifyError::Integrity(p, e)) => {
            warn!("Integrity error on file {}: {}", p.display(), e);

            // try to delete the file since it's bad
            let _ = fs::remove_file(path).await
                .map_err(|e| warn!("Error deleting corrupted/modified file {} (ignoring): {}", path.display(), e));
            Ok(true)
        }
        Err(FileVerifyError::Open(_, e) | FileVerifyError::Read(_, e)) => {
            warn!("Error verifying file {} on disk: {}", path.display(), e);
            Err(e)
        }
    }
}

pub async fn ensure_file(path: impl AsRef<Path>, url: Option<&str>, expect_size: Option<usize>, expect_sha1: Option<Digest>, online: bool, force_download: bool) -> Result<bool, EnsureFileError> {
    let path = path.as_ref();

    if !force_download {
        if !should_download(path, expect_size, expect_sha1).await
            .map_err(|e| EnsureFileError::IO { what: "verifying file on disk", error: e })? {
            
            return Ok(false);
        }
    }

    if !online {
        warn!("Cannot download {} to {} while offline!", url.unwrap_or("(no url)"), path.display());
        return Err(EnsureFileError::Offline);
    }

    // download the file
    let Some(url) = url else {
        return Err(EnsureFileError::MissingURL);
    };

    let mut file = File::create(path).await.map_err(|e| EnsureFileError::IO {
        what: "save downloaded file (open)",
        error: e
    })?;

    debug!("File {} must be downloaded ({}).", path.display(), url);

    let mut response = reqwest::get(url).await.map_err(|e| EnsureFileError::Download { url: url.to_owned(), error: e })?;
    let mut tally = 0usize;
    let mut sha1 = Sha1::new();

    while let Some(chunk) = response.chunk().await.map_err(|e| EnsureFileError::Download { url: url.to_owned(), error: e })? {
        let slice = chunk.as_ref();

        file.write_all(slice).await.map_err(|e| EnsureFileError::IO {
            what: "save downloaded file (write)",
            error: e
        })?;

        tally += slice.len();
        sha1.update(slice);
    }

    drop(file); // manually close file

    let del_file_silent = || async {
        debug!("Deleting downloaded file {} since its integrity doesn't match :(", path.display());
        let _ = fs::remove_file(path).await.map_err(|e| warn!("failed to delete invalid downloaded file: {}", e));
        ()
    };

    if expect_size.is_some_and(|s| s != tally) {
        del_file_silent().await;

        return Err(EnsureFileError::Integrity(IntegrityError::SizeMismatch {
            expect: expect_size.unwrap(),
            actual: tally
        }));
    }

    let digest = sha1.digest();

    if expect_sha1.is_some_and(|exp_dig| exp_dig != digest) {
        del_file_silent().await;

        return Err(EnsureFileError::Integrity(IntegrityError::Sha1Mismatch {
            expect: expect_sha1.unwrap(),
            actual: digest
        }));
    }

    info!("File {} downloaded successfully.", path.display());
    Ok(true)
}

pub fn check_path(name: &str) -> Result<&Path, &'static str> {
    let entry_path: &Path = Path::new(name);

    let mut depth = 0usize;
    for component in entry_path.components() {
        depth = match component {
            Component::Prefix(_) | Component::RootDir =>
                return Err("root path component in entry"),
            Component::ParentDir => depth.checked_sub(1)
                .map_or_else(|| Err("entry path escapes"), |s| Ok(s))?,
            Component::Normal(_) => depth + 1,
            _ => depth
        }
    }

    Ok(entry_path)
}

#[cfg(windows)]
pub fn strip_verbatim(path: &Path) -> &Path {
    let Some(Component::Prefix(p)) = path.components().next() else {
        return path;
    };

    use std::path::Prefix;
    use std::ffi::OsStr;

    match p.kind() {
        Prefix::VerbatimDisk(_) =>
            Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&path.as_os_str().as_encoded_bytes()[4..]) }),
        _ => path
    }
}

#[cfg(not(windows))]
pub fn strip_verbatim(path: &Path) -> &Path {
    path
}

pub trait AsJavaPath {
    fn as_java_path(&self) -> &Path;
}

impl AsJavaPath for Path {
    fn as_java_path(&self) -> &Path {
        strip_verbatim(self)
    }
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;
    use std::path::Prefix;

    #[test]
    #[cfg(windows)]
    fn test_strip_verbatim() {
        let path = Path::new(r"\\?\C:\Some\Verbatim\Path");
        match path.components().next().unwrap() {
            Component::Prefix(p) => assert!(matches!(p.kind(), Prefix::VerbatimDisk(_)), "(TEST BUG) path does not start with verbatim disk"),
            _ => panic!("(TEST BUG) path does not start with prefix")
        }

        let path2 = path.as_java_path();
        match path2.components().next().unwrap() {
            Component::Prefix(p) => assert!(matches!(p.kind(), Prefix::Disk(_))),
            _ => panic!("path does not begin with prefix")
        }
    }
}