From 1cfb938dd18526b4d961dfc8ac374ac678724c18 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 8 Jan 2018 23:12:50 -0600 Subject: ToolsInstaller: Add methods to check/make symlinks Signed-off-by: Samuel Holland --- .../com/wireguard/android/util/ToolsInstaller.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java index ebe69855..fddcf98b 100644 --- a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java +++ b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java @@ -31,11 +31,13 @@ public final class ToolsInstaller { }; private static final File INSTALL_DIR = getInstallDir(); + private final File localBinaryDir; private final File nativeLibraryDir; private final RootShell rootShell; @Inject public ToolsInstaller(@ApplicationContext final Context context, final RootShell rootShell) { + localBinaryDir = new File(context.getCacheDir(), "bin"); nativeLibraryDir = new File(context.getApplicationInfo().nativeLibraryDir); this.rootShell = rootShell; } @@ -68,6 +70,21 @@ public final class ToolsInstaller { } } + public boolean areSymlinked() { + final StringBuilder script = new StringBuilder(); + for (final String[] names : EXECUTABLES) { + script.append(String.format("test '%s' -ef '%s' && ", + new File(nativeLibraryDir, names[0]), + new File(localBinaryDir, names[1]))); + } + script.append("exit ").append(OsConstants.EALREADY); + 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; @@ -88,4 +105,22 @@ public final class ToolsInstaller { return OsConstants.EACCES; } } + + public int symlink() { + final StringBuilder script = new StringBuilder("set -ex"); + for (final String[] names : EXECUTABLES) { + script.append(String.format("; ln -fns '%s' '%s'", + new File(nativeLibraryDir, names[0]), + new File(localBinaryDir, names[1]))); + } + try { + return rootShell.run(null, script.toString()); + } catch (final ErrnoException e) { + return e.errno; + } catch (final IOException ignored) { + return OsConstants.EXIT_FAILURE; + } catch (final NoRootException ignored) { + return OsConstants.EACCES; + } + } } -- cgit v1.2.3