mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fix handling of unicode paths in preferences_repository.
PiperOrigin-RevId: 761661104
This commit is contained in:
committed by
Copybara-Service
parent
4fcd7a312f
commit
eec16048f4
@@ -15,13 +15,13 @@
|
||||
#include "internal/platform/implementation/windows/preferences_repository.h"
|
||||
|
||||
#include <exception>
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/base/files.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
@@ -69,25 +69,28 @@ json PreferencesRepository::LoadPreferences() {
|
||||
bool PreferencesRepository::SavePreferences(json preferences) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
try {
|
||||
std::filesystem::path path = path_;
|
||||
if (!nearby::sharing::FileExists(path) &&
|
||||
!nearby::sharing::CreateDirectories(path)) {
|
||||
FilePath path{path_};
|
||||
if (!nearby::sharing::FileExists(path.GetPath()) &&
|
||||
!nearby::sharing::CreateDirectories(path.GetPath())) {
|
||||
LOG(ERROR) << "Failed to create preferences path.";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::filesystem::path full_name = path / kPreferencesFileName;
|
||||
std::filesystem::path full_name_backup = path / kPreferencesBackupFileName;
|
||||
FilePath full_name = path;
|
||||
full_name.append(FilePath(kPreferencesFileName));
|
||||
FilePath full_name_backup = path;
|
||||
full_name_backup.append(FilePath(kPreferencesBackupFileName));
|
||||
|
||||
// Create a backup without moving the bytes on disk
|
||||
if (nearby::sharing::FileExists(full_name)) {
|
||||
if (nearby::sharing::FileExists(full_name.GetPath())) {
|
||||
LOG(INFO) << "Making backup of preferences file.";
|
||||
if (!nearby::sharing::Rename(full_name, full_name_backup)) {
|
||||
if (!nearby::sharing::Rename(full_name.GetPath(),
|
||||
full_name_backup.GetPath())) {
|
||||
LOG(ERROR) << "Failed to rename preferences backup file.";
|
||||
}
|
||||
}
|
||||
|
||||
std::ofstream preferences_file(full_name);
|
||||
std::ofstream preferences_file(full_name.GetPath());
|
||||
preferences_file << preferences;
|
||||
preferences_file.close();
|
||||
|
||||
@@ -113,15 +116,16 @@ 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::DirectoryExists(path) ||
|
||||
!nearby::sharing::FileExists(full_name)) {
|
||||
FilePath path{path_};
|
||||
FilePath full_name = path;
|
||||
full_name.append(FilePath(kPreferencesFileName));
|
||||
if (!nearby::sharing::DirectoryExists(path.GetPath()) ||
|
||||
!nearby::sharing::FileExists(full_name.GetPath())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
try {
|
||||
std::ifstream preferences_file(full_name);
|
||||
std::ifstream preferences_file(full_name.GetPath());
|
||||
if (!preferences_file.good()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -145,16 +149,19 @@ std::optional<json> PreferencesRepository::AttemptLoad() {
|
||||
}
|
||||
|
||||
std::optional<json> PreferencesRepository::RestoreFromBackup() {
|
||||
std::filesystem::path path = path_;
|
||||
std::filesystem::path full_name = path / kPreferencesFileName;
|
||||
std::filesystem::path full_name_backup = path / kPreferencesBackupFileName;
|
||||
FilePath path{path_};
|
||||
FilePath full_name = path;
|
||||
full_name.append(FilePath(kPreferencesFileName));
|
||||
FilePath full_name_backup = path;
|
||||
full_name_backup.append(FilePath(kPreferencesBackupFileName));
|
||||
|
||||
if (!nearby::sharing::FileExists(full_name_backup)) {
|
||||
if (!nearby::sharing::FileExists(full_name_backup.GetPath())) {
|
||||
LOG(WARNING) << "Backup requested but no backup preferences file found.";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (!nearby::sharing::Rename(full_name_backup, full_name)) {
|
||||
if (!nearby::sharing::Rename(full_name_backup.GetPath(),
|
||||
full_name.GetPath())) {
|
||||
LOG(ERROR) << "Failed to rename preferences backup file.";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user