Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using type/extension plugins within printed context #2340

Merged
merged 25 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fbd8228
plugins types REFACTOR shorter ids
Jan 16, 2025
f363067
plugins types UPDATE index/ptr plugin retrieval
Jan 16, 2025
6a54fb7
context UPDATE widen options from 16 to 32-bit
Jan 17, 2025
1f6ac6d
context UPDATE add static plugins only flag
Jan 17, 2025
ab23d01
plugins extension REFACTOR add a dash to ext IDs
Jan 17, 2025
1615a6c
plugins types UPDATE simplify type plugin getter
Jan 20, 2025
dd4f6bc
plugins exts UPDATE simplify ext plugin getter
Jan 20, 2025
4268b7f
context BUGFIX free plugins with printed ctx
Jan 20, 2025
73eddc6
test schema UPDATE finish compiled ctx print test
Jan 20, 2025
c22a49b
plugins REFACTOR add doxygen to static func
Jan 20, 2025
579d38c
plugins UPDATE merge type/ext plugin find funcs
Jan 20, 2025
2911183
plugins BUGFIX move ext plgs outside builtin only
Jan 22, 2025
6178f9a
printer context BUGFIX build addr ht first
Jan 22, 2025
d18e6df
printer context BUGFIX struct ext handling
Jan 22, 2025
701f955
test schema UPDATE compiled print test
Jan 22, 2025
f5b9361
plguins REFACTOR various smaller docs refactoring
Jan 22, 2025
750c5f3
plugins REFACTOR s/plugin_id/plugin_ref
Jan 22, 2025
9732792
plugins REFACTOR remove dash from id
Jan 24, 2025
3d552c6
plugins UPDATE replace plg get fn call with macro
Roytak Jan 24, 2025
17acb3a
context UPDATE clarify static and builtin flags
Roytak Jan 24, 2025
91a9b24
tests UPDATE replace canonical print by func call
Roytak Jan 24, 2025
508c21c
printer lyb BUGFIX move assignment from assert
Roytak Jan 24, 2025
c107fd2
printer context UPDATE clarify addr_ht building
Roytak Jan 24, 2025
9b7711a
plugins BUGFIX use correct array bound
Roytak Jan 24, 2025
417a6bc
plugins REFACTOR various small changes
Roytak Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ ly_ctx_ht_leafref_links_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod)
}

LIBYANG_API_DEF LY_ERR
ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx)
ly_ctx_new(const char *search_dir, uint32_t options, struct ly_ctx **new_ctx)
{
struct ly_ctx *ctx = NULL;
struct lys_module *module;
Expand All @@ -279,7 +279,7 @@ ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx)
struct ly_in *in = NULL;
LY_ERR rc = LY_SUCCESS;
struct lys_glob_unres unres = {0};
ly_bool builtin_plugins_only;
ly_bool builtin_plugins_only, static_plugins_only;

LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);

Expand All @@ -291,7 +291,8 @@ ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx)

/* plugins */
builtin_plugins_only = (options & LY_CTX_BUILTIN_PLUGINS_ONLY) ? 1 : 0;
LY_CHECK_ERR_GOTO(lyplg_init(builtin_plugins_only), LOGINT(NULL); rc = LY_EINT, cleanup);
static_plugins_only = (options & LY_CTX_STATIC_PLUGINS_ONLY) ? 1 : 0;
LY_CHECK_ERR_GOTO(lyplg_init(builtin_plugins_only, static_plugins_only), LOGINT(NULL); rc = LY_EINT, cleanup);

/* ctx data */
ctx_data = ly_ctx_data_add(ctx);
Expand Down Expand Up @@ -601,7 +602,7 @@ ly_ctx_compile(struct ly_ctx *ctx)
return ret;
}

LIBYANG_API_DEF uint16_t
LIBYANG_API_DEF uint32_t
ly_ctx_get_options(const struct ly_ctx *ctx)
{
LY_CHECK_ARG_RET(ctx, ctx, 0);
Expand All @@ -610,7 +611,7 @@ ly_ctx_get_options(const struct ly_ctx *ctx)
}

LIBYANG_API_DEF LY_ERR
ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option)
ly_ctx_set_options(struct ly_ctx *ctx, uint32_t option)
{
LY_ERR lyrc = LY_SUCCESS;
struct ly_ctx_data *ctx_data;
Expand All @@ -628,6 +629,13 @@ ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option)
return LY_EINVAL;
}

