From 5201c7f8492ea5e77267d3e45f4a4c1285e8f10b Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 8 Jan 2018 23:25:49 -0600 Subject: [PATCH] ToolsInstaller: Add a method to ensure tool availability Signed-off-by: Samuel Holland --- .../android/util/ToolsInstaller.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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 fddcf98b..5042abe6 100644 --- a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java +++ b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java @@ -3,12 +3,14 @@ package com.wireguard.android.util; import android.content.Context; import android.system.ErrnoException; import android.system.OsConstants; +import android.util.Log; import com.wireguard.android.Application.ApplicationContext; import com.wireguard.android.Application.ApplicationScope; import com.wireguard.android.util.RootShell.NoRootException; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -30,10 +32,12 @@ public final class ToolsInstaller { new File("/system/bin"), }; private static final File INSTALL_DIR = getInstallDir(); + private static final String TAG = "WireGuard/" + ToolsInstaller.class.getSimpleName(); private final File localBinaryDir; private final File nativeLibraryDir; private final RootShell rootShell; + private Boolean areToolsAvailable; @Inject public ToolsInstaller(@ApplicationContext final Context context, final RootShell rootShell) { @@ -85,6 +89,30 @@ public final class ToolsInstaller { } } + public void ensureToolsAvailable() throws FileNotFoundException { + if (areToolsAvailable == null) { + synchronized (this) { + if (areToolsAvailable == null) { + if (areInstalled()) { + Log.d(TAG, "Tools are installed to /system"); + areToolsAvailable = true; + } else if (areSymlinked()) { + Log.d(TAG, "Tools were already symlinked into our private binary dir"); + areToolsAvailable = true; + } else if (symlink() == OsConstants.EXIT_SUCCESS) { + Log.d(TAG, "Tools are now symlinked into our private binary dir"); + areToolsAvailable = true; + } else { + Log.e(TAG, "For some reason, wg and wg-quick are not available at all"); + areToolsAvailable = false; + } + } + } + } + if (!areToolsAvailable) + throw new FileNotFoundException("Required tools unavailable"); + } + public int install() { if (INSTALL_DIR == null) return OsConstants.ENOENT;