Loading adb/adb_auth_host.cpp +8 −34 Original line number Original line Diff line number Diff line Loading @@ -18,18 +18,13 @@ #include "sysdeps.h" #include "sysdeps.h" #include "adb_auth.h" #include "adb_auth.h" #include "adb_utils.h" #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <string.h> #include <string.h> #ifdef _WIN32 #ifndef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include "windows.h" # include "shlobj.h" #else # include <sys/types.h> # include <sys/types.h> # include <sys/stat.h> # include <sys/stat.h> # include <unistd.h> # include <unistd.h> Loading Loading @@ -247,36 +242,14 @@ static int read_key(const char *file, struct listnode *list) static int get_user_keyfilepath(char *filename, size_t len) static int get_user_keyfilepath(char *filename, size_t len) { { const char *format, *home; char android_dir[PATH_MAX]; char android_dir[PATH_MAX]; struct stat buf; struct stat buf; #ifdef _WIN32 std::string home_str; home = getenv("ANDROID_SDK_HOME"); if (!home) { WCHAR path[MAX_PATH]; const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); if (FAILED(hr)) { D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str()); return -1; } if (!android::base::WideToUTF8(path, &home_str)) { return -1; } home = home_str.c_str(); } format = "%s\\%s"; #else home = getenv("HOME"); if (!home) return -1; format = "%s/%s"; #endif D("home '%s'", home); std::string home = adb_get_homedir_path(true); D("home '%s'", home.c_str()); if (snprintf(android_dir, sizeof(android_dir), format, home, if (snprintf(android_dir, sizeof(android_dir), "%s%c%s", home.c_str(), ANDROID_PATH) >= (int)sizeof(android_dir)) OS_PATH_SEPARATOR, ANDROID_PATH) >= (int)sizeof(android_dir)) return -1; return -1; if (stat(android_dir, &buf)) { if (stat(android_dir, &buf)) { Loading @@ -286,7 +259,8 @@ static int get_user_keyfilepath(char *filename, size_t len) } } } } return snprintf(filename, len, format, android_dir, ADB_KEY_FILE); return snprintf(filename, len, "%s%c%s", android_dir, OS_PATH_SEPARATOR, ADB_KEY_FILE); } } static int get_user_key(struct listnode *list) static int get_user_key(struct listnode *list) Loading adb/adb_utils.cpp +35 −0 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,14 @@ #include "adb_trace.h" #include "adb_trace.h" #include "sysdeps.h" #include "sysdeps.h" #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include "windows.h" # include "shlobj.h" #endif ADB_MUTEX_DEFINE(basename_lock); ADB_MUTEX_DEFINE(basename_lock); ADB_MUTEX_DEFINE(dirname_lock); ADB_MUTEX_DEFINE(dirname_lock); Loading Loading @@ -254,3 +262,30 @@ bool forward_targets_are_valid(const std::string& source, const std::string& des return true; return true; } } std::string adb_get_homedir_path(bool check_env_first) { #ifdef _WIN32 if (check_env_first) { if (const char* const home = getenv("ANDROID_SDK_HOME")) { return home; } } WCHAR path[MAX_PATH]; const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); if (FAILED(hr)) { D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str()); return {}; } std::string home_str; if (!android::base::WideToUTF8(path, &home_str)) { return {}; } return home_str; #else if (const char* const home = getenv("HOME")) { return home; } return {}; #endif } adb/adb_utils.h +6 −0 Original line number Original line Diff line number Diff line Loading @@ -32,6 +32,12 @@ bool directory_exists(const std::string& path); std::string adb_basename(const std::string& path); std::string adb_basename(const std::string& path); std::string adb_dirname(const std::string& path); std::string adb_dirname(const std::string& path); // Return the user's home directory. // |check_env_first| - if true, on Windows check the ANDROID_SDK_HOME // environment variable before trying the WinAPI call (useful when looking for // the .android directory) std::string adb_get_homedir_path(bool check_env_first); bool mkdirs(const std::string& path); bool mkdirs(const std::string& path); std::string escape_arg(const std::string& s); std::string escape_arg(const std::string& s); Loading adb/console.cpp +27 −2 Original line number Original line Diff line number Diff line Loading @@ -26,6 +26,31 @@ #include "adb.h" #include "adb.h" #include "adb_client.h" #include "adb_client.h" #include "adb_io.h" #include "adb_io.h" #include "adb_utils.h" // Return the console authentication command for the emulator, if needed static std::string adb_construct_auth_command() { static const char auth_token_filename[] = ".emulator_console_auth_token"; std::string auth_token_path = adb_get_homedir_path(false); auth_token_path += OS_PATH_SEPARATOR; auth_token_path += auth_token_filename; // read the token std::string token; if (!android::base::ReadFileToString(auth_token_path, &token) || token.empty()) { // we either can't read the file, or it doesn't exist, or it's empty - // either way we won't add any authentication command. return {}; } // now construct and return the actual command: "auth <token>\n" std::string command = "auth "; command += token; command += '\n'; return command; } // Return the console port of the currently connected emulator (if any) or -1 if // Return the console port of the currently connected emulator (if any) or -1 if // there is no emulator, and -2 if there is more than one. // there is no emulator, and -2 if there is more than one. Loading Loading @@ -88,11 +113,11 @@ int adb_send_emulator_command(int argc, const char** argv, const char* serial) { return 1; return 1; } } std::string commands; std::string commands = adb_construct_auth_command(); for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) { commands.append(argv[i]); commands.append(argv[i]); commands.append(i == argc - 1 ? "\n" : " "); commands.push_back(i == argc - 1 ? '\n' : ' '); } } commands.append("quit\n"); commands.append("quit\n"); Loading Loading
adb/adb_auth_host.cpp +8 −34 Original line number Original line Diff line number Diff line Loading @@ -18,18 +18,13 @@ #include "sysdeps.h" #include "sysdeps.h" #include "adb_auth.h" #include "adb_auth.h" #include "adb_utils.h" #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <string.h> #include <string.h> #ifdef _WIN32 #ifndef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include "windows.h" # include "shlobj.h" #else # include <sys/types.h> # include <sys/types.h> # include <sys/stat.h> # include <sys/stat.h> # include <unistd.h> # include <unistd.h> Loading Loading @@ -247,36 +242,14 @@ static int read_key(const char *file, struct listnode *list) static int get_user_keyfilepath(char *filename, size_t len) static int get_user_keyfilepath(char *filename, size_t len) { { const char *format, *home; char android_dir[PATH_MAX]; char android_dir[PATH_MAX]; struct stat buf; struct stat buf; #ifdef _WIN32 std::string home_str; home = getenv("ANDROID_SDK_HOME"); if (!home) { WCHAR path[MAX_PATH]; const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); if (FAILED(hr)) { D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str()); return -1; } if (!android::base::WideToUTF8(path, &home_str)) { return -1; } home = home_str.c_str(); } format = "%s\\%s"; #else home = getenv("HOME"); if (!home) return -1; format = "%s/%s"; #endif D("home '%s'", home); std::string home = adb_get_homedir_path(true); D("home '%s'", home.c_str()); if (snprintf(android_dir, sizeof(android_dir), format, home, if (snprintf(android_dir, sizeof(android_dir), "%s%c%s", home.c_str(), ANDROID_PATH) >= (int)sizeof(android_dir)) OS_PATH_SEPARATOR, ANDROID_PATH) >= (int)sizeof(android_dir)) return -1; return -1; if (stat(android_dir, &buf)) { if (stat(android_dir, &buf)) { Loading @@ -286,7 +259,8 @@ static int get_user_keyfilepath(char *filename, size_t len) } } } } return snprintf(filename, len, format, android_dir, ADB_KEY_FILE); return snprintf(filename, len, "%s%c%s", android_dir, OS_PATH_SEPARATOR, ADB_KEY_FILE); } } static int get_user_key(struct listnode *list) static int get_user_key(struct listnode *list) Loading
adb/adb_utils.cpp +35 −0 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,14 @@ #include "adb_trace.h" #include "adb_trace.h" #include "sysdeps.h" #include "sysdeps.h" #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include "windows.h" # include "shlobj.h" #endif ADB_MUTEX_DEFINE(basename_lock); ADB_MUTEX_DEFINE(basename_lock); ADB_MUTEX_DEFINE(dirname_lock); ADB_MUTEX_DEFINE(dirname_lock); Loading Loading @@ -254,3 +262,30 @@ bool forward_targets_are_valid(const std::string& source, const std::string& des return true; return true; } } std::string adb_get_homedir_path(bool check_env_first) { #ifdef _WIN32 if (check_env_first) { if (const char* const home = getenv("ANDROID_SDK_HOME")) { return home; } } WCHAR path[MAX_PATH]; const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); if (FAILED(hr)) { D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str()); return {}; } std::string home_str; if (!android::base::WideToUTF8(path, &home_str)) { return {}; } return home_str; #else if (const char* const home = getenv("HOME")) { return home; } return {}; #endif }
adb/adb_utils.h +6 −0 Original line number Original line Diff line number Diff line Loading @@ -32,6 +32,12 @@ bool directory_exists(const std::string& path); std::string adb_basename(const std::string& path); std::string adb_basename(const std::string& path); std::string adb_dirname(const std::string& path); std::string adb_dirname(const std::string& path); // Return the user's home directory. // |check_env_first| - if true, on Windows check the ANDROID_SDK_HOME // environment variable before trying the WinAPI call (useful when looking for // the .android directory) std::string adb_get_homedir_path(bool check_env_first); bool mkdirs(const std::string& path); bool mkdirs(const std::string& path); std::string escape_arg(const std::string& s); std::string escape_arg(const std::string& s); Loading
adb/console.cpp +27 −2 Original line number Original line Diff line number Diff line Loading @@ -26,6 +26,31 @@ #include "adb.h" #include "adb.h" #include "adb_client.h" #include "adb_client.h" #include "adb_io.h" #include "adb_io.h" #include "adb_utils.h" // Return the console authentication command for the emulator, if needed static std::string adb_construct_auth_command() { static const char auth_token_filename[] = ".emulator_console_auth_token"; std::string auth_token_path = adb_get_homedir_path(false); auth_token_path += OS_PATH_SEPARATOR; auth_token_path += auth_token_filename; // read the token std::string token; if (!android::base::ReadFileToString(auth_token_path, &token) || token.empty()) { // we either can't read the file, or it doesn't exist, or it's empty - // either way we won't add any authentication command. return {}; } // now construct and return the actual command: "auth <token>\n" std::string command = "auth "; command += token; command += '\n'; return command; } // Return the console port of the currently connected emulator (if any) or -1 if // Return the console port of the currently connected emulator (if any) or -1 if // there is no emulator, and -2 if there is more than one. // there is no emulator, and -2 if there is more than one. Loading Loading @@ -88,11 +113,11 @@ int adb_send_emulator_command(int argc, const char** argv, const char* serial) { return 1; return 1; } } std::string commands; std::string commands = adb_construct_auth_command(); for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) { commands.append(argv[i]); commands.append(argv[i]); commands.append(i == argc - 1 ? "\n" : " "); commands.push_back(i == argc - 1 ? '\n' : ' '); } } commands.append("quit\n"); commands.append("quit\n"); Loading