Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4546520c authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "Move __adb_error to std::string, and improve various errors."

parents 5d67a2c9 078f0fcf
Loading
Loading
Loading
Loading
+69 −75
Original line number Original line Diff line number Diff line
@@ -29,6 +29,9 @@
#include <sys/types.h>
#include <sys/types.h>


#include <string>
#include <string>
#include <vector>

#include <base/stringprintf.h>


#include "adb_io.h"
#include "adb_io.h"


@@ -63,10 +66,11 @@ int adb_get_emulator_console_port(void)
        /* if no specific device was specified, we need to look at */
        /* if no specific device was specified, we need to look at */
        /* the list of connected devices, and extract an emulator  */
        /* the list of connected devices, and extract an emulator  */
        /* name from it. two emulators is an error                 */
        /* name from it. two emulators is an error                 */
        char*  tmp = adb_query("host:devices");
        std::string error;
        char*  tmp = adb_query("host:devices", &error);
        char*  p   = tmp;
        char*  p   = tmp;
        if (!tmp) {
        if (!tmp) {
            printf("no emulator connected\n");
            printf("no emulator connected: %s\n", error.c_str());
            return -1;
            return -1;
        }
        }
        while (*p) {
        while (*p) {
@@ -101,15 +105,11 @@ int adb_get_emulator_console_port(void)
    return port;
    return port;
}
}


static char __adb_error[256] = { 0 };
std::string perror_str(const char* msg) {

    return android::base::StringPrintf("%s: %s", msg, strerror(errno));
const char *adb_error(void)
{
    return __adb_error;
}
}


static int switch_socket_transport(int fd)
static int switch_socket_transport(int fd, std::string* error) {
{
    std::string service;
    std::string service;
    if (__adb_serial) {
    if (__adb_serial) {
        service += "host:transport:";
        service += "host:transport:";
@@ -137,70 +137,64 @@ static int switch_socket_transport(int fd)
    char tmp[5];
    char tmp[5];
    snprintf(tmp, sizeof(tmp), "%04zx", service.size());
    snprintf(tmp, sizeof(tmp), "%04zx", service.size());
    if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) {
    if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) {
        strcpy(__adb_error, "write failure during connection");
        *error = perror_str("write failure during connection");
        adb_close(fd);
        adb_close(fd);
        return -1;
        return -1;
    }
    }
    D("Switch transport in progress\n");
    D("Switch transport in progress\n");


    if (adb_status(fd)) {
    if (!adb_status(fd, error)) {
        adb_close(fd);
        adb_close(fd);
        D("Switch transport failed\n");
        D("Switch transport failed: %s\n", error->c_str());
        return -1;
        return -1;
    }
    }
    D("Switch transport success\n");
    D("Switch transport success\n");
    return 0;
    return 0;
}
}


int adb_status(int fd)
bool adb_status(int fd, std::string* error) {
{
    char buf[5];
    unsigned char buf[5];
    unsigned len;


    if (!ReadFdExactly(fd, buf, 4)) {
    if (!ReadFdExactly(fd, buf, 4)) {
        strcpy(__adb_error, "protocol fault (no status)");
        *error = perror_str("protocol fault (couldn't read status)");
        return -1;
        return false;
    }
    }


    if (!memcmp(buf, "OKAY", 4)) {
    if (!memcmp(buf, "OKAY", 4)) {
        return 0;
        return true;
    }
    }


    if (memcmp(buf, "FAIL", 4)) {
    if (memcmp(buf, "FAIL", 4)) {
        sprintf(__adb_error,
        *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
                "protocol fault (status %02x %02x %02x %02x?!)",
                                             buf[0], buf[1], buf[2], buf[3]);
                                             buf[0], buf[1], buf[2], buf[3]);
        return -1;
        return false;
    }
    }


    if (!ReadFdExactly(fd, buf, 4)) {
    if (!ReadFdExactly(fd, buf, 4)) {
        strcpy(__adb_error, "protocol fault (status len)");
        *error = perror_str("protocol fault (couldn't read status length)");
        return -1;
        return false;
    }
    }
    buf[4] = 0;
    buf[4] = 0;
    len = strtoul((char*)buf, 0, 16);

    if(len > 255) len = 255;
    unsigned long len = strtoul(buf, 0, 16);
    if(!ReadFdExactly(fd, __adb_error, len)) {
    error->resize(len + 1, '\0'); // Ensure NUL-termination.
        strcpy(__adb_error, "protocol fault (status read)");
    if (!ReadFdExactly(fd, &(*error)[0], len)) {
        return -1;
        *error = perror_str("protocol fault (couldn't read status message)");
    }
    }
    __adb_error[len] = 0;
    return false;
    return -1;
}
}


