From 4b790d855fc8edaaef7d72dc98ee02e081f8a59b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 1 Mar 2025 19:29:01 +0000 Subject: [PATCH] query: add command-line switches to control what info is printed --- src/query.c | 156 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 122 insertions(+), 34 deletions(-) diff --git a/src/query.c b/src/query.c index f27c372..95dbc15 100644 --- a/src/query.c +++ b/src/query.c @@ -12,6 +12,19 @@ enum { ARG_CONTAINER, OPT_VERBOSE, + + OPT_INFO_IMAGE, + OPT_INFO_TAGS, + OPT_INFO_EXTENTS, + OPT_INFO_CHUNKS, +}; + +enum info_type { + INFO_NONE = 0, + INFO_IMAGE = 0x01, + INFO_TAGS = 0x02, + INFO_EXTENTS = 0x04, + INFO_CHUNKS = 0x08, }; static void tag_type_string(unsigned long in, char out[5]) @@ -48,36 +61,9 @@ static const char *compression_function_name(unsigned int v) } } -static int query( - const b_command *self, - const b_arglist *opt, - const b_array *args) +static void print_image_info(struct ec3_image_ioctx *image) { - const char *in_path = NULL; - b_arglist_get_string( - opt, - B_COMMAND_INVALID_ID, - ARG_CONTAINER, - 0, - &in_path); - - FILE *fp = fopen(in_path, "rb"); - - struct ec3_image_ioctx *reader = NULL; - enum ec3_status status = ec3_image_ioctx_open( - in_path, - NULL, - EC3_IMAGE_IO_READ, - &reader); - if (status != EC3_SUCCESS) { - fclose(fp); - - b_err("cannot open container '%s'", in_path); - return -1; - } - - const struct ec3_image_info *img_info - = ec3_image_ioctx_get_info(reader); + const struct ec3_image_info *img_info = ec3_image_ioctx_get_info(image); char container_id[32]; ec3_identifier_to_string( @@ -109,9 +95,14 @@ static int query( printf(" %-20s: %u\n", "cluster group count", img_info->img_nr_cluster_groups); +} - printf("\ntags:\n"); - const struct ec3_tag_info *tags = ec3_image_ioctx_get_tag_info(reader); +static void print_tag_info(struct ec3_image_ioctx *image) +{ + const struct ec3_image_info *img_info = ec3_image_ioctx_get_info(image); + + printf("tags:\n"); + const struct ec3_tag_info *tags = ec3_image_ioctx_get_tag_info(image); for (unsigned int i = 0; i < img_info->img_nr_tags; i++) { char tag_type[5]; tag_type_string(tags[i].tag_type, tag_type); @@ -129,10 +120,15 @@ static int query( "size", tags[i].tag_total_length); } +} - printf("\nextents:\n"); +static void print_extent_info(struct ec3_image_ioctx *image) +{ + const struct ec3_image_info *img_info = ec3_image_ioctx_get_info(image); + + printf("extents:\n"); const struct ec3_extent_info *extents - = ec3_image_ioctx_get_extent_info(reader); + = ec3_image_ioctx_get_extent_info(image); for (unsigned int i = 0; i < img_info->img_nr_extents; i++) { char tag_id[32]; ec3_identifier_to_string( @@ -145,13 +141,77 @@ static int query( size_t logical_base = extents[i].ex_logical_cluster; size_t logical_limit = logical_base + extents[i].ex_count - 1; - printf(" (%16s) %lu clusters", tag_id, extents[i].ex_count); + printf(" (%16s) %4lu clusters", tag_id, extents[i].ex_count); printf(" [%05lx-%05lx] -> [%05lx-%05lx]\n", logical_base, logical_limit, physical_base, physical_limit); } +} + +static enum info_type get_selected_info_types(const struct b_arglist *opt) +{ + enum info_type type = INFO_NONE; + + if (b_arglist_get_count(opt, OPT_INFO_IMAGE, B_COMMAND_INVALID_ID) + > 0) { + type |= INFO_IMAGE; + } + + if (b_arglist_get_count(opt, OPT_INFO_TAGS, B_COMMAND_INVALID_ID) > 0) { + type |= INFO_TAGS; + } + + if (b_arglist_get_count(opt, OPT_INFO_EXTENTS, B_COMMAND_INVALID_ID) + > 0) { + type |= INFO_EXTENTS; + } + + return type; +} + +static int query( + const b_command *self, + const b_arglist *opt, + const b_array *args) +{ + const char *in_path = NULL; + b_arglist_get_string( + opt, + B_COMMAND_INVALID_ID, + ARG_CONTAINER, + 0, + &in_path); + + FILE *fp = fopen(in_path, "rb"); + + struct ec3_image_ioctx *reader = NULL; + enum ec3_status status = ec3_image_ioctx_open( + in_path, + NULL, + EC3_IMAGE_IO_READ, + &reader); + if (status != EC3_SUCCESS) { + fclose(fp); + + b_err("cannot open container '%s'", in_path); + return -1; + } + + enum info_type selected_info = get_selected_info_types(opt); + + if (selected_info & INFO_IMAGE) { + print_image_info(reader); + } + + if (selected_info & INFO_TAGS) { + print_tag_info(reader); + } + + if (selected_info & INFO_EXTENTS) { + print_extent_info(reader); + } ec3_image_ioctx_close(reader); fclose(fp); @@ -176,6 +236,34 @@ B_COMMAND(CMD_QUERY, CMD_ROOT) B_ARG_NR_VALUES(1); } + B_COMMAND_OPTION(OPT_INFO_IMAGE) + { + B_OPTION_LONG_NAME("header"); + B_OPTION_SHORT_NAME('H'); + B_OPTION_DESC("print the contents of the image header."); + } + + B_COMMAND_OPTION(OPT_INFO_TAGS) + { + B_OPTION_LONG_NAME("tags"); + B_OPTION_SHORT_NAME('t'); + B_OPTION_DESC("print the contents of the image tag table."); + } + + B_COMMAND_OPTION(OPT_INFO_EXTENTS) + { + B_OPTION_LONG_NAME("extents"); + B_OPTION_SHORT_NAME('x'); + B_OPTION_DESC("print the contents of the image extent table."); + } + + B_COMMAND_OPTION(OPT_INFO_CHUNKS) + { + B_OPTION_LONG_NAME("chunks"); + B_OPTION_SHORT_NAME('c'); + B_OPTION_DESC("print the contents of the image chunk table."); + } + B_COMMAND_USAGE() { B_COMMAND_USAGE_ARG(ARG_CONTAINER);