diff --git a/src/file/file.odin b/src/file/file.odin index 076edcd..9c38eae 100644 --- a/src/file/file.odin +++ b/src/file/file.odin @@ -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 } diff --git a/src/file/list_dir.odin b/src/file/list_dir.odin index a6ae847..882c934 100644 --- a/src/file/list_dir.odin +++ b/src/file/list_dir.odin @@ -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) { diff --git a/src/main.odin b/src/main.odin index 0e86cd1..84ca2fb 100644 --- a/src/main.odin +++ b/src/main.odin @@ -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))