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