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

Commit ab52c181 authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Add WriteFdFmt and clean up more code.

Also say *which* device wasn't found.

Bug: http://b/20666660
Change-Id: I50e234ad89e39ae0a8995083c0b642c61275c5a3
parent b74d8892
Loading
Loading
Loading
Loading
+2 −4
Original line number Original line Diff line number Diff line
@@ -89,10 +89,8 @@ void start_device_log(void) {
    char timestamp[PATH_MAX];
    char timestamp[PATH_MAX];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);


    char path[PATH_MAX];
    std::string path = android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp, getpid());
    snprintf(path, sizeof(path), "/data/adb/adb-%s-%d", timestamp, getpid());
    int fd = unix_open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);

    int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
    if (fd == -1) {
    if (fd == -1) {
        return;
        return;
    }
    }
+0 −2
Original line number Original line 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) {
static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
    char buf[5];
    char buf[5];

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


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

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


#define TRACE_TAG TRACE_RWX
#define TRACE_TAG TRACE_RWX


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


#include <unistd.h>
#include <unistd.h>


#include <base/stringprintf.h>

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


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


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


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


bool WriteStringFully(int fd, const char* str) {
bool WriteFdFmt(int fd, const char* fmt, ...) {
    return WriteFdExactly(fd, str, strlen(str));
    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 Original line 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);
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 char* s);
bool WriteFdExactly(int fd, const std::string& s);
bool WriteFdExactly(int fd, const std::string& s);


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


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