Skip to content
Closed

Changes #2638

Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,14 @@ jobs:
${{ matrix.config.pkg-setup }}
${{ matrix.config.pkg-cmd }} ${{ matrix.config.pkg-packages }}
${{ matrix.config.pipx-install }}
echo "$HOME/.local/bin" >> $GITHUB_PATH
# messes up PATH on Rocky so sh is not found in the next step ???
# echo "$HOME/.local/bin" >> "$GITHUB_PATH"
pipx install apkg
$HOME/.local/bin/apkg system-setup

- name: Build-and-install-package
run: |
git clone -b ${{ needs.git-branch.outputs.branch-name }} https://github.com/CESNET/libyang.git
cd libyang
apkg build
apkg install
$HOME/.local/bin/apkg build
$HOME/.local/bin/apkg install
11 changes: 6 additions & 5 deletions src/plugins_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct lysc_type_leafref;
/**
* @brief Type API version
*/
#define LYPLG_TYPE_API_VERSION 4
#define LYPLG_TYPE_API_VERSION 5

/**
* @brief Type of the LYB size of a value of a particular type.
Expand Down Expand Up @@ -343,20 +343,21 @@ LIBYANG_API_DECL ly_bool lyplg_type_bits_is_bit_set(const char *bitmap, uint32_t
* @param[in] token Token to transform.
* @param[in] tok_len Lenghth of @p token.
* @param[in] is_nametest Whether the token is a nametest, it then always requires a prefix in XML @p get_format.
* @param[in,out] context_mod Current context module, may be updated.
* @param[in] resolve_ctx Context to use for resolving prefixes.
* @param[in] resolve_format Format of the resolved prefixes.
* @param[in] resolve_prefix_data Resolved prefixes prefix data.
* @param[in] get_format Format of the output prefixes.
* @param[in] get_prefix_data Format-specific prefix data for the output.
* @param[in,out] ctx_mod Current context module, may be updated.
* @param[in,out] ctx_node Current context node, may be updated.
* @param[out] token_p Printed token.
* @param[out] err Error structure on error.
* @return LY_ERR value.
*/
LIBYANG_API_DECL LY_ERR lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_nametest,
const struct lys_module **context_mod, const struct ly_ctx *resolve_ctx, LY_VALUE_FORMAT resolve_format,
const void *resolve_prefix_data, LY_VALUE_FORMAT get_format, void *get_prefix_data, char **token_p,
struct ly_err_item **err);
const struct ly_ctx *resolve_ctx, LY_VALUE_FORMAT resolve_format, const void *resolve_prefix_data,
LY_VALUE_FORMAT get_format, void *get_prefix_data, const struct lys_module **ctx_mod,
const struct lysc_node **ctx_node, char **token_p, struct ly_err_item **err);

/**
* @brief Get format-specific prefix for a module.
Expand Down
7 changes: 4 additions & 3 deletions src/plugins_types/instanceid_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val
char *str_tok;
const char *cur_exp_ptr;
ly_bool is_nt;
const struct lys_module *context_mod = NULL, *local_mod = NULL;
const struct lys_module *local_mod = NULL, *ctx_mod = NULL;
const struct lysc_node *ctx_node = NULL;
struct ly_set *mods;

*str_value = NULL;
Expand All @@ -84,8 +85,8 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val
if ((cur_tok == LYXP_TOKEN_NAMETEST) || (cur_tok == LYXP_TOKEN_LITERAL)) {
/* tokens that may include prefixes, get them in the target format */
is_nt = (cur_tok == LYXP_TOKEN_NAMETEST) ? 1 : 0;
LY_CHECK_GOTO(ret = lyplg_type_xpath10_print_token(cur_exp_ptr, val->keys->tok_len[cur_idx], is_nt, &context_mod,
val->ctx, val->format, val->prefix_data, format, prefix_data, &str_tok, err), cleanup);
LY_CHECK_GOTO(ret = lyplg_type_xpath10_print_token(cur_exp_ptr, val->keys->tok_len[cur_idx], is_nt, val->ctx,
val->format, val->prefix_data, format, prefix_data, &ctx_mod, &ctx_node, &str_tok, err), cleanup);

/* append the converted token */
ret = ly_append_str(str_value, &str_size, &str_len, "%s", str_tok);
Expand Down
118 changes: 79 additions & 39 deletions src/plugins_types/xpath1.0.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "compat.h"
#include "dict.h"
#include "ly_common.h"
#include "tree_schema_internal.h"
#include "xml.h"
#include "xpath.h"

