@@ -13,6 +13,53 @@ use rustc::ty::layout::LayoutOf;
13
13
14
14
use crate :: * ;
15
15
16
+ /// Represent how path separator conversion should be done.
17
+ enum Pathconversion {
18
+ HostToTarget ,
19
+ TargetToHost ,
20
+ }
21
+
22
+ /// Perform path separator conversion if needed.
23
+ fn convert_path_separator < ' a > (
24
+ os_str : & ' 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
+ Cow :: Borrowed ( 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
+ Cow :: Borrowed ( os_str)
60
+ } ;
61
+ }
62
+
16
63
impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
17
64
pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
18
65
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
@@ -177,9 +224,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
177
224
' mir : ' a ,
178
225
{
179
226
let this = self . eval_context_ref ( ) ;
180
- let os_str: & ' a OsStr = this. read_os_str_from_c_str ( scalar) ?;
227
+ let os_str = this. read_os_str_from_c_str ( scalar) ?;
181
228
182
- Ok ( match convert_path_separator ( os_str, & this. tcx . sess . target . target . target_os , PathConversionDirection :: TargetToHost ) {
229
+ Ok ( match convert_path_separator ( os_str, & this. tcx . sess . target . target . target_os , Pathconversion :: TargetToHost ) {
183
230
Cow :: Borrowed ( x) => Cow :: Borrowed ( Path :: new ( x) ) ,
184
231
Cow :: Owned ( y) => Cow :: Owned ( PathBuf :: from ( y) ) ,
185
232
} )
@@ -188,9 +235,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
188
235
/// Read a null-terminated sequence of `u16`s, and perform path separator conversion if needed.
189
236
fn read_path_from_wide_str ( & self , scalar : Scalar < Tag > ) -> InterpResult < ' tcx , PathBuf > {
190
237
let this = self . eval_context_ref ( ) ;
191
- let os_str: OsString = this. read_os_str_from_wide_str ( scalar) ?;
238
+ let os_str = this. read_os_str_from_wide_str ( scalar) ?;
192
239
193
- Ok ( PathBuf :: from ( & convert_path_separator ( & os_str, & this. tcx . sess . target . target . target_os , PathConversionDirection :: TargetToHost ) ) )
240
+ Ok ( PathBuf :: from ( & convert_path_separator ( & os_str, & this. tcx . sess . target . target . target_os , Pathconversion :: TargetToHost ) ) )
194
241
}
195
242
196
243
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -202,7 +249,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
202
249
size : u64 ,
203
250
) -> InterpResult < ' tcx , ( bool , u64 ) > {
204
251
let this = self . eval_context_mut ( ) ;
205
- let os_str = convert_path_separator ( path. as_os_str ( ) , & this. tcx . sess . target . target . target_os , PathConversionDirection :: HostToTarget ) ;
252
+ let os_str = convert_path_separator ( path. as_os_str ( ) , & this. tcx . sess . target . target . target_os , Pathconversion :: HostToTarget ) ;
206
253
this. write_os_str_to_c_str ( & os_str, scalar, size)
207
254
}
208
255
@@ -215,53 +262,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
215
262
size : u64 ,
216
263
) -> InterpResult < ' tcx , ( bool , u64 ) > {
217
264
let this = self . eval_context_mut ( ) ;
218
- let os_str = convert_path_separator ( path. as_os_str ( ) , & this. tcx . sess . target . target . target_os , PathConversionDirection :: HostToTarget ) ;
265
+ let os_str = convert_path_separator ( path. as_os_str ( ) , & this. tcx . sess . target . target . target_os , Pathconversion :: HostToTarget ) ;
219
266
this. write_os_str_to_wide_str ( & os_str, scalar, size)
220
267
}
221
268
}
222
-
223
- enum PathConversionDirection {
224
- HostToTarget ,
225
- TargetToHost ,
226
- }
227
-
228
- /// Perform path separator conversion if needed.
229
- fn convert_path_separator < ' a > (
230
- os_str : & ' a OsStr ,
231
- target_os : & str ,
232
- direction : PathConversionDirection ,
233
- ) -> Cow < ' a , OsStr > {
234
- #[ cfg( windows) ]
235
- return if target_os == "windows" {
236
- // Windows-on-Windows, all fine.
237
- Cow :: Borrowed ( os_str)
238
- } else {
239
- // Unix target, Windows host.
240
- let ( from, to) = match direction {
241
- PathConversionDirection :: HostToTarget => ( '\\' , '/' ) ,
242
- PathConversionDirection :: TargetToHost => ( '/' , '\\' ) ,
243
- } ;
244
- let converted = os_str
245
- . encode_wide ( )
246
- . map ( |wchar| if wchar == from as u16 { to as u16 } else { wchar } )
247
- . collect :: < Vec < _ > > ( ) ;
248
- Cow :: Owned ( OsString :: from_wide ( & converted) )
249
- } ;
250
- #[ cfg( unix) ]
251
- return if target_os == "windows" {
252
- // Windows target, Unix host.
253
- let ( from, to) = match direction {
254
- PathConversionDirection :: HostToTarget => ( '/' , '\\' ) ,
255
- PathConversionDirection :: TargetToHost => ( '\\' , '/' ) ,
256
- } ;
257
- let converted = os_str
258
- . as_bytes ( )
259
- . iter ( )
260
- . map ( |& wchar| if wchar == from as u8 { to as u8 } else { wchar } )
261
- . collect :: < Vec < _ > > ( ) ;
262
- Cow :: Owned ( OsString :: from_vec ( converted) )
263
- } else {
264
- // Unix-on-Unix, all is fine.
265
- Cow :: Borrowed ( os_str)
266
- } ;
267
- }
0 commit comments