Improvements and bug fixing
This commit is contained in:
parent
f05c9882ed
commit
b80df54b9a
|
@ -2,9 +2,7 @@ cmake_minimum_required(VERSION 3.21)
|
||||||
project(clAugeas VERSION 1.0.1 DESCRIPTION "clAugeas description")
|
project(clAugeas VERSION 1.0.1 DESCRIPTION "clAugeas description")
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
if(WIN32)
|
if(APPLE)
|
||||||
message("TODO: Windows instructions")
|
|
||||||
elseif(APPLE)
|
|
||||||
include_directories(/opt/homebrew/opt/augeas/include/)
|
include_directories(/opt/homebrew/opt/augeas/include/)
|
||||||
#link_directories(/opt/homebrew/opt/augeas/lib) # TODO: Check if works with this commente.d
|
#link_directories(/opt/homebrew/opt/augeas/lib) # TODO: Check if works with this commente.d
|
||||||
elseif(UNIX)
|
elseif(UNIX)
|
||||||
|
@ -25,6 +23,7 @@ find_library(
|
||||||
add_library(clAugeas SHARED
|
add_library(clAugeas SHARED
|
||||||
augSettings.cpp
|
augSettings.cpp
|
||||||
augSettings.h
|
augSettings.h
|
||||||
|
utils.h utils.cpp
|
||||||
main.cpp main.h)
|
main.cpp main.h)
|
||||||
|
|
||||||
target_link_libraries(clAugeas xml2)
|
target_link_libraries(clAugeas xml2)
|
||||||
|
|
174
main.cpp
174
main.cpp
|
@ -1,97 +1,109 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
template<class T, size_t N>
|
||||||
|
constexpr size_t size(T (&)[N]) { return N; }
|
||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
inline bool path_exists (const std::string& name) {
|
|
||||||
struct stat buffer;
|
|
||||||
return (stat (name.c_str(), &buffer) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
CLAPI void* init_aug(augSettings settings, int flags) {
|
|
||||||
|
|
||||||
if(!path_exists(std::string(settings.root))) {
|
|
||||||
std::cout << "error: root path is invalid." << settings.root << std::endl;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
CLAPI void*
|
||||||
|
init_aug(augSettings settings, int flags) {
|
||||||
|
CHECK_RET_NULLPTR(!path_exists(std::string(settings.root)), "Root path is invalid.");
|
||||||
return aug_init(settings.root, settings.loadPath, flags);
|
return aug_init(settings.root, settings.loadPath, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
CLAPI void close_aug(void* aug) {
|
CLAPI void
|
||||||
|
close_aug(void *aug) {
|
||||||
|
CHECK_RET_VOID(!aug, "Failed to close augeas.");
|
||||||
aug_close((augeas *) aug);
|
aug_close((augeas *) aug);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_file(void* aug, const char* filePath) {
|
CLAPI bool
|
||||||
int r = aug_load_file((augeas *) aug,filePath);
|
load_file(void *aug, const char *filePath) {
|
||||||
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
||||||
|
int r = aug_load_file((augeas *) aug, filePath);
|
||||||
return r == 0;
|
return r == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLAPI void
|
||||||
CLAPI void print_preview(augeas* aug, const char* matchPath) {
|
print_preview(augeas *aug, const char *path) {
|
||||||
|
CHECK_RET_VOID(!aug, "Augeas is in invalid state");
|
||||||
if(aug == nullptr) {
|
|
||||||
std::cout << "error: augeas is not initialized." << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
char *s;
|
char *s;
|
||||||
|
r = aug_preview(aug, path, &s);
|
||||||
r = aug_preview(aug,matchPath,&s);
|
CHECK_RET_VOID(r != 0, "Failed to preview.")
|
||||||
|
|
||||||
if(r != 0) {
|
|
||||||
std::cout << "Failure previewing." << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << s << std::endl;
|
std::cout << s << std::endl;
|
||||||
|
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLAPI int rm_node(augeas *aug, const char *path) {
|
||||||
|
CHECK_RET_INT(!aug, INVALID_STATE);
|
||||||
|
return aug_rm(aug, path);
|
||||||
|
}
|
||||||
|
|
||||||
// Print tree of the matchPath
|
|
||||||
|
|
||||||
|
CLAPI char*
|
||||||
|
get_preview(augeas *aug, const char *path) {
|
||||||
|
|
||||||
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
||||||
|
int r;
|
||||||
|
char *s;
|
||||||
|
|
||||||
|
r = aug_preview(aug, path, &s);
|
||||||
|
|
||||||
|
if (r != 0) {
|
||||||
|
std::cout << "Failure previewing." << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// must be freed
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Print tree of the path
|
||||||
CLAPI void print_tree(
|
CLAPI void print_tree(
|
||||||
augeas *aug,
|
augeas *aug,
|
||||||
const char* matchPath
|
const char *path
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if(aug == nullptr) {
|
CHECK_RET_VOID(!aug, INVALID_STATE);
|
||||||
std::cout << "error: augeas is not initialized." << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
FILE *out = tmpfile();
|
FILE *out = tmpfile();
|
||||||
|
|
||||||
r = aug_print(aug, out,matchPath);
|
r = aug_print(aug, out, path);
|
||||||
|
|
||||||
if(r != 0) {
|
if (r != 0) {
|
||||||
std::cout << "error: aug_print failure." << std::endl;
|
std::cout << "error: aug_print failure." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map <std::string, std::string> stdBindList;
|
std::map<std::string, std::string> stdBindList;
|
||||||
std::map <std::string ,std::string>::iterator pos;
|
std::map<std::string, std::string>::iterator pos;
|
||||||
|
|
||||||
char line[256];
|
char line[256];
|
||||||
rewind(out);
|
rewind(out);
|
||||||
while (fgets(line, 256, out) != nullptr) {
|
while (fgets(line, 256, out) != nullptr) {
|
||||||
// remove end of line
|
// remove end of line
|
||||||
line[strlen(line) - 1] = '\0';
|
line[strlen(line) - 1] = '\0';
|
||||||
std::string str_matchPath = matchPath;
|
std::string str_path = path;
|
||||||
std::string s = line;
|
std::string s = line;
|
||||||
// skip comments
|
// skip comments
|
||||||
if (s.find("#comment") != std::string::npos)
|
if (s.find("#comment") != std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
s = s.substr(str_matchPath.length() - 1);
|
s = s.substr(str_path.length() - 1);
|
||||||
// split by '=' sign
|
// split by '=' sign
|
||||||
size_t eqpos = s.find(" = ");
|
size_t eqpos = s.find(" = ");
|
||||||
if (eqpos == std::string::npos)
|
if (eqpos == std::string::npos)
|
||||||
|
@ -102,55 +114,50 @@ CLAPI void print_tree(
|
||||||
// remove '"' sign from around value
|
// remove '"' sign from around value
|
||||||
value.erase(value.begin());
|
value.erase(value.begin());
|
||||||
value.erase(value.end() - 1);
|
value.erase(value.end() - 1);
|
||||||
stdBindList.insert(std::pair<std::string,std::string>(key,value)); // 2
|
stdBindList.insert(std::pair<std::string, std::string>(key, value)); // 2
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(out);
|
fclose(out);
|
||||||
|
|
||||||
for (pos = stdBindList.begin();pos!=stdBindList.end();pos++)
|
for (pos = stdBindList.begin(); pos != stdBindList.end(); pos++) {
|
||||||
{
|
|
||||||
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
|
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
CLAPI const char* get_tree(
|
CLAPI char *get_tree(
|
||||||
augeas *aug,
|
augeas *aug,
|
||||||
const char* matchPath
|
const char *path
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if(aug == nullptr) {
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
||||||
std::cout << "error: augeas is not initialized." << std::endl;
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
FILE *out = tmpfile();
|
FILE *out = tmpfile();
|
||||||
|
|
||||||
r = aug_print(aug, out,matchPath);
|
r = aug_print(aug, out, path);
|
||||||
|
|
||||||
if(r != 0) {
|
if (r != 0) {
|
||||||
std::cout << "error: aug_print failure." << std::endl;
|
std::cout << "error: aug_print failure." << std::endl;
|
||||||
return "";
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map <std::string ,std::string>::iterator pos;
|
std::map<std::string, std::string>::iterator pos;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
|
|
||||||
char line[256];
|
char line[256];
|
||||||
rewind(out);
|
rewind(out);
|
||||||
while (fgets(line, 256, out) != nullptr) {
|
while (fgets(line, 256, out) != nullptr) {
|
||||||
// remove end of line
|
// remove end of line
|
||||||
line[strlen(line) - 1] = '\0';
|
line[strlen(line) - 1] = '\0';
|
||||||
std::string str_matchPath = matchPath;
|
std::string str_path = path;
|
||||||
std::string s = line;
|
std::string s = line;
|
||||||
// skip comments
|
// skip comments
|
||||||
if (s.find("#comment") != std::string::npos)
|
if (s.find("#comment") != std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
s = s.substr(str_matchPath.length() - 1);
|
s = s.substr(str_path.length() - 1);
|
||||||
// split by '=' sign
|
// split by '=' sign
|
||||||
size_t eqpos = s.find(" = ");
|
size_t eqpos = s.find(" = ");
|
||||||
if (eqpos == std::string::npos)
|
if (eqpos == std::string::npos)
|
||||||
|
@ -170,16 +177,34 @@ CLAPI const char* get_tree(
|
||||||
return strdup(ss.str().c_str());
|
return strdup(ss.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
CLAPI void free_str(char* str) {
|
CLAPI void free_str(char *str) {
|
||||||
if(str != nullptr) {
|
if (str != nullptr) {
|
||||||
free(str);
|
free(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CLAPI const char* get_node(augeas* aug, char* path ) {
|
CLAPI bool save(augeas *aug) {
|
||||||
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
||||||
|
return aug_save(aug) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CLAPI void get_arr( unsigned long &size, char** result)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
CLAPI int match(augeas *aug, const char *match_path, char*** matches) {
|
||||||
|
CHECK_RET_INT(!aug, INVALID_STATE);
|
||||||
|
return aug_match(aug, match_path, matches);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
CLAPI const char *get_node(augeas *aug, const char *path) {
|
||||||
|
|
||||||
const char *value;
|
const char *value;
|
||||||
|
|
||||||
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
||||||
/*
|
/*
|
||||||
* Return 1 if there is exactly one node matching PATH,
|
* Return 1 if there is exactly one node matching PATH,
|
||||||
* 0 if there is none, and a negative value
|
* 0 if there is none, and a negative value
|
||||||
|
@ -190,17 +215,18 @@ CLAPI const char* get_node(augeas* aug, char* path ) {
|
||||||
* and is valid as long as its node remains unchanged.
|
* and is valid as long as its node remains unchanged.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int rc = aug_get(aug, path, &value);
|
aug_get(aug, path, &value);
|
||||||
if (1 == rc) {
|
return value;
|
||||||
if (nullptr != value) {
|
}
|
||||||
return value;
|
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
CLAPI bool set_node(augeas *aug, const char *path, const char *value) {
|
||||||
return "";
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
||||||
}
|
return aug_set(aug, path, value) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CLAPI bool insert_node(augeas *aug, const char *path, const char *label, int before) {
|
||||||
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
||||||
|
return aug_insert(aug, path, label, before) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
34
main.h
34
main.h
|
@ -4,29 +4,43 @@
|
||||||
#include "map"
|
#include "map"
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include "augSettings.h"
|
#include "augSettings.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#define CLAPI __declspec(dllexport)
|
#define CLAPI __attribute__((unused)) __declspec(dllexport)
|
||||||
#else
|
#else
|
||||||
#define CLAPI
|
#define CLAPI __attribute__((unused))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
CLAPI void *init_aug (augSettings settings, int flags);
|
// Object Life-cycle
|
||||||
|
CLAPI void *init_aug (augSettings settings, int flags);
|
||||||
|
|
||||||
CLAPI bool load_file (void *aug, const char *filePath);
|
CLAPI bool load_file (void *aug, const char *filePath);
|
||||||
|
|
||||||
CLAPI void print_preview (augeas *aug,const char *matchPath);
|
CLAPI void print_preview (augeas *aug, const char *path);
|
||||||
|
|
||||||
CLAPI void print_tree (augeas *aug,const char *matchPath);
|
CLAPI void close_aug (void *aug);
|
||||||
|
|
||||||
CLAPI const char *get_tree (augeas *aug,const char *matchPath);
|
CLAPI void free_str (char *str);
|
||||||
|
|
||||||
CLAPI const char *get_node (augeas *aug, char *path);
|
CLAPI void print_tree (augeas *aug, const char *path);
|
||||||
|
|
||||||
CLAPI void close_aug (void *aug);
|
CLAPI char *get_tree (augeas *aug, const char *path);
|
||||||
|
|
||||||
CLAPI void free_str (char *str);
|
CLAPI const char *get_node (augeas *aug, const char *path);
|
||||||
|
|
||||||
|
CLAPI bool set_node (augeas *aug, const char *path, const char *value);
|
||||||
|
|
||||||
|
CLAPI bool insert_node (augeas *aug, const char *path, const char *label, int before);
|
||||||
|
|
||||||
|
CLAPI char* get_preview (augeas *aug, const char *path);
|
||||||
|
|
||||||
|
CLAPI int rm_node (augeas *aug, const char *path);
|
||||||
|
|
||||||
|
CLAPI bool save (augeas *aug);
|
||||||
|
|
||||||
|
CLAPI int match(augeas *aug, const char *match_path, char*** matches);
|
||||||
|
// internal static extern int match(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath, uint* size, char*** stringArray);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
bool path_exists(const std::string &name) {
|
||||||
|
struct stat buffer;
|
||||||
|
return (stat(name.c_str(), &buffer) == 0);
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "string"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Some guards
|
||||||
|
#define CHECK_RET_VOID(bx, msg) if(bx) {std::cout << msg << std::endl; return;}
|
||||||
|
#define CHECK_RET_NULLPTR(bx, msg) if(bx) {std::cout << msg << std::endl; return nullptr;}
|
||||||
|
#define CHECK_RET_BOOL(bx, msg) if(bx) {std::cout << msg << std::endl; return false;}
|
||||||
|
#define CHECK_RET_INT(bx, msg) if(bx) {std::cout << msg << std::endl; return -1;}
|
||||||
|
|
||||||
|
#define INVALID_STATE "Augeas is in invalid state."
|
||||||
|
|
||||||
|
bool path_exists(const std::string &name);
|
||||||
|
|
Loading…
Reference in New Issue