Config: refuse a data_dir equal to or inside the cache_dir (#38, #42)

The cache may be deleted at any time; saves and settings kept inside it
would go with it. `is_within` compares resolved whole components, so a
sibling such as `cache-old` is still accepted, and a cache inside the
data folder remains allowed.

Ported from uiliannn/VItoriaBr 30d9ba1 (pull requests #38 and #42).

(cherry picked from commit 0f52dbd892f2e9af7a84e0edc9bec635ec42dc32)
This commit is contained in:
Luiz Gustavo Benicio Neves authored and trustno1-coder committed 2026-11-27 17:29:36 -08:00
1 parent aa7dd70250
commit ee69dc0e1d
2 files changed
+48 -1

No files matched your search

+7
View File
@@ -62,6 +62,13 @@ pub fn ensure_outside_repo(path: &Path) -> Result<(), PolicyError> {
Ok(())
}
/// True if `path` is `root` or lies below it, after resolving symlinks and
/// `..` (neither path has to exist yet). Compares whole components, so
/// `cache-old` is not inside `cache`.
pub fn is_within(path: &Path, root: &Path) -> Result<bool, PolicyError> {
Ok(resolve(path)?.starts_with(resolve(root)?))
}
/// Canonicalizes the longest existing prefix of `path` and re-appends the
/// rest, so that paths which do not exist yet can still be compared.
fn resolve(path: &Path) -> Result<PathBuf, PolicyError> {