diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2010-12-02 14:35:25 -0800 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2010-12-02 14:35:25 -0800 |
commit | 352a9e94534933aca7403e91d1dc55c26bf633ce (patch) | |
tree | df413a5fbd1ab0e0b4776eff98149dd9cbc8006d /src/cstore | |
parent | 58005829f825c3e6843d0c2c36e1e343194ec3dd (diff) | |
download | vyatta-cfg-352a9e94534933aca7403e91d1dc55c26bf633ce.tar.gz vyatta-cfg-352a9e94534933aca7403e91d1dc55c26bf633ce.zip |
implement load function in new config input/output infrastructure.
* add "commands diff" functionality to config input/output infrastructure.
* consolidate similar logic in "commands diff" and "show diff".
* add loadFile functionality to cstore using "commands diff".
* export loadFile through shell API.
Diffstat (limited to 'src/cstore')
-rw-r--r-- | src/cstore/cstore.cpp | 75 | ||||
-rw-r--r-- | src/cstore/cstore.hpp | 8 |
2 files changed, 80 insertions, 3 deletions
diff --git a/src/cstore/cstore.cpp b/src/cstore/cstore.cpp index ed18c9e..0ff8ba2 100644 --- a/src/cstore/cstore.cpp +++ b/src/cstore/cstore.cpp @@ -31,6 +31,9 @@ #include <cli_cstore.h> #include <cstore/cstore.hpp> #include <cstore/cstore-varref.hpp> +#include <cnode/cnode.hpp> +#include <cnode/cnode-algorithm.hpp> +#include <cparse/cparse.hpp> ////// constants @@ -1803,6 +1806,63 @@ Cstore::unmarkCfgPathDeactivated(const vector<string>& path_comps) return ret; } +// load specified config file +bool +Cstore::loadFile(const char *filename) +{ + if (!inSession()) { + output_user("Cannot load config outside configuration session\n"); + // exit handled by assert below + } + ASSERT_IN_SESSION; + + FILE *fin = fopen(filename, "r"); + if (!fin) { + output_user("Failed to open specified config file\n"); + return false; + } + + // get the config tree from the file + cnode::CfgNode *froot = cparse::parse_file(fin, *this); + if (!froot) { + output_user("Failed to parse specified config file\n"); + return false; + } + + // get the config tree from the active config + vector<string> args; + cnode::CfgNode aroot(*this, args, true, true); + + // get the "commands diff" between the two + vector<vector<string> > del_list; + vector<vector<string> > set_list; + vector<vector<string> > com_list; + cnode::get_cmds_diff(aroot, *froot, del_list, set_list, com_list); + + // "apply" the changes to the working config + for (size_t i = 0; i < del_list.size(); i++) { + vtw_def def; + if (!validateDeletePath(del_list[i], def) + || !deleteCfgPath(del_list[i], def)) { + print_str_vec("Delete [", "] failed\n", del_list[i], "'"); + } + } + for (size_t i = 0; i < set_list.size(); i++) { + if (!validateSetPath(set_list[i]) || !setCfgPath(set_list[i])) { + print_str_vec("Set [", "] failed\n", set_list[i], "'"); + } + } + for (size_t i = 0; i < com_list.size(); i++) { + vtw_def def; + if (!validateCommentArgs(com_list[i], def) + || !commentCfgPath(com_list[i], def)) { + print_str_vec("Comment [", "] failed\n", com_list[i], "'"); + } + } + + return true; +} + /* "changed" status handling. * the "changed" status is used during commit to check if a node has been * changed. note that if a node is "changed", all of its ancestors are also @@ -2797,3 +2857,18 @@ Cstore::shell_escape_squotes(string& str) } } +// print a vector of strings +void +Cstore::print_str_vec(const char *pre, const char *post, + const vector<string>& vec, const char *quote) +{ + output_user("%s", pre); + for (size_t i = 0; i < vec.size(); i++) { + if (i > 0) { + output_user(" "); + } + output_user("%s%s%s", quote, vec[i].c_str(), quote); + } + output_user("%s", post); +} + diff --git a/src/cstore/cstore.hpp b/src/cstore/cstore.hpp index 267dabe..d1d01fa 100644 --- a/src/cstore/cstore.hpp +++ b/src/cstore/cstore.hpp @@ -112,7 +112,7 @@ public: * edit-related commands (invoked from shell functions) * completion-related (for completion script) * session-related (setup, teardown, etc.) - * load (XXX currently still implemented in perl) + * load * * these operate on the "working config" and the session and MUST NOT * be used by anything other than the listed operations. @@ -165,8 +165,8 @@ public: virtual bool inSession() = 0; // commit bool unmarkCfgPathChanged(const vector<string>& path_comps); - // XXX load - //bool unmarkCfgPathDeactivatedDescendants(const vector<string>& path_comps); + // load + bool loadFile(const char *filename); /****** * these functions are observers of the current "working config" or @@ -476,6 +476,8 @@ private: // util functions string get_shell_prompt(const string& level); void shell_escape_squotes(string& str); + void print_str_vec(const char *pre, const char *post, + const vector<string>& vec, const char *quote); }; #endif /* _CSTORE_H_ */ |