14
14
// facts of which OS the user is on -- they should be given the opportunity
15
15
// to write OS-ignorant code by default.
16
16
17
- import libc:: { c_char, c_void, c_int, c_uint, size_t, mode_t, FILE } ;
17
+ import libc:: { c_char, c_void, c_int, c_uint, size_t, mode_t, pid_t , FILE } ;
18
18
import libc:: { close, fclose} ;
19
19
20
20
import getcwd = rustrt:: rust_getcwd;
@@ -32,6 +32,7 @@ native mod rustrt {
32
32
fn rust_path_is_dir ( path : str:: sbuf ) -> c_int ;
33
33
fn rust_path_exists ( path : str:: sbuf ) -> c_int ;
34
34
fn rust_list_files ( path : str ) -> [ str ] ;
35
+ fn rust_process_wait ( handle : c_int ) -> c_int ;
35
36
}
36
37
37
38
@@ -96,6 +97,77 @@ fn fdopen(fd: c_int) -> *FILE {
96
97
}
97
98
98
99
100
+ // fsync related
101
+
102
+ enum fsync_level {
103
+ // whatever fsync does on that platform
104
+ fsync,
105
+
106
+ // fdatasync on linux, similiar or more on other platforms
107
+ fdatasync,
108
+
109
+ // full fsync
110
+ //
111
+ // You must additionally sync the parent directory as well!
112
+ fullfsync,
113
+ }
114
+
115
+ #[ cfg( target_os = "win32" ) ]
116
+ fn fsync_fd ( fd : c_int , _level : fsync_level ) -> c_int {
117
+ import libc:: funcs:: extra:: msvcrt:: * ;
118
+ ret commit ( fd) ;
119
+ }
120
+
121
+ #[ cfg( target_os = "linux" ) ]
122
+ fn fsync_fd ( fd : c_int , level : fsync_level ) -> c_int {
123
+ import libc:: funcs:: posix01:: unistd:: * ;
124
+ alt level {
125
+ fsync | fullfsync { ret fsync( fd) ; }
126
+ fdatasync { ret fdatasync( fd) ; }
127
+ }
128
+ }
129
+
130
+ #[ cfg( target_os = "macos" ) ]
131
+ fn fsync_fd ( fd : c_int , level : fsync_level ) -> c_int {
132
+ import libc:: consts:: os:: extra:: * ;
133
+ import libc:: funcs:: posix88:: fcntl:: * ;
134
+ import libc:: funcs:: posix01:: unistd:: * ;
135
+ alt level {
136
+ fsync { ret fsync( fd) ; }
137
+ _ {
138
+ // According to man fnctl, the ok retval is only specified to be !=-1
139
+ if ( fcntl ( F_FULLFSYNC as c_int , fd) == -1 as c_int )
140
+ { ret -1 as c_int ; }
141
+ else
142
+ { ret 0 as c_int ; }
143
+ }
144
+ }
145
+ }
146
+
147
+ #[ cfg( target_os = "freebsd" ) ]
148
+ fn fsync_fd ( fd : c_int , _l : fsync_level ) -> c_int {
149
+ import libc:: funcs:: posix01:: unistd:: * ;
150
+ ret fsync ( fd) ;
151
+ }
152
+
153
+
154
+ #[ cfg( target_os = "win32" ) ]
155
+ fn waitpid ( pid : pid_t ) -> c_int {
156
+ ret rustrt:: rust_process_wait ( pid) ;
157
+ }
158
+
159
+ #[ cfg( target_os = "linux" ) ]
160
+ #[ cfg( target_os = "freebsd" ) ]
161
+ #[ cfg( target_os = "macos" ) ]
162
+ fn waitpid ( pid : pid_t ) -> c_int {
163
+ import libc:: funcs:: posix01:: wait:: * ;
164
+ let status = 0 as c_int ;
165
+
166
+ assert ( waitpid ( pid, ptr:: mut_addr_of ( status) ,
167
+ 0 as c_int ) != ( -1 as c_int ) ) ;
168
+ ret status;
169
+ }
170
+
99
171
100
172
#[ cfg( target_os = "linux" ) ]
101
173
#[ cfg( target_os = "freebsd" ) ]
@@ -406,6 +478,34 @@ fn change_dir(p: path) -> bool {
406
478
}
407
479
}
408
480
481
+ /*
482
+ Function: remove_file
483
+
484
+ Deletes an existing file.
485
+ */
486
+ fn remove_file ( p : path ) -> bool {
487
+ ret unlink ( p) ;
488
+
489
+ #[ cfg( target_os = "win32" ) ]
490
+ fn unlink ( p : path ) -> bool {
491
+ // FIXME: remove imports when export globs work properly.
492
+ import libc:: funcs:: extra:: kernel32;
493
+ import libc:: types:: os:: arch:: extra:: * ;
494
+ ret as_c_charp ( p) { |buf|
495
+ kernel32:: DeleteFileA ( buf) != ( 0 as BOOL )
496
+ } ;
497
+ }
498
+
499
+ #[ cfg( target_os = "linux" ) ]
500
+ #[ cfg( target_os = "macos" ) ]
501
+ #[ cfg( target_os = "freebsd" ) ]
502
+ fn unlink ( p : path ) -> bool {
503
+ ret as_c_charp ( p) { |buf|
504
+ libc:: unlink ( buf) == ( 0 as c_int )
505
+ } ;
506
+ }
507
+ }
508
+
409
509
410
510
411
511
#[ cfg( target_os = "macos" ) ]
0 commit comments