No global for working dir

This commit is contained in:
Riyyi
2026-08-06 22:11:37 +02:00
parent 8eeeca0d90
commit 70ec4fc9c7
3 changed files with 11 additions and 18 deletions
+2 -12
View File
@@ -5,22 +5,12 @@ import "core:os"
// -----------------------------------------
working_dir: string
// -----------------------------------------
store_working_dir :: proc(allocator := context.allocator) {
get_working_dir :: proc(allocator := context.allocator) -> string {
wd, err := os.get_working_directory(allocator)
if err != nil {
fmt.eprintln("error: get working directory failed:", err)
os.exit(1)
}
working_dir = wd
}
remove_working_dir :: proc(allocator := context.allocator) {
if working_dir != "" {
delete(working_dir, allocator)
working_dir = ""
}
return wd
}
+5 -2
View File
@@ -17,18 +17,20 @@ FileEntry :: struct #all_or_none {
// Returning `[dynamic]DirectoryEntry` and each relpath `string` inside it are owned by the caller.
list_dir_recursive :: proc(
f: ^os.File,
working_dir: string,
allocator := context.allocator,
) -> [dynamic]FileEntry {
path := os.name(f)
return list_dir_recursive_by_path_impl(path, allocator)
return list_dir_recursive_by_path_impl(path, working_dir, allocator)
}
// Returning `[dynamic]DirectoryEntry` and each relpath `string` inside it are owned by the caller.
list_dir_recursive_by_path :: proc(
path: string,
working_dir: string,
allocator := context.allocator,
) -> [dynamic]FileEntry {
return list_dir_recursive_by_path_impl(path, allocator)
return list_dir_recursive_by_path_impl(path, working_dir, allocator)
}
// Frees every owning relpath string then the dynamic array itself.
@@ -42,6 +44,7 @@ delete_entries :: proc(entries: ^[dynamic]FileEntry) {
@(private)
list_dir_recursive_by_path_impl :: proc(
path: string,
working_dir: string,
allocator: runtime.Allocator,
) -> [dynamic]FileEntry {
if !os.is_dir(path) {
+4 -4
View File
@@ -8,16 +8,16 @@ import "src:file"
VERSION :: #config(VERSION, "dev")
main :: proc() {
file.store_working_dir()
defer file.remove_working_dir()
working_dir := file.get_working_dir()
defer delete(working_dir)
opts := cli.parse(file.working_dir)
opts := cli.parse(working_dir)
// fmt.println("verbose:", opts.verbose)
// fmt.println("compression:", opts.compression)
// fmt.println("size:", opts.size)
entries := file.list_dir_recursive(opts.input)
entries := file.list_dir_recursive(opts.input, working_dir)
defer file.delete_entries(&entries)
w := file.writer_init(cast(u64)len(entries))