-
Notifications
You must be signed in to change notification settings - Fork 3
/
adb_utils.go
200 lines (154 loc) · 3.13 KB
/
adb_utils.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
adb "github.com/zach-klippenstein/goadb"
)
func checkAdb() bool {
_, err := getAdb()
if err != nil {
showErrorMsg(err, false)
return false
}
return true
}
func getAdb() (*adb.Device, error) {
client, err := adb.NewWithConfig(adb.ServerConfig{})
if err != nil {
return nil, fmt.Errorf("ADB client not found")
}
device := client.Device(adb.AnyDevice())
state, err := device.State()
if err != nil || state != adb.StateOnline {
return nil, fmt.Errorf("ADB device not found")
}
return device, nil
}
func isAdbSymDir(testPath, name string) bool {
device, err := getAdb()
if err != nil {
return false
}
cmd := fmt.Sprintf("ls -pd %s%s/", testPath, name)
out, err := device.RunCommand(cmd)
if err != nil {
return false
}
if !strings.HasSuffix(strings.TrimSpace(out), "//") {
return false
}
return true
}
func (o *operation) adbOps(src, dst string) error {
var err error
device, err := getAdb()
if err != nil {
return err
}
switch o.transfer {
case adbToAdb:
err = o.execAdbCmd(src, dst, device)
case localToAdb:
err = o.pushRecursive(src, dst, device)
case adbToLocal:
err = o.pullRecursive(src, dst, device)
}
return err
}
func (o *operation) execAdbCmd(src, dst string, device *adb.Device) error {
var cmd string
srcfmt := fmt.Sprintf(" '%s'", src)
dstfmt := fmt.Sprintf(" '%s'", dst)
param := srcfmt + dstfmt
switch o.opmode {
case opMkdir:
cmd = "mkdir"
param = srcfmt
default:
stat, err := device.Stat(src)
if err != nil {
return err
}
switch o.opmode {
case opRename:
_, err := device.Stat(dst)
if err == nil {
return fmt.Errorf("rename %s %s: file exists", src, dst)
}
fallthrough
case opMove:
cmd = "mv"
case opCopy:
if stat.Mode.IsDir() {
cmd = "cp -r"
} else {
cmd = "cp"
}
case opDelete:
if stat.Mode.IsDir() {
cmd = "rm -rf"
} else {
cmd = "rm"
}
param = srcfmt
}
}
cmd = cmd + param
out, err := exec.CommandContext(o.ctx, "adb", "shell", cmd).Output()
if err != nil {
if err.Error() == "signal: killed" {
return error(context.Canceled)
}
return err
}
if string(out) != "" {
return fmt.Errorf(string(out))
}
return nil
}
func (p *dirPane) adbListDir(testPath string, autocomplete bool) ([]string, bool) {
var dlist []string
device, err := getAdb()
if err != nil {
showErrorMsg(err, autocomplete)
return nil, false
}
_, err = device.Stat(testPath)
if err != nil {
showErrorMsg(err, autocomplete)
return nil, false
}
dent, err := device.ListDirEntries(testPath)
if err != nil {
showErrorMsg(err, autocomplete)
return nil, false
}
if !autocomplete {
p.pathList = nil
}
for dent.Next() {
ent := dent.Entry()
name := ent.Name
if name == ".." || name == "." {
continue
}
if p.getHidden() && strings.HasPrefix(name, ".") {
continue
}
if ent.Mode&os.ModeDir != 0 {
dlist = append(dlist, filepath.Join(testPath, name))
}
if autocomplete {
continue
}
p.pathList = append(p.pathList, ent)
}
if dent.Err() != nil {
return nil, false
}
return dlist, true
}