From dae8ebfe7e558010c913835366e321e507152aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Tue, 12 Nov 2024 10:48:34 +0100 Subject: [PATCH] Refactor Android filesystem platform helpers (#6928) * Use std::filesystem to implement Android platform helpers * Add a note in the changelog --- CHANGELOG.md | 4 +-- .../binding/android/src/main/cpp/platform.cpp | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f30f797076..07fc52633a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,9 +25,7 @@ const realm = new Realm({ * File format: generates Realms with format v24 (reads and upgrades file format v10). ### Internal - - - +* Refactored Android filesystem platform helpers. ([#5296](https://github.com/realm/realm-js/issues/5296) and [realm/realm-js-private#507](https://github.com/realm/realm-js-private/issues/507)) ## 20.0.0 (2024-09-09) diff --git a/packages/realm/binding/android/src/main/cpp/platform.cpp b/packages/realm/binding/android/src/main/cpp/platform.cpp index 29d92d162d..85300521c2 100644 --- a/packages/realm/binding/android/src/main/cpp/platform.cpp +++ b/packages/realm/binding/android/src/main/cpp/platform.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include @@ -29,6 +31,8 @@ #define REALM_FILE_FILTER ".realm" #define REALM_FILE_FILTER_LEN 6 +namespace fs = std::filesystem; + static inline bool is_realm_file(const char* str) { size_t lenstr = strlen(str); @@ -57,7 +61,11 @@ std::string JsPlatformHelpers::default_realm_file_directory() return s_default_realm_directory; } -void JsPlatformHelpers::ensure_directory_exists_for_file(const std::string& file) {} +void JsPlatformHelpers::ensure_directory_exists_for_file(const std::string& file) +{ + auto parent_path = fs::path(file).parent_path(); + fs::create_directories(parent_path); +} void JsPlatformHelpers::copy_bundled_realm_files() { @@ -88,23 +96,31 @@ void JsPlatformHelpers::copy_bundled_realm_files() void JsPlatformHelpers::remove_realm_files_from_directory(const std::string& directory) { - std::string cmd = "rm " + s_default_realm_directory + "/*.realm " + s_default_realm_directory + "/*.realm.lock"; - system(cmd.c_str()); + std::vector files_to_delete; + // Collect the files to delete (as deleting while iterating gives undefined behaviour) + for (const auto& entry : fs::directory_iterator(directory)) { + if (entry.is_regular_file()) { + const auto& path = entry.path(); + if (path.extension() == ".realm" || path.extension() == ".realm.lock") { + files_to_delete.push_back(path); + } + } + } + + // Delete the files + for (const auto& path : files_to_delete) { + fs::remove(path); + } } void JsPlatformHelpers::remove_directory(const std::string& path) { - std::string cmd_clear_dir = "rm " + path + "/*"; - system(cmd_clear_dir.c_str()); - - std::string cmd_rmdir = "rmdir " + path; - system(cmd_rmdir.c_str()); + fs::remove_all(path); } void JsPlatformHelpers::remove_file(const std::string& path) { - std::string cmd = "rm " + path; - system(cmd.c_str()); + fs::remove(path); } void JsPlatformHelpers::print(const char* fmt, ...)