diff --git a/src/chunks/chunks.odin b/src/chunks/chunks.odin index ce20f66..fa3bc50 100644 --- a/src/chunks/chunks.odin +++ b/src/chunks/chunks.odin @@ -2,6 +2,7 @@ package chunks import "base:runtime" import "core:fmt" +import "core:io" import "core:os" import "core:strconv" import "core:strings" @@ -42,6 +43,12 @@ Writer :: struct { asset_table: AssetTable, } +Error :: union #shared_nil { + io.Error, + os.Error, + Read_Error, +} + // ----------------------------------------- writer_init :: proc( diff --git a/src/chunks/read_metadata.odin b/src/chunks/read_metadata.odin index 17100d5..3033df1 100644 --- a/src/chunks/read_metadata.odin +++ b/src/chunks/read_metadata.odin @@ -1 +1,69 @@ package chunks + +import "base:runtime" +import "core:io" +import "core:mem" +import "core:os" + +// ----------------------------------------- + +Reader :: struct {} + +Read_Error :: enum u8 { + None = 0, + File_Not_A_Chunk = 1, + Unimplemented = 127, + Okay = None, +} + +// ----------------------------------------- + +// read header +// file_exists() func +// read_file() -> check chunks -> check fs -> fail + +read_header :: proc( + chunk1: ^os.File, + allocator := context.allocator, +) -> ( + Header, + Error, +) { + return read_header_impl(chunk1, allocator) +} + +read_header_by_path :: proc( + chunk1: string, + allocator := context.allocator, +) -> ( + header: Header, + err: Error, +) { + chunk1_file := os.open(chunk1, {.Read}) or_return + + return read_header_impl(chunk1_file, allocator) +} + +@(private) +read_header_impl :: proc( + chunk1: ^os.File, + allocator: runtime.Allocator, +) -> ( + header: Header, + err: Error, +) { + chunk1_stream := os.to_stream(chunk1) + + header_bytes := mem.ptr_to_bytes(&header) + io.read_at(chunk1_stream, header_bytes, 0) or_return + + if header.magic_string != MAGIC_STRING || header.version != 1 { + return {}, .File_Not_A_Chunk + } + + if header.compression != 0 { + return {}, .Unimplemented + } + + return header, nil +} diff --git a/src/main.odin b/src/main.odin index 84ca2fb..2c637e7 100644 --- a/src/main.odin +++ b/src/main.odin @@ -2,6 +2,7 @@ package main import "core:os" +import "src:chunks" import "src:cli" import "src:file" @@ -20,16 +21,24 @@ main :: proc() { entries := file.list_dir_recursive(opts.input, working_dir) defer file.delete_entries(&entries) - w := file.writer_init(cast(u64)len(entries)) - defer file.writer_destroy(&w) + w := chunks.writer_init(cast(u64)len(entries)) + defer chunks.writer_destroy(&w) - file.write_assets(&w, opts.output, entries[:], opts.size, opts.compression) - - file.write_metadata( + chunks.write_assets( &w, opts.output, entries[:], opts.size, opts.compression, ) + + chunks.write_metadata( + &w, + opts.output, + entries[:], + opts.size, + opts.compression, + ) + + chunks.read_header_by_path("./build/CHUNK0") }