Fix F2FS crashes on SQLite 3.21.0

This commit is contained in:
topjohnwu 2018-02-18 18:12:12 +08:00
parent 74379150a1
commit c1423ca9ad
3 changed files with 57 additions and 42 deletions

View File

@ -7,6 +7,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Build; import android.os.Build;
import android.os.Process; import android.os.Process;
import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.widget.Toast; import android.widget.Toast;
@ -17,6 +18,7 @@ import com.topjohnwu.magisk.container.SuLogEntry;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.io.SuFile;
import java.io.File; import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
@ -37,6 +39,7 @@ public class SuDatabaseHelper {
private SQLiteDatabase mDb; private SQLiteDatabase mDb;
private File DB_FILE; private File DB_FILE;
@NonNull
public static SuDatabaseHelper getInstance(MagiskManager mm) { public static SuDatabaseHelper getInstance(MagiskManager mm) {
try { try {
return new SuDatabaseHelper(mm); return new SuDatabaseHelper(mm);
@ -61,7 +64,7 @@ public class SuDatabaseHelper {
} }
private SQLiteDatabase openDatabase(MagiskManager mm) { private SQLiteDatabase openDatabase(MagiskManager mm) {
String GLOBAL_DB = "/data/adb/magisk.db"; final SuFile GLOBAL_DB = new SuFile("/data/adb/magisk.db", true);
DB_FILE = new File(Utils.fmt("/sbin/.core/db-%d/magisk.db", Const.USER_ID)); DB_FILE = new File(Utils.fmt("/sbin/.core/db-%d/magisk.db", Const.USER_ID));
Context de = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N Context de = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? mm.createDeviceProtectedStorageContext() : mm; ? mm.createDeviceProtectedStorageContext() : mm;
@ -89,16 +92,23 @@ public class SuDatabaseHelper {
mm.deleteDatabase("su.db"); mm.deleteDatabase("su.db");
de.deleteDatabase("su.db"); de.deleteDatabase("su.db");
if (mm.magiskVersionCode < 1460) { if (mm.magiskVersionCode < 1460) {
// v14.5 global DB location // Link to new path
GLOBAL_DB = new File(de.getFilesDir().getParentFile().getParentFile(), File oldDB = new File(de.getFilesDir().getParentFile().getParentFile(),
"magisk.db").getPath(); "magisk.db");
Shell.Sync.su(Utils.fmt("mv -f %s %s; ln -s %s %s",
oldDB, GLOBAL_DB, GLOBAL_DB, oldDB));
} }
if (mm.magiskVersionCode < 1550) { if (mm.magiskVersionCode < 1550) {
// We need some additional policies on old versions // We need some additional policies on old versions
Shell.Sync.su("magiskpolicy --live " + Shell.Sync.su("magiskpolicy --live " +
"'create su_file' 'allow * su_file file *' 'allow * su_file dir *'"); "'create su_file' 'allow * su_file file *' 'allow * su_file dir *'");
} }
Shell.Sync.su("sudb_setup " + Process.myUid() + " " + GLOBAL_DB); if (!GLOBAL_DB.exists()) {
Shell.Sync.su("sudb_init");
SQLiteDatabase.openOrCreateDatabase(GLOBAL_DB, null).close();
Shell.Sync.su("sudb_restore");
}
Shell.Sync.su("sudb_setup " + Process.myUid());
} }
} }
// Not using legacy mode, open the mounted global DB // Not using legacy mode, open the mounted global DB
@ -106,7 +116,6 @@ public class SuDatabaseHelper {
} }
public void onUpgrade(SQLiteDatabase db, int oldVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion) {
try {
if (oldVersion == 0) { if (oldVersion == 0) {
createTables(db); createTables(db);
oldVersion = 3; oldVersion = 3;
@ -139,10 +148,6 @@ public class SuDatabaseHelper {
db.execSQL(Utils.fmt("UPDATE %s SET uid=uid%%100000", POLICY_TABLE)); db.execSQL(Utils.fmt("UPDATE %s SET uid=uid%%100000", POLICY_TABLE));
++oldVersion; ++oldVersion;
} }
} catch (Exception e) {
e.printStackTrace();
onDowngrade(db);
}
} }
// Remove everything, we do not support downgrade // Remove everything, we do not support downgrade

View File

@ -27,7 +27,6 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity; import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;

View File

@ -3,17 +3,28 @@ sudb_clean() {
local DIR="/sbin/.core/db-${USERID}" local DIR="/sbin/.core/db-${USERID}"
umount -l /data/user*/*/*/databases/su.db $DIR $DIR/* umount -l /data/user*/*/*/databases/su.db $DIR $DIR/*
rm -rf $DIR rm -rf $DIR
[ "$USERID" = "*" ] && rm -f /data/adb/magisk.db
}
sudb_init() {
ADB_CONTEXT=`/system/bin/ls -dZ /data/adb | awk '{print $1}'`
chcon u:object_r:su_file:s0 /data/adb
chmod 777 /data/adb
}
sudb_restore() {
chcon $ADB_CONTEXT /data/adb
chmod 700 /data/adb
} }
sudb_setup() { sudb_setup() {
local USER=$1 local USER=$1
local GLOBAL_DB=$2
local USERID=$(($USER / 100000)) local USERID=$(($USER / 100000))
local DIR=/sbin/.core/db-${USERID} local DIR=/sbin/.core/db-${USERID}
touch $GLOBAL_DB
mkdir -p $DIR mkdir -p $DIR
touch $DIR/magisk.db touch $DIR/magisk.db
mount -o bind $GLOBAL_DB $DIR/magisk.db mount -o bind /data/adb/magisk.db $DIR/magisk.db
rm -f /data/adb/magisk.db-journal
chcon u:object_r:su_file:s0 $DIR $DIR/* chcon u:object_r:su_file:s0 $DIR $DIR/*
chmod 700 $DIR chmod 700 $DIR
chown $USER.$USER $DIR chown $USER.$USER $DIR