Skip to content

Commit d5e901b

Browse files
authored
Refactor Android filesystem platform helpers (#6928)
* Use std::filesystem to implement Android platform helpers * Add a note in the changelog
1 parent 93d7d3e commit d5e901b

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ const realm = new Realm({
2424
* File format: generates Realms with format v24 (reads and upgrades file format v10).
2525

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

3129
## 12.13.2 (2024-10-30)
3230

packages/realm/binding/android/src/main/cpp/platform.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdarg.h>
2222
#include <unistd.h>
2323
#include <cstdio>
24+
#include <vector>
25+
#include <filesystem>
2426
#include <android/asset_manager.h>
2527
#include <android/log.h>
2628

@@ -29,6 +31,8 @@
2931
#define REALM_FILE_FILTER ".realm"
3032
#define REALM_FILE_FILTER_LEN 6
3133

34+
namespace fs = std::filesystem;
35+
3236
static inline bool is_realm_file(const char* str)
3337
{
3438
size_t lenstr = strlen(str);
@@ -57,7 +61,11 @@ std::string JsPlatformHelpers::default_realm_file_directory()
5761
return s_default_realm_directory;
5862
}
5963

60-
void JsPlatformHelpers::ensure_directory_exists_for_file(const std::string& file) {}
64+
void JsPlatformHelpers::ensure_directory_exists_for_file(const std::string& file)
65+
{
66+
auto parent_path = fs::path(file).parent_path();
67+
fs::create_directories(parent_path);
68+
}
6169

6270
void JsPlatformHelpers::copy_bundled_realm_files()
6371
{
@@ -88,23 +96,31 @@ void JsPlatformHelpers::copy_bundled_realm_files()
8896

8997
void JsPlatformHelpers::remove_realm_files_from_directory(const std::string& directory)
9098
{
91-
std::string cmd = "rm " + s_default_realm_directory + "/*.realm " + s_default_realm_directory + "/*.realm.lock";
92-
system(cmd.c_str());
99+
std::vector<fs::path> files_to_delete;
100+
// Collect the files to delete (as deleting while iterating gives undefined behaviour)
101+
for (const auto& entry : fs::directory_iterator(directory)) {
102+
if (entry.is_regular_file()) {
103+
const auto& path = entry.path();
104+
if (path.extension() == ".realm" || path.extension() == ".realm.lock") {
105+
files_to_delete.push_back(path);
106+
}
107+
}
108+
}
109+
110+
// Delete the files
111+
for (const auto& path : files_to_delete) {
112+
fs::remove(path);
113+
}
93114
}
94115

95116
void JsPlatformHelpers::remove_directory(const std::string& path)
96117
{
97-
std::string cmd_clear_dir = "rm " + path + "/*";
98-
system(cmd_clear_dir.c_str());
99-
100-
std::string cmd_rmdir = "rmdir " + path;
101-
system(cmd_rmdir.c_str());
118+
fs::remove_all(path);
102119
}
103120

104121
void JsPlatformHelpers::remove_file(const std::string& path)
105122
{
106-
std::string cmd = "rm " + path;
107-
system(cmd.c_str());
123+
fs::remove(path);
108124
}
109125

110126
void JsPlatformHelpers::print(const char* fmt, ...)

0 commit comments

Comments
 (0)