Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Android filesystem platform helpers #6928

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,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))

## 12.13.2 (2024-10-30)

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
Loading