From 962a5dae31fec57247b2a3209d7c8d36f19eec8c Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 27 Feb 2025 15:53:39 +0000 Subject: [PATCH] capture: write file data to CDAT/CTAB tags --- src/capture.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/src/capture.c b/src/capture.c index f7bac0c..5f2d5ed 100644 --- a/src/capture.c +++ b/src/capture.c @@ -11,6 +11,7 @@ #include #include #include +#include #define BUFFER_SIZE 65536 @@ -37,6 +38,73 @@ struct capture_ctx { struct string_table ctx_strings; }; +static enum ec3_status capture_file( + struct capture_ctx *ctx, + struct ec3_tag_ioctx *vol, + b_directory *dir, + const char *filename, + const b_path *filepath) +{ + const struct ec3_image_info *image_info + = ec3_image_ioctx_get_info(ctx->ctx_image); + size_t key = string_table_get(&ctx->ctx_strings, filename); + size_t buf_len = image_info->img_cluster_size; + char *buf = malloc(buf_len); + + if (!buf) { + return EC3_ERR_NO_MEMORY; + } + + b_file *src = NULL; + b_status status = b_file_open( + dir, + filepath, + B_FILE_READ_ONLY | B_FILE_BINARY, + &src); + if (!B_OK(status)) { + free(buf); + return ec3_status_from_b_status(status, EC3_ERR_NO_ENTRY); + } + + enum ec3_status s2 = EC3_SUCCESS; + + while (1) { + size_t nr_read = 0; + status = b_file_read( + src, + B_OFFSET_CURRENT, + buf_len, + buf, + &nr_read); + + if (!B_OK(status)) { + s2 = ec3_status_from_b_status( + status, + EC3_ERR_IO_FAILURE); + break; + } + + ec3_chunk_id chunk; + enum ec3_status s2 = chunk_table_put( + &ctx->ctx_chunks, + buf, + nr_read, + chunk); + + if (s2 != EC3_SUCCESS) { + break; + } + + if (nr_read < buf_len) { + break; + } + } + + free(buf); + b_file_release(src); + return s2; +} + static enum ec3_status capture_directory( struct capture_ctx *ctx, uint64_t id, @@ -72,8 +140,27 @@ static enum ec3_status capture_directory( b_directory_iterator it; b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST); while (b_directory_iterator_is_valid(&it)) { + if (!b_directory_path_is_file(dir, it.filepath)) { + b_directory_iterator_next(&it); + continue; + } + printf("%s\n", b_path_ptr(it.filepath)); - size_t key = string_table_get(&ctx->ctx_strings, it.filename); + + enum ec3_status status2 = capture_file( + ctx, + volu, + dir, + it.filename, + it.filepath); + + if (status2 != EC3_SUCCESS) { + b_err("failed to capture file '%s'", + b_path_ptr(it.filepath)); + b_i("error code: %s", ec3_status_to_string(status2)); + break; + } + b_directory_iterator_next(&it); } @@ -88,6 +175,10 @@ static int capture( const b_arglist *opt, const b_array *args) { + printf("sizeof(struct ec3_vnode_group) = %zu\n", + sizeof(struct ec3_vnode_group)); + return 0; + const char *out_path = NULL; b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path); @@ -170,6 +261,7 @@ static int capture( struct capture_ctx ctx = {0}; ctx.ctx_image = image; chunk_table_init(ctab, cdat, param.p_cluster_size, &ctx.ctx_chunks); + chunk_table_init_empty_table(&ctx.ctx_chunks); string_table_init(&ctx.ctx_strings); uint64_t next_auto_id = 0;