Add separate normalization for registry paths

This commit is contained in:
mtkennerly 2024-04-21 21:18:51 -04:00
parent a7cc7d5fd3
commit e7a8647c17
No known key found for this signature in database
GPG key ID: E764BE00BE6E6408
4 changed files with 65 additions and 4 deletions

View file

@ -2,6 +2,7 @@ mod cli;
mod manifest; mod manifest;
mod missing; mod missing;
mod path; mod path;
mod registry;
mod resource; mod resource;
mod schema; mod schema;
mod steam; mod steam;

View file

@ -50,7 +50,7 @@ pub fn normalize(path: &str) -> String {
path path
} }
pub fn too_broad(path: &str) -> bool { fn too_broad(path: &str) -> bool {
use placeholder::{BASE, HOME, ROOT, STORE_USER_ID, WIN_APP_DATA, WIN_DIR, WIN_DOCUMENTS, XDG_CONFIG, XDG_DATA}; use placeholder::{BASE, HOME, ROOT, STORE_USER_ID, WIN_APP_DATA, WIN_DIR, WIN_DOCUMENTS, XDG_CONFIG, XDG_DATA};
for item in placeholder::ALL { for item in placeholder::ALL {

54
src/registry.rs Normal file
View file

@ -0,0 +1,54 @@
use once_cell::sync::Lazy;
use regex::Regex;
pub fn normalize(path: &str) -> String {
let mut path = path.trim().trim_end_matches(['/', '\\']).replace('\\', "/");
static CONSECUTIVE_SLASHES: Lazy<Regex> = Lazy::new(|| Regex::new(r"/{2,}").unwrap());
static UNNECESSARY_DOUBLE_STAR_1: Lazy<Regex> = Lazy::new(|| Regex::new(r"([^/*])\*{2,}").unwrap());
static UNNECESSARY_DOUBLE_STAR_2: Lazy<Regex> = Lazy::new(|| Regex::new(r"\*{2,}([^/*])").unwrap());
static ENDING_WILDCARD: Lazy<Regex> = Lazy::new(|| Regex::new(r"(/\*)+$").unwrap());
static ENDING_DOT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(/\.)$").unwrap());
static INTERMEDIATE_DOT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(/\./)").unwrap());
for (pattern, replacement) in [
(&CONSECUTIVE_SLASHES, "/"),
(&UNNECESSARY_DOUBLE_STAR_1, "${1}*"),
(&UNNECESSARY_DOUBLE_STAR_2, "*${1}"),
(&ENDING_WILDCARD, ""),
(&ENDING_DOT, ""),
(&INTERMEDIATE_DOT, "/"),
] {
path = pattern.replace_all(&path, replacement).to_string();
}
path
}
fn too_broad(path: &str) -> bool {
let path = path.to_lowercase();
let valid = &["hkey_current_user", "hkey_local_machine"];
if !valid.iter().any(|x| path.starts_with(x)) {
return true;
}
for item in [
"hkey_current_user",
"hkey_current_user/software",
"hkey_current_user/software/wow6432node",
"hkey_local_machine",
"hkey_local_machine/software",
"hkey_local_machine/software/wow6432node",
] {
if path == item {
return true;
}
}
false
}
pub fn usable(path: &str) -> bool {
!path.is_empty() && !path.contains("{{") && !too_broad(path)
}

View file

@ -6,7 +6,7 @@ use wikitext_parser::{Attribute, TextPiece};
use crate::{ use crate::{
manifest::{placeholder, Os, Store, Tag}, manifest::{placeholder, Os, Store, Tag},
path, path, registry,
resource::ResourceFile, resource::ResourceFile,
should_cancel, Error, Regularity, State, should_cancel, Error, Regularity, State,
}; };
@ -625,7 +625,10 @@ impl WikiPath {
} }
pub fn normalize(mut self) -> Self { pub fn normalize(mut self) -> Self {
self.composite = path::normalize(&self.composite); self.composite = match self.kind {
None | Some(PathKind::File) => path::normalize(&self.composite),
Some(PathKind::Registry) => registry::normalize(&self.composite),
};
if self.kind.is_none() { if self.kind.is_none() {
self.kind = Some(PathKind::File); self.kind = Some(PathKind::File);
@ -692,7 +695,10 @@ impl WikiPath {
} }
pub fn usable(&self) -> bool { pub fn usable(&self) -> bool {
path::usable(&self.composite) && !self.irregular() match self.kind {
None | Some(PathKind::File) => path::usable(&self.composite) && !self.irregular(),
Some(PathKind::Registry) => registry::usable(&self.composite) && !self.irregular(),
}
} }
} }