pathAccess.js
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* 同一安装包在 Android 上可能出现的多套私有根路径(外置 /data/user /data/data)。
* 仅处理当前运行包名,不扫描其它应用。
*/
function normalizeAccessPath(path) {
if (!path) return ''
return String(path)
.replace(/^file:\/\//, '')
.trim()
.replace(/\/+$/, '')
}
function escapeRegExp(str) {
return String(str).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/** 从包根或 doc 路径解析 Android 多用户 id,默认 0 */
export function parseAndroidUserIdFromPath(path) {
const p = normalizeAccessPath(path)
const m = p.match(/^\/data\/user\/(\d+)\//)
return m ? m[1] : '0'
}
/**
* 当前应用(单包名)在设备上可能出现的包根目录
* @param {string} packageName 当前运行包名
* @param {string} [hintRoot] 当前 doc 反推的包根,用于带上实际 /data/user/N
*/
export function getSameAppPackageRoots(packageName, hintRoot) {
if (!packageName) return []
const seen = new Set()
const roots = []
const add = path => {
const n = normalizeAccessPath(path)
if (!n || seen.has(n)) return
seen.add(n)
roots.push(n)
}
add(`/storage/emulated/0/Android/data/${packageName}`)
add(`/data/data/${packageName}`)
const userIds = new Set(['0'])
if (hintRoot) userIds.add(parseAndroidUserIdFromPath(hintRoot))
for (const uid of userIds) {
add(`/data/user/${uid}/${packageName}`)
}
return roots
}
/**
* 是否属于当前应用(单包名)的存储根(外置或内部)
*/
export function isOwnAppStoragePath(path, packageName) {
const p = normalizeAccessPath(path)
if (!p || !packageName) return false
return getSameAppPackageRoots(packageName).some(
root => p === root || p.startsWith(`${root}/`)
)
}
/** @deprecated 使用 isOwnAppStoragePath */
export function isOwnAppDataPath(path, packageNames) {
const pkg = Array.isArray(packageNames) ? packageNames[0] : packageNames
return isOwnAppStoragePath(path, pkg)
}
/**
* 是否应跳过:内部路径且不属于当前应用
*/
export function isRestrictedInternalPath(path, packageName) {
const p = normalizeAccessPath(path)
if (!p.startsWith('/data/data/') && !p.startsWith('/data/user/')) return false
if (packageName && isOwnAppStoragePath(path, packageName)) return false
return true
}
export function isExternalAppDataRoot(path) {
if (!path) return false
return normalizeAccessPath(path).includes('/Android/data/')
}
/** @deprecated 使用 getSameAppPackageRoots */
export function getInternalPackageRoots(packageNames) {
const pkg = Array.isArray(packageNames) ? packageNames[0] : packageNames
return getSameAppPackageRoots(pkg).filter(
r => r.startsWith('/data/data/') || r.startsWith('/data/user/')
)
}
/** @deprecated 使用 getSameAppPackageRoots */
export function getMergeAccessiblePackageRoots(getPackageNamesForMerge) {
const names = typeof getPackageNamesForMerge === 'function' ? getPackageNamesForMerge() : []
const pkg = names[0]
if (!pkg) return []
return getSameAppPackageRoots(pkg).filter(r => r.includes('/Android/data/'))
}