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

Commit 3b4de3c3 authored by Josh Gao's avatar Josh Gao
Browse files

adb: delete hellish hodgepodge.

This code was unreachable, since all of the callers were calling
register_socket_transport with foo.c_str() as the serial. Lift this
assumption into the type system by switching from char* to std::string
for the argument type.

Bug: http://b/112147760
Bug: https://www.viva64.com/en/b/0579/
Test: mma
Change-Id: I5a6ee265feee6b83bc933a64d895eed39fce68e7
parent ddcee93c
Loading
Loading
Loading
Loading
+5 −11
Original line number Diff line number Diff line
@@ -1190,17 +1190,11 @@ void close_usb_devices() {
}
#endif  // ADB_HOST

int register_socket_transport(unique_fd s, const char* serial, int port, int local,
int register_socket_transport(unique_fd s, std::string serial, int port, int local,
                              atransport::ReconnectCallback reconnect) {
    atransport* t = new atransport(std::move(reconnect), kCsOffline);

    if (!serial) {
        char buf[32];
        snprintf(buf, sizeof(buf), "T-%p", t);
        serial = buf;
    }

    D("transport: %s init'ing for socket %d, on port %d", serial, s.get(), port);
    D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port);
    if (init_socket_transport(t, std::move(s), port, local) < 0) {
        delete t;
        return -1;
@@ -1208,7 +1202,7 @@ int register_socket_transport(unique_fd s, const char* serial, int port, int loc

    std::unique_lock<std::recursive_mutex> lock(transport_lock);
    for (const auto& transport : pending_list) {
        if (strcmp(serial, transport->serial.c_str()) == 0) {
        if (serial == transport->serial) {
            VLOG(TRANSPORT) << "socket transport " << transport->serial
                            << " is already in pending_list and fails to register";
            delete t;
@@ -1217,7 +1211,7 @@ int register_socket_transport(unique_fd s, const char* serial, int port, int loc
    }

    for (const auto& transport : transport_list) {
        if (strcmp(serial, transport->serial.c_str()) == 0) {
        if (serial == transport->serial) {
            VLOG(TRANSPORT) << "socket transport " << transport->serial
                            << " is already in transport_list and fails to register";
            delete t;
@@ -1225,8 +1219,8 @@ int register_socket_transport(unique_fd s, const char* serial, int port, int loc
        }
    }

    t->serial = std::move(serial);
    pending_list.push_front(t);
    t->serial = serial;

    lock.unlock();

+1 −1
Original line number Diff line number Diff line
@@ -364,7 +364,7 @@ void register_usb_transport(usb_handle* h, const char* serial,
void connect_device(const std::string& address, std::string* response);

/* cause new transports to be init'd and added to the list */
int register_socket_transport(unique_fd s, const char* serial, int port, int local,
int register_socket_transport(unique_fd s, std::string serial, int port, int local,
                              atransport::ReconnectCallback reconnect);

// This should only be used for transports with connection_state == kCsNoPerm.
+4 −5
Original line number Diff line number Diff line
@@ -125,8 +125,7 @@ void connect_device(const std::string& address, std::string* response) {
        return init_socket_transport(t, std::move(fd), port, 0) >= 0;
    };

    int ret =
            register_socket_transport(std::move(fd), serial.c_str(), port, 0, std::move(reconnect));
    int ret = register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect));
    if (ret < 0) {
        if (ret == -EALREADY) {
            *response = android::base::StringPrintf("already connected to %s", serial.c_str());
@@ -162,7 +161,7 @@ int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* e
        close_on_exec(fd.get());
        disable_tcp_nagle(fd.get());
        std::string serial = getEmulatorSerialString(console_port);
        if (register_socket_transport(std::move(fd), serial.c_str(), adb_port, 1,
        if (register_socket_transport(std::move(fd), std::move(serial), adb_port, 1,
                                      [](atransport*) { return false; }) == 0) {
            return 0;
        }
@@ -265,7 +264,7 @@ static void server_socket_thread(int port) {
            close_on_exec(fd.get());
            disable_tcp_nagle(fd.get());
            std::string serial = android::base::StringPrintf("host-%d", fd.get());
            register_socket_transport(std::move(fd), serial.c_str(), port, 1,
            register_socket_transport(std::move(fd), std::move(serial), port, 1,
                                      [](atransport*) { return false; });
        }
    }
@@ -362,7 +361,7 @@ static void qemu_socket_thread(int port) {
                 * exchange. */
                std::string serial = android::base::StringPrintf("host-%d", fd.get());
                WriteFdExactly(fd.get(), _start_req, strlen(_start_req));
                register_socket_transport(std::move(fd), serial.c_str(), port, 1,
                register_socket_transport(std::move(fd), std::move(serial), port, 1,
                                          [](atransport*) { return false; });
            }