From 4ff39f88172ce7da33ad0a5063a7c75894f6e48c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 20 Jun 2018 04:43:03 +0800 Subject: [PATCH] Update to libsu 1.2.0 --- build.gradle | 4 +- .../com/topjohnwu/magisk/MagiskFragment.java | 4 +- .../com/topjohnwu/magisk/MagiskManager.java | 42 +++++-------------- .../topjohnwu/magisk/SettingsActivity.java | 3 +- .../com/topjohnwu/magisk/asyncs/FlashZip.java | 3 +- .../topjohnwu/magisk/asyncs/HideManager.java | 2 +- .../magisk/asyncs/InstallMagisk.java | 9 ++-- .../magisk/asyncs/RestoreImages.java | 8 ++-- .../topjohnwu/magisk/container/Module.java | 16 +++---- .../magisk/database/MagiskDatabaseHelper.java | 2 +- .../com/topjohnwu/magisk/utils/RootUtils.java | 16 ++----- .../magisk/utils/ShellInitializer.java | 32 ++++++++++++++ .../com/topjohnwu/magisk/utils/ShowUI.java | 8 ++-- 13 files changed, 72 insertions(+), 77 deletions(-) create mode 100644 src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java diff --git a/build.gradle b/build.gradle index f3b38baa2..fdb4591f4 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ android { productFlavors { full { - versionCode 118 + versionCode 120 versionName "5.7.0" } stub { @@ -57,7 +57,7 @@ dependencies { fullImplementation "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}" fullImplementation "com.android.support:cardview-v7:${rootProject.ext.supportLibVersion}" fullImplementation "com.android.support:design:${rootProject.ext.supportLibVersion}" - fullImplementation 'com.github.topjohnwu:libsu:1.1.1' + fullImplementation 'com.github.topjohnwu:libsu:1.2.0' fullImplementation 'com.atlassian.commonmark:commonmark:0.11.0' fullImplementation 'org.kamranzafar:jtar:2.3' fullImplementation 'com.jakewharton:butterknife:8.8.1' diff --git a/src/full/java/com/topjohnwu/magisk/MagiskFragment.java b/src/full/java/com/topjohnwu/magisk/MagiskFragment.java index d3edabbef..46567a9f2 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskFragment.java @@ -25,11 +25,11 @@ import com.topjohnwu.magisk.components.ExpandableView; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.ISafetyNetHelper; -import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.ShowUI; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellUtils; import butterknife.BindColor; import butterknife.BindView; @@ -265,7 +265,7 @@ public class MagiskFragment extends Fragment || mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { install(); } else if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV && - !RootUtils.cmdResult("env_check")) { + !ShellUtils.fastCmdResult("env_check")) { ShowUI.envFixDialog(getActivity()); } } diff --git a/src/full/java/com/topjohnwu/magisk/MagiskManager.java b/src/full/java/com/topjohnwu/magisk/MagiskManager.java index 4514fc906..9421c0675 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskManager.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskManager.java @@ -8,7 +8,6 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.content.res.Resources; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Xml; @@ -20,10 +19,11 @@ import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.services.UpdateCheckService; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.RootUtils; +import com.topjohnwu.magisk.utils.ShellInitializer; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; -import com.topjohnwu.superuser.BusyBox; import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.io.SuFile; import com.topjohnwu.superuser.io.SuFileInputStream; @@ -32,7 +32,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; @@ -111,26 +110,7 @@ public class MagiskManager extends Application implements Shell.Container { Shell.setFlags(Shell.FLAG_MOUNT_MASTER); Shell.verboseLogging(BuildConfig.DEBUG); - BusyBox.BB_PATH = new File(Const.BUSYBOX_PATH); - Shell.setInitializer(new Shell.Initializer() { - @Override - public void onRootShellInit(@NonNull Shell shell) { - try (InputStream magiskUtils = getAssets().open(Const.UTIL_FUNCTIONS); - InputStream managerUtils = getResources().openRawResource(R.raw.utils) - ) { - shell.loadInputStream(null, null, magiskUtils); - shell.loadInputStream(null, null, managerUtils); - } catch (IOException e) { - e.printStackTrace(); - } - shell.run(null, null, - "mount_partitions", - "find_boot_image", - "find_dtbo_image", - "get_flags", - "run_migrations"); - } - }); + Shell.setInitializer(ShellInitializer.class); prefs = PreferenceManager.getDefaultSharedPreferences(this); mDB = MagiskDatabaseHelper.getInstance(this); @@ -207,19 +187,19 @@ public class MagiskManager extends Application implements Shell.Container { public void loadMagiskInfo() { try { - magiskVersionString = RootUtils.cmd("magisk -v").split(":")[0]; - magiskVersionCode = Integer.parseInt(RootUtils.cmd("magisk -V")); - String s = RootUtils.cmd((magiskVersionCode >= Const.MAGISK_VER.RESETPROP_PERSIST ? "resetprop -p " : "getprop ") - + Const.MAGISKHIDE_PROP); + magiskVersionString = ShellUtils.fastCmd("magisk -v").split(":")[0]; + magiskVersionCode = Integer.parseInt(ShellUtils.fastCmd("magisk -V")); + String s = ShellUtils.fastCmd((magiskVersionCode >= Const.MAGISK_VER.RESETPROP_PERSIST ? + "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP); magiskHide = s == null || Integer.parseInt(s) != 0; } catch (Exception ignored) {} - bootBlock = RootUtils.cmd("echo \"$BOOTIMAGE\""); + bootBlock = ShellUtils.fastCmd("echo \"$BOOTIMAGE\""); } public void getDefaultInstallFlags() { - keepVerity = Boolean.parseBoolean(RootUtils.cmd("echo $KEEPVERITY")); - keepEnc = Boolean.parseBoolean(RootUtils.cmd("echo $KEEPFORCEENCRYPT")); + keepVerity = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPVERITY")); + keepEnc = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPFORCEENCRYPT")); } public void setupUpdateCheck() { @@ -250,7 +230,7 @@ public class MagiskManager extends Application implements Shell.Container { } public void loadPrefs() { - SuFile config = new SuFile(Utils.fmt("/data/user/%d/%s", Const.USER_ID, Const.MANAGER_CONFIGS), true); + SuFile config = new SuFile(Utils.fmt("/data/user/%d/%s", Const.USER_ID, Const.MANAGER_CONFIGS)); if (config.exists()) { SharedPreferences.Editor editor = prefs.edit(); try { diff --git a/src/full/java/com/topjohnwu/magisk/SettingsActivity.java b/src/full/java/com/topjohnwu/magisk/SettingsActivity.java index fd69d9942..ef0d06170 100644 --- a/src/full/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/src/full/java/com/topjohnwu/magisk/SettingsActivity.java @@ -30,6 +30,7 @@ import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellUtils; import java.io.IOException; import java.util.Locale; @@ -172,7 +173,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { @Override public void onDownloadDone(Context context, Uri uri) { mm.dumpPrefs(); - if (RootUtils.cmdResult("pm install " + uri.getPath())) + if (ShellUtils.fastCmdResult("pm install " + uri.getPath())) RootUtils.uninstallPkg(context.getPackageName()); } }, diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java b/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java index 66506497b..b214101f9 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java @@ -9,7 +9,6 @@ import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.utils.Const; -import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.superuser.Shell; @@ -41,7 +40,7 @@ public class FlashZip extends ParallelTask { private boolean unzipAndCheck() throws Exception { ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", true); - String s = RootUtils.cmd("head -n 1 " + new File(mCachedFile.getParentFile(), "updater-script")); + String s = ShellUtils.fastCmd("head -n 1 " + new File(mCachedFile.getParentFile(), "updater-script")); return s != null && s.contains("#MAGISK"); } diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java b/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java index 999d51654..92c7bf8a6 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java @@ -58,7 +58,7 @@ public class HideManager extends ParallelTask { MagiskManager mm = MagiskManager.get(); // Generate a new app with random package name - SuFile repack = new SuFile("/data/local/tmp/repack.apk", true); + SuFile repack = new SuFile("/data/local/tmp/repack.apk"); String pkg = genPackageName("com.", Const.ORIG_PKG_NAME.length()); try { diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index b52a1d2bc..9077e6b60 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -13,7 +13,6 @@ import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.utils.Const; -import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.superuser.Shell; @@ -148,9 +147,8 @@ public class InstallMagisk extends ParallelTask { case DIRECT_MODE: console.add("- Patch boot/ramdisk image: " + mBootLocation); if (mm.remoteMagiskVersionCode >= 1463) { - highCompression = Integer.parseInt(RootUtils.cmd(Utils.fmt( - "%s/magiskboot --parse %s; echo $?", - install, mBootLocation))) == 2; + highCompression = Integer.parseInt(ShellUtils.fastCmd(Utils.fmt( + "%s/magiskboot --parse %s; echo $?", install, mBootLocation))) == 2; if (highCompression) console.add("! Insufficient boot partition size detected"); } @@ -263,6 +261,7 @@ public class InstallMagisk extends ParallelTask { .getFilesDir().getParent() , "install"); Shell.Sync.sh("rm -rf " + install); + install.mkdirs(); } List abis = Arrays.asList(Build.SUPPORTED_ABIS); @@ -294,7 +293,7 @@ public class InstallMagisk extends ParallelTask { ); } else { File boot = new File(install, "boot.img"); - SuFile patched_boot = new SuFile(install.getParent() + "/new-boot.img", true); + SuFile patched_boot = new SuFile(install.getParent(), "new-boot.img"); if (!dumpBoot(boot) || !patchBoot(boot, patched_boot)) return false; diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java b/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java index f55bfae8d..fbc26be11 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java @@ -4,8 +4,6 @@ import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.utils.RootUtils; -import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.ShellUtils; public class RestoreImages extends ParallelTask { @@ -13,15 +11,15 @@ public class RestoreImages extends ParallelTask { @Override protected Boolean doInBackground(Void... voids) { String sha1; - sha1 = RootUtils.cmd("cat /.backup/.sha1"); + sha1 = ShellUtils.fastCmd("cat /.backup/.sha1"); if (sha1 == null) { - sha1 = RootUtils.cmd("cat /init.magisk.rc | grep STOCKSHA1"); + sha1 = ShellUtils.fastCmd("cat /init.magisk.rc | grep STOCKSHA1"); if (sha1 == null) return false; sha1 = sha1.substring(sha1.indexOf('=') + 1); } - return ShellUtils.fastCmdResult(Shell.getShell(), "restore_imgs " + sha1); + return ShellUtils.fastCmdResult("restore_imgs " + sha1); } @Override diff --git a/src/full/java/com/topjohnwu/magisk/container/Module.java b/src/full/java/com/topjohnwu/magisk/container/Module.java index 8caeac17c..399854cf9 100644 --- a/src/full/java/com/topjohnwu/magisk/container/Module.java +++ b/src/full/java/com/topjohnwu/magisk/container/Module.java @@ -3,8 +3,6 @@ package com.topjohnwu.magisk.container; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.io.SuFile; -import java.io.IOException; - public class Module extends BaseModule { private SuFile mRemoveFile, mDisableFile, mUpdateFile; @@ -16,9 +14,9 @@ public class Module extends BaseModule { parseProps(Shell.Sync.su("dos2unix < " + path + "/module.prop")); } catch (NumberFormatException ignored) {} - mRemoveFile = new SuFile(path + "/remove", true); - mDisableFile = new SuFile(path + "/disable", true); - mUpdateFile = new SuFile(path + "/update", true); + mRemoveFile = new SuFile(path + "/remove"); + mDisableFile = new SuFile(path + "/disable"); + mUpdateFile = new SuFile(path + "/update"); if (getId() == null) { int sep = path.lastIndexOf('/'); @@ -36,9 +34,7 @@ public class Module extends BaseModule { public void createDisableFile() { mEnable = false; - try { - mDisableFile.createNewFile(); - } catch (IOException ignored) {} + mDisableFile.createNewFile(); } public void removeDisableFile() { @@ -52,9 +48,7 @@ public class Module extends BaseModule { public void createRemoveFile() { mRemove = true; - try { - mRemoveFile.createNewFile(); - } catch (IOException ignored) {} + mRemoveFile.createNewFile(); } public void deleteRemoveFile() { diff --git a/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java b/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java index c8b35aa44..36548119f 100644 --- a/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java +++ b/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java @@ -86,7 +86,7 @@ public class MagiskDatabaseHelper { return de.openOrCreateDatabase("su.db", Context.MODE_PRIVATE, null); } else { // Global database - final SuFile GLOBAL_DB = new SuFile("/data/adb/magisk.db", true); + final SuFile GLOBAL_DB = new SuFile("/data/adb/magisk.db"); mm.deleteDatabase("su.db"); de.deleteDatabase("su.db"); if (mm.magiskVersionCode < Const.MAGISK_VER.SEPOL_REFACTOR) { diff --git a/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java b/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java index 5e81ca9f0..614fc544f 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java +++ b/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java @@ -8,26 +8,18 @@ import com.topjohnwu.superuser.io.SuFile; public class RootUtils { public static void init() { - Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk", true); - SuFile file = new SuFile("/sbin/.core/img", true); + Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); + SuFile file = new SuFile("/sbin/.core/img"); if (file.exists()) { Const.MAGISK_PATH = file; - } else if ((file = new SuFile("/dev/magisk/img", true)).exists()) { + } else if ((file = new SuFile("/dev/magisk/img")).exists()) { Const.MAGISK_PATH = file; } else { - Const.MAGISK_PATH = new SuFile("/magisk", true); + Const.MAGISK_PATH = new SuFile("/magisk"); } Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts"); } - public static String cmd(String cmd) { - return ShellUtils.fastCmd(Shell.getShell(), cmd); - } - - public static boolean cmdResult(String cmd) { - return ShellUtils.fastCmdResult(Shell.getShell(), cmd); - } - public static void uninstallPkg(String pkg) { Shell.Sync.su("db_clean " + Const.USER_ID, "pm uninstall " + pkg); } diff --git a/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java b/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java new file mode 100644 index 000000000..f11d6a324 --- /dev/null +++ b/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java @@ -0,0 +1,32 @@ +package com.topjohnwu.magisk.utils; + +import android.content.Context; +import android.support.annotation.NonNull; + +import com.topjohnwu.magisk.R; +import com.topjohnwu.superuser.BusyBox; +import com.topjohnwu.superuser.Shell; + +import java.io.File; +import java.io.InputStream; + +public class ShellInitializer extends Shell.Initializer { + + @Override + public boolean onRootShellInit(Context context, @NonNull Shell shell) throws Exception { + BusyBox.BB_PATH = new File(Const.BUSYBOX_PATH); + try (InputStream magiskUtils = context.getAssets().open(Const.UTIL_FUNCTIONS); + InputStream managerUtils = context.getResources().openRawResource(R.raw.utils) + ) { + shell.loadInputStream(null, null, magiskUtils); + shell.loadInputStream(null, null, managerUtils); + } + shell.run(null, null, + "mount_partitions", + "find_boot_image", + "find_dtbo_image", + "get_flags", + "run_migrations"); + return true; + } +} diff --git a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java index d05b7b36d..43d97a5ea 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -137,7 +137,7 @@ public class ShowUI { if (Shell.rootAccess()) { options.add(mm.getString(R.string.direct_install)); } - String s = RootUtils.cmd("echo $SLOT"); + String s = ShellUtils.fastCmd("echo $SLOT"); if (s != null) { options.add(mm.getString(R.string.install_second_slot)); } @@ -211,9 +211,9 @@ public class ShowUI { if (slot[1] == 'a') slot[1] = 'b'; else slot[1] = 'a'; // Then find the boot image again - boot = RootUtils.cmd( - "SLOT=" + String.valueOf(slot) + - "; find_boot_image;" + + boot = ShellUtils.fastCmd( + "SLOT=" + String.valueOf(slot), + "find_boot_image", "echo \"$BOOTIMAGE\"" ); Shell.Async.su("mount_partitions");