goadb/device_client.go

267 lines
7.8 KiB
Go
Raw Normal View History

package goadb
import (
"fmt"
"io"
"os"
"strings"
"time"
2015-07-12 06:18:58 +00:00
"github.com/zach-klippenstein/goadb/util"
"github.com/zach-klippenstein/goadb/wire"
)
// MtimeOfClose should be passed to OpenWrite to set the file modification time to the time the Close
// method is called.
var MtimeOfClose = time.Time{}
// DeviceClient communicates with a specific Android device.
type DeviceClient struct {
server Server
2015-07-11 21:32:04 +00:00
descriptor DeviceDescriptor
// Used to get device info.
deviceListFunc func() ([]*DeviceInfo, error)
2015-07-11 21:32:04 +00:00
}
func NewDeviceClient(server Server, descriptor DeviceDescriptor) *DeviceClient {
2015-07-11 21:32:04 +00:00
return &DeviceClient{
server: server,
descriptor: descriptor,
deviceListFunc: NewHostClient(server).ListDevices,
2015-07-11 21:32:04 +00:00
}
}
func (c *DeviceClient) String() string {
return c.descriptor.String()
}
// get-product is documented, but not implemented in the server.
// TODO(z): Make getProduct exported if get-product is ever implemented in adb.
func (c *DeviceClient) getProduct() (string, error) {
2015-07-12 06:18:58 +00:00
attr, err := c.getAttribute("get-product")
return attr, wrapClientError(err, c, "GetProduct")
}
func (c *DeviceClient) GetSerial() (string, error) {
2015-07-12 06:18:58 +00:00
attr, err := c.getAttribute("get-serialno")
return attr, wrapClientError(err, c, "GetSerial")
}
func (c *DeviceClient) GetDevicePath() (string, error) {
2015-07-12 06:18:58 +00:00
attr, err := c.getAttribute("get-devpath")
return attr, wrapClientError(err, c, "GetDevicePath")
}
func (c *DeviceClient) GetState() (string, error) {
2015-07-12 06:18:58 +00:00
attr, err := c.getAttribute("get-state")
return attr, wrapClientError(err, c, "GetState")
}
func (c *DeviceClient) GetDeviceInfo() (*DeviceInfo, error) {
// Adb doesn't actually provide a way to get this for an individual device,
// so we have to just list devices and find ourselves.
serial, err := c.GetSerial()
if err != nil {
return nil, wrapClientError(err, c, "GetDeviceInfo(GetSerial)")
}
devices, err := c.deviceListFunc()
if err != nil {
return nil, wrapClientError(err, c, "GetDeviceInfo(ListDevices)")
}
for _, deviceInfo := range devices {
if deviceInfo.Serial == serial {
return deviceInfo, nil
}
}
err = util.Errorf(util.DeviceNotFound, "device list doesn't contain serial %s", serial)
return nil, wrapClientError(err, c, "GetDeviceInfo")
}
/*
RunCommand runs the specified commands on a shell on the device.
From the Android docs:
Run 'command arg1 arg2 ...' in a shell on the device, and return
its output and error streams. Note that arguments must be separated
by spaces. If an argument contains a space, it must be quoted with
double-quotes. Arguments cannot contain double quotes or things
will go very wrong.
Note that this is the non-interactive version of "adb shell"
Source: https://android.googlesource.com/platform/system/core/+/master/adb/SERVICES.TXT
This method quotes the arguments for you, and will return an error if any of them
contain double quotes.
*/
func (c *DeviceClient) RunCommand(cmd string, args ...string) (string, error) {
cmd, err := prepareCommandLine(cmd, args...)
if err != nil {
2015-07-12 06:18:58 +00:00
return "", wrapClientError(err, c, "RunCommand")
}
conn, err := c.dialDevice()
if err != nil {
2015-07-12 06:18:58 +00:00
return "", wrapClientError(err, c, "RunCommand")
}
defer conn.Close()
req := fmt.Sprintf("shell:%s", cmd)
// Shell responses are special, they don't include a length header.
// We read until the stream is closed.
// So, we can't use conn.RoundTripSingleResponse.
if err = conn.SendMessage([]byte(req)); err != nil {
2015-07-12 06:18:58 +00:00
return "", wrapClientError(err, c, "RunCommand")
}
if _, err = conn.ReadStatus(req); err != nil {
2015-07-12 06:18:58 +00:00
return "", wrapClientError(err, c, "RunCommand")
}
resp, err := conn.ReadUntilEof()
2015-07-12 06:18:58 +00:00
return string(resp), wrapClientError(err, c, "RunCommand")
}
/*
Remount, from the docs,
Ask adbd to remount the device's filesystem in read-write mode,
instead of read-only. This is usually necessary before performing
an "adb sync" or "adb push" request.
This request may not succeed on certain builds which do not allow
that.
Source: https://android.googlesource.com/platform/system/core/+/master/adb/SERVICES.TXT
*/
func (c *DeviceClient) Remount() (string, error) {
conn, err := c.dialDevice()
if err != nil {
2015-07-12 06:18:58 +00:00
return "", wrapClientError(err, c, "Remount")
}
defer conn.Close()
resp, err := conn.RoundTripSingleResponse([]byte("remount"))
2015-07-12 06:18:58 +00:00
return string(resp), wrapClientError(err, c, "Remount")
}
func (c *DeviceClient) ListDirEntries(path string) (*DirEntries, error) {
conn, err := c.getSyncConn()
if err != nil {
2015-07-12 06:18:58 +00:00
return nil, wrapClientError(err, c, "ListDirEntries(%s)", path)
}
2015-07-12 06:18:58 +00:00
entries, err := listDirEntries(conn, path)
return entries, wrapClientError(err, c, "ListDirEntries(%s)", path)
}
func (c *DeviceClient) Stat(path string) (*DirEntry, error) {
conn, err := c.getSyncConn()
if err != nil {
2015-07-12 06:18:58 +00:00
return nil, wrapClientError(err, c, "Stat(%s)", path)
}
defer conn.Close()
2015-07-12 06:18:58 +00:00
entry, err := stat(conn, path)
return entry, wrapClientError(err, c, "Stat(%s)", path)
}
func (c *DeviceClient) OpenRead(path string) (io.ReadCloser, error) {
conn, err := c.getSyncConn()
if err != nil {
2015-07-12 06:18:58 +00:00
return nil, wrapClientError(err, c, "OpenRead(%s)", path)
}
2015-07-12 06:18:58 +00:00
reader, err := receiveFile(conn, path)
return reader, wrapClientError(err, c, "OpenRead(%s)", path)
}
// OpenWrite opens the file at path on the device, creating it with the permissions specified
// by perms if necessary, and returns a writer that writes to the file.
// The files modification time will be set to mtime when the WriterCloser is closed. The zero value
// is TimeOfClose, which will use the time the Close method is called as the modification time.
func (c *DeviceClient) OpenWrite(path string, perms os.FileMode, mtime time.Time) (io.WriteCloser, error) {
conn, err := c.getSyncConn()
if err != nil {
return nil, wrapClientError(err, c, "OpenWrite(%s)", path)
}
writer, err := sendFile(conn, path, perms, mtime)
return writer, wrapClientError(err, c, "OpenWrite(%s)", path)
}
// getAttribute returns the first message returned by the server by running
// <host-prefix>:<attr>, where host-prefix is determined from the DeviceDescriptor.
func (c *DeviceClient) getAttribute(attr string) (string, error) {
resp, err := roundTripSingleResponse(c.server,
fmt.Sprintf("%s:%s", c.descriptor.getHostPrefix(), attr))
if err != nil {
return "", err
}
return string(resp), nil
}
2015-07-12 06:18:58 +00:00
func (c *DeviceClient) getSyncConn() (*wire.SyncConn, error) {
conn, err := c.dialDevice()
if err != nil {
return nil, err
}
// Switch the connection to sync mode.
if err := wire.SendMessageString(conn, "sync:"); err != nil {
return nil, err
}
if _, err := conn.ReadStatus("sync"); err != nil {
2015-07-12 06:18:58 +00:00
return nil, err
}
return conn.NewSyncConn(), nil
}
// dialDevice switches the connection to communicate directly with the device
// by requesting the transport defined by the DeviceDescriptor.
func (c *DeviceClient) dialDevice() (*wire.Conn, error) {
conn, err := c.server.Dial()
if err != nil {
2015-07-12 06:18:58 +00:00
return nil, err
}
req := fmt.Sprintf("host:%s", c.descriptor.getTransportDescriptor())
if err = wire.SendMessageString(conn, req); err != nil {
conn.Close()
2015-07-12 06:18:58 +00:00
return nil, util.WrapErrf(err, "error connecting to device '%s'", c.descriptor)
}
if _, err = conn.ReadStatus(req); err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
// prepareCommandLine validates the command and argument strings, quotes
// arguments if required, and joins them into a valid adb command string.
func prepareCommandLine(cmd string, args ...string) (string, error) {
if isBlank(cmd) {
2015-07-12 06:18:58 +00:00
return "", util.AssertionErrorf("command cannot be empty")
}
for i, arg := range args {
if strings.ContainsRune(arg, '"') {
2015-07-12 06:18:58 +00:00
return "", util.Errorf(util.ParseError, "arg at index %d contains an invalid double quote: %s", i, arg)
}
if containsWhitespace(arg) {
args[i] = fmt.Sprintf("\"%s\"", arg)
}
}
2015-07-11 21:32:04 +00:00
// Prepend the command to the args array.
if len(args) > 0 {
cmd = fmt.Sprintf("%s %s", cmd, strings.Join(args, " "))
}
return cmd, nil
}