4
4
#![ feature( io_error_more) ]
5
5
#![ feature( rustc_private) ]
6
6
7
+ use std:: path:: PathBuf ;
8
+
7
9
extern crate libc;
8
10
9
- fn tmp ( ) -> std :: path :: PathBuf {
11
+ fn tmp ( ) -> PathBuf {
10
12
std:: env:: var ( "MIRI_TEMP" )
11
- . map ( std:: path:: PathBuf :: from)
13
+ . map ( |tmp| {
14
+ // MIRI_TEMP is set outside of our emulated
15
+ // program, so it may have path separators that don't
16
+ // correspond to our target platform. We normalize them here
17
+ // before constructing a `PathBuf`
18
+
19
+ #[ cfg( windows) ]
20
+ return PathBuf :: from ( tmp. replace ( "/" , "\\ " ) ) ;
21
+
22
+ #[ cfg( not( windows) ) ]
23
+ return PathBuf :: from ( tmp. replace ( "\\ " , "/" ) ) ;
24
+ } )
12
25
. unwrap_or_else ( |_| std:: env:: temp_dir ( ) )
13
26
}
14
27
@@ -19,7 +32,6 @@ fn test_posix_realpath_alloc() {
19
32
use std:: fs:: { remove_file, File } ;
20
33
use std:: os:: unix:: ffi:: OsStrExt ;
21
34
use std:: os:: unix:: ffi:: OsStringExt ;
22
- use std:: path:: PathBuf ;
23
35
24
36
let buf;
25
37
let path = tmp ( ) . join ( "miri_test_libc_posix_realpath_alloc" ) ;
@@ -36,18 +48,17 @@ fn test_posix_realpath_alloc() {
36
48
libc:: free ( r as * mut _ ) ;
37
49
}
38
50
let canonical = PathBuf :: from ( OsString :: from_vec ( buf) ) ;
51
+ assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
52
+
39
53
// Cleanup after test.
40
54
remove_file ( & path) . unwrap ( ) ;
41
-
42
- assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
43
55
}
44
56
45
57
/// Test non-allocating variant of `realpath`.
46
58
fn test_posix_realpath_noalloc ( ) {
47
59
use std:: ffi:: { CStr , CString } ;
48
60
use std:: fs:: { remove_file, File } ;
49
61
use std:: os:: unix:: ffi:: OsStrExt ;
50
- use std:: path:: PathBuf ;
51
62
52
63
let path = tmp ( ) . join ( "miri_test_libc_posix_realpath_noalloc" ) ;
53
64
let c_path = CString :: new ( path. as_os_str ( ) . as_bytes ( ) ) . expect ( "CString::new failed" ) ;
@@ -64,17 +75,18 @@ fn test_posix_realpath_noalloc() {
64
75
}
65
76
let c = unsafe { CStr :: from_ptr ( v. as_ptr ( ) ) } ;
66
77
let canonical = PathBuf :: from ( c. to_str ( ) . expect ( "CStr to str" ) ) ;
67
- // Cleanup after test.
68
- remove_file ( & path) . unwrap ( ) ;
69
78
70
79
assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
80
+
81
+ // Cleanup after test.
82
+ remove_file ( & path) . unwrap ( ) ;
71
83
}
72
84
73
85
/// Test failure cases for `realpath`.
74
86
fn test_posix_realpath_errors ( ) {
75
87
use std:: convert:: TryInto ;
76
88
use std:: ffi:: CString ;
77
- use std:: fs:: { create_dir_all, remove_dir , remove_file } ;
89
+ use std:: fs:: { create_dir_all, remove_dir_all } ;
78
90
use std:: io:: ErrorKind ;
79
91
use std:: os:: unix:: ffi:: OsStrExt ;
80
92
use std:: os:: unix:: fs:: symlink;
@@ -89,13 +101,15 @@ fn test_posix_realpath_errors() {
89
101
90
102
// Test that a long path returns an error.
91
103
//
92
- // Linux first checks if the path to exists and macos does not.
104
+ // Linux first checks if the path exists and macos does not.
93
105
// Using an existing path ensures all platforms return `ENAMETOOLONG` given a long path.
94
106
//
95
107
// Rather than creating a bunch of directories, we create two directories containing symlinks.
96
108
// Sadly we can't avoid creating directories and instead use a path like "./././././" or "./../../" as linux
97
109
// appears to collapse "." and ".." before checking path length.
98
- let path = tmp ( ) ;
110
+ let path = tmp ( ) . join ( "posix_realpath_errors" ) ;
111
+ // Cleanup before test.
112
+ remove_dir_all ( & path) . ok ( ) ;
99
113
100
114
// The directories we will put symlinks in.
101
115
let x = path. join ( "x/" ) ;
@@ -105,12 +119,6 @@ fn test_posix_realpath_errors() {
105
119
let yx_sym = y. join ( "x" ) ;
106
120
let xy_sym = x. join ( "y" ) ;
107
121
108
- // Cleanup before test.
109
- remove_file ( & yx_sym) . ok ( ) ;
110
- remove_file ( & xy_sym) . ok ( ) ;
111
- remove_dir ( & x) . ok ( ) ;
112
- remove_dir ( & y) . ok ( ) ;
113
-
114
122
// Create directories.
115
123
create_dir_all ( & x) . expect ( "dir x" ) ;
116
124
create_dir_all ( & y) . expect ( "dir y" ) ;
@@ -126,15 +134,12 @@ fn test_posix_realpath_errors() {
126
134
let r = unsafe { libc:: realpath ( c_path. as_ptr ( ) , std:: ptr:: null_mut ( ) ) } ;
127
135
let e = std:: io:: Error :: last_os_error ( ) ;
128
136
129
- // Cleanup after test.
130
- remove_file ( & yx_sym) . ok ( ) ;
131
- remove_file ( & xy_sym) . ok ( ) ;
132
- remove_dir ( & x) . ok ( ) ;
133
- remove_dir ( & y) . ok ( ) ;
134
-
135
137
assert ! ( r. is_null( ) ) ;
136
138
assert_eq ! ( e. raw_os_error( ) , Some ( libc:: ENAMETOOLONG ) ) ;
137
139
assert_eq ! ( e. kind( ) , ErrorKind :: InvalidFilename ) ;
140
+
141
+ // Cleanup after test.
142
+ remove_dir_all ( & path) . ok ( ) ;
138
143
}
139
144
140
145
#[ cfg( any( target_os = "linux" , target_os = "freebsd" ) ) ]
0 commit comments