11
11
use prelude:: * ;
12
12
use super :: support:: PathLike ;
13
13
use super :: { Reader , Writer , Seek } ;
14
- use super :: { SeekSet , SeekCur , SeekEnd , SeekStyle } ;
14
+ use super :: { SeekStyle , SeekSet , SeekCur , SeekEnd ,
15
+ Open , Read , Create , ReadWrite } ;
15
16
use rt:: rtio:: { RtioFileStream , IoFactory , IoFactoryObject } ;
16
17
use rt:: io:: { io_error, read_error, EndOfFile ,
17
- FileMode , FileAccess , Open , Read , Create , ReadWrite } ;
18
+ FileMode , FileAccess , FileStat } ;
18
19
use rt:: local:: Local ;
19
- use rt:: test:: * ;
20
+ use option:: { Some , None } ;
21
+ use path:: Path ;
22
+ use super :: super :: test:: * ;
20
23
21
24
/// Open a file for reading/writing, as indicated by `path`.
22
25
pub fn open < P : PathLike > ( path : & P ,
@@ -145,6 +148,123 @@ impl Seek for FileStream {
145
148
}
146
149
}
147
150
151
+ pub struct FileInfo ( Path ) ;
152
+
153
+ /// FIXME: DOCS
154
+ impl < ' self > FileInfo {
155
+ pub fn new < P : PathLike > ( path : & P ) -> FileInfo {
156
+ do path. path_as_str |p| {
157
+ FileInfo ( Path ( p) )
158
+ }
159
+ }
160
+ // FIXME #8873 can't put this in FileSystemInfo
161
+ pub fn get_path ( & ' self self ) -> & ' self Path {
162
+ & ( * * self )
163
+ }
164
+ pub fn stat ( & self ) -> Option < FileStat > {
165
+ do io_error:: cond. trap ( |_| {
166
+ // FIXME: can we do something more useful here?
167
+ } ) . inside {
168
+ stat ( self . get_path ( ) )
169
+ }
170
+ }
171
+ pub fn exists ( & self ) -> bool {
172
+ match self . stat ( ) {
173
+ Some ( s) => {
174
+ match s. is_file {
175
+ true => {
176
+ true
177
+ } ,
178
+ false => {
179
+ // FIXME: raise condition?
180
+ false
181
+ }
182
+ }
183
+ } ,
184
+ None => false
185
+ }
186
+ }
187
+ pub fn is_file ( & self ) -> bool {
188
+ match self . stat ( ) {
189
+ Some ( s) => s. is_file ,
190
+ None => {
191
+ // FIXME: raise condition
192
+ false
193
+ }
194
+ }
195
+ }
196
+ pub fn open ( & self , mode : FileMode , access : FileAccess ) -> Option < FileStream > {
197
+ match self . is_file ( ) {
198
+ true => {
199
+ open ( self . get_path ( ) , mode, access)
200
+ } ,
201
+ false => {
202
+ // FIXME: raise condition
203
+ None
204
+ }
205
+ }
206
+ }
207
+ //fn open_read(&self) -> FileStream;
208
+ //fn open_write(&self) -> FileStream;
209
+ //fn create(&self) -> FileStream;
210
+ //fn truncate(&self) -> FileStream;
211
+ //fn open_or_create(&self) -> FileStream;
212
+ //fn create_or_truncate(&self) -> FileStream;
213
+ //fn unlink(&self);
214
+ }
215
+
216
+ /*
217
+ /// FIXME: DOCS
218
+ impl DirectoryInfo<'self> {
219
+ fn new<P: PathLike>(path: &P) -> FileInfo {
220
+ FileInfo(Path(path.path_as_str()))
221
+ }
222
+ // FIXME #8873 can't put this in FileSystemInfo
223
+ fn get_path(&'self self) -> &'self Path {
224
+ &*self
225
+ }
226
+ fn stat(&self) -> Option<FileStat> {
227
+ file::stat(self.get_path())
228
+ }
229
+ fn exists(&self) -> bool {
230
+ do io_error::cond.trap(|_| {
231
+ }).inside {
232
+ match self.stat() {
233
+ Some(_) => true,
234
+ None => false
235
+ }
236
+ }
237
+ }
238
+ fn is_dir(&self) -> bool {
239
+
240
+ }
241
+ fn create(&self);
242
+ fn get_subdirs(&self, filter: &str) -> ~[Path];
243
+ fn get_files(&self, filter: &str) -> ~[Path];
244
+ }
245
+ */
246
+
247
+ /// Given a `rt::io::support::PathLike`, query the file system to get
248
+ /// information about a file, directory, etc.
249
+ ///
250
+ /// Returns a `Some(PathInfo)` on success, and raises a `rt::io::IoError` condition
251
+ /// on failure and returns `None`.
252
+ pub fn stat < P : PathLike > ( path : & P ) -> Option < FileStat > {
253
+ let open_result = unsafe {
254
+ let io: * mut IoFactoryObject = Local :: unsafe_borrow ( ) ;
255
+ ( * io) . fs_stat ( path)
256
+ } ;
257
+ match open_result {
258
+ Ok ( p) => {
259
+ Some ( p)
260
+ } ,
261
+ Err ( ioerr) => {
262
+ read_error:: cond. raise ( ioerr) ;
263
+ None
264
+ }
265
+ }
266
+ }
267
+
148
268
fn file_test_smoke_test_impl ( ) {
149
269
do run_in_mt_newsched_task {
150
270
let message = "it's alright. have a good time" ;
@@ -273,7 +393,6 @@ fn file_test_io_seek_and_tell_smoke_test() {
273
393
}
274
394
275
395
fn file_test_io_seek_and_write_impl ( ) {
276
- use io;
277
396
do run_in_mt_newsched_task {
278
397
use str;
279
398
let initial_msg = "food-is-yummy" ;
@@ -293,8 +412,7 @@ fn file_test_io_seek_and_write_impl() {
293
412
read_stream. read ( read_mem) ;
294
413
}
295
414
unlink ( filename) ;
296
- let read_str = str:: from_utf8 ( read_mem) ;
297
- io:: println ( fmt ! ( "read_str: '%?' final_msg: '%?'" , read_str, final_msg) ) ;
415
+ let read_str = str:: from_bytes ( read_mem) ;
298
416
assert ! ( read_str == final_msg. to_owned( ) ) ;
299
417
}
300
418
}
@@ -343,3 +461,35 @@ fn file_test_io_seek_shakedown_impl() {
343
461
fn file_test_io_seek_shakedown ( ) {
344
462
file_test_io_seek_shakedown_impl ( ) ;
345
463
}
464
+
465
+ #[ test]
466
+ fn file_test_stat_is_correct_on_is_file ( ) {
467
+ do run_in_newsched_task {
468
+ let filename = & Path ( "./tmp/file_stat_correct_on_is_file.txt" ) ;
469
+ {
470
+ let mut fs = open ( filename, Create , ReadWrite ) . unwrap ( ) ;
471
+ let msg = "hw" ;
472
+ fs. write ( msg. as_bytes ( ) ) ;
473
+ }
474
+ let stat_res = match stat ( filename) {
475
+ Some ( s) => s,
476
+ None => fail ! ( "shouldn't happen" )
477
+ } ;
478
+ assert ! ( stat_res. is_file) ;
479
+ }
480
+ }
481
+
482
+ #[ test]
483
+ fn file_test_stat_is_correct_on_is_dir ( ) {
484
+ //assert!(false);
485
+ }
486
+
487
+ #[ test]
488
+ fn file_test_fileinfo_false_when_checking_is_file_on_a_directory ( ) {
489
+ //assert!(false);
490
+ }
491
+
492
+ #[ test]
493
+ fn file_test_fileinfo_check_exists_before_and_after_file_creation ( ) {
494
+ //assert!(false);
495
+ }
0 commit comments