@@ -222,6 +222,24 @@ impl OpenOptions {
222222impl File {
223223 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
224224 run_path_with_cstr ( path, & |path| {
225+ // Enforce the invariants of `create_new`/`create`.
226+ //
227+ // Since VEXos doesn't have anything akin to POSIX's `oflags`, we need to enforce
228+ // the requirements that `create_new` can't have an existing file and `!create`
229+ // doesn't create a file ourselves.
230+ if !opts. read && ( opts. write || opts. append ) && ( opts. create_new || !opts. create ) {
231+ let status = vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) ;
232+
233+ if opts. create_new && status != 0 {
234+ return Err ( io:: const_error!( io:: ErrorKind :: AlreadyExists , "File exists" , ) ) ;
235+ } else if !opts. create && status == 0 {
236+ return Err ( io:: const_error!(
237+ io:: ErrorKind :: NotFound ,
238+ "No such file or directory" ,
239+ ) ) ;
240+ }
241+ }
242+
225243 let file = match opts {
226244 // read + write - unsupported
227245 OpenOptions { read : true , write : true , .. } => {
@@ -247,53 +265,19 @@ impl File {
247265 write : _,
248266 append : true ,
249267 truncate : false ,
250- create,
251- create_new,
252- } => unsafe {
253- if * create_new {
254- if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
255- return Err ( io:: const_error!(
256- io:: ErrorKind :: AlreadyExists ,
257- "File exists" ,
258- ) ) ;
259- }
260- } else if !* create {
261- if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) == 0 {
262- return Err ( io:: const_error!(
263- io:: ErrorKind :: NotFound ,
264- "No such file or directory" ,
265- ) ) ;
266- }
267- }
268-
269- vex_sdk:: vexFileOpenWrite ( path. as_ptr ( ) )
270- } ,
268+ create : _,
269+ create_new : _,
270+ } => unsafe { vex_sdk:: vexFileOpenWrite ( path. as_ptr ( ) ) } ,
271271
272272 // write
273273 OpenOptions {
274274 read : false ,
275275 write : true ,
276276 append : false ,
277277 truncate,
278- create,
279- create_new,
278+ create : _ ,
279+ create_new : _ ,
280280 } => unsafe {
281- if * create_new {
282- if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
283- return Err ( io:: const_error!(
284- io:: ErrorKind :: AlreadyExists ,
285- "File exists" ,
286- ) ) ;
287- }
288- } else if !* create {
289- if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) == 0 {
290- return Err ( io:: const_error!(
291- io:: ErrorKind :: NotFound ,
292- "No such file or directory" ,
293- ) ) ;
294- }
295- }
296-
297281 if * truncate {
298282 vex_sdk:: vexFileOpenCreate ( path. as_ptr ( ) )
299283 } else {
0 commit comments