Add a flag for non-removable devices (fix #24)

This commit is contained in:
Roey Darwish Dror
2019-08-13 21:43:55 +03:00
parent 25cdc44c7c
commit 750653c222
8 changed files with 56 additions and 25 deletions

View File

@@ -12,5 +12,5 @@ pub use filesystem::{Filesystem, FilesystemType};
pub use loop_device::LoopDevice;
pub use markers::BlockDevice;
pub use mount_stack::MountStack;
pub use removeable_devices::get_removable_devices;
pub use removeable_devices::get_storage_devices;
pub use storage_device::StorageDevice;

View File

@@ -27,22 +27,24 @@ fn trimmed(source: String) -> String {
String::from(source.trim_end())
}
pub fn get_removable_devices() -> Result<Vec<Device>, Error> {
pub fn get_storage_devices(allow_non_removable: bool) -> Result<Vec<Device>, Error> {
let mut result = Vec::new();
for entry in fs::read_dir("/sys/block").context(ErrorKind::RemoveableDevicesQuery)? {
let entry = entry.context(ErrorKind::RemoveableDevicesQuery)?;
for entry in fs::read_dir("/sys/block").context(ErrorKind::StorageDevicesQuery)? {
let entry = entry.context(ErrorKind::StorageDevicesQuery)?;
let removable = fs::read_to_string(entry.path().join("removable"))
.context(ErrorKind::RemoveableDevicesQuery)?;
let removable = allow_non_removable
|| fs::read_to_string(entry.path().join("removable"))
.map(|v| v == "1\n")
.context(ErrorKind::StorageDevicesQuery)?;
if removable != "1\n" {
if !removable {
continue;
}
let model = fs::read_to_string(entry.path().join("device/model"))
.map(trimmed)
.context(ErrorKind::RemoveableDevicesQuery)?;
.context(ErrorKind::StorageDevicesQuery)?;
if model == "CD-ROM" {
continue;
@@ -58,10 +60,10 @@ pub fn get_removable_devices() -> Result<Vec<Device>, Error> {
model,
vendor: fs::read_to_string(entry.path().join("device/vendor"))
.map(trimmed)
.context(ErrorKind::RemoveableDevicesQuery)?,
.context(ErrorKind::StorageDevicesQuery)?,
size: Byte::from_bytes(
fs::read_to_string(entry.path().join("size"))
.context(ErrorKind::RemoveableDevicesQuery)?
.context(ErrorKind::StorageDevicesQuery)?
.trim()
.parse::<u128>()
.unwrap()

View File

@@ -15,7 +15,7 @@ pub struct StorageDevice<'a> {
}
impl<'a> StorageDevice<'a> {
pub fn from_path(path: &'a Path) -> Result<Self, Error> {
pub fn from_path(path: &'a Path, allow_non_removable: bool) -> Result<Self, Error> {
debug!("path: {:?}", path);
let path = path.canonicalize().context(ErrorKind::DeviceQuery)?;
let device_name = path
@@ -31,7 +31,7 @@ impl<'a> StorageDevice<'a> {
path,
origin: PhantomData,
};
if !(_self.is_removable_device()? || _self.is_loop_device()) {
if !allow_non_removable && (!(_self.is_removable_device()? || _self.is_loop_device())) {
return Err(ErrorKind::DangerousDevice)?;
}