int _adb_connect(const char *service)
int _adb_connect(const char *service, std::string* error) {
{
    char tmp[5];
    char tmp[5];
    int len;
    int fd;
    int fd;


    D("_adb_connect: %s\n", service);
    D("_adb_connect: %s\n", service);
    len = strlen(service);
    size_t len = strlen(service);
    if ((len < 1) || (len > 1024)) {
    if ((len < 1) || (len > 1024)) {
        strcpy(__adb_error, "service name too long");
        *error = android::base::StringPrintf("service name too long (%zd)", len);
        return -1;
        return -1;
    }
    }
    snprintf(tmp, sizeof tmp, "%04x", len);
    snprintf(tmp, sizeof tmp, "%04zx", len);


    if (__adb_server_name)
    if (__adb_server_name)
        fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
        fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
@@ -208,21 +202,21 @@ int _adb_connect(const char *service)
        fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
        fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);


    if(fd < 0) {
    if(fd < 0) {
        strcpy(__adb_error, "cannot connect to daemon");
        *error = perror_str("cannot connect to daemon");
        return -2;
        return -2;
    }
    }


    if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd)) {
    if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd, error)) {
        return -1;
        return -1;
    }
    }


    if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
    if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
        strcpy(__adb_error, "write failure during connection");
        *error = perror_str("write failure during connection");
        adb_close(fd);
        adb_close(fd);
        return -1;
        return -1;
    }
    }


    if(adb_status(fd)) {
    if (!adb_status(fd, error)) {
        adb_close(fd);
        adb_close(fd);
        return -1;
        return -1;
    }
    }
@@ -231,10 +225,9 @@ int _adb_connect(const char *service)
    return fd;
    return fd;
}
}


int adb_connect(const char *service)
int adb_connect(const char* service, std::string* error) {
{
    // first query the adb server's version
    // first query the adb server's version
    int fd = _adb_connect("host:version");
    int fd = _adb_connect("host:version", error);


    D("adb_connect: service %s\n", service);
    D("adb_connect: service %s\n", service);
    if (fd == -2 && __adb_server_name) {
    if (fd == -2 && __adb_server_name) {
@@ -273,13 +266,14 @@ int adb_connect(const char *service)
        } else {
        } else {
            // if fd is -1, then check for "unknown host service",
            // if fd is -1, then check for "unknown host service",
            // which would indicate a version of adb that does not support the version command
            // which would indicate a version of adb that does not support the version command
            if (strcmp(__adb_error, "unknown host service") != 0)
            if (*error == "unknown host service") {
                return fd;
                return fd;
            }
            }
        }


        if(version != ADB_SERVER_VERSION) {
        if(version != ADB_SERVER_VERSION) {
            printf("adb server is out of date.  killing...\n");
            printf("adb server is out of date.  killing...\n");
            fd = _adb_connect("host:kill");
            fd = _adb_connect("host:kill", error);
            adb_close(fd);
            adb_close(fd);


            /* XXX can we better detect its death? */
            /* XXX can we better detect its death? */
@@ -289,12 +283,13 @@ int adb_connect(const char *service)
    }
    }


    // if the command is start-server, we are done.
    // if the command is start-server, we are done.
    if (!strcmp(service, "host:start-server"))
    if (!strcmp(service, "host:start-server")) {
        return 0;
        return 0;
    }


    fd = _adb_connect(service);
    fd = _adb_connect(service, error);
    if (fd == -1) {
    if (fd == -1) {
        D("_adb_connect error: %s", __adb_error);
        D("_adb_connect error: %s", error->c_str());
    } else if(fd == -2) {
    } else if(fd == -2) {
        fprintf(stderr,"** daemon still not running\n");
        fprintf(stderr,"** daemon still not running\n");
    }
    }
@@ -307,15 +302,14 @@ error:
}
}




