use super::{cmd::execute_rfs_command, error::RfsError, types::Mount};
pub fn list_mounts() -> Result<Vec<Mount>, RfsError> {
let result = execute_rfs_command(&["list", "--json"])?;
match serde_json::from_str::<serde_json::Value>(&result.stdout) {
Ok(json) => {
if let serde_json::Value::Array(mounts_json) = json {
let mut mounts = Vec::new();
for mount_json in mounts_json {
let id = match mount_json.get("id").and_then(|v| v.as_str()) {
Some(id) => id.to_string(),
None => return Err(RfsError::ListFailed("Missing mount ID".to_string())),
};
let source = match mount_json.get("source").and_then(|v| v.as_str()) {
Some(source) => source.to_string(),
None => return Err(RfsError::ListFailed("Missing source".to_string())),
};
let target = match mount_json.get("target").and_then(|v| v.as_str()) {
Some(target) => target.to_string(),
None => return Err(RfsError::ListFailed("Missing target".to_string())),
};
let fs_type = match mount_json.get("type").and_then(|v| v.as_str()) {
Some(fs_type) => fs_type.to_string(),
None => {
return Err(RfsError::ListFailed("Missing filesystem type".to_string()))
}
};
let options = match mount_json.get("options").and_then(|v| v.as_array()) {
Some(options_array) => {
let mut options_vec = Vec::new();
for option_value in options_array {
if let Some(option_str) = option_value.as_str() {
options_vec.push(option_str.to_string());
}
}
options_vec
}
None => Vec::new(), };
mounts.push(Mount {
id,
source,
target,
fs_type,
options,
});
}
Ok(mounts)
} else {
Err(RfsError::ListFailed("Expected JSON array".to_string()))
}
}
Err(e) => Err(RfsError::ListFailed(format!(
"Failed to parse mount list JSON: {}",
e
))),
}
}
pub fn unmount(target: &str) -> Result<(), RfsError> {
let result = execute_rfs_command(&["unmount", target])?;
if !result.success {
return Err(RfsError::UnmountFailed(format!(
"Failed to unmount {}: {}",
target, result.stderr
)));
}
Ok(())
}
pub fn unmount_all() -> Result<(), RfsError> {
let result = execute_rfs_command(&["unmount", "--all"])?;
if !result.success {
return Err(RfsError::UnmountFailed(format!(
"Failed to unmount all filesystems: {}",
result.stderr
)));
}
Ok(())
}
pub fn get_mount_info(target: &str) -> Result<Mount, RfsError> {
let mounts = list_mounts()?;
for mount in mounts {
if mount.target == target {
return Ok(mount);
}
}
Err(RfsError::Other(format!("No mount found at {}", target)))
}