Upload fastsearch-core/src/utils/drives.rs
Browse files
fastsearch-core/src/utils/drives.rs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use crate::mft::types::NtfsDrive;
|
| 2 |
+
use windows::{
|
| 3 |
+
Win32::Storage::FileSystem::{
|
| 4 |
+
GetLogicalDriveStringsW, GetVolumeInformationW,
|
| 5 |
+
},
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
/// Returns all NTFS drives on the system
|
| 9 |
+
pub fn get_ntfs_drives() -> Vec<NtfsDrive> {
|
| 10 |
+
let mut drives = Vec::new();
|
| 11 |
+
|
| 12 |
+
let mut buf = vec![0u16; 256];
|
| 13 |
+
let len = unsafe { GetLogicalDriveStringsW(Some(&mut buf)) } as usize;
|
| 14 |
+
if len == 0 {
|
| 15 |
+
return drives;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
let drive_strings: Vec<String> = buf[..len]
|
| 19 |
+
.split(|&c| c == 0)
|
| 20 |
+
.filter(|s| !s.is_empty())
|
| 21 |
+
.map(|s| String::from_utf16_lossy(s))
|
| 22 |
+
.collect();
|
| 23 |
+
|
| 24 |
+
for root in drive_strings {
|
| 25 |
+
if is_ntfs(&root) {
|
| 26 |
+
let letter = root.chars().next().unwrap();
|
| 27 |
+
drives.push(NtfsDrive {
|
| 28 |
+
letter,
|
| 29 |
+
root: root.clone(),
|
| 30 |
+
device_path: format!("\\\\.\\{}:", letter),
|
| 31 |
+
});
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
drives
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
fn is_ntfs(root: &str) -> bool {
|
| 39 |
+
let root_wide: Vec<u16> = root.encode_utf16().chain(Some(0)).collect();
|
| 40 |
+
let mut fs_name = vec![0u16; 32];
|
| 41 |
+
|
| 42 |
+
let ok = unsafe {
|
| 43 |
+
GetVolumeInformationW(
|
| 44 |
+
windows::core::PCWSTR(root_wide.as_ptr()),
|
| 45 |
+
None,
|
| 46 |
+
None,
|
| 47 |
+
None,
|
| 48 |
+
None,
|
| 49 |
+
Some(&mut fs_name),
|
| 50 |
+
)
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
if ok.is_err() {
|
| 54 |
+
return false;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
let fs = String::from_utf16_lossy(&fs_name);
|
| 58 |
+
fs.starts_with("NTFS")
|
| 59 |
+
}
|