if (!(ctx->opts & LY_CTX_STATIC_PLUGINS_ONLY) && (option & LY_CTX_STATIC_PLUGINS_ONLY)) {
LOGERR(ctx, LY_EINVAL,
"Invalid argument %s (LY_CTX_STATIC_PLUGINS_ONLY can be set only when creating a new context) (%s()).",
"option", __func__);
return LY_EINVAL;
}

if (!(ctx->opts & LY_CTX_LEAFREF_LINKING) && (option & LY_CTX_LEAFREF_LINKING)) {
ctx_data = ly_ctx_data_get(ctx);
ctx_data->leafref_links_ht = lyht_new(1, sizeof(struct lyd_leafref_links_rec), ly_ctx_ht_leafref_links_equal_cb, NULL, 1);
Expand Down Expand Up @@ -685,7 +693,7 @@ ly_ctx_ht_leafref_links_rec_free(void *val_p)
}

LIBYANG_API_DEF LY_ERR
ly_ctx_unset_options(struct ly_ctx *ctx, uint16_t option)
ly_ctx_unset_options(struct ly_ctx *ctx, uint32_t option)
{
LY_ARRAY_COUNT_TYPE u, v;
const struct lysc_ext_instance *ext;
Expand Down Expand Up @@ -1392,7 +1400,7 @@ ly_ctx_compiled_print(const struct ly_ctx *ctx, void *mem, void **mem_end)

LY_CHECK_ARG_RET(ctx, ctx, mem, LY_EINVAL);

if (ctx->plugins_types.count || ctx->plugins_extensions.count) {
if (ctx->plugins_types.count || ctx->plugins_extensions.count || !(ctx->opts & LY_CTX_STATIC_PLUGINS_ONLY)) {
LOGERR(ctx, LY_EINVAL, "Printing context with dynamic type or extension plugins is not supported.");
return LY_EINVAL;
}
Expand Down Expand Up @@ -1430,6 +1438,7 @@ ly_ctx_new_printed(const void *mem, struct ly_ctx **ctx)
{
LY_ERR rc = LY_SUCCESS;
struct ly_ctx_data *ctx_data = NULL;
ly_bool builtin_plugins_only, static_plugins_only;

LY_CHECK_ARG_RET(NULL, mem, ctx, LY_EINVAL);

Expand All @@ -1438,6 +1447,11 @@ ly_ctx_new_printed(const void *mem, struct ly_ctx **ctx)
/* ctx data */
ctx_data = ly_ctx_data_add(*ctx);

/* plugins */
builtin_plugins_only = ((*ctx)->opts & LY_CTX_BUILTIN_PLUGINS_ONLY) ? 1 : 0;
static_plugins_only = 1;
LY_CHECK_ERR_GOTO(lyplg_init(builtin_plugins_only, static_plugins_only), LOGINT(NULL); rc = LY_EINT, cleanup);

/* data dictionary */
ctx_data->data_dict = malloc(sizeof *ctx_data->data_dict);
LY_CHECK_ERR_GOTO(!ctx_data->data_dict, rc = LY_EMEM, cleanup);
Expand Down Expand Up @@ -1470,6 +1484,7 @@ ly_ctx_destroy(struct ly_ctx *ctx)
if (ctx->opts & LY_CTX_INT_IMMUTABLE) {
/* ctx data */
ly_ctx_data_del(ctx);
lyplg_clean();
return;
}

Expand Down
25 changes: 16 additions & 9 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,20 @@ struct ly_ctx;
'require-instance false;'. It also enables usage of
[lyd_leafref_get_links](@ref lyd_leafref_get_links) and
[lyd_leafref_link_node_tree](@ref lyd_leafref_link_node_tree) APIs. */
#define LY_CTX_BUILTIN_PLUGINS_ONLY 0x0800 /**< By default, context uses all available plugins for types and extensions,
both included and external. This options prevents all included plugins to be
loaded except for built-in YANG types so all derived types will use these and
for all purposes behave as the base type. The option can be used for cases when
invalid data needs to be stored in YANG node values. */
#define LY_CTX_BUILTIN_PLUGINS_ONLY 0x0800 /**< Limits the context to use only built-in YANG type plugins,
treating all derived types as their base type.
Useful for storing invalid data in YANG node values.
This option has a global effect: the global plugin array is initialized only when no contexts exist.
If any context was created without this flag and is still alive,
creating a new context with this flag will not have the intended effect. */

#define LY_CTX_LYB_HASHES 0x1000 /**< Generate hashes for all the schema nodes. Required when using LYB data parse
or print. */
#define LY_CTX_STATIC_PLUGINS_ONLY 0x2000 /**< Restricts the context to load only static (built-in) plugins,
ignoring external plugins from directories specified by `LIBYANG_TYPES_PLUGINS_DIR` and
`LIBYANG_EXTENSIONS_PLUGINS_DIR`. This option has a global effect: the global plugin array
is initialized only when no contexts exist. If any context was created without this flag
and is still alive, creating a new context with this flag will not have the intended effect. */

/* 0x80000000 reserved for internal use */

Expand Down Expand Up @@ -236,7 +243,7 @@ struct ly_ctx;
* @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned.
* @return LY_ERR return value.
*/
LIBYANG_API_DECL LY_ERR ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx);
LIBYANG_API_DECL LY_ERR ly_ctx_new(const char *search_dir, uint32_t options, struct ly_ctx **new_ctx);

/**
* @brief Create libyang context according to the provided yang-library data in a file.
Expand Down Expand Up @@ -356,7 +363,7 @@ LIBYANG_API_DECL const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *
* @param[in] ctx Context to query.
* @return Combination of all the currently set context's options, see @ref contextoptions.
*/
LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx);
LIBYANG_API_DECL uint32_t ly_ctx_get_options(const struct ly_ctx *ctx);

/**
* @brief Set some of the context's options, see @ref contextoptions.
Expand All @@ -366,15 +373,15 @@ LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx);
* and all ::lysc_node.priv in the modules will be overwritten, see ::LY_CTX_SET_PRIV_PARSED.
* @return LY_ERR value.
*/
LIBYANG_API_DECL LY_ERR ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option);
LIBYANG_API_DECL LY_ERR ly_ctx_set_options(struct ly_ctx *ctx, uint32_t option);

