blob: 1e06be446d009d702ba88ca8bdc0d1a11d64bf81 [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))
hscham043355b2020-11-17 16:50:10 +090044#if BASE_VER < 800000
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070045 base::DeleteFile(dir_path, false);
hscham043355b2020-11-17 16:50:10 +090046#else
47 base::DeleteFile(dir_path);
48#endif
Jae Hoon Kimc1f36922020-05-11 18:20:18 -070049 }
50}
51
52} // namespace
53
Alex Deymoa0284ac2016-07-22 12:51:41 -070054bool PrefsBase::GetString(const string& key, string* value) const {
55 return storage_->GetKey(key, value);
Darin Petkov30030592010-07-27 13:53:20 -070056}
57
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040058bool PrefsBase::SetString(const string& key, std::string_view value) {
Alex Deymoa0284ac2016-07-22 12:51:41 -070059 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
Alex Deymod6f60072015-10-12 12:22:27 -070060 const auto observers_for_key = observers_.find(key);
61 if (observers_for_key != observers_.end()) {
62 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
63 for (ObserverInterface* observer : copy_observers)
64 observer->OnPrefSet(key);
65 }
Darin Petkov30030592010-07-27 13:53:20 -070066 return true;
67}
68
Alex Deymoa0284ac2016-07-22 12:51:41 -070069bool PrefsBase::GetInt64(const string& key, int64_t* value) const {
Darin Petkov30030592010-07-27 13:53:20 -070070 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070071 if (!GetString(key, &str_value))
72 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070073 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070074 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070075 return true;
76}
77
Alex Deymoa0284ac2016-07-22 12:51:41 -070078bool PrefsBase::SetInt64(const string& key, const int64_t value) {
hscham00b6aa22020-02-20 12:32:06 +090079 return SetString(key, base::NumberToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070080}
81
Alex Deymoa0284ac2016-07-22 12:51:41 -070082bool PrefsBase::GetBoolean(const string& key, bool* value) const {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070083 string str_value;
84 if (!GetString(key, &str_value))
85 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070086 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070087 if (str_value == "false") {
88 *value = false;
89 return true;
90 }
91 if (str_value == "true") {
92 *value = true;
93 return true;
94 }
95 return false;
96}
97
Alex Deymoa0284ac2016-07-22 12:51:41 -070098bool PrefsBase::SetBoolean(const string& key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070099 return SetString(key, value ? "true" : "false");
100}
101
Alex Deymoa0284ac2016-07-22 12:51:41 -0700102bool PrefsBase::Exists(const string& key) const {
103 return storage_->KeyExists(key);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700104}
105
Alex Deymoa0284ac2016-07-22 12:51:41 -0700106bool PrefsBase::Delete(const string& key) {
107 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
Alex Deymod6f60072015-10-12 12:22:27 -0700108 const auto observers_for_key = observers_.find(key);
109 if (observers_for_key != observers_.end()) {
110 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
111 for (ObserverInterface* observer : copy_observers)
112 observer->OnPrefDeleted(key);
113 }
114 return true;
115}
116
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800117bool PrefsBase::Delete(const string& pref_key, const vector<string>& nss) {
118 // Delete pref key for platform.
119 bool success = Delete(pref_key);
120 // Delete pref key in each namespace.
121 for (const auto& ns : nss) {
122 vector<string> namespace_keys;
123 success = GetSubKeys(ns, &namespace_keys) && success;
124 for (const auto& key : namespace_keys) {
125 auto last_key_seperator = key.find_last_of(kKeySeparator);
126 if (last_key_seperator != string::npos &&
127 pref_key == key.substr(last_key_seperator + 1)) {
128 success = Delete(key) && success;
129 }
130 }
131 }
132 return success;
133}
134
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700135bool PrefsBase::GetSubKeys(const string& ns, vector<string>* keys) const {
136 return storage_->GetSubKeys(ns, keys);
137}
138
Alex Deymoa0284ac2016-07-22 12:51:41 -0700139void PrefsBase::AddObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -0700140 observers_[key].push_back(observer);
141}
142
Alex Deymoa0284ac2016-07-22 12:51:41 -0700143void PrefsBase::RemoveObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -0700144 std::vector<ObserverInterface*>& observers_for_key = observers_[key];
145 auto observer_it =
146 std::find(observers_for_key.begin(), observers_for_key.end(), observer);
147 if (observer_it != observers_for_key.end())
148 observers_for_key.erase(observer_it);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700149}
150
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700151string PrefsInterface::CreateSubKey(const vector<string>& ns_and_key) {
152 return base::JoinString(ns_and_key, string(1, kKeySeparator));
Andrew065d78d2020-04-07 15:43:07 -0700153}
154
Alex Deymoa0284ac2016-07-22 12:51:41 -0700155// Prefs
156
157bool Prefs::Init(const base::FilePath& prefs_dir) {
158 return file_storage_.Init(prefs_dir);
159}
160
161bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
162 prefs_dir_ = prefs_dir;
Andrew065d78d2020-04-07 15:43:07 -0700163 // Delete empty directories. Ignore errors when deleting empty directories.
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700164 DeleteEmptyDirectories(prefs_dir_);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700165 return true;
166}
167
168bool Prefs::FileStorage::GetKey(const string& key, string* value) const {
169 base::FilePath filename;
170 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
171 if (!base::ReadFileToString(filename, value)) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700172 return false;
173 }
174 return true;
175}
176
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700177bool Prefs::FileStorage::GetSubKeys(const string& ns,
178 vector<string>* keys) const {
179 base::FilePath filename;
180 TEST_AND_RETURN_FALSE(GetFileNameForKey(ns, &filename));
181 base::FileEnumerator namespace_enum(
182 prefs_dir_, true, base::FileEnumerator::FILES);
183 for (base::FilePath f = namespace_enum.Next(); !f.empty();
184 f = namespace_enum.Next()) {
185 auto filename_str = filename.value();
186 if (f.value().compare(0, filename_str.length(), filename_str) == 0) {
187 // Only return the key portion excluding the |prefs_dir_| with slash.
188 keys->push_back(f.value().substr(
189 prefs_dir_.AsEndingWithSeparator().value().length()));
190 }
191 }
192 return true;
193}
194
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400195bool Prefs::FileStorage::SetKey(const string& key, std::string_view value) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700196 base::FilePath filename;
197 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
198 if (!base::DirectoryExists(filename.DirName())) {
199 // Only attempt to create the directory if it doesn't exist to avoid calls
200 // to parent directories where we might not have permission to write to.
201 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
202 }
203 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
204 static_cast<int>(value.size()));
205 return true;
206}
207
208bool Prefs::FileStorage::KeyExists(const string& key) const {
209 base::FilePath filename;
210 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
211 return base::PathExists(filename);
212}
213
214bool Prefs::FileStorage::DeleteKey(const string& key) {
215 base::FilePath filename;
216 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
hscham043355b2020-11-17 16:50:10 +0900217#if BASE_VER < 800000
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700218 TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false));
hscham043355b2020-11-17 16:50:10 +0900219#else
220 TEST_AND_RETURN_FALSE(base::DeleteFile(filename));
221#endif
Alex Deymoa0284ac2016-07-22 12:51:41 -0700222 return true;
223}
224
225bool Prefs::FileStorage::GetFileNameForKey(const string& key,
226 base::FilePath* filename) const {
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700227 // Allows only non-empty keys containing [A-Za-z0-9_-/].
Darin Petkov30030592010-07-27 13:53:20 -0700228 TEST_AND_RETURN_FALSE(!key.empty());
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700229 for (char c : key)
Alex Vakulenko0103c362016-01-20 07:56:15 -0800230 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
Andrew065d78d2020-04-07 15:43:07 -0700231 c == '_' || c == '-' || c == kKeySeparator);
Darin Petkov30030592010-07-27 13:53:20 -0700232 *filename = prefs_dir_.Append(key);
233 return true;
234}
235
Alex Deymoa0284ac2016-07-22 12:51:41 -0700236// MemoryPrefs
237
238bool MemoryPrefs::MemoryStorage::GetKey(const string& key,
239 string* value) const {
240 auto it = values_.find(key);
241 if (it == values_.end())
242 return false;
243 *value = it->second;
244 return true;
245}
246
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700247bool MemoryPrefs::MemoryStorage::GetSubKeys(const string& ns,
248 vector<string>* keys) const {
249 using value_type = decltype(values_)::value_type;
250 using key_type = decltype(values_)::key_type;
251 auto lower_comp = [](const value_type& pr, const key_type& ns) {
252 return pr.first.substr(0, ns.length()) < ns;
253 };
254 auto upper_comp = [](const key_type& ns, const value_type& pr) {
255 return ns < pr.first.substr(0, ns.length());
256 };
257 auto lower_it =
258 std::lower_bound(begin(values_), end(values_), ns, lower_comp);
259 auto upper_it = std::upper_bound(lower_it, end(values_), ns, upper_comp);
260 while (lower_it != upper_it)
261 keys->push_back((lower_it++)->first);
262 return true;
263}
264
Alex Deymoa0284ac2016-07-22 12:51:41 -0700265bool MemoryPrefs::MemoryStorage::SetKey(const string& key,
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400266 std::string_view value) {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700267 values_[key] = value;
268 return true;
269}
270
271bool MemoryPrefs::MemoryStorage::KeyExists(const string& key) const {
272 return values_.find(key) != values_.end();
273}
274
275bool MemoryPrefs::MemoryStorage::DeleteKey(const string& key) {
276 auto it = values_.find(key);
Andrew914f5542020-04-21 10:56:33 -0700277 if (it != values_.end())
278 values_.erase(it);
Alex Deymoa0284ac2016-07-22 12:51:41 -0700279 return true;
280}
281
Darin Petkov30030592010-07-27 13:53:20 -0700282} // namespace chromeos_update_engine