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

Commit 14799c62 authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "Add WriteFdFmt and clean up more code."

parents fb36dd62 ab52c181
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -89,10 +89,8 @@ void start_device_log(void) {
    char timestamp[PATH_MAX];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);

    char path[PATH_MAX];
    snprintf(path, sizeof(path), "/data/adb/adb-%s-%d", timestamp, getpid());

    int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
    std::string path = android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp, getpid());
    int fd = unix_open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
    if (fd == -1) {
        return;
    }
+0 −2
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ static std::string perror_str(const char* msg) {

static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
    char buf[5];

    if (!ReadFdExactly(fd, buf, 4)) {
        *error = perror_str("protocol fault (couldn't read status length)");
        return false;
@@ -154,7 +153,6 @@ static int switch_socket_transport(int fd, std::string* error) {

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

    if (!ReadFdExactly(fd, buf, 4)) {
        *error = perror_str("protocol fault (couldn't read status)");
        return false;
+13 −6
Original line number Diff line number Diff line
@@ -16,13 +16,15 @@

#define TRACE_TAG TRACE_RWX

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

#include <unistd.h>

#include <base/stringprintf.h>

#include "adb_trace.h"
#include "adb_utils.h"
#include "sysdeps.h"

bool SendProtocolString(int fd, const std::string& s) {
    int length = s.size();
@@ -30,9 +32,7 @@ bool SendProtocolString(int fd, const std::string& s) {
        length = 0xffff;
    }

    char buf[5];
    snprintf(buf, sizeof(buf), "%04x", length);
    return WriteFdExactly(fd, buf, 4) && WriteFdExactly(fd, s);
    return WriteFdFmt(fd, "%04x", length) && WriteFdExactly(fd, s);
}

bool SendOkay(int fd) {
@@ -111,6 +111,13 @@ bool WriteFdExactly(int fd, const std::string& str) {
    return WriteFdExactly(fd, str.c_str(), str.size());
}

bool WriteStringFully(int fd, const char* str) {
    return WriteFdExactly(fd, str, strlen(str));
bool WriteFdFmt(int fd, const char* fmt, ...) {
    std::string str;

    va_list ap;
    va_start(ap, fmt);
    android::base::StringAppendV(&str, fmt, ap);
    va_end(ap);

    return WriteFdExactly(fd, str);
}
+3 −3
Original line number Diff line number Diff line
@@ -49,11 +49,11 @@ bool ReadFdExactly(int fd, void *buf, size_t len);
 */
bool WriteFdExactly(int fd, const void* buf, size_t len);

/* Same as above, but with an implicit len = strlen(buf). */
// Same as above, but for strings.
bool WriteFdExactly(int fd, const char* s);
bool WriteFdExactly(int fd, const std::string& s);

// TODO: move minadb off this and remove it.
bool WriteStringFully(int fd, const char* str);
// Same as above, but formats the string to send.
bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));

#endif /* ADB_IO_H */
+13 −0
Original line number Diff line number Diff line
@@ -152,3 +152,16 @@ TEST(io, WriteFdExactly_string) {
  ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
  EXPECT_STREQ(str, s.c_str());
}

TEST(io, WriteFdFmt) {
    TemporaryFile tf;
    ASSERT_NE(-1, tf.fd);

    // Test writing a partial string to the file.
    ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
    ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));

    std::string s;
    ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
    EXPECT_STREQ("Foobar123", s.c_str());
}
Loading