@@ -15,7 +15,9 @@ pub struct File {
15
15
16
16
//TODO: We may be able to get some of this info
17
17
#[ derive( Clone ) ]
18
- pub struct FileAttr ;
18
+ pub struct FileAttr {
19
+ size : u64 ,
20
+ }
19
21
20
22
pub struct ReadDir ( !) ;
21
23
@@ -26,6 +28,7 @@ pub struct OpenOptions {
26
28
read : bool ,
27
29
write : bool ,
28
30
append : bool ,
31
+ create_new : bool ,
29
32
}
30
33
31
34
#[ derive( Copy , Clone , Debug , Default ) ]
@@ -44,7 +47,7 @@ pub struct DirBuilder {}
44
47
45
48
impl FileAttr {
46
49
pub fn size ( & self ) -> u64 {
47
- todo ! ( )
50
+ self . size
48
51
}
49
52
50
53
pub fn perm ( & self ) -> FilePermissions {
@@ -131,7 +134,7 @@ impl DirEntry {
131
134
132
135
impl OpenOptions {
133
136
pub fn new ( ) -> OpenOptions {
134
- OpenOptions { read : false , write : false , append : false }
137
+ OpenOptions { read : false , write : false , append : false , create_new : false }
135
138
}
136
139
137
140
pub fn read ( & mut self , read : bool ) {
@@ -143,11 +146,17 @@ impl OpenOptions {
143
146
pub fn append ( & mut self , append : bool ) {
144
147
self . append = append;
145
148
}
146
- pub fn truncate ( & mut self , _truncate : bool ) { }
149
+ pub fn truncate ( & mut self , truncate : bool ) {
150
+ if truncate {
151
+ panic ! ( "Truncation is not supported" )
152
+ }
153
+ }
147
154
pub fn create ( & mut self , create : bool ) {
148
155
self . write = create;
149
156
}
150
- pub fn create_new ( & mut self , _create_new : bool ) { }
157
+ pub fn create_new ( & mut self , create_new : bool ) {
158
+ self . create_new = create_new;
159
+ }
151
160
}
152
161
153
162
impl File {
@@ -159,6 +168,18 @@ impl File {
159
168
io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
160
169
} ) ?;
161
170
171
+ if opts. create_new {
172
+ let file_exists = unsafe {
173
+ vex_sdk:: vexFileStatus ( path. as_ptr ( ) )
174
+ } ;
175
+ if file_exists != 0 {
176
+ return Err ( io:: Error :: new (
177
+ io:: ErrorKind :: AlreadyExists ,
178
+ "File already exists"
179
+ ) )
180
+ }
181
+ }
182
+
162
183
let file = if opts. read && !opts. write {
163
184
// The second argument to this function is ignored.
164
185
// Open in read only mode
@@ -313,8 +334,19 @@ pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
313
334
unsupported ( )
314
335
}
315
336
316
- pub fn try_exists ( _path : & Path ) -> io:: Result < bool > {
317
- todo ! ( )
337
+ pub fn try_exists ( path : & Path ) -> io:: Result < bool > {
338
+ let path = CString :: new ( path. as_os_str ( ) . as_encoded_bytes ( ) ) . map_err ( |_| {
339
+ io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
340
+ } ) ?;
341
+
342
+ let file_exists = unsafe {
343
+ vex_sdk:: vexFileStatus ( path. as_ptr ( ) )
344
+ } ;
345
+ if file_exists != 0 {
346
+ Ok ( true )
347
+ } else {
348
+ Ok ( false )
349
+ }
318
350
}
319
351
320
352
pub fn readlink ( _p : & Path ) -> io:: Result < PathBuf > {
@@ -329,12 +361,34 @@ pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
329
361
unsupported ( )
330
362
}
331
363
332
- pub fn stat ( _p : & Path ) -> io:: Result < FileAttr > {
333
- todo ! ( )
364
+ pub fn stat ( p : & Path ) -> io:: Result < FileAttr > {
365
+ let mut opts = OpenOptions :: new ( ) ;
366
+ opts. read ( true ) ;
367
+ let file = File :: open ( p, & opts) ?;
368
+ let fd = file. fd . 0 ;
369
+
370
+ const SEEK_END : i32 = 2 ;
371
+
372
+ let end = unsafe {
373
+ map_fresult ( vex_sdk:: vexFileSeek ( fd, 0 , SEEK_END ) ) ?;
374
+ vex_sdk:: vexFileTell ( fd)
375
+ } ;
376
+
377
+ if end >= 0 {
378
+ Ok ( FileAttr {
379
+ size : end as u64 ,
380
+ } )
381
+ } else {
382
+ Err ( io:: Error :: new (
383
+ io:: ErrorKind :: NotSeekable ,
384
+ "Failed to seek file"
385
+ ) )
386
+ }
334
387
}
335
388
336
- pub fn lstat ( _p : & Path ) -> io:: Result < FileAttr > {
337
- unsupported ( )
389
+ pub fn lstat ( p : & Path ) -> io:: Result < FileAttr > {
390
+ // Symlinks aren't supported in our filesystem
391
+ stat ( p)
338
392
}
339
393
340
394
pub fn canonicalize ( _p : & Path ) -> io:: Result < PathBuf > {
0 commit comments