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