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
use crate::describer::Describable;
use crate::errors::ProtoError;
use starbase_utils::fs::{self, FsError};
use std::fs::File;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use tar::Archive;
use tracing::{debug, trace};
use zip::ZipArchive;

#[async_trait::async_trait]
pub trait Installable<'tool>: Send + Sync + Describable<'tool> {
    /// Return a prefix that will be removed from all paths when
    /// unpacking an archive and copying the files.
    fn get_archive_prefix(&self) -> Result<Option<String>, ProtoError> {
        Ok(None)
    }

    /// Return an absolute file path to the directory containing the installed tool.
    /// This is typically `~/.proto/tools/<tool>/<version>`.
    fn get_install_dir(&self) -> Result<PathBuf, ProtoError>;

    /// Run any installation steps after downloading and verifying the tool.
    /// This is typically unzipping an archive, and running any installers/binaries.
    async fn install(&self, install_dir: &Path, download_path: &Path) -> Result<bool, ProtoError> {
        if install_dir.exists() {
            debug!(tool = self.get_id(), "Tool already installed, continuing");

            return Ok(false);
        }

        if !download_path.exists() {
            return Err(ProtoError::InstallMissingDownload(self.get_name()));
        }

        let prefix = self.get_archive_prefix()?;

        debug!(
            tool = self.get_id(),
            download_file = ?download_path,
            install_dir = ?install_dir,
            "Attempting to install tool",
        );

        if self.should_unpack() && unpack(download_path, install_dir, prefix)? {
            // Unpacked archive
        } else {
            let install_path = install_dir.join(if cfg!(windows) {
                format!("{}.exe", self.get_id())
            } else {
                self.get_id().to_string()
            });

            // Not an archive, assume a binary and copy
            fs::rename(download_path, &install_path)?;
            fs::update_perms(install_path, None)?;
        }

        debug!(tool = self.get_id(), "Successfully installed tool");

        Ok(true)
    }

    /// Whether or not the downloaded file should be unpacked before installing.
    fn should_unpack(&self) -> bool {
        true
    }

    /// Uninstall the tool by deleting the install directory.
    async fn uninstall(&self, install_dir: &Path) -> Result<bool, ProtoError> {
        if !install_dir.exists() {
            debug!(
                tool = self.get_id(),
                "Tool has not been installed, aborting"
            );

            return Ok(false);
        }

        debug!(
            tool = self.get_id(),
            install_dir = ?install_dir,
            "Deleting install directory"
        );

        fs::remove_dir_all(install_dir)?;

        debug!(tool = self.get_id(), "Successfully uninstalled tool");

        Ok(true)
    }
}

pub fn unpack<I: AsRef<Path>, O: AsRef<Path>>(
    input_file: I,
    output_dir: O,
    remove_prefix: Option<String>,
) -> Result<bool, ProtoError> {
    let input_file = input_file.as_ref();
    let ext = input_file.extension().map(|e| e.to_str().unwrap());

    match ext {
        Some("zip") => unzip(input_file, output_dir, remove_prefix)?,
        Some("tgz" | "gz") => untar_gzip(input_file, output_dir, remove_prefix)?,
        Some("txz" | "xz") => untar_xzip(input_file, output_dir, remove_prefix)?,
        Some("exe") | None => {
            return Ok(false);
        }
        _ => {
            return Err(ProtoError::UnsupportedArchiveFormat(
                input_file.to_path_buf(),
                ext.unwrap_or_default().to_string(),
            ))
        }
    };

    Ok(true)
}

#[tracing::instrument(skip_all)]
pub fn untar<I: AsRef<Path>, O: AsRef<Path>, R: FnOnce(File) -> D, D: Read>(
    input_file: I,
    output_dir: O,
    remove_prefix: Option<String>,
    decoder: R,
) -> Result<(), ProtoError> {
    let input_file = input_file.as_ref();
    let output_dir = output_dir.as_ref();
    let handle_input_error = |error: io::Error| FsError::Read {
        path: input_file.to_path_buf(),
        error,
    };

    trace!(
        input_file = ?input_file,
        output_dir = ?output_dir,
        "Unpacking tar archive",
    );

    if !output_dir.exists() {
        fs::create_dir_all(output_dir)?;
    }

    // Open .tar.gz file
    let tar_gz = fs::open_file(input_file)?;

    // Decompress to .tar
    let tar = decoder(tar_gz);

    // Unpack the archive into the output dir
    let mut archive = Archive::new(tar);

    for entry_result in archive.entries().map_err(handle_input_error)? {
        let mut entry = entry_result.map_err(handle_input_error)?;
        let mut path: PathBuf = entry.path().map_err(handle_input_error)?.into_owned();

        // Remove the prefix
        if let Some(prefix) = &remove_prefix {
            if path.starts_with(prefix) {
                path = path.strip_prefix(prefix).unwrap().to_owned();
            }
        }

        let output_path = output_dir.join(path);

        // Create parent dirs
        if let Some(parent_dir) = output_path.parent() {
            fs::create_dir_all(parent_dir)?;
        }

        entry.unpack(&output_path).map_err(|error| FsError::Write {
            path: output_path.to_path_buf(),
            error,
        })?;
    }

    Ok(())
}

pub fn untar_gzip<I: AsRef<Path>, O: AsRef<Path>>(
    input_file: I,
    output_dir: O,
    remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
    untar(input_file, output_dir, remove_prefix, |file| {
        flate2::read::GzDecoder::new(file)
    })
}

pub fn untar_xzip<I: AsRef<Path>, O: AsRef<Path>>(
    input_file: I,
    output_dir: O,
    remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
    untar(input_file, output_dir, remove_prefix, |file| {
        xz2::read::XzDecoder::new(file)
    })
}

#[tracing::instrument(skip_all)]
pub fn unzip<I: AsRef<Path>, O: AsRef<Path>>(
    input_file: I,
    output_dir: O,
    remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
    let input_file = input_file.as_ref();
    let output_dir = output_dir.as_ref();

    trace!(
        input_file = ?input_file,
        output_dir = ?output_dir,
        "Unzipping zip archive"
    );

    if !output_dir.exists() {
        fs::create_dir_all(output_dir)?;
    }

    // Open .zip file
    let zip = fs::open_file(input_file)?;

    // Unpack the archive into the output dir
    let mut archive = ZipArchive::new(zip).map_err(ProtoError::Zip)?;

    for i in 0..archive.len() {
        let mut file = archive.by_index(i).map_err(ProtoError::Zip)?;

        let mut path = match file.enclosed_name() {
            Some(path) => path.to_owned(),
            None => continue,
        };

        // Remove the prefix
        if let Some(prefix) = &remove_prefix {
            if path.starts_with(prefix) {
                path = path.strip_prefix(prefix).unwrap().to_owned();
            }
        }

        let output_path = output_dir.join(&path);

        // Create parent dirs
        if let Some(parent_dir) = &output_path.parent() {
            fs::create_dir_all(parent_dir)?;
        }

        // If a folder, create the dir
        if file.is_dir() {
            fs::create_dir_all(&output_path)?;
        }

        // If a file, copy it to the output dir
        if file.is_file() {
            let mut out = fs::create_file(&output_path)?;

            io::copy(&mut file, &mut out).map_err(|error| FsError::Write {
                path: output_path.to_path_buf(),
                error,
            })?;

            fs::update_perms(&output_path, file.unix_mode())?;
        }
    }

    Ok(())
}