Skip to content

Commit 5e74a13

Browse files
committed
fix: #344
Change-Id: I7e56b5c971654533cd201f3cd5b2b5b5c5d8eb39
1 parent a4bd2c4 commit 5e74a13

File tree

2 files changed

+11
-40
lines changed

2 files changed

+11
-40
lines changed

source/core/rootservice/src/main/kotlin/com/xayah/core/rootservice/impl/RemoteRootServiceImpl.kt

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ import java.nio.file.attribute.BasicFileAttributes
5252
import kotlin.io.path.pathString
5353

5454
internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {
55-
companion object {
56-
const val ParcelTmpFilePath = "/data/local/tmp"
57-
const val ParcelTmpFileName = "data_backup_tmp"
58-
}
59-
6055
private val lock = Any()
6156
private var systemContext: Context
6257
private var packageManager: PackageManager
@@ -78,25 +73,6 @@ internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {
7873
private fun getAppOpsManager(): AppOpsManagerHidden = systemContext.getSystemService(Context.APP_OPS_SERVICE).castTo()
7974

8075
init {
81-
/**
82-
* If [ParcelTmpFilePath] has incorrect SELinux context, the transaction will get failed:
83-
* Fatal Exception: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died, but this could also be caused by running out of binder buffe
84-
* Correct SELinux context should be: u:object_r:shell_data_file:s0
85-
*
86-
* If [ParcelTmpFilePath] doesn't exist, the transaction will failed:
87-
* pfd must not be null
88-
*/
89-
ShellUtils.fastCmd(
90-
"""
91-
mkdir "$ParcelTmpFilePath/"
92-
""".trimIndent()
93-
)
94-
ShellUtils.fastCmd(
95-
"""
96-
chcon -hR "u:object_r:shell_data_file:s0" "$ParcelTmpFilePath/"
97-
""".trimIndent()
98-
)
99-
10076
systemContext = getSystemContext()
10177
packageManager = systemContext.packageManager
10278
packageManagerHidden = packageManager.castTo()
@@ -151,20 +127,18 @@ internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {
151127
FileUtil.listFilePaths(path = path, listFiles = listFiles, listDirs = listDirs)
152128
}
153129

154-
private fun writeToParcel(onWrite: (Parcel) -> Unit) = run {
130+
private fun writeToParcel(block: (Parcel) -> Unit): ParcelFileDescriptor {
155131
val parcel = Parcel.obtain()
156132
parcel.setDataPosition(0)
157-
158-
onWrite(parcel)
159-
160-
val tmp = File(ParcelTmpFilePath, ParcelTmpFileName)
161-
tmp.createNewFile()
162-
tmp.writeBytes(parcel.marshall())
163-
val pfd = ParcelFileDescriptor.open(tmp, ParcelFileDescriptor.MODE_READ_WRITE)
164-
tmp.deleteRecursively()
165-
133+
block(parcel)
134+
val tmpFile = File.createTempFile("databackup-parcel-", ".tmp")
135+
tmpFile.delete()
136+
tmpFile.createNewFile()
137+
tmpFile.writeBytes(parcel.marshall())
138+
val pfd = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_WRITE)
139+
tmpFile.delete()
166140
parcel.recycle()
167-
pfd
141+
return pfd
168142
}
169143

170144
override fun readText(path: String): ParcelFileDescriptor = synchronized(lock) {

source/core/rootservice/src/main/kotlin/com/xayah/core/rootservice/service/RemoteRootService.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,13 @@ class RemoteRootService(private val context: Context) {
201201
suspend fun listFilePaths(path: String, listFiles: Boolean = true, listDirs: Boolean = true): List<String> =
202202
runCatching { getService().listFilePaths(path, listFiles, listDirs) }.onFailure(onFailure).getOrElse { listOf() }
203203

204-
private fun readFromParcel(pfd: ParcelFileDescriptor, onRead: (Parcel) -> Unit) = run {
204+
private fun readFromParcel(pfd: ParcelFileDescriptor, block: (Parcel) -> Unit) = run {
205205
val stream = ParcelFileDescriptor.AutoCloseInputStream(pfd)
206206
val bytes = stream.readBytes()
207207
val parcel = Parcel.obtain()
208-
209208
parcel.unmarshall(bytes, 0, bytes.size)
210209
parcel.setDataPosition(0)
211-
212-
onRead(parcel)
213-
210+
block(parcel)
214211
parcel.recycle()
215212
}
216213

0 commit comments

Comments
 (0)