Skip to content

Commit eef99a0

Browse files
committed
Move TargetType, TargetTypeBuf out of core
Just use in outer layer. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent f5e11b3 commit eef99a0

10 files changed

Lines changed: 47 additions & 56 deletions

File tree

src/cachedev.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use std::fmt;
77
use std::path::PathBuf;
88
use std::str::FromStr;
99

10-
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, TargetTypeBuf, DM};
10+
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::{
1414
device_create, device_exists, device_match, get_status_line_fields,
1515
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
16-
TargetTable,
16+
TargetTable, TargetTypeBuf,
1717
};
1818
use crate::units::{DataBlocks, MetaBlocks, Sectors};
1919

@@ -187,9 +187,7 @@ impl fmt::Display for CacheDevTargetTable {
187187
}
188188

189189
impl TargetTable for CacheDevTargetTable {
190-
fn from_raw_table(
191-
table: &[(u64, u64, TargetTypeBuf, String)],
192-
) -> DmResult<CacheDevTargetTable> {
190+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<CacheDevTargetTable> {
193191
if table.len() != 1 {
194192
let err_msg = format!(
195193
"CacheDev table should have exactly one line, has {} lines",
@@ -205,7 +203,7 @@ impl TargetTable for CacheDevTargetTable {
205203
))
206204
}
207205

208-
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
206+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)> {
209207
to_raw_table_unique!(self)
210208
}
211209
}

src/core/dm.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use std::{cmp, slice, u32};
1010
use nix::libc::c_ulong;
1111
use nix::libc::ioctl as nix_ioctl;
1212

13-
use crate::core::{
14-
DevId, Device, DeviceInfo, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid, TargetTypeBuf,
15-
};
13+
use crate::core::{DevId, Device, DeviceInfo, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid};
1614
use crate::result::{DmError, DmResult};
1715

1816
use crate::core::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
@@ -400,7 +398,7 @@ impl DM {
400398
&self,
401399
id: &DevId,
402400
options: &DmOptions,
403-
) -> DmResult<(DeviceInfo, Vec<(u64, u64, TargetTypeBuf, String)>)> {
401+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, String, String)>)> {
404402
let mut hdr = options.to_ioctl_hdr(Some(id), DmFlags::DM_QUERY_INACTIVE_TABLE);
405403

406404
let data_out = self.do_ioctl(dmi::DM_DEV_WAIT_CMD as u8, &mut hdr, None)?;
@@ -422,15 +420,15 @@ impl DM {
422420
/// # Example
423421
///
424422
/// ```no_run
425-
/// use devicemapper::{DM, DevId, DmName, TargetTypeBuf};
423+
/// use devicemapper::{DM, DevId, DmName};
426424
/// let dm = DM::new().unwrap();
427425
///
428426
/// // Create a 16MiB device (32768 512-byte sectors) that maps to /dev/sdb1
429427
/// // starting 1MiB into sdb1
430428
/// let table = vec![(
431429
/// 0,
432430
/// 32768,
433-
/// TargetTypeBuf::new("linear".into()).expect("valid"),
431+
/// "linear".into(),
434432
/// "/dev/sdb1 2048".into()
435433
/// )];
436434
///
@@ -441,7 +439,7 @@ impl DM {
441439
pub fn table_load(
442440
&self,
443441
id: &DevId,
444-
targets: &[(u64, u64, TargetTypeBuf, String)],
442+
targets: &[(u64, u64, String, String)],
445443
) -> DmResult<DeviceInfo> {
446444
let mut targs = Vec::new();
447445

@@ -547,13 +545,15 @@ impl DM {
547545
/// Panics if there is an error parsing the table.
548546
/// Trims trailing white space off final entry on each line. This
549547
/// canonicalization makes checking identity of tables easier.
548+
/// Postcondition: The length of the next to last entry in any tuple is
549+
/// no more than 16 characters.
550550
// Justification: If the ioctl succeeded, the data is correct and
551551
// complete. An error in parsing can only result from a change in the
552552
// kernel. We rely on DM's interface versioning system. Kernel changes
553553
// will either be backwards-compatible, or will increment
554554
// DM_VERSION_MAJOR. Since calls made with a non-matching major version
555555
// will fail, this protects against callers parsing unknown formats.
556-
fn parse_table_status(count: u32, buf: &[u8]) -> Vec<(u64, u64, TargetTypeBuf, String)> {
556+
fn parse_table_status(count: u32, buf: &[u8]) -> Vec<(u64, u64, String, String)> {
557557
let mut targets = Vec::new();
558558
if !buf.is_empty() {
559559
let mut next_off = 0;
@@ -579,12 +579,7 @@ impl DM {
579579
String::from_utf8_lossy(slc).trim_end().to_owned()
580580
};
581581

582-
targets.push((
583-
targ.sector_start,
584-
targ.length,
585-
TargetTypeBuf::new(target_type).expect("< sizeof target_spec"),
586-
params,
587-
));
582+
targets.push((targ.sector_start, targ.length, target_type, params));
588583

589584
next_off = targ.next as usize;
590585
}
@@ -625,7 +620,7 @@ impl DM {
625620
&self,
626621
id: &DevId,
627622
options: &DmOptions,
628-
) -> DmResult<(DeviceInfo, Vec<(u64, u64, TargetTypeBuf, String)>)> {
623+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, String, String)>)> {
629624
let mut hdr = options.to_ioctl_hdr(
630625
Some(id),
631626
DmFlags::DM_NOFLUSH | DmFlags::DM_STATUS_TABLE | DmFlags::DM_QUERY_INACTIVE_TABLE,

src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ pub use self::deviceinfo::DeviceInfo;
1919
pub use self::dm::DM;
2020
pub use self::dm_flags::{DmCookie, DmFlags};
2121
pub use self::dm_options::DmOptions;
22-
pub use self::types::{DevId, DmName, DmNameBuf, DmUuid, DmUuidBuf, TargetType, TargetTypeBuf};
22+
pub use self::types::{DevId, DmName, DmNameBuf, DmUuid, DmUuidBuf};

src/core/types.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,3 @@ impl<'a> fmt::Display for DevId<'a> {
3838
}
3939
}
4040
}
41-
42-
/// Number of bytes in Struct_dm_target_spec::target_type field.
43-
const DM_TARGET_TYPE_LEN: usize = 16;
44-
45-
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN);

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ pub use crate::cachedev::{
133133
pub use crate::consts::IEC;
134134
pub use crate::core::{
135135
devnode_to_devno, DevId, Device, DmCookie, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid,
136-
DmUuidBuf, TargetType, TargetTypeBuf, DM,
136+
DmUuidBuf, DM,
137137
};
138138
pub use crate::lineardev::{
139139
FlakeyTargetParams, LinearDev, LinearDevTargetParams, LinearDevTargetTable, LinearTargetParams,
140140
};
141141
pub use crate::result::{DmError, DmResult, ErrorEnum};
142-
pub use crate::shared::{device_exists, DmDevice, TargetLine};
142+
pub use crate::shared::{device_exists, DmDevice, TargetLine, TargetType, TargetTypeBuf};
143143
pub use crate::thindev::{ThinDev, ThinDevWorkingStatus, ThinStatus};
144144
pub use crate::thindevid::ThinDevId;
145145
pub use crate::thinpooldev::{

src/lineardev.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::fmt;
77
use std::path::PathBuf;
88
use std::str::FromStr;
99

10-
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, TargetTypeBuf, DM};
10+
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, DM};
1111
use crate::result::{DmError, DmResult, ErrorEnum};
1212
use crate::shared::{
1313
device_create, device_exists, device_match, parse_device, parse_value, DmDevice, TargetLine,
14-
TargetParams, TargetTable,
14+
TargetParams, TargetTable, TargetTypeBuf,
1515
};
1616
use crate::units::Sectors;
1717

@@ -323,31 +323,29 @@ impl fmt::Display for LinearDevTargetTable {
323323
}
324324

325325
impl TargetTable for LinearDevTargetTable {
326-
fn from_raw_table(
327-
table: &[(u64, u64, TargetTypeBuf, String)],
328-
) -> DmResult<LinearDevTargetTable> {
326+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<LinearDevTargetTable> {
329327
Ok(LinearDevTargetTable {
330328
table: table
331329
.iter()
332330
.map(|x| -> DmResult<TargetLine<LinearDevTargetParams>> {
333331
Ok(TargetLine::new(
334332
Sectors(x.0),
335333
Sectors(x.1),
336-
format!("{} {}", x.2.to_string(), x.3).parse::<LinearDevTargetParams>()?,
334+
format!("{} {}", x.2, x.3).parse::<LinearDevTargetParams>()?,
337335
))
338336
})
339337
.collect::<DmResult<Vec<_>>>()?,
340338
})
341339
}
342340

343-
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
341+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)> {
344342
self.table
345343
.iter()
346344
.map(|x| {
347345
(
348346
*x.start,
349347
*x.length,
350-
x.params.target_type(),
348+
x.params.target_type().to_string(),
351349
x.params.param_str(),
352350
)
353351
})

src/shared.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@
66
// devices.
77

88
use std::fmt;
9+
10+
use std::borrow::Borrow;
11+
use std::ops::Deref;
912
use std::path::{Path, PathBuf};
1013
use std::str::FromStr;
1114

1215
use crate::core::{
13-
devnode_to_devno, DevId, Device, DeviceInfo, DmFlags, DmName, DmOptions, DmUuid, TargetTypeBuf,
14-
DM,
16+
devnode_to_devno, DevId, Device, DeviceInfo, DmFlags, DmName, DmOptions, DmUuid, DM,
1517
};
1618
use crate::result::{DmError, DmResult, ErrorEnum};
1719
use crate::units::Sectors;
1820

21+
use crate::core::errors::ErrorKind;
22+
23+
/// Number of bytes in Struct_dm_target_spec::target_type field.
24+
const DM_TARGET_TYPE_LEN: usize = 16;
25+
26+
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN);
27+
1928
/// The trait for properties of the params string of TargetType
2029
pub trait TargetParams: Clone + fmt::Debug + fmt::Display + Eq + FromStr + PartialEq {
2130
/// Return the param string only
@@ -49,10 +58,10 @@ impl<T: TargetParams> TargetLine<T> {
4958

5059
pub trait TargetTable: Clone + fmt::Debug + fmt::Display + Eq + PartialEq + Sized {
5160
/// Constructs a table from a raw table returned by DM::table_status()
52-
fn from_raw_table(table: &[(u64, u64, TargetTypeBuf, String)]) -> DmResult<Self>;
61+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<Self>;
5362

5463
/// Generates a table that can be loaded by DM::table_load()
55-
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)>;
64+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)>;
5665
}
5766

5867
/// A trait capturing some shared properties of DM devices.

src/shared_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ macro_rules! to_raw_table_unique {
3636
vec![(
3737
*$s.table.start,
3838
*$s.table.length,
39-
$s.table.params.target_type(),
39+
$s.table.params.target_type().to_string(),
4040
$s.table.params.param_str(),
4141
)]
4242
};

src/thindev.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ use std::fmt;
66
use std::path::PathBuf;
77
use std::str::FromStr;
88

9-
use crate::core::{
10-
DevId, Device, DeviceInfo, DmCookie, DmFlags, DmName, DmOptions, DmUuid, TargetTypeBuf, DM,
11-
};
9+
use crate::core::{DevId, Device, DeviceInfo, DmCookie, DmFlags, DmName, DmOptions, DmUuid, DM};
1210
use crate::result::{DmError, DmResult, ErrorEnum};
1311
use crate::shared::{
1412
device_create, device_exists, device_match, get_status_line_fields, message, parse_device,
15-
parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
13+
parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
1614
};
1715
use crate::thindevid::ThinDevId;
1816
use crate::thinpooldev::ThinPoolDev;
@@ -118,7 +116,7 @@ impl fmt::Display for ThinDevTargetTable {
118116
}
119117

120118
impl TargetTable for ThinDevTargetTable {
121-
fn from_raw_table(table: &[(u64, u64, TargetTypeBuf, String)]) -> DmResult<ThinDevTargetTable> {
119+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<ThinDevTargetTable> {
122120
if table.len() != 1 {
123121
let err_msg = format!(
124122
"ThinDev table should have exactly one line, has {} lines",
@@ -130,11 +128,11 @@ impl TargetTable for ThinDevTargetTable {
130128
Ok(ThinDevTargetTable::new(
131129
Sectors(line.0),
132130
Sectors(line.1),
133-
format!("{} {}", line.2.to_string(), line.3).parse::<ThinTargetParams>()?,
131+
format!("{} {}", line.2, line.3).parse::<ThinTargetParams>()?,
134132
))
135133
}
136134

137-
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
135+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)> {
138136
to_raw_table_unique!(self)
139137
}
140138
}

src/thinpooldev.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use std::fmt;
77
use std::path::PathBuf;
88
use std::str::FromStr;
99

10-
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, TargetTypeBuf, DM};
10+
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::{
1414
device_create, device_exists, device_match, get_status_line_fields,
1515
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
16-
TargetTable,
16+
TargetTable, TargetTypeBuf,
1717
};
1818
use crate::units::{DataBlocks, MetaBlocks, Sectors};
1919

@@ -158,9 +158,7 @@ impl fmt::Display for ThinPoolDevTargetTable {
158158
}
159159

160160
impl TargetTable for ThinPoolDevTargetTable {
161-
fn from_raw_table(
162-
table: &[(u64, u64, TargetTypeBuf, String)],
163-
) -> DmResult<ThinPoolDevTargetTable> {
161+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<ThinPoolDevTargetTable> {
164162
if table.len() != 1 {
165163
let err_msg = format!(
166164
"ThinPoolDev table should have exactly one line, has {} lines",
@@ -172,11 +170,11 @@ impl TargetTable for ThinPoolDevTargetTable {
172170
Ok(ThinPoolDevTargetTable::new(
173171
Sectors(line.0),
174172
Sectors(line.1),
175-
format!("{} {}", line.2.to_string(), line.3).parse::<ThinPoolTargetParams>()?,
173+
format!("{} {}", line.2, line.3).parse::<ThinPoolTargetParams>()?,
176174
))
177175
}
178176

179-
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
177+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)> {
180178
to_raw_table_unique!(self)
181179
}
182180
}

0 commit comments

Comments
 (0)