@@ -169,7 +169,7 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
169
169
. chain_err ( || ErrorKind :: PermissionDenied ) ?;
170
170
} else {
171
171
let reg_value = RegValue {
172
- bytes : utils :: string_to_winreg_bytes ( new_path) ,
172
+ bytes : to_winreg_bytes ( new_path) ,
173
173
vtype : RegType :: REG_EXPAND_SZ ,
174
174
} ;
175
175
environment
@@ -209,7 +209,7 @@ fn get_windows_path_var() -> Result<Option<Vec<u16>>> {
209
209
let reg_value = environment. get_raw_value ( "PATH" ) ;
210
210
match reg_value {
211
211
Ok ( val) => {
212
- if let Some ( s) = utils :: string_from_winreg_value ( & val) {
212
+ if let Some ( s) = from_winreg_value ( & val) {
213
213
Ok ( Some ( s) )
214
214
} else {
215
215
warn ! (
@@ -285,6 +285,36 @@ pub fn do_remove_from_path() -> Result<()> {
285
285
_apply_new_path ( new_path)
286
286
}
287
287
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
+
288
318
pub fn run_update ( setup_path : & Path ) -> Result < utils:: ExitCode > {
289
319
Command :: new ( setup_path)
290
320
. arg ( "--self-replace" )
@@ -419,7 +449,6 @@ mod tests {
419
449
420
450
use crate :: currentprocess;
421
451
use crate :: test:: with_saved_path;
422
- use crate :: utils:: utils;
423
452
424
453
fn wide ( str : & str ) -> Vec < u16 > {
425
454
OsString :: from ( str) . encode_wide ( ) . collect ( )
@@ -479,7 +508,7 @@ mod tests {
479
508
. unwrap ( ) ;
480
509
let path = environment. get_raw_value ( "PATH" ) . unwrap ( ) ;
481
510
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[ ..] ) ;
483
512
} )
484
513
} ) ;
485
514
}
@@ -500,7 +529,7 @@ mod tests {
500
529
. set_raw_value (
501
530
"PATH" ,
502
531
& RegValue {
503
- bytes : utils :: string_to_winreg_bytes ( wide ( "foo" ) ) ,
532
+ bytes : super :: to_winreg_bytes ( wide ( "foo" ) ) ,
504
533
vtype : RegType :: REG_EXPAND_SZ ,
505
534
} ,
506
535
)
0 commit comments