mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-12-06 19:39:20 +01:00
Interactive device selection
This commit is contained in:
@@ -4,6 +4,7 @@ mod loop_device;
|
||||
mod markers;
|
||||
mod mount_stack;
|
||||
mod partition;
|
||||
mod removeable_devices;
|
||||
mod storage_device;
|
||||
|
||||
pub use crypt::{is_encrypted_device, EncryptedDevice};
|
||||
@@ -11,4 +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 storage_device::StorageDevice;
|
||||
|
||||
85
src/storage/removeable_devices.rs
Normal file
85
src/storage/removeable_devices.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use byte_unit::Byte;
|
||||
use failure::ResultExt;
|
||||
use std::{fmt, fs};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Device {
|
||||
model: String,
|
||||
vendor: String,
|
||||
size: Byte,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Device {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{} {} ({})",
|
||||
self.vendor,
|
||||
self.model,
|
||||
self.size.get_appropriate_unit(true)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn trimmed(source: String) -> String {
|
||||
String::from(source.trim_end())
|
||||
}
|
||||
|
||||
pub fn get_removable_devices() -> 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)?;
|
||||
|
||||
let removable = fs::read_to_string(entry.path().join("removable"))
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?;
|
||||
|
||||
if removable != "1\n" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let model = fs::read_to_string(entry.path().join("device/model"))
|
||||
.map(trimmed)
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?;
|
||||
|
||||
if model == "CD-ROM" {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push(Device {
|
||||
name: entry
|
||||
.path()
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
model,
|
||||
vendor: fs::read_to_string(entry.path().join("device/vendor"))
|
||||
.map(trimmed)
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?,
|
||||
size: Byte::from_bytes(
|
||||
fs::read_to_string(entry.path().join("size"))
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?
|
||||
.trim()
|
||||
.parse::<u128>()
|
||||
.unwrap()
|
||||
* 512,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sanity() {
|
||||
let devices = get_removable_devices().unwrap();
|
||||
println!("{:?}", devices);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user