mirror of
https://github.com/tobi/disktree.git
synced 2026-12-04 08:01:05 +00:00
removal: read Windows' volume list, so a volume mounted inside a target is refused
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2ed6720638
commit
a64a58060d
@@ -566,34 +566,46 @@ pub fn prime_mount_points() {
|
||||
let _ = mount_points();
|
||||
}
|
||||
|
||||
/// Read the mount points now: from `/proc/self/mounts`, or on macOS from
|
||||
/// `mount`, which has no `/proc`. Empty where neither can be read.
|
||||
/// Read the mount points now: from `/proc/self/mounts`, on macOS from
|
||||
/// `mount`, which has no `/proc`, and on Windows from the volume list.
|
||||
/// Empty where none can be read.
|
||||
fn read_mount_points() -> Vec<PathBuf> {
|
||||
let points: Vec<PathBuf> =
|
||||
if let Ok(table) = fs::read_to_string("/proc/self/mounts") {
|
||||
crate::space::parse_mounts(&table)
|
||||
.into_iter()
|
||||
.map(|mount| mount.point)
|
||||
.collect()
|
||||
} else if cfg!(target_os = "macos") {
|
||||
Command::new("/sbin/mount")
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|output| output.status.success())
|
||||
.map(|output| {
|
||||
crate::space::parse_macos_mounts(&String::from_utf8_lossy(
|
||||
&output.stdout,
|
||||
))
|
||||
})
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let points: Vec<PathBuf> = if cfg!(windows) {
|
||||
windows_mount_points()
|
||||
} else if let Ok(table) = fs::read_to_string("/proc/self/mounts") {
|
||||
crate::space::parse_mounts(&table)
|
||||
.into_iter()
|
||||
.map(|mount| mount.point)
|
||||
.collect()
|
||||
} else if cfg!(target_os = "macos") {
|
||||
Command::new("/sbin/mount")
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|output| output.status.success())
|
||||
.map(|output| {
|
||||
crate::space::parse_macos_mounts(&String::from_utf8_lossy(
|
||||
&output.stdout,
|
||||
))
|
||||
})
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
points.iter().map(|point| guard_key(point)).collect()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn windows_mount_points() -> Vec<PathBuf> {
|
||||
crate::windows::mount_points()
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
const fn windows_mount_points() -> Vec<PathBuf> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// A mount point strictly inside the path keyed `key`, if there is one. A
|
||||
/// scan that stays on one filesystem never shows what is mounted there, so
|
||||
/// the user cannot have meant to remove it.
|
||||
|
||||
@@ -27,14 +27,16 @@ use std::sync::Arc;
|
||||
|
||||
use windows_sys::Win32::Foundation::{
|
||||
ERROR_INVALID_FUNCTION, ERROR_INVALID_LEVEL, ERROR_INVALID_PARAMETER,
|
||||
ERROR_NO_MORE_FILES, ERROR_NOT_SUPPORTED,
|
||||
ERROR_NO_MORE_FILES, ERROR_NOT_SUPPORTED, INVALID_HANDLE_VALUE,
|
||||
};
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS,
|
||||
FILE_ATTRIBUTE_RECALL_ON_OPEN, FILE_ATTRIBUTE_REPARSE_POINT,
|
||||
FILE_FLAG_BACKUP_SEMANTICS, FILE_ID_EXTD_DIR_INFO, FILE_LIST_DIRECTORY,
|
||||
FILE_READ_ATTRIBUTES, FileIdExtdDirectoryInfo, GetDiskFreeSpaceExW,
|
||||
GetFileInformationByHandleEx, GetVolumePathNameW, SYNCHRONIZE,
|
||||
FILE_READ_ATTRIBUTES, FileIdExtdDirectoryInfo, FindFirstVolumeW,
|
||||
FindNextVolumeW, FindVolumeClose, GetDiskFreeSpaceExW,
|
||||
GetFileInformationByHandleEx, GetVolumePathNameW,
|
||||
GetVolumePathNamesForVolumeNameW, SYNCHRONIZE,
|
||||
};
|
||||
|
||||
use crate::space::SpaceInfo;
|
||||
@@ -454,6 +456,68 @@ pub fn volume_root(path: &Path) -> Option<PathBuf> {
|
||||
Some(PathBuf::from(OsString::from_wide(&root[..end])))
|
||||
}
|
||||
|
||||
/// Every place a volume is mounted: drive roots such as `D:\`, and folders
|
||||
/// a volume is mounted on, such as `C:\Data\Disk2\`. The mount table
|
||||
/// Windows has in place of `/proc/self/mounts`.
|
||||
pub fn mount_points() -> Vec<PathBuf> {
|
||||
let mut points = Vec::new();
|
||||
// A volume GUID path, `\\?\Volume{…}\`, is 49 units with its NUL.
|
||||
let mut volume = [0_u16; 64];
|
||||
let length = u32::try_from(volume.len()).unwrap_or(u32::MAX);
|
||||
// SAFETY: `volume` is writable for the length passed.
|
||||
let find = unsafe { FindFirstVolumeW(volume.as_mut_ptr(), length) };
|
||||
if find.is_null() || find == INVALID_HANDLE_VALUE {
|
||||
return points;
|
||||
}
|
||||
loop {
|
||||
points.extend(volume_paths(&volume));
|
||||
// SAFETY: `find` is a live search handle, and `volume` is writable
|
||||
// for the length passed.
|
||||
if unsafe { FindNextVolumeW(find, volume.as_mut_ptr(), length) } == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// SAFETY: `find` is a live search handle, closed once.
|
||||
unsafe { FindVolumeClose(find) };
|
||||
points
|
||||
}
|
||||
|
||||
/// The paths the volume named by the NUL-terminated `volume` is mounted at.
|
||||
fn volume_paths(volume: &[u16]) -> Vec<PathBuf> {
|
||||
let mut names = vec![0_u16; 1024];
|
||||
loop {
|
||||
let mut needed = 0_u32;
|
||||
let Ok(length) = u32::try_from(names.len()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
// SAFETY: `volume` is NUL-terminated, `names` is writable for the
|
||||
// length passed, and `needed` is a live `u32`.
|
||||
let ok = unsafe {
|
||||
GetVolumePathNamesForVolumeNameW(
|
||||
volume.as_ptr(),
|
||||
names.as_mut_ptr(),
|
||||
length,
|
||||
&raw mut needed,
|
||||
)
|
||||
};
|
||||
if ok != 0 {
|
||||
break;
|
||||
}
|
||||
// Too small: grow to what it asked for, once more at most.
|
||||
let needed = usize::try_from(needed).unwrap_or(0);
|
||||
if needed <= names.len() {
|
||||
return Vec::new();
|
||||
}
|
||||
names.resize(needed, 0);
|
||||
}
|
||||
// NUL-separated names, ended by an empty one.
|
||||
names
|
||||
.split(|&unit| unit == 0)
|
||||
.take_while(|name| !name.is_empty())
|
||||
.map(|name| PathBuf::from(OsString::from_wide(name)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether `path` is a folder another volume is mounted on. Removing one
|
||||
/// would reach onto that volume, which nobody marked.
|
||||
pub fn is_mount_point(path: &Path) -> bool {
|
||||
@@ -573,6 +637,19 @@ mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn the_volume_list_has_the_system_drive() {
|
||||
let drive =
|
||||
std::env::var_os("SystemDrive").unwrap_or_else(|| "C:".into());
|
||||
let root = PathBuf::from(drive).join(Component::RootDir.as_os_str());
|
||||
let points = mount_points();
|
||||
assert!(
|
||||
points.iter().any(|point| same(point, &root)),
|
||||
"{} not in {points:?}",
|
||||
root.display()
|
||||
);
|
||||
}
|
||||
|
||||
fn listed(dir: &Path) -> Vec<Entry> {
|
||||
let mut entries: Vec<Entry> = read_dir(dir)
|
||||
.expect("list")
|
||||
|
||||
Reference in New Issue
Block a user