new file: src/disk_util.rs

deleted:    src/disk_util/mod.rs
	new file:   src/ipod_util.rs
	modified:   src/main.rs
This commit is contained in:
Michael Wain 2024-12-02 22:21:11 +02:00
parent 0ec9755b7d
commit 0143e6eb2e
4 changed files with 90 additions and 21 deletions

75
src/disk_util.rs Normal file
View File

@ -0,0 +1,75 @@
use std::{str, process::Command, error::Error, str::FromStr};
use regex::Regex;
pub fn list() -> Result<Vec<String>, Box<dyn Error>> {
let mut disks = Vec::new();
let r = match Command::new("diskutil").arg("list").output() {
Ok(s) => s,
Err(e) => return Err(Box::new(e))
};
if !r.status.success() { return Ok(disks); }
let rg = Regex::new(r"\d:.+ [a-zA-Z0-9].+").unwrap();
let a = match str::from_utf8(&r.stdout) {
Ok(r) => r,
Err(e) => return Err(Box::new(e))
};
for cap in Regex::new(r"\/dev\/.+\(external\, physical\):").unwrap().find_iter(a) {
let mut b = &a[cap.end()..];
let i = match b.find("\n\n") {
Some(r) => r,
None => return Ok(disks)
};
b = &b[..i];
for gap in rg.find_iter(b) {
let j = match gap.as_str().rfind(" ") {
Some(r) => r + 1,
None => return Ok(disks)
};
let g= &gap.as_str()[j..];
disks.push(String::from_str(g).unwrap());
}
}
Ok(disks)
}
pub fn is_ipod(name: &str) -> bool {
let r = match Command::new("diskutil").arg("info").arg(name).output() {
Ok(s) => s,
Err(_e) => return false
};
if !r.status.success() { return false; }
let a = match str::from_utf8(&r.stdout) {
Ok(r) => r,
Err(_e) => return false
};
let cap = Regex::new(r"Media Type:.+\n").unwrap().find(a);
if let Some(g) = cap {
let mut b = g.as_str();
let f = b.rfind(" ").unwrap() + 1;
b = &b[f..b.len()-1];
return b == "iPod";
}
false
}
pub fn get_mount_point(name: &str) -> Option<String> {
let r = match Command::new("diskutil").arg("info").arg(name).output() {
Ok(s) => s,
Err(_e) => return None
};
if !r.status.success() { return None; }
let a = match str::from_utf8(&r.stdout) {
Ok(r) => r,
Err(_e) => return None
};
let cap = Regex::new(r"Mount Point:.+\n").unwrap().find(a);
match cap {
Some(g) => {
let i = g.as_str();
let j = i.rfind(" ").unwrap() + 1;
Some(i[j..i.len()-1].to_string())
},
None => None
}
}

View File

@ -1,19 +0,0 @@
use core::str;
use std::process::Command;
use regex::Regex;
pub fn list() {
let r = Command::new("diskutil")
.arg("list")
.output()
.expect("failed to access Disk Utility");
if !r.status.success() { panic!("Unable to get result from Disk Utility"); }
let a = str::from_utf8(&r.stdout).unwrap();
for cap in Regex::new(r"\/dev\/.+\(external\, physical\):").unwrap().find_iter(a) {
println!("{:#?}", cap);
let b = &a[cap.end()..];
// split the string and get before double \n.
// then parse using diskutil info /dev/disk2
}
}

13
src/ipod_util.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::disk_util;
pub fn get_ipod_path() -> Option<String> {
match disk_util::list() {
Ok(l) => l.iter()
.filter(|d| disk_util::is_ipod(d))
.map(|d| disk_util::get_mount_point(d))
.filter(|d| d.is_some())
.map(|d| d.unwrap())
.last(),
Err(e) => None
}
}

View File

@ -1,5 +1,5 @@
mod disk_util;
mod ipod_util;
const VENDOR_ID: u16 = 1452;
const PRODUCT_ID: u16 = 4617;
@ -9,7 +9,7 @@ fn main() {
let device_desc = device.device_descriptor().unwrap();
if VENDOR_ID == device_desc.vendor_id() && PRODUCT_ID == device_desc.product_id() {
println!("FOUND!");
disk_util::list();
println!("{}", ipod_util::get_ipod_path().is_some());
}
}
}