ToolsInstaller: Add method to check installation status

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland 2018-01-08 23:11:16 -06:00
parent 2776455d4f
commit b87568482d

View File

@ -29,13 +29,14 @@ public final class ToolsInstaller {
new File("/system/xbin"), new File("/system/xbin"),
new File("/system/bin"), new File("/system/bin"),
}; };
private static final File INSTALL_DIR = getInstallDir();
private final String nativeLibraryDir; private final File nativeLibraryDir;
private final RootShell rootShell; private final RootShell rootShell;
@Inject @Inject
public ToolsInstaller(@ApplicationContext final Context context, final RootShell rootShell) { public ToolsInstaller(@ApplicationContext final Context context, final RootShell rootShell) {
nativeLibraryDir = context.getApplicationInfo().nativeLibraryDir; nativeLibraryDir = new File(context.getApplicationInfo().nativeLibraryDir);
this.rootShell = rootShell; this.rootShell = rootShell;
} }
@ -50,23 +51,32 @@ public final class ToolsInstaller {
return null; return null;
} }
public int install() { public boolean areInstalled() {
final File installDir = getInstallDir(); if (INSTALL_DIR == null)
if (installDir == null) return false;
return OsConstants.ENOENT; final StringBuilder script = new StringBuilder();
final StringBuilder script = new StringBuilder("set -ex; ");
for (final String[] names : EXECUTABLES) { for (final String[] names : EXECUTABLES) {
script.append(String.format("cmp -s '%s' '%s' && ", script.append(String.format("cmp -s '%s' '%s' && ",
new File(nativeLibraryDir, names[0]), new File(nativeLibraryDir, names[0]),
new File(installDir, names[1]))); new File(INSTALL_DIR, names[1])));
} }
script.append("exit ").append(OsConstants.EALREADY).append("; "); script.append("exit ").append(OsConstants.EALREADY);
script.append("trap 'mount -o ro,remount /system' EXIT; mount -o rw,remount /system; "); try {
return rootShell.run(null, script.toString()) == OsConstants.EALREADY;
} catch (final ErrnoException | IOException | NoRootException ignored) {
return false;
}
}
public int install() {
if (INSTALL_DIR == null)
return OsConstants.ENOENT;
final StringBuilder script = new StringBuilder("set -ex"
+ "; trap 'mount -o ro,remount /system' EXIT; mount -o rw,remount /system");
for (final String[] names : EXECUTABLES) { for (final String[] names : EXECUTABLES) {
script.append(String.format("cp %s %s; chmod 755 %s; ", final File destination = new File(INSTALL_DIR, names[1]);
new File(nativeLibraryDir, names[0]), script.append(String.format("; cp '%s' '%s'; chmod 755 '%s'; restorecon '%s' ",
new File(installDir, names[1]), new File(nativeLibraryDir, names[0]), destination, destination, destination));
new File(installDir, names[1])));
} }
try { try {
return rootShell.run(null, script.toString()); return rootShell.run(null, script.toString());