@@ -7,10 +7,9 @@ use std::fs;
7
7
use std:: io:: Write ;
8
8
use std:: io;
9
9
use std:: path:: Path ;
10
+ use std:: ptr;
10
11
use std:: process:: { Command , Stdio , ExitStatus } ;
11
12
use std:: str;
12
- use std:: thread;
13
- use std:: time:: Duration ;
14
13
15
14
use rand:: random;
16
15
@@ -194,27 +193,14 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
194
193
pub ReparseTarget : WCHAR ,
195
194
}
196
195
197
- fn to_u16s < S : AsRef < OsStr > > ( s : S ) -> io:: Result < Vec < u16 > > {
198
- fn inner ( s : & OsStr ) -> io:: Result < Vec < u16 > > {
199
- let mut maybe_result: Vec < u16 > = s. encode_wide ( ) . collect ( ) ;
200
- if maybe_result. iter ( ) . any ( |& u| u == 0 ) {
201
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
202
- "strings passed to WinAPI cannot contain NULs" ) ) ;
203
- }
204
- maybe_result. push ( 0 ) ;
205
- Ok ( maybe_result)
206
- }
207
- inner ( s. as_ref ( ) )
208
- }
209
-
210
196
// We're using low-level APIs to create the junction, and these are more picky about paths.
211
197
// For example, forward slashes cannot be used as a path separator, so we should try to
212
198
// canonicalize the path first.
213
199
let target = try!( fs:: canonicalize ( target) ) ;
214
200
215
201
try!( fs:: create_dir ( junction) ) ;
216
202
217
- let path = try!( to_u16s ( junction) ) ;
203
+ let path = try!( windows :: to_u16s ( junction) ) ;
218
204
219
205
unsafe {
220
206
let h = CreateFileW ( path. as_ptr ( ) ,
@@ -382,15 +368,29 @@ pub fn open_browser(path: &Path) -> io::Result<bool> {
382
368
}
383
369
#[ cfg( windows) ]
384
370
fn inner ( path : & Path ) -> io:: Result < bool > {
385
- Command :: new ( "cmd" )
386
- . arg ( "/C" )
387
- . arg ( "start" )
388
- . arg ( path)
389
- . stdin ( Stdio :: null ( ) )
390
- . stdout ( Stdio :: null ( ) )
391
- . stderr ( Stdio :: null ( ) )
392
- . spawn ( )
393
- . map ( |_| true )
371
+ use winapi;
372
+ extern "system" {
373
+ pub fn ShellExecuteW ( hwnd : winapi:: HWND ,
374
+ lpOperation : winapi:: LPCWSTR ,
375
+ lpFile : winapi:: LPCWSTR ,
376
+ lpParameters : winapi:: LPCWSTR ,
377
+ lpDirectory : winapi:: LPCWSTR ,
378
+ nShowCmd : winapi:: c_int )
379
+ -> winapi:: HINSTANCE ;
380
+ }
381
+ const SW_SHOW : winapi:: c_int = 5 ;
382
+
383
+ let path = windows:: to_u16s ( path) ?;
384
+ let operation = windows:: to_u16s ( "open" ) ?;
385
+ let result = unsafe {
386
+ ShellExecuteW ( ptr:: null_mut ( ) ,
387
+ operation. as_ptr ( ) ,
388
+ path. as_ptr ( ) ,
389
+ ptr:: null ( ) ,
390
+ ptr:: null ( ) ,
391
+ SW_SHOW )
392
+ } ;
393
+ Ok ( result as usize > 32 )
394
394
}
395
395
inner ( path)
396
396
}
@@ -402,8 +402,8 @@ pub mod windows {
402
402
use std:: path:: PathBuf ;
403
403
use std:: ptr;
404
404
use std:: slice;
405
- use std:: ffi:: OsString ;
406
- use std:: os:: windows:: ffi:: OsStringExt ;
405
+ use std:: ffi:: { OsString , OsStr } ;
406
+ use std:: os:: windows:: ffi:: { OsStringExt , OsStrExt } ;
407
407
use shell32;
408
408
use ole32;
409
409
@@ -444,4 +444,17 @@ pub mod windows {
444
444
}
445
445
result
446
446
}
447
+
448
+ pub fn to_u16s < S : AsRef < OsStr > > ( s : S ) -> io:: Result < Vec < u16 > > {
449
+ fn inner ( s : & OsStr ) -> io:: Result < Vec < u16 > > {
450
+ let mut maybe_result: Vec < u16 > = s. encode_wide ( ) . collect ( ) ;
451
+ if maybe_result. iter ( ) . any ( |& u| u == 0 ) {
452
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
453
+ "strings passed to WinAPI cannot contain NULs" ) ) ;
454
+ }
455
+ maybe_result. push ( 0 ) ;
456
+ Ok ( maybe_result)
457
+ }
458
+ inner ( s. as_ref ( ) )
459
+ }
447
460
}
0 commit comments