stat_linux.odin 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //+private
  2. package os2
  3. import "core:time"
  4. import "base:runtime"
  5. import "core:sys/linux"
  6. import "core:path/filepath"
  7. _fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
  8. impl := (^File_Impl)(f.impl)
  9. return _fstat_internal(impl.fd, allocator)
  10. }
  11. _fstat_internal :: proc(fd: linux.Fd, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  12. s: linux.Stat
  13. errno := linux.fstat(fd, &s)
  14. if errno != .NONE {
  15. return {}, _get_platform_error(errno)
  16. }
  17. type := File_Type.Regular
  18. switch s.mode & linux.S_IFMT {
  19. case linux.S_IFBLK: type = .Block_Device
  20. case linux.S_IFCHR: type = .Character_Device
  21. case linux.S_IFDIR: type = .Directory
  22. case linux.S_IFIFO: type = .Named_Pipe
  23. case linux.S_IFLNK: type = .Symlink
  24. case linux.S_IFREG: type = .Regular
  25. case linux.S_IFSOCK: type = .Socket
  26. }
  27. mode := int(0o7777 & transmute(u32)s.mode)
  28. // TODO: As of Linux 4.11, the new statx syscall can retrieve creation_time
  29. fi = File_Info {
  30. fullpath = _get_full_path(fd, allocator) or_return,
  31. name = "",
  32. inode = u128(u64(s.ino)),
  33. size = i64(s.size),
  34. mode = mode,
  35. type = type,
  36. modification_time = time.Time {i64(s.mtime.time_sec) * i64(time.Second) + i64(s.mtime.time_nsec)},
  37. access_time = time.Time {i64(s.atime.time_sec) * i64(time.Second) + i64(s.atime.time_nsec)},
  38. creation_time = time.Time{i64(s.ctime.time_sec) * i64(time.Second) + i64(s.ctime.time_nsec)}, // regular stat does not provide this
  39. }
  40. fi.creation_time = fi.modification_time
  41. fi.name = filepath.base(fi.fullpath)
  42. return
  43. }
  44. // NOTE: _stat and _lstat are using _fstat to avoid a race condition when populating fullpath
  45. _stat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  46. TEMP_ALLOCATOR_GUARD()
  47. name_cstr := temp_cstring(name) or_return
  48. fd, errno := linux.open(name_cstr, {})
  49. if errno != .NONE {
  50. return {}, _get_platform_error(errno)
  51. }
  52. defer linux.close(fd)
  53. return _fstat_internal(fd, allocator)
  54. }
  55. _lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  56. TEMP_ALLOCATOR_GUARD()
  57. name_cstr := temp_cstring(name) or_return
  58. fd, errno := linux.open(name_cstr, {.PATH, .NOFOLLOW})
  59. if errno != .NONE {
  60. return {}, _get_platform_error(errno)
  61. }
  62. defer linux.close(fd)
  63. return _fstat_internal(fd, allocator)
  64. }
  65. _same_file :: proc(fi1, fi2: File_Info) -> bool {
  66. return fi1.fullpath == fi2.fullpath
  67. }