Skip to content

Commit e381dd2

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 48c3647 commit e381dd2

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
@@ -14,9 +14,9 @@ use crate::dm_options::DmOptions;
1414
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1515
use crate::result::{DmError, DmResult, ErrorEnum};
1616
use crate::shared::{
17-
device_create, device_exists, device_match, get_status, get_status_line_fields,
18-
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
19-
TargetTable,
17+
device_create, device_exists, device_match, get_status, get_status_line,
18+
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
19+
TargetLine, TargetParams, TargetTable,
2020
};
2121
use crate::types::{DataBlocks, DevId, DmName, DmUuid, MetaBlocks, Sectors, TargetTypeBuf};
2222

@@ -368,6 +368,8 @@ impl FromStr for CacheDevStatus {
368368
// Note: This method is not entirely complete. In particular, *_args values
369369
// may require more or better checking or processing.
370370
fn from_str(status_line: &str) -> DmResult<CacheDevStatus> {
371+
let status_line = get_status_line(status_line, &CACHE_TARGET_NAME)?;
372+
371373
if status_line.starts_with("Fail") {
372374
return Ok(CacheDevStatus::Fail);
373375
}

src/shared.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,27 @@ where
215215
})
216216
}
217217

218+
/// Obtain a status line from a line containing the target type as its first
219+
/// entry. Return an error if the line does not have the expected target type.
220+
/// Precondition: The line's first entry is the target type, followed by a
221+
/// space, followed by the actual status values.
222+
pub fn get_status_line<'a, 'b>(
223+
status_line: &'a str,
224+
expected_target_type: &'b str,
225+
) -> DmResult<&'a str> {
226+
let target_status_pair: Vec<&str> = status_line.splitn(2, ' ').collect();
227+
let target_type = target_status_pair[0];
228+
if target_type != expected_target_type {
229+
let err_msg = format!(
230+
"Expected a \"{}\" target entry but found target type \"{}\" in \"{}\"",
231+
expected_target_type, target_type, status_line
232+
);
233+
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
234+
}
235+
236+
Ok(target_status_pair[1])
237+
}
238+
218239
/// Get fields for a single status line.
219240
/// Return an error if an insufficient number of fields are obtained.
220241
pub fn get_status_line_fields<'a>(
@@ -249,11 +270,10 @@ pub fn get_status(status_lines: &[(Sectors, Sectors, TargetTypeBuf, String)]) ->
249270
),
250271
));
251272
}
252-
Ok(status_lines
273+
let line = status_lines
253274
.first()
254-
.expect("if length != 1, already returned")
255-
.3
256-
.to_owned())
275+
.expect("if length != 1, already returned");
276+
Ok(format!("{} {}", line.2.to_string(), line.3))
257277
}
258278

259279
/// 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
@@ -13,8 +13,9 @@ use crate::dm_flags::{DmCookie, DmFlags};
1313
use crate::dm_options::DmOptions;
1414
use crate::result::{DmError, DmResult, ErrorEnum};
1515
use crate::shared::{
16-
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
17-
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
16+
device_create, device_exists, device_match, get_status, get_status_line,
17+
get_status_line_fields, message, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
18+
TargetTable,
1819
};
1920
use crate::thindevid::ThinDevId;
2021
use crate::thinpooldev::ThinPoolDev;
@@ -223,6 +224,8 @@ impl FromStr for ThinStatus {
223224
type Err = DmError;
224225

225226
fn from_str(status_line: &str) -> DmResult<ThinStatus> {
227+
let status_line = get_status_line(status_line, &THIN_TARGET_NAME)?;
228+
226229
if status_line.starts_with("Fail") {
227230
return Ok(ThinStatus::Fail);
228231
}

src/thinpooldev.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use crate::dm_options::DmOptions;
1414
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1515
use crate::result::{DmError, DmResult, ErrorEnum};
1616
use crate::shared::{
17-
device_create, device_exists, device_match, get_status, get_status_line_fields,
18-
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
19-
TargetTable,
17+
device_create, device_exists, device_match, get_status, get_status_line,
18+
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
19+
TargetLine, TargetParams, TargetTable,
2020
};
2121
use crate::types::{DataBlocks, DevId, DmName, DmUuid, MetaBlocks, Sectors, TargetTypeBuf};
2222

@@ -340,6 +340,8 @@ impl FromStr for ThinPoolStatus {
340340
type Err = DmError;
341341

342342
fn from_str(status_line: &str) -> DmResult<ThinPoolStatus> {
343+
let status_line = get_status_line(status_line, &THINPOOL_TARGET_NAME)?;
344+
343345
if status_line.starts_with("Fail") {
344346
return Ok(ThinPoolStatus::Fail);
345347
}

0 commit comments

Comments
 (0)