platform.cpp
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <cstdio>
#include <android/asset_manager.h>
#include "../platform.hpp"
#define REALM_FILE_FILTER ".realm"
#define REALM_FILE_FILTER_LEN 6
static inline bool is_realm_file(const char* str)
{
size_t lenstr = strlen(str);
if (REALM_FILE_FILTER_LEN > lenstr)
return false;
return strncmp(str + lenstr - REALM_FILE_FILTER_LEN, REALM_FILE_FILTER, REALM_FILE_FILTER_LEN) == 0;
}
static AAssetManager* s_asset_manager;
static std::string s_default_realm_directory;
namespace realm {
void set_default_realm_file_directory(std::string dir)
{
s_default_realm_directory = dir;
}
void set_asset_manager(AAssetManager* asset_manager)
{
s_asset_manager = asset_manager;
}
std::string default_realm_file_directory()
{
return s_default_realm_directory;
}
void ensure_directory_exists_for_file(const std::string &file)
{
}
void copy_bundled_realm_files()
{
AAssetDir* assetDir = AAssetManager_openDir(s_asset_manager, "");
const char* filename = nullptr;
while ((filename = AAssetDir_getNextFileName(assetDir)) != nullptr) {
if (is_realm_file(filename)) {
AAsset* asset = AAssetManager_open(s_asset_manager, filename, AASSET_MODE_STREAMING);
char buf[BUFSIZ];
int nb_read = 0;
const char* dest_filename = (s_default_realm_directory + '/' + filename).c_str();
if (access(dest_filename, F_OK ) == -1) {
// file doesn't exist, copy
FILE* out = fopen(dest_filename, "w");
while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0) {
fwrite(buf, nb_read, 1, out);
}
fclose(out);
}
AAsset_close(asset);
}
}
AAssetDir_close(assetDir);
}
void 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());
}
void 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());
}
void remove_file(const std::string &path)
{
std::string cmd = "rm " + path;
system(cmd.c_str());
}
}