summaryrefslogtreecommitdiffstats
path: root/src/iso.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/iso.rs')
-rw-r--r--src/iso.rs94
1 files changed, 35 insertions, 59 deletions
diff --git a/src/iso.rs b/src/iso.rs
index 4b5ff61..3c5c0c5 100644
--- a/src/iso.rs
+++ b/src/iso.rs
@@ -3,7 +3,7 @@ use log::info;
use crate::{
mount_help::MountHelper,
- password::{sudo, sudo_password, PasswordHolder, PasswordKind},
+ password::{execute_sudo_command, execute_sudo_command_with_password, PasswordHolder},
};
/// Used to mount an iso file as a loop device.
@@ -14,7 +14,7 @@ pub struct IsoDevice {
/// The source, aka the iso file itself.
pub path: String,
/// None when creating, updated once the device is mounted.
- pub mountpoints: Option<String>,
+ pub mountpoints: Option<std::path::PathBuf>,
is_mounted: bool,
}
@@ -23,14 +23,18 @@ impl IsoDevice {
/// Creates a new instance from an iso file path.
pub fn from_path(path: String) -> Self {
+ log::info!("IsoDevice from_path: {path}");
Self {
path,
..Default::default()
}
}
- fn mountpoints(&self, username: &str) -> String {
- format!("/run/media/{}/{}", username, Self::FILENAME)
+ fn mountpoints(&self, username: &str) -> std::path::PathBuf {
+ let mut mountpoint = std::path::PathBuf::from("/run/media");
+ mountpoint.push(username);
+ mountpoint.push(Self::FILENAME);
+ mountpoint
}
}
@@ -51,14 +55,18 @@ impl MountHelper for IsoDevice {
"-o".to_owned(),
"loop".to_owned(),
self.path.clone(),
- mountpoints,
+ mountpoints.to_string_lossy().to_string(),
]
}
fn format_umount_parameters(&self, username: &str) -> Vec<String> {
vec![
"umount".to_owned(),
- format!("/run/media/{}/{}", username, self.mountpoints(username),),
+ format!(
+ "/run/media/{}/{}",
+ username,
+ self.mountpoints(username).display(),
+ ),
]
}
@@ -67,46 +75,53 @@ impl MountHelper for IsoDevice {
}
fn umount(&mut self, username: &str, passwords: &mut PasswordHolder) -> Result<bool> {
+ let root_path = std::path::Path::new("/");
// sudo
- let (success, _, _) = sudo_password(
- &["-S".to_owned(), "ls".to_owned(), "/root".to_owned()],
+ let (success, _, _) = execute_sudo_command_with_password(
+ &["-S", "ls", "/root"],
&passwords.sudo()?,
+ root_path,
)?;
if !success {
return Ok(false);
}
// unmount
- let (success, stdout, stderr) = sudo(&self.format_umount_parameters(username))?;
+ let (success, stdout, stderr) =
+ execute_sudo_command(&self.format_umount_parameters(username))?;
info!("stdout: {}\nstderr: {}", stdout, stderr);
if success {
self.is_mounted = false;
}
// sudo -k
- let (success, stdout, stderr) = sudo(&["-k".to_owned()])?;
+ let (success, stdout, stderr) = execute_sudo_command(&["-k"])?;
info!("stdout: {}\nstderr: {}", stdout, stderr);
Ok(success)
}
fn mount(&mut self, username: &str, passwords: &mut PasswordHolder) -> Result<bool> {
+ let root_path = std::path::Path::new("/");
info!("iso mount: {username}, {passwords:?}");
if self.is_mounted {
Err(anyhow!("iso device mount: device is already mounted"))
} else {
// sudo
- let (success, _, _) = sudo_password(
- &["-S".to_owned(), "ls".to_owned(), "/root".to_owned()],
+ let (success, _, _) = execute_sudo_command_with_password(
+ &["ls", "/root"],
&passwords.sudo()?,
+ root_path,
)?;
if !success {
return Ok(false);
}
// mkdir
- let (success, stdout, stderr) = sudo(&self.format_mkdir_parameters(username))?;
+ let (success, stdout, stderr) =
+ execute_sudo_command(&self.format_mkdir_parameters(username))?;
info!("stdout: {}\nstderr: {}", stdout, stderr);
let mut last_success = false;
if success {
// mount
- let (success, stdout, stderr) = sudo(&self.format_mount_parameters(username))?;
+ let (success, stdout, stderr) =
+ execute_sudo_command(&self.format_mount_parameters(username))?;
last_success = success;
info!("stdout: {}\nstderr: {}", stdout, stderr);
// sudo -k
@@ -114,7 +129,7 @@ impl MountHelper for IsoDevice {
} else {
self.is_mounted = false;
}
- sudo(&["-k".to_owned()])?;
+ execute_sudo_command(&["-k"])?;
Ok(last_success)
}
}
@@ -122,52 +137,13 @@ impl MountHelper for IsoDevice {
/// String representation of the device.
fn as_string(&self) -> Result<String> {
if let Some(ref mount_point) = self.mountpoints {
- Ok(format!("mounted {}\nto {}", self.path, mount_point))
+ Ok(format!(
+ "mounted {}\nto {}",
+ self.path,
+ mount_point.display()
+ ))
} else {
Ok(format!("not mounted {}", self.path))
}
}
}
-
-/// Holds every thing needed to mount a device.
-/// It has an `IsoDevice` instance and a password holder.
-#[derive(Clone, Debug, Default)]
-pub struct IsoMounter {
- pub iso_device: IsoDevice,
- password_holder: PasswordHolder,
-}
-
-impl IsoMounter {
- /// True if the sudo password is known.
- pub fn has_sudo(&self) -> bool {
- self.password_holder.has_sudo()
- }
-
- /// Mount the device. Will fail if the password isn't known or the file can't be mounted.
- pub fn mount(&mut self, username: &str) -> Result<bool> {
- self.iso_device.mount(username, &mut self.password_holder)
- }
-
- /// Currently unused.
- /// Un mount the device.
- pub fn umount(&mut self, username: &str) -> Result<bool> {
- self.iso_device.umount(username, &mut self.password_holder)
- }
-
- /// Creates an instance from an iso filepath.
- pub fn from_path(path: String) -> Self {
- let iso_device = IsoDevice::from_path(path);
- Self {
- iso_device,
- ..Default::default()
- }
- }
-
- /// Set the password.
- /// Only sudo password can be set.
- pub fn set_password(&mut self, password_kind: PasswordKind, password: String) {
- if let PasswordKind::SUDO = password_kind {
- self.password_holder.set_sudo(password)
- }
- }
-}