Skip to content

Commit

Permalink
removed device info api's and used the api from litho
Browse files Browse the repository at this point in the history
Signed-off-by: Girish Joshi <mail@girishjoshi.io>
  • Loading branch information
girish946 committed Feb 13, 2024
1 parent 58b59ac commit 021d962
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 177 deletions.
4 changes: 3 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 2 additions & 173 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

use litho;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::io::{self, Read};
use std::path::Path;
use std::process;
use std::{fs, path::PathBuf};
use tauri::Window;

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -71,27 +67,6 @@ async fn execute(
}
Ok("".to_string())
}
fn is_removable_device(device_path: &str) -> io::Result<bool> {
// Extract device name from the device path
let device_name = Path::new(device_path)
.file_name()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid device path"))?
.to_str()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Non UTF-8 device name"))?;

// Construct the path to the removable file
let removable_path = format!("/sys/block/{}/removable", device_name);

println!("removable_path: {}", removable_path);

// Read the contents of the removable file
let mut file = fs::File::open(removable_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;

// Check if the device is removable
Ok(contents.trim() == "1")
}

fn validate_and_execute(
operation: String,
Expand All @@ -102,163 +77,17 @@ fn validate_and_execute(
"operation: {}, device: {}, image: {}",
operation, device, image
);
if is_removable_device(&device).unwrap() {
if litho::devices::is_removable_device(&device).unwrap() {
println!("device is removable");
} else {
println!("device is not removable");
}
Ok("".to_string())
}

/// function to read the file contents as a string
fn get_file_content(input_file: String) -> Result<String, Box<dyn std::error::Error>> {
let content = match fs::read_to_string(input_file.clone()) {
Ok(s) => s,
Err(e) => {
format!(
"error coccured while reading :{}: {}",
input_file.clone(),
e.to_string()
);
"".to_string()
}
};

Ok(content)
}

#[tauri::command]
fn get_storage_devices() -> Result<Vec<String>, String> {
let paths = match fs::read_dir("/sys/block/") {
Ok(paths) => paths,
Err(e) => {
println!("could not get the subdirs of /sys/block/: {}", e);
return Err("".to_string());
}
};
let mut devices: Vec<String> = Vec::new();

for path in paths {
let p = match path {
Ok(p) => p,
Err(e) => {
println!("could not get the path: {}", e);
return Err("".to_string());
}
};
let mut dev = p.path().clone();
let device_end_name = match p.path().clone().file_name() {
Some(device) => match device.to_str() {
Some(dev) => dev.to_string(),
None => {
println!("could not get the device name");
"".to_string()
}
},

None => {
println!("could not get the device name ");
"".to_string()
}
};

dev.push("device");
if dev.exists() {
dev.push("vendor");
let vendor_name_file = match dev.clone().into_os_string().to_str() {
Some(name) => name.to_string(),
None => "".to_string(),
};

let mut dev_vendor_name = "".to_string();
if !vendor_name_file.is_empty() {
dev_vendor_name = match get_file_content(vendor_name_file) {
Ok(name) => name.replace("\n", ""),
Err(e) => {
println!("error occured while reading vendor name: {}", e);
"".to_string()
}
};
}
dev.pop();
dev.push("model");
let model_name_file = match dev.clone().into_os_string().to_str() {
Some(name) => name.to_string(),
None => "".to_string(),
};

let model_name = match get_file_content(model_name_file) {
Ok(model) => model.replace("\n", ""),
Err(e) => {
println!("error occured while reading model name: {}", e);
"".to_string()
}
};
dev.pop();
dev.pop();
dev.push("removable");
let mut removable = "".to_string();
if dev.exists() {
removable = match fs::read_to_string(dev.clone()) {
Ok(removable) => removable,
Err(e) => {
println!("error occured while reading removable: {}", e);
"".to_string()
}
}
// println!("removable: {}", removable);
}

dev.pop();
dev.push("size");
let size: u64;
size = match fs::read_to_string(dev.clone()) {
Ok(size_str) => {
println!("size: {}", size_str);
match size_str.trim().parse::<u64>() {
Ok(s) => s,
Err(e) => {
println!("error occured while parsing size: {}", e);
0
}
}
}
Err(e) => {
println!("error occured while reading size: {}", e);
0
}
};

if !device_end_name.is_empty() {
let mut dev_path = PathBuf::from("/dev/");
dev_path.push(device_end_name);
if dev_path.exists() {
if removable == "1\n" {
let dev_info = DeviceInfo {
device_name: dev_path.display().to_string(),
vendor_name: dev_vendor_name,
model_name,
removable: 1,
size,
};
let device_json = json!(dev_info).to_string();
devices.push(device_json);
} else if removable == "0\n" {
let dev_info = DeviceInfo {
device_name: dev_path.display().to_string(),
vendor_name: dev_vendor_name,
model_name,
removable: 0,
size,
};
let dev_json = json!(dev_info).to_string();
devices.push(dev_json);
}
}
}
}
}
return Ok(devices);
litho::devices::get_storage_devices()
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
<div class="central-control-row">
<div class="left-text-column">
<lebal class="flash-image" id="writeFile">Flash Image</lebal>
<lebal class="clone-image" id="cloneDiskToFile">Clone to File</lebal>
<lebal class="clone-image" id="cloneDiskToFile">Clone Image</lebal>
<!-- <lebal class="clone-device">Clone to Device</lebal> -->
</div>
<div class="select-device-column">
<select class="select-device-dropdown" id="diskSelect"></select>
<select class="select-device-dropdown" id="diskSelect">
<option value="default">Select a device</option>
</select>
</div>
<div class="start-column">
<button class="start-button" style="color:powderblue background-color:grey" id="startProcess">Start</button>
Expand Down
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function add_storage_device_names() {
for (var i = 0; i < non_removable_devices.length; i++) {
diskSelect.appendChild(non_removable_devices[i]);
}
diskSelect.selectedIndex = 0;
});
}

Expand Down Expand Up @@ -90,10 +91,13 @@ async function start_process_on_click(e) {
}

function select_device_on_click(e) {

if (e.target.value == "default") {
return;
}
device_details = JSON.parse(e.target.value);
selected_disk = device_details.device_name;
target_size = device_details.size;
console.log("selested device:" + device_details);
console.log(selected_disk);
}

Expand Down

0 comments on commit 021d962

Please sign in to comment.