mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-29 10:50:05 +00:00
add os2.name
This commit is contained in:
@@ -4,6 +4,7 @@ package os2
|
|||||||
import "core:io"
|
import "core:io"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
import "core:strconv"
|
||||||
import "core:sys/unix"
|
import "core:sys/unix"
|
||||||
|
|
||||||
|
|
||||||
@@ -55,8 +56,20 @@ _close :: proc(fd: Handle) -> Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_name :: proc(fd: Handle, allocator := context.allocator) -> string {
|
_name :: proc(fd: Handle, allocator := context.allocator) -> string {
|
||||||
//TODO
|
// NOTE: Not sure how portable this really is
|
||||||
return ""
|
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) {
|
_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))
|
return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr))
|
||||||
}
|
}
|
||||||
|
|
||||||
_read_link :: proc(name: string, allocator := context.allocator) -> (string, Error) {
|
_read_link_cstr :: proc(name_cstr: cstring, allocator := context.allocator) -> (string, Error) {
|
||||||
name_cstr := strings.clone_to_cstring(name)
|
|
||||||
defer delete(name_cstr)
|
|
||||||
|
|
||||||
bufsz : uint = 256
|
bufsz : uint = 256
|
||||||
buf := make([]byte, bufsz, allocator)
|
buf := make([]byte, bufsz, allocator)
|
||||||
for {
|
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 {
|
_unlink :: proc(name: string) -> Error {
|
||||||
name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
||||||
return _ok_or_error(unix.sys_unlink(name_cstr))
|
return _ok_or_error(unix.sys_unlink(name_cstr))
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package test_os2
|
package test_os2
|
||||||
|
|
||||||
|
import "core:os/os2"
|
||||||
|
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os/os2"
|
import "core:strings"
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
@@ -116,7 +118,7 @@ file_test :: proc(t: ^testing.T) {
|
|||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
expect_value(t, n, 10)
|
expect_value(t, n, 10)
|
||||||
|
|
||||||
// seek to the "ll" in "hello"
|
// seek FROM BEGINNING to the "ll" in "hello"
|
||||||
n64: i64
|
n64: i64
|
||||||
n64, err = os2.seek(fd, 12, .Start)
|
n64, err = os2.seek(fd, 12, .Start)
|
||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
@@ -127,7 +129,7 @@ file_test :: proc(t: ^testing.T) {
|
|||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
expect_value(t, n, 2)
|
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)
|
n64, err = os2.seek(fd, -3, .Current)
|
||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
expect_value(t, n64, 11)
|
expect_value(t, n64, 11)
|
||||||
@@ -137,7 +139,7 @@ file_test :: proc(t: ^testing.T) {
|
|||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
expect_value(t, n, 1)
|
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)
|
n64, err = os2.seek(fd, -1, .End)
|
||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
expect_value(t, n64, 14)
|
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")
|
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
|
// NOTE: chown not possible without root user
|
||||||
//_expect_no_error(t, os2.chown(fd, 0, 0))
|
//_expect_no_error(t, os2.chown(fd, 0, 0))
|
||||||
_expect_no_error(t, os2.close(fd))
|
_expect_no_error(t, os2.close(fd))
|
||||||
|
|
||||||
|
|
||||||
fd, err = os2.open("file.txt")
|
fd, err = os2.open("file.txt")
|
||||||
_expect_no_error(t, err)
|
_expect_no_error(t, err)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user