1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! Daemon management commands — start, stop, status, restart, clear-cache
use anyhow::Result;
use crate::cli::DaemonAction;
use crate::daemon_client;
pub fn run_daemon(action: &DaemonAction) -> Result<()> {
match action {
DaemonAction::Start => {
if daemon_client::is_daemon_running() {
println!("Daemon is already running");
} else {
daemon_client::ensure_daemon_running();
if daemon_client::is_daemon_running() {
println!("Daemon started");
} else {
anyhow::bail!("Failed to start daemon");
}
}
}
DaemonAction::Stop => {
if daemon_client::is_daemon_running() {
daemon_client::send_shutdown();
// Wait for the daemon to actually exit. `send_shutdown` only
// sets an in-process flag; the daemon still needs to drain
// its accept loop and run cache-save + socket-unlink on the
// way out. Poll for up to 5s.
let mut exited = false;
for _ in 0..50 {
if !daemon_client::is_daemon_running() {
exited = true;
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
if exited {
println!("Daemon stopped");
} else {
anyhow::bail!("Daemon did not exit within 5s of shutdown request");
}
} else {
println!("Daemon is not running");
}
}
DaemonAction::Status => {
if daemon_client::is_daemon_running() {
println!("Daemon is running");
} else {
println!("Daemon is not running");
}
}
DaemonAction::Restart => {
if daemon_client::is_daemon_running() {
daemon_client::send_shutdown();
// Wait for the old daemon to actually exit before spawning
// a replacement, otherwise the second instance sees the
// live socket and bails with "daemon already running".
let mut exited = false;
for _ in 0..50 {
if !daemon_client::is_daemon_running() {
exited = true;
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
if !exited {
anyhow::bail!("Daemon did not exit within 5s of shutdown request");
}
}
daemon_client::ensure_daemon_running();
if daemon_client::is_daemon_running() {
println!("Daemon restarted");
} else {
anyhow::bail!("Failed to restart daemon");
}
}
DaemonAction::ClearCache => {
if daemon_client::is_daemon_running() {
daemon_client::send_shutdown();
std::thread::sleep(std::time::Duration::from_millis(200));
}
let data_dir = crate::state::get_data_dir()?;
let mut cleared = Vec::new();
// Per-path banner data cache (written by banner_data_cache.rs) is
// served by the client disk fast path — it must be cleared too,
// otherwise stale banners survive a "clear-cache".
let banner_data_dir = data_dir.join("banner_data");
if banner_data_dir.exists() {
std::fs::remove_dir_all(&banner_data_dir)?;
cleared.push(banner_data_dir);
}
for file_name in ["banner_cache.json", "dir_sizes.json", "fabd.sock"] {
let path = data_dir.join(file_name);
if path.exists() && std::fs::remove_file(&path).is_ok() {
cleared.push(path);
}
}
let cache_dir = directories::ProjectDirs::from("com", "fab", "fab")
.map(|project_dir| project_dir.cache_dir().to_path_buf());
if let Some(cache_dir) = cache_dir {
if cache_dir.exists() {
std::fs::remove_dir_all(&cache_dir)?;
cleared.push(cache_dir);
}
}
if cleared.is_empty() {
println!("No cache files found");
} else {
println!("Cache cleared: {}", cleared.len());
for path in cleared {
println!(" {}", path.display());
}
}
}
}
Ok(())
}