Skip to content

Commit

Permalink
Use separate smb shares for each partition (fixes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ALSchwalm committed Oct 14, 2018
1 parent 3c4381f commit a852ddf
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 31 deletions.
5 changes: 4 additions & 1 deletion pISO/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ pub enum Action {
FlipDisplay,

OpenVersion,
CloseVersion
CloseVersion,

SmbSharePartition(String),
SmbRemoveShare(String),
}
8 changes: 4 additions & 4 deletions pISO/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Options {
removable: buttons::vdrivelist::DriveList,
delete: buttons::vdrivelist::DriveList,
snapshot: buttons::vdrivelist::DriveList,
version: version::VersionMenu
version: version::VersionMenu,
}

impl Options {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Options {
removable: removable,
delete: delete,
snapshot: snapshot,
version: version
version: version,
})
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Widget for Options {
&mut self.removable as &mut Widget,
&mut self.snapshot as &mut Widget,
&mut self.delete as &mut Widget,
&mut self.version as &mut Widget
&mut self.version as &mut Widget,
]
} else {
vec![]
Expand All @@ -134,7 +134,7 @@ impl Widget for Options {
&self.removable as &Widget,
&self.snapshot as &Widget,
&self.delete as &Widget,
&self.version as &Widget
&self.version as &Widget,
]
} else {
vec![]
Expand Down
53 changes: 42 additions & 11 deletions pISO/src/piso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,45 @@ impl PIso {
Ok(drives)
}

fn add_drive(
&mut self,
disp: &mut DisplayManager,
fn add_drive<'a, 'b>(
&'a mut self,
disp: &'b mut DisplayManager,
volume: lvm::LogicalVolume,
) -> Result<&vdrive::VirtualDrive> {
let vdrive = vdrive::VirtualDrive::new(disp, self.usb.clone(), volume, &self.config)?;
) -> Result<&'a mut vdrive::VirtualDrive> {
let mut vdrive = vdrive::VirtualDrive::new(disp, self.usb.clone(), volume, &self.config)?;
vdrive.mount_internal(disp)?;
self.drives.push(vdrive);

Ok(self.drives
.last()
.last_mut()
.expect("vdrive was somehow empty after push"))
}

fn share_drive(drive: &mut vdrive::VirtualDrive, remove: bool) -> Result<Vec<action::Action>> {
match drive.state {
vdrive::MountState::Unmounted | vdrive::MountState::External(_) => {
if remove {
Ok(vec![])
} else {
Err("Cannot share drive when not mounted internal".into())
}
}
vdrive::MountState::Internal(ref info) => Ok(info.part_mount_paths
.iter()
.map(|path| {
let name = path.file_name()
.expect("Partition has no name")
.to_string_lossy()
.into_owned();
if remove {
action::Action::SmbRemoveShare(name)
} else {
action::Action::SmbSharePartition(name)
}
})
.collect()),
}
}
}

impl render::Render for PIso {
Expand All @@ -145,23 +172,27 @@ impl input::Input for PIso {
Ok((true, vec![]))
}
action::Action::CreateDrive(ref volume) => {
self.add_drive(disp, volume.clone())?;
Ok((true, vec![]))
let drive = self.add_drive(disp, volume.clone())?;
let actions = PIso::share_drive(drive, false)?;
Ok((true, actions))
}
action::Action::SnapshotDrive(ref name) => {
let report = self.vg.snapshot_volume(name)?;
self.add_drive(disp, report)?;
Ok((true, vec![]))
let drive = self.add_drive(disp, report)?;
let actions = PIso::share_drive(drive, false)?;
Ok((true, actions))
}
action::Action::DeleteDrive(ref name) => {
let mut actions = vec![];
if let Some(ref mut drive) =
self.drives.iter_mut().find(|drive| drive.name() == name)
{
actions = PIso::share_drive(drive, true)?;
drive.unmount()?;
}
self.drives.retain(|drive| drive.name() != name);
self.vg.delete_volume(&name)?;
Ok((true, vec![]))
Ok((true, actions))
}
_ => Ok((false, vec![])),
}
Expand Down
15 changes: 11 additions & 4 deletions pISO/src/vdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ impl VirtualDrive {
).into())
}

