From d33c8c6aefd992791b88fdb53dc14dfcb0a83cad Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Wed, 3 Apr 2024 12:25:44 -0700 Subject: [PATCH] Fix preference file loading bug. PiperOrigin-RevId: 621604205 --- internal/base/files.cc | 5 +++-- internal/base/files.h | 5 +++-- .../implementation/windows/preferences_repository.cc | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/base/files.cc b/internal/base/files.cc index 83620d85..f2056fb8 100644 --- a/internal/base/files.cc +++ b/internal/base/files.cc @@ -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) { diff --git a/internal/base/files.h b/internal/base/files.h index 8970834f..f6c4ff5c 100644 --- a/internal/base/files.h +++ b/internal/base/files.h @@ -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 diff --git a/internal/platform/implementation/windows/preferences_repository.cc b/internal/platform/implementation/windows/preferences_repository.cc index 1bf94500..30d9bb2e 100644 --- a/internal/platform/implementation/windows/preferences_repository.cc +++ b/internal/platform/implementation/windows/preferences_repository.cc @@ -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 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; }