use std::path::{Path, PathBuf};
pub(crate) const DISABLED_MARKER_FILE: &str = "daemon-disabled";
pub(crate) fn disabled_marker_path(config_dir: &Path) -> PathBuf {
config_dir.join(DISABLED_MARKER_FILE)
}
pub(crate) fn is_daemon_disabled(config_dir: &Path) -> bool {
disabled_marker_path(config_dir).exists()
}
pub(crate) fn write_disabled_marker(config_dir: &Path) -> std::io::Result<bool> {
let already = is_daemon_disabled(config_dir);
std::fs::create_dir_all(config_dir)?;
let body = "This file disables the Freenet background daemon.\n\
While it exists, `freenet network` refuses to start and stays idle instead,\n\
so no service supervisor can bring the node back across restarts or reboots.\n\
Delete this file, or run `freenet service enable`, to re-enable the daemon.\n";
std::fs::write(disabled_marker_path(config_dir), body)?;
Ok(already)
}
pub(crate) fn remove_disabled_marker(config_dir: &Path) -> std::io::Result<bool> {
match std::fs::remove_file(disabled_marker_path(config_dir)) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn disabled_marker_roundtrip() {
let tmp = tempfile::tempdir().unwrap();
let config_dir = tmp.path().join("nested").join("freenet-config");
assert!(!is_daemon_disabled(&config_dir));
let already = write_disabled_marker(&config_dir).unwrap();
assert!(!already, "first disable must report not-already-disabled");
assert!(is_daemon_disabled(&config_dir));
assert!(disabled_marker_path(&config_dir).exists());
let already = write_disabled_marker(&config_dir).unwrap();
assert!(already, "second disable must report already-disabled");
assert!(is_daemon_disabled(&config_dir));
let was_disabled = remove_disabled_marker(&config_dir).unwrap();
assert!(was_disabled, "enable must report the daemon was disabled");
assert!(!is_daemon_disabled(&config_dir));
let was_disabled = remove_disabled_marker(&config_dir).unwrap();
assert!(!was_disabled, "second enable must report nothing to do");
assert!(!is_daemon_disabled(&config_dir));
}
#[test]
fn disabled_marker_path_is_under_config_dir() {
let dir = Path::new("/some/config");
assert_eq!(disabled_marker_path(dir), dir.join(DISABLED_MARKER_FILE));
assert!(disabled_marker_path(dir).starts_with(dir));
}
#[test]
fn marker_contents_are_present_but_not_load_bearing() {
let tmp = tempfile::tempdir().unwrap();
let config_dir = tmp.path().to_path_buf();
std::fs::write(disabled_marker_path(&config_dir), "").unwrap();
assert!(is_daemon_disabled(&config_dir));
}
}