-
Notifications
You must be signed in to change notification settings - Fork 2
/
gdata_android.go
45 lines (41 loc) · 889 Bytes
/
gdata_android.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
//go:build android
package gdata
import (
"errors"
"os"
"path/filepath"
)
func newDataManager(config Config) (dataManagerImpl, error) {
app, err := detectAndroidApp()
if err != nil {
return nil, err
}
dataPath := filepath.Join("/data/data/", app)
if !fileExists(dataPath) {
return nil, errors.New("can't find the app data directory")
}
m := &filesystemDataManager{
dataPath: dataPath,
}
return m, nil
}
func detectAndroidApp() (string, error) {
data, err := os.ReadFile("/proc/self/cmdline")
if err != nil {
return "", err
}
// Trim any potential "\n" and remove the null bytes.
copied := make([]byte, 0, len(data))
for _, ch := range data {
switch ch {
case 0, '\n':
continue
}
copied = append(copied, ch)
}
result := string(copied)
if result == "" {
return "", errors.New("got empty output from /proc/self/cmdline")
}
return result, nil
}