1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/prefs.h"
18
19 #include <algorithm>
20
21 #include <base/files/file_util.h>
22 #include <base/logging.h>
23 #include <base/strings/string_number_conversions.h>
24 #include <base/strings/string_util.h>
25
26 #include "update_engine/common/utils.h"
27
28 using std::string;
29
30 namespace chromeos_update_engine {
31
GetString(const string & key,string * value) const32 bool PrefsBase::GetString(const string& key, string* value) const {
33 return storage_->GetKey(key, value);
34 }
35
SetString(const string & key,const string & value)36 bool PrefsBase::SetString(const string& key, const string& value) {
37 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
38 const auto observers_for_key = observers_.find(key);
39 if (observers_for_key != observers_.end()) {
40 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
41 for (ObserverInterface* observer : copy_observers)
42 observer->OnPrefSet(key);
43 }
44 return true;
45 }
46
GetInt64(const string & key,int64_t * value) const47 bool PrefsBase::GetInt64(const string& key, int64_t* value) const {
48 string str_value;
49 if (!GetString(key, &str_value))
50 return false;
51 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
52 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
53 return true;
54 }
55
SetInt64(const string & key,const int64_t value)56 bool PrefsBase::SetInt64(const string& key, const int64_t value) {
57 return SetString(key, base::Int64ToString(value));
58 }
59
GetBoolean(const string & key,bool * value) const60 bool PrefsBase::GetBoolean(const string& key, bool* value) const {
61 string str_value;
62 if (!GetString(key, &str_value))
63 return false;
64 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
65 if (str_value == "false") {
66 *value = false;
67 return true;
68 }
69 if (str_value == "true") {
70 *value = true;
71 return true;
72 }
73 return false;
74 }
75
SetBoolean(const string & key,const bool value)76 bool PrefsBase::SetBoolean(const string& key, const bool value) {
77 return SetString(key, value ? "true" : "false");
78 }
79
Exists(const string & key) const80 bool PrefsBase::Exists(const string& key) const {
81 return storage_->KeyExists(key);
82 }
83
Delete(const string & key)84 bool PrefsBase::Delete(const string& key) {
85 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
86 const auto observers_for_key = observers_.find(key);
87 if (observers_for_key != observers_.end()) {
88 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
89 for (ObserverInterface* observer : copy_observers)
90 observer->OnPrefDeleted(key);
91 }
92 return true;
93 }
94
AddObserver(const string & key,ObserverInterface * observer)95 void PrefsBase::AddObserver(const string& key, ObserverInterface* observer) {
96 observers_[key].push_back(observer);
97 }
98
RemoveObserver(const string & key,ObserverInterface * observer)99 void PrefsBase::RemoveObserver(const string& key, ObserverInterface* observer) {
100 std::vector<ObserverInterface*>& observers_for_key = observers_[key];
101 auto observer_it =
102 std::find(observers_for_key.begin(), observers_for_key.end(), observer);
103 if (observer_it != observers_for_key.end())
104 observers_for_key.erase(observer_it);
105 }
106
107 // Prefs
108
Init(const base::FilePath & prefs_dir)109 bool Prefs::Init(const base::FilePath& prefs_dir) {
110 return file_storage_.Init(prefs_dir);
111 }
112
Init(const base::FilePath & prefs_dir)113 bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
114 prefs_dir_ = prefs_dir;
115 return true;
116 }
117
GetKey(const string & key,string * value) const118 bool Prefs::FileStorage::GetKey(const string& key, string* value) const {
119 base::FilePath filename;
120 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
121 if (!base::ReadFileToString(filename, value)) {
122 LOG(INFO) << key << " not present in " << prefs_dir_.value();
123 return false;
124 }
125 return true;
126 }
127
SetKey(const string & key,const string & value)128 bool Prefs::FileStorage::SetKey(const string& key, const string& value) {
129 base::FilePath filename;
130 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
131 if (!base::DirectoryExists(filename.DirName())) {
132 // Only attempt to create the directory if it doesn't exist to avoid calls
133 // to parent directories where we might not have permission to write to.
134 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
135 }
136 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
137 static_cast<int>(value.size()));
138 return true;
139 }
140
KeyExists(const string & key) const141 bool Prefs::FileStorage::KeyExists(const string& key) const {
142 base::FilePath filename;
143 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
144 return base::PathExists(filename);
145 }
146
DeleteKey(const string & key)147 bool Prefs::FileStorage::DeleteKey(const string& key) {
148 base::FilePath filename;
149 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
150 TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false));
151 return true;
152 }
153
GetFileNameForKey(const string & key,base::FilePath * filename) const154 bool Prefs::FileStorage::GetFileNameForKey(const string& key,
155 base::FilePath* filename) const {
156 // Allows only non-empty keys containing [A-Za-z0-9_-].
157 TEST_AND_RETURN_FALSE(!key.empty());
158 for (size_t i = 0; i < key.size(); ++i) {
159 char c = key.at(i);
160 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
161 c == '_' || c == '-');
162 }
163 *filename = prefs_dir_.Append(key);
164 return true;
165 }
166
167 // MemoryPrefs
168
GetKey(const string & key,string * value) const169 bool MemoryPrefs::MemoryStorage::GetKey(const string& key,
170 string* value) const {
171 auto it = values_.find(key);
172 if (it == values_.end())
173 return false;
174 *value = it->second;
175 return true;
176 }
177
SetKey(const string & key,const string & value)178 bool MemoryPrefs::MemoryStorage::SetKey(const string& key,
179 const string& value) {
180 values_[key] = value;
181 return true;
182 }
183
KeyExists(const string & key) const184 bool MemoryPrefs::MemoryStorage::KeyExists(const string& key) const {
185 return values_.find(key) != values_.end();
186 }
187
DeleteKey(const string & key)188 bool MemoryPrefs::MemoryStorage::DeleteKey(const string& key) {
189 auto it = values_.find(key);
190 if (it == values_.end())
191 return false;
192 values_.erase(it);
193 return true;
194 }
195
196 } // namespace chromeos_update_engine
197