Fix leaks
This commit is contained in:
+5
-7
@@ -3,14 +3,12 @@ package file
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
// TODO:
|
||||
// - list files in directory
|
||||
// - get relative path
|
||||
// - read file contents
|
||||
// - write file contents
|
||||
// -----------------------------------------
|
||||
|
||||
working_dir: string
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
store_working_dir :: proc(allocator := context.allocator) {
|
||||
wd, err := os.get_working_directory(allocator)
|
||||
if err != nil {
|
||||
@@ -20,9 +18,9 @@ store_working_dir :: proc(allocator := context.allocator) {
|
||||
working_dir = wd
|
||||
}
|
||||
|
||||
remove_working_dir :: proc() {
|
||||
remove_working_dir :: proc(allocator := context.allocator) {
|
||||
if working_dir != "" {
|
||||
delete(working_dir)
|
||||
delete(working_dir, allocator)
|
||||
working_dir = ""
|
||||
}
|
||||
}
|
||||
|
||||
+10
-2
@@ -31,6 +31,14 @@ list_dir_recursive_by_path :: proc(
|
||||
return list_dir_recursive_by_path_impl(path, allocator)
|
||||
}
|
||||
|
||||
// Frees every owning relpath string then the dynamic array itself.
|
||||
delete_entries :: proc(entries: ^[dynamic]FileEntry) {
|
||||
for entry in entries {
|
||||
delete(entry.relpath, entries.allocator)
|
||||
}
|
||||
delete(entries^)
|
||||
}
|
||||
|
||||
@(private)
|
||||
list_dir_recursive_by_path_impl :: proc(
|
||||
path: string,
|
||||
@@ -51,7 +59,7 @@ list_dir_recursive_by_path_impl :: proc(
|
||||
list_dir_queue(&queue, &result, working_dir, allocator)
|
||||
}
|
||||
|
||||
return result
|
||||
return result // owning [dynamic]
|
||||
}
|
||||
|
||||
@(private)
|
||||
@@ -78,7 +86,7 @@ list_dir_queue :: proc(
|
||||
fmt.eprintln("error: read directory failed:", read_err)
|
||||
os.exit(1)
|
||||
}
|
||||
defer delete(entries, allocator)
|
||||
defer os.file_info_slice_delete(entries, allocator)
|
||||
|
||||
// Reserve space for new entries, minor waste as directories are also counted.
|
||||
reserve(result, len(result) + len(entries))
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package file
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
Asset :: struct #all_or_none {
|
||||
relpath: string,
|
||||
size: i64,
|
||||
data: []u8,
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
read_by_path :: proc(path: string) -> []u8 {
|
||||
f, open_err := os.open(path, {.Read})
|
||||
if open_err != nil {
|
||||
fmt.eprintln("error: open failed:", open_err)
|
||||
os.exit(1)
|
||||
}
|
||||
defer os.close(f)
|
||||
|
||||
if !os.is_file(path) {
|
||||
fmt.eprintln("error: path is not a file:", path)
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
path := path // shadow parameter
|
||||
path = strings.clone(path)
|
||||
|
||||
size, size_err := os.file_size(f)
|
||||
if size_err != nil {
|
||||
fmt.eprintln("error: get file size failed:", size_err)
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
data := make([]u8, size)
|
||||
|
||||
n, read_err := os.read_full(f, data)
|
||||
if read_err != nil {
|
||||
delete(data)
|
||||
fmt.eprintln(
|
||||
"error: file read failed:",
|
||||
read_err,
|
||||
"got",
|
||||
n,
|
||||
"of",
|
||||
size,
|
||||
"bytes",
|
||||
)
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
get_asset_by_path :: proc(path: string) -> Asset {
|
||||
data := read_by_path(path)
|
||||
return Asset{relpath = path, size = cast(i64)len(data), data = data}
|
||||
}
|
||||
@@ -74,6 +74,7 @@ write_asset_index :: proc(
|
||||
// ----------------------------------------
|
||||
|
||||
index_bytes := make([]u8, index_size, allocator)
|
||||
defer delete(index_bytes)
|
||||
|
||||
for f, i in entries {
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ main :: proc() {
|
||||
// fmt.println("size:", opts.size)
|
||||
|
||||
entries := file.list_dir_recursive(opts.input)
|
||||
defer file.delete_entries(&entries)
|
||||
|
||||
file.compute_metadata_offsets(cast(u64)len(entries))
|
||||
file.write_assets(opts.output, entries[:], opts.size, opts.compression)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package tests
|
||||
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
foo :: proc(_: ^testing.T) {
|
||||
}
|
||||
Reference in New Issue
Block a user