Refactor scan_local_drives()
This commit is contained in:
parent
2aed8d130b
commit
cf3d71567e
4 changed files with 81 additions and 56 deletions
|
|
@ -17,3 +17,6 @@
|
|||
members = ["core", "boot_diags", "deja_vu", "pe_menu", "win_installer"]
|
||||
default-members = ["core", "boot_diags", "deja_vu", "pe_menu", "win_installer"]
|
||||
resolver = "2"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ use ratatui::{
|
|||
use tokio::sync::mpsc;
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::{components::wim_scan::WimScan, state::State, wim::scan_local_drives};
|
||||
use crate::{components::wim_scan::WimScan, state::State};
|
||||
|
||||
pub struct App {
|
||||
// TUI
|
||||
|
|
@ -143,13 +143,7 @@ impl App {
|
|||
String::from("Scanning Disks..."),
|
||||
))?;
|
||||
}
|
||||
Mode::ScanWinImages => {
|
||||
let disk_list_arc = self.state.disk_list.clone();
|
||||
let wim_sources_arc = self.state.wim_sources.clone();
|
||||
tokio::task::spawn(async move {
|
||||
scan_local_drives(disk_list_arc, wim_sources_arc);
|
||||
});
|
||||
}
|
||||
Mode::ScanWinImages => self.state.scan_wim_local(),
|
||||
Mode::Done => {
|
||||
self.action_tx
|
||||
.send(Action::DisplayPopup(popup::Type::Success, popup::fortune()))?;
|
||||
|
|
|
|||
|
|
@ -14,14 +14,27 @@
|
|||
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use core::system::{
|
||||
disk::{Disk, PartitionTableType},
|
||||
drivers,
|
||||
use std::{
|
||||
fs::read_dir,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::wim::WimSources;
|
||||
use core::{
|
||||
action::Action,
|
||||
components::popup::Type as PopupType,
|
||||
config::Config,
|
||||
system::{
|
||||
disk::{Disk, PartitionTableType},
|
||||
drivers,
|
||||
},
|
||||
};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
net::connect_network_share,
|
||||
wim::{WimSources, parse_wim_file},
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct State {
|
||||
|
|
@ -63,4 +76,47 @@ impl State {
|
|||
pub fn scan_drivers(&mut self) {
|
||||
self.driver_list = drivers::scan();
|
||||
}
|
||||
|
||||
pub fn scan_wim_local(&mut self) {
|
||||
let disk_list_arc = self.disk_list.clone();
|
||||
let wim_sources_arc = self.wim_sources.clone();
|
||||
tokio::task::spawn(async move {
|
||||
scan_local_drives(disk_list_arc, wim_sources_arc);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn scan_local_drives(
|
||||
disk_list_arc: Arc<Mutex<Vec<Disk>>>,
|
||||
wim_sources_arc: Arc<Mutex<WimSources>>,
|
||||
) {
|
||||
let mut to_check = vec![String::from(".")];
|
||||
|
||||
// Get drive letters
|
||||
if let Ok(disk_list) = disk_list_arc.lock() {
|
||||
disk_list.iter().for_each(|d| {
|
||||
d.parts.iter().for_each(|p| {
|
||||
if !p.letter.is_empty() {
|
||||
to_check.push(format!("{}:\\Images", &p.letter));
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
// Scan drives
|
||||
to_check.iter().for_each(|scan_path| {
|
||||
info!("Scanning: {}", &scan_path);
|
||||
if let Ok(read_dir) = read_dir(scan_path) {
|
||||
read_dir.for_each(|item| {
|
||||
if let Ok(item) = item
|
||||
&& item.file_name().to_string_lossy().ends_with(".wim")
|
||||
&& let Some(path_str) = item.path().to_str()
|
||||
&& let Ok(new_source) = parse_wim_file(path_str)
|
||||
&& let Ok(mut wim_sources) = wim_sources_arc.lock()
|
||||
{
|
||||
wim_sources.local.push(new_source);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
use core::system::disk::Disk;
|
||||
// This file is part of Deja-Vu.
|
||||
//
|
||||
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||
|
|
@ -16,18 +15,26 @@ use core::system::disk::Disk;
|
|||
//
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
fs::{File, read_dir},
|
||||
env, fmt,
|
||||
fs::File,
|
||||
io::BufReader,
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
sync::{Arc, LazyLock, Mutex},
|
||||
sync::LazyLock,
|
||||
};
|
||||
|
||||
use tempfile::NamedTempFile;
|
||||
use tracing::info;
|
||||
use xml::reader::{EventReader, XmlEvent};
|
||||
|
||||
static WIMINFO_EXE: LazyLock<String> = LazyLock::new(|| {
|
||||
let program_files =
|
||||
PathBuf::from(env::var("PROGRAMFILES").expect("Failed to resolve %PROGRAMFILES%"));
|
||||
program_files
|
||||
.join("wimlib/wiminfo.cmd")
|
||||
.to_string_lossy()
|
||||
.into_owned()
|
||||
});
|
||||
|
||||
static WIN_BUILDS: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
|
||||
HashMap::from([
|
||||
// Windows 10
|
||||
|
|
@ -118,7 +125,7 @@ impl WimSources {
|
|||
|
||||
fn get_wim_xml(wim_file: &str) -> std::io::Result<File> {
|
||||
let tmp_file = NamedTempFile::new()?;
|
||||
let _ = Command::new("wiminfo")
|
||||
let _ = Command::new(&*WIMINFO_EXE)
|
||||
.args([
|
||||
wim_file,
|
||||
"--extract-xml",
|
||||
|
|
@ -200,38 +207,3 @@ pub fn parse_wim_file(wim_file: &str) -> std::io::Result<WimFile> {
|
|||
|
||||
Ok(wim_file)
|
||||
}
|
||||
|
||||
pub fn scan_local_drives(
|
||||
disk_list_arc: Arc<Mutex<Vec<Disk>>>,
|
||||
wim_sources_arc: Arc<Mutex<WimSources>>,
|
||||
) {
|
||||
let mut to_check = vec![String::from(".")];
|
||||
|
||||
// Get drive letters
|
||||
if let Ok(disk_list) = disk_list_arc.lock() {
|
||||
disk_list.iter().for_each(|d| {
|
||||
d.parts.iter().for_each(|p| {
|
||||
if !p.letter.is_empty() {
|
||||
to_check.push(format!("{}/Images", &p.letter));
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
// Scan drives
|
||||
to_check.iter().for_each(|scan_path| {
|
||||
info!("Scanning: {}", &scan_path);
|
||||
if let Ok(read_dir) = read_dir(scan_path) {
|
||||
read_dir.for_each(|item| {
|
||||
if let Ok(item) = item
|
||||
&& item.file_name().to_string_lossy().ends_with(".wim")
|
||||
&& let Some(path_str) = item.path().to_str()
|
||||
&& let Ok(new_source) = parse_wim_file(path_str)
|
||||
&& let Ok(mut wim_sources) = wim_sources_arc.lock()
|
||||
{
|
||||
wim_sources.local.push(new_source);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue