Ref: Extrahiert Funktionen für gemountete Dateisysteme in eigenes Modul.

This commit is contained in:
2025-08-20 23:31:25 +02:00
parent 11d748aa68
commit 32874990ea
2 changed files with 89 additions and 89 deletions

View File

@@ -1,89 +1,2 @@
mod mount; pub mod mount;
pub mod mounted;
use std::process::Command;
/// Überprüft, ob ein Dateisystem am angegebenen Mount-Point mit dem spezifizierten Mount-Typ eingebunden ist.
///
/// # Parameter
/// * `mount_point` - Der Pfad, an dem das Dateisystem eingebunden sein soll
/// * `mount_type` - Der erwartete Mount-Typ des Dateisystems (z.B. nfs, smb, davfs2)
///
/// # Rückgabewert
/// * `true` wenn das Dateisystem mit dem angegebenen Typ eingebunden ist
/// * `false` wenn das Dateisystem nicht oder mit einem anderen Typ eingebunden ist
pub fn is_mounted_as(mount_point: &str, mount_type: &str) -> bool {
#[cfg(target_os = "windows")]
{
let drive_letter = &mount_point[0..1];
let output = Command::new("powershell")
.args([
"-Command",
&format!("(Get-PSDrive -Name {} -PSProvider 'FileSystem').Description", drive_letter)
])
.output()
.expect("Failed to execute get-psdrive command");
if !output.status.success() {
return false;
}
let drive_type = String::from_utf8_lossy(&output.stdout).trim().to_string();
drive_type == mount_type
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("mount")
.output()
.expect("Failed to execute mount command");
if !output.status.success() {
return false;
}
let mount_output = String::from_utf8_lossy(&output.stdout);
mount_output
.lines()
.any(|line| line.contains(mount_point) && line.contains(mount_type))
}
}
/// Überprüft, ob ein Dateisystem am angegebenen Mount-Point eingebunden ist.
///
/// # Parameter
/// * `mount_point` - Der Pfad, an dem das Dateisystem eingebunden sein soll
///
/// # Rückgabewert
/// * `true` wenn ein Dateisystem am angegebenen Pfad eingebunden ist
/// * `false` wenn kein Dateisystem eingebunden ist
pub fn is_mounted(mount_point: &str) -> bool {
#[cfg(target_os = "windows")]
{
let drive_letter = &mount_point[0..1];
let output = Command::new("powershell")
.args([
"-Command",
&format!("(Get-PSDrive -Name {} -PSProvider 'FileSystem')", drive_letter)
])
.output()
.expect("Failed to execute get-psdrive command");
output.status.success()
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("mount")
.output()
.expect("Failed to execute mount command");
if !output.status.success() {
return false;
}
let mount_output = String::from_utf8_lossy(&output.stdout);
mount_output.lines().any(|line| line.contains(mount_point))
}
}

87
src/filesystem/mounted.rs Normal file
View File

@@ -0,0 +1,87 @@
use std::process::Command;
/// Überprüft, ob ein Dateisystem am angegebenen Mount-Point mit dem spezifizierten Mount-Typ eingebunden ist.
///
/// # Parameter
/// * `mount_point` - Der Pfad, an dem das Dateisystem eingebunden sein soll
/// * `mount_type` - Der erwartete Mount-Typ des Dateisystems (z.B. nfs, smb, davfs2)
///
/// # Rückgabewert
/// * `true` wenn das Dateisystem mit dem angegebenen Typ eingebunden ist
/// * `false` wenn das Dateisystem nicht oder mit einem anderen Typ eingebunden ist
pub fn is_mounted_as(mount_point: &str, mount_type: &str) -> bool {
#[cfg(target_os = "windows")]
{
let drive_letter = &mount_point[0..1];
let output = Command::new("powershell")
.args([
"-Command",
&format!("(Get-PSDrive -Name {} -PSProvider 'FileSystem').Description", drive_letter)
])
.output()
.expect("Failed to execute get-psdrive command");
if !output.status.success() {
return false;
}
let drive_type = String::from_utf8_lossy(&output.stdout).trim().to_string();
drive_type == mount_type
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("mount")
.output()
.expect("Failed to execute mount command");
if !output.status.success() {
return false;
}
let mount_output = String::from_utf8_lossy(&output.stdout);
mount_output
.lines()
.any(|line| line.contains(mount_point) && line.contains(mount_type))
}
}
/// Überprüft, ob ein Dateisystem am angegebenen Mount-Point eingebunden ist.
///
/// # Parameter
/// * `mount_point` - Der Pfad, an dem das Dateisystem eingebunden sein soll
///
/// # Rückgabewert
/// * `true` wenn ein Dateisystem am angegebenen Pfad eingebunden ist
/// * `false` wenn kein Dateisystem eingebunden ist
pub fn is_mounted(mount_point: &str) -> bool {
#[cfg(target_os = "windows")]
{
let drive_letter = &mount_point[0..1];
let output = Command::new("powershell")
.args([
"-Command",
&format!("(Get-PSDrive -Name {} -PSProvider 'FileSystem')", drive_letter)
])
.output()
.expect("Failed to execute get-psdrive command");
output.status.success()
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("mount")
.output()
.expect("Failed to execute mount command");
if !output.status.success() {
return false;
}
let mount_output = String::from_utf8_lossy(&output.stdout);
mount_output.lines().any(|line| line.contains(mount_point))
}
}