From da2d7274ecba9869de922e76350eac0e3b000111 Mon Sep 17 00:00:00 2001 From: OrIOg Date: Mon, 26 Jun 2023 22:34:30 +0200 Subject: [PATCH] fix: remove superfluous asteriks recursively --- plugins/persisted-scope/src/lib.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/persisted-scope/src/lib.rs b/plugins/persisted-scope/src/lib.rs index 60f2ef0f..11cd9196 100644 --- a/plugins/persisted-scope/src/lib.rs +++ b/plugins/persisted-scope/src/lib.rs @@ -76,6 +76,26 @@ fn detect_scope_type(scope_state_path: &str) -> TargetType { } } +fn fix_directory(path: &str) -> &str { + if let Some(path) = path.strip_suffix(format!("{}{}", MAIN_SEPARATOR, "*").as_str()) { + return fix_directory(path); + } else if let Some(path) = path.strip_suffix(format!("{}{}", "*", MAIN_SEPARATOR).as_str()) { + return fix_directory(path); + } + + path +} + +fn fix_recursive_directory(path: &str) -> &str { + if let Some(path) = path.strip_suffix(format!("{}{}", MAIN_SEPARATOR, "**").as_str()) { + return fix_recursive_directory(path); + } else if let Some(path) = path.strip_suffix(format!("{}{}", "**", MAIN_SEPARATOR).as_str()) { + return fix_recursive_directory(path); + } + + path +} + fn allow_path(scope: &FsScope, path: &str) { let target_type = detect_scope_type(path); @@ -85,11 +105,11 @@ fn allow_path(scope: &FsScope, path: &str) { } TargetType::Directory => { // We remove the '*' at the end of it, else it will be escaped by the pattern. - let _ = scope.allow_directory(&path[..path.len() - 1], false); + let _ = scope.allow_directory(fix_directory(path), false); } TargetType::RecursiveDirectory => { // We remove the '**' at the end of it, else it will be escaped by the pattern. - let _ = scope.allow_directory(&path[..path.len() - 2], true); + let _ = scope.allow_directory(fix_recursive_directory(path), true); } } }