Skip to content

Commit

Permalink
Change dependency to windows-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
InfyniteHeap committed Jun 20, 2024
1 parent 605b3c4 commit ea9c564
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 238 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ zstd = "0.13"

[target."cfg(windows)".dependencies]
cc = "1"
winreg = "0.52"
#winreg = "0.52"
windows-registry = "0.1.2"
windows-result = "0.1.2"

[target."cfg(windows)".dependencies.windows-sys]
features = [
Expand Down Expand Up @@ -143,7 +145,10 @@ proptest = "1.1.0"
tempfile = "3.8"
termcolor = "1.2"
thiserror = "1.0"
tokio = { version = "1.26.0", default-features = false, features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.26.0", default-features = false, features = [
"macros",
"rt-multi-thread",
] }
tokio-retry = { version = "0.3.0" }
tokio-stream = { version = "0.1.14" }
tracing = "0.1"
Expand Down
45 changes: 24 additions & 21 deletions src/cli/self_update/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
use std::{io, sync::Mutex};

#[cfg(windows)]
use winreg::{
enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE},
RegKey, RegValue,
};
// use winreg::{
// enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE},
// RegKey, RegValue,
// };
use windows_registry::{Key, CURRENT_USER};
#[cfg(windows)]
use windows_result::HRESULT;
#[cfg(windows)]
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;

/// Support testing of code that mutates global state
pub fn with_saved_global_state<S>(
Expand All @@ -28,7 +33,7 @@ pub fn with_saved_global_state<S>(

#[cfg(windows)]
pub fn with_saved_path(f: &mut dyn FnMut()) {
with_saved_reg_value(&RegKey::predef(HKEY_CURRENT_USER), "Environment", "PATH", f)
with_saved_reg_string(CURRENT_USER, "Environment", "PATH", f)
}

#[cfg(unix)]
Expand All @@ -37,37 +42,35 @@ pub fn with_saved_path(f: &mut dyn FnMut()) {
}

#[cfg(windows)]
pub fn get_path() -> io::Result<Option<RegValue>> {
get_reg_value(&RegKey::predef(HKEY_CURRENT_USER), "Environment", "PATH")
pub fn get_path() -> io::Result<Option<String>> {
get_reg_string(CURRENT_USER, "Environment", "PATH")
}

#[cfg(windows)]
pub fn with_saved_reg_value(root: &RegKey, subkey: &str, name: &str, f: &mut dyn FnMut()) {
pub fn with_saved_reg_string(root: &Key, subkey: &str, name: &str, f: &mut dyn FnMut()) {
with_saved_global_state(
|| get_reg_value(root, subkey, name),
|p| restore_reg_value(root, subkey, name, p),
|| get_reg_string(root, subkey, name),
|p| restore_reg_string(root, subkey, name, p),
f,
)
}

#[cfg(windows)]
fn get_reg_value(root: &RegKey, subkey: &str, name: &str) -> io::Result<Option<RegValue>> {
let subkey = root.open_subkey_with_flags(subkey, KEY_READ | KEY_WRITE)?;
match subkey.get_raw_value(name) {
fn get_reg_string(root: &Key, subkey: &str, name: &str) -> io::Result<Option<String>> {
let subkey = root.create(subkey)?;
match subkey.get_string(name) {
Ok(val) => Ok(Some(val)),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
Err(ref e) if e.code() == HRESULT::from_win32(ERROR_FILE_NOT_FOUND) => Ok(None),
Err(e) => Err(e.into()),
}
}

#[cfg(windows)]
fn restore_reg_value(root: &RegKey, subkey: &str, name: &str, p: Option<RegValue>) {
let subkey = root
.open_subkey_with_flags(subkey, KEY_READ | KEY_WRITE)
.unwrap();
fn restore_reg_string(root: &Key, subkey: &str, name: &str, p: Option<String>) {
let subkey = root.create(subkey).unwrap();
if let Some(p) = p.as_ref() {
subkey.set_raw_value(name, p).unwrap();
subkey.set_string(name, p).unwrap();
} else {
let _ = subkey.delete_value(name);
let _ = subkey.remove_value(name);
}
}
Loading

0 comments on commit ea9c564

Please sign in to comment.