mirror of
https://github.com/odin-lang/Odin.git
synced 2025-12-30 01:44:36 +00:00
add os2.name
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user