Skip to content

Commit 162a65b

Browse files
committed
Abstract getting unique status result into a single method
Removes a possibly failing assertion and some unnecessary code duplication. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent 5ece31c commit 162a65b

4 files changed

Lines changed: 28 additions & 28 deletions

File tree

src/cachedev.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, DM};
1111
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1212
use crate::result::{DmError, DmResult, ErrorEnum};
1313
use crate::shared::{
14-
device_create, device_exists, device_match, get_status_line_fields,
14+
device_create, device_exists, device_match, get_status, get_status_line_fields,
1515
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
1616
TargetTable, TargetTypeBuf,
1717
};
@@ -713,14 +713,7 @@ impl CacheDev {
713713
/// Get the current status of the cache device.
714714
pub fn status(&self, dm: &DM) -> DmResult<CacheDevStatus> {
715715
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
716-
717-
assert_eq!(
718-
status.len(),
719-
1,
720-
"Kernel must return 1 line from cache dev status"
721-
);
722-
723-
status.first().expect("assertion above holds").3.parse()
716+
get_status(&status)?.parse()
724717
}
725718
}
726719

src/shared.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,27 @@ pub fn get_status_line_fields<'a>(
245245
Ok(status_vals)
246246
}
247247

248+
/// Get unique status element from a status result.
249+
/// Return an error if an incorrect number of lines is obtained.
250+
pub fn get_status(status_lines: &[(u64, u64, String, String)]) -> DmResult<String> {
251+
let length = status_lines.len();
252+
if length != 1 {
253+
return Err(DmError::Dm(
254+
ErrorEnum::Invalid,
255+
format!(
256+
"Incorrect number of lines for status; expected 1, found {} in status result \"{}\"",
257+
length,
258+
status_lines.iter().map(|(s, l, t, v)| format!("{} {} {} {}", s, l, t.to_string(), v)).collect::<Vec<String>>().join(", ")
259+
),
260+
));
261+
}
262+
Ok(status_lines
263+
.first()
264+
.expect("if length != 1, already returned")
265+
.3
266+
.to_owned())
267+
}
268+
248269
/// Construct an error when parsing yields an unexpected value.
249270
/// Indicate the location of the unexpected value, 1-indexed, its actual
250271
/// value, and the name of the expected thing.

src/thindev.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::str::FromStr;
99
use crate::core::{DevId, Device, DeviceInfo, DmCookie, DmFlags, DmName, DmOptions, DmUuid, DM};
1010
use crate::result::{DmError, DmResult, ErrorEnum};
1111
use crate::shared::{
12-
device_create, device_exists, device_match, get_status_line_fields, message, parse_device,
13-
parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
12+
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
13+
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
1414
};
1515
use crate::thindevid::ThinDevId;
1616
use crate::thinpooldev::ThinPoolDev;
@@ -379,14 +379,7 @@ impl ThinDev {
379379
/// Get the current status of the thin device.
380380
pub fn status(&self, dm: &DM) -> DmResult<ThinStatus> {
381381
let (_, table) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
382-
383-
assert_eq!(
384-
table.len(),
385-
1,
386-
"Kernel must return 1 line table for thin status"
387-
);
388-
389-
table.first().expect("assertion above holds").3.parse()
382+
get_status(&table)?.parse()
390383
}
391384

392385
/// Set the table for the thin device's target

src/thinpooldev.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, DM};
1111
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1212
use crate::result::{DmError, DmResult, ErrorEnum};
1313
use crate::shared::{
14-
device_create, device_exists, device_match, get_status_line_fields,
14+
device_create, device_exists, device_match, get_status, get_status_line_fields,
1515
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
1616
TargetTable, TargetTypeBuf,
1717
};
@@ -547,14 +547,7 @@ impl ThinPoolDev {
547547
/// Returns an error if there was an error getting the status value.
548548
pub fn status(&self, dm: &DM) -> DmResult<ThinPoolStatus> {
549549
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
550-
551-
assert_eq!(
552-
status.len(),
553-
1,
554-
"Kernel must return 1 line from thin pool status"
555-
);
556-
557-
status.first().expect("assertion above holds").3.parse()
550+
get_status(&status)?.parse()
558551
}
559552

560553
/// Set the table for the existing metadata device.

0 commit comments

Comments
 (0)