src/tools/disks/zfs.rs: rename ZFSPoolStatus into ZFSPoolInfo, fix error message

This commit is contained in:
Dietmar Maurer 2020-06-17 09:08:26 +02:00
parent 3f851d1321
commit 2fd3d57490
1 changed files with 16 additions and 16 deletions

View File

@ -37,7 +37,7 @@ pub struct ZFSPoolUsage {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct ZFSPoolStatus { pub struct ZFSPoolInfo {
pub name: String, pub name: String,
pub health: String, pub health: String,
pub usage: Option<ZFSPoolUsage>, pub usage: Option<ZFSPoolUsage>,
@ -127,7 +127,7 @@ fn parse_pool_device(i: &str) -> IResult<&str, String> {
Ok((i, device.to_string())) Ok((i, device.to_string()))
} }
fn parse_pool_header(i: &str) -> IResult<&str, ZFSPoolStatus> { fn parse_zpool_list_header(i: &str) -> IResult<&str, ZFSPoolInfo> {
// name, size, allocated, free, checkpoint, expandsize, fragmentation, capacity, dedupratio, health, altroot. // name, size, allocated, free, checkpoint, expandsize, fragmentation, capacity, dedupratio, health, altroot.
let (i, (text, size, alloc, free, _, _, let (i, (text, size, alloc, free, _, _,
@ -148,14 +148,14 @@ fn parse_pool_header(i: &str) -> IResult<&str, ZFSPoolStatus> {
))(i)?; ))(i)?;
let status = if let (Some(size), Some(alloc), Some(free), Some(frag), Some(dedup)) = (size, alloc, free, frag, dedup) { let status = if let (Some(size), Some(alloc), Some(free), Some(frag), Some(dedup)) = (size, alloc, free, frag, dedup) {
ZFSPoolStatus { ZFSPoolInfo {
name: text.into(), name: text.into(),
health: health.into(), health: health.into(),
usage: Some(ZFSPoolUsage { size, alloc, free, frag, dedup }), usage: Some(ZFSPoolUsage { size, alloc, free, frag, dedup }),
devices: Vec::new(), devices: Vec::new(),
} }
} else { } else {
ZFSPoolStatus { ZFSPoolInfo {
name: text.into(), name: text.into(),
health: health.into(), health: health.into(),
usage: None, usage: None,
@ -166,9 +166,9 @@ fn parse_pool_header(i: &str) -> IResult<&str, ZFSPoolStatus> {
Ok((i, status)) Ok((i, status))
} }
fn parse_pool_status(i: &str) -> IResult<&str, ZFSPoolStatus> { fn parse_zpool_list_item(i: &str) -> IResult<&str, ZFSPoolInfo> {
let (i, mut stat) = parse_pool_header(i)?; let (i, mut stat) = parse_zpool_list_header(i)?;
let (i, devices) = many0(parse_pool_device)(i)?; let (i, devices) = many0(parse_pool_device)(i)?;
for device_path in devices.into_iter().filter(|n| n.starts_with("/dev/")) { for device_path in devices.into_iter().filter(|n| n.starts_with("/dev/")) {
@ -184,14 +184,14 @@ fn parse_pool_status(i: &str) -> IResult<&str, ZFSPoolStatus> {
/// ///
/// Note: This does not reveal any details on how the pool uses the devices, because /// Note: This does not reveal any details on how the pool uses the devices, because
/// the zpool list output format is not really defined... /// the zpool list output format is not really defined...
fn parse_zpool_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> { fn parse_zpool_list(i: &str) -> Result<Vec<ZFSPoolInfo>, Error> {
match all_consuming(many0(parse_pool_status))(i) { match all_consuming(many0(parse_zpool_list_item))(i) {
Err(nom::Err::Error(err)) | Err(nom::Err::Error(err)) |
Err(nom::Err::Failure(err)) => { Err(nom::Err::Failure(err)) => {
bail!("unable to parse zfs list output - {}", nom::error::convert_error(i, err)); bail!("unable to parse zfs list output - {}", nom::error::convert_error(i, err));
} }
Err(err) => { Err(err) => {
bail!("unable to parse calendar event: {}", err); bail!("unable to parse zfs list output - {}", err);
} }
Ok((_, ce)) => Ok(ce), Ok((_, ce)) => Ok(ce),
} }
@ -201,7 +201,7 @@ fn parse_zpool_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> {
/// ///
/// Devices are only included when run with verbose flags /// Devices are only included when run with verbose flags
/// set. Without, device lists are empty. /// set. Without, device lists are empty.
pub fn zpool_list(pool: Option<String>, verbose: bool) -> Result<Vec<ZFSPoolStatus>, Error> { pub fn zpool_list(pool: Option<String>, verbose: bool) -> Result<Vec<ZFSPoolInfo>, Error> {
// Note: zpools list verbose output can include entries for 'special', 'cache' and 'logs' // Note: zpools list verbose output can include entries for 'special', 'cache' and 'logs'
// and maybe other things. // and maybe other things.
@ -264,7 +264,7 @@ fn test_zfs_parse_list() -> Result<(), Error> {
let output = "btest 427349245952 405504 427348840448 - - 0 0 1.00 ONLINE -\n"; let output = "btest 427349245952 405504 427348840448 - - 0 0 1.00 ONLINE -\n";
let data = parse_zpool_list(&output)?; let data = parse_zpool_list(&output)?;
let expect = vec![ let expect = vec![
ZFSPoolStatus { ZFSPoolInfo {
name: "btest".to_string(), name: "btest".to_string(),
health: "ONLINE".to_string(), health: "ONLINE".to_string(),
devices: Vec::new(), devices: Vec::new(),
@ -291,7 +291,7 @@ logs
let data = parse_zpool_list(&output)?; let data = parse_zpool_list(&output)?;
let expect = vec![ let expect = vec![
ZFSPoolStatus { ZFSPoolInfo {
name: String::from("rpool"), name: String::from("rpool"),
health: String::from("ONLINE"), health: String::from("ONLINE"),
devices: vec![String::from("/dev/disk/by-id/ata-Crucial_CT500MX200SSD1_154210EB4078-part3")], devices: vec![String::from("/dev/disk/by-id/ata-Crucial_CT500MX200SSD1_154210EB4078-part3")],
@ -303,13 +303,13 @@ logs
frag: 22, frag: 22,
}), }),
}, },
ZFSPoolStatus { ZFSPoolInfo {
name: String::from("special"), name: String::from("special"),
health: String::from("-"), health: String::from("-"),
devices: vec![String::from("/dev/sda2")], devices: vec![String::from("/dev/sda2")],
usage: None, usage: None,
}, },
ZFSPoolStatus { ZFSPoolInfo {
name: String::from("logs"), name: String::from("logs"),
health: String::from("-"), health: String::from("-"),
devices: vec![String::from("/dev/sda3")], devices: vec![String::from("/dev/sda3")],
@ -333,7 +333,7 @@ logs - - - - - - - - -
let data = parse_zpool_list(&output)?; let data = parse_zpool_list(&output)?;
let expect = vec![ let expect = vec![
ZFSPoolStatus { ZFSPoolInfo {
name: String::from("btest"), name: String::from("btest"),
health: String::from("ONLINE"), health: String::from("ONLINE"),
usage: Some(ZFSPoolUsage { usage: Some(ZFSPoolUsage {
@ -350,7 +350,7 @@ logs - - - - - - - - -
String::from("/dev/sda4"), String::from("/dev/sda4"),
] ]
}, },
ZFSPoolStatus { ZFSPoolInfo {
name: String::from("logs"), name: String::from("logs"),
health: String::from("-"), health: String::from("-"),
usage: None, usage: None,