update capture command to use new image i/o api

This commit is contained in:
2025-02-24 15:56:04 +00:00
parent 765945c9b7
commit ef56b1eff9

View File

@@ -1,12 +1,10 @@
#if 0
#include "bin.h" #include "bin.h"
#include "chunk-table.h" #include "chunk-table.h"
#include "commands.h" #include "commands.h"
#include "image.h"
#include "misc.h" #include "misc.h"
#include "pipeline.h"
#include "status.h" #include "status.h"
#include "string-table.h" #include "string-table.h"
#include "write.h"
#include <blue/cmd.h> #include <blue/cmd.h>
#include <blue/io/directory.h> #include <blue/io/directory.h>
@@ -33,9 +31,14 @@ enum {
OPT_VERBOSE, 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( static enum ec3_status capture_directory(
struct ec3_writer *writer, struct capture_ctx *ctx,
struct chunk_table *chunk_tab,
uint64_t id, uint64_t id,
const char *cpath) const char *cpath)
{ {
@@ -48,30 +51,33 @@ static enum ec3_status capture_directory(
return EC3_ERR_NO_ENTRY; return EC3_ERR_NO_ENTRY;
} }
struct ec3_tag_writer *volu; struct ec3_tag_ioctx *volu;
enum ec3_status s2 = ec3_writer_create_tag( enum ec3_status s2 = ec3_image_ioctx_create_tag(
writer, ctx->ctx_image,
EC3_TAG_WRITER_BUFFERED,
EC3_TAG_VOLU, EC3_TAG_VOLU,
id, id,
0, EC3_TAG_IO_READ | EC3_TAG_IO_WRITE,
&volu); &volu);
if (s2 != EC3_SUCCESS) { if (s2 != EC3_SUCCESS) {
b_directory_release(dir); b_directory_release(dir);
return s2; 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 it;
b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST); b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST);
while (b_directory_iterator_is_valid(&it)) { while (b_directory_iterator_is_valid(&it)) {
printf("%s\n", b_path_ptr(it.filepath)); 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); b_directory_iterator_next(&it);
} }
ec3_tag_writer_finish(volu); ec3_tag_ioctx_close(volu);
b_directory_release(dir); b_directory_release(dir);
return EC3_SUCCESS; return EC3_SUCCESS;
@@ -85,13 +91,6 @@ static int capture(
const char *out_path = NULL; const char *out_path = NULL;
b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path); 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; enum ec3_status status = EC3_SUCCESS;
uint64_t ident = 0; uint64_t ident = 0;
@@ -106,59 +105,72 @@ static int capture(
return -1; return -1;
} }
struct ec3_writer *writer = NULL; struct ec3_image_ioctx *image = NULL;
struct ec3_parameters param = { struct ec3_parameters param = {
.p_outp = outp,
.p_cluster_size = EC3_CLUSTER_16K, .p_cluster_size = EC3_CLUSTER_16K,
.p_compression_func = EC3_COMPRESSION_ZSTD, .p_compression_func = EC3_COMPRESSION_ZSTD,
.p_ident = ident, .p_ident = ident,
}; };
status = ec3_writer_create(&param, &writer); status = ec3_image_ioctx_open(
out_path,
&param,
EC3_IMAGE_IO_WRITE | EC3_IMAGE_IO_TRUNCATE,
&image);
if (status != EC3_SUCCESS) { if (status != EC3_SUCCESS) {
b_err("cannot initialise EC3 writer"); b_err("cannot initialise EC3 writer");
return -1; 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("_CHKTAB0", &ctab_id);
ec3_identifier_from_string("_CHKDAT0", &cdat_id); ec3_identifier_from_string("_CHKDAT0", &cdat_id);
ec3_identifier_from_string("_VOLSTR0", &stab_id);
struct ec3_tag_writer *ctab, *cdat; struct ec3_tag_ioctx *ctab, *cdat, *stab;
status = ec3_writer_create_tag( status = ec3_image_ioctx_create_tag(
writer, image,
EC3_TAG_WRITER_BUFFERED,
EC3_TAG_CTAB, EC3_TAG_CTAB,
ctab_id, ctab_id,
0, EC3_TAG_IO_READ | EC3_TAG_IO_WRITE,
&ctab); &ctab);
if (status != EC3_SUCCESS) { 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; return -1;
} }
status = ec3_writer_create_tag( status = ec3_image_ioctx_create_tag(
writer, image,
EC3_TAG_WRITER_BUFFERED,
EC3_TAG_CDAT, EC3_TAG_CDAT,
cdat_id, cdat_id,
0, EC3_TAG_IO_READ | EC3_TAG_IO_WRITE,
&cdat); &cdat);
if (status != EC3_SUCCESS) { 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; return -1;
} }
struct chunk_table chunk_tab; status = ec3_image_ioctx_create_tag(
#if 0 image,
chunk_table_init_write( EC3_TAG_STAB,
ctab, stab_id,
cdat, EC3_TAG_IO_WRITE | EC3_TAG_IO_SEQUENTIAL,
ec3_get_cluster_size(param.p_cluster_size), &stab);
&chunk_tab);
#endif 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; uint64_t next_auto_id = 0;
@@ -168,8 +180,7 @@ static int capture(
printf("%s\n", it.value->val_str); printf("%s\n", it.value->val_str);
status = capture_directory( status = capture_directory(
writer, &ctx,
&chunk_tab,
next_auto_id, next_auto_id,
it.value->val_str); it.value->val_str);
next_auto_id++; next_auto_id++;
@@ -177,6 +188,7 @@ static int capture(
if (status != EC3_SUCCESS) { if (status != EC3_SUCCESS) {
b_err("an error occurred while writing to the " b_err("an error occurred while writing to the "
"container"); "container");
b_i("error: %s", ec3_status_to_string(status));
return -1; return -1;
} }
} }
@@ -214,11 +226,7 @@ static int capture(
return -1; return -1;
} }
status = capture_directory( status = capture_directory(&ctx, id, path->val_str);
writer,
&chunk_tab,
id,
path->val_str);
if (status != EC3_SUCCESS) { if (status != EC3_SUCCESS) {
b_err("an error occurred while writing to the " b_err("an error occurred while writing to the "
@@ -227,11 +235,31 @@ static int capture(
} }
} }
ec3_tag_writer_finish(ctab); b_btree_iterator s_it;
ec3_tag_writer_finish(cdat); b_btree_iterator_begin(&ctx.ctx_strings.s_offset_tree, &s_it);
ec3_writer_finish(writer); 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; return 0;
} }
@@ -344,4 +372,3 @@ B_COMMAND(CMD_CAPTURE, CMD_ROOT)
B_COMMAND_USAGE_OPT(OPT_TAGGED_DIRECTORY); B_COMMAND_USAGE_OPT(OPT_TAGGED_DIRECTORY);
} }
} }
#endif