-
Notifications
You must be signed in to change notification settings - Fork 45
/
package_resolver.go
67 lines (57 loc) · 1.67 KB
/
package_resolver.go
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
package main
import (
"bufio"
"fmt"
"os/exec"
"strings"
)
type PackageMappings map[int]string
type UidMappings map[string]int
var uidPackageMap = make(PackageMappings)
var packageUidMap = make(UidMappings)
// LoadPackageMappings 加载包名和UID的映射
func LoadPackageMappings() error {
// 执行系统命令
cmd := exec.Command("pm", "list", "packages", "-U")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
// 解析命令输出
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
// 样例输出:package:com.example.app uid:10086
parts := strings.Split(line, " ")
if len(parts) != 2 {
continue
}
packagePart := strings.TrimPrefix(parts[0], "package:")
uidPart := strings.TrimPrefix(parts[1], "uid:")
packageName := strings.TrimSpace(packagePart)
var uid int
if _, err := fmt.Sscan(uidPart, &uid); err != nil {
return fmt.Errorf("failed to parse UID: %w", err)
}
uidPackageMap[uid] = packageName
packageUidMap[packageName] = uid
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading command output: %w", err)
}
return nil
}
// GetPackageNameByUid 根据UID获取包名
func GetPackageNameByUid(uid int) (string, error) {
if packageName, ok := uidPackageMap[uid]; ok {
return packageName, nil
}
return "", fmt.Errorf("package name not found for UID %d", uid)
}
// GetUidByPackageName 根据包名获取UID
func GetUidByPackageName(packageName string) (int, error) {
if uid, ok := packageUidMap[packageName]; ok {
return uid, nil
}
return -1, fmt.Errorf("UID not found for package name %s", packageName)
}