@@ -28,6 +28,7 @@ pub struct OpenOptions {
28
28
read : bool ,
29
29
write : bool ,
30
30
append : bool ,
31
+ truncate : bool ,
31
32
create_new : bool ,
32
33
}
33
34
@@ -46,6 +47,19 @@ pub struct FileType {
46
47
pub struct DirBuilder { }
47
48
48
49
impl FileAttr {
50
+ /// Creates a FileAttr by getting data from an opened file.
51
+ fn from_fd ( fd : * mut vex_sdk:: FIL ) -> io:: Result < Self > {
52
+ let size = unsafe {
53
+ vex_sdk:: vexFileSize ( fd)
54
+ } ;
55
+
56
+ if size >= 0 {
57
+ Ok ( Self { size : size as u64 } )
58
+ } else {
59
+ Err ( io:: Error :: new ( io:: ErrorKind :: NotSeekable , "Failed to seek file" ) )
60
+ }
61
+ }
62
+
49
63
pub fn size ( & self ) -> u64 {
50
64
self . size
51
65
}
@@ -134,7 +148,7 @@ impl DirEntry {
134
148
135
149
impl OpenOptions {
136
150
pub fn new ( ) -> OpenOptions {
137
- OpenOptions { read : false , write : false , append : false , create_new : false }
151
+ OpenOptions { read : false , write : false , append : false , truncate : false , create_new : false }
138
152
}
139
153
140
154
pub fn read ( & mut self , read : bool ) {
@@ -146,7 +160,9 @@ impl OpenOptions {
146
160
pub fn append ( & mut self , append : bool ) {
147
161
self . append = append;
148
162
}
149
- pub fn truncate ( & mut self , _truncate : bool ) { }
163
+ pub fn truncate ( & mut self , truncate : bool ) {
164
+ self . truncate = truncate;
165
+ }
150
166
pub fn create ( & mut self , create : bool ) {
151
167
self . write = create;
152
168
}
@@ -164,6 +180,12 @@ impl File {
164
180
io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
165
181
} ) ?;
166
182
183
+ if opts. write && opts. read {
184
+ return Err ( io:: Error :: new (
185
+ io:: ErrorKind :: InvalidInput ,
186
+ "Files cannot be opened with read and write access" ,
187
+ ) ) ;
188
+ }
167
189
if opts. create_new {
168
190
let file_exists = unsafe { vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) } ;
169
191
if file_exists != 0 {
@@ -178,9 +200,19 @@ impl File {
178
200
} else if opts. write && opts. append {
179
201
// Open in read/write and append mode
180
202
unsafe { vex_sdk:: vexFileOpenWrite ( path. as_ptr ( ) ) }
181
- } else if opts. write {
203
+ } else if opts. write && opts . truncate {
182
204
// Open in read/write mode
183
205
unsafe { vex_sdk:: vexFileOpenCreate ( path. as_ptr ( ) ) }
206
+ } else if opts. write {
207
+ // Open in read/write and overwrite mode
208
+ unsafe {
209
+ // Open in read/write and append mode
210
+ let fd = vex_sdk:: vexFileOpenWrite ( path. as_ptr ( ) ) ;
211
+ // Seek to beginning of the file
212
+ vex_sdk:: vexFileSeek ( fd, 0 , 0 ) ;
213
+
214
+ fd
215
+ }
184
216
} else {
185
217
return Err ( io:: Error :: new (
186
218
io:: ErrorKind :: InvalidInput ,
@@ -196,7 +228,7 @@ impl File {
196
228
}
197
229
198
230
pub fn file_attr ( & self ) -> io:: Result < FileAttr > {
199
- todo ! ( )
231
+ FileAttr :: from_fd ( self . fd . 0 )
200
232
}
201
233
202
234
pub fn fsync ( & self ) -> io:: Result < ( ) > {
@@ -351,25 +383,7 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
351
383
let file = File :: open ( p, & opts) ?;
352
384
let fd = file. fd . 0 ;
353
385
354
- const SEEK_END : i32 = 2 ;
355
- const SEEK_SET : i32 = 0 ;
356
-
357
- let end = unsafe {
358
- let cur = vex_sdk:: vexFileTell ( fd) ;
359
- if cur < 0 {
360
- return Err ( io:: Error :: new ( io:: ErrorKind :: NotSeekable , "Failed to seek file" ) ) ;
361
- }
362
- map_fresult ( vex_sdk:: vexFileSeek ( fd, 0 , SEEK_END ) ) ?;
363
- let end = vex_sdk:: vexFileTell ( fd) ;
364
- map_fresult ( vex_sdk:: vexFileSeek ( fd, cur as _ , SEEK_SET ) ) ?;
365
- end
366
- } ;
367
-
368
- if end >= 0 {
369
- Ok ( FileAttr { size : end as u64 } )
370
- } else {
371
- Err ( io:: Error :: new ( io:: ErrorKind :: NotSeekable , "Failed to seek file" ) )
372
- }
386
+ FileAttr :: from_fd ( fd)
373
387
}
374
388
375
389
pub fn lstat ( p : & Path ) -> io:: Result < FileAttr > {
0 commit comments