Fix preference file loading bug.

PiperOrigin-RevId: 621604205
This commit is contained in:
Francis Tsui
2024-04-03 12:27:51 -07:00
committed by Copybara-Service
parent 2c6926be17
commit d33c8c6aef
3 changed files with 9 additions and 7 deletions
+3 -2
View File
@@ -76,7 +76,8 @@ std::filesystem::path CurrentDirectory() {
return std::filesystem::current_path(error_code);
}
bool Rename(std::filesystem::path old_path, std::filesystem::path new_path) {
bool Rename(const std::filesystem::path& old_path,
const std::filesystem::path& new_path) {
std::error_code error_code;
std::filesystem::rename(old_path, new_path, error_code);
if (error_code) {
@@ -85,7 +86,7 @@ bool Rename(std::filesystem::path old_path, std::filesystem::path new_path) {
return true;
}
bool CreateDirectories(std::filesystem::path path) {
bool CreateDirectories(const std::filesystem::path& path) {
std::error_code error_code;
std::filesystem::create_directories(path, error_code);
if (error_code) {
+3 -2
View File
@@ -44,11 +44,12 @@ std::filesystem::path CurrentDirectory();
// Renames the file at old_path to new_path.
// Returns true on success.
bool Rename(std::filesystem::path old_path, std::filesystem::path new_path);
bool Rename(const std::filesystem::path& old_path,
const std::filesystem::path& new_path);
// Creates all directory leading to path.
// Returns true on success.
bool CreateDirectories(std::filesystem::path path);
bool CreateDirectories(const std::filesystem::path& path);
} // namespace nearby::sharing
@@ -86,7 +86,7 @@ bool PreferencesRepository::SavePreferences(json preferences) {
}
}
std::ofstream preferences_file(full_name.c_str());
std::ofstream preferences_file(full_name);
preferences_file << preferences;
preferences_file.close();
@@ -114,13 +114,13 @@ bool PreferencesRepository::SavePreferences(json preferences) {
std::optional<json> PreferencesRepository::AttemptLoad() {
std::filesystem::path path = path_;
std::filesystem::path full_name = path / kPreferencesFileName;
if (!nearby::sharing::FileExists(path) ||
if (!nearby::sharing::DirectoryExists(path) ||
!nearby::sharing::FileExists(full_name)) {
return std::nullopt;
}
try {
std::ifstream preferences_file(full_name.c_str());
std::ifstream preferences_file(full_name);
if (!preferences_file.good()) {
return std::nullopt;
}