1use crate::errors::AppError;
7use crate::i18n::validation;
8use directories::ProjectDirs;
9use std::path::{Component, Path, PathBuf};
10
11#[derive(Debug, Clone)]
16pub struct AppPaths {
17 pub db: PathBuf,
19 pub models: PathBuf,
21}
22
23impl AppPaths {
24 pub fn resolve(db_override: Option<&str>) -> Result<Self, AppError> {
25 let proj = ProjectDirs::from("", "", "sqlite-graphrag").ok_or_else(|| {
26 AppError::Io(std::io::Error::other("could not determine home directory"))
27 })?;
28
29 let cache_root = if let Some(override_dir) = std::env::var_os("SQLITE_GRAPHRAG_CACHE_DIR") {
30 PathBuf::from(override_dir)
31 } else {
32 proj.cache_dir().to_path_buf()
33 };
34
35 let db = if let Some(p) = db_override {
36 validate_path(p)?;
37 PathBuf::from(p)
38 } else if let Ok(env_path) = std::env::var("SQLITE_GRAPHRAG_DB_PATH") {
39 validate_path(&env_path)?;
40 PathBuf::from(env_path)
41 } else if let Some(home_dir) = home_env_dir()? {
42 home_dir.join("graphrag.sqlite")
43 } else {
44 std::env::current_dir()
45 .map_err(AppError::Io)?
46 .join("graphrag.sqlite")
47 };
48
49 Ok(Self {
50 db,
51 models: cache_root.join("models"),
52 })
53 }
54
55 pub fn ensure_dirs(&self) -> Result<(), AppError> {
56 for dir in [parent_or_err(&self.db)?, self.models.as_path()] {
57 std::fs::create_dir_all(dir)?;
58 }
59 Ok(())
60 }
61}
62
63fn validate_path(p: &str) -> Result<(), AppError> {
64 if Path::new(p).components().any(|c| c == Component::ParentDir) {
65 return Err(AppError::Validation(validation::path_traversal(p)));
66 }
67 Ok(())
68}
69
70fn home_env_dir() -> Result<Option<PathBuf>, AppError> {
76 let raw = match std::env::var("SQLITE_GRAPHRAG_HOME") {
77 Ok(v) => v,
78 Err(_) => return Ok(None),
79 };
80 if raw.is_empty() {
81 return Ok(None);
82 }
83 validate_path(&raw)?;
84 Ok(Some(PathBuf::from(raw)))
85}
86
87pub fn config_dir() -> Result<PathBuf, AppError> {
89 let proj = ProjectDirs::from("", "", "sqlite-graphrag").ok_or_else(|| {
90 AppError::Io(std::io::Error::other(
91 "could not determine home directory for config",
92 ))
93 })?;
94 Ok(proj.config_dir().to_path_buf())
95}
96
97pub(crate) fn parent_or_err(path: &Path) -> Result<&Path, AppError> {
98 path.parent().ok_or_else(|| {
99 AppError::Validation(format!(
100 "path '{}' has no valid parent component",
101 path.display()
102 ))
103 })
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109 use serial_test::serial;
110 use tempfile::TempDir;
111
112 fn clean_env_paths() {
115 unsafe {
117 std::env::remove_var("SQLITE_GRAPHRAG_HOME");
118 std::env::remove_var("SQLITE_GRAPHRAG_DB_PATH");
119 std::env::remove_var("SQLITE_GRAPHRAG_CACHE_DIR");
120 }
121 }
122
123 #[test]
124 #[serial]
125 fn home_env_resolves_db_in_subdir() {
126 clean_env_paths();
127 let tmp = TempDir::new().expect("tempdir");
128 unsafe {
130 std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp.path());
131 }
132
133 let paths = AppPaths::resolve(None).expect("resolve with valid HOME");
134 assert_eq!(paths.db, tmp.path().join("graphrag.sqlite"));
135
136 clean_env_paths();
137 }
138
139 #[test]
140 #[serial]
141 fn home_env_traversal_rejected() {
142 clean_env_paths();
143 unsafe {
145 std::env::set_var("SQLITE_GRAPHRAG_HOME", "/tmp/../etc");
146 }
147
148 let result = AppPaths::resolve(None);
149 assert!(
150 matches!(result, Err(AppError::Validation(_))),
151 "traversal in SQLITE_GRAPHRAG_HOME must fail as Validation, got {result:?}"
152 );
153
154 clean_env_paths();
155 }
156
157 #[test]
158 #[serial]
159 fn db_path_overrides_home() {
160 clean_env_paths();
161 let tmp_home = TempDir::new().expect("tempdir home");
162 let tmp_db = TempDir::new().expect("tempdir db");
163 let explicit_db = tmp_db.path().join("explicit.sqlite");
164 unsafe {
166 std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp_home.path());
167 std::env::set_var("SQLITE_GRAPHRAG_DB_PATH", &explicit_db);
168 }
169
170 let paths = AppPaths::resolve(None).expect("resolve with DB_PATH and HOME");
171 assert_eq!(paths.db, explicit_db);
172
173 clean_env_paths();
174 }
175
176 #[test]
177 #[serial]
178 fn flag_overrides_home() {
179 clean_env_paths();
180 let tmp_home = TempDir::new().expect("tempdir home");
181 let tmp_flag = TempDir::new().expect("tempdir flag");
182 let db_flag = tmp_flag.path().join("via-flag.sqlite");
183 unsafe {
185 std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp_home.path());
186 }
187
188 let paths = AppPaths::resolve(Some(db_flag.to_str().expect("utf8")))
189 .expect("resolve with flag and HOME");
190 assert_eq!(paths.db, db_flag);
191
192 clean_env_paths();
193 }
194
195 #[test]
196 #[serial]
197 fn home_env_empty_falls_back_to_cwd() {
198 clean_env_paths();
199 unsafe {
201 std::env::set_var("SQLITE_GRAPHRAG_HOME", "");
202 }
203
204 let paths = AppPaths::resolve(None).expect("resolve with empty HOME");
205 let expected = std::env::current_dir()
206 .expect("cwd")
207 .join("graphrag.sqlite");
208 assert_eq!(paths.db, expected);
209
210 clean_env_paths();
211 }
212
213 #[test]
214 fn parent_or_err_accepts_normal_path() {
215 let p = PathBuf::from("/home/user/db.sqlite");
216 let parent = parent_or_err(&p).expect("valid parent");
217 assert_eq!(parent, Path::new("/home/user"));
218 }
219
220 #[test]
221 fn parent_or_err_accepts_relative_path() {
222 let p = PathBuf::from("subdir/file.sqlite");
223 let parent = parent_or_err(&p).expect("relative parent");
224 assert_eq!(parent, Path::new("subdir"));
225 }
226
227 #[test]
228 fn parent_or_err_rejects_unix_root() {
229 let p = PathBuf::from("/");
230 let result = parent_or_err(&p);
231 assert!(matches!(result, Err(AppError::Validation(_))));
232 }
233
234 #[test]
235 fn parent_or_err_rejects_empty_path() {
236 let p = PathBuf::from("");
237 let result = parent_or_err(&p);
238 assert!(matches!(result, Err(AppError::Validation(_))));
239 }
240}