Skip to content

Commit 5ece31c

Browse files
authored
Merge pull request #421 from mulkieran/master-core-module
Make a core sub-module
2 parents 7d534a5 + e052a14 commit 5ece31c

23 files changed

Lines changed: 191 additions & 198 deletions

src/cachedev.rs

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

10-
use crate::device::Device;
11-
use crate::deviceinfo::DeviceInfo;
12-
use crate::dm::DM;
13-
use crate::dm_options::DmOptions;
10+
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, DM};
1411
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1512
use crate::result::{DmError, DmResult, ErrorEnum};
1613
use crate::shared::{
1714
device_create, device_exists, device_match, get_status_line_fields,
1815
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
19-
TargetTable,
16+
TargetTable, TargetTypeBuf,
2017
};
21-
use crate::types::{DataBlocks, DevId, DmName, DmUuid, MetaBlocks, Sectors, TargetTypeBuf};
18+
use crate::units::{DataBlocks, MetaBlocks, Sectors};
2219

2320
const CACHE_TARGET_NAME: &str = "cache";
2421

@@ -190,9 +187,7 @@ impl fmt::Display for CacheDevTargetTable {
190187
}
191188

192189
impl TargetTable for CacheDevTargetTable {
193-
fn from_raw_table(
194-
table: &[(Sectors, Sectors, TargetTypeBuf, String)],
195-
) -> DmResult<CacheDevTargetTable> {
190+
fn from_raw_table(table: &[(u64, u64, String, String)]) -> DmResult<CacheDevTargetTable> {
196191
if table.len() != 1 {
197192
let err_msg = format!(
198193
"CacheDev table should have exactly one line, has {} lines",
@@ -202,13 +197,13 @@ impl TargetTable for CacheDevTargetTable {
202197
}
203198
let line = table.first().expect("table.len() == 1");
204199
Ok(CacheDevTargetTable::new(
205-
line.0,
206-
line.1,
200+
Sectors(line.0),
201+
Sectors(line.1),
207202
format!("{} {}", line.2.to_string(), line.3).parse::<CacheTargetParams>()?,
208203
))
209204
}
210205

211-
fn to_raw_table(&self) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)> {
206+
fn to_raw_table(&self) -> Vec<(u64, u64, String, String)> {
212207
to_raw_table_unique!(self)
213208
}
214209
}
@@ -737,7 +732,7 @@ use std::path::Path;
737732
#[cfg(test)]
738733
use crate::consts::IEC;
739734
#[cfg(test)]
740-
use crate::device::devnode_to_devno;
735+
use crate::core::devnode_to_devno;
741736
#[cfg(test)]
742737
use crate::lineardev::LinearTargetParams;
743738
#[cfg(test)]

src/consts.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
/// disk sector size in bytes
6-
pub const SECTOR_SIZE: usize = 512;
7-
85
#[allow(non_upper_case_globals)]
96
#[allow(non_snake_case)]
107
/// International Electrotechnical Commission Units Standards

src/device.rs renamed to src/core/device.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use std::{fmt, io};
1010
use libc::{dev_t, major, makedev, minor};
1111
use nix::sys::stat::SFlag;
1212

13-
use crate::errors::ErrorKind;
1413
use crate::result::{DmError, DmResult};
1514

15+
use crate::core::errors::ErrorKind;
16+
1617
/// A struct containing the device's major and minor numbers
1718
///
1819
/// Also allows conversion to/from a single 64bit dev_t value.
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
use std::str::from_utf8;
55

6-
use crate::device::Device;
7-
use crate::dm_flags::DmFlags;
8-
use crate::dm_ioctl as dmi;
9-
use crate::types::{DmName, DmUuid};
10-
use crate::util::slice_to_null;
6+
use crate::core::{Device, DmFlags, DmName, DmUuid};
7+
8+
use crate::core::dm_ioctl as dmi;
9+
use crate::core::util::slice_to_null;
1110

1211
/// Name max length
1312
pub const DM_NAME_LEN: usize = 128;

src/dm.rs renamed to src/core/dm.rs

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

13-
use crate::device::Device;
14-
use crate::deviceinfo::{DeviceInfo, DM_NAME_LEN, DM_UUID_LEN};
15-
use crate::dm_flags::DmFlags;
16-
use crate::dm_ioctl as dmi;
17-
use crate::dm_options::DmOptions;
18-
use crate::errors::ErrorKind;
13+
use crate::core::{DevId, Device, DeviceInfo, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid};
1914
use crate::result::{DmError, DmResult};
2015

21-
use crate::types::{DevId, DmName, DmNameBuf, DmUuid, Sectors, TargetTypeBuf};
22-
use crate::util::{align_to, slice_to_null};
16+
use crate::core::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
17+
use crate::core::dm_ioctl as dmi;
18+
use crate::core::errors::ErrorKind;
19+
use crate::core::util::{align_to, slice_to_null};
2320

2421
/// Indicator to send IOCTL to DM
2522
const DM_IOCTL: u8 = 0xfd;
@@ -401,7 +398,7 @@ impl DM {
401398
&self,
402399
id: &DevId,
403400
options: &DmOptions,
404-
) -> DmResult<(DeviceInfo, Vec<(Sectors, Sectors, TargetTypeBuf, String)>)> {
401+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, String, String)>)> {
405402
let mut hdr = options.to_ioctl_hdr(Some(id), DmFlags::DM_QUERY_INACTIVE_TABLE);
406403

407404
let data_out = self.do_ioctl(dmi::DM_DEV_WAIT_CMD as u8, &mut hdr, None)?;
@@ -423,15 +420,15 @@ impl DM {
423420
/// # Example
424421
///
425422
/// ```no_run
426-
/// use devicemapper::{DM, DevId, DmName, Sectors, TargetTypeBuf};
423+
/// use devicemapper::{DM, DevId, DmName};
427424
/// let dm = DM::new().unwrap();
428425
///
429426
/// // Create a 16MiB device (32768 512-byte sectors) that maps to /dev/sdb1
430427
/// // starting 1MiB into sdb1
431428
/// let table = vec![(
432-
/// Sectors(0),
433-
/// Sectors(32768),
434-
/// TargetTypeBuf::new("linear".into()).expect("valid"),
429+
/// 0,
430+
/// 32768,
431+
/// "linear".into(),
435432
/// "/dev/sdb1 2048".into()
436433
/// )];
437434
///
@@ -442,16 +439,16 @@ impl DM {
442439
pub fn table_load(
443440
&self,
444441
id: &DevId,
445-
targets: &[(Sectors, Sectors, TargetTypeBuf, String)],
442+
targets: &[(u64, u64, String, String)],
446443
) -> DmResult<DeviceInfo> {
447444
let mut targs = Vec::new();
448445

449446
// Construct targets first, since we need to know how many & size
450447
// before initializing the header.
451448
for t in targets {
452449
let mut targ: dmi::Struct_dm_target_spec = Default::default();
453-
targ.sector_start = *t.0;
454-
targ.length = *t.1;
450+
targ.sector_start = t.0;
451+
targ.length = t.1;
455452
targ.status = 0;
456453

457454
let dst: &mut [u8] = unsafe { &mut *(&mut targ.target_type[..] as *mut [u8]) };
@@ -548,16 +545,15 @@ impl DM {
548545
/// Panics if there is an error parsing the table.
549546
/// Trims trailing white space off final entry on each line. This
550547
/// 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.
551550
// Justification: If the ioctl succeeded, the data is correct and
552551
// complete. An error in parsing can only result from a change in the
553552
// kernel. We rely on DM's interface versioning system. Kernel changes
554553
// will either be backwards-compatible, or will increment
555554
// DM_VERSION_MAJOR. Since calls made with a non-matching major version
556555
// will fail, this protects against callers parsing unknown formats.
557-
fn parse_table_status(
558-
count: u32,
559-
buf: &[u8],
560-
) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)> {
556+
fn parse_table_status(count: u32, buf: &[u8]) -> Vec<(u64, u64, String, String)> {
561557
let mut targets = Vec::new();
562558
if !buf.is_empty() {
563559
let mut next_off = 0;
@@ -583,12 +579,7 @@ impl DM {
583579
String::from_utf8_lossy(slc).trim_end().to_owned()
584580
};
585581

586-
targets.push((
587-
Sectors(targ.sector_start),
588-
Sectors(targ.length),
589-
TargetTypeBuf::new(target_type).expect("< sizeof target_spec"),
590-
params,
591-
));
582+
targets.push((targ.sector_start, targ.length, target_type, params));
592583

593584
next_off = targ.next as usize;
594585
}
@@ -629,7 +620,7 @@ impl DM {
629620
&self,
630621
id: &DevId,
631622
options: &DmOptions,
632-
) -> DmResult<(DeviceInfo, Vec<(Sectors, Sectors, TargetTypeBuf, String)>)> {
623+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, String, String)>)> {
633624
let mut hdr = options.to_ioctl_hdr(
634625
Some(id),
635626
DmFlags::DM_NOFLUSH | DmFlags::DM_STATUS_TABLE | DmFlags::DM_QUERY_INACTIVE_TABLE,
@@ -683,13 +674,13 @@ impl DM {
683674
pub fn target_msg(
684675
&self,
685676
id: &DevId,
686-
sector: Option<Sectors>,
677+
sector: Option<u64>,
687678
msg: &str,
688679
) -> DmResult<(DeviceInfo, Option<String>)> {
689680
let mut hdr = DmOptions::new().to_ioctl_hdr(Some(id), DmFlags::empty());
690681

691682
let mut msg_struct: dmi::Struct_dm_target_msg = Default::default();
692-
msg_struct.sector = *sector.unwrap_or_default();
683+
msg_struct.sector = sector.unwrap_or_default();
693684
let mut data_in = unsafe {
694685
let ptr = &msg_struct as *const dmi::Struct_dm_target_msg as *mut u8;
695686
slice::from_raw_parts(ptr, size_of::<dmi::Struct_dm_target_msg>()).to_vec()
@@ -723,10 +714,11 @@ impl DM {
723714
#[cfg(test)]
724715
mod tests {
725716

726-
use crate::errors::Error;
727717
use crate::result::DmError;
728718
use crate::test_lib::{test_name, test_uuid};
729719

720+
use crate::core::errors::Error;
721+
730722
use super::*;
731723

732724
#[test]

src/dm_flags.rs renamed to src/core/dm_flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
use crate::dm_ioctl as dmi;
5+
use crate::core::dm_ioctl as dmi;
66

77
bitflags! {
88
/// Flags used by devicemapper.

src/dm_ioctl.rs renamed to src/core/dm_ioctl.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use std::borrow::Cow;
1111
use std::fmt;
1212

13-
use crate::device::Device;
14-
use crate::util::slice_to_null;
13+
use crate::core::Device;
14+
15+
use crate::core::util::slice_to_null;
1516

1617
pub type __s8 = ::libc::c_char;
1718
pub type __u8 = ::libc::c_uchar;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4-
use crate::dm_flags::{DmCookie, DmFlags};
4+
use crate::core::{DmCookie, DmFlags};
55

66
/// Encapsulates options for device mapper calls
77
#[derive(Debug, Default, Clone)]

src/errors.rs renamed to src/core/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77

88
use nix;
99

10-
use crate::deviceinfo::DeviceInfo;
10+
use crate::core::DeviceInfo;
1111

1212
error_chain! {
1313
errors {

src/core/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
//! Modules that support handling of devicemapper ioctls at a low-level.
6+
7+
mod device;
8+
pub(self) mod deviceinfo;
9+
mod dm;
10+
mod dm_flags;
11+
pub(self) mod dm_ioctl;
12+
mod dm_options;
13+
pub mod errors;
14+
mod types;
15+
pub(self) mod util;
16+
17+
pub use self::device::{devnode_to_devno, Device};
18+
pub use self::deviceinfo::DeviceInfo;
19+
pub use self::dm::DM;
20+
pub use self::dm_flags::{DmCookie, DmFlags};
21+
pub use self::dm_options::DmOptions;
22+
pub use self::types::{DevId, DmName, DmNameBuf, DmUuid, DmUuidBuf};

0 commit comments

Comments
 (0)