File: install-mimic.rs

package info (click to toggle)
install-mimic 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: perl: 385; ansic: 200; makefile: 96; sh: 51
file content (265 lines) | stat: -rw-r--r-- 7,808 bytes parent folder | download
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
// SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
// SPDX-License-Identifier: BSD-2-Clause

use std::env;
use std::fs;
use std::io::ErrorKind;
use std::os::unix::fs::MetadataExt as _;
use std::process::Command;

use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser as _;
use clap_derive::Parser;
use eyre::{Result, WrapErr as _, bail, eyre};

#[derive(Parser)]
#[clap(version)]
struct Cli {
    /// Display the features supported by the program.
    #[clap(long)]
    features: bool,

    /// Specify a reference file to obtain the information from.
    #[clap(short)]
    reffile: Option<Utf8PathBuf>,

    /// Verbose operation; display diagnostic output.
    #[clap(short, long)]
    verbose: bool,

    filenames: Vec<Utf8PathBuf>,
}

const VERSION_STR: &str = env!("CARGO_PKG_VERSION");

struct Config {
    filenames: Vec<Utf8PathBuf>,
    destination: Utf8PathBuf,
    refname: Option<Utf8PathBuf>,
    verbose: bool,
}

enum Mode {
    Handled,
    Install(Config),
}

#[expect(clippy::print_stdout, reason = "This is the purpose of this function")]
fn features() {
    println!("Features: install-mimic={VERSION_STR}");
}

fn install_mimic<SP: AsRef<Utf8Path>, DP: AsRef<Utf8Path>, RP: AsRef<Utf8Path>>(
    src: SP,
    dst: DP,
    refname: Option<&RP>,
    verbose: bool,
) -> Result<()> {
    let filetoref = refname.as_ref().map_or_else(
        || dst.as_ref().to_path_buf(),
        |refpath| refpath.as_ref().to_path_buf(),
    );
    let stat = filetoref
        .metadata()
        .with_context(|| format!("Could not examine {filetoref}"))?;
    let user_id = stat.uid().to_string();
    let group_id = stat.gid().to_string();
    let mode = format!("{mode:o}", mode = stat.mode() & 0o7777);
    let prog_name = "install";
    let args = [
        "-c",
        "-o",
        &user_id,
        "-g",
        &group_id,
        "-m",
        &mode,
        "--",
        src.as_ref().as_str(),
        dst.as_ref().as_str(),
    ];
    let mut cmd = Command::new(prog_name);
    cmd.args(args);
    #[expect(clippy::print_stdout, reason = "This is the purpose of this function")]
    if verbose {
        println!("{prog_name} {args}", args = shell_words::join(args));
    }
    if !cmd.status().context("Could not run install")?.success() {
        bail!(
            "Could not install {src} as {dst}",
            src = src.as_ref(),
            dst = dst.as_ref()
        );
    }
    Ok(())
}

fn parse_args() -> Result<Mode> {
    let opts = Cli::parse();
    if opts.features {
        features();
        return Ok(Mode::Handled);
    }

    let mut filenames = opts.filenames;
    let destination = filenames
        .pop()
        .ok_or_else(|| eyre!("No source or destination paths specified"))?;
    if filenames.is_empty() {
        bail!("At least one source and one destination path must be specified");
    }
    Ok(Mode::Install(Config {
        filenames,
        destination,
        refname: opts.reffile,
        verbose: opts.verbose,
    }))
}

