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

Commit d5db4e1b authored by Josh Gao's avatar Josh Gao Committed by Gerrit Code Review
Browse files

Merge changes I1d899134,If4ea92ae,I92c05721,I298517b6,Iccbeb619, ...

* changes:
  adb: add stroll equivalent for string_view.
  adb: finish switching service creation to string_view.
  adb: switch socket spec to string_view.
  adb: switch daemon_service_to_fd to string_view.
  adb: switch usb_linux helper to string_view.
  adb: switch unix_open to string_view.
parents c02d05dc 3add0c4d
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -357,9 +357,9 @@ void handle_packet(apacket *p, atransport *t)


    case A_OPEN: /* OPEN(local-id, 0, "destination") */
    case A_OPEN: /* OPEN(local-id, 0, "destination") */
        if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
        if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
            // TODO: Switch to string_view.
            std::string_view address(p->payload.begin(), p->payload.size());
            std::string address(p->payload.begin(), p->payload.end());

            asocket* s = create_local_service_socket(address.c_str(), t);
            asocket* s = create_local_service_socket(address, t);
            if (s == nullptr) {
            if (s == nullptr) {
                send_close(0, p->msg.arg0, t);
                send_close(0, p->msg.arg0, t);
            } else {
            } else {
@@ -600,7 +600,7 @@ static void ReportServerStartupFailure(pid_t pid) {
    fprintf(stderr, "Full server startup log: %s\n", GetLogFilePath().c_str());
    fprintf(stderr, "Full server startup log: %s\n", GetLogFilePath().c_str());
    fprintf(stderr, "Server had pid: %d\n", pid);
    fprintf(stderr, "Server had pid: %d\n", pid);


    android::base::unique_fd fd(unix_open(GetLogFilePath().c_str(), O_RDONLY));
    android::base::unique_fd fd(unix_open(GetLogFilePath(), O_RDONLY));
    if (fd == -1) return;
    if (fd == -1) return;


    // Let's not show more than 128KiB of log...
    // Let's not show more than 128KiB of log...
+2 −2
Original line number Original line Diff line number Diff line
@@ -139,9 +139,9 @@ atransport* find_emulator_transport_by_adb_port(int adb_port);
atransport* find_emulator_transport_by_console_port(int console_port);
atransport* find_emulator_transport_by_console_port(int console_port);
#endif
#endif


int service_to_fd(const char* name, atransport* transport);
int service_to_fd(std::string_view name, atransport* transport);
#if !ADB_HOST
#if !ADB_HOST
unique_fd daemon_service_to_fd(const char* name, atransport* transport);
unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
#endif
#endif


#if ADB_HOST
#if ADB_HOST
+1 −2
Original line number Original line Diff line number Diff line
@@ -72,8 +72,7 @@ static std::string get_log_file_name() {
}
}


void start_device_log(void) {
void start_device_log(void) {
    int fd = unix_open(get_log_file_name().c_str(),
    int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
                       O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
    if (fd == -1) {
    if (fd == -1) {
        return;
        return;
    }
    }
+46 −0
Original line number Original line Diff line number Diff line
@@ -19,6 +19,8 @@
#include <condition_variable>
#include <condition_variable>
#include <mutex>
#include <mutex>
#include <string>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include <vector>


#include <android-base/macros.h>
#include <android-base/macros.h>
@@ -94,3 +96,47 @@ class BlockingQueue {
};
};


std::string GetLogFilePath();
std::string GetLogFilePath();

inline std::string_view StripTrailingNulls(std::string_view str) {
    size_t n = 0;
    for (auto it = str.rbegin(); it != str.rend(); ++it) {
        if (*it != '\0') {
            break;
        }
        ++n;
    }

    str.remove_suffix(n);
    return str;
}

// Base-10 stroll on a string_view.
template <typename T>
inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining) {
    if (str.empty() || !isdigit(str[0])) {
        return false;
    }

    T value = 0;
    std::string_view::iterator it;
    constexpr T max = std::numeric_limits<T>::max();
    for (it = str.begin(); it != str.end() && isdigit(*it); ++it) {
        if (value > max / 10) {
            return false;
        }

        value *= 10;

        T digit = *it - '0';
        if (value > max - digit) {
            return false;
        }

        value += digit;
    }
    *result = value;
    if (remaining) {
        *remaining = str.substr(it - str.begin());
    }
    return true;
}
+45 −0
Original line number Original line Diff line number Diff line
@@ -181,3 +181,48 @@ TEST(adb_utils, test_forward_targets_are_valid) {
    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:a", &error));
    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:a", &error));
    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:22x", &error));
    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:22x", &error));
}
}

void TestParseUint(std::string_view string, bool expected_success, uint32_t expected_value = 0) {
    // Standalone.
    {
        uint32_t value;
        std::string_view remaining;
        bool success = ParseUint(&value, string, &remaining);
        EXPECT_EQ(success, expected_success);
        if (expected_success) {
            EXPECT_EQ(value, expected_value);
        }
        EXPECT_TRUE(remaining.empty());
    }

    // With trailing text.
    {
        std::string text = std::string(string) + "foo";
        uint32_t value;
        std::string_view remaining;
        bool success = ParseUint(&value, text, &remaining);
        EXPECT_EQ(success, expected_success);
        if (expected_success) {
            EXPECT_EQ(value, expected_value);
            EXPECT_EQ(remaining, "foo");
        }
    }
}

TEST(adb_utils, ParseUint) {
    TestParseUint("", false);
    TestParseUint("foo", false);
    TestParseUint("foo123", false);
    TestParseUint("-1", false);

    TestParseUint("123", true, 123);
    TestParseUint("9999999999999999999999999", false);
    TestParseUint(std::to_string(UINT32_MAX), true, UINT32_MAX);
    TestParseUint("0" + std::to_string(UINT32_MAX), true, UINT32_MAX);
    TestParseUint(std::to_string(static_cast<uint64_t>(UINT32_MAX) + 1), false);
    TestParseUint("0" + std::to_string(static_cast<uint64_t>(UINT32_MAX) + 1), false);

    std::string x = std::to_string(UINT32_MAX) + "123";
    std::string_view substr = std::string_view(x).substr(0, std::to_string(UINT32_MAX).size());
    TestParseUint(substr, true, UINT32_MAX);
}
Loading