diff options
author | John Estabrook <jestabro@vyos.io> | 2024-10-23 19:48:31 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2024-10-23 19:48:31 -0500 |
commit | 3e16a23768680d95090e5c1d75a19395980e0a91 (patch) | |
tree | 23bc2e600266a53d14f57de5eab6136b346847c6 | |
parent | 4cd430f5f80fbbb44298ee1c2898230fbb42a7e5 (diff) | |
download | vyatta-cfg-3e16a23768680d95090e5c1d75a19395980e0a91.tar.gz vyatta-cfg-3e16a23768680d95090e5c1d75a19395980e0a91.zip |
T5528: add adapter interface
Expose legacy set/delete functions in a form suitable for external
calls.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/vy_adapter.cpp | 102 | ||||
-rw-r--r-- | src/vy_adapter.h | 31 |
3 files changed, 135 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 2170f95..6ef8d64 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,6 +52,7 @@ src_libvyatta_cfg_la_SOURCES += src/cnode/cnode-algorithm.cpp src_libvyatta_cfg_la_SOURCES += src/cparse/cparse.cpp src_libvyatta_cfg_la_SOURCES += src/cparse/cparse_lex.c src_libvyatta_cfg_la_SOURCES += src/commit/commit-algorithm.cpp +src_libvyatta_cfg_la_SOURCES += src/vy_adapter.cpp CLEANFILES = src/cli_parse.c src/cli_parse.h src/cli_def.c src/cli_val.c CLEANFILES += src/cparse/cparse.cpp src/cparse/cparse.h CLEANFILES += src/cparse/cparse_lex.c @@ -61,6 +62,7 @@ LDADD += $(GOBJECT_LIBS) vincludedir = $(includedir)/vyatta-cfg vinclude_HEADERS = src/cli_cstore.h +vinclude_HEADERS += src/vy_adapter.h vcincdir = $(vincludedir)/cstore vcinc_HEADERS = src/cstore/cstore-c.h diff --git a/src/vy_adapter.cpp b/src/vy_adapter.cpp new file mode 100644 index 0000000..793728f --- /dev/null +++ b/src/vy_adapter.cpp @@ -0,0 +1,102 @@ +/* Copyright 2024 VyOS maintainers and contributors <maintainers@vyos.io> + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <cstring> +#include <cstdlib> +#include <vector> +#include <string> +#include <sstream> +#include <iostream> +#include <cstdint> +#include <cassert> + +#include <cstore/cstore.hpp> +#include <vy_adapter.h> + +using namespace cstore; + +static uint64_t uint_of_voidptr(void* p) +{ + assert (((uintptr_t) p & 1) == 0); + return (uint64_t) p | 1; +} + +static void *voidptr_of_uint(uint64_t v) +{ + return (void *)(uintptr_t)(v & ~1); +} + +static Cstore *cstore_of_handle(uint64_t handle) +{ + return (Cstore *) voidptr_of_uint(handle); +} + +uint64_t +vy_cstore_init(void) +{ + Cstore *handle = Cstore::createCstore(false); + return uint_of_voidptr(handle); +} + +void +vy_cstore_free(uint64_t handle) +{ + Cstore *h = cstore_of_handle(handle); + delete h; +} + +int +vy_in_session(uint64_t handle) +{ + Cstore *h = cstore_of_handle(handle); + return h->inSession() ? 1 : 0; +} + +int +vy_set_path(uint64_t handle, const void** path_ptr, size_t len) +{ + Cstore *cstore = cstore_of_handle(handle); + const char **path = (const char **) path_ptr; + Cpath path_comps = Cpath(path, len); + int res; + + res = cstore->validateSetPath(path_comps); + if (!res) { + return 1; + } + + res = cstore->setCfgPath(path_comps); + if (!res) { + return 2; + } + + return 0; +} + +int +vy_delete_path(uint64_t handle, const void** path_ptr, size_t len) +{ + Cstore *cstore = cstore_of_handle(handle); + const char **path = (const char **) path_ptr; + Cpath path_comps = Cpath(path, len); + int res; + + res = cstore->deleteCfgPath(path_comps); + if (!res) { + return 1; + } + + return 0; +} diff --git a/src/vy_adapter.h b/src/vy_adapter.h new file mode 100644 index 0000000..8e0f376 --- /dev/null +++ b/src/vy_adapter.h @@ -0,0 +1,31 @@ +/* Copyright 2024 VyOS maintainers and contributors <maintainers@vyos.io> + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _VY_ADAPTER_H_ +#define _VY_ADAPTER_H_ +#ifdef __cplusplus +extern "C" { +#endif + +uint64_t vy_cstore_init(void); +void vy_cstore_free(uint64_t); +int vy_in_session(uint64_t); +int vy_set_path(uint64_t, const void **, size_t); +int vy_delete_path(uint64_t, const void **, size_t); + +#ifdef __cplusplus +} +#endif +#endif |