From e252d3bedf28e603ee3c33a4aebcfefbb5cfb66c Mon Sep 17 00:00:00 2001 From: CiD- Date: Wed, 23 Mar 2022 11:49:19 -0400 Subject: [PATCH] add os2.name --- core/os/os2/file_linux.odin | 27 +++++++++++++++++++++------ tests/core/os2/test_os2.odin | 25 ++++++++++++++++++++----- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/os/os2/file_linux.odin b/core/os/os2/file_linux.odin index 9030d265d..7040d0ed3 100644 --- a/core/os/os2/file_linux.odin +++ b/core/os/os2/file_linux.odin @@ -4,6 +4,7 @@ package os2 import "core:io" import "core:time" import "core:strings" +import "core:strconv" import "core:sys/unix" @@ -55,8 +56,20 @@ _close :: proc(fd: Handle) -> Error { } _name :: proc(fd: Handle, allocator := context.allocator) -> string { - //TODO - return "" + // NOTE: Not sure how portable this really is + PROC_FD_PATH :: "/proc/self/fd/" + + buf: [32]u8 + copy(buf[:], PROC_FD_PATH) + + strconv.itoa(buf[len(PROC_FD_PATH):], int(fd)) + + realpath: string + err: Error + if realpath, err = _read_link_cstr(cstring(&buf[0])); err != nil || realpath[0] != '/' { + return "" + } + return realpath } _seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) { @@ -189,10 +202,7 @@ _symlink :: proc(old_name, new_name: string) -> Error { return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr)) } -_read_link :: proc(name: string, allocator := context.allocator) -> (string, Error) { - name_cstr := strings.clone_to_cstring(name) - defer delete(name_cstr) - +_read_link_cstr :: proc(name_cstr: cstring, allocator := context.allocator) -> (string, Error) { bufsz : uint = 256 buf := make([]byte, bufsz, allocator) for { @@ -210,6 +220,11 @@ _read_link :: proc(name: string, allocator := context.allocator) -> (string, Err } } +_read_link :: proc(name: string, allocator := context.allocator) -> (string, Error) { + name_cstr := strings.clone_to_cstring(name, context.temp_allocator) + return _read_link_cstr(name_cstr, allocator) +} + _unlink :: proc(name: string) -> Error { name_cstr := strings.clone_to_cstring(name, context.temp_allocator) return _ok_or_error(unix.sys_unlink(name_cstr)) diff --git a/tests/core/os2/test_os2.odin b/tests/core/os2/test_os2.odin index e52351f03..c588e532e 100644 --- a/tests/core/os2/test_os2.odin +++ b/tests/core/os2/test_os2.odin @@ -1,9 +1,11 @@ package test_os2 +import "core:os/os2" + import "core:os" import "core:fmt" import "core:mem" -import "core:os/os2" +import "core:strings" import "core:testing" import "core:intrinsics" @@ -116,7 +118,7 @@ file_test :: proc(t: ^testing.T) { _expect_no_error(t, err) expect_value(t, n, 10) - // seek to the "ll" in "hello" + // seek FROM BEGINNING to the "ll" in "hello" n64: i64 n64, err = os2.seek(fd, 12, .Start) _expect_no_error(t, err) @@ -127,7 +129,7 @@ file_test :: proc(t: ^testing.T) { _expect_no_error(t, err) expect_value(t, n, 2) - // seek to the "e" in "he11o" + // seek BACK to the "e" in "he11o" n64, err = os2.seek(fd, -3, .Current) _expect_no_error(t, err) expect_value(t, n64, 11) @@ -137,7 +139,7 @@ file_test :: proc(t: ^testing.T) { _expect_no_error(t, err) expect_value(t, n, 1) - // seek to the "o" in "h311o" + // seek FROM THE END the "o" in "h311o" n64, err = os2.seek(fd, -1, .End) _expect_no_error(t, err) expect_value(t, n64, 14) @@ -157,11 +159,24 @@ file_test :: proc(t: ^testing.T) { expect(t, unix.sys_access("file.txt", X_OK) == 0, "expected exec permission") } + /* Build expected full path via cwd and known file name */ + parts: [2]string + parts[0], err = os2.getwd() + defer delete(parts[0]) + _expect_no_error(t, err) + + parts[1] = "/file.txt" + expected_full_path := strings.concatenate(parts[:]) + defer delete(expected_full_path) + + full_path := os2.name(fd) + defer delete(full_path) + expect_value(t, full_path, expected_full_path) + // NOTE: chown not possible without root user //_expect_no_error(t, os2.chown(fd, 0, 0)) _expect_no_error(t, os2.close(fd)) - fd, err = os2.open("file.txt") _expect_no_error(t, err)