6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | env, fs, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | os::unix::fs::{DirBuilderExt, PermissionsExt}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | path::{Path, PathBuf}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | fn home() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | env::var_os("HOME").map_or_else(|| PathBuf::from("/"), PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | fn xdg(var: &str, fallback: &str) -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | env::var_os(var) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | .map(PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | .filter(|p| p.is_absolute()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | .unwrap_or_else(|| home().join(fallback)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | pub fn config_file() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | env::var_os("ROUNDABOUT_CONFIG").map_or_else( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | || xdg("XDG_CONFIG_HOME", ".config").join("roundabout/config.toml"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | PathBuf::from, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | ) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | pub fn state_dir() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | let dir = env::var_os("ROUNDABOUT_STATE_DIR").map_or_else( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | || xdg("XDG_STATE_HOME", ".local/state").join("roundabout"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | PathBuf::from, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | let _ = fs::create_dir_all(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | dir |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | pub fn runtime_dir() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | if let Some(dir) = env::var_os("ROUNDABOUT_RUNTIME_DIR") { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | let dir = PathBuf::from(dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | let _ = fs::DirBuilder::new() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | .recursive(true) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | .mode(0o700) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | .create(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | return dir; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | if let Some(dir) = env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | && dir.is_absolute() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | && dir.is_dir() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | return dir; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | let uid = unsafe { libc::getuid() }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | let dir = env::temp_dir().join(format!("roundabout-{uid}")); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | let _ = fs::DirBuilder::new().mode(0o700).create(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | let _ = fs::set_permissions(&dir, fs::Permissions::from_mode(0o700)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | dir |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | pub fn socket() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | runtime_dir().join("roundabout.sock") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | pub fn spawn_lock() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | runtime_dir().join("roundabout.lock") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | pub fn project_root(cwd: &Path) -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | const MARKERS: [&str; 6] = [ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | ".jj", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | ".git", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | ".hg", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | "flake.nix", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | "Cargo.toml", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | "package.json", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | ]; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | let mut best = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | for dir in cwd.ancestors() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | if dir.join(".jj").exists() || dir.join(".git").exists() || dir.join(".hg").exists() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | return dir.to_path_buf(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 78 | if best.is_none() && MARKERS.iter().any(|m| dir.join(m).exists()) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 79 | best = Some(dir.to_path_buf()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 80 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 81 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 82 | best.unwrap_or_else(|| cwd.to_path_buf()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 83 | } |