@@ -37,6 +37,9 @@ import kotlinx.coroutines.launch
3737import javax.inject.Inject
3838import java.io.File
3939
40+ /* * `/proc/1/root` 是 init 的根视图,可绕开 app mount namespace 的目录项隔离。 */
41+ private const val INIT_ROOT_PREFIX = " /proc/1/root"
42+
4043class PackagesBackupUtil @Inject constructor(
4144 @ApplicationContext val context : Context ,
4245 private val rootService : RemoteRootService ,
@@ -257,7 +260,7 @@ class PackagesBackupUtil @Inject constructor(
257260 return ArgvResult .Skip
258261 }
259262
260- val srcDir = packageRepository.getDataSrcDir (dataType, userId)
263+ val srcDir = resolveDataSrcDir (dataType, userId, packageName )
261264 val src = packageRepository.getDataSrc(srcDir, packageName)
262265 if (rootService.exists(src).not ()) {
263266 return if (dataType == DataType .PACKAGE_USER ) {
@@ -303,6 +306,48 @@ class PackagesBackupUtil @Inject constructor(
303306 )
304307 }
305308
309+ /* *
310+ * PACKAGE_USER / PACKAGE_USER_DE 的源目录有几个等价视图:
311+ * `/data/user/<userId>`(与 `/data/data` 同源)、`/data/user_de/<userId>`、`/proc/1/root` 下的同名路径。
312+ *
313+ * 实测(ColorOS / Android 15):root 守护进程(libsu RootService 的独立 app_process 进程)
314+ * 继承的是 app 的 mount namespace(日志里 `same=false`),该 namespace 下 `/data`、`/data/user`、
315+ * `/data/user/0`、`/data/data`、`/data/user_de`、`/storage/emulated/0` 目录本身都可见,但**目录项
316+ * 被 ROM 隔离成只含本应用的视图** —— `Os.stat("/data/user/0/<other pkg>")` 与
317+ * `/data/data/<other pkg>` 都是 ENOENT(不是 EACCES),而 `/data/app`、`/data/media/0` 的子项正常。
318+ *
319+ * 因此这里按「父目录 + 包名」探测,探测不到时依次回退:
320+ * 1. `/proc/1/root<原路径>` —— init 的根视图,不在 app 的隔离 namespace 内;
321+ * 2. `/data/data` —— PACKAGE_USER 的等价挂载(仅主用户)。
322+ * 回退结果同时用于 `tar -C`,保证「探测」和「打包」落在同一个可见视图上。
323+ */
324+ private suspend fun resolveDataSrcDir (dataType : DataType , userId : Int , packageName : String ): String {
325+ val primary = packageRepository.getDataSrcDir(dataType, userId)
326+ if (dataType != DataType .PACKAGE_USER && dataType != DataType .PACKAGE_USER_DE ) return primary
327+ if (rootService.exists(" $primary /$packageName " )) return primary
328+
329+ for (dir in namespaceFallbackDirs(dataType, userId)) {
330+ if (dir == primary) continue
331+ if (rootService.exists(" $dir /$packageName " )) {
332+ log { " Src dir $primary /$packageName invisible to root service, fallback to $dir ." }
333+ return dir
334+ }
335+ }
336+ return primary
337+ }
338+
339+ private fun namespaceFallbackDirs (dataType : DataType , userId : Int ): List <String > = buildList {
340+ when (dataType) {
341+ DataType .PACKAGE_USER -> {
342+ add(" $INIT_ROOT_PREFIX${PathUtil .getPackageUserDir(userId)} " )
343+ if (userId == 0 ) add(PathUtil .getDataDataDir())
344+ }
345+
346+ DataType .PACKAGE_USER_DE -> add(" $INIT_ROOT_PREFIX${PathUtil .getPackageUserDeDir(userId)} " )
347+ else -> {}
348+ }
349+ }
350+
306351 /* *
307352 * fd 流式入库:ArgvResult.Ready → remoteRootService.backupTarToRustic。
308353 * 返回 Pair<ShellResult, snapshotId?>:ShellResult.code=0 成功(exit=1 部分成功也归 0),
0 commit comments