stat.odin 2.2 KB

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