@@ -248,43 +248,47 @@ pub unsafe extern "C" fn fd_datasync(fd: Fd) -> Errno {
248
248
pub unsafe extern "C" fn fd_fdstat_get ( fd : Fd , stat : * mut Fdstat ) -> Errno {
249
249
match Descriptor :: get ( fd) {
250
250
Descriptor :: File ( file) => {
251
- let info = match wasi_filesystem:: info ( file. fd ) {
251
+ let flags = match wasi_filesystem:: flags ( file. fd ) {
252
+ Ok ( info) => info,
253
+ Err ( err) => return errno_from_wasi_filesystem ( err) ,
254
+ } ;
255
+ let type_ = match wasi_filesystem:: todo_type ( file. fd ) {
252
256
Ok ( info) => info,
253
257
Err ( err) => return errno_from_wasi_filesystem ( err) ,
254
258
} ;
255
259
256
- let fs_filetype = match info . type_ {
257
- wasi_filesystem:: Type :: RegularFile => FILETYPE_REGULAR_FILE ,
258
- wasi_filesystem:: Type :: Directory => FILETYPE_DIRECTORY ,
259
- wasi_filesystem:: Type :: BlockDevice => FILETYPE_BLOCK_DEVICE ,
260
- wasi_filesystem:: Type :: CharacterDevice => FILETYPE_CHARACTER_DEVICE ,
261
- wasi_filesystem:: Type :: Fifo => FILETYPE_UNKNOWN ,
262
- wasi_filesystem:: Type :: Socket => FILETYPE_SOCKET_STREAM ,
263
- wasi_filesystem:: Type :: SymbolicLink => FILETYPE_SYMBOLIC_LINK ,
264
- wasi_filesystem:: Type :: Unknown => FILETYPE_UNKNOWN ,
260
+ let fs_filetype = match type_ {
261
+ wasi_filesystem:: DescriptorType :: RegularFile => FILETYPE_REGULAR_FILE ,
262
+ wasi_filesystem:: DescriptorType :: Directory => FILETYPE_DIRECTORY ,
263
+ wasi_filesystem:: DescriptorType :: BlockDevice => FILETYPE_BLOCK_DEVICE ,
264
+ wasi_filesystem:: DescriptorType :: CharacterDevice => FILETYPE_CHARACTER_DEVICE ,
265
+ wasi_filesystem:: DescriptorType :: Fifo => FILETYPE_UNKNOWN ,
266
+ wasi_filesystem:: DescriptorType :: Socket => FILETYPE_SOCKET_STREAM ,
267
+ wasi_filesystem:: DescriptorType :: SymbolicLink => FILETYPE_SYMBOLIC_LINK ,
268
+ wasi_filesystem:: DescriptorType :: Unknown => FILETYPE_UNKNOWN ,
265
269
} ;
266
270
267
271
let mut fs_flags = 0 ;
268
272
let mut fs_rights_base = !0 ;
269
- if !info . flags . contains ( wasi_filesystem:: Flags :: READ ) {
273
+ if !flags. contains ( wasi_filesystem:: DescriptorFlags :: READ ) {
270
274
fs_rights_base &= !RIGHTS_FD_READ ;
271
275
}
272
- if !info . flags . contains ( wasi_filesystem:: Flags :: WRITE ) {
276
+ if !flags. contains ( wasi_filesystem:: DescriptorFlags :: WRITE ) {
273
277
fs_rights_base &= !RIGHTS_FD_WRITE ;
274
278
}
275
- if info . flags . contains ( wasi_filesystem:: Flags :: APPEND ) {
279
+ if flags. contains ( wasi_filesystem:: DescriptorFlags :: APPEND ) {
276
280
fs_flags |= FDFLAGS_APPEND ;
277
281
}
278
- if info . flags . contains ( wasi_filesystem:: Flags :: DSYNC ) {
282
+ if flags. contains ( wasi_filesystem:: DescriptorFlags :: DSYNC ) {
279
283
fs_flags |= FDFLAGS_DSYNC ;
280
284
}
281
- if info . flags . contains ( wasi_filesystem:: Flags :: NONBLOCK ) {
285
+ if flags. contains ( wasi_filesystem:: DescriptorFlags :: NONBLOCK ) {
282
286
fs_flags |= FDFLAGS_NONBLOCK ;
283
287
}
284
- if info . flags . contains ( wasi_filesystem:: Flags :: RSYNC ) {
288
+ if flags. contains ( wasi_filesystem:: DescriptorFlags :: RSYNC ) {
285
289
fs_flags |= FDFLAGS_RSYNC ;
286
290
}
287
- if info . flags . contains ( wasi_filesystem:: Flags :: SYNC ) {
291
+ if flags. contains ( wasi_filesystem:: DescriptorFlags :: SYNC ) {
288
292
fs_flags |= FDFLAGS_SYNC ;
289
293
}
290
294
let fs_rights_inheriting = fs_rights_base;
@@ -318,7 +322,31 @@ pub unsafe extern "C" fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno {
318
322
/// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
319
323
#[ no_mangle]
320
324
pub unsafe extern "C" fn fd_fdstat_set_flags ( fd : Fd , flags : Fdflags ) -> Errno {
321
- unreachable ( )
325
+ let mut new_flags = wasi_filesystem:: DescriptorFlags :: empty ( ) ;
326
+ if flags & FDFLAGS_APPEND == FDFLAGS_APPEND {
327
+ new_flags |= wasi_filesystem:: DescriptorFlags :: APPEND ;
328
+ }
329
+ if flags & FDFLAGS_DSYNC == FDFLAGS_DSYNC {
330
+ new_flags |= wasi_filesystem:: DescriptorFlags :: DSYNC ;
331
+ }
332
+ if flags & FDFLAGS_NONBLOCK == FDFLAGS_NONBLOCK {
333
+ new_flags |= wasi_filesystem:: DescriptorFlags :: NONBLOCK ;
334
+ }
335
+ if flags & FDFLAGS_RSYNC == FDFLAGS_RSYNC {
336
+ new_flags |= wasi_filesystem:: DescriptorFlags :: RSYNC ;
337
+ }
338
+ if flags & FDFLAGS_SYNC == FDFLAGS_SYNC {
339
+ new_flags |= wasi_filesystem:: DescriptorFlags :: SYNC ;
340
+ }
341
+
342
+ match Descriptor :: get ( fd) {
343
+ Descriptor :: File ( file) => match wasi_filesystem:: set_flags ( file. fd , new_flags) {
344
+ Ok ( ( ) ) => ERRNO_SUCCESS ,
345
+ Err ( err) => errno_from_wasi_filesystem ( err) ,
346
+ } ,
347
+ Descriptor :: Log => ERRNO_INVAL ,
348
+ Descriptor :: Closed => ERRNO_BADF ,
349
+ }
322
350
}
323
351
324
352
/// Adjust the rights associated with a file descriptor.
@@ -335,7 +363,39 @@ pub unsafe extern "C" fn fd_fdstat_set_rights(
335
363
/// Return the attributes of an open file.
336
364
#[ no_mangle]
337
365
pub unsafe extern "C" fn fd_filestat_get ( fd : Fd , buf : * mut Filestat ) -> Errno {
338
- unreachable ( )
366
+ match Descriptor :: get ( fd) {
367
+ Descriptor :: File ( file) => match wasi_filesystem:: stat ( file. fd ) {
368
+ Ok ( stat) => {
369
+ let filetype = match stat. type_ {
370
+ wasi_filesystem:: DescriptorType :: Unknown => FILETYPE_UNKNOWN ,
371
+ wasi_filesystem:: DescriptorType :: Directory => FILETYPE_DIRECTORY ,
372
+ wasi_filesystem:: DescriptorType :: BlockDevice => FILETYPE_BLOCK_DEVICE ,
373
+ wasi_filesystem:: DescriptorType :: RegularFile => FILETYPE_REGULAR_FILE ,
374
+ // TODO: Add a way to disginguish between FILETYPE_SOCKET_STREAM and
375
+ // FILETYPE_SOCKET_DGRAM.
376
+ wasi_filesystem:: DescriptorType :: Socket => unreachable ( ) ,
377
+ wasi_filesystem:: DescriptorType :: SymbolicLink => FILETYPE_SYMBOLIC_LINK ,
378
+ wasi_filesystem:: DescriptorType :: CharacterDevice => FILETYPE_CHARACTER_DEVICE ,
379
+ // preview1 never had a FIFO code.
380
+ wasi_filesystem:: DescriptorType :: Fifo => FILETYPE_UNKNOWN ,
381
+ } ;
382
+ * buf = Filestat {
383
+ dev : stat. dev ,
384
+ ino : stat. ino ,
385
+ filetype,
386
+ nlink : stat. nlink ,
387
+ size : stat. size ,
388
+ atim : stat. atim ,
389
+ mtim : stat. mtim ,
390
+ ctim : stat. ctim ,
391
+ } ;
392
+ ERRNO_SUCCESS
393
+ }
394
+ Err ( err) => errno_from_wasi_filesystem ( err) ,
395
+ } ,
396
+ Descriptor :: Closed => ERRNO_BADF ,
397
+ Descriptor :: Log => ERRNO_NOTDIR ,
398
+ }
339
399
}
340
400
341
401
/// Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
@@ -715,17 +775,17 @@ pub unsafe extern "C" fn path_filestat_get(
715
775
Descriptor :: File ( file) => match wasi_filesystem:: stat_at ( file. fd , at_flags, path) {
716
776
Ok ( stat) => {
717
777
let filetype = match stat. type_ {
718
- wasi_filesystem:: Type :: Unknown => FILETYPE_UNKNOWN ,
719
- wasi_filesystem:: Type :: Directory => FILETYPE_DIRECTORY ,
720
- wasi_filesystem:: Type :: BlockDevice => FILETYPE_BLOCK_DEVICE ,
721
- wasi_filesystem:: Type :: RegularFile => FILETYPE_REGULAR_FILE ,
778
+ wasi_filesystem:: DescriptorType :: Unknown => FILETYPE_UNKNOWN ,
779
+ wasi_filesystem:: DescriptorType :: Directory => FILETYPE_DIRECTORY ,
780
+ wasi_filesystem:: DescriptorType :: BlockDevice => FILETYPE_BLOCK_DEVICE ,
781
+ wasi_filesystem:: DescriptorType :: RegularFile => FILETYPE_REGULAR_FILE ,
722
782
// TODO: Add a way to disginguish between FILETYPE_SOCKET_STREAM and
723
783
// FILETYPE_SOCKET_DGRAM.
724
- wasi_filesystem:: Type :: Socket => unreachable ( ) ,
725
- wasi_filesystem:: Type :: SymbolicLink => FILETYPE_SYMBOLIC_LINK ,
726
- wasi_filesystem:: Type :: CharacterDevice => FILETYPE_CHARACTER_DEVICE ,
784
+ wasi_filesystem:: DescriptorType :: Socket => unreachable ( ) ,
785
+ wasi_filesystem:: DescriptorType :: SymbolicLink => FILETYPE_SYMBOLIC_LINK ,
786
+ wasi_filesystem:: DescriptorType :: CharacterDevice => FILETYPE_CHARACTER_DEVICE ,
727
787
// preview1 never had a FIFO code.
728
- wasi_filesystem:: Type :: Fifo => FILETYPE_UNKNOWN ,
788
+ wasi_filesystem:: DescriptorType :: Fifo => FILETYPE_UNKNOWN ,
729
789
} ;
730
790
* buf = Filestat {
731
791
dev : stat. dev ,
0 commit comments