Skip to content

Commit

Permalink
Include missing helper program name in error message
Browse files Browse the repository at this point in the history
3cpio fails with following error message if the helper like zstd, xz
etc. is not found in PATH:

```
3cpio: Error: Failed to extract content of ../tmp/bla.img: No such file or directory (os error 2)
```

This error message does not help to resolve the issue. Therefore mention
which helper program is missing. The new error message will look like:

```
3cpio: Error: Failed to extract content of ../tmp/bla.img: Program 'zstd' not found in PATH.
```

Fixes #4
  • Loading branch information
bdrung committed Aug 7, 2024
1 parent 168c591 commit 078cc6f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,17 @@ fn read_magic_header<R: Read + Seek>(file: &mut R) -> Option<Result<Command>> {

fn decompress(command: &mut Command, file: File) -> Result<ChildStdout> {
// TODO: Propper error message if spawn fails
let cmd = command.stdin(file).stdout(Stdio::piped()).spawn()?;
let cmd = command
.stdin(file)
.stdout(Stdio::piped())
.spawn()
.map_err(|e| match e.kind() {
ErrorKind::NotFound => Error::other(format!(
"Program '{}' not found in PATH.",
command.get_program().to_str().unwrap()
)),
_ => e,
})?;
// TODO: Should unwrap be replaced by returning Result?
Ok(cmd.stdout.unwrap())
}
Expand Down Expand Up @@ -914,6 +924,18 @@ mod tests {
assert_eq!(align_to_4_bytes(32), 0);
}

#[test]
fn test_decompress_program_not_found() {
let file = File::open("tests/single.cpio").expect("test cpio should be present");
let mut cmd = Command::new("non-existing-program");
let got = decompress(&mut cmd, file).unwrap_err();
assert_eq!(got.kind(), ErrorKind::Other);
assert_eq!(
got.to_string(),
"Program 'non-existing-program' not found in PATH."
);
}

#[test]
fn test_hex_str_to_u32() {
let value = hex_str_to_u32(b"000003E8").unwrap();
Expand Down

0 comments on commit 078cc6f

Please sign in to comment.