From 349b3e961b924be79a272d8982bc1a91e1188d19 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 29 Dec 2017 04:01:39 +0800 Subject: [PATCH] More robust sudb handling --- .../com/topjohnwu/magisk/MagiskManager.java | 4 ++-- .../magisk/database/SuDatabaseHelper.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/src/main/java/com/topjohnwu/magisk/MagiskManager.java index d4c57f55c..45131069f 100644 --- a/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -106,7 +106,7 @@ public class MagiskManager extends Application { } catch (PackageManager.NameNotFoundException ignored) { /* Expected */ } } - suDB = new SuDatabaseHelper(false); + suDB = SuDatabaseHelper.getSuDB(false); repoDB = new RepoDatabaseHelper(this); defaultLocale = Locale.getDefault(); setLocale(); @@ -214,7 +214,7 @@ public class MagiskManager extends Application { if (suDB != null && !SuDatabaseHelper.verified) { suDB.close(); - suDB = new SuDatabaseHelper(); + suDB = SuDatabaseHelper.getSuDB(true); } } diff --git a/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java b/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java index 2a7b61e54..e71abcc20 100644 --- a/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java +++ b/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java @@ -62,7 +62,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { File db = Utils.getDB(context, DB_NAME); if (!verify) { - if (db.length() == 0) { + if (db.exists() && db.length() == 0) { ce.loadMagiskInfo(); // Continue verification } else { @@ -108,7 +108,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { // New global su db Shell.su(Utils.fmt("mkdir %s 2>/dev/null; chmod 700 %s", GLOBAL_DB.getParent(), GLOBAL_DB.getParent())); if (!Utils.itemExist(GLOBAL_DB)) { - Utils.javaCreateFile(db); + context.openOrCreateDatabase(DB_NAME, 0, null).close(); Shell.su(Utils.fmt("cp -af %s %s; rm -f %s*", db, GLOBAL_DB, db)); } verified = TextUtils.equals(Utils.checkInode(GLOBAL_DB), Utils.checkInode(db)); @@ -125,12 +125,17 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { return context; } - public SuDatabaseHelper() { - this(true); - } - - public SuDatabaseHelper(boolean verify) { - this(initDB(verify)); + public static SuDatabaseHelper getSuDB(boolean verify) { + try { + return new SuDatabaseHelper(initDB(verify)); + } catch(Exception e) { + // Try to catch runtime exceptions and remove all db for retry + unmntDB(); + Shell.su(Utils.fmt("rm -rf /data/user*/*/magisk.db /data/adb/magisk.db /data/user*/*/%s/databases"), + MagiskManager.get().getPackageName()); + e.printStackTrace(); + return new SuDatabaseHelper(initDB(false)); + } } private SuDatabaseHelper(Context context) {