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