blob: c3dc23bd6da51519e967db74b8b147c3e862f568 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions for data trees
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko43297a02021-05-19 11:12:37 +020014
15#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcie7b95092019-05-15 11:03:07 +020016
17#include <assert.h>
Michal Vasko43297a02021-05-19 11:12:37 +020018#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020021#include <string.h>
Michal Vasko43297a02021-05-19 11:12:37 +020022#include <time.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010025#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "dict.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020028#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "log.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
Michal Vaskoa820c312021-02-05 16:33:00 +010032#include "printer_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020033#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038#include "tree_schema.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020039#include "tree_schema_internal.h"
Radek Krejci4f2e3e52021-03-30 14:20:28 +020040#include "validation.h"
Radek Krejci77114102021-03-10 15:21:57 +010041#include "xml.h"
aPiecekdf23eee2021-10-07 12:21:50 +020042#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020043
Michal Vaskod7c048c2021-05-18 16:12:55 +020044/**
45 * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
46 *
47 * @param[in] first_inst Instance of the cache entry.
48 * @param[in,out] dup_inst_cache Duplicate instance cache.
49 * @return Instance cache entry.
50 */
51static struct lyd_dup_inst *
52lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache)
53{
54 struct lyd_dup_inst *item;
55 LY_ARRAY_COUNT_TYPE u;
56
57 LY_ARRAY_FOR(*dup_inst_cache, u) {
58 if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) {
59 return &(*dup_inst_cache)[u];
60 }
61 }
62
63 /* it was not added yet, add it now */
64 LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL);
65
66 return item;
67}
68
69LY_ERR
70lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache)
71{
72 struct lyd_dup_inst *dup_inst;
73
74 if (!*inst || !lysc_is_dup_inst_list((*inst)->schema)) {
75 /* no match or not dup-inst list, inst is unchanged */
76 return LY_SUCCESS;
77 }
78
79 /* there can be more exact same instances and we must make sure we do not match a single node more times */
80 dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache);
81 LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM);
82
83 if (!dup_inst->used) {
84 /* we did not cache these instances yet, do so */
85 lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set);
86 assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst));
87 }
88
89 if (dup_inst->used == dup_inst->inst_set->count) {
90 /* we have used all the instances */
91 *inst = NULL;
92 } else {
93 assert(dup_inst->used < dup_inst->inst_set->count);
94
95 /* use another instance */
96 *inst = dup_inst->inst_set->dnodes[dup_inst->used];
97 ++dup_inst->used;
98 }
99
100 return LY_SUCCESS;
101}
102
103void
104lyd_dup_inst_free(struct lyd_dup_inst *dup_inst)
105{
106 LY_ARRAY_COUNT_TYPE u;
107
108 LY_ARRAY_FOR(dup_inst, u) {
109 ly_set_free(dup_inst[u].inst_set, NULL);
110 }
111 LY_ARRAY_FREE(dup_inst);
112}
113
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200114struct lyd_node *
115lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +0200116 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200117{
118 const struct lysc_node *siter = NULL;
119 struct lyd_node *match = NULL;
120
121 assert(parent || module);
122 assert(!last || (slast && *slast));
123
124 if (slast) {
125 siter = *slast;
126 }
127
128 if (last && last->next && (last->next->schema == siter)) {
129 /* return next data instance */
130 return last->next;
131 }
132
133 /* find next schema node data instance */
134 while ((siter = lys_getnext(siter, parent, module, 0))) {
135 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
136 break;
137 }
138 }
139
140 if (slast) {
141 *slast = siter;
142 }
143 return match;
144}
145
Radek Krejcie7b95092019-05-15 11:03:07 +0200146struct lyd_node **
Michal Vaskoe0665742021-02-11 11:08:44 +0100147lyd_node_child_p(struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200148{
149 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +0100150
151 if (!node->schema) {
152 return &((struct lyd_node_opaq *)node)->child;
153 } else {
154 switch (node->schema->nodetype) {
155 case LYS_CONTAINER:
156 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +0100157 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100158 case LYS_ACTION:
159 case LYS_NOTIF:
160 return &((struct lyd_node_inner *)node)->child;
161 default:
162 return NULL;
163 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200164 }
165}
166
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100167LIBYANG_API_DEF LY_ERR
aPiecekdf23eee2021-10-07 12:21:50 +0200168lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value)
169{
170 LY_ERR ret = LY_SUCCESS;
171 char *var_name = NULL, *var_value = NULL;
172 struct lyxp_var *item;
173
174 if (!vars || !name || !value) {
175 return LY_EINVAL;
176 }
177
178 /* If variable is already defined then change its value. */
179 if (*vars && !lyxp_vars_find(*vars, name, 0, &item)) {
180 var_value = strdup(value);
181 LY_CHECK_RET(!var_value, LY_EMEM);
182
183 /* Set new value. */
184 free(item->value);
185 item->value = var_value;
186 } else {
187 var_name = strdup(name);
188 var_value = strdup(value);
189 LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error);
190
191 /* Add new variable. */
192 LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error);
193 item->name = var_name;
194 item->value = var_value;
195 }
196
197 return LY_SUCCESS;
198
199error:
200 free(var_name);
201 free(var_value);
202 return ret;
203}
204
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100205LIBYANG_API_DEF void
aPiecekdf23eee2021-10-07 12:21:50 +0200206lyxp_vars_free(struct lyxp_var *vars)
207{
208 LY_ARRAY_COUNT_TYPE u;
209
210 if (!vars) {
211 return;
212 }
213
214 LY_ARRAY_FOR(vars, u) {
215 free(vars[u].name);
216 free(vars[u].value);
217 }
218
219 LY_ARRAY_FREE(vars);
220}
221
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100222LIBYANG_API_DEF struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200223lyd_child_no_keys(const struct lyd_node *node)
224{
225 struct lyd_node **children;
226
227 if (!node) {
228 return NULL;
229 }
230
231 if (!node->schema) {
232 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +0100233 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200234 }
235
Michal Vaskoe0665742021-02-11 11:08:44 +0100236 children = lyd_node_child_p((struct lyd_node *)node);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200237 if (children) {
238 struct lyd_node *child = *children;
239 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
240 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200241 }
242 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200243 } else {
244 return NULL;
245 }
246}
Michal Vasko9b368d32020-02-14 13:53:31 +0100247
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100248LIBYANG_API_DEF const struct lys_module *
Michal Vaskoc193ce92020-03-06 11:04:48 +0100249lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100250{
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100251 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100252
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100253 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100254 return NULL;
255 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100256
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100257 if (!node->schema) {
258 opaq = (struct lyd_node_opaq *)node;
259 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200260 case LY_VALUE_XML:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100261 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
Radek Krejci8df109d2021-04-23 12:19:08 +0200262 case LY_VALUE_JSON:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100263 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
264 default:
265 return NULL;
266 }
267 }
268
Michal Vaskoef53c812021-10-13 10:21:03 +0200269 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100270}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100271
Michal Vasko598063b2021-07-19 11:39:05 +0200272void
273lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
274{
275 int cmp;
276 struct lyd_node *first;
277
278 assert(node && mod);
279
280 if (!*node) {
281 return;
282 }
283
284 first = *node;
285 cmp = strcmp(lyd_owner_module(first)->name, mod->name);
286 if (cmp > 0) {
287 /* there may be some preceding data */
288 while (first->prev->next) {
289 first = first->prev;
290 if (lyd_owner_module(first) == mod) {
291 cmp = 0;
292 break;
293 }
294 }
295 }
296
297 if (cmp == 0) {
298 /* there may be some preceding data belonging to this module */
299 while (first->prev->next) {
300 if (lyd_owner_module(first->prev) != mod) {
301 break;
302 }
303 first = first->prev;
304 }
305 }
306
307 if (cmp < 0) {
308 /* there may be some following data */
309 LY_LIST_FOR(first, first) {
310 if (lyd_owner_module(first) == mod) {
311 cmp = 0;
312 break;
313 }
314 }
315 }
316
317 if (cmp == 0) {
318 /* we have found the first module data node */
319 *node = first;
320 }
321}
322
Michal Vaskob1b5c262020-03-05 14:29:47 +0100323const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200324lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100326{
327 struct lyd_node *iter;
328 const struct lys_module *mod;
329
330 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200331 if (module) {
332 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100333 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200334 } else {
335 mod = module;
336 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100337 }
338 } else {
339 do {
340 mod = ly_ctx_get_module_iter(ctx, i);
341 } while (mod && !mod->implemented);
342 }
343
344 /* find its data */
345 *first = NULL;
346 if (mod) {
347 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100348 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100349 *first = iter;
350 break;
351 }
352 }
353 }
354
355 return mod;
356}
357
358const struct lys_module *
359lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
360{
361 const struct lys_module *mod;
362
363 if (!*next) {
364 /* all data traversed */
365 *first = NULL;
366 return NULL;
367 }
368
369 *first = *next;
370
371 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100372 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100373 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100374 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100375 break;
376 }
377 }
378
379 return mod;
380}
Michal Vasko9f96a052020-03-10 09:41:45 +0100381
382LY_ERR
383lyd_parse_check_keys(struct lyd_node *node)
384{
385 const struct lysc_node *skey = NULL;
386 const struct lyd_node *key;
387
388 assert(node->schema->nodetype == LYS_LIST);
389
Radek Krejcia1c1e542020-09-29 16:06:52 +0200390 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100391 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
392 if (!key || (key->schema != skey)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100393 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100394 return LY_EVALID;
395 }
396
397 key = key->next;
398 }
399
400 return LY_SUCCESS;
401}
Michal Vasko60ea6352020-06-29 13:39:39 +0200402
403void
Michal Vaskoaac267d2022-01-17 13:34:48 +0100404lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *node_when, struct lyd_meta **meta, uint32_t parse_opts)
Michal Vasko60ea6352020-06-29 13:39:39 +0200405{
406 struct lyd_meta *meta2, *prev_meta = NULL;
407
Michal Vaskoa5705e52020-12-09 18:15:14 +0100408 if (lysc_has_when(node->schema)) {
Michal Vaskof9b68342021-07-23 13:50:59 +0200409 if (!(parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 /* remember we need to evaluate this node's when */
Michal Vaskof9b68342021-07-23 13:50:59 +0200411 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200412 }
413 }
414
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200416 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
417 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 /* node is default according to the metadata */
419 node->flags |= LYD_DEFAULT;
420
421 /* delete the metadata */
422 if (prev_meta) {
423 prev_meta->next = meta2->next;
424 } else {
425 *meta = (*meta)->next;
426 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200427 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 break;
429 }
430
431 prev_meta = meta2;
432 }
433}
434
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100435LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400436lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200437{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200438 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
439
Michal Vasko33876022021-04-27 16:42:24 +0200440 return value->_canonical ? value->_canonical :
441 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200442}
443
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100444LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100445lyd_any_value_str(const struct lyd_node *any, char **value_str)
446{
447 const struct lyd_node_any *a;
448 struct lyd_node *tree = NULL;
449 const char *str = NULL;
450 ly_bool dynamic = 0;
451 LY_ERR ret = LY_SUCCESS;
452
453 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200454 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100455
456 a = (struct lyd_node_any *)any;
457 *value_str = NULL;
458
459 if (!a->value.str) {
460 /* there is no value in the union */
461 return LY_SUCCESS;
462 }
463
464 switch (a->value_type) {
465 case LYD_ANYDATA_LYB:
466 /* parse into a data tree */
467 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
468 LY_CHECK_GOTO(ret, cleanup);
469 dynamic = 1;
470 break;
471 case LYD_ANYDATA_DATATREE:
472 tree = a->value.tree;
473 break;
474 case LYD_ANYDATA_STRING:
475 case LYD_ANYDATA_XML:
476 case LYD_ANYDATA_JSON:
477 /* simply use the string */
478 str = a->value.str;
479 break;
480 }
481
482 if (tree) {
483 /* print into a string */
484 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
485 LY_CHECK_GOTO(ret, cleanup);
486 } else {
487 assert(str);
488 *value_str = strdup(str);
489 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
490 }
491
492 /* success */
493
494cleanup:
495 if (dynamic) {
496 lyd_free_all(tree);
497 }
498 return ret;
499}
500
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100501LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200502lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
503{
504 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200505
Michal Vaskoa820c312021-02-05 16:33:00 +0100506 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200507 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200508
509 t = (struct lyd_node_any *)trg;
510
511 /* free trg */
512 switch (t->value_type) {
513 case LYD_ANYDATA_DATATREE:
514 lyd_free_all(t->value.tree);
515 break;
516 case LYD_ANYDATA_STRING:
517 case LYD_ANYDATA_XML:
518 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100519 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200520 break;
521 case LYD_ANYDATA_LYB:
522 free(t->value.mem);
523 break;
524 }
525 t->value.str = NULL;
526
527 if (!value) {
528 /* only free value in this case */
529 return LY_SUCCESS;
530 }
531
532 /* copy src */
533 t->value_type = value_type;
534 switch (value_type) {
535 case LYD_ANYDATA_DATATREE:
536 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200537 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200538 }
539 break;
540 case LYD_ANYDATA_STRING:
541 case LYD_ANYDATA_XML:
542 case LYD_ANYDATA_JSON:
543 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200544 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200545 }
546 break;
547 case LYD_ANYDATA_LYB:
548 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200549 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200550 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200551 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200552 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200553 memcpy(t->value.mem, value->mem, len);
554 }
555 break;
556 }
557
558 return LY_SUCCESS;
559}
560
Michal Vasko106f0862021-11-02 11:49:27 +0100561const struct lysc_node *
562lyd_node_schema(const struct lyd_node *node)
563{
564 const struct lysc_node *schema = NULL;
565 const struct lyd_node *prev_iter = NULL, *iter;
566 const struct lys_module *mod;
567
568 if (!node) {
569 return NULL;
570 } else if (node->schema) {
571 return node->schema;
572 }
573
574 /* get schema node of an opaque node */
575 do {
576 /* get next data node */
577 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
578
579 /* get equivalent schema node */
580 if (iter->schema) {
581 schema = iter->schema;
582 } else {
583 /* get module */
584 mod = lyd_owner_module(iter);
Michal Vaskoa41826a2021-11-02 12:13:03 +0100585 if (!mod && !schema) {
586 /* top-level opaque node has unknown module */
587 break;
588 }
Michal Vasko106f0862021-11-02 11:49:27 +0100589
590 /* get schema node */
591 schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0);
592 }
Michal Vaskod2f404f2021-11-04 15:37:11 +0100593
594 /* remember to move to the descendant */
595 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +0100596 } while (schema && (iter != node));
597
598 return schema;
599}
600
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100601void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100602lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
603{
604 if (*root && (lyd_owner_module(*root) != mod)) {
605 /* there are no data of mod so this is simply the first top-level sibling */
606 mod = NULL;
607 }
608
609 if ((*root != to_del) || (*root)->parent) {
610 return;
611 }
612
Michal Vasko598063b2021-07-19 11:39:05 +0200613 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
614 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100615 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +0200616 } else {
617 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100618 }
619}
620
621void
Radek Krejci8df109d2021-04-23 12:19:08 +0200622ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100623{
624 struct ly_set *ns_list;
625 struct lysc_prefix *prefixes;
626 uint32_t i;
627 LY_ARRAY_COUNT_TYPE u;
628
629 if (!prefix_data) {
630 return;
631 }
632
633 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200634 case LY_VALUE_XML:
Michal Vaskoaac267d2022-01-17 13:34:48 +0100635 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100636 ns_list = prefix_data;
637 for (i = 0; i < ns_list->count; ++i) {
638 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
639 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
640 }
641 ly_set_free(ns_list, free);
642 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200643 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100644 prefixes = prefix_data;
645 LY_ARRAY_FOR(prefixes, u) {
646 free(prefixes[u].prefix);
647 }
648 LY_ARRAY_FREE(prefixes);
649 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200650 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200651 case LY_VALUE_SCHEMA:
652 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200653 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100654 break;
655 }
656}
657
658LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200659ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100660 void **prefix_data_p)
661{
662 LY_ERR ret = LY_SUCCESS;
663 struct lyxml_ns *ns;
664 struct lysc_prefix *prefixes = NULL, *orig_pref;
665 struct ly_set *ns_list, *orig_ns;
666 uint32_t i;
667 LY_ARRAY_COUNT_TYPE u;
668
669 assert(!*prefix_data_p);
670
671 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200672 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100673 *prefix_data_p = (void *)prefix_data;
674 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200675 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100676 /* copy all the value prefixes */
677 orig_pref = (struct lysc_prefix *)prefix_data;
678 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
679 *prefix_data_p = prefixes;
680
681 LY_ARRAY_FOR(orig_pref, u) {
682 if (orig_pref[u].prefix) {
683 prefixes[u].prefix = strdup(orig_pref[u].prefix);
684 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
685 }
686 prefixes[u].mod = orig_pref[u].mod;
687 LY_ARRAY_INCREMENT(prefixes);
688 }
689 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200690 case LY_VALUE_XML:
Michal Vaskoaac267d2022-01-17 13:34:48 +0100691 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100692 /* copy all the namespaces */
693 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
694 *prefix_data_p = ns_list;
695
696 orig_ns = (struct ly_set *)prefix_data;
697 for (i = 0; i < orig_ns->count; ++i) {
698 ns = calloc(1, sizeof *ns);
699 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
700 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
701
702 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
703 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
704 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
705 }
706 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
707 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
708 }
709 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200710 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200711 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200712 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100713 assert(!prefix_data);
714 *prefix_data_p = NULL;
715 break;
716 }
717
718cleanup:
719 if (ret) {
720 ly_free_prefix_data(format, *prefix_data_p);
721 *prefix_data_p = NULL;
722 }
723 return ret;
724}
725
726LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +0200727ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
Radek Krejci8df109d2021-04-23 12:19:08 +0200728 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100729{
730 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100731 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100732 const struct lyxml_ns *ns;
733 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100734 struct ly_set *ns_list;
735 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +0200736 const char *value_iter, *value_next, *value_end;
737 uint32_t substr_len;
738 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100739
740 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200741 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100742 /* copy all referenced modules as prefix - module pairs */
743 if (!*prefix_data_p) {
744 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100745 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +0200746 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100747 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100748 } else {
749 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +0200750 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100751 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100752 }
753
754 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +0200755 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +0200756 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +0200757 LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
aPiecek83436bc2021-03-30 12:20:45 +0200758 if (is_prefix) {
759 /* we have a possible prefix. Do we already have the prefix? */
760 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
761 if (!mod) {
762 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
763 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200764 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +0200765 /* store a new prefix - module pair */
766 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
767 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100768
aPiecek83436bc2021-03-30 12:20:45 +0200769 val_pref->prefix = strndup(value_iter, substr_len);
770 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
771 val_pref->mod = mod;
772 } /* else it is not even defined */
773 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100774 }
775 }
776 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200777 case LY_VALUE_XML:
Michal Vaskoaac267d2022-01-17 13:34:48 +0100778 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100779 /* copy all referenced namespaces as prefix - namespace pairs */
780 if (!*prefix_data_p) {
781 /* new prefix data */
782 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoaac267d2022-01-17 13:34:48 +0100783 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100784 *prefix_data_p = ns_list;
785 } else {
786 /* reuse prefix data */
Michal Vaskoaac267d2022-01-17 13:34:48 +0100787 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100788 ns_list = *prefix_data_p;
789 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100790
Michal Vaskofc2cd072021-02-24 13:17:17 +0100791 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +0200792 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +0200793 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +0200794 LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
aPiecek83436bc2021-03-30 12:20:45 +0200795 if (is_prefix) {
796 /* we have a possible prefix. Do we already have the prefix? */
797 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
798 if (!ns) {
799 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
800 if (ns) {
801 /* store a new prefix - namespace pair */
802 new_ns = calloc(1, sizeof *new_ns);
803 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
804 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100805
aPiecek83436bc2021-03-30 12:20:45 +0200806 new_ns->prefix = strndup(value_iter, substr_len);
807 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
808 new_ns->uri = strdup(ns->uri);
809 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
810 } /* else it is not even defined */
811 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100812 }
813 }
814 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200815 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200816 case LY_VALUE_SCHEMA_RESOLVED:
817 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200818 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100819 if (!*prefix_data_p) {
820 /* new prefix data - simply copy all the prefix data */
821 *format_p = format;
822 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
823 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100824 break;
825 }
826
827cleanup:
828 if (ret) {
829 ly_free_prefix_data(*format_p, *prefix_data_p);
830 *prefix_data_p = NULL;
831 }
832 return ret;
833}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100834
835const char *
Radek Krejci8df109d2021-04-23 12:19:08 +0200836ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100837{
838 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +0200839 case LY_VALUE_CANON:
840 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +0200841 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100842 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +0200843 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100844 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +0200845 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100846 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +0200847 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100848 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +0200849 case LY_VALUE_LYB:
850 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100851 default:
852 break;
853 }
854
855 return NULL;
856}
Michal Vasko43297a02021-05-19 11:12:37 +0200857
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100858LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +0200859ly_time_str2time(const char *value, time_t *time, char **fractions_s)
860{
861 struct tm tm = {0};
862 uint32_t i, frac_len;
863 const char *frac;
864 int64_t shift, shift_m;
865 time_t t;
866
867 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
868
869 tm.tm_year = atoi(&value[0]) - 1900;
870 tm.tm_mon = atoi(&value[5]) - 1;
871 tm.tm_mday = atoi(&value[8]);
872 tm.tm_hour = atoi(&value[11]);
873 tm.tm_min = atoi(&value[14]);
874 tm.tm_sec = atoi(&value[17]);
875
876 t = timegm(&tm);
877 i = 19;
878
879 /* fractions of a second */
880 if (value[i] == '.') {
881 ++i;
882 frac = &value[i];
883 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
884
885 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +0200886 } else {
887 frac = NULL;
888 }
889
890 /* apply offset */
891 if ((value[i] == 'Z') || (value[i] == 'z')) {
892 /* zero shift */
893 shift = 0;
894 } else {
895 shift = strtol(&value[i], NULL, 10);
896 shift = shift * 60 * 60; /* convert from hours to seconds */
897 shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
898 /* correct sign */
899 if (shift < 0) {
900 shift_m *= -1;
901 }
902 /* connect hours and minutes of the shift */
903 shift = shift + shift_m;
904 }
905
906 /* we have to shift to the opposite way to correct the time */
907 t -= shift;
908
909 *time = t;
910 if (fractions_s) {
911 if (frac) {
912 *fractions_s = strndup(frac, frac_len);
913 LY_CHECK_RET(!*fractions_s, LY_EMEM);
914 } else {
915 *fractions_s = NULL;
916 }
917 }
918 return LY_SUCCESS;
919}
920
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100921LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +0200922ly_time_time2str(time_t time, const char *fractions_s, char **str)
923{
924 struct tm tm;
Michal Vasko143ffa82021-05-20 11:11:39 +0200925 char zoneshift[8];
Michal Vasko43297a02021-05-19 11:12:37 +0200926 int32_t zonediff_h, zonediff_m;
927
928 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
929
930 /* initialize the local timezone */
931 tzset();
932
Jan Kundrátf0737532022-02-14 18:32:18 +0100933#ifdef HAVE_TM_GMTOFF
Michal Vasko43297a02021-05-19 11:12:37 +0200934 /* convert */
935 if (!localtime_r(&time, &tm)) {
936 return LY_ESYS;
937 }
938
939 /* get timezone offset */
940 if (tm.tm_gmtoff == 0) {
941 /* time is Zulu (UTC) */
942 zonediff_h = 0;
943 zonediff_m = 0;
944 } else {
945 /* timezone offset */
946 zonediff_h = tm.tm_gmtoff / 60 / 60;
947 zonediff_m = tm.tm_gmtoff / 60 % 60;
948 }
949 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
Jan Kundrátaac59c22021-12-09 23:25:15 +0100950#else
Jan Kundrátf0737532022-02-14 18:32:18 +0100951 /* convert */
952 if (!gmtime_r(&time, &tm)) {
953 return LY_ESYS;
954 }
955
Jan Kundrátaac59c22021-12-09 23:25:15 +0100956 (void)zonediff_h;
957 (void)zonediff_m;
958 sprintf(zoneshift, "-00:00");
959#endif
Michal Vasko43297a02021-05-19 11:12:37 +0200960
961 /* print */
962 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
963 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
964 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
965 return LY_EMEM;
966 }
967
968 return LY_SUCCESS;
969}
970
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100971LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +0200972ly_time_str2ts(const char *value, struct timespec *ts)
973{
974 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +0200975 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +0200976 int frac_len;
977
978 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
979
980 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
981 LY_CHECK_RET(rc);
982
983 /* convert fractions of a second to nanoseconds */
984 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +0200985 /* init frac_buf with zeroes */
986 memset(frac_buf, '0', 9);
987 frac_buf[9] = '\0';
988
Michal Vasko43297a02021-05-19 11:12:37 +0200989 frac_len = strlen(fractions_s);
990 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
991 ts->tv_nsec = atol(frac_buf);
992 free(fractions_s);
993 } else {
994 ts->tv_nsec = 0;
995 }
996
997 return LY_SUCCESS;
998}
999
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001000LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001001ly_time_ts2str(const struct timespec *ts, char **str)
1002{
1003 char frac_buf[10];
1004
Jan Kundrátbd157002021-08-30 14:02:22 +02001005 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001006
1007 /* convert nanoseconds to fractions of a second */
1008 if (ts->tv_nsec) {
1009 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1010 }
1011
1012 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1013}