stat_wasi.odin 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #+private
  2. package os2
  3. import "base:runtime"
  4. import "core:sys/wasm/wasi"
  5. import "core:time"
  6. internal_stat :: proc(stat: wasi.filestat_t, fullpath: string) -> (fi: File_Info) {
  7. fi.fullpath = fullpath
  8. _, fi.name = split_path(fi.fullpath)
  9. fi.inode = u128(stat.ino)
  10. fi.size = i64(stat.size)
  11. switch stat.filetype {
  12. case .BLOCK_DEVICE: fi.type = .Block_Device
  13. case .CHARACTER_DEVICE: fi.type = .Character_Device
  14. case .DIRECTORY: fi.type = .Directory
  15. case .REGULAR_FILE: fi.type = .Regular
  16. case .SOCKET_DGRAM, .SOCKET_STREAM: fi.type = .Socket
  17. case .SYMBOLIC_LINK: fi.type = .Symlink
  18. case .UNKNOWN: fi.type = .Undetermined
  19. case: fi.type = .Undetermined
  20. }
  21. fi.creation_time = time.Time{_nsec=i64(stat.ctim)}
  22. fi.modification_time = time.Time{_nsec=i64(stat.mtim)}
  23. fi.access_time = time.Time{_nsec=i64(stat.atim)}
  24. return
  25. }
  26. _fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  27. if f == nil || f.impl == nil {
  28. err = .Invalid_File
  29. return
  30. }
  31. impl := (^File_Impl)(f.impl)
  32. stat, _err := wasi.fd_filestat_get(__fd(f))
  33. if _err != nil {
  34. err = _get_platform_error(_err)
  35. return
  36. }
  37. fullpath := clone_string(impl.name, allocator) or_return
  38. return internal_stat(stat, fullpath), nil
  39. }
  40. _stat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  41. if name == "" {
  42. err = .Invalid_Path
  43. return
  44. }
  45. dir_fd, relative, ok := match_preopen(name)
  46. if !ok {
  47. err = .Invalid_Path
  48. return
  49. }
  50. stat, _err := wasi.path_filestat_get(dir_fd, {.SYMLINK_FOLLOW}, relative)
  51. if _err != nil {
  52. err = _get_platform_error(_err)
  53. return
  54. }
  55. // NOTE: wasi doesn't really do full paths afact.
  56. fullpath := clone_string(name, allocator) or_return
  57. return internal_stat(stat, fullpath), nil
  58. }
  59. _lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  60. if name == "" {
  61. err = .Invalid_Path
  62. return
  63. }
  64. dir_fd, relative, ok := match_preopen(name)
  65. if !ok {
  66. err = .Invalid_Path
  67. return
  68. }
  69. stat, _err := wasi.path_filestat_get(dir_fd, {}, relative)
  70. if _err != nil {
  71. err = _get_platform_error(_err)
  72. return
  73. }
  74. // NOTE: wasi doesn't really do full paths afact.
  75. fullpath := clone_string(name, allocator) or_return
  76. return internal_stat(stat, fullpath), nil
  77. }
  78. _same_file :: proc(fi1, fi2: File_Info) -> bool {
  79. return fi1.fullpath == fi2.fullpath
  80. }