Expand All @@ -37,50 +38,84 @@
*/

LIBYANG_API_DEF LY_ERR
lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_nametest, const struct lys_module **context_mod,
const struct ly_ctx *resolve_ctx, LY_VALUE_FORMAT resolve_format, const void *resolve_prefix_data,
LY_VALUE_FORMAT get_format, void *get_prefix_data, char **token_p, struct ly_err_item **err)
lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_nametest, const struct ly_ctx *resolve_ctx,
LY_VALUE_FORMAT resolve_format, const void *resolve_prefix_data, LY_VALUE_FORMAT get_format, void *get_prefix_data,
const struct lys_module **ctx_mod, const struct lysc_node **ctx_node, char **token_p, struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
const char *str_begin, *str_next, *prefix;
ly_bool is_prefix, has_prefix = 0;
LY_ERR rc = LY_SUCCESS, r;
const char *str_begin, *str_next, *str_next2, *prefix;
ly_bool is_prefix, is_prefix2, has_prefix = 0;
char *str = NULL;
uint32_t len, str_size = 0, str_len = 0, pref_len;
uint32_t len, len2, str_size = 0, str_len = 0, pref_len;
const struct lysc_node *node = NULL;
const struct lys_module *mod;

*token_p = NULL;

if (*ctx_mod) {
/* use context module context in case it is from an extension */
resolve_ctx = (*ctx_mod)->ctx;
}
str_begin = token;

while (!(ret = ly_value_prefix_next(str_begin, token + tok_len, &len, &is_prefix, &str_next)) && len) {
while (!(r = ly_value_prefix_next(str_begin, token + tok_len, &len, &is_prefix, &str_next)) && len) {
if (!is_prefix) {
if (!has_prefix && is_nametest && (get_format == LY_VALUE_XML) && *context_mod) {
if (!has_prefix && is_nametest && (get_format == LY_VALUE_XML) && *ctx_mod) {
/* get the prefix */
prefix = lyplg_type_get_prefix(*context_mod, get_format, get_prefix_data);
prefix = lyplg_type_get_prefix(*ctx_mod, get_format, get_prefix_data);
assert(prefix);

/* append the nametest and prefix */
LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%s:%.*s", prefix, (int)len, str_begin),
ret = ly_err_new_no_mem(err), cleanup);
rc = ly_err_new_no_mem(err), cleanup);
} else {
/* just append the string, we may get the first expression node without a prefix but since this
* is not strictly forbidden, allow it */
LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%.*s", (int)len, str_begin),
ret = ly_err_new_no_mem(err), cleanup);
rc = ly_err_new_no_mem(err), cleanup);
}
} else {
/* remember there was a prefix found */
has_prefix = 1;

/* resolve the module in the original format */
mod = lys_find_module(resolve_ctx, NULL, str_begin, len, resolve_format, resolve_prefix_data);
if (!mod && is_nametest) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to resolve prefix \"%.*s\".",
(int)len, str_begin);
goto cleanup;
if (is_nametest) {
/* parse the name as well */
r = ly_value_prefix_next(str_next, token + tok_len, &len2, &is_prefix2, &str_next2);
if (r) {
/* invalid characters in the xpath */
break;
} else if (!len2) {
rc = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Unexpected token end (\"%.*s\").",
(int)((token + tok_len) - str_begin), str_begin);
goto cleanup;
} else if (is_prefix2) {
rc = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Unexpected prefix found (\"%.*s\").",
(int)((token + tok_len) - str_next), str_next);
goto cleanup;
}

/* find the node */
r = lys_find_child_node(resolve_ctx, *ctx_node, *ctx_mod, str_begin, len, resolve_format,
(void *)resolve_prefix_data, str_next, len2, 0, &node, NULL);
if (r) {
/* we were not able to find the node but this function does not strictly require it, only the
* module must be found */
mod = lys_find_module(resolve_ctx, NULL, str_begin, len, resolve_format, resolve_prefix_data);
if (!mod) {
rc = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to resolve prefix \"%.*s\".",
(int)len, str_begin);
goto cleanup;
}
} else {
/* set the current module */
mod = node->module;
}
} else {
/* resolve the module in the original format */
mod = lys_find_module(resolve_ctx, NULL, str_begin, len, resolve_format, resolve_prefix_data);
}

