Skip to content

Commit 6fdec98

Browse files
committed
Combine target type value with status to be tested
This brings it into line with params, and is a bit better encapsulated. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent ffe7cc9 commit 6fdec98

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

src/cachedev.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ 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, get_status_line_fields,
15-
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
16-
TargetTable, TargetTypeBuf,
14+
device_create, device_exists, device_match, get_status, get_status_line,
15+
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
16+
TargetLine, TargetParams, TargetTable, TargetTypeBuf,
1717
};
1818
use crate::units::{DataBlocks, MetaBlocks, Sectors};
1919

@@ -363,6 +363,8 @@ impl FromStr for CacheDevStatus {
363363
// Note: This method is not entirely complete. In particular, *_args values
364364
// may require more or better checking or processing.
365365
fn from_str(status_line: &str) -> DmResult<CacheDevStatus> {
366+
let status_line = get_status_line(status_line, &CACHE_TARGET_NAME)?;
367+
366368
if status_line.starts_with("Fail") {
367369
return Ok(CacheDevStatus::Fail);
368370
}

src/shared.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,27 @@ where
225225
})
226226
}
227227

228+
/// Obtain a status line from a line containing the target type as its first
229+
/// entry. Return an error if the line does not have the expected target type.
230+
/// Precondition: The line's first entry is the target type, followed by a
231+
/// space, followed by the actual status values.
232+
pub fn get_status_line<'a, 'b>(
233+
status_line: &'a str,
234+
expected_target_type: &'b str,
235+
) -> DmResult<&'a str> {
236+
let target_status_pair: Vec<&str> = status_line.splitn(2, ' ').collect();
237+
let target_type = target_status_pair[0];
238+
if target_type != expected_target_type {
239+
let err_msg = format!(
240+
"Expected a \"{}\" target entry but found target type \"{}\" in \"{}\"",
241+
expected_target_type, target_type, status_line
242+
);
243+
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
244+
}
245+
246+
Ok(target_status_pair[1])
247+
}
248+
228249
/// Get fields for a single status line.
229250
/// Return an error if an insufficient number of fields are obtained.
230251
pub fn get_status_line_fields<'a>(
@@ -259,11 +280,10 @@ pub fn get_status(status_lines: &[(u64, u64, String, String)]) -> DmResult<Strin
259280
),
260281
));
261282
}
262-
Ok(status_lines
283+
let line = status_lines
263284
.first()
264-
.expect("if length != 1, already returned")
265-
.3
266-
.to_owned())
285+
.expect("if length != 1, already returned");
286+
Ok(format!("{} {}", line.2, line.3))
267287
}
268288

269289
/// Construct an error when parsing yields an unexpected value.

src/thindev.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ 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, get_status_line_fields, message,
13-
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
12+
device_create, device_exists, device_match, get_status, get_status_line,
13+
get_status_line_fields, message, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
14+
TargetTable, TargetTypeBuf,
1415
};
1516
use crate::thindevid::ThinDevId;
1617
use crate::thinpooldev::ThinPoolDev;
@@ -217,6 +218,8 @@ impl FromStr for ThinStatus {
217218
type Err = DmError;
218219

219220
fn from_str(status_line: &str) -> DmResult<ThinStatus> {
221+
let status_line = get_status_line(status_line, &THIN_TARGET_NAME)?;
222+
220223
if status_line.starts_with("Fail") {
221224
return Ok(ThinStatus::Fail);
222225
}

src/thinpooldev.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ 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, get_status_line_fields,
15-
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
16-
TargetTable, TargetTypeBuf,
14+
device_create, device_exists, device_match, get_status, get_status_line,
15+
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
16+
TargetLine, TargetParams, TargetTable, TargetTypeBuf,
1717
};
1818
use crate::units::{DataBlocks, MetaBlocks, Sectors};
1919

@@ -335,6 +335,8 @@ impl FromStr for ThinPoolStatus {
335335
type Err = DmError;
336336

337337
fn from_str(status_line: &str) -> DmResult<ThinPoolStatus> {
338+
let status_line = get_status_line(status_line, &THINPOOL_TARGET_NAME)?;
339+
338340
if status_line.starts_with("Fail") {
339341
return Ok(ThinPoolStatus::Fail);
340342
}

0 commit comments

Comments
 (0)