2022-11-02 18:25:09 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
#include <augeas.h>
|
2022-11-02 21:27:10 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2022-11-02 19:27:57 +03:00
|
|
|
// This file is currently for testing and training using augeas with C++
|
|
|
|
// https://github.com/hercules-team/augeas/blob/master/tests/test-api.c
|
2022-11-02 18:25:09 +03:00
|
|
|
|
2022-11-02 19:27:57 +03:00
|
|
|
static const char *abs_top_srcdir;
|
|
|
|
static const char *root = "../root";
|
|
|
|
static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist";
|
|
|
|
|
|
|
|
|
|
|
|
static void testGet() {
|
|
|
|
int r;
|
|
|
|
const char *value;
|
|
|
|
const char *label;
|
|
|
|
struct augeas *aug;
|
|
|
|
char *s;
|
|
|
|
|
2022-11-02 20:12:19 +03:00
|
|
|
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
|
2022-11-02 19:27:57 +03:00
|
|
|
|
|
|
|
r = aug_load_file(aug, "/etc/hosts");
|
|
|
|
r = aug_source(aug, "/files/etc/hosts/1", &s);
|
|
|
|
|
|
|
|
std::cout << "The value read from node: " << s << std::endl;
|
|
|
|
|
|
|
|
aug_close(aug);
|
|
|
|
}
|
|
|
|
|
2022-11-02 20:12:19 +03:00
|
|
|
static void readHostsFile() {
|
2022-11-02 19:27:57 +03:00
|
|
|
struct augeas *aug;
|
|
|
|
int r;
|
|
|
|
char *s;
|
|
|
|
char *etc_hosts_fn = NULL;
|
|
|
|
FILE *hosts_fp = NULL;
|
|
|
|
char *hosts_txt = NULL;
|
2022-11-02 18:25:09 +03:00
|
|
|
|
2022-11-02 20:12:19 +03:00
|
|
|
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
|
2022-11-02 18:25:09 +03:00
|
|
|
|
2022-11-02 19:27:57 +03:00
|
|
|
r = aug_load_file(aug, "/etc/hosts");
|
|
|
|
r = aug_preview(aug, "/files/etc/hosts/1", &s);
|
|
|
|
|
|
|
|
std::cout << s << std::endl;
|
|
|
|
|
|
|
|
free(hosts_txt);
|
|
|
|
free(s);
|
|
|
|
aug_close(aug);
|
2022-11-02 18:25:09 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 21:27:10 +03:00
|
|
|
static void printHostsText() {
|
2022-11-02 20:12:19 +03:00
|
|
|
struct augeas *aug;
|
|
|
|
int r;
|
|
|
|
char *s;
|
|
|
|
char *etc_hosts_fn = NULL;
|
|
|
|
|
|
|
|
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
|
|
|
|
|
|
|
|
r = aug_load_file(aug, "/etc/hosts");
|
|
|
|
r = aug_preview(aug, "/files/etc/hosts/1", &s);
|
|
|
|
std::cout << s << std::endl;
|
|
|
|
|
|
|
|
free(s);
|
|
|
|
aug_close(aug);
|
|
|
|
}
|
2022-11-02 18:25:09 +03:00
|
|
|
|
|
|
|
|
2022-11-02 21:27:10 +03:00
|
|
|
/// Get the tree of the /etc/hosts file printed in the console
|
|
|
|
/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784
|
|
|
|
|
|
|
|
static void readHostsTree() {
|
|
|
|
struct augeas *aug;
|
|
|
|
int r;
|
|
|
|
FILE *out = tmpfile();
|
|
|
|
char *etc_hosts_fn = NULL;
|
|
|
|
|
|
|
|
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
|
|
|
|
|
|
|
|
const std::string matchPath = "/files/etc/hosts/*";
|
|
|
|
|
|
|
|
r = aug_load_file(aug, "/etc/hosts");
|
|
|
|
r = aug_print(aug, out,"/files/etc/hosts/*");
|
|
|
|
|
|
|
|
std::map <std::string, std::string> stdBindList;
|
|
|
|
std::map <std::string , std::string>::iterator pos;
|
|
|
|
|
|
|
|
char line[256];
|
|
|
|
rewind(out);
|
|
|
|
while (fgets(line, 256, out) != NULL) {
|
|
|
|
// remove end of line
|
|
|
|
line[strlen(line) - 1] = '\0';
|
|
|
|
std::string s = line;
|
|
|
|
;
|
|
|
|
// skip comments
|
|
|
|
if (s.find("#comment") != std::string::npos)
|
|
|
|
continue;
|
|
|
|
s = s.substr(matchPath.length() - 1);
|
|
|
|
// split by '=' sign
|
|
|
|
size_t eqpos = s.find(" = ");
|
|
|
|
if (eqpos == std::string::npos)
|
|
|
|
continue;
|
|
|
|
// extract key and value
|
|
|
|
std::string key = s.substr(0, eqpos);
|
|
|
|
std::string value = s.substr(eqpos + 3);
|
|
|
|
// remove '"' sign from around value
|
|
|
|
value.erase(value.begin());
|
|
|
|
value.erase(value.end() - 1);
|
|
|
|
stdBindList.insert(std::pair<std::string,std::string>(key,value)); // 2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
for (pos = stdBindList.begin();pos!=stdBindList.end();pos++)
|
|
|
|
{
|
|
|
|
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
aug_close(aug);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-02 19:27:57 +03:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
2022-11-02 20:12:19 +03:00
|
|
|
testGet();
|
2022-11-02 21:27:10 +03:00
|
|
|
readHostsTree();
|
2022-11-02 18:25:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|