|
21 | 21 | #include <stdarg.h> |
22 | 22 | #include <unistd.h> |
23 | 23 | #include <cstdio> |
| 24 | +#include <vector> |
| 25 | +#include <filesystem> |
24 | 26 | #include <android/asset_manager.h> |
25 | 27 | #include <android/log.h> |
26 | 28 |
|
|
29 | 31 | #define REALM_FILE_FILTER ".realm" |
30 | 32 | #define REALM_FILE_FILTER_LEN 6 |
31 | 33 |
|
| 34 | +namespace fs = std::filesystem; |
| 35 | + |
32 | 36 | static inline bool is_realm_file(const char* str) |
33 | 37 | { |
34 | 38 | size_t lenstr = strlen(str); |
@@ -57,7 +61,11 @@ std::string JsPlatformHelpers::default_realm_file_directory() |
57 | 61 | return s_default_realm_directory; |
58 | 62 | } |
59 | 63 |
|
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 | +} |
61 | 69 |
|
62 | 70 | void JsPlatformHelpers::copy_bundled_realm_files() |
63 | 71 | { |
@@ -88,23 +96,31 @@ void JsPlatformHelpers::copy_bundled_realm_files() |
88 | 96 |
|
89 | 97 | void JsPlatformHelpers::remove_realm_files_from_directory(const std::string& directory) |
90 | 98 | { |
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 | + } |
93 | 114 | } |
94 | 115 |
|
95 | 116 | void JsPlatformHelpers::remove_directory(const std::string& path) |
96 | 117 | { |
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); |
102 | 119 | } |
103 | 120 |
|
104 | 121 | void JsPlatformHelpers::remove_file(const std::string& path) |
105 | 122 | { |
106 | | - std::string cmd = "rm " + path; |
107 | | - system(cmd.c_str()); |
| 123 | + fs::remove(path); |
108 | 124 | } |
109 | 125 |
|
110 | 126 | void JsPlatformHelpers::print(const char* fmt, ...) |
|
0 commit comments