Revisit the simple_key_value_store into a class.
The simple_key_value_store was implemented as two separated functions
to parse and assemble a string containing several lines of key=value
pairs. The representation of that was passed to the caller as a
map<string, string> who would use the map operations to modify it.
Also, the inteded use for these strings was to parse and write text
files on the filesystem.
This key=value store is used to store strings and boolean values,
and will be reused for the policy manager config provider.
This patch reworks those functions as a class and adds support for
reading and writing boolean values and does the file read and write
operations as well.
BUG=chromium:359674
TEST=Unittest extended.
Change-Id: I4890c4a4ca81c1a4857e9893ea827c3fa7815aab
Reviewed-on: https://chromium-review.googlesource.com/195489
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/omaha_request_params.cc b/omaha_request_params.cc
index 3564b22..667ed46 100644
--- a/omaha_request_params.cc
+++ b/omaha_request_params.cc
@@ -133,19 +133,15 @@
<< ", existing target channel = " << target_channel_
<< ", download channel = " << download_channel_;
TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
+ KeyValueStore lsb_release;
base::FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
- string file_data;
- map<string, string> data;
- if (base::ReadFileToString(kFile, &file_data)) {
- data = simple_key_value_store::ParseString(file_data);
- }
- data[kUpdateChannelKey] = new_target_channel;
- data[kIsPowerwashAllowedKey] = is_powerwash_allowed ? "true" : "false";
- file_data = simple_key_value_store::AssembleString(data);
+
+ lsb_release.Load(kFile.value());
+ lsb_release.SetString(kUpdateChannelKey, new_target_channel);
+ lsb_release.SetBoolean(kIsPowerwashAllowedKey, is_powerwash_allowed);
+
TEST_AND_RETURN_FALSE(base::CreateDirectory(kFile.DirName()));
- TEST_AND_RETURN_FALSE(
- file_util::WriteFile(kFile, file_data.data(), file_data.size()) ==
- static_cast<int>(file_data.size()));
+ TEST_AND_RETURN_FALSE(lsb_release.Save(kFile.value()));
target_channel_ = new_target_channel;
is_powerwash_allowed_ = is_powerwash_allowed;
return true;
@@ -221,13 +217,12 @@
it != files.end(); ++it) {
// TODO(adlr): make sure files checked are owned as root (and all their
// parents are recursively, too).
- string file_data;
- if (!utils::ReadFile(root_ + *it, &file_data))
+ KeyValueStore data;
+ if (!data.Load(root_ + *it))
continue;
- map<string, string> data = simple_key_value_store::ParseString(file_data);
- if (utils::MapContainsKey(data, key)) {
- const string& value = data[key];
+ string value;
+ if (data.GetString(key, &value)) {
if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
continue;
}