@@ -14,52 +14,11 @@ use rustc_target::abi::LayoutOf;
14
14
use crate :: * ;
15
15
16
16
/// Represent how path separator conversion should be done.
17
- enum Pathconversion {
17
+ pub enum Pathconversion {
18
18
HostToTarget ,
19
19
TargetToHost ,
20
20
}
21
21
22
- /// Perform path separator conversion if needed.
23
- fn convert_path_separator < ' a > (
24
- os_str : Cow < ' a , OsStr > ,
25
- target_os : & str ,
26
- direction : Pathconversion ,
27
- ) -> Cow < ' a , OsStr > {
28
- #[ cfg( windows) ]
29
- return if target_os == "windows" {
30
- // Windows-on-Windows, all fine.
31
- os_str
32
- } else {
33
- // Unix target, Windows host.
34
- let ( from, to) = match direction {
35
- Pathconversion :: HostToTarget => ( '\\' , '/' ) ,
36
- Pathconversion :: TargetToHost => ( '/' , '\\' ) ,
37
- } ;
38
- let converted = os_str
39
- . encode_wide ( )
40
- . map ( |wchar| if wchar == from as u16 { to as u16 } else { wchar } )
41
- . collect :: < Vec < _ > > ( ) ;
42
- Cow :: Owned ( OsString :: from_wide ( & converted) )
43
- } ;
44
- #[ cfg( unix) ]
45
- return if target_os == "windows" {
46
- // Windows target, Unix host.
47
- let ( from, to) = match direction {
48
- Pathconversion :: HostToTarget => ( '/' , '\\' ) ,
49
- Pathconversion :: TargetToHost => ( '\\' , '/' ) ,
50
- } ;
51
- let converted = os_str
52
- . as_bytes ( )
53
- . iter ( )
54
- . map ( |& wchar| if wchar == from as u8 { to as u8 } else { wchar } )
55
- . collect :: < Vec < _ > > ( ) ;
56
- Cow :: Owned ( OsString :: from_vec ( converted) )
57
- } else {
58
- // Unix-on-Unix, all is fine.
59
- os_str
60
- } ;
61
- }
62
-
63
22
#[ cfg( unix) ]
64
23
pub fn os_str_to_bytes < ' a , ' tcx > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
65
24
Ok ( os_str. as_bytes ( ) )
@@ -229,7 +188,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
229
188
let this = self . eval_context_ref ( ) ;
230
189
let os_str = this. read_os_str_from_c_str ( scalar) ?;
231
190
232
- Ok ( match convert_path_separator ( Cow :: Borrowed ( os_str) , & this . tcx . sess . target . target . target_os , Pathconversion :: TargetToHost ) {
191
+ Ok ( match this . convert_path_separator ( Cow :: Borrowed ( os_str) , Pathconversion :: TargetToHost ) {
233
192
Cow :: Borrowed ( x) => Cow :: Borrowed ( Path :: new ( x) ) ,
234
193
Cow :: Owned ( y) => Cow :: Owned ( PathBuf :: from ( y) ) ,
235
194
} )
@@ -240,7 +199,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
240
199
let this = self . eval_context_ref ( ) ;
241
200
let os_str = this. read_os_str_from_wide_str ( scalar) ?;
242
201
243
- Ok ( convert_path_separator ( Cow :: Owned ( os_str) , & this . tcx . sess . target . target . target_os , Pathconversion :: TargetToHost ) . into_owned ( ) . into ( ) )
202
+ Ok ( this . convert_path_separator ( Cow :: Owned ( os_str) , Pathconversion :: TargetToHost ) . into_owned ( ) . into ( ) )
244
203
}
245
204
246
205
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -252,7 +211,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
252
211
size : u64 ,
253
212
) -> InterpResult < ' tcx , ( bool , u64 ) > {
254
213
let this = self . eval_context_mut ( ) ;
255
- let os_str = convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , & this . tcx . sess . target . target . target_os , Pathconversion :: HostToTarget ) ;
214
+ let os_str = this . convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , Pathconversion :: HostToTarget ) ;
256
215
this. write_os_str_to_c_str ( & os_str, scalar, size)
257
216
}
258
217
@@ -265,8 +224,50 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
265
224
size : u64 ,
266
225
) -> InterpResult < ' tcx , ( bool , u64 ) > {
267
226
let this = self . eval_context_mut ( ) ;
268
- let os_str = convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , & this . tcx . sess . target . target . target_os , Pathconversion :: HostToTarget ) ;
227
+ let os_str = this . convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , Pathconversion :: HostToTarget ) ;
269
228
this. write_os_str_to_wide_str ( & os_str, scalar, size)
270
229
}
230
+
231
+ fn convert_path_separator < ' a > (
232
+ & self ,
233
+ os_str : Cow < ' a , OsStr > ,
234
+ direction : Pathconversion ,
235
+ ) -> Cow < ' a , OsStr > {
236
+ let this = self . eval_context_ref ( ) ;
237
+ let target_os = & this. tcx . sess . target . target . target_os ;
238
+ #[ cfg( windows) ]
239
+ return if target_os == "windows" {
240
+ // Windows-on-Windows, all fine.
241
+ os_str
242
+ } else {
243
+ // Unix target, Windows host.
244
+ let ( from, to) = match direction {
245
+ Pathconversion :: HostToTarget => ( '\\' , '/' ) ,
246
+ Pathconversion :: TargetToHost => ( '/' , '\\' ) ,
247
+ } ;
248
+ let converted = os_str
249
+ . encode_wide ( )
250
+ . map ( |wchar| if wchar == from as u16 { to as u16 } else { wchar } )
251
+ . collect :: < Vec < _ > > ( ) ;
252
+ Cow :: Owned ( OsString :: from_wide ( & converted) )
253
+ } ;
254
+ #[ cfg( unix) ]
255
+ return if target_os == "windows" {
256
+ // Windows target, Unix host.
257
+ let ( from, to) = match direction {
258
+ Pathconversion :: HostToTarget => ( '/' , '\\' ) ,
259
+ Pathconversion :: TargetToHost => ( '\\' , '/' ) ,
260
+ } ;
261
+ let converted = os_str
262
+ . as_bytes ( )
263
+ . iter ( )
264
+ . map ( |& wchar| if wchar == from as u8 { to as u8 } else { wchar } )
265
+ . collect :: < Vec < _ > > ( ) ;
266
+ Cow :: Owned ( OsString :: from_vec ( converted) )
267
+ } else {
268
+ // Unix-on-Unix, all is fine.
269
+ os_str
270
+ } ;
271
+ }
271
272
}
272
273
0 commit comments