summaryrefslogtreecommitdiff
path: root/src/cstore
diff options
context:
space:
mode:
authorAn-Cheng Huang <ancheng@vyatta.com>2011-01-27 12:07:44 -0800
committerAn-Cheng Huang <ancheng@vyatta.com>2011-01-27 12:07:44 -0800
commitfa99a35967bcaf6a42bd58bcb2808769d4223a50 (patch)
treefc0a107b2a7c861adf5aceb2bdff55430a0980e0 /src/cstore
parent617a9b3c8915f1e9934bd9a0e0272fafb9c31d59 (diff)
downloadvyatta-cfg-fa99a35967bcaf6a42bd58bcb2808769d4223a50.tar.gz
vyatta-cfg-fa99a35967bcaf6a42bd58bcb2808769d4223a50.zip
rework/simplify path handling
Diffstat (limited to 'src/cstore')
-rw-r--r--src/cstore/cstore.hpp1
-rw-r--r--src/cstore/unionfs/cstore-unionfs.cpp113
-rw-r--r--src/cstore/unionfs/cstore-unionfs.hpp12
3 files changed, 63 insertions, 63 deletions
diff --git a/src/cstore/cstore.hpp b/src/cstore/cstore.hpp
index e22bb0d..9a41279 100644
--- a/src/cstore/cstore.hpp
+++ b/src/cstore/cstore.hpp
@@ -327,7 +327,6 @@ private:
virtual void push_tmpl_path_tag() = 0;
virtual string pop_tmpl_path() = 0;
virtual void push_cfg_path(const string& path_comp) = 0;
- virtual void push_cfg_path_val() = 0;
virtual string pop_cfg_path() = 0;
virtual void append_cfg_path(const vector<string>& path_comps) = 0;
virtual void reset_paths() = 0;
diff --git a/src/cstore/unionfs/cstore-unionfs.cpp b/src/cstore/unionfs/cstore-unionfs.cpp
index da5d68e..233a111 100644
--- a/src/cstore/unionfs/cstore-unionfs.cpp
+++ b/src/cstore/unionfs/cstore-unionfs.cpp
@@ -199,7 +199,11 @@ UnionfsCstore::UnionfsCstore(bool use_edit_level)
if ((val = getenv(C_ENV_EDIT_LEVEL.c_str()))) {
mutable_cfg_path = val;
}
- if ((val = getenv(C_ENV_TMPL_LEVEL.c_str()))) {
+ if ((val = getenv(C_ENV_TMPL_LEVEL.c_str())) && val[0] && val[1]) {
+ /* no need to append root (i.e., "/"). level (if exists) always
+ * starts with '/', so only append it if it is at least two chars
+ * (i.e., it is not "/").
+ */
tmpl_path /= val;
}
}
@@ -247,7 +251,8 @@ UnionfsCstore::UnionfsCstore(const string& sid, string& env)
if ((val = getenv(C_ENV_EDIT_LEVEL.c_str()))) {
mutable_cfg_path = val;
}
- if ((val = getenv(C_ENV_TMPL_LEVEL.c_str()))) {
+ if ((val = getenv(C_ENV_TMPL_LEVEL.c_str())) && val[0] && val[1]) {
+ // see comment in the other constructor
tmpl_path /= val;
}
@@ -416,10 +421,9 @@ UnionfsCstore::tmpl_node_exists()
bool
UnionfsCstore::tmpl_parse(vtw_def& def)
{
- push_tmpl_path(C_DEF_NAME);
- bool ret = (b_fs_exists(tmpl_path) && b_fs_is_regular(tmpl_path)
- && parse_def(&def, tmpl_path.file_string().c_str(), 0) == 0);
- pop_tmpl_path();
+ b_fs::path tp = tmpl_path / C_DEF_NAME;
+ bool ret = (b_fs_exists(tp) && b_fs_is_regular(tp)
+ && parse_def(&def, tp.file_string().c_str(), 0) == 0);
return ret;
}
@@ -500,73 +504,66 @@ UnionfsCstore::get_all_child_node_names_impl(vector<string>& cnodes,
bool
UnionfsCstore::read_value_vec(vector<string>& vvec, bool active_cfg)
{
- push_cfg_path_val();
b_fs::path vpath = (active_cfg ? get_active_path() : get_work_path());
- bool ret = false;
- do {
- string ostr;
- if (!read_whole_file(vpath, ostr)) {
- break;
- }
+ vpath /= C_VAL_NAME;
- /* XXX original implementation used to remove a trailing '\n' after
- * a read. it was only necessary because it was adding a '\n' when
- * writing the file. don't remove anything now since we shouldn't
- * be writing it any more.
- */
- // separate values using newline as delimiter
- size_t start_idx = 0, idx = 0;
- for (; idx < ostr.size(); idx++) {
- if (ostr[idx] == '\n') {
- // got a value
- vvec.push_back(ostr.substr(start_idx, (idx - start_idx)));
- start_idx = idx + 1;
- }
- }
- if (start_idx < ostr.size()) {
+ string ostr;
+ if (!read_whole_file(vpath, ostr)) {
+ return false;
+ }
+
+ /* XXX original implementation used to remove a trailing '\n' after
+ * a read. it was only necessary because it was adding a '\n' when
+ * writing the file. don't remove anything now since we shouldn't
+ * be writing it any more.
+ */
+ // separate values using newline as delimiter
+ size_t start_idx = 0, idx = 0;
+ for (; idx < ostr.size(); idx++) {
+ if (ostr[idx] == '\n') {
+ // got a value
vvec.push_back(ostr.substr(start_idx, (idx - start_idx)));
- } else {
- // last char is a newline => another empty value
- vvec.push_back("");
+ start_idx = idx + 1;
}
- ret = true;
- } while (0);
- pop_cfg_path();
- return ret;
+ }
+ if (start_idx < ostr.size()) {
+ vvec.push_back(ostr.substr(start_idx, (idx - start_idx)));
+ } else {
+ // last char is a newline => another empty value
+ vvec.push_back("");
+ }
+ return true;
}
bool
UnionfsCstore::write_value_vec(const vector<string>& vvec, bool active_cfg)
{
- push_cfg_path_val();
- bool ret = false;
b_fs::path wp = (active_cfg ? get_active_path() : get_work_path());
- do {
- if (b_fs_exists(wp) && !b_fs_is_regular(wp)) {
- // not a file
- break;
- }
+ wp /= C_VAL_NAME;
- string ostr = "";
- for (size_t i = 0; i < vvec.size(); i++) {
- if (i > 0) {
- // subsequent values require delimiter
- ostr += "\n";
- }
- ostr += vvec[i];
- }
+ if (b_fs_exists(wp) && !b_fs_is_regular(wp)) {
+ // not a file
+ output_internal("failed to write node value (file) [%s]\n",
+ wp.file_string().c_str());
+ return false;
+ }
- if (!write_file(wp.file_string().c_str(), ostr)) {
- break;
+ string ostr = "";
+ for (size_t i = 0; i < vvec.size(); i++) {
+ if (i > 0) {
+ // subsequent values require delimiter
+ ostr += "\n";
}
- ret = true;
- } while (0);
- pop_cfg_path();
- if (!ret) {
- output_internal("failed to write node value [%s]\n",
+ ostr += vvec[i];
+ }
+
+ if (!write_file(wp.file_string().c_str(), ostr)) {
+ output_internal("failed to write node value (write) [%s]\n",
wp.file_string().c_str());
+ return false;
}
- return ret;
+
+ return true;
}
bool
diff --git a/src/cstore/unionfs/cstore-unionfs.hpp b/src/cstore/unionfs/cstore-unionfs.hpp
index 2dc2c0b..70d4cf6 100644
--- a/src/cstore/unionfs/cstore-unionfs.hpp
+++ b/src/cstore/unionfs/cstore-unionfs.hpp
@@ -95,7 +95,14 @@ private:
push_path(tmpl_path, new_comp);
};
void push_tmpl_path_tag() {
- push_tmpl_path(C_TAG_NAME);
+ /* not using push_path => not "escaped".
+ * NOTE: this is the only interface that can push tmpl_path "unescaped".
+ * this means the pop_tmpl_path() function potentially may return
+ * incorrect value if used in combination with this function.
+ * however, since current C_TAG_NAME doesn't contain any escape
+ * sequences, this cannot happen for now.
+ */
+ tmpl_path /= C_TAG_NAME;
};
string pop_tmpl_path() {
return pop_path(tmpl_path);
@@ -103,9 +110,6 @@ private:
void push_cfg_path(const string& new_comp) {
push_path(mutable_cfg_path, new_comp);
};
- void push_cfg_path_val() {
- push_cfg_path(C_VAL_NAME);
- };
string pop_cfg_path() {
return pop_path(mutable_cfg_path);
};