From 2a0fd994e5229968fae1eb11a1d5fd60ae52bd34 Mon Sep 17 00:00:00 2001 From: Carl Niklas Rydberg Date: Sat, 20 Dec 2025 11:21:07 +0100 Subject: [PATCH] Share ASL store-id validation --- include/amduat/asl/asl_store_fs_meta.h | 6 ++ src/adapters/asl_store_fs/asl_store_fs_meta.c | 4 +- src/tools/amduat_asl_cli.c | 55 +------------------ 3 files changed, 10 insertions(+), 55 deletions(-) diff --git a/include/amduat/asl/asl_store_fs_meta.h b/include/amduat/asl/asl_store_fs_meta.h index 318854a..bea6b88 100644 --- a/include/amduat/asl/asl_store_fs_meta.h +++ b/include/amduat/asl/asl_store_fs_meta.h @@ -27,6 +27,12 @@ bool amduat_asl_store_fs_init_root( const amduat_asl_store_fs_config_t *cfg_in, amduat_asl_store_fs_config_t *cfg_out); +bool amduat_asl_store_fs_meta_validate_store_id(const char *store_id); + +bool amduat_asl_store_fs_meta_copy_store_id( + char out_id[AMDUAT_ASL_STORE_FS_STORE_ID_MAX + 1], + const char *store_id); + bool amduat_asl_store_fs_load_config( const char *root_path, amduat_asl_store_fs_config_t *out_cfg); diff --git a/src/adapters/asl_store_fs/asl_store_fs_meta.c b/src/adapters/asl_store_fs/asl_store_fs_meta.c index 1f03c05..40cdf33 100644 --- a/src/adapters/asl_store_fs/asl_store_fs_meta.c +++ b/src/adapters/asl_store_fs/asl_store_fs_meta.c @@ -39,7 +39,7 @@ static bool amduat_asl_store_fs_meta_is_store_id_char(char c) { return c == '.' || c == '_' || c == '-'; } -static bool amduat_asl_store_fs_meta_validate_store_id(const char *store_id) { +bool amduat_asl_store_fs_meta_validate_store_id(const char *store_id) { size_t i; if (store_id == NULL || store_id[0] == '\0') { @@ -56,7 +56,7 @@ static bool amduat_asl_store_fs_meta_validate_store_id(const char *store_id) { return true; } -static bool amduat_asl_store_fs_meta_copy_store_id( +bool amduat_asl_store_fs_meta_copy_store_id( char out_id[AMDUAT_ASL_STORE_FS_STORE_ID_MAX + 1], const char *store_id) { size_t len; diff --git a/src/tools/amduat_asl_cli.c b/src/tools/amduat_asl_cli.c index 487c0e7..0ae4dc4 100644 --- a/src/tools/amduat_asl_cli.c +++ b/src/tools/amduat_asl_cli.c @@ -22,8 +22,6 @@ enum { AMDUAT_ASL_CLI_EXIT_CONFIG = 8 }; -enum { AMDUAT_ASL_CLI_STORE_ID_MAX = AMDUAT_ASL_STORE_FS_STORE_ID_MAX }; - static const char *const AMDUAT_ASL_CLI_DEFAULT_ROOT = ".amduat-asl"; typedef enum { @@ -97,56 +95,6 @@ static void amduat_asl_cli_print_usage(FILE *stream) { AMDUAT_ASL_CLI_DEFAULT_ROOT); } -static bool amduat_asl_cli_is_store_id_char(char c) { - if (c >= 'a' && c <= 'z') { - return true; - } - if (c >= 'A' && c <= 'Z') { - return true; - } - if (c >= '0' && c <= '9') { - return true; - } - return c == '.' || c == '_' || c == '-'; -} - -static bool amduat_asl_cli_validate_store_id(const char *store_id) { - size_t i; - - if (store_id == NULL || store_id[0] == '\0') { - return false; - } - for (i = 0; store_id[i] != '\0'; ++i) { - if (i >= AMDUAT_ASL_CLI_STORE_ID_MAX) { - return false; - } - if (!amduat_asl_cli_is_store_id_char(store_id[i])) { - return false; - } - } - return true; -} - -static bool amduat_asl_cli_copy_store_id( - char out_id[AMDUAT_ASL_CLI_STORE_ID_MAX + 1], - const char *store_id) { - size_t len; - - if (out_id == NULL || store_id == NULL) { - return false; - } - if (!amduat_asl_cli_validate_store_id(store_id)) { - return false; - } - len = strlen(store_id); - if (len > AMDUAT_ASL_CLI_STORE_ID_MAX) { - return false; - } - memcpy(out_id, store_id, len); - out_id[len] = '\0'; - return true; -} - static bool amduat_asl_cli_parse_ref_format( const char *text, amduat_asl_cli_ref_format_t *out_fmt) { @@ -290,7 +238,8 @@ static int amduat_asl_cli_cmd_init(int argc, char **argv) { memset(&cfg_in, 0, sizeof(cfg_in)); if (opts.store_id != NULL) { - if (!amduat_asl_cli_copy_store_id(cfg_in.store_id, opts.store_id)) { + if (!amduat_asl_store_fs_meta_copy_store_id(cfg_in.store_id, + opts.store_id)) { fprintf(stderr, "error: invalid store-id\n"); return AMDUAT_ASL_CLI_EXIT_USAGE; }