mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Decouple usage of filepath from os2
This commit is contained in:
@@ -3,7 +3,6 @@ package os2
|
|||||||
import "base:intrinsics"
|
import "base:intrinsics"
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
|
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
Path_Separator :: _Path_Separator // OS-Specific
|
Path_Separator :: _Path_Separator // OS-Specific
|
||||||
@@ -91,7 +90,7 @@ Get the directory for the currently running executable.
|
|||||||
*/
|
*/
|
||||||
get_executable_directory :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) {
|
get_executable_directory :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) {
|
||||||
path = _get_executable_path(allocator) or_return
|
path = _get_executable_path(allocator) or_return
|
||||||
path, _ = filepath.split(path)
|
path, _ = split_path(path)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
package os2
|
package os2
|
||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:path/filepath"
|
|
||||||
|
|
||||||
import "core:sys/posix"
|
import "core:sys/posix"
|
||||||
|
|
||||||
@@ -35,11 +34,11 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
|
|||||||
return .Exist
|
return .Exist
|
||||||
}
|
}
|
||||||
|
|
||||||
clean_path := filepath.clean(path, temp_allocator())
|
clean_path := clean_path(path, temp_allocator()) or_return
|
||||||
return internal_mkdir_all(clean_path, perm)
|
return internal_mkdir_all(clean_path, perm)
|
||||||
|
|
||||||
internal_mkdir_all :: proc(path: string, perm: int) -> Error {
|
internal_mkdir_all :: proc(path: string, perm: int) -> Error {
|
||||||
dir, file := filepath.split(path)
|
dir, file := split_path(path)
|
||||||
if file != path && dir != "/" {
|
if file != path && dir != "/" {
|
||||||
if len(dir) > 1 && dir[len(dir) - 1] == '/' {
|
if len(dir) > 1 && dir[len(dir) - 1] == '/' {
|
||||||
dir = dir[:len(dir) - 1]
|
dir = dir[:len(dir) - 1]
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package os2
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
|
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:sync"
|
import "core:sync"
|
||||||
import "core:sys/wasm/wasi"
|
import "core:sys/wasm/wasi"
|
||||||
|
|
||||||
@@ -35,11 +34,11 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
|
|||||||
return .Exist
|
return .Exist
|
||||||
}
|
}
|
||||||
|
|
||||||
clean_path := filepath.clean(path, temp_allocator())
|
clean_path := clean_path(path, temp_allocator())
|
||||||
return internal_mkdir_all(clean_path)
|
return internal_mkdir_all(clean_path)
|
||||||
|
|
||||||
internal_mkdir_all :: proc(path: string) -> Error {
|
internal_mkdir_all :: proc(path: string) -> Error {
|
||||||
dir, file := filepath.split(path)
|
dir, file := split_path(path)
|
||||||
if file != path && dir != "/" {
|
if file != path && dir != "/" {
|
||||||
if len(dir) > 1 && dir[len(dir) - 1] == '/' {
|
if len(dir) > 1 && dir[len(dir) - 1] == '/' {
|
||||||
dir = dir[:len(dir) - 1]
|
dir = dir[:len(dir) - 1]
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import "core:slice"
|
|||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:sys/linux"
|
import "core:sys/linux"
|
||||||
import "core:path/filepath"
|
|
||||||
|
|
||||||
PIDFD_UNASSIGNED :: ~uintptr(0)
|
PIDFD_UNASSIGNED :: ~uintptr(0)
|
||||||
|
|
||||||
@@ -205,7 +204,7 @@ _process_info_by_pid :: proc(pid: int, selection: Process_Info_Fields, allocator
|
|||||||
info.executable_path = strings.clone(cmdline[:terminator], allocator) or_return
|
info.executable_path = strings.clone(cmdline[:terminator], allocator) or_return
|
||||||
info.fields += {.Executable_Path}
|
info.fields += {.Executable_Path}
|
||||||
} else if cwd_err == nil {
|
} else if cwd_err == nil {
|
||||||
info.executable_path = filepath.join({ cwd, cmdline[:terminator] }, allocator) or_return
|
info.executable_path = join_path({ cwd, cmdline[:terminator] }, allocator) or_return
|
||||||
info.fields += {.Executable_Path}
|
info.fields += {.Executable_Path}
|
||||||
} else {
|
} else {
|
||||||
break cmdline_if
|
break cmdline_if
|
||||||
@@ -407,7 +406,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
|
|||||||
executable_name := desc.command[0]
|
executable_name := desc.command[0]
|
||||||
if strings.index_byte(executable_name, '/') < 0 {
|
if strings.index_byte(executable_name, '/') < 0 {
|
||||||
path_env := get_env("PATH", temp_allocator())
|
path_env := get_env("PATH", temp_allocator())
|
||||||
path_dirs := filepath.split_list(path_env, temp_allocator()) or_return
|
path_dirs := split_path_list(path_env, temp_allocator()) or_return
|
||||||
|
|
||||||
exe_builder := strings.builder_make(temp_allocator()) or_return
|
exe_builder := strings.builder_make(temp_allocator()) or_return
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import "base:runtime"
|
|||||||
|
|
||||||
import "core:time"
|
import "core:time"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:path/filepath"
|
|
||||||
|
|
||||||
import kq "core:sys/kqueue"
|
import kq "core:sys/kqueue"
|
||||||
import "core:sys/posix"
|
import "core:sys/posix"
|
||||||
@@ -62,7 +61,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
|
|||||||
exe_name := desc.command[0]
|
exe_name := desc.command[0]
|
||||||
if strings.index_byte(exe_name, '/') < 0 {
|
if strings.index_byte(exe_name, '/') < 0 {
|
||||||
path_env := get_env("PATH", temp_allocator())
|
path_env := get_env("PATH", temp_allocator())
|
||||||
path_dirs := filepath.split_list(path_env, temp_allocator())
|
path_dirs := split_path_list(path_env, temp_allocator()) or_return
|
||||||
|
|
||||||
found: bool
|
found: bool
|
||||||
for dir in path_dirs {
|
for dir in path_dirs {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package os2
|
package os2
|
||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
@@ -25,7 +24,7 @@ File_Info :: struct {
|
|||||||
file_info_clone :: proc(fi: File_Info, allocator: runtime.Allocator) -> (cloned: File_Info, err: runtime.Allocator_Error) {
|
file_info_clone :: proc(fi: File_Info, allocator: runtime.Allocator) -> (cloned: File_Info, err: runtime.Allocator_Error) {
|
||||||
cloned = fi
|
cloned = fi
|
||||||
cloned.fullpath = strings.clone(fi.fullpath, allocator) or_return
|
cloned.fullpath = strings.clone(fi.fullpath, allocator) or_return
|
||||||
cloned.name = filepath.base(cloned.fullpath)
|
_, cloned.name = split_path(cloned.fullpath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package os2
|
|||||||
import "core:time"
|
import "core:time"
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:sys/linux"
|
import "core:sys/linux"
|
||||||
import "core:path/filepath"
|
|
||||||
|
|
||||||
_fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
|
_fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
|
||||||
impl := (^File_Impl)(f.impl)
|
impl := (^File_Impl)(f.impl)
|
||||||
@@ -42,7 +41,7 @@ _fstat_internal :: proc(fd: linux.Fd, allocator: runtime.Allocator) -> (fi: File
|
|||||||
creation_time = time.Time{i64(s.ctime.time_sec) * i64(time.Second) + i64(s.ctime.time_nsec)}, // regular stat does not provide this
|
creation_time = time.Time{i64(s.ctime.time_sec) * i64(time.Second) + i64(s.ctime.time_nsec)}, // regular stat does not provide this
|
||||||
}
|
}
|
||||||
fi.creation_time = fi.modification_time
|
fi.creation_time = fi.modification_time
|
||||||
fi.name = filepath.base(fi.fullpath)
|
_, fi.name = split_path(fi.fullpath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,12 @@ package os2
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
|
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:sys/posix"
|
import "core:sys/posix"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
internal_stat :: proc(stat: posix.stat_t, fullpath: string) -> (fi: File_Info) {
|
internal_stat :: proc(stat: posix.stat_t, fullpath: string) -> (fi: File_Info) {
|
||||||
fi.fullpath = fullpath
|
fi.fullpath = fullpath
|
||||||
fi.name = filepath.base(fi.fullpath)
|
_, fi.name = split_path(fi.fullpath)
|
||||||
|
|
||||||
fi.inode = u128(stat.st_ino)
|
fi.inode = u128(stat.st_ino)
|
||||||
fi.size = i64(stat.st_size)
|
fi.size = i64(stat.st_size)
|
||||||
@@ -104,7 +103,7 @@ _lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, er
|
|||||||
// NOTE: This might not be correct when given "/symlink/foo.txt",
|
// NOTE: This might not be correct when given "/symlink/foo.txt",
|
||||||
// you would want that to resolve "/symlink", but not resolve "foo.txt".
|
// you would want that to resolve "/symlink", but not resolve "foo.txt".
|
||||||
|
|
||||||
fullpath := filepath.clean(name, temp_allocator())
|
fullpath := clean_path(name, temp_allocator()) or_return
|
||||||
assert(len(fullpath) > 0)
|
assert(len(fullpath) > 0)
|
||||||
switch {
|
switch {
|
||||||
case fullpath[0] == '/':
|
case fullpath[0] == '/':
|
||||||
|
|||||||
@@ -3,13 +3,12 @@ package os2
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
|
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:sys/wasm/wasi"
|
import "core:sys/wasm/wasi"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
internal_stat :: proc(stat: wasi.filestat_t, fullpath: string) -> (fi: File_Info) {
|
internal_stat :: proc(stat: wasi.filestat_t, fullpath: string) -> (fi: File_Info) {
|
||||||
fi.fullpath = fullpath
|
fi.fullpath = fullpath
|
||||||
fi.name = filepath.base(fi.fullpath)
|
_, fi.name = split_path(fi.fullpath)
|
||||||
|
|
||||||
fi.inode = u128(stat.ino)
|
fi.inode = u128(stat.ino)
|
||||||
fi.size = i64(stat.size)
|
fi.size = i64(stat.size)
|
||||||
|
|||||||
+22
-12
@@ -2,27 +2,27 @@ package tests_core_os_os2
|
|||||||
|
|
||||||
import os "core:os/os2"
|
import os "core:os/os2"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:slice"
|
import "core:slice"
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_read_dir :: proc(t: ^testing.T) {
|
test_read_dir :: proc(t: ^testing.T) {
|
||||||
path := filepath.join({#directory, "../dir"})
|
path, err_join := os.join_path({#directory, "../dir"}, context.allocator)
|
||||||
defer delete(path)
|
defer delete(path)
|
||||||
|
|
||||||
fis, err := os.read_all_directory_by_path(path, context.allocator)
|
fis, err_read := os.read_all_directory_by_path(path, context.allocator)
|
||||||
defer os.file_info_slice_delete(fis, context.allocator)
|
defer os.file_info_slice_delete(fis, context.allocator)
|
||||||
|
|
||||||
slice.sort_by_key(fis, proc(fi: os.File_Info) -> string { return fi.name })
|
slice.sort_by_key(fis, proc(fi: os.File_Info) -> string { return fi.name })
|
||||||
|
|
||||||
if err == .Unsupported {
|
if err_read == .Unsupported {
|
||||||
log.warn("os2 directory functionality is unsupported, skipping test")
|
log.warn("os2 directory functionality is unsupported, skipping test")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
testing.expect_value(t, err, nil)
|
testing.expect_value(t, err_join, nil)
|
||||||
|
testing.expect_value(t, err_read, nil)
|
||||||
testing.expect_value(t, len(fis), 2)
|
testing.expect_value(t, len(fis), 2)
|
||||||
|
|
||||||
testing.expect_value(t, fis[0].name, "b.txt")
|
testing.expect_value(t, fis[0].name, "b.txt")
|
||||||
@@ -34,8 +34,9 @@ test_read_dir :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_walker :: proc(t: ^testing.T) {
|
test_walker :: proc(t: ^testing.T) {
|
||||||
path := filepath.join({#directory, "../dir"})
|
path, err := os.join_path({#directory, "../dir"}, context.allocator)
|
||||||
defer delete(path)
|
defer delete(path)
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
|
||||||
w := os.walker_create(path)
|
w := os.walker_create(path)
|
||||||
defer os.walker_destroy(&w)
|
defer os.walker_destroy(&w)
|
||||||
@@ -45,11 +46,12 @@ test_walker :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_walker_file :: proc(t: ^testing.T) {
|
test_walker_file :: proc(t: ^testing.T) {
|
||||||
path := filepath.join({#directory, "../dir"})
|
path, err_join := os.join_path({#directory, "../dir"}, context.allocator)
|
||||||
defer delete(path)
|
defer delete(path)
|
||||||
|
testing.expect_value(t, err_join, nil)
|
||||||
|
|
||||||
f, err := os.open(path)
|
f, err_open := os.open(path)
|
||||||
testing.expect_value(t, err, nil)
|
testing.expect_value(t, err_open, nil)
|
||||||
defer os.close(f)
|
defer os.close(f)
|
||||||
|
|
||||||
w := os.walker_create(f)
|
w := os.walker_create(f)
|
||||||
@@ -64,10 +66,18 @@ test_walker_internal :: proc(t: ^testing.T, w: ^os.Walker) {
|
|||||||
path: string,
|
path: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
joined_1, err_joined_1 := os.join_path({"dir", "b.txt"}, context.allocator)
|
||||||
|
joined_2, err_joined_2 := os.join_path({"dir", "sub"}, context.allocator)
|
||||||
|
joined_3, err_joined_3 := os.join_path({"dir", "sub", ".gitkeep"}, context.allocator)
|
||||||
|
|
||||||
|
testing.expect_value(t, err_joined_1, nil)
|
||||||
|
testing.expect_value(t, err_joined_2, nil)
|
||||||
|
testing.expect_value(t, err_joined_3, nil)
|
||||||
|
|
||||||
expected := [?]Seen{
|
expected := [?]Seen{
|
||||||
{.Regular, filepath.join({"dir", "b.txt"})},
|
{.Regular, joined_1},
|
||||||
{.Directory, filepath.join({"dir", "sub"})},
|
{.Directory, joined_2},
|
||||||
{.Regular, filepath.join({"dir", "sub", ".gitkeep"})},
|
{.Regular, joined_3},
|
||||||
}
|
}
|
||||||
|
|
||||||
seen: [dynamic]Seen
|
seen: [dynamic]Seen
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package tests_core_os_os2
|
|||||||
|
|
||||||
import os "core:os/os2"
|
import os "core:os/os2"
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:path/filepath"
|
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_clone :: proc(t: ^testing.T) {
|
test_clone :: proc(t: ^testing.T) {
|
||||||
f, err := os.open(filepath.join({#directory, "file.odin"}, context.temp_allocator))
|
joined, err := os.join_path({#directory, "file.odin"}, context.temp_allocator)
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
f: ^os.File
|
||||||
|
f, err = os.open(joined)
|
||||||
testing.expect_value(t, err, nil)
|
testing.expect_value(t, err, nil)
|
||||||
testing.expect(t, f != nil)
|
testing.expect(t, f != nil)
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package tests_core_os_os2
|
|||||||
|
|
||||||
import os "core:os/os2"
|
import os "core:os/os2"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:path/filepath"
|
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
@@ -17,6 +16,7 @@ test_executable :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
testing.expect_value(t, err, nil)
|
testing.expect_value(t, err, nil)
|
||||||
testing.expect(t, len(path) > 0)
|
testing.expect(t, len(path) > 0)
|
||||||
testing.expect(t, filepath.is_abs(path))
|
testing.expect(t, os.is_absolute_path(path))
|
||||||
testing.expectf(t, strings.contains(path, filepath.base(os.args[0])), "expected the executable path to contain the base of os.args[0] which is %q", filepath.base(os.args[0]))
|
_, filename := os.split_path(os.args[0])
|
||||||
|
testing.expectf(t, strings.contains(path, filename), "expected the executable path to contain the base of os.args[0] which is %q", filename)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user