if (is_nametest && ((get_format == LY_VALUE_JSON) || (get_format == LY_VALUE_LYB)) && (*context_mod == mod)) {
if (is_nametest && ((get_format == LY_VALUE_JSON) || (get_format == LY_VALUE_LYB)) && (*ctx_mod == mod)) {
/* inherit the prefix and do not print it again */
} else {
if (mod) {
Expand All @@ -96,39 +131,41 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n

/* append the prefix */
LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%.*s:", (int)pref_len, prefix),
ret = ly_err_new_no_mem(err), cleanup);
rc = ly_err_new_no_mem(err), cleanup);
}

if (is_nametest) {
/* update context module */
*context_mod = mod;
/* update context module and node */
*ctx_mod = mod;
*ctx_node = node;
}
}

str_begin = str_next;
}
if (ret) {
if (r) {
/* invalid characters in the xpath */
ly_err_new(err, ret, LYVE_DATA, NULL, NULL, "Invalid characters found (\"%.*s\").",
rc = ly_err_new(err, r, LYVE_DATA, NULL, NULL, "Invalid characters found (\"%.*s\").",
(int)((token + tok_len) - str_begin), str_begin);
}

cleanup:
if (ret) {
if (rc) {
free(str);
} else {
*token_p = str;
}
return ret;
return rc;
}

/**
* @brief Print xpath1.0 subexpression in the specific format.
*
* @param[in,out] cur_idx Current index of the next token in the expression.
* @param[in] end_tok End token (including) that finishes this subexpression parsing. If 0, parse until the end.
* @param[in] context_mod Current context module, some formats (::LY_VALUE_JSON and ::LY_VALUE_LYB) inherit this module
* instead of printing it again.
* @param[in] ctx_mod Current context module, some formats (::LY_VALUE_JSON and ::LY_VALUE_LYB) inherit it instead of
* printing it again.
* @param[in] ctx_node Current context node.
* @param[in] xp_val xpath1.0 value structure.
* @param[in] format Format to print in.
* @param[in] prefix_data Format-specific prefix data.
Expand All @@ -139,16 +176,17 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n
* @return LY_ERR value.
*/
static LY_ERR
xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct lys_module *context_mod,
const struct lyd_value_xpath10 *xp_val, LY_VALUE_FORMAT format, void *prefix_data, char **str_value,
uint32_t *str_size, uint32_t *str_len, struct ly_err_item **err)
xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct lys_module *ctx_mod,
const struct lysc_node *ctx_node, const struct lyd_value_xpath10 *xp_val, LY_VALUE_FORMAT format,
void *prefix_data, char **str_value, uint32_t *str_size, uint32_t *str_len, struct ly_err_item **err)
{
LY_ERR r;
enum lyxp_token cur_tok, sub_end_tok;
char *str_tok;
const char *cur_exp_ptr;
ly_bool is_nt;
const struct lys_module *orig_context_mod = context_mod;
const struct lys_module *orig_ctx_mod = ctx_mod;
const struct lysc_node *orig_ctx_node = ctx_node;

while (*cur_idx < xp_val->exp->used) {
cur_tok = xp_val->exp->tokens[*cur_idx];
Expand All @@ -157,8 +195,8 @@ xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct
if ((cur_tok == LYXP_TOKEN_NAMETEST) || (cur_tok == LYXP_TOKEN_LITERAL)) {
/* tokens that may include prefixes, get them in the target format */
is_nt = (cur_tok == LYXP_TOKEN_NAMETEST) ? 1 : 0;
LY_CHECK_RET(lyplg_type_xpath10_print_token(cur_exp_ptr, xp_val->exp->tok_len[*cur_idx], is_nt, &context_mod,
xp_val->ctx, xp_val->format, xp_val->prefix_data, format, prefix_data, &str_tok, err));
LY_CHECK_RET(lyplg_type_xpath10_print_token(cur_exp_ptr, xp_val->exp->tok_len[*cur_idx], is_nt, xp_val->ctx,
xp_val->format, xp_val->prefix_data, format, prefix_data, &ctx_mod, &ctx_node, &str_tok, err));

/* append the converted token */
r = ly_append_str(str_value, str_size, str_len, "%s", str_tok);
Expand All @@ -173,8 +211,9 @@ xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct
LY_CHECK_GOTO(ly_append_str(str_value, str_size, str_len, " %.*s ", (int)xp_val->exp->tok_len[*cur_idx],
cur_exp_ptr), error_mem);

/* reset context mod */
context_mod = orig_context_mod;
/* reset context module and node */
ctx_mod = orig_ctx_mod;
ctx_node = orig_ctx_node;
} else {
/* just copy the token */
LY_CHECK_GOTO(ly_append_str(str_value, str_size, str_len, "%.*s", (int)xp_val->exp->tok_len[*cur_idx],
Expand All @@ -191,8 +230,8 @@ xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct
sub_end_tok = (cur_tok == LYXP_TOKEN_BRACK1) ? LYXP_TOKEN_BRACK2 : LYXP_TOKEN_PAR2;

/* parse the subexpression separately, use the current context mod */
LY_CHECK_RET(xpath10_print_subexpr_r(cur_idx, sub_end_tok, context_mod, xp_val, format, prefix_data,
str_value, str_size, str_len, err));
LY_CHECK_RET(xpath10_print_subexpr_r(cur_idx, sub_end_tok, ctx_mod, ctx_node, xp_val, format,
prefix_data, str_value, str_size, str_len, err));
}
}
}
Expand Down Expand Up @@ -238,7 +277,8 @@ lyplg_type_print_xpath10_value(const struct lyd_value_xpath10 *xp_val, LY_VALUE_
}

/* recursively print the expression */
ret = xpath10_print_subexpr_r(&expr_idx, 0, NULL, xp_val, format, prefix_data, str_value, &str_size, &str_len, err);
ret = xpath10_print_subexpr_r(&expr_idx, 0, NULL, NULL, xp_val, format, prefix_data, str_value, &str_size, &str_len,
err);

if (local_mod) {
mods->objs[0] = (void *)local_mod;
Expand Down
2 changes: 1 addition & 1 deletion src/tree_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ lys_find_child_node(const struct ly_ctx *ctx, const struct lysc_node *parent, co
}

/* find the module */
if (!mod) {
if (prefix || !mod) {
mod = lys_find_module(ctx, parent, prefix, prefix_len, format, prefix_data);
}

Expand Down
33 changes: 11 additions & 22 deletions tests/utests/basic/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,31 +310,21 @@ test_parse_instance_predicate(void **state)
* @param token The input token to translate.
* @param tok_len The length of the input token.
* @param is_nametest Whether the token is a name test.
* @param mod The YANG module.
* @param mod Module to use.
* @param expected_ret The expected return value.
* @param expected_out The expected output token.
*/
static void
assert_token_translation(const char *token, uint16_t tok_len, ly_bool is_nametest,
struct lys_module *mod, LY_ERR expected_ret)
assert_token_translation(const char *token, uint16_t tok_len, ly_bool is_nametest, const struct lys_module *mod,
LY_ERR expected_ret)
{
LY_ERR r;
char *out_token = NULL;
const struct lys_module *context_mod = mod;

LY_ERR ret = lyplg_type_xpath10_print_token(
token,
tok_len,
is_nametest,
&context_mod,
mod->ctx,
LY_VALUE_SCHEMA,
mod->parsed,
LY_VALUE_JSON,
NULL,
&out_token,
NULL);

assert_int_equal(expected_ret, ret);
const struct lysc_node *ctx_node = NULL;

r = lyplg_type_xpath10_print_token(token, tok_len, is_nametest, mod->ctx, LY_VALUE_SCHEMA, mod->parsed,
LY_VALUE_JSON, NULL, &mod, &ctx_node, &out_token, NULL);
assert_int_equal(expected_ret, r);
free(out_token);
}

Expand All @@ -347,6 +337,8 @@ test_value_prefix_next(void **state)
"module test {"
" namespace \"urn:test\";"
" prefix pref;"
""
" leaf node {type string;}"
"}";

assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, yang, LYS_IN_YANG, &mod));
Expand All @@ -357,9 +349,6 @@ test_value_prefix_next(void **state)
/* Valid prefix with node (Prefix translation) */
assert_token_translation("pref:node", 9, 1, mod, LY_SUCCESS);

/* Valid prefix only (Nametest ending in colon) */
assert_token_translation("pref:", 5, 1, mod, LY_SUCCESS);

/* No prefix (Standard node) */
assert_token_translation("node", 4, 1, mod, LY_SUCCESS);

Expand Down
Loading