summaryrefslogtreecommitdiffstats
path: root/src/launcher/extract.rs
blob: 0a0817578219d87e9255e08808642ee9f05defd2 (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
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::{fs, io, os};
use std::fs::File;
use std::io::{BufReader, Error as IOError, Read};
use std::path::{Component, Path, PathBuf};
use log::{debug, trace};
use zip::result::ZipError;
use zip::ZipArchive;
use crate::util;

#[derive(Debug)]
pub enum ZipExtractError {
    IO { what: &'static str, error: IOError },
    Zip { what: &'static str, error: ZipError },
    InvalidEntry { why: &'static str, name: String }
}

impl From<(&'static str, IOError)> for ZipExtractError {
    fn from((what, error): (&'static str, IOError)) -> Self {
        ZipExtractError::IO { what, error }
    }
}

impl From<(&'static str, ZipError)> for ZipExtractError {
    fn from((what, error): (&'static str, ZipError)) -> Self {
        ZipExtractError::Zip { what, error }
    }
}

impl Display for ZipExtractError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ZipExtractError::IO { what, error } => write!(f, "i/o error ({what}): {error}"),
            ZipExtractError::Zip { what, error } => write!(f, "zip error ({what}): {error}"),
            ZipExtractError::InvalidEntry { why, name } => write!(f, "invalid entry in zip file ({why}): {name}")
        }
    }
}

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

fn check_entry_path(name: &str) -> Result<&Path, ZipExtractError> {
    util::check_path(name).map_err(|e| ZipExtractError::InvalidEntry {
        why: e,
        name: name.to_owned()
    })
}

#[cfg(unix)]
fn extract_symlink(path: impl AsRef<Path>, target: &str) -> io::Result<()> {
    os::unix::fs::symlink(target, path)
}

#[cfg(windows)]
fn extract_symlink(path: impl AsRef<Path>, target: &str) -> io::Result<()> {
    os::windows::fs::symlink_file(target, path)
}

#[cfg(not(any(unix, windows)))]
fn extract_symlink(path: impl AsRef<Path>, _target: &str) -> io::Result<()> {
    warn!("Refusing to extract symbolic link to {}. I don't know how to do it on this platform!", path.as_ref().display());
    Ok(())
}

pub fn extract_zip<F>(zip_path: impl AsRef<Path>, extract_root: impl AsRef<Path>, condition: F) -> Result<usize, ZipExtractError>
where
    F: Fn(&str) -> bool
{
    debug!("Extracting zip file {} into {}", zip_path.as_ref().display(), extract_root.as_ref().display());

    fs::create_dir_all(&extract_root).map_err(|e| ZipExtractError::from(("create extract root", e)))?;

    let mut extracted = 0usize;

    let file = File::open(&zip_path).map_err(|e| ZipExtractError::from(("extract zip file (open)", e)))?;
    let read = BufReader::new(file);

    let mut archive = ZipArchive::new(read).map_err(|e| ZipExtractError::from(("read zip archive", e)))?;

    // create directories
    for n in 0..archive.len() {
        let entry = archive.by_index(n).map_err(|e| ZipExtractError::from(("read zip entry (1)", e)))?;
        if !entry.is_dir() { continue; }

        let name = entry.name();
        if !condition(name) {
            continue;
        }

        let entry_path = check_entry_path(name)?;
        let entry_path: PathBuf = [extract_root.as_ref(), entry_path].iter().collect();

        trace!("Extracting directory {} from {}", entry.name(), zip_path.as_ref().display());
        fs::create_dir_all(entry_path).map_err(|e| ZipExtractError::from(("extract directory", e)))?;
    }

    // extract the files
    for n in 0..archive.len() {
        let mut entry = archive.by_index(n).map_err(|e| ZipExtractError::from(("read zip entry (2)", e)))?;
        let name = entry.name();

        if entry.is_dir() { continue; }

        if !condition(name) {
            continue;
        }

        let entry_path = check_entry_path(name)?;
        let entry_path: PathBuf = [extract_root.as_ref(), entry_path].iter().collect();

        if entry.is_symlink() {
            let mut target = String::new();
            entry.read_to_string(&mut target).map_err(|e| ZipExtractError::from(("read to symlink target", e)))?;

            trace!("Extracting symbolic link {} -> {} from {}", entry.name(), target, zip_path.as_ref().display());
            extract_symlink(entry_path.as_path(), target.as_str()).map_err(|e| ZipExtractError::from(("extract symlink", e)))?;
        } else if entry.is_file() {
            let mut outfile = File::create(&entry_path).map_err(|e| ZipExtractError::from(("extract zip entry (open)", e)))?;

            trace!("Extracting file {} from {}", entry.name(), zip_path.as_ref().display());
            io::copy(&mut entry, &mut outfile).map_err(|e| ZipExtractError::from(("extract zip entry (write)", e)))?;
            extracted += 1;
        }
    }

    Ok(extracted)
}