Improve building of relative path

This commit is contained in:
Riyyi
2026-08-17 17:48:30 +02:00
parent ed402102c4
commit f69f817bd5
2 changed files with 26 additions and 25 deletions
+2
View File
@@ -15,6 +15,7 @@ Error :: union #shared_nil {
File_Error :: enum u8 {
None = 0,
Not_A_Directory = 1,
Not_Under_Working_Directory = 2,
Unimplemented = 127,
Okay = None,
}
@@ -22,6 +23,7 @@ File_Error :: enum u8 {
file_error_strings := #sparse[File_Error]string { // enumerated array
.None = "",
.Not_A_Directory = "path is not a directory",
.Not_Under_Working_Directory = "path is not a subdirectory of the working directory",
.Unimplemented = "feature is unimplemented",
}
+15 -16
View File
@@ -3,6 +3,7 @@ package file
import "base:runtime"
import "core:fmt"
import "core:os"
import "core:path/filepath"
import "core:strings"
// -----------------------------------------
@@ -23,9 +24,9 @@ list_dir_recursive :: proc(
entries: [dynamic]File_Entry,
err: Error,
) {
path := os.name(f)
full_path := os.name(f)
result := list_dir_recursive_by_path_impl(
path,
full_path,
working_dir,
allocator,
) or_return
@@ -41,8 +42,9 @@ list_dir_recursive_by_path :: proc(
entries: [dynamic]File_Entry,
err: Error,
) {
full_path := filepath.abs(path) or_return
result := list_dir_recursive_by_path_impl(
path,
full_path,
working_dir,
allocator,
) or_return
@@ -111,25 +113,22 @@ list_dir_queue :: proc(
append(queue, strings.clone(entry.fullpath, queue^.allocator))
case os.File_Type.Regular:
{
relpath_dirty, was_allocation := strings.replace(
entry.fullpath,
rel := entry.fullpath[:]
if !strings.has_prefix(rel, working_dir) ||
!os.is_path_separator(rel[len(working_dir)]) {
fmt.eprintf(
"fullpath not a subdirectory of working dir\n {}\n {}\n",
working_dir,
".",
1,
result^.allocator,
entry.fullpath,
)
// Remove unneeded references to the current or parent directory (./).
relpath := os.clean_path(
relpath_dirty,
result^.allocator,
) or_return
return .Not_Under_Working_Directory
}
rel = rel[len(working_dir) + 1:]
relpath := strings.clone(rel, result^.allocator) or_return
append(
result,
File_Entry{relpath = relpath, size = entry.size},
)
if was_allocation {
delete(relpath_dirty, result^.allocator)
}
}
case: // skip other types
}