diff --git a/src/disk_util.rs b/src/disk_util.rs new file mode 100644 index 0000000..63a13a4 --- /dev/null +++ b/src/disk_util.rs @@ -0,0 +1,75 @@ +use std::{str, process::Command, error::Error, str::FromStr}; +use regex::Regex; + +pub fn list() -> Result, Box> { + 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 { + 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 + } +} \ No newline at end of file diff --git a/src/disk_util/mod.rs b/src/disk_util/mod.rs deleted file mode 100644 index e4cc31e..0000000 --- a/src/disk_util/mod.rs +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/src/ipod_util.rs b/src/ipod_util.rs new file mode 100644 index 0000000..d1f7d64 --- /dev/null +++ b/src/ipod_util.rs @@ -0,0 +1,13 @@ +use crate::disk_util; + +pub fn get_ipod_path() -> Option { + 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 + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9dbd61f..f38ce08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); } } }