1
0

fix: update blank guard usage and fix issue

This commit is contained in:
2026-05-18 17:04:11 +08:00
parent 1086039dcb
commit 53cc8edcfd
7 changed files with 68 additions and 90 deletions

View File

@@ -77,6 +77,7 @@ default = '@C:\path\to\ppic.exe,-1001'
# And more string resources...
jpg = '@C:\path\to\ppic.exe,-1011'
jfif = '@C:\path\to\ppic.exe,-1050'
gif = '@C:\path\to\ppic.exe,-1012'
bmp = '@C:\path\to\ppic.exe,-1013'
png = '@C:\path\to\ppic.exe,-1014'

View File

@@ -168,7 +168,6 @@ fn run_ext_list(
pub fn run(c: cli::Cli) -> Result<()> {
// Read manifest file first
let mf = manifest::Manifest::from_file(Path::new(&c.manifest_file))?;
println!("{:?}", mf);
// Parse it into schema
let schema = mf.into_schema()?;
// Parse it into program

View File

@@ -39,8 +39,7 @@ impl AppPathsKey {
OpenKeyTerritory::Hybrid => panic!("unexpected hybrid key territory"),
};
// navigate to App Paths
let app_paths =
hk.open_subkey_with_flags(regext::blank_path_guard(Self::APP_PATHS)?, perms)?;
let app_paths = hk.open_subkey_with_flags(Self::APP_PATHS, perms)?;
// open file name key if possible
let this_app = regext::try_open_subkey_with_flags(
&app_paths,

View File

@@ -40,14 +40,12 @@ impl ApplicationsKey {
OpenKeyTerritory::Hybrid => RegKey::predef(HKEY_CLASSES_ROOT),
};
let applications = match territory {
OpenKeyTerritory::User | OpenKeyTerritory::System => hk.open_subkey_with_flags(
regext::blank_path_guard(Self::FULL_APPLICATIONS)?,
perms,
)?,
OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags(
regext::blank_path_guard(Self::PARTIAL_APPLICATIONS)?,
perms,
)?,
OpenKeyTerritory::User | OpenKeyTerritory::System => {
hk.open_subkey_with_flags(Self::FULL_APPLICATIONS, perms)?
}
OpenKeyTerritory::Hybrid => {
hk.open_subkey_with_flags(Self::PARTIAL_APPLICATIONS, perms)?
}
};
// open app key if possible
let this_app = regext::try_open_subkey_with_flags(
@@ -117,7 +115,7 @@ impl ApplicationsKey {
// Get shell subkey
let shell_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?,
Self::NAMEOF_SHELL_VERB_PART1,
PERM_R,
)? {
Some(key) => key,
@@ -139,7 +137,7 @@ impl ApplicationsKey {
// Get command subkey.
let command_key = match regext::try_open_subkey_with_flags(
&verb_key,
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?,
Self::NAMEOF_SHELL_VERB_PART3,
PERM_R,
)? {
Some(key) => key,
@@ -167,24 +165,20 @@ impl ApplicationsKey {
match sv {
Some(sv) => {
// Create shell subkey
let (shell_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?,
PERM_RW,
)?;
let (shell_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART1, PERM_RW)?;
// Create verb key
let (verb_key, _) =
shell_key.create_subkey_with_flags(sv.get_verb().inner(), PERM_RW)?;
// Create command key
let (command_key, _) = verb_key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?,
PERM_RW,
)?;
let (command_key, _) =
verb_key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART3, PERM_RW)?;
// Set command key default value
command_key.set_value(Self::NAMEOF_SHELL_VERB_PART4, &sv.get_command().full())?;
}
None => {
// Delete shell and its all subkey.
key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?)?;
key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?;
}
}
@@ -199,7 +193,7 @@ impl ApplicationsKey {
// Get default icon subkey
let default_icon_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?,
Self::NAMEOF_DEFAULT_ICON_PART1,
PERM_R,
)? {
Some(key) => key,
@@ -218,16 +212,14 @@ impl ApplicationsKey {
match icon {
Some(icon) => {
// Create default icon subkey
let (default_icon_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?,
PERM_RW,
)?;
let (default_icon_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_DEFAULT_ICON_PART1, PERM_RW)?;
// Set default value of default icon subkey.
default_icon_key.set_value(Self::NAMEOF_DEFAULT_ICON_PART2, &icon.to_string())?;
}
None => {
// Delete shell and its all subkey.
key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?)?;
key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?;
}
}
@@ -270,14 +262,11 @@ impl ApplicationsKey {
pub fn get_supported_types(&self, view: View) -> Result<Option<Vec<concept::Ext>>> {
let key = self.open_view_for_getter(view)?;
// Get supported types subkey
let supported_types_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?,
PERM_R,
)? {
Some(key) => key,
None => return Ok(None),
};
let supported_types_key =
match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_SUPPORTED_TYPES, PERM_R)? {
Some(key) => key,
None => return Ok(None),
};
// Fetch all sub-values
let key_names = regext::get_all_string_subkey_names(&supported_types_key)?;
// Map the result
@@ -299,20 +288,19 @@ impl ApplicationsKey {
match tys {
Some(tys) => {
// Create supported types key
let (supported_types_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?,
PERM_RW,
)?;
let (supported_types_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_SUPPORTED_TYPES, PERM_RW)?;
// Clean all contents of this key
regext::clean_all_contents(&supported_types_key)?;
// Add file types one by one
for ty in tys {
supported_types_key.set_value(ty.dotted_inner(), &"")?;
supported_types_key
.set_value(regext::blank_path_guard(ty.dotted_inner())?, &"")?;
}
}
None => {
// Delete this subkey.
key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?)?;
key.delete_subkey_all(Self::NAMEOF_SUPPORTED_TYPES)?;
}
}

View File

@@ -41,11 +41,9 @@ impl ExtKey {
};
let classes = match territory {
OpenKeyTerritory::User | OpenKeyTerritory::System => {
hk.open_subkey_with_flags(regext::blank_path_guard(Self::FULL_CLASSES)?, perms)?
}
OpenKeyTerritory::Hybrid => {
hk.open_subkey_with_flags(regext::blank_path_guard(Self::PARTIAL_CLASSES)?, perms)?
hk.open_subkey_with_flags(Self::FULL_CLASSES, perms)?
}
OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags(Self::PARTIAL_CLASSES, perms)?,
};
// open extension key if possible
let this_ext = regext::try_open_subkey_with_flags(
@@ -141,14 +139,12 @@ impl ExtKey {
pub fn get_open_with_progids(&self, view: View) -> Result<Option<Vec<LosseProgId>>> {
let key = self.open_view_for_getter(view)?;
// Get OpenWithProgIds subkey
let open_with_progids_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?,
PERM_R,
)? {
Some(key) => key,
None => return Ok(None),
};
let open_with_progids_key =
match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_R)?
{
Some(key) => key,
None => return Ok(None),
};
// Fetch all sub-values
let key_names = regext::get_all_string_subkey_names(&open_with_progids_key)?;
// Map the result
@@ -166,16 +162,18 @@ impl ExtKey {
pub fn is_in_open_with_progids(&self, view: View, pid: &LosseProgId) -> Result<bool> {
let key = self.open_view_for_getter(view)?;
// Get OpenWithProgIds subkey
let open_with_progids_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?,
PERM_R,
)? {
Some(key) => key,
None => return Ok(false),
};
let open_with_progids_key =
match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_R)?
{
Some(key) => key,
None => return Ok(false),
};
// Check whether there is given ProgId
Ok(regext::try_get_value::<String, _>(&open_with_progids_key, pid.to_string())?.is_some())
Ok(regext::try_get_value::<String, _>(
&open_with_progids_key,
regext::blank_path_guard(pid.to_string())?,
)?
.is_some())
}
///
@@ -185,12 +183,10 @@ impl ExtKey {
pub fn add_into_open_with_progids(&mut self, scope: Scope, pid: &LosseProgId) -> Result<()> {
let key = self.open_scope_for_setter(scope)?;
// Get subkey
let (open_with_progids_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?,
PERM_RW,
)?;
let (open_with_progids_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_RW)?;
// Add value
open_with_progids_key.set_value(pid.to_string(), &"")?;
open_with_progids_key.set_value(regext::blank_path_guard(pid.to_string())?, &"")?;
Ok(())
}
@@ -202,7 +198,7 @@ impl ExtKey {
// Try get subkey
let open_with_progids_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?,
Self::NAMEOF_OPEN_WITH_PROGIDS,
PERM_RW,
)? {
Some(key) => key,

View File

@@ -41,11 +41,9 @@ impl ProgIdKey {
};
let classes = match territory {
OpenKeyTerritory::User | OpenKeyTerritory::System => {
hk.open_subkey_with_flags(regext::blank_path_guard(Self::FULL_CLASSES)?, perms)?
}
OpenKeyTerritory::Hybrid => {
hk.open_subkey_with_flags(regext::blank_path_guard(Self::PARTIAL_CLASSES)?, perms)?
hk.open_subkey_with_flags(Self::FULL_CLASSES, perms)?
}
OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags(Self::PARTIAL_CLASSES, perms)?,
};
// open ProgId key if possible
let this_progid = regext::try_open_subkey_with_flags(
@@ -148,7 +146,7 @@ impl ProgIdKey {
// Get shell subkey
let shell_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?,
Self::NAMEOF_SHELL_VERB_PART1,
PERM_R,
)? {
Some(key) => key,
@@ -170,7 +168,7 @@ impl ProgIdKey {
// Get command subkey.
let command_key = match regext::try_open_subkey_with_flags(
&verb_key,
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?,
Self::NAMEOF_SHELL_VERB_PART3,
PERM_R,
)? {
Some(key) => key,
@@ -198,24 +196,20 @@ impl ProgIdKey {
match sv {
Some(sv) => {
// Create shell subkey
let (shell_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?,
PERM_RW,
)?;
let (shell_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART1, PERM_RW)?;
// Create verb key
let (verb_key, _) =
shell_key.create_subkey_with_flags(sv.get_verb().inner(), PERM_RW)?;
// Create command key
let (command_key, _) = verb_key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?,
PERM_RW,
)?;
let (command_key, _) =
verb_key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART3, PERM_RW)?;
// Set command key default value
command_key.set_value(Self::NAMEOF_SHELL_VERB_PART4, &sv.get_command().full())?;
}
None => {
// Delete shell and its all subkey.
key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?)?;
key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?;
}
}
@@ -264,7 +258,7 @@ impl ProgIdKey {
// Get default icon subkey
let default_icon_key = match regext::try_open_subkey_with_flags(
&key,
regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?,
Self::NAMEOF_DEFAULT_ICON_PART1,
PERM_R,
)? {
Some(key) => key,
@@ -283,16 +277,14 @@ impl ProgIdKey {
match icon {
Some(icon) => {
// Create default icon subkey
let (default_icon_key, _) = key.create_subkey_with_flags(
regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?,
PERM_RW,
)?;
let (default_icon_key, _) =
key.create_subkey_with_flags(Self::NAMEOF_DEFAULT_ICON_PART1, PERM_RW)?;
// Set default value of default icon subkey.
default_icon_key.set_value(Self::NAMEOF_DEFAULT_ICON_PART2, &icon.to_string())?;
}
None => {
// Delete shell and its all subkey.
key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?)?;
key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?;
}
}

View File

@@ -135,6 +135,9 @@ impl BlankPathError {
/// Because it will cause unexpected behavior that returning key self, rather than subkey.
/// This is VERY dangerous especially for those registry delete functions.
/// So I create this function to prevent any harmful blank path was passed into registry function.
///
/// This function MUST be used for the value, whose content can not be confirmed at compile time,
/// and it will be passed to get/set value, or create/delete key functions.
pub fn blank_path_guard<P: AsRef<OsStr>>(path: P) -> std::result::Result<P, BlankPathError> {
if path.as_ref().is_empty() {
Err(BlankPathError::new())