pub fn mount_internal(&mut self, disp: &mut DisplayManager) -> Result<()> {
pub fn mount_internal<'a, 'b>(
&'a mut self,
disp: &'b mut DisplayManager,
) -> Result<&'a MountInfo> {
match self.state {
MountState::Unmounted => {
let volume_path = &self.volume.path.to_string_lossy();
Expand Down Expand Up @@ -222,9 +225,12 @@ impl VirtualDrive {
isos: isos,
loopback_path: loopback_path.to_path_buf(),
});
Ok(())
match &self.state {
&MountState::Internal(ref info) => Ok(info),
_ => unreachable!(),
}
}
MountState::Internal(_) => Ok(()),
MountState::Internal(ref state) => Ok(state),
MountState::External(_) => {
Err("Attempt to mount_internal while mounted external".into())
}
Expand Down Expand Up @@ -262,7 +268,8 @@ impl VirtualDrive {
}
MountState::External(_) => {
self.unmount_external()?;
self.mount_internal(disp)
self.mount_internal(disp)?;
Ok(())
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions pISO/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use action;
use bitmap;
use controller;
use display;
use displaymanager::{DisplayManager, Position, Widget, Window, WindowId};
use error;
use font;
Expand Down Expand Up @@ -56,14 +57,14 @@ enum VersionState {

pub struct VersionMenu {
pub window: WindowId,
state: VersionState
state: VersionState,
}

impl VersionMenu {
pub fn new(disp: &mut DisplayManager) -> error::Result<VersionMenu> {
Ok(VersionMenu {
window: disp.add_child(Position::Normal)?,
state: VersionState::Unselected
state: VersionState::Unselected,
})
}
}
Expand Down Expand Up @@ -97,8 +98,7 @@ impl input::Input for VersionMenu {
) -> error::Result<(bool, Vec<action::Action>)> {
match *action {
action::Action::OpenVersion => {
let menu =
OpenVersionMenu::new(disp)?;
let menu = OpenVersionMenu::new(disp)?;
disp.shift_focus(&menu);
self.state = VersionState::Selected(menu);
Ok((true, vec![]))
Expand Down Expand Up @@ -135,10 +135,8 @@ impl Widget for VersionMenu {
}
}



struct OpenVersionMenu {
pub window: WindowId
pub window: WindowId,
}

impl OpenVersionMenu {
Expand Down
61 changes: 57 additions & 4 deletions pISO/src/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const HOSTAPD_TMP_CONF: &'static str = "/tmp/hostapd.conf";
const WPA_SUPPLICANT_CONF: &'static str = "/etc/wpa_supplicant.conf";
const WPA_SUPPLICANT_TMP_CONF: &'static str = "/tmp/wpa_supplicant.conf";
const SMB_CONF: &'static str = "/etc/samba/smb.conf";
const SMB_TMP_CONF: &'static str = "/tmp/smb.conf";
const PURE_FTPD_CONF: &'static str = "/etc/pure-ftpd.conf";

#[derive(PartialEq)]
Expand All @@ -46,6 +45,10 @@ impl WifiManager {
}))
}

fn is_enabled(&self) -> bool {
self.state != WifiState::Uninitialized
}

fn enable_wifi(&mut self) -> error::Result<()> {
if self.state != WifiState::Uninitialized {
return Ok(());
Expand Down Expand Up @@ -87,13 +90,55 @@ impl WifiManager {
&[&self.config.user.name, &self.config.user.password],
)?;

fs::copy(SMB_CONF, SMB_TMP_CONF)?;
utils::run_check_output("smbd", &["-D", "-s", SMB_TMP_CONF])?;
utils::run_check_output("nmbd", &["-D", "-s", SMB_TMP_CONF])?;
// Setup usershare folder
fs::create_dir_all("/var/lib/samba/usershares")?;
utils::run_check_output("chmod", &["1770", "/var/lib/samba/usershares"])?;

utils::run_check_output("smbd", &["-D", "-s", SMB_CONF])?;
utils::run_check_output("nmbd", &["-D", "-s", SMB_CONF])?;

utils::run_check_output("pure-ftpd", &[PURE_FTPD_CONF])?;

self.state = WifiState::Inactive;

for entry in fs::read_dir("/mnt")? {
let entry = entry?;
let path = entry.path();
let name = path.file_name()
.expect("Partition has no name")
.to_string_lossy()
.into_owned();
self.share_mounted_partition(&name)?;
}

Ok(())
}

fn share_mounted_partition(&mut self, name: &str) -> error::Result<()> {
if !self.is_enabled() {
return Ok(());
}

let path = "/user-mnt/".to_owned() + name;
utils::run_check_output(
"net",
&[
"usershare",
"add",
name,
&path,
"",
&format!("piso\\{}:F", &self.config.user.name),
],
)?;
Ok(())
}

fn remove_shared_partition(&mut self, name: &str) -> error::Result<()> {
if !self.is_enabled() {
return Ok(());
}
utils::run_check_output("net", &["usershare", "delete", &name])?;
Ok(())
}

Expand Down Expand Up @@ -262,6 +307,14 @@ impl input::Input for WifiMenu {
disp.shift_focus(self);
Ok((true, vec![]))
}
action::Action::SmbSharePartition(ref name) => {
self.manager.lock()?.share_mounted_partition(name)?;
Ok((true, vec![]))
}
action::Action::SmbRemoveShare(ref name) => {
self.manager.lock()?.remove_shared_partition(name)?;
Ok((true, vec![]))
}
_ => Ok((false, vec![])),
}
}
Expand Down

0 comments on commit a852ddf

Please sign in to comment.