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

Commit b7dfb79b authored by Spencer Low's avatar Spencer Low
Browse files

adb: win32: fix exec-in and exec-out to use binary mode

adb exec-in and exec-out are designed to read/write binary data
according to the commit description at:
https://android.googlesource.com/platform/system/core/+/5d9d434%5E!/



On Windows, when adb_read and adb_write are used, they are always in
binary mode (because sysdeps_win32.cpp calls Windows APIs direct). But
unix_read, unix_write, fread, fwrite, read, write use the text
translation mode of the C Runtime file descriptor, which is by default
textmode.

adb exec-in and exec-out use copy_to_file() which uses unix_read() and
fwrite() when reading/writing stdin and stdout, thus, copy_to_file()
should switch to binary mode for those cases (it already uses binary
mode for file descriptors other than stdin and stdout).

copy_to_file() is also called by adb backup, adb restore, and adb
install-multiple, but those do not use stdin or stdout, so those
codepaths should not be affected by this change.

Change-Id: I3446d9b363d20a2c2f6be2b96e55b653d99df2f9
Signed-off-by: default avatarSpencer Low <CompareAndSwap@gmail.com>
parent 6af8e11f
Loading
Loading
Loading
Loading
+32 −0
Original line number Original line Diff line number Diff line
@@ -302,12 +302,31 @@ static void copy_to_file(int inFd, int outFd) {
    if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
    if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
    int len;
    int len;
    long total = 0;
    long total = 0;
#ifdef _WIN32
    int old_stdin_mode = -1;
    int old_stdout_mode = -1;
#endif


    D("copy_to_file(%d -> %d)\n", inFd, outFd);
    D("copy_to_file(%d -> %d)\n", inFd, outFd);


    if (inFd == STDIN_FILENO) {
    if (inFd == STDIN_FILENO) {
        stdin_raw_init(STDIN_FILENO);
        stdin_raw_init(STDIN_FILENO);
#ifdef _WIN32
        old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY);
        if (old_stdin_mode == -1) {
            fatal_errno("could not set stdin to binary");
        }
#endif
    }

#ifdef _WIN32
    if (outFd == STDOUT_FILENO) {
        old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY);
        if (old_stdout_mode == -1) {
            fatal_errno("could not set stdout to binary");
        }
        }
    }
#endif


    while (true) {
    while (true) {
        if (inFd == STDIN_FILENO) {
        if (inFd == STDIN_FILENO) {
@@ -338,7 +357,20 @@ static void copy_to_file(int inFd, int outFd) {


    if (inFd == STDIN_FILENO) {
    if (inFd == STDIN_FILENO) {
        stdin_raw_restore(STDIN_FILENO);
        stdin_raw_restore(STDIN_FILENO);
#ifdef _WIN32
        if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) {
            fatal_errno("could not restore stdin mode");
        }
        }
#endif
    }

#ifdef _WIN32
    if (outFd == STDOUT_FILENO) {
        if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) {
            fatal_errno("could not restore stdout mode");
        }
    }
#endif


    D("copy_to_file() finished after %lu bytes\n", total);
    D("copy_to_file() finished after %lu bytes\n", total);
    free(buf);
    free(buf);