Fix: Bereinigt Mount-/Unmount-Logik und verschiebt WebDAV-Anmeldeinformationen in richtigen Ablauf

This commit is contained in:
2025-08-23 17:30:28 +02:00
parent c57ffe4580
commit c2d92bb807

View File

@@ -1,69 +1,113 @@
use crate::config::{get_config, modify_config, Storage};
use crate::config::{Storage, get_config, modify_config};
use crate::filesystem::credentials::save_credentials_webdav;
use crate::filesystem::mount::{mount, unmount};
use crate::filesystem::mounted::{is_mounted, is_mounted_as};
use crate::log::log;
use crate::log::LogLevel;
use crate::log::log;
use crate::network::network_interface::{get_active_network_interface, get_interface_ip_address};
use crate::network::utils::{get_ip_from_mac, get_mac_from_ip, get_network_address, is_reachable, wake_on_land};
use crate::network::utils::{
get_ip_from_mac, get_mac_from_ip, get_network_address, is_reachable, wake_on_land,
};
use crate::sudo::{is_run_as_root, run_as_root};
use std::process::exit;
use std::thread;
use std::thread::{sleep, JoinHandle};
use std::thread::{JoinHandle, sleep};
use std::time::Duration;
mod config;
mod filesystem;
mod log;
mod network;
mod config;
mod program;
mod filesystem;
mod sudo;
fn main() {
log("main", "========== PROGRAM START ==========", LogLevel::Info);
log(
"main",
"========== PROGRAM START ==========",
LogLevel::Info,
);
if !is_run_as_root() {
log("main", "Program is not run as root. Trying to run as root...", LogLevel::Warn);
log(
"main",
"Program is not run as root. Trying to run as root...",
LogLevel::Warn,
);
run_as_root();
}
let network_interface : String;
let network_interface: String;
let mut count : i32 = 0;
let mut count: i32 = 0;
loop {
let interface_str : String = get_active_network_interface().unwrap().trim().to_string();
let interface_str: String = get_active_network_interface().unwrap().trim().to_string();
if !interface_str.is_empty() {
network_interface = interface_str;
break;
} else if count >= 10 {
log("main", "Couldn't find active network card, exiting.", LogLevel::Error);
log(
"main",
"Couldn't find active network card, exiting.",
LogLevel::Error,
);
exit(1);
}
log("main", "No active network card found, waiting 1 second.", LogLevel::Warn);
count = count+1;
log(
"main",
"No active network card found, waiting 1 second.",
LogLevel::Warn,
);
count = count + 1;
sleep(Duration::from_secs(1));
}
log("main", &*format!("Active network interface found: {}", network_interface), LogLevel::Info);
log(
"main",
&*format!("Active network interface found: {}", network_interface),
LogLevel::Info,
);
let interface_address = get_interface_ip_address(network_interface.as_str()).unwrap().trim().to_string();
log("main", &*format!("Interface address: {}", interface_address), LogLevel::Info);
let interface_address = get_interface_ip_address(network_interface.as_str())
.unwrap()
.trim()
.to_string();
log(
"main",
&*format!("Interface address: {}", interface_address),
LogLevel::Info,
);
let network_address = get_network_address(interface_address.as_str()).unwrap().trim().to_string();
log("main", &*format!("Network address: {}", network_address), LogLevel::Info);
let network_address = get_network_address(interface_address.as_str())
.unwrap()
.trim()
.to_string();
log(
"main",
&*format!("Network address: {}", network_address),
LogLevel::Info,
);
count = 0;
loop {
if is_reachable(network_address.as_str()) {
break;
} else if count >= 10 {
log("main", "Couldn't reach network address, exiting.", LogLevel::Error);
log(
"main",
"Couldn't reach network address, exiting.",
LogLevel::Error,
);
exit(1);
}
log("main", "Network address not reachable, waiting 1 second.", LogLevel::Warn);
count = count+1;
log(
"main",
"Network address not reachable, waiting 1 second.",
LogLevel::Warn,
);
count = count + 1;
sleep(Duration::from_secs(1));
}
log("main", "Network address is reachable.", LogLevel::Info);
@@ -78,7 +122,11 @@ fn main() {
}
fn mount_local(network_address: String) {
log("main", "Trying to mount filesystem locally...", LogLevel::Info);
log(
"main",
"Trying to mount filesystem locally...",
LogLevel::Info,
);
let mount_point: &str = get_config().general.mount_point.as_str();
@@ -91,18 +139,63 @@ fn mount_local(network_address: String) {
device_address = Some(get_config().storage.clone().unwrap().device_ip);
if is_reachable(&device_address.clone().unwrap()) {
log("main", format!("Searching mac for device address {}.", device_address.clone().unwrap()).as_str(), LogLevel::Info);
let mac_of_ip = get_mac_from_ip(device_address.clone().unwrap().as_str()).unwrap_or("".to_string());
log("main", format!("Found mac {} for device address {}.", mac_of_ip, device_address.clone().unwrap()).as_str(), LogLevel::Info);
log(
"main",
format!(
"Searching mac for device address {}.",
device_address.clone().unwrap()
)
.as_str(),
LogLevel::Info,
);
let mac_of_ip =
get_mac_from_ip(device_address.clone().unwrap().as_str()).unwrap_or("".to_string());
log(
"main",
format!(
"Found mac {} for device address {}.",
mac_of_ip,
device_address.clone().unwrap()
)
.as_str(),
LogLevel::Info,
);
if mac_of_ip == mac_address {
log("main", format!("Found device mac {} on saved ip {}.", mac_address, device_address.clone().unwrap()).as_str(), LogLevel::Info);
log(
"main",
format!(
"Found device mac {} on saved ip {}.",
mac_address,
device_address.clone().unwrap()
)
.as_str(),
LogLevel::Info,
);
} else {
log("main", format!("Device mac {} is not the same as {} of ip {}.", mac_address, mac_of_ip, device_address.clone().unwrap()).as_str(), LogLevel::Warn);
log(
"main",
format!(
"Device mac {} is not the same as {} of ip {}.",
mac_address,
mac_of_ip,
device_address.clone().unwrap()
)
.as_str(),
LogLevel::Warn,
);
device_address = None;
}
} else {
log("main", format!("Device address {} is not reachable.", device_address.clone().unwrap()).as_str(), LogLevel::Warn);
log(
"main",
format!(
"Device address {} is not reachable.",
device_address.clone().unwrap()
)
.as_str(),
LogLevel::Warn,
);
device_address = None;
}
}
@@ -121,7 +214,11 @@ fn mount_local(network_address: String) {
break;
}
log("main", "Couldn't find MAC adress in local network, sending awake call...", LogLevel::Info);
log(
"main",
"Couldn't find MAC adress in local network, sending awake call...",
LogLevel::Info,
);
wake_on_land(mac_address);
log("main", "Waiting 30 seconds...", LogLevel::Info);
@@ -131,28 +228,52 @@ fn mount_local(network_address: String) {
}
if device_address.is_none() {
log("main", "Couldn't find device address for MAC address.", LogLevel::Warn);
log(
"main",
"Couldn't find device address for MAC address.",
LogLevel::Warn,
);
if is_mounted_as(mount_point, mount_type) {
log("main", "Filesystem is mounted locally. Unmounting...", LogLevel::Info);
log(
"main",
"Filesystem is mounted locally. Unmounting...",
LogLevel::Info,
);
unmount(mount_point);
}
mount_remote();
} else {
let dev_ip: String = device_address.unwrap();
log("main", "Found MAC address in local network.", LogLevel::Info);
log(
"main",
"Found MAC address in local network.",
LogLevel::Info,
);
if !is_mounted_as(mount_point, mount_type) {
if is_mounted_as(mount_point, get_config().remote.mount_type.as_str()) {
log("main", "Filesystem is mounted remotely. Unmounting...", LogLevel::Info);
if is_mounted(mount_point) {
log(
"main",
"Filesystem is mounted. Unmounting...",
LogLevel::Info,
);
unmount(mount_point);
}
log("main", "Mounting local filesystem...", LogLevel::Info);
mount(mount_point, &*format!("{}:{}", dev_ip, get_config().local.mount_path), mount_type);
mount(
mount_point,
&*format!("{}:{}", dev_ip, get_config().local.mount_path),
mount_type,
);
} else {
log("main", "Filesystem is already mounted locally. Doing nothing.", LogLevel::Info);
log(
"main",
"Filesystem is already mounted locally. Doing nothing.",
LogLevel::Info,
);
}
}
}
@@ -171,26 +292,40 @@ fn mount_remote() {
break;
}
log("main", "Couldn't reach remote server, waiting 1 second.", LogLevel::Warn);
log(
"main",
"Couldn't reach remote server, waiting 1 second.",
LogLevel::Warn,
);
count = count + 1;
sleep(Duration::from_secs(1));
}
if could_reach {
log("main", "Remote server reachable.", LogLevel::Info);
if is_mounted_as(mount_point, get_config().local.mount_type.as_str()) ||
is_mounted_as(mount_point, mount_type) {
log("main", "Filesystem is already mounted. Doing nothing.", LogLevel::Info);
if mount_type == "davfs" || mount_type == "webdav" {
save_credentials_webdav();
}
if is_mounted_as(mount_point, get_config().local.mount_type.as_str())
|| is_mounted_as(mount_point, mount_type)
{
log(
"main",
"Filesystem is already mounted. Doing nothing.",
LogLevel::Info,
);
} else {
if is_mounted(mount_point) {
log("main", "Filesystem is already mounted. Unmounting...", LogLevel::Info);
log(
"main",
"Filesystem is already mounted. Unmounting...",
LogLevel::Info,
);
unmount(mount_point);
}
if mount_type == "davfs" || mount_type == "webdav" {
save_credentials_webdav();
}
log("main", "Mounting remote filesystem...", LogLevel::Info);
mount(mount_point, &*get_config().remote.mount_path, mount_type);
}