Skip to content

Commit 19e5e46

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 2534396 commit 19e5e46

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::{
1414
lineardev::{LinearDev, LinearDevTargetParams},
1515
result::{DmError, DmResult, ErrorEnum},
1616
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, TargetTypeBuf,
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, TargetTypeBuf,
2020
},
2121
units::{DataBlocks, MetaBlocks, Sectors},
2222
};
@@ -369,6 +369,8 @@ impl FromStr for CacheDevStatus {
369369
// Note: This method is not entirely complete. In particular, *_args values
370370
// may require more or better checking or processing.
371371
fn from_str(status_line: &str) -> DmResult<CacheDevStatus> {
372+
let status_line = get_status_line(status_line, &CACHE_TARGET_NAME)?;
373+
372374
if status_line.starts_with("Error") {
373375
return Ok(CacheDevStatus::Error);
374376
}

src/shared.rs

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

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

270290
/// 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
@@ -8,8 +8,9 @@ use crate::{
88
core::{DevId, Device, DeviceInfo, DmCookie, DmFlags, DmName, DmOptions, DmUuid, DM},
99
result::{DmError, DmResult, ErrorEnum},
1010
shared::{
11-
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
12-
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
11+
device_create, device_exists, device_match, get_status, get_status_line,
12+
get_status_line_fields, message, parse_device, parse_value, DmDevice, TargetLine,
13+
TargetParams, TargetTable, TargetTypeBuf,
1314
},
1415
thindevid::ThinDevId,
1516
thinpooldev::ThinPoolDev,
@@ -219,6 +220,8 @@ impl FromStr for ThinStatus {
219220
type Err = DmError;
220221

221222
fn from_str(status_line: &str) -> DmResult<ThinStatus> {
223+
let status_line = get_status_line(status_line, &THIN_TARGET_NAME)?;
224+
222225
if status_line.starts_with("Error") {
223226
return Ok(ThinStatus::Error);
224227
}

src/thinpooldev.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::{
99
lineardev::{LinearDev, LinearDevTargetParams},
1010
result::{DmError, DmResult, ErrorEnum},
1111
shared::{
12-
device_create, device_exists, device_match, get_status, get_status_line_fields,
13-
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
14-
TargetTable, TargetTypeBuf,
12+
device_create, device_exists, device_match, get_status, get_status_line,
13+
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
14+
TargetLine, TargetParams, TargetTable, TargetTypeBuf,
1515
},
1616
units::{DataBlocks, MetaBlocks, Sectors},
1717
};
@@ -337,6 +337,8 @@ impl FromStr for ThinPoolStatus {
337337
type Err = DmError;
338338

339339
fn from_str(status_line: &str) -> DmResult<ThinPoolStatus> {
340+
let status_line = get_status_line(status_line, &THINPOOL_TARGET_NAME)?;
341+
340342
if status_line.starts_with("Error") {
341343
return Ok(ThinPoolStatus::Error);
342344
}

0 commit comments

Comments
 (0)