Skip to content

Commit 6919352

Browse files
committed
Use flow-state for runtime files when config dir is not a directory
1 parent c421130 commit 6919352

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/config.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,26 @@ pub fn ensure_global_config_dir() -> Result<PathBuf> {
798798
Ok(dir)
799799
}
800800

801+
/// Global state directory for runtime data.
802+
pub fn global_state_dir() -> PathBuf {
803+
let config_dir = global_config_dir();
804+
if is_dir_path(&config_dir) {
805+
return config_dir;
806+
}
807+
808+
config_dir.with_file_name("flow-state")
809+
}
810+
811+
/// Ensure the global state directory exists.
812+
pub fn ensure_global_state_dir() -> Result<PathBuf> {
813+
let dir = global_state_dir();
814+
if let Some(parent) = dir.parent() {
815+
ensure_dir(parent)?;
816+
}
817+
ensure_dir(&dir)?;
818+
Ok(dir)
819+
}
820+
801821
fn ensure_dir(path: &Path) -> Result<()> {
802822
if let Ok(meta) = fs::symlink_metadata(path) {
803823
let is_dir = meta.is_dir();
@@ -833,6 +853,20 @@ fn ensure_dir(path: &Path) -> Result<()> {
833853
Ok(())
834854
}
835855

856+
fn is_dir_path(path: &Path) -> bool {
857+
if let Ok(meta) = fs::symlink_metadata(path) {
858+
if meta.is_dir() {
859+
return true;
860+
}
861+
if meta.file_type().is_symlink() {
862+
if let Ok(target_meta) = fs::metadata(path) {
863+
return target_meta.is_dir();
864+
}
865+
}
866+
}
867+
false
868+
}
869+
836870
fn backup_path(path: &Path) -> PathBuf {
837871
let name = path
838872
.file_name()

src/history.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl InvocationRecord {
5959

6060
pub fn record(invocation: InvocationRecord) -> Result<()> {
6161
let path = history_path();
62-
let _ = config::ensure_global_config_dir()
62+
let _ = config::ensure_global_state_dir()
6363
.with_context(|| format!("failed to create history dir {}", path.display()))?;
6464

6565
let mut file = OpenOptions::new()
@@ -194,12 +194,7 @@ pub fn load_last_record_for_project(project_root: &Path) -> Result<Option<Invoca
194194
}
195195

196196
fn history_path() -> PathBuf {
197-
std::env::var_os("HOME")
198-
.map(PathBuf::from)
199-
.unwrap_or_else(|| PathBuf::from("."))
200-
.join(".config")
201-
.join("flow")
202-
.join("history.jsonl")
197+
config::global_state_dir().join("history.jsonl")
203198
}
204199

205200
fn now_ms() -> u128 {

src/running.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ pub struct RunningProcesses {
4242

4343
/// Returns ~/.config/flow/running.json
4444
pub fn running_processes_path() -> PathBuf {
45-
std::env::var_os("HOME")
46-
.map(PathBuf::from)
47-
.unwrap_or_else(|| PathBuf::from("."))
48-
.join(".config/flow/running.json")
45+
crate::config::global_state_dir().join("running.json")
4946
}
5047

5148
/// Load running processes, validating that PIDs are still alive
@@ -81,7 +78,7 @@ pub fn load_running_processes() -> Result<RunningProcesses> {
8178
/// Atomically save running processes (write to temp, then rename)
8279
pub fn save_running_processes(processes: &RunningProcesses) -> Result<()> {
8380
let path = running_processes_path();
84-
let _ = crate::config::ensure_global_config_dir()
81+
let _ = crate::config::ensure_global_state_dir()
8582
.with_context(|| format!("failed to create {}", path.display()))?;
8683

8784
let temp_path = path.with_extension("json.tmp");

0 commit comments

Comments
 (0)