1111use prelude:: * ;
1212use super :: support:: PathLike ;
1313use super :: { Reader , Writer , Seek } ;
14- use super :: { SeekSet , SeekCur , SeekEnd , SeekStyle } ;
14+ use super :: { SeekStyle , SeekSet , SeekCur , SeekEnd ,
15+ Open , Read , Create , ReadWrite } ;
1516use rt:: rtio:: { RtioFileStream , IoFactory , IoFactoryObject } ;
1617use rt:: io:: { io_error, read_error, EndOfFile ,
17- FileMode , FileAccess , Open , Read , Create , ReadWrite } ;
18+ FileMode , FileAccess , FileStat } ;
1819use rt:: local:: Local ;
19- use rt:: test:: * ;
20+ use option:: { Some , None } ;
21+ use path:: Path ;
22+ use super :: super :: test:: * ;
2023
2124/// Open a file for reading/writing, as indicated by `path`.
2225pub fn open < P : PathLike > ( path : & P ,
@@ -145,6 +148,123 @@ impl Seek for FileStream {
145148 }
146149}
147150
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+
148268fn file_test_smoke_test_impl ( ) {
149269 do run_in_mt_newsched_task {
150270 let message = "it's alright. have a good time" ;
@@ -273,7 +393,6 @@ fn file_test_io_seek_and_tell_smoke_test() {
273393}
274394
275395fn file_test_io_seek_and_write_impl ( ) {
276- use io;
277396 do run_in_mt_newsched_task {
278397 use str;
279398 let initial_msg = "food-is-yummy" ;
@@ -293,8 +412,7 @@ fn file_test_io_seek_and_write_impl() {
293412 read_stream. read ( read_mem) ;
294413 }
295414 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) ;
298416 assert ! ( read_str == final_msg. to_owned( ) ) ;
299417 }
300418}
@@ -343,3 +461,35 @@ fn file_test_io_seek_shakedown_impl() {
343461fn file_test_io_seek_shakedown ( ) {
344462 file_test_io_seek_shakedown_impl ( ) ;
345463}
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