-
Notifications
You must be signed in to change notification settings - Fork 17
/
file-browser.js
261 lines (245 loc) · 8.48 KB
/
file-browser.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var SEEK_SET = 0;
var SEEK_END = 2;
var nativeApi = {
'open': getNativeFunction('open', 'int', ['pointer', 'int']),
'lseek': getNativeFunction(exportExists('lseek64') ? 'lseek64' : 'lseek', 'int64', ['int', 'int64', 'int']),
'read': getNativeFunction('read', 'uint64',['int', 'pointer', 'uint64']),
'close': getNativeFunction('close', 'int', ['int'])
};
function getApplicationContext() {
var ActivityThread = Java.use('android.app.ActivityThread');
var app = ActivityThread.currentApplication();
return app.getApplicationContext();
}
function getNativeFunction(name, returnType, args) {
return new NativeFunction(Module.findExportByName(null, name), returnType, args);
}
function exportExists(name) {
return Module.findExportByName(null, name) != null;
}
/* POSIX requires that a file size be printed without a sign, even
when negative. Assume the typical case where negative sizes are
actually positive values that have wrapped around. */
function unsigned_file_size(size) {
return size + (size < 0) * (Number.MAX_SAFE_INTEGER - Number.MIN_SAFE_INTEGER + 1);
}
rpc.exports = {
isFile: function(path) {
return new Promise(function(resolve) {
if(Java.available) {
Java.perform(function () {
var file = Java.use('java.io.File');
resolve(file.$new(path).isFile());
});
}
else {
var isDirectory = Memory.alloc(Process.pointerSize);
ObjC.classes.NSFileManager.defaultManager().fileExistsAtPath_isDirectory_(path, isDirectory);
resolve(Memory.readInt(isDirectory) === 0);
}
});
},
readFile: function(path, size) {
var pathStr = Memory.allocUtf8String(path);
var fd = nativeApi.open(pathStr, 0);
if (fd === -1)
throw new Error('error open file');
var fileSize = nativeApi.lseek(fd, 0, SEEK_END).valueOf();
fileSize = unsigned_file_size(fileSize);
nativeApi.lseek(fd, 0, SEEK_SET);
if(size === 0 || size > fileSize) {
size = fileSize;
}
var buf = Memory.alloc(size);
var readResult;
readResult = nativeApi.read(fd, buf, size);
if (readResult === -1)
throw new Error('error read');
nativeApi.close(fd);
// console.log(buf.readByteArray(size));
return buf.readByteArray(size);
},
fileExists: function(path) {
return new Promise(function(resolve) {
if(Java.available) {
Java.perform(function () {
var file = Java.use('java.io.File');
resolve(file.$new(path).exists());
});
}
else {
resolve(ObjC.classes.NSFileManager.defaultManager().fileExistsAtPath_(path));
}
});
},
dbQuery: function(path, query) {
return new Promise(function(resolve) {
if(Java.available) {
Java.perform(function () {
var SQLiteDatabase = Java.use('android.database.sqlite.SQLiteDatabase');
var result = [];
var dbHandler = SQLiteDatabase.openDatabase(path, null, 0);
var cursor = dbHandler.rawQuery(query, null);
var column_names = cursor.getColumnNames();
var count = cursor.getCount();
var i;
result.push(column_names);
for (i = 1; i <= count; i++) {
result.push([]);
}
i = 1;
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
column_names.forEach(function (column) {
result[i].push(cursor.getString(cursor.getColumnIndex(column)));
});
cursor.moveToNext();
i++;
}
}
resolve(result);
});
}
else {
// TODO: get the real column names via sqlite3_column_name or any other way
var dbHandler = SqliteDatabase.open(path);
var s = dbHandler.prepare(query);
var row = s.step();
var flag = false;
var result = [];
while(row != null) {
if(!flag) { // add the columns first
result.push(Array.from({length: row.length}, (_, i) => "c" + (i +1)));
flag = true;
}
result.push(row.map(item => item)); // TODO: handle ios special objects
row = s.step();
}
resolve(result);
}
});
},
getPackageInfo: function() {
return new Promise(function(resolve) {
if(Java.available) {
Java.perform(function () {
var context = getApplicationContext();
var package_name = context.getPackageName();
var package_version = context.getPackageManager().getPackageInfo(package_name, 0).versionName.value;
var resp = {
'directories': [
context.getApplicationInfo().sourceDir.value.split("/").slice(0, -1).join("/"),
context.getDataDir().getAbsolutePath().toString(),
],
'package_name': package_name,
'package_version': package_version,
};
for(var i = 0; i < resp.directories.length; i++) {
var item = Java.use('java.io.File').$new(resp.directories[i]);;
resp.directories[i] = {
'isDirectory': item.isDirectory(),
'isFile': item.isFile(),
'isHidden': item.isHidden(),
'lastModified': item.lastModified(),
'size': item.length(),
'path': item.getAbsolutePath(),
'readable': item.canRead(),
'writeable': item.canWrite(),
'executable': item.canExecute(),
'file_type': null
};
}
resolve(resp);
});
}
else {
var fileManager = ObjC.classes.NSFileManager.defaultManager();
var info = ObjC.classes.NSBundle.mainBundle().infoDictionary();
var resp = {
'directories': [
ObjC.classes.NSBundle.mainBundle().bundlePath().toString(),
ObjC.classes.NSProcessInfo.processInfo().environment().objectForKey_("HOME").toString(),
],
'package_name': info.objectForKey_("CFBundleDisplayName").toString(),
'package_version': info.objectForKey_("CFBundleShortVersionString").toString()
};
for(var i = 0; i < resp.directories.length; i++) {
var item = fileManager.attributesOfItemAtPath_error_(ObjC.classes.NSURL.fileURLWithPath_(resp.directories[i]).path(), NULL);
resp.directories[i] = {
'isDirectory': item.objectForKey_('NSFileType').toString() === 'NSFileTypeDirectory',
'isFile': item.objectForKey_('NSFileType').toString() === 'NSFileTypeRegular',
'isHidden': resp.directories[i].toString()[0] === '.',
'lastModified': Date.parse(item.objectForKey_('NSFileModificationDate').toString()),
'size': item.objectForKey_('NSFileSize').toString(),
'path': resp.directories[i],
'readable': fileManager.isReadableFileAtPath_(resp.directories[i]),
'writeable': fileManager.isWritableFileAtPath_(resp.directories[i]),
'executable': fileManager.isExecutableFileAtPath_(resp.directories[i]),
'file_type': null
}
}
resolve(resp);
}
});
},
ls: function(path) {
return new Promise(function (resolve) {
if(Java.available) {
Java.perform(function () {
var resp = {
'files': [],
'path': path,
'readable': false,
'writeable': false,
};
var file = Java.use('java.io.File');
var directory = file.$new(path);
resp.readable = directory.canRead();
resp.writeable = directory.canWrite();
var files = directory.listFiles();
files.forEach(function (item) {
resp.files.push({
'isDirectory': item.isDirectory(),
'isFile': item.isFile(),
'isHidden': item.isHidden(),
'lastModified': item.lastModified(),
'size': item.length(),
'path': item.getAbsolutePath(),
'readable': item.canRead(),
'writeable': item.canWrite(),
'executable': item.canExecute()
});
});
resolve(resp);
});
}
else {
var resp = {
'files': [],
'path': path,
'readable': false,
'writeable': false,
};
var fileManager = ObjC.classes.NSFileManager.defaultManager();
var files = fileManager.contentsOfDirectoryAtPath_error_(path, NULL);
for(var i = 0; i < files.count(); i++) {
var filepath = [path, files.objectAtIndex_(i)].join('/');
var attributes = fileManager.attributesOfItemAtPath_error_(ObjC.classes.NSURL.fileURLWithPath_(filepath).path(), NULL);
resp.files.push({
'isDirectory': attributes.objectForKey_('NSFileType').toString() === 'NSFileTypeDirectory',
'isFile': attributes.objectForKey_('NSFileType').toString() === 'NSFileTypeRegular',
'isHidden': files.objectAtIndex_(i).toString()[0] === '.',
'lastModified': Date.parse(attributes.objectForKey_('NSFileModificationDate').toString()),
'size': attributes.objectForKey_('NSFileSize').toString(),
'path': filepath,
/* TODO: check with NSFilePosixPermissions */
'readable': fileManager.isReadableFileAtPath_(filepath),
'writeable': fileManager.isWritableFileAtPath_(filepath),
'executable': fileManager.isExecutableFileAtPath_(filepath)
});
}
resolve(resp);
}
});
}
};