blob: 41a7b245c51c7230318a320d0a9fcdf9d77d71fc [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
Kelvin Zhang49170aa2022-11-28 10:55:16 -080028#include "brillo/file_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/utils.h"
Darin Petkov30030592010-07-27 13:53:20 -070030
31using std::string;
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070032using std::vector;
Darin Petkov30030592010-07-27 13:53:20 -070033
34namespace chromeos_update_engine {
35
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070036namespace {
37
38void DeleteEmptyDirectories(const base::FilePath& path) {
39 base::FileEnumerator path_enum(
40 path, false /* recursive */, base::FileEnumerator::DIRECTORIES);
41 for (base::FilePath dir_path = path_enum.Next(); !dir_path.empty();
42 dir_path = path_enum.Next()) {
43 DeleteEmptyDirectories(dir_path);
44 if (base::IsDirectoryEmpty(dir_path))
hscham043355b2020-11-17 16:50:10 +090045#if BASE_VER < 800000
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070046 base::DeleteFile(dir_path, false);
hscham043355b2020-11-17 16:50:10 +090047#else
48 base::DeleteFile(dir_path);
49#endif
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070050 }
51}
52
53} // namespace
54
Kelvin Zhang1c86a922021-05-13 10:30:48 -040055bool PrefsBase::GetString(const std::string_view key, string* value) const {
Alex Deymoa0284ac2016-07-22 12:51:41 -070056 return storage_->GetKey(key, value);
Darin Petkov30030592010-07-27 13:53:20 -070057}
58
Kelvin Zhang1c86a922021-05-13 10:30:48 -040059bool PrefsBase::SetString(std::string_view key, std::string_view value) {
Alex Deymoa0284ac2016-07-22 12:51:41 -070060 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
Alex Deymod6f60072015-10-12 12:22:27 -070061 const auto observers_for_key = observers_.find(key);
62 if (observers_for_key != observers_.end()) {
63 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
64 for (ObserverInterface* observer : copy_observers)
65 observer->OnPrefSet(key);
66 }
Darin Petkov30030592010-07-27 13:53:20 -070067 return true;
68}
69
Kelvin Zhang1c86a922021-05-13 10:30:48 -040070bool PrefsBase::GetInt64(const std::string_view key, int64_t* value) const {
Darin Petkov30030592010-07-27 13:53:20 -070071 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070072 if (!GetString(key, &str_value))
73 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070074 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070075 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070076 return true;
77}
78
Kelvin Zhang1c86a922021-05-13 10:30:48 -040079bool PrefsBase::SetInt64(std::string_view key, const int64_t value) {
hscham00b6aa22020-02-20 12:32:06 +090080 return SetString(key, base::NumberToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070081}
82
Kelvin Zhang1c86a922021-05-13 10:30:48 -040083bool PrefsBase::GetBoolean(std::string_view key, bool* value) const {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070084 string str_value;
85 if (!GetString(key, &str_value))
86 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070087 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070088 if (str_value == "false") {
89 *value = false;
90 return true;
91 }
92 if (str_value == "true") {
93 *value = true;
94 return true;
95 }
96 return false;
97}
98
Kelvin Zhang1c86a922021-05-13 10:30:48 -040099bool PrefsBase::SetBoolean(std::string_view key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700100 return SetString(key, value ? "true" : "false");
101}
102
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400103bool PrefsBase::Exists(std::string_view key) const {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700104 return storage_->KeyExists(key);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700105}
106
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400107bool PrefsBase::Delete(std::string_view key) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700108 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
Alex Deymod6f60072015-10-12 12:22:27 -0700109 const auto observers_for_key = observers_.find(key);
110 if (observers_for_key != observers_.end()) {
111 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
112 for (ObserverInterface* observer : copy_observers)
113 observer->OnPrefDeleted(key);
114 }
115 return true;
116}
117
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400118bool PrefsBase::Delete(std::string_view pref_key, const vector<string>& nss) {
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800119 // Delete pref key for platform.
120 bool success = Delete(pref_key);
121 // Delete pref key in each namespace.
122 for (const auto& ns : nss) {
123 vector<string> namespace_keys;
124 success = GetSubKeys(ns, &namespace_keys) && success;
125 for (const auto& key : namespace_keys) {
126 auto last_key_seperator = key.find_last_of(kKeySeparator);
127 if (last_key_seperator != string::npos &&
128 pref_key == key.substr(last_key_seperator + 1)) {
129 success = Delete(key) && success;
130 }
131 }
132 }
133 return success;
134}
135
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400136bool PrefsBase::GetSubKeys(std::string_view ns, vector<string>* keys) const {
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700137 return storage_->GetSubKeys(ns, keys);
138}
139
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400140void PrefsBase::AddObserver(std::string_view key, ObserverInterface* observer) {
141 observers_[std::string{key}].push_back(observer);
Alex Deymod6f60072015-10-12 12:22:27 -0700142}
143
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400144void PrefsBase::RemoveObserver(std::string_view key,
145 ObserverInterface* observer) {
146 std::vector<ObserverInterface*>& observers_for_key =
147 observers_[std::string{key}];
Alex Deymod6f60072015-10-12 12:22:27 -0700148 auto observer_it =
149 std::find(observers_for_key.begin(), observers_for_key.end(), observer);
150 if (observer_it != observers_for_key.end())
151 observers_for_key.erase(observer_it);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700152}
153
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700154string PrefsInterface::CreateSubKey(const vector<string>& ns_and_key) {
155 return base::JoinString(ns_and_key, string(1, kKeySeparator));
Andrew065d78d2020-04-07 15:43:07 -0700156}
157
Alex Deymoa0284ac2016-07-22 12:51:41 -0700158// Prefs
159
160bool Prefs::Init(const base::FilePath& prefs_dir) {
161 return file_storage_.Init(prefs_dir);
162}
163
164bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
165 prefs_dir_ = prefs_dir;
Andrew065d78d2020-04-07 15:43:07 -0700166 // Delete empty directories. Ignore errors when deleting empty directories.
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700167 DeleteEmptyDirectories(prefs_dir_);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700168 return true;
169}
170
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400171bool Prefs::FileStorage::GetKey(std::string_view key, string* value) const {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700172 base::FilePath filename;
173 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
174 if (!base::ReadFileToString(filename, value)) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700175 return false;
176 }
177 return true;
178}
179
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400180bool Prefs::FileStorage::GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700181 vector<string>* keys) const {
182 base::FilePath filename;
183 TEST_AND_RETURN_FALSE(GetFileNameForKey(ns, &filename));
184 base::FileEnumerator namespace_enum(
185 prefs_dir_, true, base::FileEnumerator::FILES);
186 for (base::FilePath f = namespace_enum.Next(); !f.empty();
187 f = namespace_enum.Next()) {
188 auto filename_str = filename.value();
189 if (f.value().compare(0, filename_str.length(), filename_str) == 0) {
190 // Only return the key portion excluding the |prefs_dir_| with slash.
191 keys->push_back(f.value().substr(
192 prefs_dir_.AsEndingWithSeparator().value().length()));
193 }
194 }
195 return true;
196}
197
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400198bool Prefs::FileStorage::SetKey(std::string_view key, std::string_view value) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700199 base::FilePath filename;
200 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
201 if (!base::DirectoryExists(filename.DirName())) {
202 // Only attempt to create the directory if it doesn't exist to avoid calls
203 // to parent directories where we might not have permission to write to.
204 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
205 }
Kelvin Zhang49170aa2022-11-28 10:55:16 -0800206 TEST_AND_RETURN_FALSE(
207 utils::WriteStringToFileAtomic(filename.value(), value));
Alex Deymoa0284ac2016-07-22 12:51:41 -0700208 return true;
209}
210
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400211bool Prefs::FileStorage::KeyExists(std::string_view key) const {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700212 base::FilePath filename;
213 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
214 return base::PathExists(filename);
215}
216
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400217bool Prefs::FileStorage::DeleteKey(std::string_view key) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700218 base::FilePath filename;
219 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
hscham043355b2020-11-17 16:50:10 +0900220#if BASE_VER < 800000
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700221 TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false));
hscham043355b2020-11-17 16:50:10 +0900222#else
223 TEST_AND_RETURN_FALSE(base::DeleteFile(filename));
224#endif
Alex Deymoa0284ac2016-07-22 12:51:41 -0700225 return true;
226}
227
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400228bool Prefs::FileStorage::GetFileNameForKey(std::string_view key,
Alex Deymoa0284ac2016-07-22 12:51:41 -0700229 base::FilePath* filename) const {
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700230 // Allows only non-empty keys containing [A-Za-z0-9_-/].
Darin Petkov30030592010-07-27 13:53:20 -0700231 TEST_AND_RETURN_FALSE(!key.empty());
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700232 for (char c : key)
Alex Vakulenko0103c362016-01-20 07:56:15 -0800233 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
Andrew065d78d2020-04-07 15:43:07 -0700234 c == '_' || c == '-' || c == kKeySeparator);
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400235 *filename = prefs_dir_.Append(
236 base::FilePath::StringPieceType(key.data(), key.size()));
Darin Petkov30030592010-07-27 13:53:20 -0700237 return true;
238}
239
Alex Deymoa0284ac2016-07-22 12:51:41 -0700240// MemoryPrefs
241
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400242bool MemoryPrefs::MemoryStorage::GetKey(std::string_view key,
Alex Deymoa0284ac2016-07-22 12:51:41 -0700243 string* value) const {
244 auto it = values_.find(key);
245 if (it == values_.end())
246 return false;
247 *value = it->second;
248 return true;
249}
250
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400251bool MemoryPrefs::MemoryStorage::GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700252 vector<string>* keys) const {
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400253 auto lower_comp = [](const auto& pr, const auto& ns) {
254 return std::string_view{pr.first.data(), ns.length()} < ns;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700255 };
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400256 auto upper_comp = [](const auto& ns, const auto& pr) {
257 return ns < std::string_view{pr.first.data(), ns.length()};
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700258 };
259 auto lower_it =
260 std::lower_bound(begin(values_), end(values_), ns, lower_comp);
261 auto upper_it = std::upper_bound(lower_it, end(values_), ns, upper_comp);
262 while (lower_it != upper_it)
263 keys->push_back((lower_it++)->first);
264 return true;
265}
266
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400267bool MemoryPrefs::MemoryStorage::SetKey(std::string_view key,
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400268 std::string_view value) {
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400269 values_[std::string{key}] = value;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700270 return true;
271}
272
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400273bool MemoryPrefs::MemoryStorage::KeyExists(std::string_view key) const {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700274 return values_.find(key) != values_.end();
275}
276
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400277bool MemoryPrefs::MemoryStorage::DeleteKey(std::string_view key) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700278 auto it = values_.find(key);
Andrew914f5542020-04-21 10:56:33 -0700279 if (it != values_.end())
280 values_.erase(it);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700281 return true;
282}
283
Darin Petkov30030592010-07-27 13:53:20 -0700284} // namespace chromeos_update_engine