Skip to content

Commit

Permalink
Temporarily use /data/user_de for MIUI Android 11
Browse files Browse the repository at this point in the history
Adb on MIUI Android 11 has no permission to access Android/data.
Before MIUI Android 12, we can temporarily use /data/user_de.
After that, is better to implement "adb push" and push files directly to /data/local/tmp.
  • Loading branch information
RikkaW committed Feb 22, 2021
1 parent cc2e993 commit 9846c5c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
31 changes: 27 additions & 4 deletions manager/src/main/java/moe/shizuku/manager/starter/Starter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package moe.shizuku.manager.starter
import android.content.Context
import android.os.Build
import android.os.UserManager
import android.system.ErrnoException
import android.system.Os
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import moe.shizuku.manager.BuildConfig
import moe.shizuku.manager.R
import moe.shizuku.manager.ktx.createDeviceProtectedStorageContextCompat
import moe.shizuku.manager.ktx.logd
Expand Down Expand Up @@ -51,18 +52,40 @@ object Starter {
}
}

fun writeDataFiles(context: Context) {
if (commandInternal[0] != null) {
fun writeDataFiles(context: Context, permission: Boolean = false) {
if (commandInternal[0] != null && !permission) {
logd("already written")
return
}

val dir = context.createDeviceProtectedStorageContextCompat().filesDir?.parentFile ?: return

if (permission) {
try {
Os.chmod(dir.absolutePath, 457 /* 0711 */)
} catch (e: ErrnoException) {
e.printStackTrace()
}
}

try {
val dir = context.createDeviceProtectedStorageContextCompat().filesDir?.parentFile ?: return
val starter = copyStarter(context, File(dir, "starter"))
val sh = writeScript(context, File(dir, "start.sh"), starter)
commandInternal[0] = "sh $sh"
logd(commandInternal[0]!!)

if (permission) {
try {
Os.chmod(starter, 420 /* 0644 */)
} catch (e: ErrnoException) {
e.printStackTrace()
}
try {
Os.chmod(sh, 420 /* 0644 */)
} catch (e: ErrnoException) {
e.printStackTrace()
}
}
} catch (e: IOException) {
loge("write files", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import moe.shizuku.manager.AppConstants.EXTRA
import moe.shizuku.manager.BuildConfig
import moe.shizuku.manager.R
import moe.shizuku.manager.ShizukuSettings
import moe.shizuku.manager.adb.AdbClient
Expand Down Expand Up @@ -205,6 +206,34 @@ private class ViewModel(context: Context, root: Boolean, host: String?, port: In
sb.append('\n').append(it.toString())
postResult(it)
}

/* Adb on MIUI Android 11 has no permission to access Android/data.
Before MIUI Android 12, we can temporarily use /data/user_de.
After that, is better to implement "adb push" and push files directly to /data/local/tmp.
*/
if (sb.contains("/Android/data/${BuildConfig.APPLICATION_ID}/start.sh: Permission denied")) {
sb.append('\n')
.appendLine("adb have no permission to access Android/data, how could this possible ?!")
.appendLine("try /data/user_de instead...")
.appendLine()
postResult()

Starter.writeDataFiles(application, true)

AdbClient(host, port, key).runCatching {
connect()
shellCommand(Starter.dataCommand) {
sb.append(String(it))
postResult()
}
close()
}.onFailure {
it.printStackTrace()

sb.append('\n').append(it.toString())
postResult(it)
}
}
}
}
}

0 comments on commit 9846c5c

Please sign in to comment.