diff --git a/src/capture.c b/src/capture.c index 295dd96..f7bac0c 100644 --- a/src/capture.c +++ b/src/capture.c @@ -1,12 +1,10 @@ -#if 0 #include "bin.h" #include "chunk-table.h" #include "commands.h" +#include "image.h" #include "misc.h" -#include "pipeline.h" #include "status.h" #include "string-table.h" -#include "write.h" #include #include @@ -33,9 +31,14 @@ enum { OPT_VERBOSE, }; +struct capture_ctx { + struct ec3_image_ioctx *ctx_image; + struct chunk_table ctx_chunks; + struct string_table ctx_strings; +}; + static enum ec3_status capture_directory( - struct ec3_writer *writer, - struct chunk_table *chunk_tab, + struct capture_ctx *ctx, uint64_t id, const char *cpath) { @@ -48,30 +51,33 @@ static enum ec3_status capture_directory( return EC3_ERR_NO_ENTRY; } - struct ec3_tag_writer *volu; - enum ec3_status s2 = ec3_writer_create_tag( - writer, - EC3_TAG_WRITER_BUFFERED, + struct ec3_tag_ioctx *volu; + enum ec3_status s2 = ec3_image_ioctx_create_tag( + ctx->ctx_image, EC3_TAG_VOLU, id, - 0, + EC3_TAG_IO_READ | EC3_TAG_IO_WRITE, &volu); if (s2 != EC3_SUCCESS) { b_directory_release(dir); return s2; } - struct string_table *stab = ec3_writer_get_strings(writer); + if (s2 != EC3_SUCCESS) { + ec3_tag_ioctx_close(volu); + b_directory_release(dir); + return s2; + } b_directory_iterator it; b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST); while (b_directory_iterator_is_valid(&it)) { printf("%s\n", b_path_ptr(it.filepath)); - size_t key = string_table_get(stab, it.filename); + size_t key = string_table_get(&ctx->ctx_strings, it.filename); b_directory_iterator_next(&it); } - ec3_tag_writer_finish(volu); + ec3_tag_ioctx_close(volu); b_directory_release(dir); return EC3_SUCCESS; @@ -85,13 +91,6 @@ static int capture( const char *out_path = NULL; b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path); - FILE *outp = fopen(out_path, "wb"); - if (!outp) { - b_err("cannot open '%s'", out_path); - b_i("reason: %s", strerror(errno)); - return -1; - } - enum ec3_status status = EC3_SUCCESS; uint64_t ident = 0; @@ -106,59 +105,72 @@ static int capture( return -1; } - struct ec3_writer *writer = NULL; + struct ec3_image_ioctx *image = NULL; struct ec3_parameters param = { - .p_outp = outp, .p_cluster_size = EC3_CLUSTER_16K, .p_compression_func = EC3_COMPRESSION_ZSTD, .p_ident = ident, }; - status = ec3_writer_create(¶m, &writer); + status = ec3_image_ioctx_open( + out_path, + ¶m, + EC3_IMAGE_IO_WRITE | EC3_IMAGE_IO_TRUNCATE, + &image); if (status != EC3_SUCCESS) { b_err("cannot initialise EC3 writer"); return -1; } - uint64_t ctab_id, cdat_id; + uint64_t ctab_id, cdat_id, stab_id; ec3_identifier_from_string("_CHKTAB0", &ctab_id); ec3_identifier_from_string("_CHKDAT0", &cdat_id); + ec3_identifier_from_string("_VOLSTR0", &stab_id); - struct ec3_tag_writer *ctab, *cdat; - status = ec3_writer_create_tag( - writer, - EC3_TAG_WRITER_BUFFERED, + struct ec3_tag_ioctx *ctab, *cdat, *stab; + status = ec3_image_ioctx_create_tag( + image, EC3_TAG_CTAB, ctab_id, - 0, + EC3_TAG_IO_READ | EC3_TAG_IO_WRITE, &ctab); if (status != EC3_SUCCESS) { - b_err("cannot initialise EC3 writer"); + b_err("cannot create chunk table tag"); + b_i("error code: %s", ec3_status_to_string(status)); return -1; } - status = ec3_writer_create_tag( - writer, - EC3_TAG_WRITER_BUFFERED, + status = ec3_image_ioctx_create_tag( + image, EC3_TAG_CDAT, cdat_id, - 0, + EC3_TAG_IO_READ | EC3_TAG_IO_WRITE, &cdat); if (status != EC3_SUCCESS) { - b_err("cannot initialise EC3 writer"); + b_err("cannot create chunk data tag"); + b_i("error code: %s", ec3_status_to_string(status)); return -1; } - struct chunk_table chunk_tab; -#if 0 - chunk_table_init_write( - ctab, - cdat, - ec3_get_cluster_size(param.p_cluster_size), - &chunk_tab); -#endif + status = ec3_image_ioctx_create_tag( + image, + EC3_TAG_STAB, + stab_id, + EC3_TAG_IO_WRITE | EC3_TAG_IO_SEQUENTIAL, + &stab); + + if (status != EC3_SUCCESS) { + b_err("cannot create string table tag"); + b_i("error code: %s", ec3_status_to_string(status)); + return -1; + } + + struct capture_ctx ctx = {0}; + ctx.ctx_image = image; + chunk_table_init(ctab, cdat, param.p_cluster_size, &ctx.ctx_chunks); + string_table_init(&ctx.ctx_strings); uint64_t next_auto_id = 0; @@ -168,8 +180,7 @@ static int capture( printf("%s\n", it.value->val_str); status = capture_directory( - writer, - &chunk_tab, + &ctx, next_auto_id, it.value->val_str); next_auto_id++; @@ -177,6 +188,7 @@ static int capture( if (status != EC3_SUCCESS) { b_err("an error occurred while writing to the " "container"); + b_i("error: %s", ec3_status_to_string(status)); return -1; } } @@ -214,11 +226,7 @@ static int capture( return -1; } - status = capture_directory( - writer, - &chunk_tab, - id, - path->val_str); + status = capture_directory(&ctx, id, path->val_str); if (status != EC3_SUCCESS) { b_err("an error occurred while writing to the " @@ -227,11 +235,31 @@ static int capture( } } - ec3_tag_writer_finish(ctab); - ec3_tag_writer_finish(cdat); - ec3_writer_finish(writer); + b_btree_iterator s_it; + b_btree_iterator_begin(&ctx.ctx_strings.s_offset_tree, &s_it); + while (b_btree_iterator_is_valid(&s_it)) { + struct string_table_entry *entry = b_unbox( + struct string_table_entry, + s_it.node, + e_offset_node); - fclose(outp); + size_t len = strlen(entry->e_str) + 1; + size_t nr_written = 0; + status = ec3_tag_ioctx_write( + stab, + entry->e_str, + len, + &nr_written); + b_btree_iterator_next(&s_it); + } + + string_table_finish(&ctx.ctx_strings); + chunk_table_finish(&ctx.ctx_chunks); + + ec3_tag_ioctx_close(stab); + ec3_tag_ioctx_close(ctab); + ec3_tag_ioctx_close(cdat); + ec3_image_ioctx_close(image); return 0; } @@ -344,4 +372,3 @@ B_COMMAND(CMD_CAPTURE, CMD_ROOT) B_COMMAND_USAGE_OPT(OPT_TAGGED_DIRECTORY); } } -#endif