ryra_core/paths.rs
1//! Filesystem paths ryra reads and writes.
2//!
3//! The directory name is `services/` (not `ryra/`) because the deployments
4//! are the user's — ryra is just the scaffolding tool that puts them there.
5//! Wiping `~/.local/share/services/`, `~/.config/services/`, and the
6//! ryra-managed quadlets in `~/.config/containers/systemd/` removes ryra's
7//! footprint completely.
8
9use std::path::PathBuf;
10
11use crate::error::{Error, Result};
12
13/// Sentinel value for `InstalledService.repo` meaning "came from the
14/// default registry" (the project-managed git repo at
15/// [`DEFAULT_REGISTRY_URL`]) rather than a user-added custom registry.
16pub const REGISTRY_DEFAULT: &str = "default";
17
18/// Git URL of the default service registry. Cloned on first
19/// `ryra add`/`ryra search` into `<cache>/default/` and updated by
20/// `ryra registry update`.
21///
22/// Tests and dev workflows can short-circuit the clone by setting
23/// [`REGISTRY_DIR_ENV`] to a local directory; the resolver uses that
24/// path verbatim instead.
25pub const DEFAULT_REGISTRY_URL: &str = "https://github.com/ryanravn/ryra-registry.git";
26
27/// Env var that, when set to an existing directory, replaces the git
28/// fetch entirely — ryra uses that directory as the default registry
29/// verbatim (no clone, no pull). The E2E test harness sets this to
30/// `/opt/ryra-test-registry` inside the VM; dev workflows can point it
31/// at a local checkout to iterate without committing/pushing.
32pub const REGISTRY_DIR_ENV: &str = "RYRA_REGISTRY_DIR";
33
34/// Env var that, when set, overrides the directory holding
35/// `preferences.toml` (normally `~/.config/services/`). The E2E test
36/// harness points this at a throwaway dir for host (bare-mode) runs so
37/// tests never read or clobber the user's real SMTP/auth/backup
38/// credentials. Only the preferences/config dir moves — service data
39/// (`~/.local/share/services`) and quadlets (`~/.config/containers/systemd`)
40/// stay put, because `systemctl --user` reads those from fixed locations.
41pub const CONFIG_DIR_ENV: &str = "RYRA_CONFIG_DIR";
42
43/// Env var that, when set, overrides the service-data root (normally
44/// `~/.local/share/services/`). The host test harness points this at a
45/// sandbox (`~/.local/share/services-test/services/`) so test deployments
46/// never share a directory with the user's real services. Because ryra
47/// stores each quadlet *inside* `service_home` and only symlinks it into
48/// the systemd quadlet dir, moving this also moves the unit files; the
49/// quadlet *symlink* still lands in the fixed `~/.config/containers/systemd`.
50pub const DATA_DIR_ENV: &str = "RYRA_DATA_DIR";
51
52/// The active `RYRA_DATA_DIR` override, if any. `None` for normal installs
53/// (the common case) — callers use that to keep behaviour byte-identical
54/// when no sandbox is requested.
55pub(crate) fn data_dir_override() -> Option<PathBuf> {
56 match std::env::var_os(DATA_DIR_ENV) {
57 Some(v) if !v.is_empty() => Some(PathBuf::from(v)),
58 _ => None,
59 }
60}
61
62/// Resolve the user's home directory, falling back to $HOME.
63pub(crate) fn home_dir() -> Result<PathBuf> {
64 dirs::home_dir()
65 .or_else(|| std::env::var("HOME").ok().map(PathBuf::from))
66 .ok_or(Error::HomeDirNotFound)
67}
68
69/// Root directory holding every installed service's home dir:
70/// `~/.local/share/services/`.
71pub fn service_data_root() -> Result<PathBuf> {
72 if let Some(dir) = data_dir_override() {
73 return Ok(dir);
74 }
75 let base = match dirs::data_dir() {
76 Some(d) => d,
77 None => home_dir()?.join(".local").join("share"),
78 };
79 Ok(base.join("services"))
80}
81
82/// Data directory for a service: `~/.local/share/services/<name>`
83///
84/// Rejects path-like names before the join: `PathBuf::join` with an
85/// absolute path REPLACES the base, so an unvalidated name like
86/// `/home/user/project` would make this return that very directory,
87/// and a purge would then delete it. A test-harness bug did exactly
88/// that once; never again.
89pub fn service_home(service_name: &str) -> Result<PathBuf> {
90 if service_name.is_empty()
91 || service_name == "."
92 || service_name == ".."
93 || service_name.contains('/')
94 || service_name.contains('\\')
95 {
96 return Err(Error::ConfigValidation(format!(
97 "invalid service name '{service_name}': names must not be paths"
98 )));
99 }
100 Ok(service_data_root()?.join(service_name))
101}
102
103/// Per-install metadata file: `~/.local/share/services/<name>/metadata.toml`.
104/// Stores the install-time decisions (registry, exposure, url, auth) so
105/// later commands can reconstruct the install without scraping comments.
106pub fn metadata_path(service_name: &str) -> Result<PathBuf> {
107 Ok(service_home(service_name)?.join("metadata.toml"))
108}
109
110/// Quadlet directory: ~/.config/containers/systemd
111pub fn quadlet_dir() -> Result<PathBuf> {
112 let base = match dirs::config_dir() {
113 Some(d) => d,
114 None => home_dir()?.join(".config"),
115 };
116 Ok(base.join("containers").join("systemd"))
117}
118
119/// systemd `--user` unit directory: `~/.config/systemd/user`. Where native
120/// (non-quadlet) service units are linked so `systemctl --user` finds them —
121/// the analogue of [`quadlet_dir`] for `runtime = "native"` services.
122pub fn systemd_user_dir() -> Result<PathBuf> {
123 let base = match dirs::config_dir() {
124 Some(d) => d,
125 None => home_dir()?.join(".config"),
126 };
127 Ok(base.join("systemd").join("user"))
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn service_home_rejects_path_like_names() {
136 // An absolute or traversing name must never escape the data root
137 // (PathBuf::join with an absolute path replaces the base). A real
138 // purge once deleted a whole repo this way.
139 for bad in [
140 "/home/user/code/ryra-api",
141 ".",
142 "..",
143 "../x",
144 "a/b",
145 "a\\b",
146 "",
147 ] {
148 assert!(
149 service_home(bad).is_err(),
150 "expected '{bad}' to be rejected as a service name"
151 );
152 }
153 }
154
155 #[test]
156 fn service_home_accepts_plain_names() {
157 // Plain registry-style names still resolve (under the data root).
158 for good in ["forgejo", "ryra-api", "node-exporter", "caddy"] {
159 let home = service_home(good).expect("plain name should resolve");
160 assert!(home.ends_with(good));
161 }
162 }
163}