int adb_command(const char *service)
int adb_command(const char* service, std::string* error) {
{
    int fd = adb_connect(service, error);
    int fd = adb_connect(service);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "error: %s\n", adb_error());
        fprintf(stderr, "error: %s\n", error->c_str());
        return -1;
        return -1;
    }
    }


    if(adb_status(fd)) {
    if (!adb_status(fd, error)) {
        adb_close(fd);
        adb_close(fd);
        return -1;
        return -1;
    }
    }
@@ -323,16 +317,15 @@ int adb_command(const char *service)
    return 0;
    return 0;
}
}


char *adb_query(const char *service)
char* adb_query(const char* service, std::string* error) {
{
    char buf[5];
    char buf[5];
    unsigned long n;
    unsigned long n;
    char* tmp;
    char* tmp;


    D("adb_query: %s\n", service);
    D("adb_query: %s\n", service);
    int fd = adb_connect(service);
    int fd = adb_connect(service, error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", __adb_error);
        fprintf(stderr,"error: %s\n", error->c_str());
        return 0;
        return 0;
    }
    }


@@ -340,8 +333,9 @@ char *adb_query(const char *service)


    buf[4] = 0;
    buf[4] = 0;
    n = strtoul(buf, 0, 16);
    n = strtoul(buf, 0, 16);
    // TODO: given that we just read a 4-byte hex length 0x????, why the test?
    if (n >= 0xffff) {
    if (n >= 0xffff) {
        strcpy(__adb_error, "reply is too long (>= 64kB)");
        *error = "reply is too long (>= 64KiB)";
        goto oops;
        goto oops;
    }
    }


+10 −12
Original line number Original line Diff line number Diff line
@@ -3,23 +3,25 @@


#include "adb.h"
#include "adb.h"


#include <string>

/* connect to adb, connect to the named service, and return
/* connect to adb, connect to the named service, and return
** a valid fd for interacting with that service upon success
** a valid fd for interacting with that service upon success
** or a negative number on failure
** or a negative number on failure
*/
*/
int adb_connect(const char *service);
int adb_connect(const char* service, std::string* error);
int _adb_connect(const char *service);
int _adb_connect(const char* service, std::string* error);


/* connect to adb, connect to the named service, return 0 if
/* connect to adb, connect to the named service, return 0 if
** the connection succeeded AND the service returned OKAY
** the connection succeeded AND the service returned OKAY
*/
*/
int adb_command(const char *service);
int adb_command(const char* service, std::string* error);


/* connect to adb, connect to the named service, return
/* connect to adb, connect to the named service, return
** a malloc'd string of its response upon success or NULL
** a malloc'd string of its response upon success or NULL
** on failure.
** on failure.
*/
*/
char *adb_query(const char *service);
char* adb_query(const char* service, std::string* error);


/* Set the preferred transport to connect to.
/* Set the preferred transport to connect to.
*/
*/
@@ -45,13 +47,9 @@ int adb_get_emulator_console_port(void);
 */
 */
int  adb_send_emulator_command(int  argc, const char**  argv);
int  adb_send_emulator_command(int  argc, const char**  argv);


/* return verbose error string from last operation */
// Reads a standard adb status response (OKAY|FAIL) and
const char *adb_error(void);
// returns true in the event of OKAY, false in the event of FAIL

// or protocol error.
/* read a standard adb status response (OKAY|FAIL) and
bool adb_status(int fd, std::string* error);
** return 0 in the event of OKAY, -1 in the event of FAIL
** or protocol error
*/
int adb_status(int fd);


#endif
#endif
+90 −76
Original line number Original line Diff line number Diff line
@@ -410,11 +410,12 @@ static void *stdin_read_thread(void *x)


static int interactive_shell() {
static int interactive_shell() {
    adb_thread_t thr;
    adb_thread_t thr;
    int fdi, fd;
    int fdi;


    fd = adb_connect("shell:");
    std::string error;
    int fd = adb_connect("shell:", &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
        return 1;
    }
    }
    fdi = 0; //dup(0);
    fdi = 0; //dup(0);
@@ -452,16 +453,17 @@ static void format_host_command(char* buffer, size_t buflen, const char* comman
}
}


static int adb_download_buffer(const char *service, const char *fn, const void* data, int sz,
static int adb_download_buffer(const char *service, const char *fn, const void* data, int sz,
                               unsigned progress)
                               bool show_progress)
{
{
    char buf[4096];
    char buf[4096];
    unsigned total;
    unsigned total;
    int fd;


    sprintf(buf,"%s:%d", service, sz);
    sprintf(buf,"%s:%d", service, sz);
    fd = adb_connect(buf);

    std::string error;
    int fd = adb_connect(buf, &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        fprintf(stderr,"error: %s\n", error.c_str());
        return -1;
        return -1;
    }
    }


@@ -471,7 +473,7 @@ static int adb_download_buffer(const char *service, const char *fn, const void*
    total = sz;
    total = sz;
    const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
    const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);


    if(progress) {
    if (show_progress) {
        char *x = strrchr(service, ':');
        char *x = strrchr(service, ':');
        if(x) service = x + 1;
        if(x) service = x + 1;
    }
    }
@@ -479,18 +481,19 @@ static int adb_download_buffer(const char *service, const char *fn, const void*
    while(sz > 0) {
    while(sz > 0) {
        unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
        unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
        if (!WriteFdExactly(fd, ptr, xfer)) {
        if (!WriteFdExactly(fd, ptr, xfer)) {
            adb_status(fd);
            std::string error;
            fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
            adb_status(fd, &error);
            fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
            return -1;
            return -1;
        }
        }
        sz -= xfer;
        sz -= xfer;
        ptr += xfer;
        ptr += xfer;
        if(progress) {
        if (show_progress) {
            printf("sending: '%s' %4d%%    \r", fn, (int)(100LL - ((100LL * sz) / (total))));
            printf("sending: '%s' %4d%%    \r", fn, (int)(100LL - ((100LL * sz) / (total))));
            fflush(stdout);
            fflush(stdout);
        }
        }
    }
    }
    if(progress) {
    if (show_progress) {
        printf("\n");
        printf("\n");
    }
    }


@@ -549,12 +552,13 @@ static int adb_sideload_host(const char* fn) {


    char buf[100];
    char buf[100];
    sprintf(buf, "sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
    sprintf(buf, "sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
    int fd = adb_connect(buf);
    std::string error;
    int fd = adb_connect(buf, &error);
    if (fd < 0) {
    if (fd < 0) {
        // Try falling back to the older sideload method.  Maybe this
        // Try falling back to the older sideload method.  Maybe this
        // is an older device that doesn't support sideload-host.
        // is an older device that doesn't support sideload-host.
        printf("\n");
        printf("\n");
        status = adb_download_buffer("sideload", fn, data, sz, 1);
        status = adb_download_buffer("sideload", fn, data, sz, true);
        goto done;
        goto done;
    }
    }


@@ -562,7 +566,7 @@ static int adb_sideload_host(const char* fn) {


    while (true) {
    while (true) {
        if (!ReadFdExactly(fd, buf, 8)) {
        if (!ReadFdExactly(fd, buf, 8)) {
            fprintf(stderr, "* failed to read command: %s\n", adb_error());
            fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
            status = -1;
            status = -1;
            goto done;
            goto done;
        }
        }
@@ -577,7 +581,7 @@ static int adb_sideload_host(const char* fn) {


        size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
        size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
        if (offset >= sz) {
        if (offset >= sz) {
            fprintf(stderr, "* attempt to read past end: %s\n", adb_error());
            fprintf(stderr, "* attempt to read block %d past end\n", block);
            status = -1;
            status = -1;
            goto done;
            goto done;
        }
        }
@@ -589,8 +593,8 @@ static int adb_sideload_host(const char* fn) {
        }
        }


        if(!WriteFdExactly(fd, start, to_write)) {
        if(!WriteFdExactly(fd, start, to_write)) {
            adb_status(fd);
            adb_status(fd, &error);
            fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
            fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
            status = -1;
            status = -1;
            goto done;
            goto done;
        }
        }
@@ -636,7 +640,7 @@ static void status_window(transport_type ttype, const char* serial)


    format_host_command(command, sizeof command, "get-state", ttype, serial);
    format_host_command(command, sizeof command, "get-state", ttype, serial);


    for(;;) {
    while (true) {
        adb_sleep_ms(250);
        adb_sleep_ms(250);


        if (state) {
        if (state) {
@@ -644,7 +648,8 @@ static void status_window(transport_type ttype, const char* serial)
            state = 0;
            state = 0;
        }
        }


        state = adb_query(command);
        std::string error;
        state = adb_query(command, &error);


        if (state) {
        if (state) {
            if (laststate && !strcmp(state,laststate)){
            if (laststate && !strcmp(state,laststate)){
@@ -674,9 +679,6 @@ static int ppp(int argc, const char** argv) {
    fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
    fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
    return -1;
    return -1;
#else
#else
    pid_t pid;
    int fd;

    if (argc < 2) {
    if (argc < 2) {
        fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
        fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
                argv[0]);
                argv[0]);
@@ -685,15 +687,15 @@ static int ppp(int argc, const char** argv) {
    }
    }


    const char* adb_service_name = argv[1];
    const char* adb_service_name = argv[1];
    fd = adb_connect(adb_service_name);
    std::string error;

    int fd = adb_connect(adb_service_name, &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
        fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
                adb_service_name, adb_error());
                adb_service_name, error.c_str());
        return 1;
        return 1;
    }
    }


    pid = fork();
    pid_t pid = fork();


    if (pid < 0) {
    if (pid < 0) {
        perror("from fork()");
        perror("from fork()");
@@ -738,9 +740,11 @@ static int send_shell_command(transport_type transport, const char* serial,
                              const std::string& command) {
                              const std::string& command) {
    int fd;
    int fd;
    while (true) {
    while (true) {
        fd = adb_connect(command.c_str());
        std::string error;
        if (fd >= 0)
        fd = adb_connect(command.c_str(), &error);
        if (fd >= 0) {
            break;
            break;
        }
        fprintf(stderr,"- waiting for device -\n");
        fprintf(stderr,"- waiting for device -\n");
        adb_sleep_ms(1000);
        adb_sleep_ms(1000);
        do_cmd(transport, serial, "wait-for-device", 0);
        do_cmd(transport, serial, "wait-for-device", 0);
@@ -830,9 +834,10 @@ static int backup(int argc, const char** argv) {
    }
    }


    D("backup. filename=%s cmd=%s\n", filename, cmd.c_str());
    D("backup. filename=%s cmd=%s\n", filename, cmd.c_str());
    int fd = adb_connect(cmd.c_str());
    std::string error;
    int fd = adb_connect(cmd.c_str(), &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "adb: unable to connect for backup\n");
        fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str());
        adb_close(outFd);
        adb_close(outFd);
        return -1;
        return -1;
    }
    }
@@ -846,21 +851,19 @@ static int backup(int argc, const char** argv) {
}
}


static int restore(int argc, const char** argv) {
static int restore(int argc, const char** argv) {
    const char* filename;
    int fd, tarFd;

    if (argc != 2) return usage();
    if (argc != 2) return usage();


    filename = argv[1];
    const char* filename = argv[1];
    tarFd = adb_open(filename, O_RDONLY);
    int tarFd = adb_open(filename, O_RDONLY);
    if (tarFd < 0) {
    if (tarFd < 0) {
        fprintf(stderr, "adb: unable to open file %s\n", filename);
        fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno));
        return -1;
        return -1;
    }
    }


    fd = adb_connect("restore:");
    std::string error;
    int fd = adb_connect("restore:", &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "adb: unable to connect for restore\n");
        fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str());
        adb_close(tarFd);
        adb_close(tarFd);
        return -1;
        return -1;
    }
    }
@@ -962,13 +965,14 @@ static void parse_push_pull_args(const char **arg, int narg, char const **path1,
}
}


static int adb_connect_command(const char* command) {
static int adb_connect_command(const char* command) {
    int fd = adb_connect(command);
    std::string error;
    int fd = adb_connect(command, &error);
    if (fd != -1) {
    if (fd != -1) {
        read_and_dump(fd);
        read_and_dump(fd);
        adb_close(fd);
        adb_close(fd);
        return 0;
        return 0;
    }
    }
    fprintf(stderr, "Error: %s\n", adb_error());
    fprintf(stderr, "Error: %s\n", error.c_str());
    return 1;
    return 1;
}
}


@@ -1127,9 +1131,10 @@ int adb_commandline(int argc, const char **argv)


        format_host_command(buf, sizeof buf, service, ttype, serial);
        format_host_command(buf, sizeof buf, service, ttype, serial);


        if (adb_command(buf)) {
        std::string error;
            D("failure: %s *\n",adb_error());
        if (adb_command(buf, &error)) {
            fprintf(stderr,"error: %s\n", adb_error());
            D("failure: %s *\n", error.c_str());
            fprintf(stderr,"error: %s\n", error.c_str());
            return 1;
            return 1;
        }
        }


@@ -1158,7 +1163,8 @@ int adb_commandline(int argc, const char **argv)
            return 1;
            return 1;
        }
        }
        snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
        snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
        tmp = adb_query(buf);
        std::string error;
        tmp = adb_query(buf, &error);
        if (tmp) {
        if (tmp) {
            printf("List of devices attached \n");
            printf("List of devices attached \n");
            printf("%s\n", tmp);
            printf("%s\n", tmp);
@@ -1174,7 +1180,8 @@ int adb_commandline(int argc, const char **argv)
            return 1;
            return 1;
        }
        }
        snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
        snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
        tmp = adb_query(buf);
        std::string error;
        tmp = adb_query(buf, &error);
        if (tmp) {
        if (tmp) {
            printf("%s\n", tmp);
            printf("%s\n", tmp);
            return 0;
            return 0;
@@ -1193,7 +1200,8 @@ int adb_commandline(int argc, const char **argv)
        } else {
        } else {
            snprintf(buf, sizeof buf, "host:disconnect:");
            snprintf(buf, sizeof buf, "host:disconnect:");
        }
        }
        tmp = adb_query(buf);
        std::string error;
        tmp = adb_query(buf, &error);
        if (tmp) {
        if (tmp) {
            printf("%s\n", tmp);
            printf("%s\n", tmp);
            return 0;
            return 0;
@@ -1232,7 +1240,8 @@ int adb_commandline(int argc, const char **argv)


        while (true) {
        while (true) {
            D("interactive shell loop. cmd=%s\n", cmd.c_str());
            D("interactive shell loop. cmd=%s\n", cmd.c_str());
            int fd = adb_connect(cmd.c_str());
            std::string error;
            int fd = adb_connect(cmd.c_str(), &error);
            int r;
            int r;
            if (fd >= 0) {
            if (fd >= 0) {
                D("about to read_and_dump(fd=%d)\n", fd);
                D("about to read_and_dump(fd=%d)\n", fd);
@@ -1241,7 +1250,7 @@ int adb_commandline(int argc, const char **argv)
                adb_close(fd);
                adb_close(fd);
                r = 0;
                r = 0;
            } else {
            } else {
                fprintf(stderr,"error: %s\n", adb_error());
                fprintf(stderr,"error: %s\n", error.c_str());
                r = -1;
                r = -1;
            }
            }


@@ -1270,9 +1279,10 @@ int adb_commandline(int argc, const char **argv)
            cmd += " " + escape_arg(*argv++);
            cmd += " " + escape_arg(*argv++);
        }
        }


        int fd = adb_connect(cmd.c_str());
        std::string error;
        int fd = adb_connect(cmd.c_str(), &error);
        if (fd < 0) {
        if (fd < 0) {
            fprintf(stderr, "error: %s\n", adb_error());
            fprintf(stderr, "error: %s\n", error.c_str());
            return -1;
            return -1;
        }
        }


@@ -1286,8 +1296,8 @@ int adb_commandline(int argc, const char **argv)
        return 0;
        return 0;
    }
    }
    else if (!strcmp(argv[0], "kill-server")) {
    else if (!strcmp(argv[0], "kill-server")) {
        int fd;
        std::string error;
        fd = _adb_connect("host:kill");
        int fd = _adb_connect("host:kill", &error);
        if (fd == -1) {
        if (fd == -1) {
            fprintf(stderr,"* server not running *\n");
            fprintf(stderr,"* server not running *\n");
            return 1;
            return 1;
@@ -1378,9 +1388,10 @@ int adb_commandline(int argc, const char **argv)
            if (argc != 1)
            if (argc != 1)
                return usage();
                return usage();
            snprintf(buf, sizeof buf, "%s:list-forward", host_prefix);
            snprintf(buf, sizeof buf, "%s:list-forward", host_prefix);
            char* forwards = adb_query(buf);
            std::string error;
            char* forwards = adb_query(buf, &error);
            if (forwards == NULL) {
            if (forwards == NULL) {
                fprintf(stderr, "error: %s\n", adb_error());
                fprintf(stderr, "error: %s\n", error.c_str());
                return 1;
                return 1;
            }
            }
            printf("%s", forwards);
            printf("%s", forwards);
@@ -1412,8 +1423,9 @@ int adb_commandline(int argc, const char **argv)
          snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
          snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
        }
        }


        if (adb_command(buf)) {
        std::string error;
            fprintf(stderr,"error: %s\n", adb_error());
        if (adb_command(buf, &error)) {
            fprintf(stderr,"error: %s\n", error.c_str());
            return 1;
            return 1;
        }
        }
        return 0;
        return 0;
@@ -1511,10 +1523,9 @@ int adb_commandline(int argc, const char **argv)
        !strcmp(argv[0],"get-serialno") ||
        !strcmp(argv[0],"get-serialno") ||
        !strcmp(argv[0],"get-devpath"))
        !strcmp(argv[0],"get-devpath"))
    {
    {
        char *tmp;

        format_host_command(buf, sizeof buf, argv[0], ttype, serial);
        format_host_command(buf, sizeof buf, argv[0], ttype, serial);
        tmp = adb_query(buf);
        std::string error;
        char* tmp = adb_query(buf, &error);
        if (tmp) {
        if (tmp) {
            printf("%s\n", tmp);
            printf("%s\n", tmp);
            return 0;
            return 0;
@@ -1534,7 +1545,8 @@ int adb_commandline(int argc, const char **argv)
        return ppp(argc, argv);
        return ppp(argc, argv);
    }
    }
    else if (!strcmp(argv[0], "start-server")) {
    else if (!strcmp(argv[0], "start-server")) {
        return adb_connect("host:start-server");
        std::string error;
        return adb_connect("host:start-server", &error);
    }
    }
    else if (!strcmp(argv[0], "backup")) {
    else if (!strcmp(argv[0], "backup")) {
        return backup(argc, argv);
        return backup(argc, argv);
@@ -1742,9 +1754,10 @@ static int install_multiple_app(transport_type transport, const char* serial, in
    }
    }


    // Create install session
    // Create install session
    int fd = adb_connect(cmd.c_str());
    std::string error;
    int fd = adb_connect(cmd.c_str(), &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "Connect error for create: %s\n", adb_error());
        fprintf(stderr, "Connect error for create: %s\n", error.c_str());
        return -1;
        return -1;
    }
    }
    char buf[BUFSIZ];
    char buf[BUFSIZ];
@@ -1788,14 +1801,15 @@ static int install_multiple_app(transport_type transport, const char* serial, in


        int localFd = adb_open(file, O_RDONLY);
        int localFd = adb_open(file, O_RDONLY);
        if (localFd < 0) {
        if (localFd < 0) {
            fprintf(stderr, "Failed to open %s: %s\n", file, adb_error());
            fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
            success = 0;
            success = 0;
            goto finalize_session;
            goto finalize_session;
        }
        }


        int remoteFd = adb_connect(cmd.c_str());
        std::string error;
        int remoteFd = adb_connect(cmd.c_str(), &error);
        if (remoteFd < 0) {
        if (remoteFd < 0) {
            fprintf(stderr, "Connect error for write: %s\n", adb_error());
            fprintf(stderr, "Connect error for write: %s\n", error.c_str());
            adb_close(localFd);
            adb_close(localFd);
            success = 0;
            success = 0;
            goto finalize_session;
            goto finalize_session;
@@ -1823,9 +1837,9 @@ finalize_session:
        snprintf(buf, sizeof(buf), "exec:pm install-abandon %d", session_id);
        snprintf(buf, sizeof(buf), "exec:pm install-abandon %d", session_id);
    }
    }


    fd = adb_connect(buf);
    fd = adb_connect(buf, &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "Connect error for finalize: %s\n", adb_error());
        fprintf(stderr, "Connect error for finalize: %s\n", error.c_str());
        return -1;
        return -1;
    }
    }
    read_status_line(fd, buf, sizeof(buf));
    read_status_line(fd, buf, sizeof(buf));
+16 −16
Original line number Original line Diff line number Diff line
@@ -539,11 +539,11 @@ static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
    printf("%08x %08x %08x %s\n", mode, size, time, name);
    printf("%08x %08x %08x %s\n", mode, size, time, name);
}
}


int do_sync_ls(const char *path)
int do_sync_ls(const char* path) {
{
    std::string error;
    int fd = adb_connect("sync:");
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
        return 1;
    }
    }


@@ -743,11 +743,11 @@ int do_sync_push(const char *lpath, const char *rpath, int show_progress)
{
{
    struct stat st;
    struct stat st;
    unsigned mode;
    unsigned mode;
    int fd;


    fd = adb_connect("sync:");
    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
        return 1;
    }
    }


@@ -967,11 +967,10 @@ int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int co
    unsigned mode, time;
    unsigned mode, time;
    struct stat st;
    struct stat st;


    int fd;
    std::string error;

    int fd = adb_connect("sync:", &error);
    fd = adb_connect("sync:");
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
        return 1;
    }
    }


@@ -1031,9 +1030,10 @@ int do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_o
{
{
    fprintf(stderr, "syncing %s...\n", rpath.c_str());
    fprintf(stderr, "syncing %s...\n", rpath.c_str());


    int fd = adb_connect("sync:");
    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
    if (fd < 0) {
        fprintf(stderr, "error: %s\n", adb_error());
        fprintf(stderr, "error: %s\n", error.c_str());
        return 1;
        return 1;
    }
    }