Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/bin/nydus-image/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,10 @@ impl Executor {
("blobs", None) => inspector.cmd_list_blobs(),
("prefetch", None) => inspector.cmd_list_prefetch(),
("chunk", Some(argument)) => {
let offset: u64 = argument.parse().unwrap();
let offset: u64 = argument.parse().map_err(|_| {
println!("Wrong OFFSET is specified. Is it a valid u64 value?");
ExecuteError::ArgumentParse
})?;
inspector.cmd_show_chunk(offset)
}
("icheck", Some(argument)) => {
Expand Down Expand Up @@ -759,6 +762,7 @@ impl Prompt {
Err(ExecuteError::Exit) => break,
Err(ExecuteError::IllegalCommand) => continue,
Err(ExecuteError::HelpCommand) => continue,
Err(ExecuteError::ArgumentParse) => continue,
Err(ExecuteError::ExecError(e)) => {
println!("Failed to execute command, {:?}", e);
continue;
Expand Down
52 changes: 51 additions & 1 deletion src/bin/nydus-image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ impl Command {
})?;

if let Some(c) = cmd {
let o = inspect::Executor::execute(&mut inspector, c.to_string()).unwrap();
let o = Self::execute_inspect_request(&mut inspector, c)?;
serde_json::to_writer(std::io::stdout(), &o)
.unwrap_or_else(|e| error!("Failed to serialize result, {:?}", e));
} else {
Expand All @@ -1837,6 +1837,28 @@ impl Command {
Ok(())
}

fn execute_inspect_request(
inspector: &mut inspect::RafsInspector,
request: &str,
) -> Result<Option<serde_json::Value>> {
match inspect::Executor::execute(inspector, request.to_string()) {
Ok(output) => Ok(output),
Err(inspect::ExecuteError::ExecError(e)) => {
Err(e).context(format!("failed to execute inspect request `{request}`"))
}
Err(inspect::ExecuteError::ArgumentParse) => {
bail!("invalid arguments in inspect request `{request}`")
}
Err(inspect::ExecuteError::IllegalCommand) => {
bail!("unsupported inspect request `{request}`")
}
Err(inspect::ExecuteError::HelpCommand) => {
bail!("help is not supported in inspect request mode")
}
Err(inspect::ExecuteError::Exit) => Ok(None),
}
}

fn stat(matches: &ArgMatches) -> Result<()> {
let digester = matches
.get_one::<String>("digester")
Expand Down Expand Up @@ -2315,9 +2337,37 @@ impl Command {

#[cfg(test)]
mod tests {
use std::{path::PathBuf, sync::Arc};

use super::Command;
use nydus_api::ConfigV2;

#[test]
fn test_ensure_file() {
Command::ensure_file("/dev/stdin").unwrap();
}

#[test]
fn test_execute_inspect_request_rejects_invalid_chunk_offset() {
let bootstrap =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/texture/bootstrap/rafs-v5.boot");
let mut inspector =
super::inspect::RafsInspector::new(&bootstrap, true, Arc::new(ConfigV2::default()))
.unwrap();

let err = Command::execute_inspect_request(&mut inspector, "chunk abc").unwrap_err();
assert!(err.to_string().contains("invalid arguments"));
}

#[test]
fn test_execute_inspect_request_rejects_invalid_inode() {
let bootstrap =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/texture/bootstrap/rafs-v5.boot");
let mut inspector =
super::inspect::RafsInspector::new(&bootstrap, true, Arc::new(ConfigV2::default()))
.unwrap();

let err = Command::execute_inspect_request(&mut inspector, "icheck abc").unwrap_err();
assert!(err.to_string().contains("invalid arguments"));
}
}
Loading