blob: 52a58b71b6e26a31f348cbac6a6f39765cabe5f2 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Darin Petkov30030592010-07-27 13:53:20 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/prefs.h"
Darin Petkov30030592010-07-27 13:53:20 -070018
Alex Deymod6f60072015-10-12 12:22:27 -070019#include <algorithm>
20
Andrew065d78d2020-04-07 15:43:07 -070021#include <base/files/file_enumerator.h>
Ben Chan06c76a42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070023#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/strings/string_number_conversions.h>
Andrew065d78d2020-04-07 15:43:07 -070025#include <base/strings/string_split.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/string_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/utils.h"
Darin Petkov30030592010-07-27 13:53:20 -070029
30using std::string;
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070031using std::vector;
Darin Petkov30030592010-07-27 13:53:20 -070032
33namespace chromeos_update_engine {
34
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070035namespace {
36
37void DeleteEmptyDirectories(const base::FilePath& path) {
38 base::FileEnumerator path_enum(
39 path, false /* recursive */, base::FileEnumerator::DIRECTORIES);
40 for (base::FilePath dir_path = path_enum.Next(); !dir_path.empty();
41 dir_path = path_enum.Next()) {
42 DeleteEmptyDirectories(dir_path);
43 if (base::IsDirectoryEmpty(dir_path))
44 base::DeleteFile(dir_path, false);
45 }
46}
47
48} // namespace
49
Alex Deymoa0284ac2016-07-22 12:51:41 -070050bool PrefsBase::GetString(const string& key, string* value) const {
51 return storage_->GetKey(key, value);
Darin Petkov30030592010-07-27 13:53:20 -070052}
53
Alex Deymoa0284ac2016-07-22 12:51:41 -070054bool PrefsBase::SetString(const string& key, const string& value) {
55 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
Alex Deymod6f60072015-10-12 12:22:27 -070056 const auto observers_for_key = observers_.find(key);
57 if (observers_for_key != observers_.end()) {
58 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
59 for (ObserverInterface* observer : copy_observers)
60 observer->OnPrefSet(key);
61 }
Darin Petkov30030592010-07-27 13:53:20 -070062 return true;
63}
64
Alex Deymoa0284ac2016-07-22 12:51:41 -070065bool PrefsBase::GetInt64(const string& key, int64_t* value) const {
Darin Petkov30030592010-07-27 13:53:20 -070066 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070067 if (!GetString(key, &str_value))
68 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070069 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070070 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070071 return true;
72}
73
Alex Deymoa0284ac2016-07-22 12:51:41 -070074bool PrefsBase::SetInt64(const string& key, const int64_t value) {
hscham00b6aa22020-02-20 12:32:06 +090075 return SetString(key, base::NumberToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070076}
77
Alex Deymoa0284ac2016-07-22 12:51:41 -070078bool PrefsBase::GetBoolean(const string& key, bool* value) const {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070079 string str_value;
80 if (!GetString(key, &str_value))
81 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070082 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070083 if (str_value == "false") {
84 *value = false;
85 return true;
86 }
87 if (str_value == "true") {
88 *value = true;
89 return true;
90 }
91 return false;
92}
93
Alex Deymoa0284ac2016-07-22 12:51:41 -070094bool PrefsBase::SetBoolean(const string& key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070095 return SetString(key, value ? "true" : "false");
96}
97
Alex Deymoa0284ac2016-07-22 12:51:41 -070098bool PrefsBase::Exists(const string& key) const {
99 return storage_->KeyExists(key);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700100}
101
Alex Deymoa0284ac2016-07-22 12:51:41 -0700102bool PrefsBase::Delete(const string& key) {
103 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
Alex Deymod6f60072015-10-12 12:22:27 -0700104 const auto observers_for_key = observers_.find(key);
105 if (observers_for_key != observers_.end()) {
106 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
107 for (ObserverInterface* observer : copy_observers)
108 observer->OnPrefDeleted(key);
109 }
110 return true;
111}
112
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800113bool PrefsBase::Delete(const string& pref_key, const vector<string>& nss) {
114 // Delete pref key for platform.
115 bool success = Delete(pref_key);
116 // Delete pref key in each namespace.
117 for (const auto& ns : nss) {
118 vector<string> namespace_keys;
119 success = GetSubKeys(ns, &namespace_keys) && success;
120 for (const auto& key : namespace_keys) {
121 auto last_key_seperator = key.find_last_of(kKeySeparator);
122 if (last_key_seperator != string::npos &&
123 pref_key == key.substr(last_key_seperator + 1)) {
124 success = Delete(key) && success;
125 }
126 }
127 }
128 return success;
129}
130
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700131bool PrefsBase::GetSubKeys(const string& ns, vector<string>* keys) const {
132 return storage_->GetSubKeys(ns, keys);
133}
134
Alex Deymoa0284ac2016-07-22 12:51:41 -0700135void PrefsBase::AddObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -0700136 observers_[key].push_back(observer);
137}
138
Alex Deymoa0284ac2016-07-22 12:51:41 -0700139void PrefsBase::RemoveObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -0700140 std::vector<ObserverInterface*>& observers_for_key = observers_[key];
141 auto observer_it =
142 std::find(observers_for_key.begin(), observers_for_key.end(), observer);
143 if (observer_it != observers_for_key.end())
144 observers_for_key.erase(observer_it);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700145}
146
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700147string PrefsInterface::CreateSubKey(const vector<string>& ns_and_key) {
148 return base::JoinString(ns_and_key, string(1, kKeySeparator));
Andrew065d78d2020-04-07 15:43:07 -0700149}
150
Alex Deymoa0284ac2016-07-22 12:51:41 -0700151// Prefs
152
153bool Prefs::Init(const base::FilePath& prefs_dir) {
154 return file_storage_.Init(prefs_dir);
155}
156
157bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
158 prefs_dir_ = prefs_dir;
Andrew065d78d2020-04-07 15:43:07 -0700159 // Delete empty directories. Ignore errors when deleting empty directories.
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700160 DeleteEmptyDirectories(prefs_dir_);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700161 return true;
162}
163
164bool Prefs::FileStorage::GetKey(const string& key, string* value) const {
165 base::FilePath filename;
166 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
167 if (!base::ReadFileToString(filename, value)) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700168 return false;
169 }
170 return true;
171}
172
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700173bool Prefs::FileStorage::GetSubKeys(const string& ns,
174 vector<string>* keys) const {
175 base::FilePath filename;
176 TEST_AND_RETURN_FALSE(GetFileNameForKey(ns, &filename));
177 base::FileEnumerator namespace_enum(
178 prefs_dir_, true, base::FileEnumerator::FILES);
179 for (base::FilePath f = namespace_enum.Next(); !f.empty();
180 f = namespace_enum.Next()) {
181 auto filename_str = filename.value();
182 if (f.value().compare(0, filename_str.length(), filename_str) == 0) {
183 // Only return the key portion excluding the |prefs_dir_| with slash.
184 keys->push_back(f.value().substr(
185 prefs_dir_.AsEndingWithSeparator().value().length()));
186 }
187 }
188 return true;
189}
190
Alex Deymoa0284ac2016-07-22 12:51:41 -0700191bool Prefs::FileStorage::SetKey(const string& key, const string& value) {
192 base::FilePath filename;
193 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
194 if (!base::DirectoryExists(filename.DirName())) {
195 // Only attempt to create the directory if it doesn't exist to avoid calls
196 // to parent directories where we might not have permission to write to.
197 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
198 }
199 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
200 static_cast<int>(value.size()));
201 return true;
202}
203
204bool Prefs::FileStorage::KeyExists(const string& key) const {
205 base::FilePath filename;
206 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
207 return base::PathExists(filename);
208}
209
210bool Prefs::FileStorage::DeleteKey(const string& key) {
211 base::FilePath filename;
212 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700213 TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false));
Alex Deymoa0284ac2016-07-22 12:51:41 -0700214 return true;
215}
216
217bool Prefs::FileStorage::GetFileNameForKey(const string& key,
218 base::FilePath* filename) const {
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700219 // Allows only non-empty keys containing [A-Za-z0-9_-/].
Darin Petkov30030592010-07-27 13:53:20 -0700220 TEST_AND_RETURN_FALSE(!key.empty());
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700221 for (char c : key)
Alex Vakulenko0103c362016-01-20 07:56:15 -0800222 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
Andrew065d78d2020-04-07 15:43:07 -0700223 c == '_' || c == '-' || c == kKeySeparator);
Darin Petkov30030592010-07-27 13:53:20 -0700224 *filename = prefs_dir_.Append(key);
225 return true;
226}
227
Alex Deymoa0284ac2016-07-22 12:51:41 -0700228// MemoryPrefs
229
230bool MemoryPrefs::MemoryStorage::GetKey(const string& key,
231 string* value) const {
232 auto it = values_.find(key);
233 if (it == values_.end())
234 return false;
235 *value = it->second;
236 return true;
237}
238
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700239bool MemoryPrefs::MemoryStorage::GetSubKeys(const string& ns,
240 vector<string>* keys) const {
241 using value_type = decltype(values_)::value_type;
242 using key_type = decltype(values_)::key_type;
243 auto lower_comp = [](const value_type& pr, const key_type& ns) {
244 return pr.first.substr(0, ns.length()) < ns;
245 };
246 auto upper_comp = [](const key_type& ns, const value_type& pr) {
247 return ns < pr.first.substr(0, ns.length());
248 };
249 auto lower_it =
250 std::lower_bound(begin(values_), end(values_), ns, lower_comp);
251 auto upper_it = std::upper_bound(lower_it, end(values_), ns, upper_comp);
252 while (lower_it != upper_it)
253 keys->push_back((lower_it++)->first);
254 return true;
255}
256
Alex Deymoa0284ac2016-07-22 12:51:41 -0700257bool MemoryPrefs::MemoryStorage::SetKey(const string& key,
258 const string& value) {
259 values_[key] = value;
260 return true;
261}
262
263bool MemoryPrefs::MemoryStorage::KeyExists(const string& key) const {
264 return values_.find(key) != values_.end();
265}
266
267bool MemoryPrefs::MemoryStorage::DeleteKey(const string& key) {
268 auto it = values_.find(key);
Andrew914f5542020-04-21 10:56:33 -0700269 if (it != values_.end())
270 values_.erase(it);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700271 return true;
272}
273
Darin Petkov30030592010-07-27 13:53:20 -0700274} // namespace chromeos_update_engine