/**
* @brief Unset some of the context's options, see @ref contextoptions.
* @param[in] ctx Context to be modified.
* @param[in] option Combination of the context's options to be unset, see @ref contextoptions.
* @return LY_ERR value.
*/
LIBYANG_API_DECL LY_ERR ly_ctx_unset_options(struct ly_ctx *ctx, uint16_t option);
LIBYANG_API_DECL LY_ERR ly_ctx_unset_options(struct ly_ctx *ctx, uint32_t option);

/**
* @brief Get the change count of the context (module set) during its life-time.
Expand Down
10 changes: 6 additions & 4 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "in_internal.h"
#include "ly_common.h"
#include "plugins_exts.h"
#include "plugins_internal.h"
#include "set.h"
#include "tree_data.h"
#include "tree_data_internal.h"
Expand Down Expand Up @@ -885,7 +886,7 @@ lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance
ly_vlog_build_path_line(PARSER_CTX(pctx), &data_path, &schema_path, &line);

va_start(ap, format);
ly_ext_log(PARSER_CTX(pctx), ext->plugin->id, level, err, data_path, schema_path, line, format, ap);
ly_ext_log(PARSER_CTX(pctx), LYSC_GET_EXT_PLG(ext->plugin_ref)->id, level, err, data_path, schema_path, line, format, ap);
va_end(ap);
}

Expand All @@ -902,7 +903,7 @@ lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instanc
}

va_start(ap, format);
ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, level, err, NULL, schema_path, 0, format, ap);
va_end(ap);
}

Expand All @@ -919,7 +920,7 @@ lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext
}

va_start(ap, format);
ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, level, err, NULL, schema_path, 0, format, ap);
va_end(ap);
}

Expand All @@ -940,7 +941,8 @@ _lyplg_ext_compile_log_err(const struct ly_err_item *eitem, const struct lysc_ex
}

va_start(ap, ext);
ly_ext_log(ext->module->ctx, ext->def->plugin->id, eitem->level, eitem->err, data_path, schema_path, eitem->line, "%s", ap);
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, eitem->level, eitem->err,
data_path, schema_path, eitem->line, "%s", ap);
va_end(ap);
}

Expand Down
5 changes: 3 additions & 2 deletions src/parser_lyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "parser_internal.h"
#include "plugins_exts.h"
#include "plugins_exts/metadata.h"
#include "plugins_internal.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
Expand Down Expand Up @@ -267,9 +268,9 @@ lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uin
if (term->type->basetype == LY_TYPE_LEAFREF) {
/* Leafref itself is ignored, the target is loaded directly. */
type_lf = (struct lysc_type_leafref *)term->type;
lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
lyb_data_len = LYSC_GET_TYPE_PLG(type_lf->realtype->plugin_ref)->lyb_data_len;
} else {
lyb_data_len = term->type->plugin->lyb_data_len;
lyb_data_len = LYSC_GET_TYPE_PLG(term->type->plugin_ref)->lyb_data_len;
}

if (lyb_data_len < 0) {
Expand Down
Loading
Loading