Repository navigation
Expand file tree
/
Copy pathEnvUtils.java
More file actions
518 lines (476 loc) · 16.1 KB
/
Copy pathEnvUtils.java
File metadata and controls
518 lines (476 loc) · 16.1 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package ru.meefik.linuxdeploy;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.AssetManager;
public class EnvUtils {
/**
* Closeable helper
*
* @param c closable object
*/
private static void close(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Extract file to env directory
*
* @param c context
* @param rootAsset root asset name
* @param path path to asset file
* @return false if error
*/
private static boolean extractFile(Context c, String rootAsset, String path) {
AssetManager assetManager = c.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(rootAsset + path);
String fullPath = PrefStore.getEnvDir(c) + path;
out = new FileOutputStream(fullPath);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
close(in);
close(out);
}
return true;
}
/**
* Extract path to env directory
*
* @param c context
* @param rootAsset root asset name
* @param path path to asset directory
* @return false if error
*/
private static boolean extractDir(Context c, String rootAsset, String path) {
AssetManager assetManager = c.getAssets();
try {
String[] assets = assetManager.list(rootAsset + path);
if (assets.length == 0) {
if (!extractFile(c, rootAsset, path)) return false;
} else {
String fullPath = PrefStore.getEnvDir(c) + path;
File dir = new File(fullPath);
if (!dir.exists()) dir.mkdir();
for (String asset : assets) {
if (!extractDir(c, rootAsset, path + "/" + asset))
return false;
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Recursive remove all from directory
*
* @param path path to directory
*/
private static void cleanDirectory(File path) {
if (path == null) return;
if (path.exists()) {
for (File f : path.listFiles()) {
if (f.isDirectory()) cleanDirectory(f);
f.delete();
}
}
}
/**
* Recursive set permissions to directory
*
* @param path path to directory
*/
private static void setPermissions(File path) {
if (path == null) return;
if (path.exists()) {
for (File f : path.listFiles()) {
if (f.isDirectory()) setPermissions(f);
f.setReadable(true, false);
f.setExecutable(true, false);
}
}
}
/**
* Check root permissions
*
* @param c context
* @return false if error
*/
public static boolean isRooted(Context c) {
boolean result = false;
OutputStream stdin = null;
InputStream stdout = null;
try {
Process process = Runtime.getRuntime().exec("su");
stdin = process.getOutputStream();
stdout = process.getInputStream();
DataOutputStream os = null;
try {
os = new DataOutputStream(stdin);
os.writeBytes("ls /data\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(os);
}
int n = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(stdout));
while (reader.readLine() != null) {
n++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close(reader);
}
if (n > 0) {
result = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close(stdout);
close(stdin);
}
return result;
}
/**
* Update version file
*
* @param c context
* @return false if error
*/
private static Boolean setVersion(Context c) {
Boolean result = false;
String f = PrefStore.getEnvDir(c) + "/etc/version";
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(f));
bw.write(PrefStore.getVersion(c));
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
close(bw);
}
return result;
}
/**
* Check latest env version
*
* @param c context
* @return false if error
*/
public static Boolean isLatestVersion(Context c) {
Boolean result = false;
String f = PrefStore.getEnvDir(c) + "/etc/version";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String line = br.readLine();
if (PrefStore.getVersion(c).equals(line)) result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
close(br);
}
return result;
}
public static boolean isValidShell(Context c) {
File ash = new File(PrefStore.getShell(c));
return ash.exists();
}
public static Boolean makeScript(Context c) {
boolean result = false;
String scriptFile = PrefStore.getEnvDir(c) + "/bin/linuxdeploy";
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(scriptFile));
bw.write("#!" + PrefStore.getShell(c) + "\n");
bw.write("PATH=" + PrefStore.getEnvDir(c) + "/bin:" + PrefStore.getBusyboxDir(c) + ":$PATH\n");
bw.write("ENV_DIR=" + PrefStore.getEnvDir(c) + "\n");
bw.write(". ${ENV_DIR}/share/cli.sh\n");
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
close(bw);
}
return result;
}
/**
* Execute commands from system shell
*
* @param c context
* @param params list of commands
* @return false, if error
*/
public static boolean exec(final Context c, final String shell,
final List<String> params) {
if (params == null || params.size() == 0) {
Logger.log(c, "No scripts for processing.\n");
return false;
}
boolean result = false;
OutputStream stdin = null;
InputStream stdout = null;
InputStream stderr = null;
try {
Process process = Runtime.getRuntime().exec(shell);
stdin = process.getOutputStream();
stdout = process.getInputStream();
stderr = process.getErrorStream();
params.add(0, "PATH=" + PrefStore.getEnvDir(c) + "/bin:"
+ PrefStore.getBusyboxDir(c) + ":$PATH");
params.add("exit $?");
if (PrefStore.isTraceMode(c)) params.add(0, "set -x");
DataOutputStream os = null;
try {
os = new DataOutputStream(stdin);
for (String cmd : params) {
os.writeBytes(cmd + "\n");
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(os);
}
// show stdout log
final InputStream out = stdout;
(new Thread() {
@Override
public void run() {
Logger.log(c, out);
}
}).start();
// show stderr log
final InputStream err = stderr;
if (PrefStore.isDebugMode(c)) {
(new Thread() {
@Override
public void run() {
Logger.log(c, err);
}
}).start();
}
if (process.waitFor() == 0) result = true;
} catch (Exception e) {
result = false;
e.printStackTrace();
} finally {
close(stdout);
close(stderr);
close(stdin);
}
return result;
}
/**
* Update operating environment
*
* @param c context
* @return true, if no errors
*/
public static boolean updateEnv(Context c) {
if (!isRooted(c)) {
Logger.log(c, "Requires superuser privileges (root).\n");
return false;
}
if (!isValidShell(c)) {
Logger.log(c, "Requires an installed BusyBox.\n");
return false;
}
Logger.log(c, "Updating environment ... ");
// clean env directory
File envDir = new File(PrefStore.getEnvDir(c));
envDir.mkdirs();
cleanDirectory(envDir);
// extract assets
if (!extractDir(c, "all", "")) {
Logger.log(c, "fail\n");
return false;
}
if (!extractDir(c, PrefStore.getArch(), "")) {
Logger.log(c, "fail\n");
return false;
}
// make linuxdeploy script
if (!makeScript(c)) {
Logger.log(c, "fail\n");
return false;
}
// set permissions
setPermissions(envDir);
// update version
if (!setVersion(c)) {
Logger.log(c, "fail\n");
return false;
}
Logger.log(c, "done\n");
return true;
}
/**
* Make symlink on linuxdeploy script in /system/bin
*
* @param c context
* @return true, if no errors
*/
public static boolean makeSymlink(Context c) {
Logger.log(c, "Setting command line interface ... ");
List<String> params = new ArrayList<String>();
params.add("rm -f /system/bin/linuxdeploy");
params.add("ln -s "
+ PrefStore.getEnvDir(c)
+ "/bin/linuxdeploy /system/bin/linuxdeploy || "
+ "{ mount -o rw,remount /system; rm -f /system/bin/linuxdeploy; ln -s "
+ PrefStore.getEnvDir(c)
+ "/bin/linuxdeploy /system/bin/linuxdeploy; mount -o ro,remount /system; }");
if (EnvUtils.exec(c, "su", params)) {
Logger.log(c, "done\n");
return true;
} else {
Logger.log(c, "fail\n");
return false;
}
}
/**
* Remove operating environment
*
* @param c context
* @return true, if no errors
*/
public static boolean removeEnv(Context c) {
Logger.log(c, "Removing environment ... ");
// remove symlink
File ldSymlink = new File("/system/bin/linuxdeploy");
if (ldSymlink.exists()) {
// exec shell commands
List<String> params = new ArrayList<String>();
params.add("if [ -e /system/bin/linuxdeploy ]; then "
+ "rm -f /system/bin/linuxdeploy || "
+ "{ mount -o rw,remount /system; rm -f /system/bin/linuxdeploy; mount -o ro,remount /system; };"
+ "fi");
exec(c, "su", params);
}
// clean env directory
File envDir = new File(PrefStore.getEnvDir(c));
cleanDirectory(envDir);
Logger.log(c, "done\n");
return true;
}
/**
* Update configuration file
*
* @param c context
* @return true, if no errors
*/
public static boolean updateConf(Context c) {
if (!EnvUtils.isLatestVersion(c)) return false;
Logger.log(c, "Updating configuration file ... ");
Boolean result = false;
String confFile = PrefStore.getEnvDir(c) + "/etc/deploy.conf";
List<String> lines = new ArrayList<>();
lines.add("CHROOT_DIR=\"" + PrefStore.getChrootDir(c) + "\"");
lines.add("TARGET_PATH=\"" + PrefStore.getTargetPath(c) + "\"");
lines.add("DISK_SIZE=\"" + PrefStore.getDiskSize(c) + "\"");
lines.add("FS_TYPE=\"" + PrefStore.getFilesystem(c) + "\"");
lines.add("TARGET_TYPE=\"" + PrefStore.getTargetType(c) + "\"");
lines.add("DISTRIB=\"" + PrefStore.getDistribution(c) + "\"");
lines.add("ARCH=\"" + PrefStore.getArchitecture(c) + "\"");
lines.add("SUITE=\"" + PrefStore.getSuite(c) + "\"");
lines.add("SOURCE_PATH=\"" + PrefStore.getSourcePath(c) + "\"");
lines.add("USER_NAME=\"" + PrefStore.getUserName(c) + "\"");
lines.add("USER_PASSWORD=\"" + PrefStore.getUserPassword(c) + "\"");
lines.add("SERVER_DNS=\"" + PrefStore.getServerDns(c) + "\"");
lines.add("LOCALE=\"" + PrefStore.getLocale(c) + "\"");
lines.add("DESKTOP_ENV=\"" + PrefStore.getDesktopEnv(c) + "\"");
lines.add("USE_COMPONENTS=\"" + PrefStore.getUseComponents(c) + "\"");
lines.add("STARTUP=\"" + PrefStore.getStartup(c) + "\"");
lines.add("CUSTOM_SCRIPTS=\"" + PrefStore.getCustomScripts(c) + "\"");
lines.add("CUSTOM_MOUNTS=\"" + PrefStore.getCustomMounts(c) + "\"");
lines.add("SSH_PORT=\"" + PrefStore.getSshPort(c) + "\"");
lines.add("VNC_DISPLAY=\"" + PrefStore.getVncDisplay(c) + "\"");
lines.add("VNC_DEPTH=\"" + PrefStore.getVncDepth(c) + "\"");
lines.add("VNC_DPI=\"" + PrefStore.getVncDpi(c) + "\"");
lines.add("VNC_GEOMETRY=\"" + PrefStore.getVncGeometry(c) + "\"");
lines.add("VNC_ARGS=\"" + PrefStore.getVncArgs(c) + "\"");
lines.add("XSERVER_DISPLAY=\"" + PrefStore.getXserverDisplay(c) + "\"");
lines.add("XSERVER_HOST=\"" + PrefStore.getXserverHost(c) + "\"");
lines.add("FB_DISPLAY=\"" + PrefStore.getFbDisplay(c) + "\"");
lines.add("FB_DPI=\"" + PrefStore.getFbDpi(c) + "\"");
lines.add("FB_DEV=\"" + PrefStore.getFbDev(c) + "\"");
lines.add("FB_INPUT=\"" + PrefStore.getFbInput(c) + "\"");
lines.add("FB_ARGS=\"" + PrefStore.getFbArgs(c) + "\"");
lines.add("FB_REFRESH=\"" + PrefStore.getFbRefreshMode(c) + "\"");
lines.add("FB_FREEZE=\"" + PrefStore.getFbFreezeMode(c) + "\"");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(confFile));
for (String s : lines) {
bw.write(s + "\n");
}
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
close(bw);
}
if (result) {
Logger.log(c, "done\n");
} else {
Logger.log(c, "fail\n");
}
return result;
}
/**
* Execute linuxdeploy script
*
* @param c context
* @param arg arguments
* @return true, if no errors
*/
public static boolean linuxdeploy(Context c, String arg) {
List<String> params = new ArrayList<String>();
String opts = "";
if (PrefStore.isDebugMode(c)) opts = "-d ";
if (PrefStore.isTraceMode(c)) opts = "-t ";
params.add("printf '>>> " + arg + "\n'");
params.add("linuxdeploy " + opts + arg);
params.add("printf '<<< " + arg + "\n'");
boolean result = exec(c, "su", params);
return result;
}
}