Update to libsu 1.1.0 with su I/O

This commit is contained in:
topjohnwu
2018-02-12 23:07:35 +08:00
parent 9d6886d367
commit 691dc1d49e
16 changed files with 140 additions and 244 deletions
@@ -4,6 +4,7 @@ import android.os.Environment;
import android.os.Process;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.superuser.io.SuFile;
import java.io.File;
import java.util.Arrays;
@@ -24,8 +25,8 @@ public class Const {
public static final String SU_KEYSTORE_KEY = "su_key";
// Paths
private static String MAGISK_PATH = null;
public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
private static SuFile MAGISK_PATH = null;
public static final SuFile MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk", true);
public static final String BUSYBOX_PATH = "/sbin/.core/busybox";
public static final String TMP_FOLDER_PATH = "/dev/tmp";
public static final String MAGISK_LOG = "/cache/magisk.log";
@@ -37,21 +38,23 @@ public class Const {
public static final int SNET_VER = 7;
public static final int MIN_MODULE_VER = 1400;
public synchronized static String MAGISK_PATH() {
public synchronized static SuFile MAGISK_PATH() {
SuFile file;
if (MAGISK_PATH == null) {
if (Utils.itemExist("/sbin/.core/img")) {
MAGISK_PATH = "/sbin/.core/img";
} else if (Utils.itemExist("/dev/magisk/img")) {
MAGISK_PATH = "/dev/magisk/img";
file = new SuFile("/sbin/.core/img", true);
if (file.exists()) {
MAGISK_PATH = file;
} else if ((file = new SuFile("/dev/magisk/img", true)).exists()) {
MAGISK_PATH = file;
} else {
MAGISK_PATH = "/magisk";
MAGISK_PATH = new SuFile("/magisk", true);
}
}
return MAGISK_PATH;
}
public static String MAGISK_HOST_FILE() {
return MAGISK_PATH() + "/.core/hosts";
public static SuFile MAGISK_HOST_FILE() {
return new SuFile(MAGISK_PATH() + "/.core/hosts");
}
/* A list of apps that should not be shown as hide-able */
@@ -23,6 +23,7 @@ import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.receivers.ManagerUpdate;
import com.topjohnwu.magisk.receivers.RebootReceiver;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -111,11 +112,11 @@ public class ShowUI {
if (Shell.rootAccess()) {
options.add(mm.getString(R.string.direct_install));
}
List<String> res = Shell.Sync.su("echo $SLOT");
if (Utils.isValidShellResponse(res)) {
String s = Utils.cmd("echo $SLOT");
if (s != null) {
options.add(mm.getString(R.string.install_second_slot));
}
char[] slot = Utils.isValidShellResponse(res) ? res.get(0).toCharArray() : null;
char[] slot = s == null ? null : s.toCharArray();
new AlertDialog.Builder(activity)
.setTitle(R.string.select_method)
.setItems(
@@ -185,12 +186,11 @@ public class ShowUI {
if (slot[1] == 'a') slot[1] = 'b';
else slot[1] = 'a';
// Then find the boot image again
List<String> ret = Shell.Sync.su(
"SLOT=" + String.valueOf(slot),
"find_boot_image",
boot = Utils.cmd(
"SLOT=" + String.valueOf(slot) +
"; find_boot_image;" +
"echo \"$BOOTIMAGE\""
);
boot = Utils.isValidShellResponse(ret) ? ret.get(ret.size() - 1) : null;
Shell.Async.su("mount_partitions");
if (boot == null)
return;
@@ -253,14 +253,14 @@ public class ShowUI {
.setPositiveButton(R.string.complete_uninstall, (d, i) -> {
ByteArrayOutputStream uninstaller = new ByteArrayOutputStream();
try (InputStream in = mm.getAssets().open(Const.UNINSTALLER)) {
Utils.inToOut(in, uninstaller);
ShellUtils.pump(in, uninstaller);
} catch (IOException e) {
e.printStackTrace();
return;
}
ByteArrayOutputStream utils = new ByteArrayOutputStream();
try (InputStream in = mm.getAssets().open(Const.UTIL_FUNCTIONS)) {
Utils.inToOut(in, utils);
ShellUtils.pump(in, utils);
} catch (IOException e) {
e.printStackTrace();
return;
@@ -19,7 +19,6 @@ import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Toast;
import com.google.gson.Gson;
@@ -31,11 +30,10 @@ import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -47,37 +45,8 @@ public class Utils {
public static boolean isDownloading = false;
public static boolean itemExist(Object path) {
List<String> ret = Shell.Sync.su(fmt("[ -e %s ] && echo true || echo false", path));
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
}
public static void createFile(Object path) {
Shell.Async.su(fmt("mkdir -p `dirname '%s'` 2>/dev/null; touch '%s' 2>/dev/null", path, path));
}
public static boolean javaCreateFile(File path) {
path.getParentFile().mkdirs();
try {
path.createNewFile();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void removeItem(Object path) {
Shell.Async.su(fmt("rm -rf %s 2>/dev/null", path));
}
public static List<String> readFile(Object path) {
return Shell.Sync.su(fmt("cat %s | sed '$a\\ ' | sed '$d'", path));
}
public static String checkInode(Object path) {
List<String> ret = Shell.Sync.su(fmt("ls -i %s", path));
return isValidShellResponse(ret) ? ret.get(0).trim().split("\\s+")[0] : path.toString();
public static String cmd(String cmd) {
return ShellUtils.fastCmd(Shell.getShell(), cmd);
}
public static void uninstallPkg(String pkg) {
@@ -118,16 +87,6 @@ public class Utils {
.replace("\\", "_");
}
public static boolean isValidShellResponse(List<String> list) {
if (list != null && list.size() != 0) {
// Check if all empty
for (String res : list) {
if (!TextUtils.isEmpty(res)) return true;
}
}
return false;
}
public static int getPrefsInt(SharedPreferences prefs, String key, int def) {
return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
}
@@ -230,14 +189,6 @@ public class Utils {
}
}
public static File getDB(String dbName) {
return getDB(MagiskManager.get(), dbName);
}
public static File getDB(Context context, String dbName) {
return new File(context.getFilesDir().getParent() + "/databases", dbName);
}
public static AssetManager getAssets(String apk) {
try {
AssetManager asset = AssetManager.class.newInstance();
@@ -249,22 +200,10 @@ public class Utils {
}
}
public static int inToOut(InputStream in, OutputStream out) throws IOException {
int read, total = 0;
byte buffer[] = new byte[4096];
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
total += read;
}
out.flush();
return total;
}
public static void patchDTBO() {
MagiskManager mm = MagiskManager.get();
if (mm.magiskVersionCode >= 1446 && !mm.keepVerity) {
List<String> ret = Shell.Sync.su("patch_dtbo_image && echo true || echo false");
if (Utils.isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(ret.size() - 1))) {
if (ShellUtils.fastCmdResult(Shell.getShell(), "patch_dtbo_image")) {
ShowUI.dtboPatchedNotification();
}
}
@@ -285,9 +224,9 @@ public class Utils {
}
public static void loadPrefs() {
String config = fmt("/data/user/%d/%s", Const.USER_ID, Const.MANAGER_CONFIGS);
List<String> ret = readFile(config);
if (isValidShellResponse(ret)) {
SuFile config = new SuFile(fmt("/data/user/%d/%s", Const.USER_ID, Const.MANAGER_CONFIGS), true);
List<String> ret = Shell.Sync.su("cat " + config);
if (ShellUtils.isValidOutput(ret)) {
SharedPreferences.Editor editor = MagiskManager.get().prefs.edit();
String json = ret.get(0);
Gson gson = new Gson();
@@ -306,7 +245,7 @@ public class Utils {
editor.remove(Const.Key.ETAG_KEY);
editor.apply();
MagiskManager.get().loadConfig();
removeItem(config);
config.delete();
}
}
@@ -1,5 +1,6 @@
package com.topjohnwu.magisk.utils;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK;
@@ -39,7 +40,7 @@ public class ZipUtils {
File dest = new File(folder, name);
dest.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(dest)) {
Utils.inToOut(zipfile, out);
ShellUtils.pump(zipfile, out);
}
}
} catch(Exception e) {