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 #include <filesystem>
21 #include <unistd.h>
22
23 #include <android-base/file.h>
24 #include <base/files/file_enumerator.h>
25 #include <base/files/file_util.h>
26 #include <base/logging.h>
27 #include <base/strings/string_number_conversions.h>
28 #include <base/strings/string_split.h>
29 #include <base/strings/string_util.h>
30
31 #include "update_engine/common/utils.h"
32
33 using std::string;
34 using std::vector;
35
36 namespace chromeos_update_engine {
37
38 namespace {
39
DeleteEmptyDirectories(const base::FilePath & path)40 void DeleteEmptyDirectories(const base::FilePath& path) {
41 base::FileEnumerator path_enum(
42 path, false /* recursive */, base::FileEnumerator::DIRECTORIES);
43 for (base::FilePath dir_path = path_enum.Next(); !dir_path.empty();
44 dir_path = path_enum.Next()) {
45 DeleteEmptyDirectories(dir_path);
46 if (base::IsDirectoryEmpty(dir_path))
47 #if BASE_VER < 800000
48 base::DeleteFile(dir_path, false);
49 #else
50 base::DeleteFile(dir_path);
51 #endif
52 }
53 }
54
55 } // namespace
56
GetString(const std::string_view key,string * value) const57 bool PrefsBase::GetString(const std::string_view key, string* value) const {
58 return storage_->GetKey(key, value);
59 }
60
SetString(std::string_view key,std::string_view value)61 bool PrefsBase::SetString(std::string_view key, std::string_view value) {
62 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
63 const auto observers_for_key = observers_.find(key);
64 if (observers_for_key != observers_.end()) {
65 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
66 for (ObserverInterface* observer : copy_observers)
67 observer->OnPrefSet(key);
68 }
69 return true;
70 }
71
GetInt64(const std::string_view key,int64_t * value) const72 bool PrefsBase::GetInt64(const std::string_view key, int64_t* value) const {
73 string str_value;
74 if (!GetString(key, &str_value))
75 return false;
76 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
77 if (str_value.empty()) {
78 LOG(ERROR) << "When reading pref " << key
79 << ", got an empty value after trim";
80 return false;
81 }
82 if (!base::StringToInt64(str_value, value)) {
83 LOG(ERROR) << "When reading pref " << key << ", failed to convert value "
84 << str_value << " to integer";
85 return false;
86 }
87 return true;
88 }
89
SetInt64(std::string_view key,const int64_t value)90 bool PrefsBase::SetInt64(std::string_view key, const int64_t value) {
91 return SetString(key, base::NumberToString(value));
92 }
93
GetBoolean(std::string_view key,bool * value) const94 bool PrefsBase::GetBoolean(std::string_view key, bool* value) const {
95 string str_value;
96 if (!GetString(key, &str_value))
97 return false;
98 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
99 if (str_value == "false") {
100 *value = false;
101 return true;
102 }
103 if (str_value == "true") {
104 *value = true;
105 return true;
106 }
107 return false;
108 }
109
SetBoolean(std::string_view key,const bool value)110 bool PrefsBase::SetBoolean(std::string_view key, const bool value) {
111 return SetString(key, value ? "true" : "false");
112 }
113
Exists(std::string_view key) const114 bool PrefsBase::Exists(std::string_view key) const {
115 return storage_->KeyExists(key);
116 }
117
Delete(std::string_view key)118 bool PrefsBase::Delete(std::string_view key) {
119 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
120 const auto observers_for_key = observers_.find(key);
121 if (observers_for_key != observers_.end()) {
122 std::vector<ObserverInterface*> copy_observers(observers_for_key->second);
123 for (ObserverInterface* observer : copy_observers)
124 observer->OnPrefDeleted(key);
125 }
126 return true;
127 }
128
Delete(std::string_view pref_key,const vector<string> & nss)129 bool PrefsBase::Delete(std::string_view pref_key, const vector<string>& nss) {
130 // Delete pref key for platform.
131 bool success = Delete(pref_key);
132 // Delete pref key in each namespace.
133 for (const auto& ns : nss) {
134 vector<string> namespace_keys;
135 success = GetSubKeys(ns, &namespace_keys) && success;
136 for (const auto& key : namespace_keys) {
137 auto last_key_seperator = key.find_last_of(kKeySeparator);
138 if (last_key_seperator != string::npos &&
139 pref_key == key.substr(last_key_seperator + 1)) {
140 success = Delete(key) && success;
141 }
142 }
143 }
144 return success;
145 }
146
GetSubKeys(std::string_view ns,vector<string> * keys) const147 bool PrefsBase::GetSubKeys(std::string_view ns, vector<string>* keys) const {
148 return storage_->GetSubKeys(ns, keys);
149 }
150
AddObserver(std::string_view key,ObserverInterface * observer)151 void PrefsBase::AddObserver(std::string_view key, ObserverInterface* observer) {
152 observers_[std::string{key}].push_back(observer);
153 }
154
RemoveObserver(std::string_view key,ObserverInterface * observer)155 void PrefsBase::RemoveObserver(std::string_view key,
156 ObserverInterface* observer) {
157 std::vector<ObserverInterface*>& observers_for_key =
158 observers_[std::string{key}];
159 auto observer_it =
160 std::find(observers_for_key.begin(), observers_for_key.end(), observer);
161 if (observer_it != observers_for_key.end())
162 observers_for_key.erase(observer_it);
163 }
164
CreateSubKey(const vector<string> & ns_and_key)165 string PrefsInterface::CreateSubKey(const vector<string>& ns_and_key) {
166 return base::JoinString(ns_and_key, string(1, kKeySeparator));
167 }
168
169 // Prefs
170
Init(const base::FilePath & prefs_dir)171 bool Prefs::Init(const base::FilePath& prefs_dir) {
172 return file_storage_.Init(prefs_dir);
173 }
174
StartTransaction()175 bool PrefsBase::StartTransaction() {
176 return storage_->CreateTemporaryPrefs();
177 }
178
CancelTransaction()179 bool PrefsBase::CancelTransaction() {
180 return storage_->DeleteTemporaryPrefs();
181 }
182
SubmitTransaction()183 bool PrefsBase::SubmitTransaction() {
184 return storage_->SwapPrefs();
185 }
186
GetTemporaryDir() const187 std::string Prefs::FileStorage::GetTemporaryDir() const {
188 return prefs_dir_.value() + "_tmp";
189 }
190
CreateTemporaryPrefs()191 bool Prefs::FileStorage::CreateTemporaryPrefs() {
192 // Delete any existing prefs_tmp
193 DeleteTemporaryPrefs();
194 // Get the paths to the source and destination directories.
195 std::filesystem::path source_directory(prefs_dir_.value());
196 std::filesystem::path destination_directory(GetTemporaryDir());
197
198 if (!std::filesystem::exists(source_directory)) {
199 LOG(ERROR) << "prefs directory does not exist: " << source_directory;
200 return false;
201 }
202 // Copy the directory.
203 std::filesystem::copy(source_directory, destination_directory);
204
205 return true;
206 }
207
DeleteTemporaryPrefs()208 bool Prefs::FileStorage::DeleteTemporaryPrefs() {
209 std::filesystem::path destination_directory(GetTemporaryDir());
210
211 if (std::filesystem::exists(destination_directory)) {
212 return std::filesystem::remove_all(destination_directory);
213 }
214 return true;
215 }
216
SwapPrefs()217 bool Prefs::FileStorage::SwapPrefs() {
218 if (!utils::DeleteDirectory(prefs_dir_.value().c_str())) {
219 LOG(ERROR) << "Failed to remove prefs dir " << prefs_dir_;
220 return false;
221 }
222 if (rename(GetTemporaryDir().c_str(), prefs_dir_.value().c_str()) != 0) {
223 LOG(ERROR) << "Error replacing prefs with prefs_tmp" << strerror(errno);
224 return false;
225 }
226 if (!utils::FsyncDirectory(
227 android::base::Dirname(prefs_dir_.value()).c_str())) {
228 PLOG(ERROR) << "Failed to fsync prefs parent dir after swapping prefs";
229 }
230 return true;
231 }
232
Init(const base::FilePath & prefs_dir)233 bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
234 prefs_dir_ = prefs_dir;
235 if (!std::filesystem::exists(prefs_dir_.value())) {
236 LOG(INFO) << "Prefs dir does not exist, possibly due to an interrupted "
237 "transaction.";
238 if (std::filesystem::exists(GetTemporaryDir())) {
239 SwapPrefs();
240 }
241 }
242
243 if (std::filesystem::exists(GetTemporaryDir())) {
244 LOG(INFO)
245 << "Deleting temporary prefs, checkpoint transaction was interrupted";
246 if (!utils::DeleteDirectory(GetTemporaryDir().c_str())) {
247 LOG(ERROR) << "Failed to delete temporary prefs";
248 return false;
249 }
250 }
251
252 // Delete empty directories. Ignore errors when deleting empty directories.
253 DeleteEmptyDirectories(prefs_dir_);
254 return true;
255 }
256
GetKey(std::string_view key,string * value) const257 bool Prefs::FileStorage::GetKey(std::string_view key, string* value) const {
258 base::FilePath filename;
259 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
260 if (!base::ReadFileToString(filename, value)) {
261 return false;
262 }
263 return true;
264 }
265
GetSubKeys(std::string_view ns,vector<string> * keys) const266 bool Prefs::FileStorage::GetSubKeys(std::string_view ns,
267 vector<string>* keys) const {
268 base::FilePath filename;
269 TEST_AND_RETURN_FALSE(GetFileNameForKey(ns, &filename));
270 base::FileEnumerator namespace_enum(
271 prefs_dir_, true, base::FileEnumerator::FILES);
272 for (base::FilePath f = namespace_enum.Next(); !f.empty();
273 f = namespace_enum.Next()) {
274 auto filename_str = filename.value();
275 if (f.value().compare(0, filename_str.length(), filename_str) == 0) {
276 // Only return the key portion excluding the |prefs_dir_| with slash.
277 keys->push_back(f.value().substr(
278 prefs_dir_.AsEndingWithSeparator().value().length()));
279 }
280 }
281 return true;
282 }
283
SetKey(std::string_view key,std::string_view value)284 bool Prefs::FileStorage::SetKey(std::string_view key, std::string_view value) {
285 base::FilePath filename;
286 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
287 if (!base::DirectoryExists(filename.DirName())) {
288 // Only attempt to create the directory if it doesn't exist to avoid calls
289 // to parent directories where we might not have permission to write to.
290 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
291 }
292 TEST_AND_RETURN_FALSE(
293 utils::WriteStringToFileAtomic(filename.value(), value));
294 return true;
295 }
296
KeyExists(std::string_view key) const297 bool Prefs::FileStorage::KeyExists(std::string_view key) const {
298 base::FilePath filename;
299 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
300 return base::PathExists(filename);
301 }
302
DeleteKey(std::string_view key)303 bool Prefs::FileStorage::DeleteKey(std::string_view key) {
304 base::FilePath filename;
305 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
306 #if BASE_VER < 800000
307 TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false));
308 #else
309 TEST_AND_RETURN_FALSE(base::DeleteFile(filename));
310 #endif
311 return true;
312 }
313
GetFileNameForKey(std::string_view key,base::FilePath * filename) const314 bool Prefs::FileStorage::GetFileNameForKey(std::string_view key,
315 base::FilePath* filename) const {
316 // Allows only non-empty keys containing [A-Za-z0-9_-/].
317 TEST_AND_RETURN_FALSE(!key.empty());
318 for (char c : key)
319 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
320 c == '_' || c == '-' || c == kKeySeparator);
321 if (std::filesystem::exists(GetTemporaryDir())) {
322 *filename =
323 base::FilePath(GetTemporaryDir())
324 .Append(base::FilePath::StringPieceType(key.data(), key.size()));
325 } else {
326 *filename = prefs_dir_.Append(
327 base::FilePath::StringPieceType(key.data(), key.size()));
328 }
329 return true;
330 }
331
332 // MemoryPrefs
333
GetKey(std::string_view key,string * value) const334 bool MemoryPrefs::MemoryStorage::GetKey(std::string_view key,
335 string* value) const {
336 auto it = values_.find(key);
337 if (it == values_.end())
338 return false;
339 *value = it->second;
340 return true;
341 }
342
GetSubKeys(std::string_view ns,vector<string> * keys) const343 bool MemoryPrefs::MemoryStorage::GetSubKeys(std::string_view ns,
344 vector<string>* keys) const {
345 auto lower_comp = [](const auto& pr, const auto& ns) {
346 return std::string_view{pr.first.data(), ns.length()} < ns;
347 };
348 auto upper_comp = [](const auto& ns, const auto& pr) {
349 return ns < std::string_view{pr.first.data(), ns.length()};
350 };
351 auto lower_it =
352 std::lower_bound(begin(values_), end(values_), ns, lower_comp);
353 auto upper_it = std::upper_bound(lower_it, end(values_), ns, upper_comp);
354 while (lower_it != upper_it)
355 keys->push_back((lower_it++)->first);
356 return true;
357 }
358
SetKey(std::string_view key,std::string_view value)359 bool MemoryPrefs::MemoryStorage::SetKey(std::string_view key,
360 std::string_view value) {
361 values_[std::string{key}] = value;
362 return true;
363 }
364
KeyExists(std::string_view key) const365 bool MemoryPrefs::MemoryStorage::KeyExists(std::string_view key) const {
366 return values_.find(key) != values_.end();
367 }
368
DeleteKey(std::string_view key)369 bool MemoryPrefs::MemoryStorage::DeleteKey(std::string_view key) {
370 auto it = values_.find(key);
371 if (it != values_.end())
372 values_.erase(it);
373 return true;
374 }
375
376 } // namespace chromeos_update_engine
377