summaryrefslogtreecommitdiffstats
path: root/adb
diff options
context:
space:
mode:
authorJosh Gao <jmgao@google.com>2016-08-30 15:23:35 -0700
committerJosh Gao <jmgao@google.com>2016-09-01 15:43:22 -0700
commite0b7502c7fb12b0ac03a5903562c74820975b833 (patch)
treecf74eb30654a20c9ea4aab204597af6f596d5877 /adb
parent43824e72c7bccb934b8689af6abdb0552d420ce9 (diff)
downloadcore-e0b7502c7fb12b0ac03a5903562c74820975b833.tar.gz
core-e0b7502c7fb12b0ac03a5903562c74820975b833.tar.bz2
core-e0b7502c7fb12b0ac03a5903562c74820975b833.zip
adb: add helper to get the ~/.android directory.
Extract the logic for creating ~/.android out of get_user_key_path into its own function. Also, fall back to getpwuid_r when $HOME isn't defined. Change-Id: I676a7c750cb364f89b544818ffda07903d14fb97 Test: ran adb with ~/.android missing
Diffstat (limited to 'adb')
-rw-r--r--adb/adb_auth_host.cpp16
-rw-r--r--adb/adb_utils.cpp34
-rw-r--r--adb/adb_utils.h8
-rw-r--r--adb/console.cpp2
4 files changed, 33 insertions, 27 deletions
diff --git a/adb/adb_auth_host.cpp b/adb/adb_auth_host.cpp
index 4f4f38239..836654975 100644
--- a/adb/adb_auth_host.cpp
+++ b/adb/adb_auth_host.cpp
@@ -246,21 +246,7 @@ static bool read_keys(const std::string& path, bool allow_dir = true) {
}
static std::string get_user_key_path() {
- const std::string home = adb_get_homedir_path(true);
- LOG(DEBUG) << "adb_get_homedir_path returned '" << home << "'";
-
- const std::string android_dir = android::base::StringPrintf("%s%c.android", home.c_str(),
- OS_PATH_SEPARATOR);
-
- struct stat buf;
- if (stat(android_dir.c_str(), &buf) == -1) {
- if (adb_mkdir(android_dir.c_str(), 0750) == -1) {
- PLOG(ERROR) << "Cannot mkdir '" << android_dir << "'";
- return "";
- }
- }
-
- return android_dir + OS_PATH_SEPARATOR + "adbkey";
+ return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adbkey";
}
static bool get_user_key() {
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp
index 31ec8af2a..e138a9011 100644
--- a/adb/adb_utils.cpp
+++ b/adb/adb_utils.cpp
@@ -25,6 +25,7 @@
#include <unistd.h>
#include <algorithm>
+#include <vector>
#include <android-base/logging.h>
#include <android-base/parseint.h>
@@ -41,6 +42,8 @@
# endif
# include "windows.h"
# include "shlobj.h"
+#else
+#include <pwd.h>
#endif
ADB_MUTEX_DEFINE(basename_lock);
@@ -263,14 +266,8 @@ bool forward_targets_are_valid(const std::string& source, const std::string& des
return true;
}
-std::string adb_get_homedir_path(bool check_env_first) {
+std::string adb_get_homedir_path() {
#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)) {
@@ -286,6 +283,29 @@ std::string adb_get_homedir_path(bool check_env_first) {
if (const char* const home = getenv("HOME")) {
return home;
}
+
+ struct passwd pwent;
+ struct passwd* result;
+ int pwent_max = sysconf(_SC_GETPW_R_SIZE_MAX);
+ std::vector<char> buf(pwent_max);
+ int rc = getpwuid_r(getuid(), &pwent, buf.data(), buf.size(), &result);
+ if (rc == 0 && result) {
+ return result->pw_dir;
+ }
+
+ LOG(FATAL) << "failed to get user home directory";
return {};
#endif
}
+
+std::string adb_get_android_dir_path() {
+ std::string user_dir = adb_get_homedir_path();
+ std::string android_dir = user_dir + OS_PATH_SEPARATOR + ".android";
+ struct stat buf;
+ if (stat(android_dir.c_str(), &buf) == -1) {
+ if (adb_mkdir(android_dir.c_str(), 0750) == -1) {
+ PLOG(FATAL) << "Cannot mkdir '" << android_dir << "'";
+ }
+ }
+ return android_dir;
+}
diff --git a/adb/adb_utils.h b/adb/adb_utils.h
index f6b4b2636..d08c4119e 100644
--- a/adb/adb_utils.h
+++ b/adb/adb_utils.h
@@ -33,10 +33,10 @@ std::string adb_basename(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);
+std::string adb_get_homedir_path();
+
+// Return the adb user directory.
+std::string adb_get_android_dir_path();
bool mkdirs(const std::string& path);
diff --git a/adb/console.cpp b/adb/console.cpp
index e9b90a50a..9563eacb3 100644
--- a/adb/console.cpp
+++ b/adb/console.cpp
@@ -32,7 +32,7 @@
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);
+ std::string auth_token_path = adb_get_homedir_path();
auth_token_path += OS_PATH_SEPARATOR;
auth_token_path += auth_token_filename;