@@ -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+
288318pub 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 )
0 commit comments