|
20 | 20 | #include <filesystem>
|
21 | 21 | #include <unistd.h>
|
22 | 22 |
|
| 23 | +#include <android-base/file.h> |
23 | 24 | #include <base/files/file_enumerator.h>
|
24 | 25 | #include <base/files/file_util.h>
|
25 | 26 | #include <base/logging.h>
|
26 | 27 | #include <base/strings/string_number_conversions.h>
|
27 | 28 | #include <base/strings/string_split.h>
|
28 | 29 | #include <base/strings/string_util.h>
|
29 | 30 |
|
30 |
| -#include "update_engine/common/platform_constants.h" |
31 | 31 | #include "update_engine/common/utils.h"
|
32 | 32 |
|
33 | 33 | using std::string;
|
@@ -74,7 +74,16 @@ bool PrefsBase::GetInt64(const std::string_view key, int64_t* value) const {
|
74 | 74 | if (!GetString(key, &str_value))
|
75 | 75 | return false;
|
76 | 76 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
|
77 |
| - TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, 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 | + } |
78 | 87 | return true;
|
79 | 88 | }
|
80 | 89 |
|
@@ -206,18 +215,30 @@ bool Prefs::FileStorage::DeleteTemporaryPrefs() {
|
206 | 215 | }
|
207 | 216 |
|
208 | 217 | bool Prefs::FileStorage::SwapPrefs() {
|
209 |
| - if (std::filesystem::exists(prefs_dir_.value())) { |
210 |
| - std::filesystem::remove_all(prefs_dir_.value()); |
| 218 | + if (!utils::DeleteDirectory(prefs_dir_.value().c_str())) { |
| 219 | + LOG(ERROR) << "Failed to remove prefs dir " << prefs_dir_; |
| 220 | + return false; |
211 | 221 | }
|
212 | 222 | if (rename(GetTemporaryDir().c_str(), prefs_dir_.value().c_str()) != 0) {
|
213 | 223 | LOG(ERROR) << "Error replacing prefs with prefs_tmp" << strerror(errno);
|
214 | 224 | return false;
|
215 | 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 | + } |
216 | 230 | return true;
|
217 | 231 | }
|
218 | 232 |
|
219 | 233 | bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
|
220 | 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 | + } |
221 | 242 |
|
222 | 243 | // Delete empty directories. Ignore errors when deleting empty directories.
|
223 | 244 | DeleteEmptyDirectories(prefs_dir_);
|
|
0 commit comments