fn doit(cfg: &Config) -> Result<()> {
    let is_dir = match fs::metadata(&cfg.destination) {
        Err(err) if err.kind() == ErrorKind::NotFound => {
            if cfg.refname.is_none() {
                bail!(
                    "The destination path {dst} does not exist and no -r specified",
                    dst = cfg.destination
                );
            }
            false
        }
        Err(err) => {
            bail!("Could not examine {dst}: {err}", dst = cfg.destination);
        }
        Ok(data) => data.is_dir(),
    };
    if is_dir {
        for path in &cfg.filenames {
            let basename = path
                .file_name()
                .ok_or_else(|| eyre!("Invalid source filename {path}"))?;
            install_mimic(
                path,
                cfg.destination.join(basename),
                cfg.refname.as_ref(),
                cfg.verbose,
            )?;
        }
        Ok(())
    } else {
        match *cfg.filenames {
            [ref source] => {
                install_mimic(source, &cfg.destination, cfg.refname.as_ref(), cfg.verbose)
            }
            _ => bail!(
                "The destination path must be a directory if more than one source path is specified"
            ),
        }
    }
}

fn main() -> Result<()> {
    match parse_args().context("Could not parse the command-line arguments")? {
        Mode::Handled => Ok(()),
        Mode::Install(cfg) => doit(&cfg),
    }
}

#[cfg(test)]
#[expect(clippy::print_stdout, reason = "this is a test suite")]
#[expect(clippy::use_debug, reason = "this is a test suite")]
mod tests {
    use std::env;
    use std::io::ErrorKind as IoErrorKind;
    use std::process::{Command, Stdio};
    use std::sync::LazyLock;

    use camino::{Utf8Path, Utf8PathBuf};
    use eyre::{Result, WrapErr as _, bail, eyre};

    static PATH: LazyLock<Result<Utf8PathBuf>> = LazyLock::new(|| {
        let current = Utf8PathBuf::from_path_buf(
            env::current_exe().context("Could not get the current executable file's path")?,
        )
        .map_err(|path| {
            eyre!(
                "Could not represent the current executable file's path {path} as UTF-8",
                path = path.display()
            )
        })?;
        let exe_dir = {
            let basedir = current
                .parent()
                .ok_or_else(|| eyre!("Could not get the parent directory of {current}"))?;
            if basedir
                .file_name()
                .ok_or_else(|| eyre!("Could not get the base name of {basedir}"))?
                == "deps"
            {
                basedir
                    .parent()
                    .ok_or_else(|| eyre!("Could not get the parent directory of {basedir}"))?
            } else {
                basedir
            }
        };
        let res = exe_dir.join("install-mimic");
        if !res.is_file() {
            bail!("Expected a file at {res}");
        }
        Ok(res)
    });

    fn get_exe_path() -> Result<&'static Utf8Path> {
        match PATH.as_deref() {
            Ok(res) => Ok(res),
            Err(err) => bail!("{err}"),
        }
    }

    #[test]
    fn prove() -> Result<()> {
        println!();
        let path = get_exe_path()?;

        // See if the 'prove' command even works
        let prove_path = "prove";
        match Command::new(prove_path)
            .args(["-V"])
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit())
            .output()
        {
            Err(err) if err.kind() == IoErrorKind::NotFound => {
                println!("No '{prove_path}' command at all");
                return Ok(());
            }
            Err(err) => bail!(
                "Could not run '{prove_path}' at all: {kind}: {err}",
                kind = err.kind()
            ),
            Ok(output) if !output.status.success() => bail!("'{prove_path} -V' failed"),
            Ok(output) => {
                let contents = String::from_utf8(output.stdout).with_context(|| {
                    format!("Could not decode the output of '{prove_path} -V' as valid UTF-8")
                })?;
                println!("Got '{prove_path} -V' output {contents:?}");
                if !contents.starts_with("TAP::Harness") {
                    bail!(
                        "The output of '{prove_path} -V' did not start with 'TAP::Harness': {contents}"
                    )
                }
            }
        }

        if !Command::new(prove_path)
            .args(["-v", "t"])
            .env("INSTALL_MIMIC", path)
            .status()
            .with_context(|| format!("Could not run '{prove_path} -v t'"))?
            .success()
        {
            bail!("The TAP test suite failed");
        }
        Ok(())
    }
}