file_posix_freebsd.odin 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //+private
  2. package os2
  3. import "base:runtime"
  4. import "core:c"
  5. import "core:sys/posix"
  6. _posix_absolute_path :: proc(fd: posix.FD, name: string, allocator: runtime.Allocator) -> (path: cstring, err: Error) {
  7. // NOTE(Feoramund): The situation isn't ideal, but this was the best way I
  8. // could find to implement this. There are a couple outstanding bug reports
  9. // regarding the desire to retrieve an absolute path from a handle, but to
  10. // my knowledge, there hasn't been any work done on it.
  11. //
  12. // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=198570
  13. //
  14. // This may be unreliable, according to a comment from 2023.
  15. KInfo_File :: struct {
  16. structsize: c.int,
  17. type: c.int,
  18. fd: c.int,
  19. ref_count: c.int,
  20. flags: c.int,
  21. pad0: c.int,
  22. offset: i64,
  23. // NOTE(Feoramund): This field represents a complicated union that I am
  24. // avoiding implementing for now. I only need the path data below.
  25. _union: [336]byte,
  26. path: [posix.PATH_MAX]c.char,
  27. }
  28. F_KINFO :: 22
  29. kinfo: KInfo_File
  30. kinfo.structsize = size_of(KInfo_File)
  31. res := posix.fcntl(fd, posix.FCNTL_Cmd(F_KINFO), &kinfo)
  32. if res == -1 {
  33. err = _get_platform_error()
  34. return
  35. }
  36. return clone_to_cstring(string(cstring(&kinfo.path[0])), allocator)
  37. }