Bläddra i källkod

Decouple usage of `filepath` from `os2`

Feoramund 5 månader sedan
förälder
incheckning
4e7f54c565

+ 1 - 2
core/os/os2/path.odin

@@ -3,7 +3,6 @@ package os2
 import "base:intrinsics"
 import "base:runtime"
 
-import "core:path/filepath"
 import "core:strings"
 
 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) {
 	path = _get_executable_path(allocator) or_return
-	path, _ = filepath.split(path)
+	path, _ = split_path(path)
 	return
 }
 

+ 2 - 3
core/os/os2/path_posix.odin

@@ -3,7 +3,6 @@
 package os2
 
 import "base:runtime"
-import "core:path/filepath"
 
 import "core:sys/posix"
 
@@ -35,11 +34,11 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
 		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)
 
 	internal_mkdir_all :: proc(path: string, perm: int) -> Error {
-		dir, file := filepath.split(path)
+		dir, file := split_path(path)
 		if file != path && dir != "/" {
 			if len(dir) > 1 && dir[len(dir) - 1] == '/' {
 				dir = dir[:len(dir) - 1]

+ 2 - 3
core/os/os2/path_wasi.odin

@@ -3,7 +3,6 @@ package os2
 
 import "base:runtime"
 
-import "core:path/filepath"
 import "core:sync"
 import "core:sys/wasm/wasi"
 
@@ -35,11 +34,11 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
 		return .Exist
 	}
 
-	clean_path := filepath.clean(path, temp_allocator())
+	clean_path := clean_path(path, temp_allocator())
 	return internal_mkdir_all(clean_path)
 
 	internal_mkdir_all :: proc(path: string) -> Error {
-		dir, file := filepath.split(path)
+		dir, file := split_path(path)
 		if file != path && dir != "/" {
 			if len(dir) > 1 && dir[len(dir) - 1] == '/' {
 				dir = dir[:len(dir) - 1]

+ 2 - 3
core/os/os2/process_linux.odin

@@ -10,7 +10,6 @@ import "core:slice"
 import "core:strings"
 import "core:strconv"
 import "core:sys/linux"
-import "core:path/filepath"
 
 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.fields += {.Executable_Path}
 			} 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}
 			} else {
 				break cmdline_if
@@ -407,7 +406,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 	executable_name := desc.command[0]
 	if strings.index_byte(executable_name, '/') < 0 {
 		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
 

+ 1 - 2
core/os/os2/process_posix.odin

@@ -6,7 +6,6 @@ import "base:runtime"
 
 import "core:time"
 import "core:strings"
-import "core:path/filepath"
 
 import kq "core:sys/kqueue"
 import    "core:sys/posix"
@@ -62,7 +61,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 	exe_name    := desc.command[0]
 	if strings.index_byte(exe_name, '/') < 0 {
 		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
 		for dir in path_dirs {

+ 1 - 2
core/os/os2/stat.odin

@@ -1,7 +1,6 @@
 package os2
 
 import "base:runtime"
-import "core:path/filepath"
 import "core:strings"
 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) {
 	cloned = fi
 	cloned.fullpath = strings.clone(fi.fullpath, allocator) or_return
-	cloned.name = filepath.base(cloned.fullpath)
+	_, cloned.name = split_path(cloned.fullpath)
 	return
 }
 

+ 1 - 2
core/os/os2/stat_linux.odin

@@ -4,7 +4,6 @@ package os2
 import "core:time"
 import "base:runtime"
 import "core:sys/linux"
-import "core:path/filepath"
 
 _fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
 	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
 	}
 	fi.creation_time = fi.modification_time
-	fi.name = filepath.base(fi.fullpath)
+	_, fi.name = split_path(fi.fullpath)
 	return
 }
 

+ 2 - 3
core/os/os2/stat_posix.odin

@@ -4,13 +4,12 @@ package os2
 
 import "base:runtime"
 
-import "core:path/filepath"
 import "core:sys/posix"
 import "core:time"
 
 internal_stat :: proc(stat: posix.stat_t, fullpath: string) -> (fi: File_Info) {
 	fi.fullpath = fullpath
-	fi.name = filepath.base(fi.fullpath)
+	_, fi.name = split_path(fi.fullpath)
 
 	fi.inode = u128(stat.st_ino)
 	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",
 	// 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)
 	switch {
 	case fullpath[0] == '/':

+ 1 - 2
core/os/os2/stat_wasi.odin

@@ -3,13 +3,12 @@ package os2
 
 import "base:runtime"
 
-import "core:path/filepath"
 import "core:sys/wasm/wasi"
 import "core:time"
 
 internal_stat :: proc(stat: wasi.filestat_t, fullpath: string) -> (fi: File_Info) {
 	fi.fullpath = fullpath
-	fi.name = filepath.base(fi.fullpath)
+	_, fi.name = split_path(fi.fullpath)
 
 	fi.inode = u128(stat.ino)
 	fi.size  = i64(stat.size)

+ 22 - 12
tests/core/os/os2/dir.odin

@@ -2,27 +2,27 @@ package tests_core_os_os2
 
 import os "core:os/os2"
 import    "core:log"
-import    "core:path/filepath"
 import    "core:slice"
 import    "core:testing"
 import    "core:strings"
 
 @(test)
 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)
 
-	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)
 
 	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")
 		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, fis[0].name, "b.txt")
@@ -34,8 +34,9 @@ test_read_dir :: proc(t: ^testing.T) {
 
 @(test)
 test_walker :: proc(t: ^testing.T) {
-	path := filepath.join({#directory, "../dir"})
+	path, err := os.join_path({#directory, "../dir"}, context.allocator)
 	defer delete(path)
+	testing.expect_value(t, err, nil)
 
 	w := os.walker_create(path)
 	defer os.walker_destroy(&w)
@@ -45,11 +46,12 @@ test_walker :: proc(t: ^testing.T) {
 
 @(test)
 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)
+	testing.expect_value(t, err_join, nil)
 
-	f, err := os.open(path)
-	testing.expect_value(t, err, nil)
+	f, err_open := os.open(path)
+	testing.expect_value(t, err_open, nil)
 	defer os.close(f)
 
 	w := os.walker_create(f)
@@ -64,10 +66,18 @@ test_walker_internal :: proc(t: ^testing.T, w: ^os.Walker) {
 		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{
-		{.Regular,   filepath.join({"dir", "b.txt"})},
-		{.Directory, filepath.join({"dir", "sub"})},
-		{.Regular,   filepath.join({"dir", "sub", ".gitkeep"})},
+		{.Regular,   joined_1},
+		{.Directory, joined_2},
+		{.Regular,   joined_3},
 	}
 
 	seen: [dynamic]Seen

+ 4 - 2
tests/core/os/os2/file.odin

@@ -2,11 +2,13 @@ package tests_core_os_os2
 
 import os "core:os/os2"
 import    "core:testing"
-import    "core:path/filepath"
 
 @(test)
 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(t, f != nil)
 

+ 3 - 3
tests/core/os/os2/path.odin

@@ -2,7 +2,6 @@ package tests_core_os_os2
 
 import os "core:os/os2"
 import    "core:log"
-import    "core:path/filepath"
 import    "core:testing"
 import    "core:strings"
 
@@ -17,6 +16,7 @@ test_executable :: proc(t: ^testing.T) {
 
 	testing.expect_value(t, err, nil)
 	testing.expect(t, len(path) > 0)
-	testing.expect(t, filepath.is_abs(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]))
+	testing.expect(t, os.is_absolute_path(path))
+	_, 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)
 }