Skip to content

Commit cfbb2fd

Browse files
authored
fix(remoteocd): windows compatibility (#438)
1 parent f66879e commit cfbb2fd

File tree

1 file changed

+12
-109
lines changed

1 file changed

+12
-109
lines changed

cmd/remoteocd/adb.go

Lines changed: 12 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package main
22

33
import (
4-
"bufio"
5-
"bytes"
64
"context"
75
"fmt"
86
"io"
9-
"io/fs"
107
"os"
118
"os/exec"
129
"strings"
@@ -18,137 +15,43 @@ import (
1815
type ADBConnection struct {
1916
host string
2017
adbPath string
18+
conn remote.RemoteFs
2119
}
2220

2321
func FromSerial(serial string, adbPath string) *ADBConnection {
24-
if adbPath == "" {
25-
adbPath = adb.FindAdbPath()
26-
}
22+
serial = strings.ToLower(serial) // ensure serial is always lowecase (especially on windows)
23+
24+
conn, _ := adb.FromSerial(serial, adbPath)
25+
2726
return &ADBConnection{
2827
host: serial,
2928
adbPath: adbPath,
29+
conn: conn,
3030
}
3131
}
3232

3333
func (a *ADBConnection) List(path string) ([]remote.FileInfo, error) {
34-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "ls", "-la", path) // nolint:gosec
35-
cmd.Stderr = os.Stdout
36-
output, err := cmd.StdoutPipe()
37-
if err != nil {
38-
return nil, err
39-
}
40-
defer output.Close()
41-
if err := cmd.Start(); err != nil {
42-
return nil, err
43-
}
44-
45-
r := bufio.NewReader(output)
46-
_, err = r.ReadBytes('\n') // Skip the first line
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
var files []remote.FileInfo
52-
for {
53-
line, err := r.ReadBytes('\n')
54-
if err != nil {
55-
if err == io.EOF {
56-
break
57-
}
58-
return nil, err
59-
}
60-
line = bytes.TrimSpace(line)
61-
if len(line) == 0 {
62-
continue
63-
}
64-
parts := bytes.Split(line, []byte(" "))
65-
name := string(parts[len(parts)-1])
66-
if name == "." || name == ".." {
67-
continue
68-
}
69-
files = append(files, remote.FileInfo{
70-
Name: name,
71-
IsDir: line[0] == 'd',
72-
})
73-
}
74-
75-
return files, nil
34+
return a.conn.List(path)
7635
}
7736

7837
func (a *ADBConnection) Stats(path string) (remote.FileInfo, error) {
79-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "file", path) // nolint:gosec
80-
output, err := cmd.StdoutPipe()
81-
if err != nil {
82-
return remote.FileInfo{}, err
83-
}
84-
defer output.Close()
85-
if err := cmd.Start(); err != nil {
86-
return remote.FileInfo{}, err
87-
}
88-
89-
r := bufio.NewReader(output)
90-
line, err := r.ReadBytes('\n')
91-
if err != nil {
92-
return remote.FileInfo{}, err
93-
}
94-
95-
line = bytes.TrimSpace(line)
96-
parts := bytes.Split(line, []byte(":"))
97-
if len(parts) < 2 {
98-
return remote.FileInfo{}, fmt.Errorf("unexpected file command output: %s", line)
99-
}
100-
101-
name := string(bytes.TrimSpace(parts[0]))
102-
other := string(bytes.TrimSpace(parts[1]))
103-
104-
if strings.Contains(other, "cannot open") {
105-
return remote.FileInfo{}, fs.ErrNotExist
106-
}
107-
108-
return remote.FileInfo{
109-
Name: name,
110-
IsDir: other == "directory",
111-
}, nil
38+
return a.conn.Stats(path)
11239
}
11340

11441
func (a *ADBConnection) ReadFile(path string) (io.ReadCloser, error) {
115-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "cat", path) // nolint:gosec
116-
output, err := cmd.StdoutPipe()
117-
if err != nil {
118-
return nil, err
119-
}
120-
if err := cmd.Start(); err != nil {
121-
return nil, err
122-
}
123-
return output, nil
42+
return a.conn.ReadFile(path)
12443
}
12544

12645
func (a *ADBConnection) WriteFile(r io.Reader, path string) error {
127-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "cat", ">", path) // nolint:gosec
128-
cmd.Stdin = r
129-
out, err := cmd.CombinedOutput()
130-
if err != nil {
131-
return fmt.Errorf("failed to write file %q: %w: %s", path, err, out)
132-
}
133-
return nil
46+
return a.conn.WriteFile(r, path)
13447
}
13548

13649
func (a *ADBConnection) MkDirAll(path string) error {
137-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "mkdir", "-p", path) // nolint:gosec
138-
out, err := cmd.CombinedOutput()
139-
if err != nil {
140-
return fmt.Errorf("failed to create directory %q: %w: %s", path, err, out)
141-
}
142-
return nil
50+
return a.conn.MkDirAll(path)
14351
}
14452

14553
func (a *ADBConnection) Remove(path string) error {
146-
cmd := exec.Command(a.adbPath, "-s", a.host, "shell", "rm", "-r", path) // nolint:gosec
147-
out, err := cmd.CombinedOutput()
148-
if err != nil {
149-
return fmt.Errorf("failed to remove path %q: %w: %s", path, err, out)
150-
}
151-
return nil
54+
return a.conn.Remove(path)
15255
}
15356

15457
func (a *ADBConnection) RunOpenOcd(ctx context.Context, path, address string) (string, error) {

0 commit comments

Comments
 (0)