aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjason <jkercher43@gmail.com>2024-07-30 10:19:09 -0400
committerjason <jkercher43@gmail.com>2024-07-30 10:19:09 -0400
commitc7eb2ae6bb9b0df2b28c9b65e3edbd9abfb6179f (patch)
tree4970eb25ba7fd4fb0dbb930a4a084717e42f4f6b
parent792640df1f475527c0ef72f8439dfba9e1e3fb15 (diff)
use sys/linux dirent instead of manual iteration
-rw-r--r--core/os/os2/path_linux.odin33
-rw-r--r--core/os/os2/process_linux.odin11
2 files changed, 12 insertions, 32 deletions
diff --git a/core/os/os2/path_linux.odin b/core/os/os2/path_linux.odin
index be60f9b86..ba2f7235c 100644
--- a/core/os/os2/path_linux.odin
+++ b/core/os/os2/path_linux.odin
@@ -1,6 +1,7 @@
//+private
package os2
+import "core:strings"
import "core:strconv"
import "base:runtime"
import "core:sys/linux"
@@ -75,14 +76,6 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
return nil if has_created else .Exist
}
-dirent64 :: struct {
- d_ino: u64,
- d_off: u64,
- d_reclen: u16,
- d_type: u8,
- d_name: [1]u8,
-}
-
_remove_all :: proc(path: string) -> Error {
DT_DIR :: 4
@@ -105,26 +98,18 @@ _remove_all :: proc(path: string) -> Error {
return _get_platform_error(errno)
}
- d: ^dirent64
-
- for i := 0; i < buflen; i += int(d.d_reclen) {
- d = (^dirent64)(rawptr(&buf[i]))
- d_name_cstr := cstring(&d.d_name[0])
-
- buf_len := uintptr(d.d_reclen) - offset_of(d.d_name)
-
- /* check for current directory (.) */
- #no_bounds_check if buf_len > 1 && d.d_name[0] == '.' && d.d_name[1] == 0 {
- continue
- }
+ offset: int
+ for d in linux.dirent_iterate_buf(buf[:buflen], &offset) {
+ d_name_str := linux.dirent_name(d)
+ d_name_cstr := strings.unsafe_string_to_cstring(d_name_str)
- /* check for parent directory (..) */
- #no_bounds_check if buf_len > 2 && d.d_name[0] == '.' && d.d_name[1] == '.' && d.d_name[2] == 0 {
+ /* check for current or parent directory (. or ..) */
+ if d_name_str == "." || d_name_str == ".." {
continue
}
- switch d.d_type {
- case DT_DIR:
+ #partial switch d.type {
+ case .DIR:
new_dfd: linux.Fd
new_dfd, errno = linux.openat(dfd, d_name_cstr, _OPENDIR_FLAGS)
if errno != .NONE {
diff --git a/core/os/os2/process_linux.odin b/core/os/os2/process_linux.odin
index 74b66f272..eed600c48 100644
--- a/core/os/os2/process_linux.odin
+++ b/core/os/os2/process_linux.odin
@@ -80,17 +80,12 @@ _process_list :: proc(allocator: runtime.Allocator) -> (list: []int, err: Error)
return {}, _get_platform_error(errno)
}
- d: ^dirent64
-
- for i := 0; i < buflen; i += int(d.d_reclen) {
- d = (^dirent64)(rawptr(&buf[i]))
- d_name_cstr := cstring(&d.d_name[0])
- #no_bounds_check d_name_str := string(d.d_name[:len(d_name_cstr)])
+ offset: int
+ for d in linux.dirent_iterate_buf(buf[:buflen], &offset) {
+ d_name_str := linux.dirent_name(d)
if pid, ok := strconv.parse_int(d_name_str); ok {
append(&dynamic_list, pid)
- } else {
- return nil, .Invalid_File
}
}
}