Skip to content

Commit 0df3c7b

Browse files
committed
Move functions about the windows registry to the windows module
1 parent bdb97b9 commit 0df3c7b

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

src/cli/self_update/windows.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
169169
.chain_err(|| ErrorKind::PermissionDenied)?;
170170
} else {
171171
let reg_value = RegValue {
172-
bytes: utils::string_to_winreg_bytes(new_path),
172+
bytes: to_winreg_bytes(new_path),
173173
vtype: RegType::REG_EXPAND_SZ,
174174
};
175175
environment
@@ -209,7 +209,7 @@ fn get_windows_path_var() -> Result<Option<Vec<u16>>> {
209209
let reg_value = environment.get_raw_value("PATH");
210210
match reg_value {
211211
Ok(val) => {
212-
if let Some(s) = utils::string_from_winreg_value(&val) {
212+
if let Some(s) = from_winreg_value(&val) {
213213
Ok(Some(s))
214214
} else {
215215
warn!(
@@ -285,6 +285,36 @@ pub fn do_remove_from_path() -> Result<()> {
285285
_apply_new_path(new_path)
286286
}
287287

288+
/// Convert a vector UCS-2 chars to a null-terminated UCS-2 string in bytes
289+
pub fn to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
290+
v.push(0);
291+
unsafe { std::slice::from_raw_parts(v.as_ptr().cast::<u8>(), v.len() * 2).to_vec() }
292+
}
293+
294+
/// This is used to decode the value of HKCU\Environment\PATH. If that key is
295+
/// not REG_SZ | REG_EXPAND_SZ then this returns None. The winreg library itself
296+
/// does a lossy unicode conversion.
297+
pub fn from_winreg_value(val: &winreg::RegValue) -> Option<Vec<u16>> {
298+
use std::slice;
299+
use winreg::enums::RegType;
300+
301+
match val.vtype {
302+
RegType::REG_SZ | RegType::REG_EXPAND_SZ => {
303+
// Copied from winreg
304+
let mut words = unsafe {
305+
#[allow(clippy::cast_ptr_alignment)]
306+
slice::from_raw_parts(val.bytes.as_ptr().cast::<u16>(), val.bytes.len() / 2)
307+
.to_owned()
308+
};
309+
while words.last() == Some(&0) {
310+
words.pop();
311+
}
312+
Some(words)
313+
}
314+
_ => None,
315+
}
316+
}
317+
288318
pub fn run_update(setup_path: &Path) -> Result<utils::ExitCode> {
289319
Command::new(setup_path)
290320
.arg("--self-replace")
@@ -419,7 +449,6 @@ mod tests {
419449

420450
use crate::currentprocess;
421451
use crate::test::with_saved_path;
422-
use crate::utils::utils;
423452

424453
fn wide(str: &str) -> Vec<u16> {
425454
OsString::from(str).encode_wide().collect()
@@ -479,7 +508,7 @@ mod tests {
479508
.unwrap();
480509
let path = environment.get_raw_value("PATH").unwrap();
481510
assert_eq!(path.vtype, RegType::REG_EXPAND_SZ);
482-
assert_eq!(utils::string_to_winreg_bytes(wide("foo")), &path.bytes[..]);
511+
assert_eq!(super::to_winreg_bytes(wide("foo")), &path.bytes[..]);
483512
})
484513
});
485514
}
@@ -500,7 +529,7 @@ mod tests {
500529
.set_raw_value(
501530
"PATH",
502531
&RegValue {
503-
bytes: utils::string_to_winreg_bytes(wide("foo")),
532+
bytes: super::to_winreg_bytes(wide("foo")),
504533
vtype: RegType::REG_EXPAND_SZ,
505534
},
506535
)

src/utils/utils.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -537,39 +537,6 @@ pub fn format_path_for_display(path: &str) -> String {
537537
}
538538
}
539539

540-
/// Encodes a utf-8 string as a null-terminated UCS-2 string in bytes
541-
#[cfg(windows)]
542-
pub fn string_to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
543-
v.push(0);
544-
unsafe { std::slice::from_raw_parts(v.as_ptr().cast::<u8>(), v.len() * 2).to_vec() }
545-
}
546-
547-
// This is used to decode the value of HKCU\Environment\PATH. If that
548-
// key is not unicode (or not REG_SZ | REG_EXPAND_SZ) then this
549-
// returns null. The winreg library itself does a lossy unicode
550-
// conversion.
551-
#[cfg(windows)]
552-
pub fn string_from_winreg_value(val: &winreg::RegValue) -> Option<Vec<u16>> {
553-
use std::slice;
554-
use winreg::enums::RegType;
555-
556-
match val.vtype {
557-
RegType::REG_SZ | RegType::REG_EXPAND_SZ => {
558-
// Copied from winreg
559-
let mut words = unsafe {
560-
#[allow(clippy::cast_ptr_alignment)]
561-
slice::from_raw_parts(val.bytes.as_ptr().cast::<u16>(), val.bytes.len() / 2)
562-
.to_owned()
563-
};
564-
while words.last() == Some(&0) {
565-
words.pop();
566-
}
567-
Some(words)
568-
}
569-
_ => None,
570-
}
571-
}
572-
573540
pub fn toolchain_sort<T: AsRef<str>>(v: &mut Vec<T>) {
574541
use semver::{Identifier, Version};
575542

0 commit comments

Comments
 (0)