Store clean_path, also add to reader API
This commit is contained in:
@@ -7,14 +7,23 @@ import "core:strings"
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
file_exists :: proc(r: ^Reader, path: string) -> (bool, u32) {
|
file_exists :: proc(
|
||||||
|
r: ^Reader,
|
||||||
|
path: string,
|
||||||
|
allocator := context.allocator,
|
||||||
|
) -> (
|
||||||
|
found: bool,
|
||||||
|
index: u32,
|
||||||
|
error: Error,
|
||||||
|
) {
|
||||||
|
clean_path := os.clean_path(path, allocator) or_return
|
||||||
for i in 0 ..< r.header.number_of_assets {
|
for i in 0 ..< r.header.number_of_assets {
|
||||||
str := string(r.indices[i].relpath[:])
|
str := string(r.indices[i].relpath[:])
|
||||||
relpath := strings.trim_right_null(str)
|
relpath := strings.trim_right_null(str)
|
||||||
if path == relpath do return true, i
|
if clean_path == relpath do return true, i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, 0
|
return false, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returning `[]u8` is owned by the caller.
|
// Returning `[]u8` is owned by the caller.
|
||||||
@@ -26,11 +35,12 @@ read_file :: proc(
|
|||||||
bytes: []u8,
|
bytes: []u8,
|
||||||
err: Error,
|
err: Error,
|
||||||
) {
|
) {
|
||||||
|
clean_path := os.clean_path(path, allocator) or_return
|
||||||
|
|
||||||
// Check filesystem first, so chunks can be overriden
|
// Check filesystem first, so chunks can be overriden
|
||||||
if os.exists(path) {
|
if os.exists(clean_path) {
|
||||||
// Open file
|
// Open file
|
||||||
file := os.open(path, {.Read}) or_return
|
file := os.open(clean_path, {.Read}) or_return
|
||||||
defer os.close(file)
|
defer os.close(file)
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
@@ -42,7 +52,7 @@ read_file :: proc(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check chunk
|
// Check chunk
|
||||||
if found, i := file_exists(r, path); found {
|
if found, i := file_exists(r, clean_path) or_return; found {
|
||||||
output_path := os.name(r.output)
|
output_path := os.name(r.output)
|
||||||
entry := r.indices[i]
|
entry := r.indices[i]
|
||||||
|
|
||||||
@@ -91,6 +101,6 @@ read_file :: proc(
|
|||||||
return bytes, nil
|
return bytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.eprintln("asset: ", path)
|
fmt.eprintln("asset: ", clean_path)
|
||||||
return nil, .Asset_Not_Exist
|
return nil, .Asset_Not_Exist
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ reader_init :: proc(
|
|||||||
r: Reader,
|
r: Reader,
|
||||||
err: Error,
|
err: Error,
|
||||||
) {
|
) {
|
||||||
|
r.output = output
|
||||||
|
|
||||||
output_path := os.name(output)
|
output_path := os.name(output)
|
||||||
|
|
||||||
// Chunk path
|
// Chunk path
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
|
import "base:runtime"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
Error :: union #shared_nil {
|
Error :: union #shared_nil {
|
||||||
|
runtime.Allocator_Error,
|
||||||
os.Error,
|
os.Error,
|
||||||
File_Error,
|
File_Error,
|
||||||
}
|
}
|
||||||
@@ -42,6 +44,8 @@ format_error :: proc(ferr: Error) -> string {
|
|||||||
switch e in ferr {
|
switch e in ferr {
|
||||||
case nil:
|
case nil:
|
||||||
return ""
|
return ""
|
||||||
|
case runtime.Allocator_Error:
|
||||||
|
return fmt.tprintf("allocator: {}", e)
|
||||||
case os.Error:
|
case os.Error:
|
||||||
return fmt.tprintf("os: {}", e)
|
return fmt.tprintf("os: {}", e)
|
||||||
case File_Error:
|
case File_Error:
|
||||||
|
|||||||
@@ -111,20 +111,25 @@ 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, was_allocation := strings.replace(
|
relpath_dirty, was_allocation := strings.replace(
|
||||||
entry.fullpath,
|
entry.fullpath,
|
||||||
working_dir,
|
working_dir,
|
||||||
".",
|
".",
|
||||||
1,
|
1,
|
||||||
result^.allocator,
|
result^.allocator,
|
||||||
)
|
)
|
||||||
if !was_allocation {
|
// Remove unneeded references to the current or parent directory (./).
|
||||||
relpath = strings.clone(relpath, result^.allocator)
|
relpath := os.clean_path(
|
||||||
}
|
relpath_dirty,
|
||||||
|
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