aboutsummaryrefslogtreecommitdiff
path: root/core/os/file_posix_freebsd.odin
blob: 4ed9ecc1eb44882bfc38f75d2780c69aba1b2b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#+private
package os

import "base:runtime"

import "core:c"
import "core:sys/posix"

_posix_absolute_path :: proc(fd: posix.FD, name: string, allocator: runtime.Allocator) -> (path: cstring, err: Error) {
	// NOTE(Feoramund): The situation isn't ideal, but this was the best way I
	// could find to implement this. There are a couple outstanding bug reports
	// regarding the desire to retrieve an absolute path from a handle, but to
	// my knowledge, there hasn't been any work done on it.
	//
	// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=198570
	//
	// This may be unreliable, according to a comment from 2023.

	KInfo_File :: struct {
		structsize: c.int,
		type:       c.int,
		fd:         c.int,
		ref_count:  c.int,
		flags:      c.int,
		pad0:       c.int,
		offset:     i64,

		// NOTE(Feoramund): This field represents a complicated union that I am
		// avoiding implementing for now. I only need the path data below.
		_union: [336]byte,

		path: [posix.PATH_MAX]c.char,
	}

	F_KINFO :: 22

	kinfo: KInfo_File
	kinfo.structsize = size_of(KInfo_File)

	res := posix.fcntl(fd, posix.FCNTL_Cmd(F_KINFO), &kinfo)
	if res == -1 {
		err = _get_platform_error()
		return
	}

	return clone_to_cstring(string(cstring(&kinfo.path[0])), allocator)
}