leodos_libcfs/os/shell.rs
1//! Safe, idiomatic wrappers for OSAL Shell command execution.
2
3use crate::error::{CfsError, OsalError, Result};
4use crate::ffi;
5use crate::os::fs::File;
6use crate::cstring;
7use crate::status::check;
8
9/// Executes a shell command and redirects its standard output to a file.
10///
11/// This function provides a safe wrapper around `OS_ShellOutputToFile`.
12///
13/// # Arguments
14/// * `command`: The shell command string to execute.
15/// * `output_file`: An open, writable `libcfs::fs::File` handle where the
16/// command's output will be written. The file must remain open for the
17/// duration of this call.
18pub fn command_to_file(command: &str, output_file: &File) -> Result<()> {
19 let c_command = cstring::<{ ffi::OS_MAX_CMD_LEN as usize }>(command)
20 .map_err(|_| CfsError::Osal(OsalError::InvalidArgument))?;
21
22 check(unsafe { ffi::OS_ShellOutputToFile(c_command.as_ptr(), output_file.id().0) })?;
23 Ok(())
24}