stat.odin 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package os2
  2. import "base:runtime"
  3. import "core:strings"
  4. import "core:time"
  5. Fstat_Callback :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error)
  6. File_Info :: struct {
  7. fullpath: string,
  8. name: string,
  9. inode: u128, // might be zero if cannot be determined
  10. size: i64 `fmt:"M"`,
  11. mode: int `fmt:"o"`,
  12. type: File_Type,
  13. creation_time: time.Time,
  14. modification_time: time.Time,
  15. access_time: time.Time,
  16. }
  17. @(require_results)
  18. file_info_clone :: proc(fi: File_Info, allocator: runtime.Allocator) -> (cloned: File_Info, err: runtime.Allocator_Error) {
  19. cloned = fi
  20. cloned.fullpath = strings.clone(fi.fullpath, allocator) or_return
  21. _, cloned.name = split_path(cloned.fullpath)
  22. return
  23. }
  24. file_info_slice_delete :: proc(infos: []File_Info, allocator: runtime.Allocator) {
  25. #reverse for info in infos {
  26. file_info_delete(info, allocator)
  27. }
  28. delete(infos, allocator)
  29. }
  30. file_info_delete :: proc(fi: File_Info, allocator: runtime.Allocator) {
  31. delete(fi.fullpath, allocator)
  32. }
  33. @(require_results)
  34. fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
  35. if f == nil {
  36. return {}, nil
  37. } else if f.fstat != nil {
  38. return f->fstat(allocator)
  39. }
  40. return {}, .Invalid_Callback
  41. }
  42. @(require_results)
  43. stat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
  44. return _stat(name, allocator)
  45. }
  46. lstat :: stat_do_not_follow_links
  47. @(require_results)
  48. stat_do_not_follow_links :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
  49. return _lstat(name, allocator)
  50. }
  51. @(require_results)
  52. same_file :: proc(fi1, fi2: File_Info) -> bool {
  53. return _same_file(fi1, fi2)
  54. }
  55. last_write_time :: modification_time
  56. last_write_time_by_name :: modification_time_by_path
  57. @(require_results)
  58. modification_time :: proc(f: ^File) -> (time.Time, Error) {
  59. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  60. fi, err := fstat(f, temp_allocator)
  61. return fi.modification_time, err
  62. }
  63. @(require_results)
  64. modification_time_by_path :: proc(path: string) -> (time.Time, Error) {
  65. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  66. fi, err := stat(path, temp_allocator)
  67. return fi.modification_time, err
  68. }