Skip to content

Commit

Permalink
Refactor Android filesystem platform helpers (#6928)
Browse files Browse the repository at this point in the history
* Use std::filesystem to implement Android platform helpers

* Add a note in the changelog
  • Loading branch information
kraenhansen committed Nov 29, 2024
1 parent 534b414 commit dae8ebf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const realm = new Realm({
* File format: generates Realms with format v24 (reads and upgrades file format v10).

### Internal
<!-- * Either mention core version or upgrade -->
<!-- * Using Realm Core vX.Y.Z -->
<!-- * Upgraded Realm Core from vX.Y.Z to vA.B.C -->
* 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)

Expand Down
36 changes: 26 additions & 10 deletions packages/realm/binding/android/src/main/cpp/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <stdarg.h>
#include <unistd.h>
#include <cstdio>
#include <vector>
#include <filesystem>
#include <android/asset_manager.h>
#include <android/log.h>

Expand All @@ -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);
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<fs::path> 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, ...)
Expand Down

0 comments on commit dae8ebf

Please sign in to comment.